Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] restructure TileSource construction and callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Jun 6, 2016
1 parent 838fd7b commit 7af016e
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 265 deletions.
46 changes: 24 additions & 22 deletions src/mbgl/style/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +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/image_tile_source.hpp>

#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/tile/raster_tile_data.hpp>
Expand Down Expand Up @@ -193,8 +193,7 @@ const std::map<UnwrappedTileID, Tile>& Source::getTiles() const {

std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledTileID,
const UpdateParameters& parameters) {
std::unique_ptr<TileData> data = cache.get(overscaledTileID);
if (data) {
if (auto data = cache.get(overscaledTileID)) {
return data;
}

Expand All @@ -207,33 +206,40 @@ std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledT
const auto resource = Resource::tile(
tileset->tiles.at(0), parameters.pixelRatio, overscaledTileID.canonical.x,
overscaledTileID.canonical.y, overscaledTileID.canonical.z);
auto monitor = std::make_unique<RasterTileSource>(resource, parameters.fileSource);
data =
std::make_unique<RasterTileData>(overscaledTileID, std::move(monitor),
parameters.texturePool, parameters.worker, callback);
auto data = std::make_unique<RasterTileData>(overscaledTileID, parameters.texturePool,
parameters.worker, callback);
data->setTileSource(
std::make_unique<ImageTileSource>(*data, resource, parameters.fileSource));

// Need a std::move here to create a std::unique_ptr<TileData> from
// std::unique_ptr<GeometryTileData>.
return std::move(data);
} else {
std::unique_ptr<GeometryTileSource> monitor;

auto data = std::make_unique<GeometryTileData>(overscaledTileID, id, parameters.style,
parameters.mode, callback);
if (type == SourceType::Vector) {
assert(!tileset->tiles.empty());
const auto resource = Resource::tile(
tileset->tiles.at(0), parameters.pixelRatio, overscaledTileID.canonical.x,
overscaledTileID.canonical.y, overscaledTileID.canonical.z);
monitor = std::make_unique<VectorTileSource>(resource, parameters.fileSource);
data->setTileSource(
std::make_unique<VectorTileSource>(*data, resource, parameters.fileSource));
} else if (type == SourceType::Annotations) {
monitor = std::make_unique<AnnotationTileSource>(overscaledTileID, parameters.annotationManager);
data->setTileSource(std::make_unique<AnnotationTileSource>(
*data, overscaledTileID, parameters.annotationManager));
} else if (type == SourceType::GeoJSON) {
monitor = std::make_unique<GeoJSONTileSource>(geojsonvt.get(), overscaledTileID);
data->setTileSource(
std::make_unique<GeoJSONTileSource>(*data, geojsonvt.get(), overscaledTileID));
} else {
Log::Warning(Event::Style, "Source type '%s' is not implemented", SourceTypeClass(type).c_str());
Log::Warning(Event::Style, "Source type '%s' is not implemented",
SourceTypeClass(type).c_str());
return nullptr;
}

data = std::make_unique<GeometryTileData>(overscaledTileID, std::move(monitor), id,
parameters.style, parameters.mode, callback);
// Need a std::move here to create a std::unique_ptr<TileData> from
// std::unique_ptr<GeometryTileData>.
return std::move(data);
}

return data;
}

