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

发布于 2024-10-31 11:41:36 字数 2432 浏览 1 评论 0原文

我尝试用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)

感谢任何帮助。

I try to initialize GLES with EGL (because I want to draw from the main thread
instead of using a Renderer and drawing from inside onDrawFrame). I get the error: "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface". Obviously mEgl.eglCreateWindowSurface fails because surfaceHolder has not a valid surface. How do I get a SurfaceHolder with a valid Surface to proceed?

My Activity.onCreate method is:

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");
}

The log output I get with platform-tools/adb logcat is:

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)

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-11-07 11:41:36

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

The check valid for surface should be did when surface is created. So, surface.isValid() should be called in onSurfaceCreated or onSurfaceChanged, obviously, you should add surfaceholder.callback.

淡写薰衣草的香 2024-11-07 11:41:36

实现 SurfaceHolder.Callback,然后等待,直到告知您表面已创建。还要注意它何时被破坏。您应该只依赖匹配的创建和销毁调用之间有效的表面。

Implement SurfaceHolder.Callback, and wait until you're told that the surface has been created. Also pay attention to when it is destroyed. You should only rely on the surface being valid between matching create and destroy calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文