[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.
This commit is contained in:
Konstantin Käfer
2016-01-04 12:30:13 +01:00
parent 957415823a
commit 21e4029acf
10 changed files with 105 additions and 11 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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()) {

BIN
test/fixtures/stale/0-0-0.vector.pbf vendored Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

14
test/fixtures/stale/streets.json vendored Normal file
View File

@@ -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" ]
}

25
test/fixtures/stale/style.json vendored Normal file
View File

@@ -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"
}
}]
}

View File

@@ -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);
}

View File

@@ -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) {

View File

@@ -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',