TileData* Source::getTileData(const OverscaledTileID& overscaledTileID) const {
Expand Down Expand Up @@ -322,13 +328,9 @@ bool Source::update(const UpdateParameters& parameters) {
}

for (auto& pair : tileDataMap) {
const auto& dataTileID = pair.first;
auto tileData = pair.second.get();
if (parameters.shouldReparsePartialTiles && tileData->isIncomplete()) {
auto callback = std::bind(&Source::tileLoadingCallback, this, dataTileID,
std::placeholders::_1, false);

if (!tileData->parsePending(callback)) {
if (!tileData->parsePending()) {
allTilesUpdated = false;
}
} else {
Expand Down
18 changes: 7 additions & 11 deletions src/mbgl/tile/annotation_tile_source.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#include <mbgl/tile/annotation_tile_source.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/annotation/annotation_manager.hpp>
#include <mbgl/util/async_request.hpp>

namespace mbgl {

AnnotationTileSource::AnnotationTileSource(const OverscaledTileID& tileID_,
AnnotationManager& annotationManager_)
: tileID(tileID_), annotationManager(annotationManager_) {
AnnotationTileSource::AnnotationTileSource(GeometryTileData& tileData_,
const OverscaledTileID& tileID_,
AnnotationManager& annotationManager_)
: GeometryTileSource(tileData_), tileID(tileID_), annotationManager(annotationManager_) {
annotationManager.addTileSource(*this);
}

AnnotationTileSource::~AnnotationTileSource() {
annotationManager.removeTileSource(*this);
}

std::unique_ptr<AsyncRequest>
AnnotationTileSource::monitorTile(const GeometryTileSource::Callback& callback_) {
callback = callback_;
annotationManager.addTileSource(*this);
return nullptr;
}

void AnnotationTileSource::update(std::unique_ptr<GeometryTile> tile) {
callback(nullptr, std::move(tile), {}, {});
tileData.setData(nullptr, std::move(tile), {}, {});
}

} // namespace mbgl
5 changes: 2 additions & 3 deletions src/mbgl/tile/annotation_tile_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@
namespace mbgl {

class AnnotationManager;
class GeometryTile;

class AnnotationTileSource : public GeometryTileSource {
public:
AnnotationTileSource(const OverscaledTileID&, AnnotationManager&);
AnnotationTileSource(GeometryTileData&, const OverscaledTileID&, AnnotationManager&);
~AnnotationTileSource();

void update(std::unique_ptr<GeometryTile>);
std::unique_ptr<AsyncRequest> monitorTile(const GeometryTileSource::Callback&) override;

OverscaledTileID tileID;

private:
AnnotationManager& annotationManager;
GeometryTileSource::Callback callback;
};

} // namespace mbgl
16 changes: 6 additions & 10 deletions src/mbgl/tile/geojson_tile_source.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include <mbgl/tile/geojson_tile_source.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>

#include <mbgl/util/async_request.hpp>

#include <mapbox/geojsonvt.hpp>

namespace mbgl {

GeoJSONTileSource::GeoJSONTileSource(mapbox::geojsonvt::GeoJSONVT* geojsonvt_,
GeoJSONTileSource::GeoJSONTileSource(GeometryTileData& tileData_,
mapbox::geojsonvt::GeoJSONVT* geojsonvt_,
const OverscaledTileID& id)
: tileID(id), geojsonvt(geojsonvt_) {
: GeometryTileSource(tileData_), tileID(id), geojsonvt(geojsonvt_) {
update();
}

GeoJSONTileSource::~GeoJSONTileSource() = default;
Expand Down Expand Up @@ -89,15 +92,8 @@ void GeoJSONTileSource::update() {
if (geojsonvt) {
auto tile = convertTile(
geojsonvt->getTile(tileID.canonical.z, tileID.canonical.x, tileID.canonical.y));
callback(nullptr, std::move(tile), {}, {});
tileData.setData(nullptr, std::move(tile), {}, {});
}
}

std::unique_ptr<AsyncRequest>
GeoJSONTileSource::monitorTile(const GeometryTileSource::Callback& cb) {
callback = cb;
update();
return nullptr;
}

} // namespace mbgl
5 changes: 1 addition & 4 deletions src/mbgl/tile/geojson_tile_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ namespace mbgl {

class GeoJSONTileSource : public GeometryTileSource {
public:
GeoJSONTileSource(mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&);
GeoJSONTileSource(GeometryTileData&, mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&);
virtual ~GeoJSONTileSource();

std::unique_ptr<AsyncRequest> monitorTile(const GeometryTileSource::Callback&) override;

void setGeoJSONVT(mapbox::geojsonvt::GeoJSONVT*);

private:
Expand All @@ -31,7 +29,6 @@ class GeoJSONTileSource : public GeometryTileSource {

private:
mapbox::geojsonvt::GeoJSONVT* geojsonvt = nullptr;
GeometryTileSource::Callback callback;
};

} // namespace mbgl
Loading

2 comments on commit 7af016e

@jfirebaugh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#5143 becomes a bit hard to follow at this point. It looks like, in addition to the primary change here (essentially inverting the relationship between GeometryTileSource and GeometryTileData, which is a great idea), there are some separable changes.

  • Rename RasterTileSource to ImageTileSource
  • Remove an unused parameter from GeometryTileData::parsePending
  • Reformat some ternary conditions
  • callbackcb

Can these be split into separate commits?

@kkaefer
Copy link
Member Author

@kkaefer kkaefer commented on 7af016e Jun 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfirebaugh looks like you continued work in https://github.com/mapbox/mapbox-gl-native/compare/5143-refactor-tile-source; want to sync up later today?

Please sign in to comment.