summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-10-06 13:23:50 +0200
committerKonstantin Käfer <mail@kkaefer.com>2016-10-25 13:52:36 -0700
commit5cc390d694fc7510d445310d8eb9e32429a5e67b (patch)
tree7a24706f919ac3e8154be8b4ce33aed5bf42188d /include
parent45f4dc0166f2d609d014d2174209fdbe1994c943 (diff)
downloadqtlocation-mapboxgl-5cc390d694fc7510d445310d8eb9e32429a5e67b.tar.gz
[core] separate Backend from View for headless rendering
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/gl/implementation.hpp14
-rw-r--r--include/mbgl/map/backend.hpp34
-rw-r--r--include/mbgl/map/map.hpp7
-rw-r--r--include/mbgl/map/view.hpp39
-rw-r--r--include/mbgl/platform/default/glfw_view.hpp34
-rw-r--r--include/mbgl/platform/default/headless_backend.hpp78
-rw-r--r--include/mbgl/platform/default/headless_display.hpp9
-rw-r--r--include/mbgl/platform/default/headless_view.hpp74
8 files changed, 177 insertions, 112 deletions
diff --git a/include/mbgl/gl/implementation.hpp b/include/mbgl/gl/implementation.hpp
new file mode 100644
index 0000000000..4e3a3e51c7
--- /dev/null
+++ b/include/mbgl/gl/implementation.hpp
@@ -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
diff --git a/include/mbgl/map/backend.hpp b/include/mbgl/map/backend.hpp
new file mode 100644
index 0000000000..e4a5634b88
--- /dev/null
+++ b/include/mbgl/map/backend.hpp
@@ -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
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index 2831206d54..4f6207fc6f 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -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,
diff --git a/include/mbgl/map/view.hpp b/include/mbgl/map/view.hpp
index 590eef7237..0dff4b3602 100644
--- a/include/mbgl/map/view.hpp
+++ b/include/mbgl/map/view.hpp
@@ -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
diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp
index 8662a90bf3..a115d03d7f 100644
--- a/include/mbgl/platform/default/glfw_view.hpp
+++ b/include/mbgl/platform/default/glfw_view.hpp
@@ -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,20 +12,36 @@
#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;
+ 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.
+ void setChangeStyleCallback(std::function<void()> callback);
+
+ void setShouldClose();
+
+ 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;
- void initialize(mbgl::Map*) 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);
@@ -32,21 +49,12 @@ public:
static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers);
static void onMouseMove(GLFWwindow *window, double x, double y);
- // 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.
- void setChangeStyleCallback(std::function<void()> callback);
-
- void setShouldClose();
-
- void setWindowTitle(const std::string&);
-
- void run();
+ // 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>
diff --git a/include/mbgl/platform/default/headless_backend.hpp b/include/mbgl/platform/default/headless_backend.hpp
new file mode 100644
index 0000000000..2f4886a365
--- /dev/null
+++ b/include/mbgl/platform/default/headless_backend.hpp
@@ -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
diff --git a/include/mbgl/platform/default/headless_display.hpp b/include/mbgl/platform/default/headless_display.hpp
index edcc905221..f43e61340f 100644
--- a/include/mbgl/platform/default/headless_display.hpp
+++ b/include/mbgl/platform/default/headless_display.hpp
@@ -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 {
diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp
index 23f1e8251a..27af4fc9d9 100644
--- a/include/mbgl/platform/default/headless_view.hpp
+++ b/include/mbgl/platform/default/headless_view.hpp
@@ -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;