Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 8c58a98

Browse files
author
Asheem Mamoowala
committed
[core] Use Actors for CustomTileLoader invocation from bindings.
1 parent 78788dd commit 8c58a98

File tree

5 files changed

+13
-22
lines changed

5 files changed

+13
-22
lines changed

include/mbgl/style/sources/custom_geometry_source.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#include <mbgl/util/geojson.hpp>
66
#include <mbgl/util/range.hpp>
77
#include <mbgl/util/constants.hpp>
8-
#include <mbgl/actor/mailbox.hpp>
98

109
namespace mbgl {
1110

1211
class OverscaledTileID;
1312
class CanonicalTileID;
13+
template <class T>
14+
class Actor;
1415

1516
namespace style {
1617

@@ -43,8 +44,7 @@ class CustomGeometrySource : public Source {
4344
class Impl;
4445
const Impl& impl() const;
4546
private:
46-
std::shared_ptr<Mailbox> mailbox;
47-
std::unique_ptr<CustomTileLoader> loader;
47+
std::unique_ptr<Actor<CustomTileLoader>> loader;
4848
};
4949

5050
template <>

src/mbgl/style/custom_tile_loader.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ CustomTileLoader::CustomTileLoader(const TileFunction& fetchTileFn, const TileFu
99
}
1010

1111
void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) {
12-
std::lock_guard<std::mutex> lock(dataCacheMutex);
1312
auto cachedTileData = dataCache.find(tileID.canonical);
1413
if (cachedTileData != dataCache.end()) {
1514
callbackRef.invoke(&SetTileDataFunction::operator(), *(cachedTileData->second));
@@ -49,14 +48,12 @@ void CustomTileLoader::removeTile(const OverscaledTileID& tileID) {
4948
}
5049
}
5150
if (tileCallbacks->second.size() == 0) {
52-
std::lock_guard<std::mutex> lock(dataCacheMutex);
5351
tileCallbackMap.erase(tileCallbacks);
5452
dataCache.erase(tileID.canonical);
5553
}
5654
}
5755

5856
void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const GeoJSON& data) {
59-
std::lock_guard<std::mutex> lock(dataCacheMutex);
6057

6158
auto iter = tileCallbackMap.find(tileID);
6259
if (iter == tileCallbackMap.end()) return;
@@ -76,18 +73,14 @@ void CustomTileLoader::invalidateTile(const CanonicalTileID& tileID) {
7673
invokeTileCancel(tileID);
7774
}
7875
tileCallbackMap.erase(tileCallbacks);
79-
{
80-
std::lock_guard<std::mutex> lock(dataCacheMutex);
81-
dataCache.erase(tileID);
82-
}
76+
dataCache.erase(tileID);
8377
}
8478

8579
void CustomTileLoader::invalidateRegion(const LatLngBounds& bounds, Range<uint8_t> ) {
8680
for(auto idtuple= tileCallbackMap.begin(); idtuple != tileCallbackMap.end(); idtuple++) {
8781
const LatLngBounds tileBounds(idtuple->first);
8882
if (tileBounds.intersects(bounds) || bounds.contains(tileBounds) || tileBounds.contains(bounds)) {
8983
for (auto iter = idtuple->second.begin(); iter != idtuple->second.end(); iter++) {
90-
std::lock_guard<std::mutex> lock(dataCacheMutex);
9184
auto actor = std::get<2>(*iter);
9285
actor.invoke(&SetTileDataFunction::operator(), mapbox::geojson::feature_collection());
9386
invokeTileCancel(idtuple->first);

src/mbgl/style/custom_tile_loader.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <mbgl/actor/actor_ref.hpp>
88

99
#include <map>
10-
#include <mutex>
1110

1211
namespace mbgl {
1312
namespace style {
@@ -39,7 +38,6 @@ class CustomTileLoader : private util::noncopyable {
3938
std::unordered_map<CanonicalTileID, std::vector<OverscaledIDFunctionTuple>> tileCallbackMap;
4039
// Keep around a cache of tile data to serve back for wrapped and over-zooomed tiles
4140
std::map<CanonicalTileID, std::unique_ptr<GeoJSON>> dataCache;
42-
std::mutex dataCacheMutex;
4341

4442
};
4543

src/mbgl/style/sources/custom_geometry_source.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <mbgl/style/sources/custom_geometry_source.hpp>
22
#include <mbgl/style/custom_tile_loader.hpp>
33
#include <mbgl/style/sources/custom_geometry_source_impl.hpp>
4+
#include <mbgl/actor/actor.hpp>
45
#include <mbgl/actor/scheduler.hpp>
56
#include <mbgl/tile/tile_id.hpp>
6-
7+
#include <mbgl/util/shared_thread_pool.hpp>
78
#include <tuple>
89
#include <map>
910

@@ -13,8 +14,7 @@ namespace style {
1314
CustomGeometrySource::CustomGeometrySource(std::string id,
1415
const CustomGeometrySource::Options options)
1516
: Source(makeMutable<CustomGeometrySource::Impl>(std::move(id), options)),
16-
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
17-
loader(std::make_unique<CustomTileLoader>(options.fetchTileFunction, options.cancelTileFunction)) {
17+
loader(std::make_unique<Actor<CustomTileLoader>>(*sharedThreadPool(), options.fetchTileFunction, options.cancelTileFunction)) {
1818
}
1919

2020
CustomGeometrySource::~CustomGeometrySource() = default;
@@ -24,21 +24,21 @@ const CustomGeometrySource::Impl& CustomGeometrySource::impl() const {
2424
}
2525

2626
void CustomGeometrySource::loadDescription(FileSource&) {
27-
baseImpl = makeMutable<CustomGeometrySource::Impl>(impl(), ActorRef<CustomTileLoader>(*loader, mailbox));
27+
baseImpl = makeMutable<CustomGeometrySource::Impl>(impl(), loader->self());
2828
loaded = true;
2929
}
3030

3131
void CustomGeometrySource::setTileData(const CanonicalTileID& tileID,
3232
const GeoJSON& data) {
33-
loader->setTileData(tileID, data);
33+
loader->invoke(&CustomTileLoader::setTileData, tileID, data);
3434
}
3535

3636
void CustomGeometrySource::invalidateTile(const CanonicalTileID& tileID) {
37-
loader->invalidateTile(tileID);
37+
loader->invoke(&CustomTileLoader::invalidateTile, tileID);
3838
}
3939

4040
void CustomGeometrySource::invalidateRegion(const LatLngBounds& bounds) {
41-
loader->invalidateRegion(bounds, impl().getZoomRange());
41+
loader->invoke(&CustomTileLoader::invalidateRegion, bounds, impl().getZoomRange());
4242
}
4343

4444
} // namespace style

test/style/source.test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <mbgl/util/image.hpp>
2626

2727
#include <mbgl/util/tileset.hpp>
28-
#include <mbgl/util/default_thread_pool.hpp>
28+
#include <mbgl/util/shared_thread_pool.hpp>
2929
#include <mbgl/util/logging.hpp>
3030
#include <mbgl/util/optional.hpp>
3131
#include <mbgl/util/range.hpp>
@@ -552,7 +552,7 @@ TEST(Source, ImageSourceImageUpdate) {
552552

553553
TEST(Source, CustomGeometrySourceSetTileData) {
554554
SourceTest test;
555-
555+
std::shared_ptr<ThreadPool> threadPool = sharedThreadPool();
556556
CustomGeometrySource source("source", CustomGeometrySource::Options());
557557
source.loadDescription(test.fileSource);
558558

0 commit comments

Comments
 (0)