[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

View File

@@ -0,0 +1,14 @@
#pragma once
#if defined(__QT__)
#define MBGL_USE_QT 1
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IOS
#define MBGL_USE_EAGL 1
#else
#define MBGL_USE_CGL 1
#endif
#else
#define MBGL_USE_GLX 1
#endif

View File

@@ -0,0 +1,34 @@
#pragma once
#include <mbgl/map/change.hpp>
namespace mbgl {
class Map;
class Backend {
public:
virtual ~Backend() = default;
// Called when the backend's GL context needs to be made active or inactive. These are called,
// as a matched pair, in four situations:
//
// 1. When releasing GL resources during Map destruction
// 2. When calling a CustomLayerInitializeFunction, during Map::addLayer
// 3. When calling a CustomLayerDeinitializeFunction, during Map::removeLayer
// 4. When rendering for Map::renderStill
//
// They are *not* called for Map::render; it is assumed that the correct context is already
// activated prior to calling Map::render.
virtual void activate() = 0;
virtual void deactivate() = 0;
// Called when the map needs to be rendered; the backend should call Map::render() at some point
// in the near future. (Not called for Map::renderStill() mode.)
virtual void invalidate() = 0;
// Notifies a watcher of map x/y/scale/rotation changes.
virtual void notifyMapChange(MapChange change);
};
} // namespace mbgl

View File

@@ -19,6 +19,7 @@
namespace mbgl {
class Backend;
class View;
class FileSource;
class Scheduler;
@@ -33,7 +34,11 @@ class Layer;
class Map : private util::noncopyable {
public:
explicit Map(View&, FileSource&, Scheduler&,
explicit Map(Backend&,
View&,
float pixelRatio,
FileSource&,
Scheduler&,
MapMode mapMode = MapMode::Continuous,
GLContextMode contextMode = GLContextMode::Unique,
ConstrainMode constrainMode = ConstrainMode::HeightOnly,

View File

@@ -1,24 +1,23 @@
#pragma once
#include <mbgl/map/change.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/image.hpp>
#include <array>
#include <functional>
#include <memory>
namespace mbgl {
class Map;
class View {
class View : private util::noncopyable {
public:
virtual ~View() = default;
// Called directly after initialization. Must always return the same value, i.e. it may
// not change over time.
virtual float getPixelRatio() const = 0;
// Called when this View is associated with a Map object.
virtual void initialize(Map*);
// Called when this View is used for rendering. Implementations should ensure that a renderable
// object is bound and glClear/glDraw* calls can be done.
virtual void bind() = 0;
// Called when the View signaled a dimension change. Must return the logical dimension
// of this map in pixels.
@@ -30,34 +29,12 @@ public:
// different from that rule.
virtual std::array<uint16_t, 2> getFramebufferSize() const = 0;
// Called when this View is associated with a Map object.
virtual void initialize(Map*);
// Called when the view's GL context needs to be made active or inactive. These are called,
// as a matched pair, in four situations:
//
// 1. When releasing GL resources during Map destruction
// 2. When calling a CustomLayerInitializeFunction, during Map::addLayer
// 3. When calling a CustomLayerDeinitializeFunction, during Map::removeLayer
// 4. When rendering for Map::renderStill
//
// They are *not* called for Map::render; it is assumed that the correct context is already
// activated prior to calling Map::render.
virtual void activate() = 0;
virtual void deactivate() = 0;
// Called when the map needs to be rendered; the view should call Map::render() at some point
// in the near future. (Not called for Map::renderStill() mode.)
virtual void invalidate() = 0;
// Reads the pixel data from the current framebuffer. If your View implementation
// doesn't support reading from the framebuffer, return a null pointer.
virtual PremultipliedImage readStillImage(std::array<uint16_t, 2> size = {{ 0, 0 }});
// Notifies a watcher of map x/y/scale/rotation changes.
virtual void notifyMapChange(MapChange change);
protected:
mbgl::Map *map = nullptr;
};
} // namespace mbgl

View File

@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/mbgl.hpp>
#include <mbgl/map/backend.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/util/geometry.hpp>
@@ -11,26 +12,12 @@
#define GL_GLEXT_PROTOTYPES
#include <GLFW/glfw3.h>
class GLFWView : public mbgl::View {
class GLFWView : public mbgl::View, public mbgl::Backend {
public:
GLFWView(bool fullscreen = false, bool benchmark = false);
~GLFWView() override;
float getPixelRatio() const override;
std::array<uint16_t, 2> getSize() const override;
std::array<uint16_t, 2> getFramebufferSize() const override;
void initialize(mbgl::Map*) override;
void activate() override;
void deactivate() override;
void invalidate() override;
static void onKey(GLFWwindow *window, int key, int scancode, int action, int mods);
static void onScroll(GLFWwindow *window, double xoffset, double yoffset);
static void onWindowResize(GLFWwindow *window, int width, int height);
static void onFramebufferResize(GLFWwindow *window, int width, int height);
static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers);
static void onMouseMove(GLFWwindow *window, double x, double y);
float getPixelRatio() const;
// Callback called when the user presses the key mapped to style change.
// The expected action is to set a new style, different to the current one.
@@ -41,12 +28,33 @@ public:
void setWindowTitle(const std::string&);
void run();
private:
// mbgl::View implementation
void initialize(mbgl::Map*) override;
void bind() override;
std::array<uint16_t, 2> getSize() const override;
std::array<uint16_t, 2> getFramebufferSize() const override;
// mbgl::Backend implementation
void activate() override;
void deactivate() override;
void invalidate() override;
// Window callbacks
static void onKey(GLFWwindow *window, int key, int scancode, int action, int mods);
static void onScroll(GLFWwindow *window, double xoffset, double yoffset);
static void onWindowResize(GLFWwindow *window, int width, int height);
static void onFramebufferResize(GLFWwindow *window, int width, int height);
static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers);
static void onMouseMove(GLFWwindow *window, double x, double y);
// Internal
void report(float duration);
void setMapChangeCallback(std::function<void(mbgl::MapChange)> callback);
void notifyMapChange(mbgl::MapChange change) override;
private:
mbgl::Color makeRandomColor() const;
mbgl::Point<double> makeRandomPoint() const;
static std::shared_ptr<const mbgl::SpriteImage>

View File

@@ -0,0 +1,78 @@
#pragma once
#include <mbgl/gl/implementation.hpp>
#if MBGL_USE_QT
class QGLWidget;
#elif MBGL_USE_CGL
#include <OpenGL/OpenGL.h>
#elif MBGL_USE_GLX
typedef struct _XDisplay Display;
typedef struct __GLXcontextRec* GLXContext;
typedef struct __GLXFBConfigRec* GLXFBConfig;
typedef long unsigned int XID;
typedef XID GLXPbuffer;
#endif
#include <mbgl/map/backend.hpp>
#include <mbgl/gl/extension.hpp>
#include <memory>
#include <functional>
namespace mbgl {
class HeadlessDisplay;
class HeadlessBackend : public Backend {
public:
HeadlessBackend();
HeadlessBackend(std::shared_ptr<HeadlessDisplay>);
~HeadlessBackend() override;
void invalidate() override;
void activate() override;
void deactivate() override;
void notifyMapChange(MapChange) override;
void setMapChangeCallback(std::function<void(MapChange)>&& cb) { mapChangeCallback = std::move(cb); }
private:
void activateContext();
void deactivateContext();
private:
// Implementation specific functions
static gl::glProc initializeExtension(const char*);
void createContext();
void destroyContext();
std::shared_ptr<HeadlessDisplay> display;
bool extensionsLoaded = false;
bool active = false;
#if MBGL_USE_QT
QGLWidget* glContext = nullptr;
#endif
#if MBGL_USE_CGL
CGLContextObj glContext = nullptr;
#endif
#if MBGL_USE_EAGL
void *glContext = nullptr;
#endif
#if MBGL_USE_GLX
Display *xDisplay = nullptr;
GLXFBConfig *fbConfigs = nullptr;
GLXContext glContext = nullptr;
GLXPbuffer glxPbuffer = 0;
#endif
std::function<void(MapChange)> mapChangeCallback;
};
} // namespace mbgl

View File

@@ -1,6 +1,13 @@
#pragma once
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/gl/implementation.hpp>
#if MBGL_USE_CGL
#include <OpenGL/OpenGL.h>
#elif MBGL_USE_GLX
typedef struct _XDisplay Display;
typedef struct __GLXFBConfigRec* GLXFBConfig;
#endif
namespace mbgl {

View File

@@ -1,95 +1,37 @@
#pragma once
#if defined(__QT__)
#define MBGL_USE_QT 1
class QGLWidget;
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IOS
#define MBGL_USE_EAGL 1
#else
#define MBGL_USE_CGL 1
#endif
#else
#define GL_GLEXT_PROTOTYPES
#define MBGL_USE_GLX 1
typedef struct _XDisplay Display;
typedef struct __GLXcontextRec* GLXContext;
typedef struct __GLXFBConfigRec* GLXFBConfig;
typedef long unsigned int XID;
typedef XID GLXPbuffer;
#endif
#include <mbgl/mbgl.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/gl/types.hpp>
#include <mbgl/gl/extension.hpp>
#include <memory>
#include <thread>
namespace mbgl {
class HeadlessDisplay;
class HeadlessView : public View {
public:
HeadlessView(float pixelRatio, uint16_t width = 256, uint16_t height = 256);
HeadlessView(std::shared_ptr<HeadlessDisplay> display, float pixelRatio, uint16_t width = 256, uint16_t height = 256);
HeadlessView(float pixelRatio = 1, uint16_t width = 256, uint16_t height = 256);
~HeadlessView() override;
float getPixelRatio() const override;
void bind() override;
std::array<uint16_t, 2> getSize() const override;
std::array<uint16_t, 2> getFramebufferSize() const override;
void invalidate() override;
void activate() override;
void deactivate() override;
void notifyMapChange(MapChange) override;
PremultipliedImage readStillImage(std::array<uint16_t, 2> size = {{ 0, 0 }}) override;
float getPixelRatio() const;
void resize(uint16_t width, uint16_t height);
void setMapChangeCallback(std::function<void(MapChange)>&& cb) { mapChangeCallback = std::move(cb); }
private:
// Implementation specific functions
static gl::glProc initializeExtension(const char*);
void createContext();
void destroyContext();
void clearBuffers();
void resizeFramebuffer();
void activateContext();
void deactivateContext();
void bindFramebuffer();
std::shared_ptr<HeadlessDisplay> display;
private:
const float pixelRatio;
std::array<uint16_t, 2> dimensions;
bool needsResize = false;
bool extensionsLoaded = false;
bool active = false;
#if MBGL_USE_QT
QGLWidget* glContext = nullptr;
#endif
#if MBGL_USE_CGL
CGLContextObj glContext = nullptr;
#endif
#if MBGL_USE_EAGL
void *glContext = nullptr;
#endif
#if MBGL_USE_GLX
Display *xDisplay = nullptr;
GLXFBConfig *fbConfigs = nullptr;
GLXContext glContext = nullptr;
GLXPbuffer glxPbuffer = 0;
#endif
std::function<void(MapChange)> mapChangeCallback;
gl::FramebufferID fbo = 0;
gl::RenderbufferID fboDepthStencil = 0;