[core] Render from the main thread

Do not create a thread for the MapContext anymore.
This commit is contained in:
John Firebaugh
2015-09-01 15:07:17 +03:00
parent 18d8e80f52
commit 204c7fee03
35 changed files with 268 additions and 525 deletions

View File

@@ -32,10 +32,6 @@ class ShapeAnnotation;
struct CameraOptions;
struct AnimationOptions;
namespace util {
template <class T> class Thread;
} // namespace util
class Map : private util::noncopyable {
friend class View;
@@ -46,22 +42,15 @@ public:
ConstrainMode constrainMode = ConstrainMode::HeightOnly);
~Map();
// Pauses the render thread. The render thread will stop running but will not be terminated and will not lose state until resumed.
void pause();
bool isPaused();
// Resumes a paused render thread
void resume();
// Register a callback that will get called (on the render thread) when all resources have
// been loaded and a complete render occurs.
using StillImageCallback = std::function<void (std::exception_ptr, PremultipliedImage&&)>;
void renderStill(StillImageCallback callback);
// Triggers a synchronous render.
void renderSync();
// Main render function.
void render();
// Notifies the Map thread that the state has changed and an update might be necessary.
// Notifies the Map that the state has changed and an update might be necessary.
void update(Update update);
// Styling
@@ -189,7 +178,7 @@ public:
private:
View& view;
const std::unique_ptr<Transform> transform;
const std::unique_ptr<util::Thread<MapContext>> context;
const std::unique_ptr<MapContext> context;
MapData* data;
enum class RenderState {

View File

@@ -30,44 +30,42 @@ enum MapChange : uint8_t {
class View {
public:
// Called from the main thread directly after initialization. Must always return the same value,
// i.e. it may not change over time.
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 from the main thread when the View signaled a dimension change. Must return the
// logical dimension of this map in pixels.
// Called when the View signaled a dimension change. Must return the logical dimension
// of this map in pixels.
virtual std::array<uint16_t, 2> getSize() const = 0;
// Called from the main thread for every frame that is being rendered. Must return the absolute
// dimensions of the current framebuffer. Typically, this is the logical width scaled by the
// pixel ratio, but in case the view was moved to display with a different pixel ratio, it can
// also be different from that rule.
// Called for every frame that is being rendered. Must return the absolute dimensions of
// the current framebuffer. Typically, this is the logical width scaled by the pixel ratio,
// but in case the view was moved to display with a different pixel ratio, it can also be
// different from that rule.
virtual std::array<uint16_t, 2> getFramebufferSize() const = 0;
// Called from the main thread when this View is associated with a Map object.
virtual void initialize(Map *map_);
// Called when this View is associated with a Map object.
virtual void initialize(Map*);
// Called from the render thread. Makes the GL context active in the current
// thread. This is typically just called once at the beginning of the
// renderer setup since the render thread doesn't switch the contexts.
// 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::addCustomLayer
// 3. When calling a CustomLayerDeinitializeFunction, during Map::removeCustomLayer
// 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;
// Called from the render thread. Makes the GL context inactive in the current
// thread. This is called once just before the rendering thread terminates.
virtual void deactivate() = 0;
virtual void notify() = 0;
// Called from the render thread. The implementation must trigger a rerender.
// (map->renderSync() from the main thread must be called as a result of this)
// 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;
// Called from the render thread before the render begins.
virtual void beforeRender() = 0;
// Called from the render thread after the render is complete.
virtual void afterRender() = 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();

View File

@@ -22,13 +22,10 @@ public:
std::array<uint16_t, 2> getSize() const override;
std::array<uint16_t, 2> getFramebufferSize() const override;
void initialize(mbgl::Map *map) override;
void initialize(mbgl::Map*) override;
void activate() override;
void deactivate() override;
void notify() override;
void invalidate() override;
void beforeRender() override;
void afterRender() override;
static void onKey(GLFWwindow *window, int key, int scancode, int action, int mods);
static void onScroll(GLFWwindow *window, double xoffset, double yoffset);

View File

@@ -38,34 +38,31 @@ public:
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 notify() override;
void invalidate() override;
void beforeRender() override;
void afterRender() override;
PremultipliedImage readStillImage() override;
void resizeFramebuffer();
void resize(uint16_t width, uint16_t height);
private:
void loadExtensions();
bool isActive() const;
// Implementation specific functions
static gl::glProc initializeExtension(const char*);
void createContext();
void destroyContext();
void clearBuffers();
void resizeFramebuffer();
void activateContext();
void deactivateContext();
private:
std::shared_ptr<HeadlessDisplay> display;
const float pixelRatio;
std::array<uint16_t, 2> dimensions;
bool needsResize = false;
bool extensionsLoaded = false;
bool active = false;
#if MBGL_USE_CGL
CGLContextObj glContext = nullptr;
@@ -82,13 +79,9 @@ private:
GLXPbuffer glxPbuffer = 0;
#endif
bool extensionsLoaded = false;
GLuint fbo = 0;
GLuint fboDepthStencil = 0;
GLuint fboColor = 0;
std::thread::id thread;
};
} // namespace mbgl