1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 #ifndef QGEOFILETILECACHE_P_H
37 #define QGEOFILETILECACHE_P_H
38 
39 //
40 //  W A R N I N G
41 //  -------------
42 //
43 // This file is not part of the Qt API.  It exists purely as an
44 // implementation detail.  This header file may change from version to
45 // version without notice, or even be removed.
46 //
47 // We mean it.
48 //
49 
50 #include <QtLocation/private/qlocationglobal_p.h>
51 
52 #include <QObject>
53 #include <QCache>
54 #include "qcache3q_p.h"
55 #include <QSet>
56 #include <QMutex>
57 #include <QTimer>
58 
59 #include "qgeotilespec_p.h"
60 #include "qgeotiledmappingmanagerengine_p.h"
61 #include "qabstractgeotilecache_p.h"
62 
63 #include <QImage>
64 
65 QT_BEGIN_NAMESPACE
66 
67 class QGeoMappingManager;
68 
69 class QGeoTile;
70 class QGeoCachedTileMemory;
71 class QGeoFileTileCache;
72 
73 class QPixmap;
74 class QThread;
75 
76 /* This would be internal to qgeofiletilecache.cpp except that the eviction
77  * policy can't be defined without it being concrete here */
78 class QGeoCachedTileDisk
79 {
80 public:
81     ~QGeoCachedTileDisk();
82 
83     QGeoTileSpec spec;
84     QString filename;
85     QString format;
86     QGeoFileTileCache *cache;
87 };
88 
89 /* Custom eviction policy for the disk cache, to avoid deleting all the files
90  * when the application closes */
91 class Q_LOCATION_PRIVATE_EXPORT QCache3QTileEvictionPolicy : public QCache3QDefaultEvictionPolicy<QGeoTileSpec,QGeoCachedTileDisk>
92 {
93 protected:
94     void aboutToBeRemoved(const QGeoTileSpec &key, QSharedPointer<QGeoCachedTileDisk> obj);
95     void aboutToBeEvicted(const QGeoTileSpec &key, QSharedPointer<QGeoCachedTileDisk> obj);
96 };
97 
98 class Q_LOCATION_PRIVATE_EXPORT QGeoFileTileCache : public QAbstractGeoTileCache
99 {
100     Q_OBJECT
101 public:
102     QGeoFileTileCache(const QString &directory = QString(), QObject *parent = 0);
103     ~QGeoFileTileCache();
104 
105     void setMaxDiskUsage(int diskUsage) override;
106     int maxDiskUsage() const override;
107     int diskUsage() const override;
108 
109     void setMaxMemoryUsage(int memoryUsage) override;
110     int maxMemoryUsage() const override;
111     int memoryUsage() const override;
112 
113     void setMinTextureUsage(int textureUsage) override;
114     void setExtraTextureUsage(int textureUsage) override;
115     int maxTextureUsage() const override;
116     int minTextureUsage() const override;
117     int textureUsage() const override;
118     void clearAll() override;
119     void clearMapId(const int mapId);
120     void setCostStrategyDisk(CostStrategy costStrategy) override;
121     CostStrategy costStrategyDisk() const override;
122     void setCostStrategyMemory(CostStrategy costStrategy) override;
123     CostStrategy costStrategyMemory() const override;
124     void setCostStrategyTexture(CostStrategy costStrategy) override;
125     CostStrategy costStrategyTexture() const override;
126 
127 
128     QSharedPointer<QGeoTileTexture> get(const QGeoTileSpec &spec) override;
129 
130     // can be called without a specific tileCache pointer
131     static void evictFromDiskCache(QGeoCachedTileDisk *td);
132     static void evictFromMemoryCache(QGeoCachedTileMemory *tm);
133 
134     void insert(const QGeoTileSpec &spec,
135                 const QByteArray &bytes,
136                 const QString &format,
137                 QAbstractGeoTileCache::CacheAreas areas = QAbstractGeoTileCache::AllCaches) override;
138 
139     static QString tileSpecToFilenameDefault(const QGeoTileSpec &spec, const QString &format, const QString &directory);
140     static QGeoTileSpec filenameToTileSpecDefault(const QString &filename);
141 
142 protected:
143     void init() override;
144     void printStats() override;
145     void loadTiles();
146 
147     QString directory() const;
148 
149     QSharedPointer<QGeoCachedTileDisk> addToDiskCache(const QGeoTileSpec &spec, const QString &filename);
150     bool addToDiskCache(const QGeoTileSpec &spec, const QString &filename, const QByteArray &bytes);
151     void addToMemoryCache(const QGeoTileSpec &spec, const QByteArray &bytes, const QString &format);
152     QSharedPointer<QGeoTileTexture> addToTextureCache(const QGeoTileSpec &spec, const QImage &image);
153     QSharedPointer<QGeoTileTexture> getFromMemory(const QGeoTileSpec &spec);
154     QSharedPointer<QGeoTileTexture> getFromDisk(const QGeoTileSpec &spec);
155 
156     virtual bool isTileBogus(const QByteArray &bytes) const;
157     virtual QString tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const;
158     virtual QGeoTileSpec filenameToTileSpec(const QString &filename) const;
159 
160     QCache3Q<QGeoTileSpec, QGeoCachedTileDisk, QCache3QTileEvictionPolicy > diskCache_;
161     QCache3Q<QGeoTileSpec, QGeoCachedTileMemory > memoryCache_;
162     QCache3Q<QGeoTileSpec, QGeoTileTexture > textureCache_;
163 
164     QString directory_;
165 
166     int minTextureUsage_;
167     int extraTextureUsage_;
168     CostStrategy costStrategyDisk_;
169     CostStrategy costStrategyMemory_;
170     CostStrategy costStrategyTexture_;
171     bool isDiskCostSet_;
172     bool isMemoryCostSet_;
173     bool isTextureCostSet_;
174 };
175 
176 QT_END_NAMESPACE
177 
178 #endif // QGEOFILETILECACHE_P_H
179