SourceXtractorPlusPlus  0.14
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 
116 
117  x = x / m_tile_width * m_tile_width;
118  y = y / m_tile_height * m_tile_height;
119 
120  TileKey key {std::static_pointer_cast<const ImageSource>(source), x, y};
121  auto it = m_tile_map.find(key);
122  if (it != m_tile_map.end()) {
123 #ifndef NDEBUG
124  //m_tile_logger.debug() << "Cache hit " << key;
125 #endif
126  return std::dynamic_pointer_cast<ImageTile>(it->second);
127  } else {
128  auto tile = source->getImageTile(x, y,
129  std::min(m_tile_width, source->getWidth()-x), std::min(m_tile_height, source->getHeight()-y));
130  addTile(key, std::static_pointer_cast<ImageTile>(tile));
132  return tile;
133  }
134  }
135 
137  if (s_instance == nullptr) {
138  s_instance = std::make_shared<TileManager>();
139  }
140  return s_instance;
141  }
142 
143  void saveAllTiles() {
145 
146  for (auto tile_key : m_tile_list) {
147  m_tile_map.at(tile_key)->saveIfModified();
148  }
149  }
150 
151  int getTileWidth() const {
152  return m_tile_width;
153  }
154 
155  int getTileHeight() const {
156  return m_tile_height;
157  }
158 
159 private:
160 
161  void removeTile(TileKey tile_key) {
162 #ifndef NDEBUG
163  //m_tile_logger.debug() << "Cache eviction " << tile_key;
164 #endif
165 
166  auto& tile = m_tile_map.at(tile_key);
167 
168  tile->saveIfModified();
169  m_total_memory_used -= tile->getTileMemorySize();
170 
171  m_tile_map.erase(tile_key);
172  }
173 
176  assert(m_tile_list.size() > 0);
177  auto tile_to_remove = m_tile_list.back();
178  removeTile(tile_to_remove);
179  m_tile_list.pop_back();
180  }
181  }
182 
184 #ifndef NDEBUG
185  //m_tile_logger.debug() << "Cache miss " << key;
186 #endif
187 
188  m_tile_map[key] = tile;
189  m_tile_list.push_front(key);
190  m_total_memory_used += tile->getTileMemorySize();
191  }
192 
196 
199 
201 
203 
205 };
206 
207 }
208 
209 
210 #endif /* _SEFRAMEWORK_IMAGE_TILEMANAGER_H_ */
std::list< TileKey > m_tile_list
Definition: TileManager.h:198
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:136
std::string getRepr() const
Definition: TileManager.h:50
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
static std::shared_ptr< TileManager > s_instance
Definition: TileManager.h:204
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
std::recursive_mutex m_mutex
Definition: TileManager.h:200
void removeTile(TileKey tile_key)
Definition: TileManager.h:161
STL class.
T min(T...args)
Elements::Logging m_tile_logger
Definition: TileManager.h:202
std::shared_ptr< ImageTile > getTileForPixel(int x, int y, std::shared_ptr< const ImageSource > source)
Definition: TileManager.h:114
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::unordered_map< TileKey, std::shared_ptr< ImageTile > > m_tile_map
Definition: TileManager.h:197
void addTile(TileKey key, std::shared_ptr< ImageTile > tile)
Definition: TileManager.h:183
STL class.
std::shared_ptr< const ImageSource > m_source
Definition: TileManager.h:43
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