gpt4 book ai didi

android - 如何获得具有有效 Surface 的 SurfaceHolder(EGL.eglCreateWindowSurface 需要)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:51:29 25 4
gpt4 key购买 nike

我尝试用 EGL 初始化 GLES(因为我想从主线程中绘制而不是使用渲染器并从 onDrawFrame 内部绘图)。我收到错误消息:“确保 SurfaceView 或关联的 SurfaceHolder 具有有效的 Surface”。显然 mEgl.eglCreateWindowSurface 失败了,因为 surfaceHolder 没有有效的表面。如何获得具有有效 Surface 的 SurfaceHolder 才能继续?

我的 Activity.onCreate 方法是:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GLSurfaceView surfaceView = new GLSurfaceView(this);
setContentView(surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
Surface surface = surfaceHolder.getSurface();
Log.v("HelloAndroid", "surface.isValid()= " + Boolean.toString(surface.isValid()));

EGL10 mEgl = (EGL10) EGLContext.getEGL();
EGLDisplay mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
mEgl.eglInitialize(mEglDisplay, version);
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
int[] configSpec = {
EGL10.EGL_NONE
};
mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, num_config);
EGLConfig mEglConfig = configs[0];
EGLContext mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
EGL10.EGL_NO_CONTEXT, null);
EGLSurface mEglSurface = null;
Log.v("HelloAndroid", "M");
mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
mEglConfig, surfaceHolder, null);
Log.v("HelloAndroid", "N");
mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
mEglContext);
Log.v("HelloAndroid", "O");
}

我使用 platform-tools/adb logcat 得到的日志输出是:

V/HelloAndroid( 1861): surface.isValid()= false
D/libEGL ( 1861): egl.cfg not found, using default config
D/libEGL ( 1861): loaded /system/lib/egl/libGLES_android.so
V/HelloAndroid( 1861): M
D/AndroidRuntime( 1861): Shutting down VM
W/dalvikvm( 1861): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 1861): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.IllegalArgumentException: Make sure the SurfaceView or associated SurfaceHolder has a valid Surface
E/AndroidRuntime( 1861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

感谢任何帮助。

最佳答案

surface 的有效性检查应该在创建 surface 时进行。所以,surface.isValid()应该在onSurfaceCreatedonSurfaceChanged中调用,显然,你应该添加surfaceholder.callback.

关于android - 如何获得具有有效 Surface 的 SurfaceHolder(EGL.eglCreateWindowSurface 需要)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5577256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com