系统相关
首页 > 系统相关> > android-eglCreateWindowSurface:native_window_api_connect失败

android-eglCreateWindowSurface:native_window_api_connect失败

作者:互联网

我有一个问题,当android:hardwareAccelerated设置为清单文件中的false时,最新的Samsung Galaxy系列才出现

据我所知(我自己尝试过),它会在Galaxy S9,J6和Note 8上发生,例如在Galaxy S8上不会发生.其他电话似乎根本不受影响.

问题是我得到了一个GLSurfaceView,它什么也不显示(黑屏),但是如果我在活动之间进行切换,它将再次开始工作,我想是因为它可以无错误地更新View.

这是我发现的可疑日志行

01-13 14:39:47.813 25013 25080 E libEGL  : eglCreateWindowSurface: native_window_api_connect (win=0xc166b808) failed (0xffffffed) (already connected to another API?)
01-13 14:39:47.813 25013 25080 E libEGL  : eglCreateWindowSurface:679 error 3003 (EGL_BAD_ALLOC)

这些是我的代码的关键部分:

GLSurf glsurf;

public void onPause() {
    super.onPause();

    // stop
    if (glsurf != null) {
        glsurf.onPause();
    }
}

@Override
public void onResume() {
    super.onResume();

    if (glsurf != null)
        glsurf.onResume();
}

public class GLSurf extends GLSurfaceView {

    public GLSurf(Context context) {
        super(context);

        /* Create an OpenGL ES 2.0 context. */
        setEGLContextClientVersion(2);
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);

        // Set the Renderer for drawing on the GLSurfaceView
        mRenderer = new GLRenderer(context);
        setRenderer(mRenderer);

        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }
}

public class GLRenderer implements Renderer {

    GLRenderer(Context c){
        // does nothing
    }

    @Override
    public void onDrawFrame(GL10 unused) {

        // i've copied it but it's not even reached

        // call jni function updating the single texture filling the screen
        nativeGLRender();

        // Draw the triangles
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
                GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

        // We need to know the current width and height.
        mScreenWidth = width;
        mScreenHeight = height;

        // Redo the Viewport.
        GLES20.glViewport(0, 0, (int)mScreenWidth, (int)mScreenHeight);

    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        // Set the clear color to black
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1);
    }

}

更多信息:

EGL_BAD_ALLOC总是在具有有意义(至少我认为)日志的事件序列之后发生

onCreate
onResume
onPause

GLSurfaceView: Warning, !readyToDraw() but waiting for draw finished! Early reporting draw finished.

onResume 

libEGL  : eglCreateWindowSurface: native_window_api_connect (win=0xc166b808) failed (0xffffffed) (already connected to another API?)
libEGL  : eglCreateWindowSurface:679 error 3003 (EGL_BAD_ALLOC)

onPause
onStop
onDestroy
onCreate 
onResume
onSurfaceCreated

        : NULL == surf->write_back_color_buffer
        : NULL == surf->write_back_color_buffer
GLThread: eglSwapBuffers failed: EGL_BAD_SURFACE

... black screen ...

请注意,上述事件是在没有用户交互的情况下发生的,且发生时间为1-2秒.关于发生了什么的任何想法?

为了完成信息的处理,以下是可正常使用的电话的顺序(例如,我的nexus 6)

onCreate
onResume
onSurfaceCreated
... working screen

编辑1月16日:

有关此问题的新信息:

>首次启动应用程序时,它可以运行
>从不尝试下一次尝试
>如果放在后台并还原,则可以使用
>如果我将android:hardwareAccelerated设置为true,则错误永远不会发生(但由于其他原因我无法将其打开)

编辑1月18日

>我忘了提到,仅当android:hardwareAccelerated设置为false时,该问题才会出现

我也发现了错误的原因

不知道为什么,但我只是更改了这部分代码

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    getWindow().setFormat(PixelFormat.RGB_565);
}

有了这个

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
}

一切顺利.三星驱动程序中的错误?也许…

我希望这对某人有用

解决方法:

eglCreateWindowSurface: native_window_api_connect failed Any idea
about what’s going on
?

从logcat角度看的问题

eglApi.cpp开始的调用函数是:

/*
 * native_window_api_connect(..., int api)
 * connects an API to this window. only one API can be connected at a time.
 * Returns -EINVAL if for some reason the window cannot be connected, which
 * can happen if it's connected to some other API.
 */
static inline int native_window_api_connect(
        struct ANativeWindow* window, int api)
{
    return window->perform(window, NATIVE_WINDOW_API_CONNECT, api);
}

这是来自WindowSurface.java方法的WindowSurface.recreate()的注释块,其中说:

/*
 * If the previous EGLSurface isn't fully destroyed, e.g. it's still current on a
 * context somewhere, the create call will fail with complaints from the Surface
 * about already being connected.
*/

相关问题

来自Jitesh DalsaniyaOpengls eglCreateWindowSurface GL Error EGL_BAD_ALLOC

I solved error GL Error EGL_BAD_ALLOC. This error occurs due to I am
not handling Renderer properly with activity life cycle.

Activity Life-cycle

A GLSurfaceView must be notified when to pause and
resume rendering. GLSurfaceView clients are required to call onPause() when the activity stops and onResume() when the activity
starts. These calls allow GLSurfaceView to pause and resume the
rendering thread, and also allow GLSurfaceView to release and
recreate the OpenGL display.

EGL上下文丢失

There are situations where the EGL rendering context will be lost.
This typically happens when device wakes up after going to sleep. When
the EGL context is lost, all OpenGL resources (such as textures)
that are associated with that context will be automatically deleted.
In order to keep rendering correctly, a renderer must recreate any
lost resources that it still needs. The onSurfaceCreated(GL10,
EGLConfig) method is a convenient place to do this.

Workaround-to-losing-the-opengl-context-when-android-pauses

因此,您的Activity的onPause()应该如下所示:

@Override
public void onPause() {
    glsurf.setVisibility(View.GONE);
    super.onPause();
    ...
}

然后,您不是从onResume()而是从onWindowFocusChanged()将GLSurfaceView还原到层次结构中:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && glsurf.getVisibility() == View.GONE) {
         glsurf.setVisibility(View.VISIBLE);
    }
    ...
}

其他尝试

>您需要调试才能弄清楚为什么它不满意.
>尝试始终使用OpenglView Visible,并在获得许可时使用startPreview.隐形持有人对opengl无效,这可能会导致崩溃.
> Opengl版本不好?
>处理TextureViewGLActivity中的onSurfaceTextureSizeChanged吗?
> EGL_BAD_ALLOC和eglSwapBuffers失败:EGL_BAD_SURFACE是一个大提示…
>!readyToDraw()删除并重新创建Surface?

进一步

观察:

The error never happen if I put android:hardwareAccelerated to true

Hardware acceleration is enabled by default if your Target API level
is >=14, but can also be explicitly enabled (at the Application or Activity level).

文件连结

GLSurfaceViewGLSurfaceView.Rendererhardware-acceleration

标签:android,opengl-es,egl
来源: https://codeday.me/bug/20191011/1889436.html