summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Rex <julian.rex@mapbox.com>2018-03-23 12:18:18 -0400
committerJulian Rex <julian.rex@mapbox.com>2018-03-23 12:18:18 -0400
commit195c24dd91355053cbc45595d594fcb8f81330d5 (patch)
tree92a48798cb05c0b249d3c493410fc09c35c965dc
parentf303d64a3a2bdbdb3011e94ae7c5333697c983d0 (diff)
downloadqtlocation-mapboxgl-195c24dd91355053cbc45595d594fcb8f81330d5.tar.gz
Experimental update - turning Obj-C object into C++ to reduce amount of hand-waving to manage reference counts.
-rw-r--r--include/mbgl/style/layers/custom_layer.hpp39
-rw-r--r--platform/darwin/src/MGLOpenGLStyleLayer.h15
-rw-r--r--platform/darwin/src/MGLOpenGLStyleLayer.mm261
-rw-r--r--platform/ios/Integration Tests/MBGLIntegrationTests.mm19
-rw-r--r--src/mbgl/renderer/layers/render_custom_layer.cpp45
-rw-r--r--src/mbgl/renderer/layers/render_custom_layer.hpp3
-rw-r--r--src/mbgl/style/layers/custom_layer.cpp30
-rw-r--r--src/mbgl/style/layers/custom_layer_impl.cpp30
-rw-r--r--src/mbgl/style/layers/custom_layer_impl.hpp21
9 files changed, 138 insertions, 325 deletions
diff --git a/include/mbgl/style/layers/custom_layer.hpp b/include/mbgl/style/layers/custom_layer.hpp
index 05d12f47f4..a37ac25b01 100644
--- a/include/mbgl/style/layers/custom_layer.hpp
+++ b/include/mbgl/style/layers/custom_layer.hpp
@@ -61,30 +61,27 @@ using CustomLayerContextAttachFunction = void (*)(void* context);
using CustomLayerContextDetachFunction = void (*)(void* context);
-class CustomLayer : public Layer {
-public:
- CustomLayer(const std::string& id,
- CustomLayerInitializeFunction,
- CustomLayerRenderFunction,
- CustomLayerContextLostFunction,
- CustomLayerDeinitializeFunction,
+class CustomLayerContext {
+ public:
+ CustomLayerContext() = default;
+ virtual ~CustomLayerContext() = default;
+
+ virtual void initialize() = 0;
+ virtual void render(const CustomLayerRenderParameters&) = 0;
+ virtual void deinitialize() = 0;
- // Wrap these 4?
- CustomLayerContextOwnerChangedFunction,
- CustomLayerContextAttachFunction,
- CustomLayerContextDetachFunction,
- void* context);
+ virtual void lost() {};
+ // could lose these if we go back to having the set/style manage life times for
+ // MGLOpenGLStyleLayers
+ virtual void attach() {}
+ virtual void detach() {}
+};
+
+class CustomLayer : public Layer {
+public:
CustomLayer(const std::string& id,
- CustomLayerInitializeFunction,
- CustomLayerRenderFunction,
- CustomLayerDeinitializeFunction,
-
- // Wrap these 4?
- CustomLayerContextOwnerChangedFunction,
- CustomLayerContextAttachFunction,
- CustomLayerContextDetachFunction,
- void* context);
+ std::unique_ptr<CustomLayerContext> context);
~CustomLayer() final;
diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.h b/platform/darwin/src/MGLOpenGLStyleLayer.h
index ad854eac4d..a6b390b71a 100644
--- a/platform/darwin/src/MGLOpenGLStyleLayer.h
+++ b/platform/darwin/src/MGLOpenGLStyleLayer.h
@@ -12,19 +12,6 @@ NS_ASSUME_NONNULL_BEGIN
@class MGLOpenGLStyleLayer;
-MGL_EXPORT
-@interface MGLOpenGLStyleLayerRetainer: NSObject
-@property (nonatomic, weak, readonly) MGLOpenGLStyleLayer *layer;
-@property (nonatomic, assign, readonly) NSInteger layerRetainCount;
-@property (nonatomic, unsafe_unretained, nullable) void *owner;
-- (instancetype)initWithLayer:(MGLStyleLayer*)styleLayer;
-- (void)retainLayer;
-- (void)releaseLayer;
-@end
-
-
-
-
typedef struct MGLStyleLayerDrawingContext {
CGSize size;
CLLocationCoordinate2D centerCoordinate;
@@ -38,8 +25,6 @@ MGL_EXPORT
@interface MGLOpenGLStyleLayer : MGLStyleLayer
@property (nonatomic, weak, readonly) MGLStyle *style;
-- (BOOL)isBeingManaged;
-
- (instancetype)initWithIdentifier:(NSString *)identifier;
- (void)didMoveToMapView:(MGLMapView *)mapView;
diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.mm b/platform/darwin/src/MGLOpenGLStyleLayer.mm
index ad41958903..c4defddbe2 100644
--- a/platform/darwin/src/MGLOpenGLStyleLayer.mm
+++ b/platform/darwin/src/MGLOpenGLStyleLayer.mm
@@ -7,187 +7,96 @@
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/math/wrap.hpp>
-@interface MGLOpenGLStyleLayerRetainer ()
-@property (nonatomic, weak, readwrite) MGLOpenGLStyleLayer *layer;
-@property (nonatomic, assign, readwrite) NSInteger layerRetainCount;
-@end
-
-@implementation MGLOpenGLStyleLayerRetainer
-
-- (void)dealloc {
- NSLog(@"LayerRetainer dealloc start %p", self);
-
- while (self.layerRetainCount) {
- [self releaseLayer];
- }
-
- NSLog(@"LayerRetainer dealloc end %p", self);
-}
-
-- (instancetype)initWithLayer:(MGLOpenGLStyleLayer*)styleLayer {
- if ((self = [super init])) {
- _layer = styleLayer;
- }
- NSLog(@"LayerRetainer %p init with layer %p", self, styleLayer);
- return self;
-}
-
-- (void)setOwner:(nullable void *)owner {
- void* oldOwner = _owner;
-
- _owner = owner;
+class MGLOpenGLStyleLayerContext: public mbgl::style::CustomLayerContext
+{
+public:
+ __weak MGLOpenGLStyleLayer* layer;
+ int layerRetainCount;
+
+ MGLOpenGLStyleLayerContext(MGLOpenGLStyleLayer *layer_) :
+ mbgl::style::CustomLayerContext(),
+ layer(layer_),
+ layerRetainCount(0)
+ {
- if (oldOwner) {
- NSLog(@"LayerRetainer %p released", self);
- CFBridgingRelease((__bridge CFTypeRef)(self));
}
- if (_owner) {
- NSLog(@"LayerRetainer %p retained", self);
- CFBridgingRetain(self);
+ ~MGLOpenGLStyleLayerContext()
+ {
+ while (layerRetainCount) {
+ detach();
+ }
}
-}
-- (void)retainLayer {
+ void attach()
+ {
+ if (!layer) return;
- if (!self.layer)
- return;
+ assert(layerRetainCount >= 0);
- NSAssert(self.layerRetainCount >= 0, @"");
+ @autoreleasepool {
+ CFBridgingRetain(layer);
+ }
+ layerRetainCount++;
- @autoreleasepool {
- CFBridgingRetain(self.layer);
+ printf("LayerRetainer %p retained %d", this, layerRetainCount);
}
- self.layerRetainCount++;
- NSLog(@"LayerRetainer %p retained %ld", self, self.layerRetainCount);
-}
+ void detach()
+ {
+ if (!layer) return;
-- (void)releaseLayer {
- if (!self.layer)
- return;
+ assert(layerRetainCount > 0);
- NSAssert(self.layerRetainCount > 0, @"");
+ layerRetainCount--;
- self.layerRetainCount--;
+ @autoreleasepool {
+ CFBridgingRelease((__bridge CFTypeRef)layer);
+ }
- @autoreleasepool {
- CFBridgingRelease((__bridge CFTypeRef)self.layer);
+ printf("LayerRetainer %p released %d", this, layerRetainCount);
}
- NSLog(@"LayerRetainer %p released %ld", self, self.layerRetainCount);
-}
-@end
-
-void MGLCustomLayerContextOwnerChangedFunction(void *context, void* owner) {
- MGLOpenGLStyleLayerRetainer *contextRetainer = (__bridge MGLOpenGLStyleLayerRetainer*)context;
- contextRetainer.owner = owner;
-}
-
-void MGLCustomLayerContextAttachFunction(void *context) {
- MGLOpenGLStyleLayerRetainer *contextRetainer = (__bridge MGLOpenGLStyleLayerRetainer*)context;
- [contextRetainer retainLayer];
-}
-
-void MGLCustomLayerContextDetachFunction(void *context) {
- MGLOpenGLStyleLayerRetainer *contextRetainer = (__bridge MGLOpenGLStyleLayerRetainer*)context;
- [contextRetainer releaseLayer];
-}
-
-
-
-/**
- Runs the preparation handler block contained in the given context, which is
- implicitly an instance of `MGLOpenGLStyleLayer`.
-
- @param context An `MGLOpenGLStyleLayer` instance that was provided as context
- when creating an OpenGL style layer.
- */
-void MGLPrepareCustomStyleLayer(void *context) {
- // Note, that the layer is retained/released by MGLStyle, ensuring that the layer
- // is alive during rendering
-// MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer*)context;
-
- MGLOpenGLStyleLayerRetainer *retainer = (__bridge MGLOpenGLStyleLayerRetainer*)context;
- MGLOpenGLStyleLayer *layer = retainer.layer;
-
- [layer didMoveToMapView:layer.style.mapView];
-}
+ // Custom stuff
-/**
- Runs the drawing handler block contained in the given context, which is
- implicitly an instance of `MGLOpenGLStyleLayer`.
+ /**
+ Runs the preparation handler block contained in the given context, which is
+ implicitly an instance of `MGLOpenGLStyleLayer`.
- @param context An `MGLOpenGLStyleLayer` instance that was provided as context
- when creating an OpenGL style layer.
- */
-void MGLDrawCustomStyleLayer(void *context, const mbgl::style::CustomLayerRenderParameters &params) {
-// MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer *)context;
- MGLOpenGLStyleLayer *layer = ((__bridge MGLOpenGLStyleLayerRetainer*)context).layer;
-
- if (!layer)
- {
- NSLog(@"no layer");
- return;
- }
-
- MGLStyleLayerDrawingContext drawingContext = {
- .size = CGSizeMake(params.width, params.height),
- .centerCoordinate = CLLocationCoordinate2DMake(params.latitude, params.longitude),
- .zoomLevel = params.zoom,
- .direction = mbgl::util::wrap(params.bearing, 0., 360.),
- .pitch = static_cast<CGFloat>(params.pitch),
- .fieldOfView = static_cast<CGFloat>(params.fieldOfView),
+ */
+ void initialize() {
+ [layer didMoveToMapView:layer.style.mapView];
};
- [layer drawInMapView:layer.style.mapView withContext:drawingContext];
-}
-/**
- Runs the completion handler block contained in the given context, which is
- implicitly an instance of `MGLOpenGLStyleLayer`.
+ /**
+ Runs the drawing handler block contained in the given context, which is
+ implicitly an instance of `MGLOpenGLStyleLayer`.
+
+ */
+ void render(const mbgl::style::CustomLayerRenderParameters& params) {
+
+ MGLStyleLayerDrawingContext drawingContext = {
+ .size = CGSizeMake(params.width, params.height),
+ .centerCoordinate = CLLocationCoordinate2DMake(params.latitude, params.longitude),
+ .zoomLevel = params.zoom,
+ .direction = mbgl::util::wrap(params.bearing, 0., 360.),
+ .pitch = static_cast<CGFloat>(params.pitch),
+ .fieldOfView = static_cast<CGFloat>(params.fieldOfView),
+ };
+ [layer drawInMapView:layer.style.mapView withContext:drawingContext];
+ };
- @param context An `MGLOpenGLStyleLayer` instance that was provided as context
- when creating an OpenGL style layer.
- */
-void MGLFinishCustomStyleLayer(void *context) {
- // Note, that the layer is retained/released by MGLStyle, ensuring that the layer
- // is alive during rendering
-// MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer*)context;
- MGLOpenGLStyleLayerRetainer *retainer = (__bridge MGLOpenGLStyleLayerRetainer*)context;
- MGLOpenGLStyleLayer *layer = retainer.layer;
-
- [layer willMoveFromMapView:layer.style.mapView];
-}
+ /**
+ Runs the completion handler block contained in the given context, which is
+ implicitly an instance of `MGLOpenGLStyleLayer`.
+ */
+ void deinitialize() {
+ [layer willMoveFromMapView:layer.style.mapView];
+ };
+};
-/**
- Function to be called when the core `CustomLayer` (not the Impl) gets deallocated.
- It's possible taht at this stage the Obj-C style layer is being deallocated (but that case is detected).
- */
-//void MGLDeallocateCustomStyleLayer(mbgl::util::unique_any *peer) {
-//
-// // We know that the peer object contains a LayerWrapper with a weak pointer to
-// // our custom layer. We can use this to safely access the layer, and clear out the
-// // raw pointer.
-// //
-// // If we don't do this rawLayer can become a dangling pointer (which was previously being
-// // accessed via the description method)
-//
-// if (!(peer && peer->has_value()))
-// return;
-//
-// LayerWrapper *wrapper = mbgl::util::any_cast<LayerWrapper>(peer);
-//
-// if (!wrapper)
-// return;
-//
-// // If the MGLStyleLayer is currently being dealloc'd (and trigger the CustomLayer destructor, and
-// // this function) then layer here will be nil (even though wrapper->layer may appear to be non-nil)
-// MGLStyleLayer *layer = wrapper->layer;
-//
-// layer.rawLayer = NULL;
-//}
/**
@@ -224,6 +133,11 @@ void MGLFinishCustomStyleLayer(void *context) {
@implementation MGLOpenGLStyleLayer
+- (void)dealloc
+{
+ NSLog(@"MGLOpenGLStyleLayer dealloc %p\n", self);
+}
+
/**
Returns an OpenGL style layer object initialized with the given identifier.
@@ -236,44 +150,15 @@ void MGLFinishCustomStyleLayer(void *context) {
@return An initialized OpenGL style layer.
*/
-- (BOOL)isBeingManaged {
- if (self.rawLayer == NULL) {
- return NO;
- }
-
- auto customLayer = self.rawLayer->template as<mbgl::style::CustomLayer>();
-
- if (!customLayer) {
- return NO;
- }
-
- MGLOpenGLStyleLayerRetainer *retainer = (__bridge MGLOpenGLStyleLayerRetainer*)(customLayer->getContext());
-
- return retainer.layerRetainCount > 0;
-}
-
-- (void)dealloc
-{
- NSLog(@"MGLOpenGLStyleLayer dealloc %p\n", self);
-}
-
- (instancetype)initWithIdentifier:(NSString *)identifier {
NSLog(@"MGLOpenGLStyleLayer init %p\n", self);
- MGLOpenGLStyleLayerRetainer *retainer = [[MGLOpenGLStyleLayerRetainer alloc] initWithLayer:self];
+ auto context = std::make_unique<MGLOpenGLStyleLayerContext>(self);
// Note, do not retain self here, otherwise MGLOpenGLStyleLayer will never be dealloc'd
auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String,
- MGLPrepareCustomStyleLayer,
- MGLDrawCustomStyleLayer,
- MGLFinishCustomStyleLayer,
-
- MGLCustomLayerContextOwnerChangedFunction,
- MGLCustomLayerContextAttachFunction,
- MGLCustomLayerContextDetachFunction,
-
- (__bridge void*)retainer);
+ std::move(context));
return self = [super initWithPendingLayer:std::move(layer)];
}
diff --git a/platform/ios/Integration Tests/MBGLIntegrationTests.mm b/platform/ios/Integration Tests/MBGLIntegrationTests.mm
index 449adb5dbc..a25cc81782 100644
--- a/platform/ios/Integration Tests/MBGLIntegrationTests.mm
+++ b/platform/ios/Integration Tests/MBGLIntegrationTests.mm
@@ -78,12 +78,12 @@
// }
}
-- (BOOL)isLayerBeingManaged:(MGLStyleLayer*)layer {
-
- MGLOpenGLStyleLayer *glLayer = [layer isKindOfClass:[MGLOpenGLStyleLayer class]] ? (MGLOpenGLStyleLayer*)layer : nil;
-
- return [glLayer isBeingManaged];
-}
+//- (BOOL)isLayerBeingManaged:(MGLStyleLayer*)layer {
+//
+// MGLOpenGLStyleLayer *glLayer = [layer isKindOfClass:[MGLOpenGLStyleLayer class]] ? (MGLOpenGLStyleLayer*)layer : nil;
+//
+// return [glLayer isBeingManaged];
+//}
- (MGLStyle *)style {
return self.mapView.style;
@@ -91,7 +91,7 @@
#pragma mark - Tests
-
+/*
- (void)testLayerRetainer {
__weak MGLOpenGLStyleLayer *weaklayer1 = nil;
@@ -191,6 +191,7 @@
#undef ASSERT_RETAIN_COUNT
}
+ */
- (void)testStoringOpenGLLayerInCollections {
@@ -432,7 +433,7 @@
MGLStyleLayer *layer2 = weakLayer;
XCTAssertNotNil(weakLayer);
- XCTAssert([self isLayerBeingManaged:weakLayer]);
+// XCTAssert([self isLayerBeingManaged:weakLayer]);
[self waitForMapViewToBeRendered];
@@ -441,7 +442,7 @@
// [self waitForMapViewToBeRendered];
XCTAssertNotNil(weakLayer);
- XCTAssert(![self isLayerBeingManaged:weakLayer]);
+// XCTAssert(![self isLayerBeingManaged:weakLayer]);
layer2 = nil;
XCTAssertNil(weakLayer);
diff --git a/src/mbgl/renderer/layers/render_custom_layer.cpp b/src/mbgl/renderer/layers/render_custom_layer.cpp
index eab87b19c5..f137afdeeb 100644
--- a/src/mbgl/renderer/layers/render_custom_layer.cpp
+++ b/src/mbgl/renderer/layers/render_custom_layer.cpp
@@ -16,14 +16,24 @@ RenderCustomLayer::RenderCustomLayer(Immutable<style::CustomLayer::Impl> _impl)
RenderCustomLayer::~RenderCustomLayer() {
assert(BackendScope::exists());
- if (initialized) {
- if (contextDestroyed && impl().contextLostFn ) {
- impl().contextLostFn(impl().context);
- } else if (!contextDestroyed && impl().deinitializeFn) {
- impl().deinitializeFn(impl().context);
+ if (initialized && impl().context.get()) {
+
+ if (contextDestroyed) {
+ impl().context->lost();
+ }
+ else {
+ impl().context->deinitialize();
}
- impl().detachFn(impl().context);
+ impl().context->detach();
+
+// if (contextDestroyed && impl().contextLostFn ) {
+// impl().contextLostFn(impl().context);
+// } else if (!contextDestroyed && impl().deinitializeFn) {
+// impl().deinitializeFn(impl().context);
+// }
+//
+// impl().detachFn(impl().context);
}
}
@@ -46,23 +56,19 @@ std::unique_ptr<Bucket> RenderCustomLayer::createBucket(const BucketParameters&,
void RenderCustomLayer::render(PaintParameters& paintParameters, RenderSource*) {
if (context != impl().context || !initialized) {
- //If the context changed, deinitialize the previous one before initializing the new one.
- if (context && !contextDestroyed && impl().deinitializeFn) {
- impl().deinitializeFn(context);
- if (impl().detachFn) {
- impl().detachFn(context);
- }
+ if (!contextDestroyed && context.get()) {
+ context->deinitialize();
+ context->detach();
}
+
context = impl().context;
- if (impl().attachFn) {
- impl().attachFn(context);
+ if (context.get()) {
+ context->attach();
+ context->initialize();
}
- assert(impl().initializeFn);
- impl().initializeFn(impl().context);
-
initialized = true;
}
@@ -86,8 +92,9 @@ void RenderCustomLayer::render(PaintParameters& paintParameters, RenderSource*)
parameters.pitch = state.getPitch();
parameters.fieldOfView = state.getFieldOfView();
- assert(impl().renderFn);
- impl().renderFn(context, parameters);
+// assert(impl().renderFn);
+ context->render(parameters);
+// impl().renderFn(context, parameters);
// Reset the view back to our original one, just in case the CustomLayer changed
// the viewport or Framebuffer.
diff --git a/src/mbgl/renderer/layers/render_custom_layer.hpp b/src/mbgl/renderer/layers/render_custom_layer.hpp
index 6d1fea99d3..3e3aa6d365 100644
--- a/src/mbgl/renderer/layers/render_custom_layer.hpp
+++ b/src/mbgl/renderer/layers/render_custom_layer.hpp
@@ -2,6 +2,7 @@
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layers/custom_layer_impl.hpp>
+#include <mbgl/style/layers/custom_layer.hpp>
namespace mbgl {
@@ -26,7 +27,7 @@ public:
private:
bool initialized = false;
bool contextDestroyed = false;
- void * context = nullptr;
+ std::shared_ptr<style::CustomLayerContext> context;
};
template <>
diff --git a/src/mbgl/style/layers/custom_layer.cpp b/src/mbgl/style/layers/custom_layer.cpp
index 234aa13b83..ee8695b6c3 100644
--- a/src/mbgl/style/layers/custom_layer.cpp
+++ b/src/mbgl/style/layers/custom_layer.cpp
@@ -13,43 +13,17 @@ CustomLayer::~CustomLayer()
}
CustomLayer::CustomLayer(const std::string& layerID,
- CustomLayerInitializeFunction init,
- CustomLayerRenderFunction render,
- CustomLayerContextLostFunction contextLost,
- CustomLayerDeinitializeFunction deinit,
-
- CustomLayerContextOwnerChangedFunction ownerChanged,
- CustomLayerContextAttachFunction attach,
- CustomLayerContextDetachFunction detach,
- void* context)
- : Layer(makeMutable<Impl>(layerID, init, render, contextLost, deinit, ownerChanged, attach, detach, context))
+ std::unique_ptr<CustomLayerContext> context):
+ Layer(makeMutable<Impl>(layerID, std::move(context)))
{
printf("CustomLayer::CustomLayer %p\n", this);
}
-CustomLayer::CustomLayer(const std::string& layerID,
- CustomLayerInitializeFunction init,
- CustomLayerRenderFunction render,
- CustomLayerDeinitializeFunction deinit,
-
- CustomLayerContextOwnerChangedFunction ownerChanged,
- CustomLayerContextAttachFunction attach,
- CustomLayerContextDetachFunction detach,
- void* context)
- : Layer(makeMutable<Impl>(layerID, init, render, nullptr, deinit, ownerChanged, attach, detach, context))
-{
- printf("CustomLayer::CustomLayer %p\n", this);
-}
-
const CustomLayer::Impl& CustomLayer::impl() const {
return static_cast<const Impl&>(*baseImpl);
}
-const void* CustomLayer::getContext() const {
- return impl().context;
-}
-
Mutable<CustomLayer::Impl> CustomLayer::mutableImpl() const {
return makeMutable<Impl>(impl());
}
diff --git a/src/mbgl/style/layers/custom_layer_impl.cpp b/src/mbgl/style/layers/custom_layer_impl.cpp
index 5d1e1f6ab5..3fdeb156a2 100644
--- a/src/mbgl/style/layers/custom_layer_impl.cpp
+++ b/src/mbgl/style/layers/custom_layer_impl.cpp
@@ -4,41 +4,21 @@ namespace mbgl {
namespace style {
CustomLayer::Impl::~Impl() {
- if (changeOwnerFn)
- changeOwnerFn(context, NULL);
}
CustomLayer::Impl::Impl(const std::string& id_,
- CustomLayerInitializeFunction initializeFn_,
- CustomLayerRenderFunction renderFn_,
- CustomLayerContextLostFunction contextLostFn_,
- CustomLayerDeinitializeFunction deinitializeFn_,
-
- CustomLayerContextOwnerChangedFunction changeOwnerFn_,
- CustomLayerContextAttachFunction attachFn_,
- CustomLayerContextDetachFunction detachFn_,
-
- void* context_):
+ std::unique_ptr<CustomLayerContext> context_):
Layer::Impl(LayerType::Custom, id_, std::string()),
- initializeFn(initializeFn_),
- renderFn(renderFn_),
- contextLostFn(contextLostFn_),
- deinitializeFn(deinitializeFn_),
- changeOwnerFn(changeOwnerFn_),
- attachFn(attachFn_),
- detachFn(detachFn_),
- context(context_) {
-
- if (changeOwnerFn)
- changeOwnerFn(context, this);
+ context(std::move(context_)) {
+
}
void CustomLayer::Impl::didSetObserver(bool didSet) const {
if (didSet) {
- if (attachFn) attachFn(context);
+ context->attach();
}
else {
- if (detachFn) detachFn(context);
+ context->detach();
}
}
diff --git a/src/mbgl/style/layers/custom_layer_impl.hpp b/src/mbgl/style/layers/custom_layer_impl.hpp
index 6bc23f0759..5524b6a75e 100644
--- a/src/mbgl/style/layers/custom_layer_impl.hpp
+++ b/src/mbgl/style/layers/custom_layer_impl.hpp
@@ -14,29 +14,12 @@ public:
virtual ~Impl();
Impl(const std::string& id,
- CustomLayerInitializeFunction,
- CustomLayerRenderFunction,
- CustomLayerContextLostFunction,
- CustomLayerDeinitializeFunction,
-
- CustomLayerContextOwnerChangedFunction,
- CustomLayerContextAttachFunction,
- CustomLayerContextDetachFunction,
- void* context);
+ std::unique_ptr<CustomLayerContext> context);
bool hasLayoutDifference(const Layer::Impl&) const override;
void stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const override;
- CustomLayerInitializeFunction initializeFn = nullptr;
- CustomLayerRenderFunction renderFn = nullptr;
- CustomLayerContextLostFunction contextLostFn = nullptr;
- CustomLayerDeinitializeFunction deinitializeFn = nullptr;
-
- CustomLayerContextOwnerChangedFunction changeOwnerFn = nullptr;
- CustomLayerContextAttachFunction attachFn = nullptr;
- CustomLayerContextDetachFunction detachFn = nullptr;
-
- void* context = nullptr;
+ std::shared_ptr<CustomLayerContext> context;
void didSetObserver(bool didSet) const;
};