diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2018-04-02 16:15:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-02 16:15:25 -0700 |
commit | 099fcc3b3c72b117e93d4da39ee6f70c6c5a2bc8 (patch) | |
tree | 523c8c884404f82075702b6ea707e2b2b8015089 /platform/darwin | |
parent | c3079c6155d7ba2a0c8dd36571f17d7652a2092e (diff) | |
download | qtlocation-mapboxgl-099fcc3b3c72b117e93d4da39ee6f70c6c5a2bc8.tar.gz |
Use a host interface for CustomLayer instead of function pointers (#11553)
Use a host interface for CustomLayer instead of function pointers
Co-authored-by: Julian Rex <julian.rex@mapbox.com>
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLOpenGLStyleLayer.mm | 90 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyle_Private.h | 2 |
3 files changed, 42 insertions, 52 deletions
diff --git a/platform/darwin/src/MGLOpenGLStyleLayer.mm b/platform/darwin/src/MGLOpenGLStyleLayer.mm index 8933a77382..bbd7422c74 100644 --- a/platform/darwin/src/MGLOpenGLStyleLayer.mm +++ b/platform/darwin/src/MGLOpenGLStyleLayer.mm @@ -7,49 +7,47 @@ #include <mbgl/style/layers/custom_layer.hpp> #include <mbgl/math/wrap.hpp> -/** - Runs the preparation handler block contained in the given context, which is - implicitly an instance of `MGLOpenGLStyleLayer`. +class MGLOpenGLLayerHost : public mbgl::style::CustomLayerHost { +public: + MGLOpenGLLayerHost(MGLOpenGLStyleLayer *styleLayer) { + layerRef = styleLayer; + layer = nil; + } - @param context An `MGLOpenGLStyleLayer` instance that was provided as context - when creating an OpenGL style layer. - */ -void MGLPrepareCustomStyleLayer(void *context) { - MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer *)context; - [layer didMoveToMapView:layer.style.mapView]; -} + void initialize() { + if (layerRef == nil) return; + else if (layer == nil) layer = layerRef; -/** - Runs the drawing handler block contained in the given context, which is - implicitly an instance of `MGLOpenGLStyleLayer`. + [layer didMoveToMapView:layer.style.mapView]; + } - @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; - 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]; -} + void render(const mbgl::style::CustomLayerRenderParameters ¶ms) { + if(!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), + }; + [layer drawInMapView:layer.style.mapView withContext:drawingContext]; + } -/** - Runs the completion handler block contained in the given context, which is - implicitly an instance of `MGLOpenGLStyleLayer`. + void contextLost() {} - @param context An `MGLOpenGLStyleLayer` instance that was provided as context - when creating an OpenGL style layer. - */ -void MGLFinishCustomStyleLayer(void *context) { - MGLOpenGLStyleLayer *layer = (__bridge_transfer MGLOpenGLStyleLayer *)context; - [layer willMoveFromMapView:layer.style.mapView]; -} + void deinitialize() { + if (layer == nil) return; + + [layer willMoveFromMapView:layer.style.mapView]; + layerRef = layer; + layer = nil; + } +private: + __weak MGLOpenGLStyleLayer * layerRef; + MGLOpenGLStyleLayer * layer = nil; +}; /** An `MGLOpenGLStyleLayer` is a style layer that is rendered by OpenGL code that @@ -98,10 +96,7 @@ void MGLFinishCustomStyleLayer(void *context) { */ - (instancetype)initWithIdentifier:(NSString *)identifier { auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, - MGLPrepareCustomStyleLayer, - MGLDrawCustomStyleLayer, - MGLFinishCustomStyleLayer, - (__bridge_retained void *)self); + std::make_unique<MGLOpenGLLayerHost>(self)); return self = [super initWithPendingLayer:std::move(layer)]; } @@ -110,22 +105,15 @@ void MGLFinishCustomStyleLayer(void *context) { } #pragma mark - Adding to and removing from a map view - -- (void)setStyle:(MGLStyle *)style { - if (_style && style) { - [NSException raise:@"MGLLayerReuseException" - format:@"%@ cannot be added to more than one MGLStyle at a time.", self]; - } - _style = style; -} - - (void)addToStyle:(MGLStyle *)style belowLayer:(MGLStyleLayer *)otherLayer { self.style = style; + self.style.openGLLayers[self.identifier] = self; [super addToStyle:style belowLayer:otherLayer]; } - (void)removeFromStyle:(MGLStyle *)style { [super removeFromStyle:style]; + self.style.openGLLayers[self.identifier] = nil; self.style = nil; } diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 66abf4391e..0162dbd354 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -82,6 +82,7 @@ @property (nonatomic, readonly, weak) MGLMapView *mapView; @property (nonatomic, readonly) mbgl::style::Style *rawStyle; @property (readonly, copy, nullable) NSURL *URL; +@property (nonatomic, readwrite, strong) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLOpenGLStyleLayer *) *openGLLayers; @property (nonatomic) NS_MUTABLE_DICTIONARY_OF(NSString *, NS_DICTIONARY_OF(NSObject *, MGLTextLanguage *) *) *localizedLayersByIdentifier; @end @@ -124,6 +125,7 @@ static_assert(6 == mbgl::util::default_styles::numOrderedStyles, if (self = [super init]) { _mapView = mapView; _rawStyle = rawStyle; + _openGLLayers = [NSMutableDictionary dictionary]; _localizedLayersByIdentifier = [NSMutableDictionary dictionary]; } return self; diff --git a/platform/darwin/src/MGLStyle_Private.h b/platform/darwin/src/MGLStyle_Private.h index d7ad2975ef..24466b8018 100644 --- a/platform/darwin/src/MGLStyle_Private.h +++ b/platform/darwin/src/MGLStyle_Private.h @@ -25,7 +25,7 @@ namespace mbgl { @property (nonatomic, readonly) mbgl::style::Style *rawStyle; - (nullable NS_ARRAY_OF(MGLAttributionInfo *) *)attributionInfosWithFontSize:(CGFloat)fontSize linkColor:(nullable MGLColor *)linkColor; - +@property (nonatomic, readonly, strong) NS_MUTABLE_DICTIONARY_OF(NSString *, MGLOpenGLStyleLayer *) *openGLLayers; - (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration; @end |