summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-01-04 12:30:13 +0100
committerKonstantin Käfer <mail@kkaefer.com>2016-01-15 11:59:26 +0100
commit21e4029acf757898da8695474f94642f9858acd8 (patch)
tree567a10b18b4f1b573de5ae151018f6518244138a
parent957415823a003111f6efecd1a1552a30f999235a (diff)
downloadqtlocation-mapboxgl-21e4029acf757898da8695474f94642f9858acd8.tar.gz
[core] use stale styles
This adds support for using cached styles that are stale. They're treated like changing styles; when the refreshed style changed compared to the one we've already had, we're swapping out the entire style, which might cause a slight flicker.
-rwxr-xr-xplatform/linux/scripts/after_script.sh4
-rw-r--r--src/mbgl/map/map_context.cpp15
-rw-r--r--src/mbgl/style/style.cpp3
-rw-r--r--test/fixtures/stale/0-0-0.vector.pbfbin0 -> 482553 bytes
-rw-r--r--test/fixtures/stale/stale_style/expected.pngbin0 -> 13447 bytes
-rw-r--r--test/fixtures/stale/streets.json14
-rw-r--r--test/fixtures/stale/style.json25
-rw-r--r--test/storage/cache_stale.cpp51
-rwxr-xr-xtest/storage/server.js3
-rw-r--r--test/test.gypi1
10 files changed, 105 insertions, 11 deletions
diff --git a/platform/linux/scripts/after_script.sh b/platform/linux/scripts/after_script.sh
index f8e4bc3486..b5397f1df2 100755
--- a/platform/linux/scripts/after_script.sh
+++ b/platform/linux/scripts/after_script.sh
@@ -10,6 +10,6 @@ if [ ! -z "${AWS_ACCESS_KEY_ID}" ] && [ ! -z "${AWS_SECRET_ACCESS_KEY}" ] ; then
REPO_NAME=$(basename $TRAVIS_REPO_SLUG)
- aws s3 cp --recursive --acl public-read test/fixtures/annotations \
- s3://mapbox/$REPO_NAME/render-tests/$TRAVIS_JOB_NUMBER/annotations
+ aws s3 cp --recursive --acl public-read --exclude "*" --include "*/actual.png" test/fixtures \
+ s3://mapbox/$REPO_NAME/render-tests/$TRAVIS_JOB_NUMBER
fi
diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp
index 6f12e968bd..eb17e20aea 100644
--- a/src/mbgl/map/map_context.cpp
+++ b/src/mbgl/map/map_context.cpp
@@ -105,12 +105,6 @@ void MapContext::setStyleURL(const std::string& url) {
FileSource* fs = util::ThreadContext::getFileSource();
styleRequest = fs->request({ Resource::Kind::Style, styleURL }, [this, base](Response res) {
- if (res.stale) {
- // Only handle fresh responses.
- return;
- }
- styleRequest = nullptr;
-
if (res.error) {
if (res.error->reason == Response::Error::Reason::NotFound &&
util::mapbox::isMapboxURL(styleURL)) {
@@ -120,9 +114,11 @@ void MapContext::setStyleURL(const std::string& url) {
data.loading = false;
}
} else {
- loadStyleJSON(*res.data, base);
+ // We got a new stylesheet; only update when it's different from the previous one.
+ if (styleJSON != *res.data) {
+ loadStyleJSON(*res.data, base);
+ }
}
-
});
}
@@ -132,7 +128,7 @@ void MapContext::setStyleJSON(const std::string& json, const std::string& base)
}
styleURL.clear();
- styleJSON = json;
+ styleJSON.clear();
style = std::make_unique<Style>(data);
@@ -144,6 +140,7 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base)
style->setJSON(json, base);
style->setObserver(this);
+ styleJSON = json;
// force style cascade, causing all pending transitions to complete.
style->cascade();
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index fbca01311b..f64bdfc921 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -43,6 +43,9 @@ Style::Style(MapData& data_)
}
void Style::setJSON(const std::string& json, const std::string&) {
+ sources.clear();
+ layers.clear();
+
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
doc.Parse<0>((const char *const)json.c_str());
if (doc.HasParseError()) {
diff --git a/test/fixtures/stale/0-0-0.vector.pbf b/test/fixtures/stale/0-0-0.vector.pbf
new file mode 100644
index 0000000000..a0f049ad43
--- /dev/null
+++ b/test/fixtures/stale/0-0-0.vector.pbf
Binary files differ
diff --git a/test/fixtures/stale/stale_style/expected.png b/test/fixtures/stale/stale_style/expected.png
new file mode 100644
index 0000000000..d3c6ef3cd5
--- /dev/null
+++ b/test/fixtures/stale/stale_style/expected.png
Binary files differ
diff --git a/test/fixtures/stale/streets.json b/test/fixtures/stale/streets.json
new file mode 100644
index 0000000000..11cb5c8557
--- /dev/null
+++ b/test/fixtures/stale/streets.json
@@ -0,0 +1,14 @@
+{
+ "bounds": [ -180, -85.0511, 180, 85.0511 ],
+ "center": [ 0, 0, 0 ],
+ "description": "",
+ "format": "pbf",
+ "id": "streets",
+ "maskLevel": 8,
+ "maxzoom": 15,
+ "minzoom": 0,
+ "name": "Streets",
+ "scheme": "xyz",
+ "tilejson": "2.0.0",
+ "tiles": [ "asset://test/fixtures/stale/{z}-{x}-{y}.vector.pbf" ]
+}
diff --git a/test/fixtures/stale/style.json b/test/fixtures/stale/style.json
new file mode 100644
index 0000000000..ca86503d4f
--- /dev/null
+++ b/test/fixtures/stale/style.json
@@ -0,0 +1,25 @@
+{
+ "version": 8,
+ "name": "Water",
+ "sources": {
+ "mapbox": {
+ "type": "vector",
+ "url": "asset://test/fixtures/stale/streets.json"
+ }
+ },
+ "layers": [{
+ "id": "background",
+ "type": "background",
+ "paint": {
+ "background-color": "red"
+ }
+ }, {
+ "id": "water",
+ "type": "fill",
+ "source": "mapbox",
+ "source-layer": "water",
+ "paint": {
+ "fill-color": "blue"
+ }
+ }]
+}
diff --git a/test/storage/cache_stale.cpp b/test/storage/cache_stale.cpp
new file mode 100644
index 0000000000..cc421f7c34
--- /dev/null
+++ b/test/storage/cache_stale.cpp
@@ -0,0 +1,51 @@
+#include "storage.hpp"
+
+#include <mbgl/platform/default/headless_display.hpp>
+#include <mbgl/platform/default/headless_view.hpp>
+#include <mbgl/storage/sqlite_cache.hpp>
+#include <mbgl/storage/default_file_source.hpp>
+
+#include <mbgl/platform/log.hpp>
+#include <mbgl/util/work_request.hpp>
+#include <mbgl/util/io.hpp>
+
+using namespace mbgl;
+using namespace std::literals::chrono_literals;
+using namespace std::literals::string_literals;
+
+namespace {
+
+void checkRendering(Map& map, const char *name, std::chrono::milliseconds timeout) {
+ test::checkImage("test/fixtures/stale/"s + name, test::render(map, timeout), 0.001, 0.1);
+}
+
+Response expiredItem(const std::string& path) {
+ Response response;
+ response.data = std::make_shared<std::string>(util::read_file("test/fixtures/"s + path));
+ response.expires = 0s;
+ return response;
+}
+
+const std::string prefix = "http://127.0.0.1:3000";
+
+}
+
+auto display = std::make_shared<mbgl::HeadlessDisplay>();
+
+TEST_F(Storage, CacheStaleStyle) {
+ HeadlessView view(display, 1);
+
+ auto cache = SQLiteCache::getShared(":memory:");
+
+ // Rig the cache with an expired stylesheet.
+ const std::string stylePath = "stale/style.json";
+ const Resource styleResource{ Resource::Kind::Style, prefix + "/" + stylePath };
+ cache->put(styleResource, expiredItem(stylePath));
+
+ DefaultFileSource fileSource(":memory:", ".");
+
+ Map map(view, fileSource, MapMode::Still);
+ map.setStyleURL(styleResource.url);
+
+ checkRendering(map, "stale_style", 1000ms);
+}
diff --git a/test/storage/server.js b/test/storage/server.js
index 5d547b1506..0024330037 100755
--- a/test/storage/server.js
+++ b/test/storage/server.js
@@ -32,6 +32,9 @@ app.get('/test', function (req, res) {
res.send('Hello World!');
});
+app.get('/stale/*', function() {
+ // Never respond.
+});
var cacheCounter = 0;
app.get('/cache', function(req, res) {
diff --git a/test/test.gypi b/test/test.gypi
index 607dd2b76c..8b13db4c6b 100644
--- a/test/test.gypi
+++ b/test/test.gypi
@@ -72,6 +72,7 @@
'storage/cache_revalidate.cpp',
'storage/cache_shared.cpp',
'storage/cache_size.cpp',
+ 'storage/cache_stale.cpp',
'storage/database.cpp',
'storage/asset_file_source.cpp',
'storage/headers.cpp',