Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
119 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*************************************************************************** | ||
qgstilecache.h | ||
-------------------------------------- | ||
Date : September 2016 | ||
Copyright : (C) 2016 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgstilecache.h" | ||
|
||
#include "qgsnetworkaccessmanager.h" | ||
|
||
#include <QAbstractNetworkCache> | ||
#include <QImage> | ||
|
||
QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 ); | ||
QMutex QgsTileCache::sTileCacheMutex; | ||
|
||
|
||
void QgsTileCache::insertTile( const QUrl& url, const QImage& image ) | ||
{ | ||
QMutexLocker locker( &sTileCacheMutex ); | ||
sTileCache.insert( url, new QImage( image ) ); | ||
} | ||
|
||
bool QgsTileCache::tile( const QUrl& url, QImage& image ) | ||
{ | ||
QMutexLocker locker( &sTileCacheMutex ); | ||
if ( QImage* i = sTileCache.object( url ) ) | ||
{ | ||
image = *i; | ||
return true; | ||
} | ||
else if ( QgsNetworkAccessManager::instance()->cache()->metaData( url ).isValid() ) | ||
{ | ||
if ( QIODevice* data = QgsNetworkAccessManager::instance()->cache()->data( url ) ) | ||
{ | ||
QByteArray imageData = data->readAll(); | ||
delete data; | ||
|
||
image = QImage::fromData( imageData ); | ||
|
||
// cache it as well (mutex is already locked) | ||
sTileCache.insert( url, new QImage( image ) ); | ||
|
||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*************************************************************************** | ||
qgstilecache.h | ||
-------------------------------------- | ||
Date : September 2016 | ||
Copyright : (C) 2016 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSTILECACHE_H | ||
#define QGSTILECACHE_H | ||
|
||
|
||
#include <QCache> | ||
#include <QMutex> | ||
|
||
class QImage; | ||
class QUrl; | ||
|
||
/** A simple tile cache implementation. Tiles are cached according to their URL. | ||
* There is a small in-memory cache and a secondary caching in the local disk. | ||
* The in-memory cache is there to save CPU time otherwise wasted to read and | ||
* uncompress data saved on the disk. | ||
* | ||
* The class is thread safe (its methods can be called from any thread). | ||
*/ | ||
class QgsTileCache | ||
{ | ||
public: | ||
|
||
//! Add a tile image with given URL to the cache | ||
static void insertTile( const QUrl& url, const QImage& image ); | ||
|
||
//! Try to access a tile and load it into "image" argument | ||
//! @returns true if the tile exists in the cache | ||
static bool tile( const QUrl& url, QImage& image ); | ||
|
||
//! how many tiles are stored in the in-memory cache | ||
static int totalCost() { return sTileCache.totalCost(); } | ||
//! how many tiles can be stored in the in-memory cache | ||
static int maxCost() { return sTileCache.maxCost(); } | ||
|
||
private: | ||
//! in-memory cache | ||
static QCache<QUrl, QImage> sTileCache; | ||
//! mutex to protect the in-memory cache | ||
static QMutex sTileCacheMutex; | ||
}; | ||
|
||
#endif // QGSTILECACHE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters