This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Migrate MGLCustomStyleLayerAdditions to style layer API #7250
Merged
+430
−176
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
617de5f
[ios, macos] Replaced custom style layer API with MGLOpenGLStyleLayer
1ec5 ce4a60f
[macos] Added lime green layer demo to macosapp
1ec5 25cb13f
[ios, macos] Rationalized MGLOpenGLStyleLayer API
1ec5 41bd345
[core] Clarified that rendering happens on the main thread
1ec5 50026f2
[ios, macos] Fixed removing and re-adding MGLOpenGLStyleLayer
1ec5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#import <Foundation/Foundation.h> | ||
#import <CoreLocation/CoreLocation.h> | ||
|
||
#import "MGLStyleValue.h" | ||
#import "MGLStyleLayer.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class MGLMapView; | ||
|
||
typedef struct MGLStyleLayerDrawingContext { | ||
CGSize size; | ||
CLLocationCoordinate2D centerCoordinate; | ||
double zoomLevel; | ||
CLLocationDirection direction; | ||
CGFloat pitch; | ||
CGFloat perspectiveSkew; | ||
} MGLStyleLayerDrawingContext; | ||
|
||
@interface MGLOpenGLStyleLayer : MGLStyleLayer | ||
|
||
@property (nonatomic, weak, readonly) MGLMapView *mapView; | ||
|
||
- (void)didMoveToMapView:(MGLMapView *)mapView; | ||
|
||
- (void)willMoveFromMapView:(MGLMapView *)mapView; | ||
|
||
- (void)drawInMapView:(MGLMapView *)mapView withContext:(MGLStyleLayerDrawingContext)context; | ||
|
||
- (void)setNeedsDisplay; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
#import "MGLOpenGLStyleLayer.h" | ||
|
||
#import "MGLMapView_Private.h" | ||
#import "MGLStyle_Private.h" | ||
|
||
#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`. | ||
|
||
@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.mapView]; | ||
} | ||
|
||
/** | ||
Runs the drawing 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; | ||
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 = params.pitch, | ||
.perspectiveSkew = params.altitude, | ||
}; | ||
[layer drawInMapView:layer.mapView withContext:drawingContext]; | ||
} | ||
|
||
/** | ||
Runs the completion 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 MGLFinishCustomStyleLayer(void *context) { | ||
MGLOpenGLStyleLayer *layer = (__bridge MGLOpenGLStyleLayer *)context; | ||
[layer willMoveFromMapView:layer.mapView]; | ||
} | ||
|
||
/** | ||
An `MGLOpenGLStyleLayer` is a style layer that is rendered by OpenGL code in | ||
Objective-C blocks or Swift closures that you specify. You may initialize a new | ||
OpenGL style layer to add to an `MGLStyle` or obtain one from an `MGLMapView`’s | ||
current style using the `-[MGLStyle layerWithIdentifier:]` method. | ||
|
||
@warning This API is undocumented and therefore unsupported. It may change at | ||
any time without notice. | ||
*/ | ||
@interface MGLOpenGLStyleLayer () | ||
|
||
@property (nonatomic) mbgl::style::CustomLayer *rawLayer; | ||
|
||
/** | ||
The map view whose style currently contains the layer. | ||
|
||
If the layer is not currently part of any map view’s style, this property is | ||
set to `nil`. | ||
*/ | ||
@property (nonatomic, weak, readwrite) MGLMapView *mapView; | ||
|
||
@end | ||
|
||
@implementation MGLOpenGLStyleLayer { | ||
std::unique_ptr<mbgl::style::CustomLayer> _pendingLayer; | ||
} | ||
|
||
/** | ||
Returns an OpenGL style layer object initialized with the given identifier. | ||
|
||
After initializing and configuring the style layer, add it to a map view’s | ||
style using the `-[MGLStyle addLayer:]` or | ||
`-[MGLStyle insertLayer:belowLayer:]` method. | ||
|
||
@param identifier A string that uniquely identifies the layer in the style to | ||
which it is added. | ||
@return An initialized OpenGL style layer. | ||
*/ | ||
- (instancetype)initWithIdentifier:(NSString *)identifier { | ||
if (self = [super initWithIdentifier:identifier]) { | ||
auto layer = std::make_unique<mbgl::style::CustomLayer>(identifier.UTF8String, | ||
MGLPrepareCustomStyleLayer, | ||
MGLDrawCustomStyleLayer, | ||
MGLFinishCustomStyleLayer, | ||
(__bridge void *)self); | ||
_pendingLayer = std::move(layer); | ||
_rawLayer = _pendingLayer.get(); | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - Adding to and removing from a map view | ||
|
||
- (void)setMapView:(MGLMapView *)mapView { | ||
if (_mapView && mapView) { | ||
[NSException raise:@"MGLLayerReuseException" | ||
format:@"%@ cannot be added to more than one MGLStyle at a time.", self]; | ||
} | ||
_mapView.style.openGLLayers[self.identifier] = nil; | ||
_mapView = mapView; | ||
_mapView.style.openGLLayers[self.identifier] = self; | ||
} | ||
|
||
- (void)addToMapView:(MGLMapView *)mapView belowLayer:(MGLStyleLayer *)otherLayer { | ||
self.mapView = mapView; | ||
if (otherLayer) { | ||
const mbgl::optional<std::string> belowLayerId{ otherLayer.identifier.UTF8String }; | ||
mapView.mbglMap->addLayer(std::move(_pendingLayer), belowLayerId); | ||
} else { | ||
mapView.mbglMap->addLayer(std::move(_pendingLayer)); | ||
} | ||
} | ||
|
||
- (void)removeFromMapView:(MGLMapView *)mapView { | ||
auto removedLayer = mapView.mbglMap->removeLayer(self.identifier.UTF8String); | ||
self.mapView = nil; | ||
if (!removedLayer) { | ||
return; | ||
} | ||
_pendingLayer = std::move(reinterpret_cast<std::unique_ptr<mbgl::style::CustomLayer> &>(removedLayer)); | ||
_rawLayer = _pendingLayer.get(); | ||
} | ||
|
||
/** | ||
Called immediately after a layer is added to a map view’s style. | ||
|
||
This method is intended to be overridden in a subclass. You can use this method | ||
to perform any setup work before the layer is used to draw a frame. For | ||
example, you might use this method to compile an OpenGL shader. The default | ||
implementation of this method does nothing. | ||
|
||
Any resource acquired in this method must be released in | ||
`-willMoveFromMapView:`. | ||
|
||
@param mapView The map view to whose style the layer has been added. | ||
*/ | ||
- (void)didMoveToMapView:(MGLMapView *)mapView { | ||
|
||
} | ||
|
||
/** | ||
Called immediately before a layer is removed from a map view’s style. | ||
|
||
This method is intended to be overridden in a subclass. You can use this method | ||
to perform any teardown work once the layer has drawn its last frame and is | ||
about to be removed from the style. The default implementation of this method | ||
does nothing. | ||
|
||
This method may be called even if `-didMoveToMapView:` has not been called. | ||
|
||
@param mapView The map view from whose style the layer is about to be removed. | ||
*/ | ||
- (void)willMoveFromMapView:(MGLMapView *)mapView { | ||
|
||
} | ||
|
||
/** | ||
Called each time the layer needs to draw a new frame in a map view. | ||
|
||
This method is intended to be overridden in a subclass. You can use this method | ||
to draw the layer’s content. The default implementation of this method does | ||
nothing. | ||
|
||
Your implementation should not make any assumptions about the OpenGL state, | ||
other than that the current OpenGL context is active. It may make changes to | ||
the OpenGL state. It is not required to reset values such as the depth mask, | ||
stencil mask, or corresponding test flags to their original values. | ||
|
||
Be sure to draw your fragments with a <var>z</var> value of 1 to take advantage | ||
of the opaque fragment culling, in case the style contains any opaque layers | ||
above this layer. | ||
|
||
@param mapView The map view to which the layer draws. | ||
@param context A context structure with information defining the frame to draw. | ||
*/ | ||
- (void)drawInMapView:(MGLMapView *)mapView withContext:(MGLStyleLayerDrawingContext)context { | ||
|
||
} | ||
|
||
/** | ||
Forces the map view associated with this style to redraw the receiving layer, | ||
causing its drawing handler to be run. | ||
*/ | ||
- (void)setNeedsDisplay { | ||
[self.mapView setNeedsGLDisplay]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a holdover from the former block-based API. I reworded this sentence in 1781302.