scope Requests to an Environment object for easier cancelation

we are now scoping all file requests to an environment object. The FileSource implementation treats
this as an opaque pointer, but allows canceling all Requests that are associated with that pointer.
This is necessary to abort all file requests that originated from a particular Map object. Aborting
a file request is different from canceling a file request: A canceled request doesn't have its
callback called, while an aborted request will have its callback called with an error, indicating
that the environment is going to be shut down.
This commit is contained in:
Konstantin Käfer
2015-03-04 12:32:14 +01:00
committed by Konstantin Käfer
parent 8c0acecbe3
commit 9781785ab7
41 changed files with 485 additions and 201 deletions

View File

@@ -1,12 +1,14 @@
#include <mbgl/map/source.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/environment.hpp>
#include <mbgl/map/transform.hpp>
#include <mbgl/renderer/painter.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/raster.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/texture_pool.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/vec.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/std.hpp>
@@ -32,7 +34,7 @@ Source::Source(SourceInfo& info_)
// Note: This is a separate function that must be called exactly once after creation
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
void Source::load(Map& map, FileSource& fileSource) {
void Source::load(Map &map, Environment &env) {
if (info.url.empty()) {
loaded = true;
return;
@@ -41,7 +43,7 @@ void Source::load(Map& map, FileSource& fileSource) {
util::ptr<Source> source = shared_from_this();
const std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken());
fileSource.request({ Resource::Kind::JSON, url }, **map.loop, [source, &map](const Response &res) {
env.request({ Resource::Kind::JSON, url }, [source, &map](const Response &res) {
if (res.status != Response::Successful) {
Log::Warning(Event::General, "Failed to load source TileJSON: %s", res.message.c_str());
return;
@@ -153,13 +155,11 @@ TileData::State Source::hasTile(const Tile::ID& id) {
return TileData::State::invalid;
}
TileData::State Source::addTile(Map& map, uv::worker& worker,
util::ptr<Style> style,
GlyphAtlas& glyphAtlas, GlyphStore& glyphStore,
SpriteAtlas& spriteAtlas, util::ptr<Sprite> sprite,
FileSource& fileSource, uv_loop_t &loop, TexturePool& texturePool,
const Tile::ID& id,
std::function<void ()> callback) {
TileData::State Source::addTile(Map &map, Environment &env, uv::worker &worker,
util::ptr<Style> style, GlyphAtlas &glyphAtlas,
GlyphStore &glyphStore, SpriteAtlas &spriteAtlas,
util::ptr<Sprite> sprite, TexturePool &texturePool,
const Tile::ID &id, std::function<void()> callback) {
const TileData::State state = hasTile(id);
if (state != TileData::State::invalid) {
@@ -190,14 +190,14 @@ TileData::State Source::addTile(Map& map, uv::worker& worker,
new_tile.data = std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style,
glyphAtlas, glyphStore,
spriteAtlas, sprite,
info, fileSource);
info, env);
} else if (info.type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, fileSource);
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, env);
} else {
throw std::runtime_error("source type not implemented");
}
new_tile.data->request(worker, loop, map.getState().getPixelRatio(), callback);
new_tile.data->request(worker, map.getState().getPixelRatio(), callback);
tile_data.emplace(new_tile.data->id, new_tile.data);
}
@@ -283,12 +283,16 @@ bool Source::findLoadedParent(const Tile::ID& id, int32_t minCoveringZoom, std::
return false;
}
void Source::update(Map& map, uv::worker& worker,
void Source::update(Map &map,
Environment &env,
uv::worker &worker,
util::ptr<Style> style,
GlyphAtlas& glyphAtlas, GlyphStore& glyphStore,
SpriteAtlas& spriteAtlas, util::ptr<Sprite> sprite,
TexturePool& texturePool, FileSource& fileSource, uv_loop_t& loop,
std::function<void ()> callback) {
GlyphAtlas &glyphAtlas,
GlyphStore &glyphStore,
SpriteAtlas &spriteAtlas,
util::ptr<Sprite> sprite,
TexturePool &texturePool,
std::function<void()> callback) {
if (!loaded || map.getTime() <= updated)
return;
@@ -308,11 +312,8 @@ void Source::update(Map& map, uv::worker& worker,
// Add existing child/parent tiles if the actual tile is not yet loaded
for (const Tile::ID& id : required) {
const TileData::State state = addTile(map, worker, style,
glyphAtlas, glyphStore,
spriteAtlas, sprite,
fileSource, loop, texturePool,
id, callback);
const TileData::State state = addTile(map, env, worker, style, glyphAtlas, glyphStore,
spriteAtlas, sprite, texturePool, id, callback);
if (state != TileData::State::parsed) {
// The tile we require is not yet loaded. Try to find a parent or