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 | |
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')
-rw-r--r-- | platform/android/src/example_custom_layer.cpp | 75 | ||||
-rw-r--r-- | platform/android/src/style/layers/custom_layer.cpp | 10 | ||||
-rw-r--r-- | platform/android/src/style/layers/custom_layer.hpp | 2 |
3 files changed, 32 insertions, 55 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; } diff --git a/platform/android/src/style/layers/custom_layer.cpp b/platform/android/src/style/layers/custom_layer.cpp index 51a48520bf..61e74a9cf5 100644 --- a/platform/android/src/style/layers/custom_layer.cpp +++ b/platform/android/src/style/layers/custom_layer.cpp @@ -7,14 +7,10 @@ namespace mbgl { namespace android { - CustomLayer::CustomLayer(jni::JNIEnv& env, jni::String layerId, jni::jlong initializeFunction, jni::jlong renderFunction, jni::jlong contextLostFunction, jni::jlong deinitializeFunction, jni::jlong context) + CustomLayer::CustomLayer(jni::JNIEnv& env, jni::String layerId, jni::jlong host) : Layer(env, std::make_unique<mbgl::style::CustomLayer>( jni::Make<std::string>(env, layerId), - reinterpret_cast<mbgl::style::CustomLayerInitializeFunction>(initializeFunction), - reinterpret_cast<mbgl::style::CustomLayerRenderFunction>(renderFunction), - reinterpret_cast<mbgl::style::CustomLayerContextLostFunction>(contextLostFunction), - reinterpret_cast<mbgl::style::CustomLayerDeinitializeFunction>(deinitializeFunction), - reinterpret_cast<void*>(context)) + std::unique_ptr<mbgl::style::CustomLayerHost>(reinterpret_cast<mbgl::style::CustomLayerHost*>(host))) ) { } @@ -53,7 +49,7 @@ namespace android { // Register the peer jni::RegisterNativePeer<CustomLayer>( env, CustomLayer::javaClass, "nativePtr", - std::make_unique<CustomLayer, JNIEnv&, jni::String, jni::jlong, jni::jlong, jni::jlong, jni::jlong, jni::jlong>, + std::make_unique<CustomLayer, JNIEnv&, jni::String, jni::jlong>, "initialize", "finalize", METHOD(&CustomLayer::update, "nativeUpdate")); diff --git a/platform/android/src/style/layers/custom_layer.hpp b/platform/android/src/style/layers/custom_layer.hpp index 9e079c1288..7eb649d923 100644 --- a/platform/android/src/style/layers/custom_layer.hpp +++ b/platform/android/src/style/layers/custom_layer.hpp @@ -16,7 +16,7 @@ public: static void registerNative(jni::JNIEnv&); - CustomLayer(jni::JNIEnv&, jni::String, jni::jlong, jni::jlong, jni::jlong, jni::jlong, jni::jlong); + CustomLayer(jni::JNIEnv&, jni::String, jni::jlong); CustomLayer(mbgl::Map&, mbgl::style::CustomLayer&); |