Repository navigation
Use a host interface for CustomLayer instead of function pointers - #11553
Conversation
jfirebaugh
left a comment
There was a problem hiding this comment.
I like this idea, but doesn't the shared_ptr< CustomLayerHost> need to be used throughout the whole stack -- CustomLayer, CustomLayer::Impl -- not just RenderCustomLayer?
|
The |
|
Constructing a |
f198d30 to
b0f68d3
Compare
Unfortunately a brief spin of the run loop is required for the layer reuse test. I've tried to tune this down for my machine; YMMV
… pointers Co-authored-by: Julian Rex <[email protected]>
…style layer, and upgrade to a strong layer when initialized by the renderer Co-authored-by: Julian Rex <[email protected]>
b0f68d3 to
46383bb
Compare
| } | ||
|
|
||
| void deinitialize() { | ||
|
|
There was a problem hiding this comment.
nit additional return vs contextLost()
tobrun
left a comment
There was a problem hiding this comment.
tested on Android device and code is looking 👍
|
|
||
| #include <mbgl/style/layer.hpp> | ||
|
|
||
| #include <functional> |
|
|
||
| class CustomLayerHost { | ||
| public: | ||
| virtual ~CustomLayerHost() = default; |
| * main thread, at a point when the GL context is active but before rendering for the first | ||
| * time. | ||
| * | ||
| * Resources that are acquired in this method must be released in the UninitializeFunction. |
There was a problem hiding this comment.
"in the UninitializeFunction" → "in deinitialize"
|
|
||
| /** | ||
| * Called when the system has destroyed the underlying GL context. The | ||
| * `CustomLayerDeinitializeFunction` will not be called in this case, however |
| * Destroy any GL state needed by the custom layer, and deallocate context, if necessary. This | ||
| * method is called once, from the main thread, at a point when the GL context is active. | ||
| * | ||
| * Note that it may be called even when the InitializeFunction has not been called. |
| [NSException raise:@"MGLLayerReuseException" | ||
| format:@"%@ cannot be added to more than one MGLStyle at a time.", self]; | ||
| } | ||
| _style.openGLLayers[self.identifier] = nil; |
There was a problem hiding this comment.
Clears the entry from the previous style when calling [styleLayer removeFromStyle:]. I had removed this in #10765, but I think it needs to be returned. In the Obj-C SDK, the style and the CustomLayerHost-implementation share ownership of the peer layer.
| RenderCustomLayer::RenderCustomLayer(Immutable<style::CustomLayer::Impl> _impl) | ||
| : RenderLayer(LayerType::Custom, _impl) { | ||
| : RenderLayer(LayerType::Custom, _impl), host(_impl->host) { | ||
| host->initialize(); |
There was a problem hiding this comment.
Is there a reason you're changing the timing of when the custom layer is initialized? Are you sure the GL context is guaranteed to be active at this point? If so, add assert(BackendScope::exists()); like in the destructor.
There was a problem hiding this comment.
I moved this to the constructor in an attempt to initialize and retain the Obj-C peer layer sooner. This was to address a race condition where a custom layer is added to a style, but before the next frame renders, the style is swapped. In this case, the peer layer was released too soon.
This fix didn't address that issue, but I decided to keep it in, since the render layers are created in Renderer::Impl and rendered in the same render pass. Adding the assert for protection.
| } | ||
|
|
||
| // Visibility | ||
|
|
There was a problem hiding this comment.
Nit: revert whitespace change.
| class TestLayer : public mbgl::style::CustomLayerHost { | ||
| public: | ||
| ~TestLayer() { | ||
| if (program) { |
There was a problem hiding this comment.
This needs to move to deinitialize.
| } | ||
| if (contextDestroyed) { | ||
| host->contextLost(); | ||
| } else if (!contextDestroyed) { |
There was a problem hiding this comment.
The conditional is redundant.
adac588 to
b2e7d7e
Compare
|
|
||
| void contextLost() { | ||
| __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "ContextLost"); | ||
| if (program) { |
There was a problem hiding this comment.
In the context lost case, the system has already destroyed all the GL resources, and glDeleteBuffers etc should not be called (IIRC, they'll actually produce errors). So all this should do is clear program.
b2e7d7e to
c08c88c
Compare
c08c88c to
c3cafe7
Compare
Fixes #11143.
Fixes #10771.
CustomLayers are a special class of style layer where the core API delegates rendering of the layer to external code.With the change to asynchronous rendering, the
RenderCustomLayermay call out to the external code to initialize, render, or clean-up outside of theCustomLayer's lifecycle. For example, a custom layer added to the current map style, followed by a different style being set on the map.In addition to #11143, a number of other issues related to custom layers have been opened/addressed recently: #10771, #10755, #10765, #11343.
The existing API using a
void *context object and function pointers, doesn't provide any lifecycle information. The context object needs to exist for the lifetime of theCustomLayer(CustomLayer::Implto be more specific), and theRenderCustomLayer. This PR proposes using ashared_ptr<>to ensure that the context object has the required lifetime.Since a
shared_ptr<>cannot be used withvoid *, a host interfaceCustomLayerHostwith the former function pointers converted to equivalent events.On iOS/macOS, the SDK uses the peer style layer
MGLOpenGLStyleLayeras the context object, creating a tenuous chain of ownership. The implementation ofCustomLayerHostaddresses the retain/release issue described in this comment.RenderCustomLayercallsCustomLayerHost::initialize()in its constructor to ensure that the peer style layer is retained, even if the style (andCustomLayer::Impl) are changed prematurely.TODO
cc @julianrex @lilykaiser @jfirebaugh @akitchen @1ec5