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/android/src/example_custom_layer.cpp | |
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/android/src/example_custom_layer.cpp')
-rw-r--r-- | platform/android/src/example_custom_layer.cpp | 75 |
1 files changed, 28 insertions, 47 deletions
diff --git a/platform/android/src/example_custom_layer.cpp b/platform/android/src/example_custom_layer.cpp index f7b425c40a..e805532541 100644 --- a/platform/android/src/example_custom_layer.cpp +++ b/platform/android/src/example_custom_layer.cpp @@ -112,18 +112,9 @@ void checkCompileStatus(GLuint shader) { static const GLchar * vertexShaderSource = "attribute vec2 a_pos; void main() { gl_Position = vec4(a_pos, 0, 1); }"; static const GLchar * fragmentShaderSource = "uniform highp vec4 fill_color; void main() { gl_FragColor = fill_color; }"; -class ExampleCustomLayer { +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() { @@ -158,8 +149,15 @@ public: GL_CHECK_ERROR(glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), background, GL_STATIC_DRAW)); } - void render() { + void render(const mbgl::style::CustomLayerRenderParameters&) { __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "Render"); + glUseProgram(program); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + glEnableVertexAttribArray(a_pos); + glVertexAttribPointer(a_pos, 2, GL_FLOAT, GL_FALSE, 0, NULL); + glDisable(GL_STENCIL_TEST); + glDisable(GL_DEPTH_TEST); + glUniform4fv(fill_color, 1, color); GL_CHECK_ERROR(glUseProgram(program)); GL_CHECK_ERROR(glBindBuffer(GL_ARRAY_BUFFER, buffer)); @@ -172,6 +170,23 @@ public: } + 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; GLuint vertexShader = 0; GLuint fragmentShader = 0; @@ -186,7 +201,8 @@ GLfloat ExampleCustomLayer::color[] = { 0.0f, 1.0f, 0.0f, 1.0f }; jlong JNICALL nativeCreateContext(JNIEnv*, jobject) { __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "nativeCreateContext"); - return reinterpret_cast<jlong>(new ExampleCustomLayer()); + auto exampleCustomLayer = std::make_unique<ExampleCustomLayer>(); + return reinterpret_cast<jlong>(exampleCustomLayer.release()); } void JNICALL nativeSetColor(JNIEnv*, jobject, jfloat red, jfloat green, jfloat blue, jfloat alpha) { @@ -197,25 +213,6 @@ void JNICALL nativeSetColor(JNIEnv*, jobject, jfloat red, jfloat green, jfloat b ExampleCustomLayer::color[3] = alpha; } -void nativeInitialize(void *context) { - __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "nativeInitialize"); - reinterpret_cast<ExampleCustomLayer*>(context)->initialize(); -} - -void nativeRender(void *context, const mbgl::style::CustomLayerRenderParameters& /*parameters*/) { - __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "nativeRender"); - reinterpret_cast<ExampleCustomLayer*>(context)->render(); -} - -void nativeContextLost(void */*context*/) { - __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "nativeContextLost"); -} - -void nativeDeinitialize(void *context) { - __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "nativeDeinitialize"); - delete reinterpret_cast<ExampleCustomLayer*>(context); -} - extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { __android_log_write(ANDROID_LOG_INFO, LOG_TAG, "OnLoad"); @@ -234,22 +231,6 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { return JNI_ERR; } - env->SetStaticLongField(customLayerClass, - env->GetStaticFieldID(customLayerClass, "InitializeFunction", "J"), - reinterpret_cast<jlong>(nativeInitialize)); - - env->SetStaticLongField(customLayerClass, - env->GetStaticFieldID(customLayerClass, "RenderFunction", "J"), - reinterpret_cast<jlong>(nativeRender)); - - env->SetStaticLongField(customLayerClass, - env->GetStaticFieldID(customLayerClass, "ContextLostFunction", "J"), - reinterpret_cast<jlong>(nativeContextLost)); - - env->SetStaticLongField(customLayerClass, - env->GetStaticFieldID(customLayerClass, "DeinitializeFunction", "J"), - reinterpret_cast<jlong>(nativeDeinitialize)); - return JNI_VERSION_1_6; } |