1 /***************************************************************************
2   qgstilecache.h
3   --------------------------------------
4   Date                 : September 2016
5   Copyright            : (C) 2016 by Martin Dobias
6   Email                : wonder dot sk at gmail dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 #ifndef QGSTILECACHE_H
17 #define QGSTILECACHE_H
18 
19 #include "qgis_core.h"
20 #include <QCache>
21 #include <QMutex>
22 
23 class QImage;
24 class QUrl;
25 
26 #define SIP_NO_FILE
27 
28 /**
29  * A simple tile cache implementation. Tiles are cached according to their URL.
30  * There is a small in-memory cache and a secondary caching in the local disk.
31  * The in-memory cache is there to save CPU time otherwise wasted to read and
32  * uncompress data saved on the disk.
33  *
34  * The class is thread safe (its methods can be called from any thread).
35  *
36  * \note Not available in Python bindings
37  * \ingroup core
38  * \since QGIS 3.8.0
39  */
40 class CORE_EXPORT QgsTileCache
41 {
42   public:
43 
44     //! Add a tile image with given URL to the cache
45     static void insertTile( const QUrl &url, const QImage &image );
46 
47     /**
48      * Try to access a tile and load it into "image" argument
49      * \returns TRUE if the tile exists in the cache
50      */
51     static bool tile( const QUrl &url, QImage &image );
52 
53     //! how many tiles are stored in the in-memory cache
totalCost()54     static int totalCost() { QMutexLocker locker( &sTileCacheMutex ); return sTileCache.totalCost(); }
55     //! how many tiles can be stored in the in-memory cache
maxCost()56     static int maxCost() { QMutexLocker locker( &sTileCacheMutex ); return sTileCache.maxCost(); }
57 
58   private:
59     //! in-memory cache
60     static QCache<QUrl, QImage> sTileCache;
61     //! mutex to protect the in-memory cache
62     static QMutex sTileCacheMutex;
63 };
64 
65 #endif // QGSTILECACHE_H
66