[core] separate Backend from View for headless rendering

This commit is contained in:
Konstantin Käfer
2016-10-06 13:23:50 +02:00
parent 45f4dc0166
commit 5cc390d694
54 changed files with 779 additions and 525 deletions

12
src/mbgl/map/backend.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include <mbgl/map/backend.hpp>
#include <cassert>
namespace mbgl {
void Backend::notifyMapChange(MapChange) {
// no-op
}
} // namespace mbgl

View File

@@ -1,6 +1,7 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/map/backend.hpp>
#include <mbgl/map/transform.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/annotation/annotation_manager.hpp>
@@ -36,7 +37,15 @@ enum class RenderState : uint8_t {
class Map::Impl : public style::Observer {
public:
Impl(View&, FileSource&, Scheduler&, MapMode, GLContextMode, ConstrainMode, ViewportMode);
Impl(Backend&,
View&,
float pixelRatio,
FileSource&,
Scheduler&,
MapMode,
GLContextMode,
ConstrainMode,
ViewportMode);
void onSourceAttributionChanged(style::Source&, const std::string&) override;
void onUpdate(Update) override;
@@ -49,6 +58,7 @@ public:
void loadStyleJSON(const std::string&);
Backend& backend;
View& view;
FileSource& fileSource;
Scheduler& scheduler;
@@ -81,34 +91,53 @@ public:
bool loading = false;
};
Map::Map(View& view, FileSource& fileSource, Scheduler& scheduler, MapMode mapMode, GLContextMode contextMode, ConstrainMode constrainMode, ViewportMode viewportMode)
: impl(std::make_unique<Impl>(view, fileSource, scheduler, mapMode, contextMode, constrainMode, viewportMode)) {
Map::Map(Backend& backend,
View& view,
const float pixelRatio,
FileSource& fileSource,
Scheduler& scheduler,
MapMode mapMode,
GLContextMode contextMode,
ConstrainMode constrainMode,
ViewportMode viewportMode)
: impl(std::make_unique<Impl>(backend,
view,
pixelRatio,
fileSource,
scheduler,
mapMode,
contextMode,
constrainMode,
viewportMode)) {
view.initialize(this);
update(Update::Dimensions);
}
Map::Impl::Impl(View& view_,
Map::Impl::Impl(Backend& backend_,
View& view_,
float pixelRatio_,
FileSource& fileSource_,
Scheduler& scheduler_,
MapMode mode_,
GLContextMode contextMode_,
ConstrainMode constrainMode_,
ViewportMode viewportMode_)
: view(view_),
: backend(backend_),
view(view_),
fileSource(fileSource_),
scheduler(scheduler_),
transform([this](MapChange change) { view.notifyMapChange(change); },
transform([this](MapChange change) { backend.notifyMapChange(change); },
constrainMode_,
viewportMode_),
mode(mode_),
contextMode(contextMode_),
pixelRatio(view.getPixelRatio()),
pixelRatio(pixelRatio_),
asyncUpdate([this] { update(); }),
annotationManager(std::make_unique<AnnotationManager>(pixelRatio)) {
}
Map::~Map() {
impl->view.activate();
impl->backend.activate();
impl->styleRequest = nullptr;
@@ -118,7 +147,7 @@ Map::~Map() {
impl->annotationManager.reset();
impl->painter.reset();
impl->view.deactivate();
impl->backend.deactivate();
}
void Map::renderStill(StillImageCallback callback) {
@@ -162,16 +191,16 @@ void Map::render() {
}
if (impl->renderState == RenderState::Never) {
impl->view.notifyMapChange(MapChangeWillStartRenderingMap);
impl->backend.notifyMapChange(MapChangeWillStartRenderingMap);
}
impl->view.notifyMapChange(MapChangeWillStartRenderingFrame);
impl->backend.notifyMapChange(MapChangeWillStartRenderingFrame);
const Update flags = impl->transform.updateTransitions(Clock::now());
impl->render();
impl->view.notifyMapChange(isFullyLoaded() ?
impl->backend.notifyMapChange(isFullyLoaded() ?
MapChangeDidFinishRenderingFrameFullyRendered :
MapChangeDidFinishRenderingFrame);
@@ -179,10 +208,10 @@ void Map::render() {
impl->renderState = RenderState::Partial;
} else if (impl->renderState != RenderState::Fully) {
impl->renderState = RenderState::Fully;
impl->view.notifyMapChange(MapChangeDidFinishRenderingMapFullyRendered);
impl->backend.notifyMapChange(MapChangeDidFinishRenderingMapFullyRendered);
if (impl->loading) {
impl->loading = false;
impl->view.notifyMapChange(MapChangeDidFinishLoadingMap);
impl->backend.notifyMapChange(MapChangeDidFinishLoadingMap);
}
}
@@ -240,11 +269,11 @@ void Map::Impl::update() {
style->updateTiles(parameters);
if (mode == MapMode::Continuous) {
view.invalidate();
backend.invalidate();
} else if (callback && style->isLoaded()) {
view.activate();
backend.activate();
render();
view.deactivate();
backend.deactivate();
}
updateFlags = Update::Nothing;
@@ -255,8 +284,7 @@ void Map::Impl::render() {
painter = std::make_unique<Painter>(transform.getState());
}
FrameData frameData { view.getFramebufferSize(),
timePoint,
FrameData frameData { timePoint,
pixelRatio,
mode,
contextMode,
@@ -264,6 +292,7 @@ void Map::Impl::render() {
painter->render(*style,
frameData,
view,
annotationManager->getSpriteAtlas());
if (mode == MapMode::Still) {
@@ -291,7 +320,7 @@ void Map::setStyleURL(const std::string& url) {
impl->loading = true;
impl->view.notifyMapChange(MapChangeWillStartLoadingMap);
impl->backend.notifyMapChange(MapChangeWillStartLoadingMap);
impl->styleRequest = nullptr;
impl->styleURL = url;
@@ -335,7 +364,7 @@ void Map::setStyleJSON(const std::string& json) {
impl->loading = true;
impl->view.notifyMapChange(MapChangeWillStartLoadingMap);
impl->backend.notifyMapChange(MapChangeWillStartLoadingMap);
impl->styleURL.clear();
impl->styleJSON.clear();
@@ -801,12 +830,12 @@ void Map::addLayer(std::unique_ptr<Layer> layer, const optional<std::string>& be
}
impl->styleMutated = true;
impl->view.activate();
impl->backend.activate();
impl->style->addLayer(std::move(layer), before);
update(Update::Classes);
impl->view.deactivate();
impl->backend.deactivate();
}
void Map::removeLayer(const std::string& id) {
@@ -815,12 +844,12 @@ void Map::removeLayer(const std::string& id) {
}
impl->styleMutated = true;
impl->view.activate();
impl->backend.activate();
impl->style->removeLayer(id);
update(Update::Classes);
impl->view.deactivate();
impl->backend.deactivate();
}
void Map::addImage(const std::string& name, std::unique_ptr<const SpriteImage> image) {
@@ -971,7 +1000,7 @@ void Map::setSourceTileCacheSize(size_t size) {
impl->sourceCacheSize = size;
if (!impl->style) return;
impl->style->setSourceTileCacheSize(size);
impl->view.invalidate();
impl->backend.invalidate();
}
}
@@ -981,12 +1010,12 @@ void Map::onLowMemory() {
}
if (impl->style) {
impl->style->onLowMemory();
impl->view.invalidate();
impl->backend.invalidate();
}
}
void Map::Impl::onSourceAttributionChanged(style::Source&, const std::string&) {
view.notifyMapChange(MapChangeSourceDidChange);
backend.notifyMapChange(MapChangeSourceDidChange);
}
void Map::Impl::onUpdate(Update flags) {
@@ -999,11 +1028,11 @@ void Map::Impl::onUpdate(Update flags) {
}
void Map::Impl::onStyleLoaded() {
view.notifyMapChange(MapChangeDidFinishLoadingStyle);
backend.notifyMapChange(MapChangeDidFinishLoadingStyle);
}
void Map::Impl::onStyleError() {
view.notifyMapChange(MapChangeDidFailLoadingMap);
backend.notifyMapChange(MapChangeDidFailLoadingMap);
}
void Map::Impl::onResourceError(std::exception_ptr error) {

View File

@@ -5,18 +5,13 @@
namespace mbgl {
PremultipliedImage View::readStillImage(std::array<uint16_t, 2>) {
return {};
}
void View::initialize(Map *map_) {
assert(map_);
map = map_;
}
PremultipliedImage View::readStillImage(std::array<uint16_t, 2>) {
return {};
}
void View::notifyMapChange(MapChange) {
// no-op
}
} // namespace mbgl

View File

@@ -3,10 +3,12 @@
namespace mbgl {
class Shaders;
class View;
class PaintParameters {
public:
Shaders& shaders;
View& view;
};
} // namespace mbgl

View File

@@ -5,6 +5,8 @@
#include <mbgl/style/source.hpp>
#include <mbgl/style/source_impl.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/debugging.hpp>
@@ -90,19 +92,18 @@ void Painter::cleanup() {
context.performCleanup();
}
void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& annotationSpriteAtlas) {
if (frame.framebufferSize != frame_.framebufferSize) {
context.viewport.setDefaultValue(
{ 0, 0, frame_.framebufferSize[0], frame_.framebufferSize[1] });
}
void Painter::render(const Style& style, const FrameData& frame_, View& view, SpriteAtlas& annotationSpriteAtlas) {
context.viewport.setDefaultValue(
{ 0, 0, view.getFramebufferSize()[0], view.getFramebufferSize()[1] });
frame = frame_;
PaintParameters parameters {
#ifndef NDEBUG
paintMode() == PaintMode::Overdraw ? *overdrawShaders : *shaders
paintMode() == PaintMode::Overdraw ? *overdrawShaders : *shaders,
#else
*shaders
*shaders,
#endif
view
};
glyphAtlas = style.glyphAtlas.get();
@@ -148,7 +149,8 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a
// tiles whatsoever.
{
MBGL_DEBUG_GROUP("clear");
context.bindFramebuffer.reset();
context.bindFramebuffer.setDirty();
view.bind();
context.viewport.reset();
context.stencilFunc.reset();
context.stencilTest = true;
@@ -188,7 +190,7 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a
#if not MBGL_USE_GLES2 and not defined(NDEBUG)
if (frame.debugOptions & MapDebugOptions::StencilClip) {
renderClipMasks();
renderClipMasks(parameters);
return;
}
#endif
@@ -231,7 +233,7 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a
#if not MBGL_USE_GLES2 and not defined(NDEBUG)
if (frame.debugOptions & MapDebugOptions::DepthBuffer) {
renderDepthBuffer();
renderDepthBuffer(parameters);
}
#endif
@@ -302,7 +304,8 @@ void Painter::renderPass(PaintParameters& parameters,
setDepthSublayer(0);
layer.as<CustomLayer>()->impl->render(state);
context.setDirtyState();
context.bindFramebuffer.reset();
context.bindFramebuffer.setDirty();
parameters.view.bind();
context.viewport.reset();
} else {
MBGL_DEBUG_GROUP(layer.baseImpl->id + " - " + util::toString(item.tile->id));

View File

@@ -28,6 +28,7 @@ namespace mbgl {
class RenderTile;
class SpriteAtlas;
class View;
class GlyphAtlas;
class LineAtlas;
struct FrameData;
@@ -58,7 +59,6 @@ class BackgroundLayer;
} // namespace style
struct FrameData {
std::array<uint16_t, 2> framebufferSize = {{ 0, 0 }};
TimePoint timePoint;
float pixelRatio;
MapMode mapMode;
@@ -73,6 +73,7 @@ public:
void render(const style::Style&,
const FrameData&,
View&,
SpriteAtlas& annotationSpriteAtlas);
void cleanup();
@@ -85,9 +86,9 @@ public:
#ifndef NDEBUG
// Renders tile clip boundaries, using stencil buffer to calculate fill color.
void renderClipMasks();
void renderClipMasks(PaintParameters&);
// Renders the depth buffer.
void renderDepthBuffer();
void renderDepthBuffer(PaintParameters&);
#endif
void renderDebugText(Tile&, const mat4&);

View File

@@ -1,6 +1,8 @@
#include <mbgl/renderer/painter.hpp>
#include <mbgl/renderer/debug_bucket.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/shader/shaders.hpp>
#include <mbgl/util/string.hpp>
@@ -87,7 +89,7 @@ void Painter::renderDebugFrame(const mat4 &matrix) {
}
#ifndef NDEBUG
void Painter::renderClipMasks() {
void Painter::renderClipMasks(PaintParameters& parameters) {
context.stencilTest = false;
context.depthTest = false;
context.program = 0;
@@ -98,7 +100,7 @@ void Painter::renderClipMasks() {
context.rasterPos = { -1, -1, 0, 0 };
// Read the stencil buffer
const auto& fbSize = frame.framebufferSize;
const auto& fbSize = parameters.view.getFramebufferSize();
auto pixels = std::make_unique<uint8_t[]>(fbSize[0] * fbSize[1]);
MBGL_CHECK_ERROR(glReadPixels(
0, // GLint x
@@ -120,12 +122,14 @@ void Painter::renderClipMasks() {
MBGL_CHECK_ERROR(glWindowPos2i(0, 0));
MBGL_CHECK_ERROR(glDrawPixels(fbSize[0], fbSize[1], GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels.get()));
#else
(void)parameters;
#endif // MBGL_USE_GLES2
}
#endif // NDEBUG
#ifndef NDEBUG
void Painter::renderDepthBuffer() {
void Painter::renderDepthBuffer(PaintParameters& parameters) {
context.stencilTest = false;
context.depthTest = false;
context.program = 0;
@@ -136,7 +140,7 @@ void Painter::renderDepthBuffer() {
context.rasterPos = { -1, -1, 0, 0 };
// Read the stencil buffer
const auto& fbSize = frame.framebufferSize;
const auto& fbSize = parameters.view.getFramebufferSize();
auto pixels = std::make_unique<GLubyte[]>(fbSize[0] * fbSize[1]);
const double base = 1.0 / (1.0 - depthRangeSize);
@@ -155,6 +159,8 @@ void Painter::renderDepthBuffer() {
MBGL_CHECK_ERROR(glWindowPos2i(0, 0));
MBGL_CHECK_ERROR(glDrawPixels(fbSize[0], fbSize[1], GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels.get()));
#else
(void)parameters;
#endif // MBGL_USE_GLES2
}
#endif // NDEBUG

View File

@@ -1,6 +1,7 @@
#include <mbgl/renderer/painter.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/renderer/fill_bucket.hpp>
#include <mbgl/renderer/render_tile.hpp>
@@ -29,7 +30,7 @@ void Painter::renderFill(PaintParameters& parameters,
const bool isOutlineColorDefined = !properties.fillOutlineColor.isUndefined();
Color strokeColor = isOutlineColorDefined? properties.fillOutlineColor : fillColor;
auto worldSize = util::convert<GLfloat>(frame.framebufferSize);
auto worldSize = util::convert<GLfloat>(parameters.view.getFramebufferSize());
bool pattern = !properties.fillPattern.value.from.empty();
bool outline = properties.fillAntialias && !pattern && isOutlineColorDefined;