java层创建了一个EGLSurface surface,现在将suface通过jni传递给native层,native拿到这个suface通过opengl进行渲染,使在app应用上显示画面,请给出这个demo
时间: 2024-03-21 17:41:49 浏览: 152
egl.rar_egl生成java文件
以下是一个简单的示例代码,实现了在Java层创建一个EGLSurface,然后通过JNI传递给Native层,在Native层中使用OpenGL进行渲染,最终在Android应用上显示画面。
注意:这个示例代码仅供参考,可能需要根据具体的需求进行修改和优化。
Java层代码:
```java
import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLExt;
import android.opengl.EGLSurface;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private EGLDisplay mEglDisplay;
private EGLContext mEglContext;
private EGLSurface mEglSurface;
private SurfaceView mSurfaceView;
private native void nativeRender(long surface);
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceView.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 创建EGL Surface
mEglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
EGL14.eglInitialize(mEglDisplay, version, 0, version, 1);
EGLConfig eglConfig = chooseEglConfig();
mEglContext = createContext(eglConfig);
Surface surface = holder.getSurface();
int[] surfaceAttribs = {EGL14.EGL_NONE};
mEglSurface = EGL14.eglCreateWindowSurface(mEglDisplay, eglConfig, surface, surfaceAttribs, 0);
EGL14.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
// 传递Surface给Native层
nativeRender(surface.getNativeHandle());
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 销毁EGL Surface
EGL14.eglDestroySurface(mEglDisplay, mEglSurface);
EGL14.eglDestroyContext(mEglDisplay, mEglContext);
EGL14.eglTerminate(mEglDisplay);
}
private EGLConfig chooseEglConfig() {
int[] attributes = {
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_ALPHA_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL14.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
EGL14.eglChooseConfig(mEglDisplay, attributes, 0, configs, 0, configs.length, numConfigs, 0);
return configs[0];
}
private EGLContext createContext(EGLConfig eglConfig) {
int[] contextAttribs = {
EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,
EGL14.EGL_NONE
};
EGLContext context = EGL14.eglCreateContext(mEglDisplay, eglConfig, EGL14.EGL_NO_CONTEXT, contextAttribs, 0);
return context;
}
}
```
Native层代码:
```c++
#include <jni.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
extern "C" {
JNIEXPORT void JNICALL
Java_com_example_myapplication_MainActivity_nativeRender(JNIEnv *env, jobject thiz, jlong surfaceHandle) {
ANativeWindow* window = ANativeWindow_fromSurface(env, (jobject) surfaceHandle);
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major, minor;
eglInitialize(display, &major, &minor);
EGLConfig config;
EGLint numConfigs;
eglChooseConfig(display, NULL, &config, 1, &numConfigs);
EGLSurface surface = eglCreateWindowSurface(display, config, window, NULL);
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
eglMakeCurrent(display, surface, surface, context);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(display, surface);
eglDestroyContext(display, context);
eglDestroySurface(display, surface);
eglTerminate(display);
}
}
```
在Android应用中,需要在layout文件夹下创建一个xml文件,添加一个SurfaceView:
```xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
```
完成以上代码后,运行应用,即可在SurfaceView上看到一个红色的背景。
阅读全文