1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Use parallel ENC maps data loading in ENC atlases

This commit is contained in:
Martin Tůma 2024-06-09 13:34:13 +02:00
parent 8b51f3ff04
commit 1186cb104b
4 changed files with 66 additions and 33 deletions

View File

@ -2,45 +2,71 @@
using namespace ENC; using namespace ENC;
bool AtlasData::pointCb(const QString *map, void *context) bool AtlasData::pointCb(MapEntry *map, void *context)
{ {
PointCTX *ctx = (PointCTX*)context; PointCTX *ctx = (PointCTX*)context;
ctx->lock.lock(); start:
ctx->cacheLock.lock();
MapData *cached = ctx->cache.object(map); MapData *cached = ctx->cache.object(map->path);
if (!cached) { if (!cached) {
MapData *data = new MapData(*map); ctx->cacheLock.unlock();
if (map->lock.tryLock()) {
MapData *data = new MapData(map->path);
data->points(ctx->rect, ctx->points); data->points(ctx->rect, ctx->points);
ctx->cache.insert(map, data);
ctx->cacheLock.lock();
ctx->cache.insert(map->path, data);
map->lock.unlock();
} else {
map->lock.lock();
map->lock.unlock();
goto start;
}
} else } else
cached->points(ctx->rect, ctx->points); cached->points(ctx->rect, ctx->points);
ctx->lock.unlock(); ctx->cacheLock.unlock();
return true; return true;
} }
bool AtlasData::polyCb(const QString *map, void *context) bool AtlasData::polyCb(MapEntry *map, void *context)
{ {
PolyCTX *ctx = (PolyCTX*)context; PolyCTX *ctx = (PolyCTX*)context;
ctx->lock.lock(); start:
ctx->cacheLock.lock();
MapData *cached = ctx->cache.object(map); MapData *cached = ctx->cache.object(map->path);
if (!cached) { if (!cached) {
MapData *data = new MapData(*map); ctx->cacheLock.unlock();
if (map->lock.tryLock()) {
MapData *data = new MapData(map->path);
data->polygons(ctx->rect, ctx->polygons); data->polygons(ctx->rect, ctx->polygons);
data->lines(ctx->rect, ctx->lines); data->lines(ctx->rect, ctx->lines);
ctx->cache.insert(map, data);
ctx->cacheLock.lock();
ctx->cache.insert(map->path, data);
map->lock.unlock();
} else {
map->lock.lock();
map->lock.unlock();
goto start;
}
} else { } else {
cached->polygons(ctx->rect, ctx->polygons); cached->polygons(ctx->rect, ctx->polygons);
cached->lines(ctx->rect, ctx->lines); cached->lines(ctx->rect, ctx->lines);
} }
ctx->lock.unlock(); ctx->cacheLock.unlock();
return true; return true;
} }
@ -61,14 +87,14 @@ void AtlasData::addMap(const RectC &bounds, const QString &path)
max[0] = bounds.right(); max[0] = bounds.right();
max[1] = bounds.top(); max[1] = bounds.top();
_tree.Insert(min, max, new QString(path)); _tree.Insert(min, max, new MapEntry(path));
} }
void AtlasData::polys(const RectC &rect, QList<MapData::Poly> *polygons, void AtlasData::polys(const RectC &rect, QList<MapData::Poly> *polygons,
QList<MapData::Line> *lines) QList<MapData::Line> *lines)
{ {
double min[2], max[2]; double min[2], max[2];
PolyCTX polyCtx(rect, polygons, lines, _cache, _lock); PolyCTX polyCtx(rect, polygons, lines, _cache, _cacheLock);
min[0] = rect.left(); min[0] = rect.left();
min[1] = rect.bottom(); min[1] = rect.bottom();
@ -81,7 +107,7 @@ void AtlasData::polys(const RectC &rect, QList<MapData::Poly> *polygons,
void AtlasData::points(const RectC &rect, QList<MapData::Point> *points) void AtlasData::points(const RectC &rect, QList<MapData::Point> *points)
{ {
double min[2], max[2]; double min[2], max[2];
PointCTX pointCtx(rect, points, _cache, _lock); PointCTX pointCtx(rect, points, _cache, _cacheLock);
min[0] = rect.left(); min[0] = rect.left();
min[1] = rect.bottom(); min[1] = rect.bottom();

View File

@ -8,13 +8,13 @@
namespace ENC { namespace ENC {
typedef QCache<const QString*, MapData> MapCache; typedef QCache<QString, MapData> MapCache;
class AtlasData class AtlasData
{ {
public: public:
AtlasData(MapCache &cache, QMutex &lock) AtlasData(MapCache &cache, QMutex &cacheLock)
: _cache(cache), _lock(lock) {} : _cache(cache), _cacheLock(cacheLock) {}
~AtlasData(); ~AtlasData();
void addMap(const RectC &bounds, const QString &path); void addMap(const RectC &bounds, const QString &path);
@ -24,40 +24,47 @@ public:
void points(const RectC &rect, QList<MapData::Point> *points); void points(const RectC &rect, QList<MapData::Point> *points);
private: private:
typedef RTree<const QString*, double, 2> MapTree; struct MapEntry {
MapEntry(const QString &path) : path(path) {}
QString path;
QMutex lock;
};
typedef RTree<MapEntry*, double, 2> MapTree;
struct PolyCTX struct PolyCTX
{ {
PolyCTX(const RectC &rect, QList<MapData::Poly> *polygons, PolyCTX(const RectC &rect, QList<MapData::Poly> *polygons,
QList<MapData::Line> *lines, MapCache &cache, QMutex &lock) QList<MapData::Line> *lines, MapCache &cache, QMutex &cacheLock)
: rect(rect), polygons(polygons), lines(lines), cache(cache), : rect(rect), polygons(polygons), lines(lines), cache(cache),
lock(lock) {} cacheLock(cacheLock) {}
const RectC &rect; const RectC &rect;
QList<MapData::Poly> *polygons; QList<MapData::Poly> *polygons;
QList<MapData::Line> *lines; QList<MapData::Line> *lines;
MapCache &cache; MapCache &cache;
QMutex &lock; QMutex &cacheLock;
}; };
struct PointCTX struct PointCTX
{ {
PointCTX(const RectC &rect, QList<MapData::Point> *points, PointCTX(const RectC &rect, QList<MapData::Point> *points,
MapCache &cache, QMutex &lock) : rect(rect), points(points), MapCache &cache, QMutex &cacheLock)
cache(cache), lock(lock) {} : rect(rect), points(points), cache(cache), cacheLock(cacheLock) {}
const RectC &rect; const RectC &rect;
QList<MapData::Point> *points; QList<MapData::Point> *points;
MapCache &cache; MapCache &cache;
QMutex &lock; QMutex &cacheLock;
}; };
static bool polyCb(const QString *map, void *context); static bool polyCb(MapEntry *map, void *context);
static bool pointCb(const QString *map, void *context); static bool pointCb(MapEntry *map, void *context);
MapTree _tree; MapTree _tree;
MapCache &_cache; MapCache &_cache;
QMutex &_lock; QMutex &_cacheLock;
}; };
} }

View File

@ -113,7 +113,7 @@ void ENCAtlas::addMap(const QDir &dir, const QByteArray &file,
IntendedUsage iu = usage(path); IntendedUsage iu = usage(path);
auto it = _data.find(iu); auto it = _data.find(iu);
if (it == _data.end()) if (it == _data.end())
it = _data.insert(iu, new AtlasData(_cache, _lock)); it = _data.insert(iu, new AtlasData(_cache, _cacheLock));
it.value()->addMap(bounds, path); it.value()->addMap(bounds, path);

View File

@ -89,7 +89,7 @@ private:
QMap<IntendedUsage, ENC::AtlasData*> _data; QMap<IntendedUsage, ENC::AtlasData*> _data;
ENC::Style *_style; ENC::Style *_style;
ENC::MapCache _cache; ENC::MapCache _cache;
QMutex _lock; QMutex _cacheLock;
IntendedUsage _usage; IntendedUsage _usage;
int _zoom; int _zoom;