libosmscout  1.1.1
TileCache.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_CLIENT_QT_TILECACHE_H
2 #define OSMSCOUT_CLIENT_QT_TILECACHE_H
3 
4 /*
5  OSMScout - a Qt backend for libosmscout and libosmscout-map
6  Copyright (C) 2016 Lukas Karas
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22 
23 
24 #include <QObject>
25 #include <QString>
26 #include <QMetaType>
27 #include <QMutex>
28 #include <QHash>
29 #include <QMap>
30 #include <QSet>
31 #include <QTime>
32 #include <QElapsedTimer>
33 #include <QImage>
34 #include <QPixmap>
35 #include <QDebug>
36 
37 #include <osmscout/util/GeoBox.h>
39 
40 //#define DEBUG_TILE_CACHE
41 
42 namespace osmscout {
43 
53 {
54  uint32_t zoomLevel;
55  uint32_t xtile;
56  uint32_t ytile;
57 };
58 
59 bool operator==(const TileCacheKey &a, const TileCacheKey &b);
60 
61 bool operator<(const TileCacheKey &a, const TileCacheKey &b);
62 
63 uint qHash(const TileCacheKey &key);
64 
65 QDebug& operator<<(QDebug &out, const TileCacheKey &key);
66 
71 {
72  QElapsedTimer lastAccess;
73  QPixmap image;
74  size_t epoch;
75 };
76 
81 {
82  bool pending;
83 };
84 
91 class OSMSCOUT_CLIENT_QT_API TileCache : public QObject
92 {
93  Q_OBJECT
94 
95 signals:
96  void tileRequested(uint32_t zoomLevel, uint32_t x, uint32_t y);
97 
98 public:
99  TileCache(size_t cacheSize);
100  ~TileCache() override;
101 
107  void clearPendingRequests();
108  bool startRequestProcess(uint32_t zoomLevel, uint32_t x, uint32_t y);
109  void mergeAndStartRequests(uint32_t zoomLevel, uint32_t xtile, uint32_t ytile,
110  uint32_t &xFrom, uint32_t &xTo, uint32_t &yFrom, uint32_t &yTo, uint32_t maxWidth, uint32_t maxHeight);
111  bool isRequestQueueEmpty() const;
112 
118  bool request(uint32_t zoomLevel, uint32_t x, uint32_t y);
119 
123  bool reemitRequests();
124  bool contains(uint32_t zoomLevel, uint32_t x, uint32_t y);
125  bool containsRequest(uint32_t zoomLevel, uint32_t x, uint32_t y);
126  TileCacheVal get(uint32_t zoomLevel, uint32_t x, uint32_t y);
127 
133  bool invalidate(osmscout::GeoBox box = osmscout::GeoBox());
134 
143  bool removeRequest(uint32_t zoomLevel, uint32_t x, uint32_t y);
144  void put(uint32_t zoomLevel, uint32_t x, uint32_t y, QImage image, size_t epoch = 0);
145 
146  void cleanupCache();
147 
148  inline size_t getEpoch() const
149  {
150  return epoch;
151  }
152 
153  inline void incEpoch()
154  {
155  epoch ++;
156  }
157 
158 private:
159  QHash<TileCacheKey, TileCacheVal> tiles;
160  QHash<TileCacheKey, RequestState> requests;
161  size_t cacheSize; // maximum count of elements in cache
162  uint32_t maximumLivetimeMs;
163  size_t epoch{0};
164 };
165 
166 }
167 
168 Q_DECLARE_METATYPE(osmscout::TileCacheKey)
169 Q_DECLARE_METATYPE(osmscout::TileCacheVal)
170 Q_DECLARE_METATYPE(osmscout::RequestState)
171 
172 #endif /* OSMSCOUT_CLIENT_QT_TILECACHE_H */
uint32_t ytile
Definition: TileCache.h:56
#define OSMSCOUT_CLIENT_QT_API
Definition: ClientQtImportExport.h:45
size_t getEpoch() const
Definition: TileCache.h:148
QElapsedTimer lastAccess
Definition: TileCache.h:72
std::ostream & operator<<(std::ostream &stream, const DBId &o)
Definition: DBFileOffset.h:80
Definition: TileCache.h:52
uint32_t zoomLevel
Definition: TileCache.h:54
uint32_t xtile
Definition: TileCache.h:55
uint qHash(const TileCacheKey &key)
Definition: Area.h:38
Definition: TileCache.h:80
bool operator<(const TileCacheKey &a, const TileCacheKey &b)
Definition: TileCache.h:70
bool operator==(const MapView &a, const MapView &b)
Definition: InputHandler.h:222
Definition: TileCache.h:91
void incEpoch()
Definition: TileCache.h:153
bool pending
if pending is false, request is currently processing
Definition: TileCache.h:82
QPixmap image
Definition: TileCache.h:73
size_t epoch
Definition: TileCache.h:74