diff options
author | Julian Rex <julian.rex@mapbox.com> | 2018-03-23 12:18:18 -0400 |
---|---|---|
committer | Julian Rex <julian.rex@mapbox.com> | 2018-03-23 12:18:18 -0400 |
commit | 195c24dd91355053cbc45595d594fcb8f81330d5 (patch) | |
tree | 92a48798cb05c0b249d3c493410fc09c35c965dc /platform | |
parent | f303d64a3a2bdbdb3011e94ae7c5333697c983d0 (diff) | |
download | qtlocation-mapboxgl-195c24dd91355053cbc45595d594fcb8f81330d5.tar.gz |
Experimental update - turning Obj-C object into C++ to reduce amount of hand-waving to manage reference counts.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/darwin/src/MGLOpenGLStyleLayer.h | 15 | ||||
-rw-r--r-- | platform/darwin/src/MGLOpenGLStyleLayer.mm | 261 | ||||
-rw-r--r-- | platform/ios/Integration Tests/MBGLIntegrationTests.mm | 19 |
3 files changed, 83 insertions, 212 deletions
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 ¶ms) { -// 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); |