[core] move gl::Context to Backend and refactor View

This commit is contained in:
Konstantin Käfer
2016-10-10 17:16:37 +02:00
parent 5cc390d694
commit a4d259c33f
76 changed files with 914 additions and 1024 deletions

View File

@@ -35,10 +35,18 @@ enum class RenderState : uint8_t {
Fully,
};
struct StillImageRequest {
StillImageRequest(View& view_, Map::StillImageCallback&& callback_)
: view(view_), callback(std::move(callback_)) {
}
View& view;
Map::StillImageCallback callback;
};
class Map::Impl : public style::Observer {
public:
Impl(Backend&,
View&,
float pixelRatio,
FileSource&,
Scheduler&,
@@ -54,12 +62,11 @@ public:
void onResourceError(std::exception_ptr) override;
void update();
void render();
void render(View&);
void loadStyleJSON(const std::string&);
Backend& backend;
View& view;
FileSource& fileSource;
Scheduler& scheduler;
@@ -85,14 +92,14 @@ public:
std::unique_ptr<AsyncRequest> styleRequest;
Map::StillImageCallback callback;
std::unique_ptr<StillImageRequest> stillImageRequest;
size_t sourceCacheSize;
TimePoint timePoint;
bool loading = false;
};
Map::Map(Backend& backend,
View& view,
const std::array<uint16_t, 2> size,
const float pixelRatio,
FileSource& fileSource,
Scheduler& scheduler,
@@ -101,7 +108,6 @@ Map::Map(Backend& backend,
ConstrainMode constrainMode,
ViewportMode viewportMode)
: impl(std::make_unique<Impl>(backend,
view,
pixelRatio,
fileSource,
scheduler,
@@ -109,12 +115,10 @@ Map::Map(Backend& backend,
contextMode,
constrainMode,
viewportMode)) {
view.initialize(this);
update(Update::Dimensions);
impl->transform.resize(size);
}
Map::Impl::Impl(Backend& backend_,
View& view_,
float pixelRatio_,
FileSource& fileSource_,
Scheduler& scheduler_,
@@ -123,7 +127,6 @@ Map::Impl::Impl(Backend& backend_,
ConstrainMode constrainMode_,
ViewportMode viewportMode_)
: backend(backend_),
view(view_),
fileSource(fileSource_),
scheduler(scheduler_),
transform([this](MapChange change) { backend.notifyMapChange(change); },
@@ -150,42 +153,38 @@ Map::~Map() {
impl->backend.deactivate();
}
void Map::renderStill(StillImageCallback callback) {
void Map::renderStill(View& view, StillImageCallback callback) {
if (!callback) {
Log::Error(Event::General, "StillImageCallback not set");
return;
}
if (impl->mode != MapMode::Still) {
callback(std::make_exception_ptr(util::MisuseException("Map is not in still image render mode")), {});
callback(std::make_exception_ptr(util::MisuseException("Map is not in still image render mode")));
return;
}
if (impl->callback) {
callback(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")), {});
if (impl->stillImageRequest) {
callback(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")));
return;
}
if (!impl->style) {
callback(std::make_exception_ptr(util::MisuseException("Map doesn't have a style")), {});
callback(std::make_exception_ptr(util::MisuseException("Map doesn't have a style")));
return;
}
if (impl->style->getLastError()) {
callback(impl->style->getLastError(), {});
callback(impl->style->getLastError());
return;
}
impl->callback = callback;
impl->stillImageRequest = std::make_unique<StillImageRequest>(view, std::move(callback));
impl->updateFlags |= Update::RenderStill;
impl->asyncUpdate.send();
}
void Map::update(Update flags) {
impl->onUpdate(flags);
}
void Map::render() {
void Map::render(View& view) {
if (!impl->style) {
return;
}
@@ -198,7 +197,7 @@ void Map::render() {
const Update flags = impl->transform.updateTransitions(Clock::now());
impl->render();
impl->render(view);
impl->backend.notifyMapChange(isFullyLoaded() ?
MapChangeDidFinishRenderingFrameFullyRendered :
@@ -218,16 +217,20 @@ void Map::render() {
// Triggers an asynchronous update, that eventually triggers a view
// invalidation, causing renderSync to be called again if in transition.
if (flags != Update::Nothing) {
update(flags);
impl->onUpdate(flags);
}
}
void Map::triggerRepaint() {
impl->backend.invalidate();
}
void Map::Impl::update() {
if (!style) {
updateFlags = Update::Nothing;
}
if (updateFlags == Update::Nothing || (mode == MapMode::Still && !callback)) {
if (updateFlags == Update::Nothing || (mode == MapMode::Still && !stillImageRequest)) {
return;
}
@@ -270,18 +273,19 @@ void Map::Impl::update() {
if (mode == MapMode::Continuous) {
backend.invalidate();
} else if (callback && style->isLoaded()) {
} else if (stillImageRequest && style->isLoaded()) {
// TODO: determine whether we need activate/deactivate
backend.activate();
render();
render(stillImageRequest->view);
backend.deactivate();
}
updateFlags = Update::Nothing;
}
void Map::Impl::render() {
void Map::Impl::render(View& view) {
if (!painter) {
painter = std::make_unique<Painter>(transform.getState());
painter = std::make_unique<Painter>(backend.getContext(), transform.getState());
}
FrameData frameData { timePoint,
@@ -296,8 +300,8 @@ void Map::Impl::render() {
annotationManager->getSpriteAtlas());
if (mode == MapMode::Still) {
callback(nullptr, view.readStillImage());
callback = nullptr;
auto request = std::move(stillImageRequest);
request->callback(nullptr);
}
painter->cleanup();
@@ -399,12 +403,12 @@ std::string Map::getStyleJSON() const {
void Map::cancelTransitions() {
impl->transform.cancelTransitions();
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::setGestureInProgress(bool inProgress) {
impl->transform.setGestureInProgress(inProgress);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
bool Map::isGestureInProgress() const {
@@ -431,24 +435,24 @@ CameraOptions Map::getCameraOptions(optional<EdgeInsets> padding) const {
void Map::jumpTo(const CameraOptions& camera) {
impl->transform.jumpTo(camera);
update(camera.zoom ? Update::RecalculateStyle : Update::Repaint);
impl->onUpdate(camera.zoom ? Update::RecalculateStyle : Update::Repaint);
}
void Map::easeTo(const CameraOptions& camera, const AnimationOptions& animation) {
impl->transform.easeTo(camera, animation);
update(camera.zoom ? Update::RecalculateStyle : Update::Repaint);
impl->onUpdate(camera.zoom ? Update::RecalculateStyle : Update::Repaint);
}
void Map::flyTo(const CameraOptions& camera, const AnimationOptions& animation) {
impl->transform.flyTo(camera, animation);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
#pragma mark - Position
void Map::moveBy(const ScreenCoordinate& point, const Duration& duration) {
impl->transform.moveBy(point, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::setLatLng(const LatLng& latLng, const Duration& duration) {
@@ -457,12 +461,12 @@ void Map::setLatLng(const LatLng& latLng, const Duration& duration) {
void Map::setLatLng(const LatLng& latLng, optional<EdgeInsets> padding, const Duration& duration) {
impl->transform.setLatLng(latLng, padding, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::setLatLng(const LatLng& latLng, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->transform.setLatLng(latLng, anchor, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
LatLng Map::getLatLng(optional<EdgeInsets> padding) const {
@@ -477,7 +481,7 @@ void Map::resetPosition(optional<EdgeInsets> padding) {
camera.padding = padding;
camera.zoom = 0;
impl->transform.jumpTo(camera);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
@@ -485,12 +489,12 @@ void Map::resetPosition(optional<EdgeInsets> padding) {
void Map::scaleBy(double ds, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->transform.scaleBy(ds, anchor, duration);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
void Map::setScale(double scale, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->transform.setScale(scale, anchor, duration);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
double Map::getScale() const {
@@ -503,7 +507,7 @@ void Map::setZoom(double zoom, const Duration& duration) {
void Map::setZoom(double zoom, optional<EdgeInsets> padding, const Duration& duration) {
impl->transform.setZoom(zoom, padding, duration);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
double Map::getZoom() const {
@@ -516,7 +520,7 @@ void Map::setLatLngZoom(const LatLng& latLng, double zoom, const Duration& durat
void Map::setLatLngZoom(const LatLng& latLng, double zoom, optional<EdgeInsets> padding, const Duration& duration) {
impl->transform.setLatLngZoom(latLng, zoom, padding, duration);
update(Update::RecalculateStyle);
impl->onUpdate(Update::RecalculateStyle);
}
CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, optional<EdgeInsets> padding) const {
@@ -613,6 +617,11 @@ double Map::getMaxZoom() const {
#pragma mark - Size
void Map::setSize(const std::array<uint16_t, 2>& size) {
impl->transform.resize(size);
impl->onUpdate(Update::Repaint);
}
uint16_t Map::getWidth() const {
return impl->transform.getState().getWidth();
}
@@ -625,7 +634,7 @@ uint16_t Map::getHeight() const {
void Map::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const Duration& duration) {
impl->transform.rotateBy(first, second, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::setBearing(double degrees, const Duration& duration) {
@@ -634,12 +643,12 @@ void Map::setBearing(double degrees, const Duration& duration) {
void Map::setBearing(double degrees, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->transform.setAngle(-degrees * util::DEG2RAD, anchor, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::setBearing(double degrees, optional<EdgeInsets> padding, const Duration& duration) {
impl->transform.setAngle(-degrees * util::DEG2RAD, padding, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
double Map::getBearing() const {
@@ -648,7 +657,7 @@ double Map::getBearing() const {
void Map::resetNorth(const Duration& duration) {
impl->transform.setAngle(0, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
#pragma mark - Pitch
@@ -659,7 +668,7 @@ void Map::setPitch(double pitch, const Duration& duration) {
void Map::setPitch(double pitch, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->transform.setPitch(pitch * util::DEG2RAD, anchor, duration);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
double Map::getPitch() const {
@@ -670,7 +679,7 @@ double Map::getPitch() const {
void Map::setNorthOrientation(NorthOrientation orientation) {
impl->transform.setNorthOrientation(orientation);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
NorthOrientation Map::getNorthOrientation() const {
@@ -681,7 +690,7 @@ NorthOrientation Map::getNorthOrientation() const {
void Map::setConstrainMode(mbgl::ConstrainMode mode) {
impl->transform.setConstrainMode(mode);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
ConstrainMode Map::getConstrainMode() const {
@@ -692,7 +701,7 @@ ConstrainMode Map::getConstrainMode() const {
void Map::setViewportMode(mbgl::ViewportMode mode) {
impl->transform.setViewportMode(mode);
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
ViewportMode Map::getViewportMode() const {
@@ -737,17 +746,17 @@ double Map::getTopOffsetPixelsForAnnotationIcon(const std::string& name) {
AnnotationID Map::addAnnotation(const Annotation& annotation) {
auto result = impl->annotationManager->addAnnotation(annotation, getMaxZoom());
update(Update::AnnotationStyle | Update::AnnotationData);
impl->onUpdate(Update::AnnotationStyle | Update::AnnotationData);
return result;
}
void Map::updateAnnotation(AnnotationID id, const Annotation& annotation) {
update(impl->annotationManager->updateAnnotation(id, annotation, getMaxZoom()));
impl->onUpdate(impl->annotationManager->updateAnnotation(id, annotation, getMaxZoom()));
}
void Map::removeAnnotation(AnnotationID annotation) {
impl->annotationManager->removeAnnotation(annotation);
update(Update::AnnotationStyle | Update::AnnotationData);
impl->onUpdate(Update::AnnotationStyle | Update::AnnotationData);
}
#pragma mark - Feature query api
@@ -833,7 +842,7 @@ void Map::addLayer(std::unique_ptr<Layer> layer, const optional<std::string>& be
impl->backend.activate();
impl->style->addLayer(std::move(layer), before);
update(Update::Classes);
impl->onUpdate(Update::Classes);
impl->backend.deactivate();
}
@@ -847,7 +856,7 @@ void Map::removeLayer(const std::string& id) {
impl->backend.activate();
impl->style->removeLayer(id);
update(Update::Classes);
impl->onUpdate(Update::Classes);
impl->backend.deactivate();
}
@@ -861,7 +870,7 @@ void Map::addImage(const std::string& name, std::unique_ptr<const SpriteImage> i
impl->style->spriteAtlas->setSprite(name, std::move(image));
impl->style->spriteAtlas->updateDirty();
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::removeImage(const std::string& name) {
@@ -873,7 +882,7 @@ void Map::removeImage(const std::string& name) {
impl->style->spriteAtlas->removeSprite(name);
impl->style->spriteAtlas->updateDirty();
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
#pragma mark - Defaults
@@ -917,7 +926,7 @@ double Map::getDefaultPitch() const {
void Map::setDebug(MapDebugOptions debugOptions) {
impl->debugOptions = debugOptions;
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
void Map::cycleDebugOptions() {
@@ -941,7 +950,7 @@ void Map::cycleDebugOptions() {
else
impl->debugOptions = MapDebugOptions::TileBorders;
update(Update::Repaint);
impl->onUpdate(Update::Repaint);
}
MapDebugOptions Map::getDebug() const {
@@ -954,20 +963,20 @@ bool Map::isFullyLoaded() const {
void Map::addClass(const std::string& className) {
if (impl->style && impl->style->addClass(className)) {
update(Update::Classes);
impl->onUpdate(Update::Classes);
}
}
void Map::removeClass(const std::string& className) {
if (impl->style && impl->style->removeClass(className)) {
update(Update::Classes);
impl->onUpdate(Update::Classes);
}
}
void Map::setClasses(const std::vector<std::string>& classNames) {
if (impl->style) {
impl->style->setClasses(classNames);
update(Update::Classes);
impl->onUpdate(Update::Classes);
}
}
@@ -1019,10 +1028,6 @@ void Map::Impl::onSourceAttributionChanged(style::Source&, const std::string&) {
}
void Map::Impl::onUpdate(Update flags) {
if (flags & Update::Dimensions) {
transform.resize(view.getSize());
}
updateFlags |= flags;
asyncUpdate.send();
}
@@ -1036,9 +1041,9 @@ void Map::Impl::onStyleError() {
}
void Map::Impl::onResourceError(std::exception_ptr error) {
if (mode == MapMode::Still && callback) {
callback(error, {});
callback = nullptr;
if (mode == MapMode::Still && stillImageRequest) {
auto request = std::move(stillImageRequest);
request->callback(error);
}
}