summaryrefslogtreecommitdiff
path: root/src/mbgl
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-05-19 16:50:41 +0200
committerKonstantin Käfer <mail@kkaefer.com>2016-06-10 12:42:14 +0200
commit99b1df4e46e7fc110d479bc3efeb169e787e1c5e (patch)
tree665a534e62fa566825600d3a1174b1ff752ee679 /src/mbgl
parente8d8f52d2ea8b788a0dbe859549ec86fc0732df3 (diff)
downloadqtlocation-mapboxgl-99b1df4e46e7fc110d479bc3efeb169e787e1c5e.tar.gz
[core] introduce a RasterTileMonitor
Diffstat (limited to 'src/mbgl')
-rw-r--r--src/mbgl/style/source.cpp9
-rw-r--r--src/mbgl/tile/raster_tile_data.cpp71
-rw-r--r--src/mbgl/tile/raster_tile_data.hpp14
-rw-r--r--src/mbgl/tile/raster_tile_source.cpp31
-rw-r--r--src/mbgl/tile/raster_tile_source.hpp40
-rw-r--r--src/mbgl/tile/tile_data.cpp5
-rw-r--r--src/mbgl/tile/tile_data.hpp4
-rw-r--r--src/mbgl/tile/tile_source.hpp7
-rw-r--r--src/mbgl/tile/vector_tile_data.cpp11
-rw-r--r--src/mbgl/tile/vector_tile_data.hpp3
10 files changed, 139 insertions, 56 deletions
diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp
index 701fc2c8cc..477e34adb4 100644
--- a/src/mbgl/style/source.cpp
+++ b/src/mbgl/style/source.cpp
@@ -25,6 +25,7 @@
#include <mbgl/tile/vector_tile_source.hpp>
#include <mbgl/tile/geojson_tile_source.hpp>
#include <mbgl/tile/annotation_tile_source.hpp>
+#include <mbgl/tile/raster_tile_source.hpp>
#include <mbgl/tile/vector_tile_data.hpp>
#include <mbgl/tile/raster_tile_data.hpp>
@@ -202,9 +203,11 @@ std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledT
// If we don't find working tile data, we're just going to load it.
if (type == SourceType::Raster) {
- data = std::make_unique<RasterTileData>(overscaledTileID, parameters.pixelRatio,
- tileset->tiles.at(0), parameters.texturePool,
- parameters.worker, parameters.fileSource, callback);
+ std::unique_ptr<RasterTileSource> monitor;
+ monitor = std::make_unique<RasterTileSource>(overscaledTileID, parameters.pixelRatio, tileset->tiles.at(0), parameters.fileSource);
+
+ data = std::make_unique<RasterTileData>(overscaledTileID, std::move(monitor),
+ parameters.texturePool, parameters.worker, callback);
} else {
std::unique_ptr<GeometryTileSource> monitor;
diff --git a/src/mbgl/tile/raster_tile_data.cpp b/src/mbgl/tile/raster_tile_data.cpp
index 420cb7055e..1a5b78d7ff 100644
--- a/src/mbgl/tile/raster_tile_data.cpp
+++ b/src/mbgl/tile/raster_tile_data.cpp
@@ -1,5 +1,6 @@
#include <mbgl/tile/raster_tile_data.hpp>
#include <mbgl/style/source.hpp>
+#include <mbgl/tile/raster_tile_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
@@ -9,51 +10,51 @@
using namespace mbgl;
RasterTileData::RasterTileData(const OverscaledTileID& id_,
- float pixelRatio,
- const std::string& urlTemplate,
- gl::TexturePool &texturePool_,
+ std::unique_ptr<RasterTileSource> tileSource_,
+ gl::TexturePool& texturePool_,
Worker& worker_,
- FileSource& fileSource,
const std::function<void(std::exception_ptr)>& callback)
- : TileData(id_),
+ : TileData(id_, std::move(tileSource_)),
texturePool(texturePool_),
worker(worker_) {
- const Resource resource =
- Resource::tile(urlTemplate, pixelRatio, id.canonical.x, id.canonical.y, id.canonical.z);
- req = fileSource.request(resource, [callback, this](Response res) {
- if (res.error) {
- callback(std::make_exception_ptr(std::runtime_error(res.error->message)));
- } else if (res.notModified) {
- modified = res.modified;
- expires = res.expires;
- } else if (res.noContent) {
- availableData = DataAvailability::All;
- modified = res.modified;
- expires = res.expires;
+ auto rasterTileSource = reinterpret_cast<RasterTileSource*>(tileSource.get());
+ tileRequest = rasterTileSource->monitorTile([callback, this](std::exception_ptr err,
+ std::shared_ptr<const std::string> data,
+ optional<Timestamp> modified_,
+ optional<Timestamp> expires_) {
+ if (err) {
+ callback(err);
+ return;
+ }
+
+ modified = modified_;
+ expires = expires_;
+
+ if (!data) {
+ // This is a 404 response. We're treating these as empty tiles.
workRequest.reset();
+ availableData = DataAvailability::All;
bucket.reset();
- callback(nullptr);
- } else {
- modified = res.modified;
- expires = res.expires;
+ callback(err);
+ return;
+ }
+ workRequest.reset();
+ workRequest = worker.parseRasterTile(std::make_unique<RasterBucket>(texturePool), data, [this, callback] (RasterTileParseResult result) {
workRequest.reset();
- workRequest = worker.parseRasterTile(std::make_unique<RasterBucket>(texturePool), res.data, [this, callback] (RasterTileParseResult result) {
- workRequest.reset();
- std::exception_ptr error;
- if (result.is<std::unique_ptr<Bucket>>()) {
- bucket = std::move(result.get<std::unique_ptr<Bucket>>());
- } else {
- error = result.get<std::exception_ptr>();
- bucket.reset();
- }
+ std::exception_ptr error;
+ if (result.is<std::unique_ptr<Bucket>>()) {
+ bucket = std::move(result.get<std::unique_ptr<Bucket>>());
+ } else {
+ error = result.get<std::exception_ptr>();
+ bucket.reset();
+ }
- availableData = DataAvailability::All;
+ availableData = DataAvailability::All;
- callback(error);
- });
- }
+ callback(error);
+ });
});
}
@@ -66,6 +67,6 @@ Bucket* RasterTileData::getBucket(const style::Layer&) {
}
void RasterTileData::cancel() {
- req = nullptr;
+ tileRequest.reset();
workRequest.reset();
}
diff --git a/src/mbgl/tile/raster_tile_data.hpp b/src/mbgl/tile/raster_tile_data.hpp
index 62d21d28c4..5c522e6c75 100644
--- a/src/mbgl/tile/raster_tile_data.hpp
+++ b/src/mbgl/tile/raster_tile_data.hpp
@@ -5,7 +5,7 @@
namespace mbgl {
-class FileSource;
+class RasterTileSource;
class AsyncRequest;
namespace gl { class TexturePool; }
@@ -17,11 +17,9 @@ class Layer;
class RasterTileData : public TileData {
public:
RasterTileData(const OverscaledTileID&,
- float pixelRatio,
- const std::string& urlTemplate,
+ std::unique_ptr<RasterTileSource>,
gl::TexturePool&,
Worker&,
- FileSource&,
const std::function<void(std::exception_ptr)>& callback);
~RasterTileData();
@@ -31,9 +29,13 @@ public:
private:
gl::TexturePool& texturePool;
Worker& worker;
- std::unique_ptr<AsyncRequest> req;
- std::unique_ptr<Bucket> bucket;
+
+ std::unique_ptr<AsyncRequest> tileRequest;
std::unique_ptr<AsyncRequest> workRequest;
+
+ // Contains the Bucket object for the tile. Buckets are render
+ // objects and they get added by tile parsing operations.
+ std::unique_ptr<Bucket> bucket;
};
} // namespace mbgl
diff --git a/src/mbgl/tile/raster_tile_source.cpp b/src/mbgl/tile/raster_tile_source.cpp
new file mode 100644
index 0000000000..7220ce214e
--- /dev/null
+++ b/src/mbgl/tile/raster_tile_source.cpp
@@ -0,0 +1,31 @@
+#include <mbgl/tile/raster_tile_source.hpp>
+#include <mbgl/storage/file_source.hpp>
+
+namespace mbgl {
+
+RasterTileSource::RasterTileSource(const OverscaledTileID& tileID_,
+ float pixelRatio_,
+ const std::string& urlTemplate_,
+ FileSource& fileSource_)
+ : tileID(tileID_), pixelRatio(pixelRatio_), urlTemplate(urlTemplate_), fileSource(fileSource_) {
+}
+
+std::unique_ptr<AsyncRequest>
+RasterTileSource::monitorTile(const Callback& callback) {
+ const Resource resource = Resource::tile(urlTemplate, pixelRatio, tileID.canonical.x,
+ tileID.canonical.y, tileID.canonical.z);
+ return fileSource.request(resource, [callback, this](Response res) {
+ if (res.error) {
+ callback(std::make_exception_ptr(std::runtime_error(res.error->message)), nullptr,
+ res.modified, res.expires);
+ } else if (res.notModified) {
+ return;
+ } else if (res.noContent) {
+ callback(nullptr, nullptr, res.modified, res.expires);
+ } else {
+ callback(nullptr, res.data, res.modified, res.expires);
+ }
+ });
+}
+
+} // namespace mbgl
diff --git a/src/mbgl/tile/raster_tile_source.hpp b/src/mbgl/tile/raster_tile_source.hpp
new file mode 100644
index 0000000000..fc7a18d1a7
--- /dev/null
+++ b/src/mbgl/tile/raster_tile_source.hpp
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <mbgl/tile/tile_source.hpp>
+#include <mbgl/tile/tile_id.hpp>
+
+namespace mbgl {
+
+class FileSource;
+
+class RasterTileSource : public TileSource {
+public:
+ RasterTileSource(const OverscaledTileID&,
+ float pixelRatio,
+ const std::string& urlTemplate,
+ FileSource&);
+ virtual ~RasterTileSource() = default;
+
+ using Callback = std::function<void(std::exception_ptr,
+ std::shared_ptr<const std::string>,
+ optional<Timestamp> modified,
+ optional<Timestamp> expires)>;
+
+ /*
+ * Monitor the tile held by this object for changes. When the tile is loaded for the first time,
+ * or updates, the callback is executed. If an error occurs, the first parameter will be set.
+ * Otherwise it will be null. If there is no data for the requested tile, the second parameter
+ * will be null.
+ *
+ * To cease monitoring, release the returned Request.
+ */
+ std::unique_ptr<AsyncRequest> monitorTile(const Callback&);
+
+private:
+ OverscaledTileID tileID;
+ float pixelRatio;
+ std::string urlTemplate;
+ FileSource& fileSource;
+};
+
+} // namespace mbgl
diff --git a/src/mbgl/tile/tile_data.cpp b/src/mbgl/tile/tile_data.cpp
index e23ae2a727..9214bf98ee 100644
--- a/src/mbgl/tile/tile_data.cpp
+++ b/src/mbgl/tile/tile_data.cpp
@@ -1,11 +1,12 @@
#include <mbgl/tile/tile_data.hpp>
+#include <mbgl/tile/tile_source.hpp>
#include <mbgl/renderer/debug_bucket.hpp>
#include <mbgl/util/string.hpp>
namespace mbgl {
-TileData::TileData(const OverscaledTileID& id_)
- : id(id_) {
+TileData::TileData(const OverscaledTileID& id_, std::unique_ptr<TileSource> tileSource_)
+ : id(id_), tileSource(std::move(tileSource_)) {
}
TileData::~TileData() = default;
diff --git a/src/mbgl/tile/tile_data.hpp b/src/mbgl/tile/tile_data.hpp
index 5b6583faf8..d5b178fd24 100644
--- a/src/mbgl/tile/tile_data.hpp
+++ b/src/mbgl/tile/tile_data.hpp
@@ -19,6 +19,7 @@ namespace mbgl {
class Worker;
class DebugBucket;
class TransformState;
+class TileSource;
namespace style {
class Layer;
@@ -26,7 +27,7 @@ class Layer;
class TileData : private util::noncopyable {
public:
- TileData(const OverscaledTileID&);
+ TileData(const OverscaledTileID&, std::unique_ptr<TileSource>);
virtual ~TileData();
// Mark this tile as no longer needed and cancel any pending work.
@@ -63,6 +64,7 @@ public:
const OverscaledTileID id;
optional<Timestamp> modified;
optional<Timestamp> expires;
+ const std::unique_ptr<TileSource> tileSource;
// Contains the tile ID string for painting debug information.
std::unique_ptr<DebugBucket> debugBucket;
diff --git a/src/mbgl/tile/tile_source.hpp b/src/mbgl/tile/tile_source.hpp
index bbfd08eaf7..52601e9b5e 100644
--- a/src/mbgl/tile/tile_source.hpp
+++ b/src/mbgl/tile/tile_source.hpp
@@ -12,7 +12,12 @@ namespace mbgl {
class GeometryTile;
class AsyncRequest;
-class GeometryTileSource : private util::noncopyable {
+class TileSource : private util::noncopyable {
+public:
+ virtual ~TileSource() = default;
+};
+
+class GeometryTileSource : public TileSource {
public:
virtual ~GeometryTileSource() = default;
diff --git a/src/mbgl/tile/vector_tile_data.cpp b/src/mbgl/tile/vector_tile_data.cpp
index b23efb4273..3e189c1ba1 100644
--- a/src/mbgl/tile/vector_tile_data.cpp
+++ b/src/mbgl/tile/vector_tile_data.cpp
@@ -13,12 +13,12 @@
namespace mbgl {
VectorTileData::VectorTileData(const OverscaledTileID& id_,
- std::unique_ptr<GeometryTileSource> monitor_,
+ std::unique_ptr<GeometryTileSource> tileSource_,
std::string sourceID,
style::Style& style_,
const MapMode mode_,
const std::function<void(std::exception_ptr)>& callback)
- : TileData(id_),
+ : TileData(id_, std::move(tileSource_)),
style(style_),
worker(style_.workers),
tileWorker(id_,
@@ -27,10 +27,9 @@ VectorTileData::VectorTileData(const OverscaledTileID& id_,
*style_.glyphAtlas,
*style_.glyphStore,
obsolete,
- mode_),
- monitor(std::move(monitor_))
-{
- tileRequest = monitor->monitorTile([callback, this](std::exception_ptr err,
+ mode_) {
+ auto geometryTileSource = reinterpret_cast<GeometryTileSource*>(tileSource.get());
+ tileRequest = geometryTileSource->monitorTile([callback, this](std::exception_ptr err,
std::unique_ptr<GeometryTile> tile,
optional<Timestamp> modified_,
optional<Timestamp> expires_) {
diff --git a/src/mbgl/tile/vector_tile_data.hpp b/src/mbgl/tile/vector_tile_data.hpp
index e7ff5fbcc6..dae9966165 100644
--- a/src/mbgl/tile/vector_tile_data.hpp
+++ b/src/mbgl/tile/vector_tile_data.hpp
@@ -22,7 +22,7 @@ class Style;
class VectorTileData : public TileData {
public:
VectorTileData(const OverscaledTileID&,
- std::unique_ptr<GeometryTileSource> monitor,
+ std::unique_ptr<GeometryTileSource> tileSource,
std::string sourceID,
style::Style&,
const MapMode,
@@ -50,7 +50,6 @@ private:
Worker& worker;
TileWorker tileWorker;
- std::unique_ptr<GeometryTileSource> monitor;
std::unique_ptr<AsyncRequest> tileRequest;
std::unique_ptr<AsyncRequest> workRequest;