SourceXtractorPlusPlus  0.12
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TileManager.h
Go to the documentation of this file.
1 
17 /*
18  * TileManager.h
19  *
20  * Created on: Feb 23, 2018
21  * Author: mschefer
22  */
23 
24 #ifndef _SEFRAMEWORK_IMAGE_TILEMANAGER_H_
25 #define _SEFRAMEWORK_IMAGE_TILEMANAGER_H_
26 
27 #include <iostream>
28 #include <thread>
29 #include <mutex>
30 
31 #include <list>
32 #include <unordered_map>
33 
34 #include <ElementsKernel/Logging.h>
35 
38 
39 namespace SourceXtractor {
40 
41 
42 struct TileKey {
45 
46  bool operator==(const TileKey& other) const {
47  return m_source == other.m_source && m_tile_x == other.m_tile_x && m_tile_y == other.m_tile_y;
48  }
49 
50  std::string getRepr() const {
52  str << m_source.get() << "[" << m_source->getRepr() << "] " << m_tile_x << "," << m_tile_y;
53  return str.str();
54  }
55 };
56 
57 inline std::ostream& operator << (std::ostream &out, const TileKey &tk) {
58  out << tk.getRepr();
59  return out;
60 }
61 
62 }
63 
64 namespace std {
65 
66 template <>
67 struct hash<SourceXtractor::TileKey>
68 {
70  std::size_t hash = 0;
71  boost::hash_combine(hash, key.m_source);
72  boost::hash_combine(hash, key.m_tile_x);
73  boost::hash_combine(hash, key.m_tile_y);
74  return hash;
75  }
76 };
77 
78 }
79 
80 namespace SourceXtractor {
81 
82 class TileManager {
83 public:
84 
85  TileManager() : m_tile_width(256), m_tile_height(256), m_max_memory(100 * 1024L * 1024L), m_total_memory_used(0),
86  m_tile_logger(Elements::Logging::getLogger("TileManager")) {
87  }
88 
89  virtual ~TileManager() {
90  saveAllTiles();
91  }
92 
93  // Actually not thread safe, call before starting the multi-threading
94  void setOptions(int tile_width, int tile_height, int max_memory) {
96 
97  flush();
98 
99  m_tile_width = tile_width;
100  m_tile_height = tile_height;
101  m_max_memory = max_memory*1024L*1024L;
102  }
103 
104  void flush() {
106 
107  // empty anything still stored in cache
108  saveAllTiles();
109  m_tile_list.clear();
110  m_tile_map.clear();
112  }
113 
114  template <typename T>
117 
118  x = x / m_tile_width * m_tile_width;
119  y = y / m_tile_height * m_tile_height;
120 
121  TileKey key {std::static_pointer_cast<const ImageSourceBase>(source), x, y};
122  auto it = m_tile_map.find(key);
123  if (it != m_tile_map.end()) {
124 #ifndef NDEBUG
125  m_tile_logger.debug() << "Cache hit " << key;
126 #endif
127  return std::dynamic_pointer_cast<ImageTile<T>>(it->second);
128  } else {
129  auto tile = source->getImageTile(x, y,
130  std::min(m_tile_width, source->getWidth()-x), std::min(m_tile_height, source->getHeight()-y));
131  addTile(key, std::static_pointer_cast<ImageTileBase>(tile));
133  return tile;
134  }
135  }
136 
138  if (s_instance == nullptr) {
139  s_instance = std::make_shared<TileManager>();
140  }
141  return s_instance;
142  }
143 
144  void saveAllTiles() {
146 
147  for (auto tile_key : m_tile_list) {
148  m_tile_map.at(tile_key)->saveIfModified();
149  }
150  }
151 
152  int getTileWidth() const {
153  return m_tile_width;
154  }
155 
156  int getTileHeight() const {
157  return m_tile_height;
158  }
159 
160 private:
161 
162  void removeTile(TileKey tile_key) {
163 #ifndef NDEBUG
164  m_tile_logger.debug() << "Cache eviction " << tile_key;
165 #endif
166 
167  auto& tile = m_tile_map.at(tile_key);
168 
169  tile->saveIfModified();
170  m_total_memory_used -= tile->getTileSize();
171 
172  m_tile_map.erase(tile_key);
173  }
174 
177  assert(m_tile_list.size() > 0);
178  auto tile_to_remove = m_tile_list.back();
179  removeTile(tile_to_remove);
180  m_tile_list.pop_back();
181  }
182  }
183 
185 #ifndef NDEBUG
186  m_tile_logger.debug() << "Cache miss " << key;
187 #endif
188 
189  m_tile_map[key] = tile;
190  m_tile_list.push_front(key);
191  m_total_memory_used += tile->getTileSize();
192  }
193 
197 
200 
202 
204 
206 };
207 
208 }
209 
210 
211 #endif /* _SEFRAMEWORK_IMAGE_TILEMANAGER_H_ */
std::list< TileKey > m_tile_list
Definition: TileManager.h:199
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:137
std::string getRepr() const
Definition: TileManager.h:50
std::unordered_map< TileKey, std::shared_ptr< ImageTileBase > > m_tile_map
Definition: TileManager.h:198
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
void addTile(TileKey key, std::shared_ptr< ImageTileBase > tile)
Definition: TileManager.h:184
static std::shared_ptr< TileManager > s_instance
Definition: TileManager.h:205
void debug(const std::string &logMessage)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
std::recursive_mutex m_mutex
Definition: TileManager.h:201
void removeTile(TileKey tile_key)
Definition: TileManager.h:162
STL class.
T min(T...args)
Elements::Logging m_tile_logger
Definition: TileManager.h:203
std::shared_ptr< const ImageSourceBase > m_source
Definition: TileManager.h:43
T lock(T...args)
T static_pointer_cast(T...args)
std::size_t operator()(const SourceXtractor::TileKey &key) const
Definition: TileManager.h:69
STL class.
std::ostream & operator<<(std::ostream &out, const TileKey &tk)
Definition: TileManager.h:57
std::shared_ptr< ImageTile< T > > getTileForPixel(int x, int y, std::shared_ptr< const ImageSource< T >> source)
Definition: TileManager.h:115
STL class.
bool operator==(const TileKey &other) const
Definition: TileManager.h:46
void setOptions(int tile_width, int tile_height, int max_memory)
Definition: TileManager.h:94