EGLSurface和Surface
时间: 2023-12-19 12:05:20 浏览: 29
EGLSurface和Surface都是与Android系统窗口相关的概念,但是它们具有不同的含义和用法。
EGLSurface是EGL中的一个概念,它表示OpenGL ES绘制的目标表面,可以是一个窗口、一个像素缓冲区或者一个pixmap。在Android中,我们可以使用EGLSurface作为OpenGL ES的渲染目标,将绘制的图形显示到屏幕上。
Surface是Android系统中的一个概念,它表示一个可以用来绘制的矩形区域。在Android中,我们可以通过SurfaceView来创建一个Surface,并在其上进行绘制。SurfaceView可以在一个独立的线程中进行绘制,这样可以避免在主线程中进行耗时的绘制操作,从而提高了应用的响应速度。
在Android中,EGLSurface可以与Surface进行关联,从而实现OpenGL ES的绘制。具体而言,我们可以通过SurfaceHolder.getSurface()方法获取到一个Surface对象,然后使用EGL与该Surface对象进行交互,实现OpenGL ES的绘制。在这种情况下,EGLSurface的类型可以是EGL_WINDOW,表示它是一个窗口表面。
相关问题
EGLSurface Android
EGLSurface是EGL的一个概念,表示承载OpenGL ES绘制内容的窗口或缓冲区。在Android中,EGLSurface是通过Surface或SurfaceTexture对象来表示的。Surface是Android中表示可见视图(如Activity或Fragment)的基本类。SurfaceTexture是一个可用于在OpenGL ES中进行纹理渲染的特殊Surface。
在Android中,需要创建EGLSurface来进行OpenGL ES绘制。创建EGLSurface的方法取决于您要将内容绘制到哪个Surface上。如果您要在Activity或Fragment中绘制内容,可以使用它们提供的Surface对象作为EGLSurface。如果您要在纹理上绘制内容,则需要创建一个SurfaceTexture对象,并使用它来创建EGLSurface。
创建EGLSurface后,您可以将其与EGL context关联起来,以便进行OpenGL ES绘制。如果您要在多个线程中进行绘制,可以在每个线程中创建独立的EGLSurface,并在每个线程上使用eglMakeCurrent函数将其与EGL context关联。
java层创建了一个EGLSurface surface,现在将suface通过jni传递给native层,native拿到这个suface通过opengl进行渲染,使在app应用上显示画面,请给出这个demo
以下是一个简单的示例代码,实现了在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上看到一个红色的背景。