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>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user