summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mbgl/sprite/sprite_store.cpp30
-rw-r--r--test/fixtures/stale/sprite.json10
-rw-r--r--test/fixtures/stale/sprite.pngbin0 -> 3351 bytes
-rw-r--r--test/fixtures/stale/stale_style_and_sprite/expected.pngbin0 -> 26859 bytes
-rw-r--r--test/fixtures/stale/style_and_sprite.json26
-rw-r--r--test/storage/cache_stale.cpp28
6 files changed, 80 insertions, 14 deletions
diff --git a/src/mbgl/sprite/sprite_store.cpp b/src/mbgl/sprite/sprite_store.cpp
index 0c3a7d3ba0..8f99ea7626 100644
--- a/src/mbgl/sprite/sprite_store.cpp
+++ b/src/mbgl/sprite/sprite_store.cpp
@@ -40,15 +40,17 @@ void SpriteStore::setURL(const std::string& url) {
FileSource* fs = util::ThreadContext::getFileSource();
loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL },
[this, jsonURL](Response res) {
- if (res.stale) {
- // Only handle fresh responses.
+ if (res.error) {
+ observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
+ }
+
+ if (res.notModified) {
+ // We got the same data back as last time. Abort early.
return;
}
- loader->jsonRequest = nullptr;
- if (res.error) {
- observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
- } else {
+ if (!loader->json || *loader->json != *res.data) {
+ // Only trigger a sprite loaded event we got new data.
loader->json = res.data;
emitSpriteLoadedIfComplete();
}
@@ -57,15 +59,16 @@ void SpriteStore::setURL(const std::string& url) {
loader->spriteRequest =
fs->request({ Resource::Kind::SpriteImage, spriteURL },
[this, spriteURL](Response res) {
- if (res.stale) {
- // Only handle fresh responses.
+ if (res.error) {
+ observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
+ }
+
+ if (res.notModified) {
+ // We got the same data back as last time. Abort early.
return;
}
- loader->spriteRequest = nullptr;
- if (res.error) {
- observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
- } else {
+ if (!loader->image || *loader->image != *res.data) {
loader->image = res.data;
emitSpriteLoadedIfComplete();
}
@@ -79,8 +82,7 @@ void SpriteStore::emitSpriteLoadedIfComplete() {
return;
}
- auto local = std::move(loader);
- auto result = parseSprite(*local->image, *local->json);
+ auto result = parseSprite(*loader->image, *loader->json);
if (result.is<Sprites>()) {
loaded = true;
setSprites(result.get<Sprites>());
diff --git a/test/fixtures/stale/sprite.json b/test/fixtures/stale/sprite.json
new file mode 100644
index 0000000000..e640365519
--- /dev/null
+++ b/test/fixtures/stale/sprite.json
@@ -0,0 +1,10 @@
+{
+ "noise": {
+ "x": 0,
+ "y": 0,
+ "width": 32,
+ "height": 32,
+ "pixelRatio": 1,
+ "sdf": false
+ }
+}
diff --git a/test/fixtures/stale/sprite.png b/test/fixtures/stale/sprite.png
new file mode 100644
index 0000000000..a02d8eb542
--- /dev/null
+++ b/test/fixtures/stale/sprite.png
Binary files differ
diff --git a/test/fixtures/stale/stale_style_and_sprite/expected.png b/test/fixtures/stale/stale_style_and_sprite/expected.png
new file mode 100644
index 0000000000..171599bf5c
--- /dev/null
+++ b/test/fixtures/stale/stale_style_and_sprite/expected.png
Binary files differ
diff --git a/test/fixtures/stale/style_and_sprite.json b/test/fixtures/stale/style_and_sprite.json
new file mode 100644
index 0000000000..61e3214a5e
--- /dev/null
+++ b/test/fixtures/stale/style_and_sprite.json
@@ -0,0 +1,26 @@
+{
+ "version": 8,
+ "name": "Water",
+ "sources": {
+ "mapbox": {
+ "type": "vector",
+ "url": "asset://test/fixtures/stale/streets.json"
+ }
+ },
+ "sprite": "http://127.0.0.1:3000/stale/sprite",
+ "layers": [{
+ "id": "background",
+ "type": "background",
+ "paint": {
+ "background-color": "red"
+ }
+ }, {
+ "id": "water",
+ "type": "fill",
+ "source": "mapbox",
+ "source-layer": "water",
+ "paint": {
+ "fill-pattern": "noise"
+ }
+ }]
+}
diff --git a/test/storage/cache_stale.cpp b/test/storage/cache_stale.cpp
index 7f379b78a2..ab9fe7b202 100644
--- a/test/storage/cache_stale.cpp
+++ b/test/storage/cache_stale.cpp
@@ -72,3 +72,31 @@ TEST_F(Storage, CacheStaleStyleAndTileJSON) {
checkRendering(map, "stale_style_and_tilejson", 1000ms);
}
+
+TEST_F(Storage, CacheStaleStyleAndSprite) {
+ HeadlessView view(display, 1);
+
+ auto cache = SQLiteCache::getShared(":memory:");
+
+ // Rig the cache with an expired stylesheet.
+ const std::string stylePath = "stale/style_and_sprite.json";
+ const Resource styleResource{ Resource::Kind::Style, prefix + "/" + stylePath };
+ cache->put(styleResource, expiredItem(stylePath));
+
+ // Rig the cache with an expired sprite JSON.
+ const std::string spritejsonPath = "stale/sprite.json";
+ const Resource spritejsonResource{ Resource::Kind::SpriteJSON, prefix + "/" + spritejsonPath };
+ cache->put(spritejsonResource, expiredItem(spritejsonPath));
+
+ // Rig the cache with an expired sprite JSON.
+ const std::string spriteimagePath = "stale/sprite.png";
+ const Resource spriteimageResource{ Resource::Kind::SpriteImage, prefix + "/" + spriteimagePath };
+ cache->put(spriteimageResource, expiredItem(spriteimagePath));
+
+ DefaultFileSource fileSource(":memory:", ".");
+
+ Map map(view, fileSource, MapMode::Still);
+ map.setStyleURL(styleResource.url);
+
+ checkRendering(map, "stale_style_and_sprite", 1000ms);
+}