Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Address review feedback and fix broken CI builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Asheem Mamoowala committed Apr 2, 2018
1 parent 46383bb commit c3cafe7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 81 deletions.
6 changes: 3 additions & 3 deletions include/mbgl/renderer/renderer_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class RendererBackend {
// as a matched pair, exclusively through BackendScope, in two situations:
//
// 1. When releasing GL resources during Renderer destruction
// (Including calling CustomLayerDeinitializeFunction during RenderCustomLayer destruction)
// (Including calling CustomLayerHost::deinitialize during RenderCustomLayer destruction)
// 2. When renderering through Renderer::render()
// (Including calling CustomLayerDeinitializeFunction for newly added custom layers and
// CustomLayerDeinitializeFunction on layer removal)
// (Including calling CustomLayerHost::initialize for newly added custom layers and
// CustomLayerHost::deinitialize on layer removal)
virtual void activate() = 0;
virtual void deactivate() = 0;

Expand Down
74 changes: 36 additions & 38 deletions include/mbgl/style/layers/custom_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

#include <mbgl/style/layer.hpp>

#include <functional>

namespace mbgl {
namespace style {

/**
* Parameters that define the current camera position for a CustomLayerRenderFunction.
* Parameters that define the current camera position for a `CustomLayerHost::render()` function.
*/
struct CustomLayerRenderParameters {
double width;
Expand All @@ -23,41 +21,41 @@ struct CustomLayerRenderParameters {

class CustomLayerHost {
public:
virtual ~CustomLayerHost() = default;
/**
* Initialize any GL state needed by the custom layer. This method is called once, from the
* 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.
*/
virtual void initialize() = 0;

/**
* Render the layer. This method is called once per frame. The implementation should not make
* any assumptions about the GL state (other than that the correct context is active). It may
* make changes to the state, and is not required to reset values such as the depth mask, stencil
* mask, and corresponding test flags to their original values.
* Make sure that you are drawing your fragments with a z value of 1 to take advantage of the
* opaque fragment culling in case there are opaque layers above your custom layer.
*/
virtual void render(const CustomLayerRenderParameters&) = 0;

/**
* Called when the system has destroyed the underlying GL context. The
* `CustomLayerDeinitializeFunction` will not be called in this case, however
* `CustomLayerInitializeFunction` will be called instead to prepare for a new render.
*
*/
virtual void contextLost() = 0;

/**
* 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.
*/
virtual void deinitialize() = 0;
virtual ~CustomLayerHost() = default;
/**
* Initialize any GL state needed by the custom layer. This method is called once, from the
* 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 `deinitialize` function.
*/
virtual void initialize() = 0;

/**
* Render the layer. This method is called once per frame. The implementation should not make
* any assumptions about the GL state (other than that the correct context is active). It may
* make changes to the state, and is not required to reset values such as the depth mask, stencil
* mask, and corresponding test flags to their original values.
* Make sure that you are drawing your fragments with a z value of 1 to take advantage of the
* opaque fragment culling in case there are opaque layers above your custom layer.
*/
virtual void render(const CustomLayerRenderParameters&) = 0;

/**
* Called when the system has destroyed the underlying GL context. The
* `deinitialize` function will not be called in this case, however
* `initialize` will be called instead to prepare for a new render.
*
*/
virtual void contextLost() = 0;

/**
* 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 `initialize` function has not been called.
*/
virtual void deinitialize() = 0;
};

class CustomLayer : public Layer {
Expand Down
23 changes: 12 additions & 11 deletions platform/android/src/example_custom_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ static const GLchar * fragmentShaderSource = "uniform highp vec4 fill_color; voi
class ExampleCustomLayer: mbgl::style::CustomLayerHost {
public:
~ExampleCustomLayer() {
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, "~ExampleCustomLayer");
if (program) {
glDeleteBuffers(1, &buffer);
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteProgram(program);
}
}

void initialize() {
Expand Down Expand Up @@ -159,7 +150,7 @@ class ExampleCustomLayer: mbgl::style::CustomLayerHost {
}

void render(const mbgl::style::CustomLayerRenderParameters&) {
mbgl::Log::Info(mbgl::Event::General, "Render");
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, "Render");
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(a_pos);
Expand All @@ -180,10 +171,20 @@ class ExampleCustomLayer: mbgl::style::CustomLayerHost {
}

void contextLost() {
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, "ContextLost");
program = 0;
}

void deinitialize() {

__android_log_write(ANDROID_LOG_INFO, LOG_TAG, "DeInitialize");
if (program) {
glDeleteBuffers(1, &buffer);
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteProgram(program);
}
}

GLuint program = 0;
Expand Down
20 changes: 4 additions & 16 deletions platform/darwin/src/MGLOpenGLStyleLayer.mm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "MGLOpenGLStyleLayer.h"
#import "MGLOpenGLStyleLayer.h"

#import "MGLMapView_Private.h"
#import "MGLStyle_Private.h"
Expand All @@ -13,9 +13,6 @@
layerRef = styleLayer;
layer = nil;
}
~MGLOpenGLLayerHost () {
layer = nil;
}

void initialize() {
if (layerRef == nil) return;
Expand All @@ -25,7 +22,7 @@ void initialize() {
}

void render(const mbgl::style::CustomLayerRenderParameters &params) {
assert(layerRef);
if(!layer) return;

MGLStyleLayerDrawingContext drawingContext = {
.size = CGSizeMake(params.width, params.height),
Expand Down Expand Up @@ -108,24 +105,15 @@ - (instancetype)initWithIdentifier:(NSString *)identifier {
}

#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.openGLLayers[self.identifier] = nil;
_style = style;
_style.openGLLayers[self.identifier] = self;
}

- (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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/layers/render_custom_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ using namespace style;

RenderCustomLayer::RenderCustomLayer(Immutable<style::CustomLayer::Impl> _impl)
: RenderLayer(LayerType::Custom, _impl), host(_impl->host) {
assert(BackendScope::exists());
host->initialize();
}

RenderCustomLayer::~RenderCustomLayer() {
assert(BackendScope::exists());
if (contextDestroyed) {
host->contextLost();
} else if (!contextDestroyed) {
} else {
host->deinitialize();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/layers/custom_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ std::unique_ptr<Layer> CustomLayer::cloneRef(const std::string&) const {
}

// Visibility

void CustomLayer::setVisibility(VisibilityType value) {
if (value == getVisibility())
return;
Expand Down
22 changes: 10 additions & 12 deletions test/api/custom_layer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ void main() {

class TestLayer : public mbgl::style::CustomLayerHost {
public:
~TestLayer() {
if (program) {
MBGL_CHECK_ERROR(glDeleteBuffers(1, &buffer));
MBGL_CHECK_ERROR(glDetachShader(program, vertexShader));
MBGL_CHECK_ERROR(glDetachShader(program, fragmentShader));
MBGL_CHECK_ERROR(glDeleteShader(vertexShader));
MBGL_CHECK_ERROR(glDeleteShader(fragmentShader));
MBGL_CHECK_ERROR(glDeleteProgram(program));
}
}

void initialize() {
program = MBGL_CHECK_ERROR(glCreateProgram());
vertexShader = MBGL_CHECK_ERROR(glCreateShader(GL_VERTEX_SHADER));
Expand Down Expand Up @@ -77,7 +66,16 @@ class TestLayer : public mbgl::style::CustomLayerHost {

void contextLost() {}

void deinitialize() {}
void deinitialize() {
if (program) {
MBGL_CHECK_ERROR(glDeleteBuffers(1, &buffer));
MBGL_CHECK_ERROR(glDetachShader(program, vertexShader));
MBGL_CHECK_ERROR(glDetachShader(program, fragmentShader));
MBGL_CHECK_ERROR(glDeleteShader(vertexShader));
MBGL_CHECK_ERROR(glDeleteShader(fragmentShader));
MBGL_CHECK_ERROR(glDeleteProgram(program));
}
}

GLuint program = 0;
GLuint vertexShader = 0;
Expand Down

0 comments on commit c3cafe7

Please sign in to comment.