Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Render from the main thread
Browse files Browse the repository at this point in the history
Do not create a thread for the MapContext anymore.
  • Loading branch information
jfirebaugh committed Apr 12, 2016
1 parent be47e35 commit 8cc9aad
Show file tree
Hide file tree
Showing 35 changed files with 266 additions and 539 deletions.
19 changes: 4 additions & 15 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,22 +42,15 @@ class Map : private util::noncopyable {
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
Expand Down Expand Up @@ -189,7 +178,7 @@ class Map : private util::noncopyable {
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 {
Expand Down
52 changes: 25 additions & 27 deletions include/mbgl/map/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 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::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();
Expand Down
5 changes: 1 addition & 4 deletions include/mbgl/platform/default/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ class GLFWView : public mbgl::View {
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);
Expand Down
19 changes: 6 additions & 13 deletions include/mbgl/platform/default/headless_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,31 @@ class HeadlessView : public View {
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;
Expand All @@ -82,13 +79,9 @@ class HeadlessView : public View {
GLXPbuffer glxPbuffer = 0;
#endif

bool extensionsLoaded = false;

GLuint fbo = 0;
GLuint fboDepthStencil = 0;
GLuint fboColor = 0;

std::thread::id thread;
};

} // namespace mbgl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ public void onPause() {
mConnectivityReceiver = null;

mUserLocationView.onPause();
mNativeMapView.pause();
}

/**
Expand All @@ -533,7 +532,6 @@ public void onResume() {
mConnectivityReceiver = new ConnectivityReceiver();
getContext().registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

mNativeMapView.resume();
mNativeMapView.update();
mUserLocationView.onResume();

Expand Down Expand Up @@ -1094,9 +1092,7 @@ private List<Marker> getMarkersInBounds(@NonNull LatLngBounds bbox) {
}

int getTopOffsetPixelsForIcon(Icon icon) {
// This method will dead lock if map paused. Causes a freeze if you add a marker in an
// activity's onCreate()
if (mDestroyed || mNativeMapView.isPaused()) {
if (mDestroyed) {
return 0;
}

Expand Down Expand Up @@ -1243,11 +1239,11 @@ public void onDraw(Canvas canvas) {
return;
}

if (mDestroyed || mNativeMapView.isPaused()) {
if (mDestroyed) {
return;
}

mNativeMapView.renderSync();
mNativeMapView.render();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,12 @@ public void destroySurface() {
nativeDestroySurface(mNativeMapViewPtr);
}

public void pause() {
nativePause(mNativeMapViewPtr);
}

public boolean isPaused() {
return nativeIsPaused(mNativeMapViewPtr);
}

public void resume() {
nativeResume(mNativeMapViewPtr);
}

public void update() {
nativeUpdate(mNativeMapViewPtr);
}

public void renderSync() {
nativeRenderSync(mNativeMapViewPtr);
public void render() {
nativeRender(mNativeMapViewPtr);
}

public void resizeView(int width, int height) {
Expand Down Expand Up @@ -515,15 +503,9 @@ private native void nativeCreateSurface(long nativeMapViewPtr,

private native void nativeDestroySurface(long nativeMapViewPtr);

private native void nativePause(long nativeMapViewPtr);

private native boolean nativeIsPaused(long nativeMapViewPtr);

private native void nativeResume(long nativeMapViewPtr);

private native void nativeUpdate(long nativeMapViewPtr);

private native void nativeRenderSync(long nativeMapViewPtr);
private native void nativeRender(long nativeMapViewPtr);

private native void nativeViewResize(long nativeMapViewPtr, int width, int height);

Expand Down
32 changes: 4 additions & 28 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,39 +404,18 @@ void nativeDestroySurface(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr
nativeMapView->destroySurface();
}

void nativePause(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativePause");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->pause();
}

jboolean nativeIsPaused(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeIsPaused");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
return nativeMapView->getMap().isPaused();
}

void nativeResume(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeResume");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->resume();
}

void nativeUpdate(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeUpdate");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->getMap().update(mbgl::Update::Repaint);
}

void nativeRenderSync(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeRenderSync");
void nativeRender(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeRender");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->getMap().renderSync();
nativeMapView->render();
}

void nativeViewResize(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jint width, jint height) {
Expand Down Expand Up @@ -1815,11 +1794,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
MAKE_NATIVE_METHOD(nativeTerminateContext, "(J)V"),
MAKE_NATIVE_METHOD(nativeCreateSurface, "(JLandroid/view/Surface;)V"),
MAKE_NATIVE_METHOD(nativeDestroySurface, "(J)V"),
MAKE_NATIVE_METHOD(nativePause, "(J)V"),
MAKE_NATIVE_METHOD(nativeIsPaused, "(J)Z"),
MAKE_NATIVE_METHOD(nativeResume, "(J)V"),
MAKE_NATIVE_METHOD(nativeUpdate, "(J)V"),
MAKE_NATIVE_METHOD(nativeRenderSync, "(J)V"),
MAKE_NATIVE_METHOD(nativeRender, "(J)V"),
MAKE_NATIVE_METHOD(nativeViewResize, "(JII)V"),
MAKE_NATIVE_METHOD(nativeFramebufferResize, "(JII)V"),
MAKE_NATIVE_METHOD(nativeAddClass, "(JLjava/lang/String;)V"),
Expand Down
Loading

0 comments on commit 8cc9aad

Please sign in to comment.