gpt4 book ai didi

android - Opengls eglCreateWindowSurface GL 错误 EGL_BAD_ALLOC

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

在 Samsung Galaxy S6 上安装后第一次打开应用程序时出现 eglCreateWindowSurface GL Error EGL_BAD_ALLOC 并且相同的代码在其他设备上工作正常。

我试图清理内存并销毁纹理并在销毁时取消初始化所有 opengl 对象,但这对我没有帮助。

03-30 09:21:32.983 21942-26083/com.example E/TextureSurfaceRenderer: createContext: EGL10.EGL_YES_CONTEXT com.google.android.gles_jni.GLImpl@24406d0
03-30 09:21:32.983 21942-26083/com.example E/libEGL: eglCreateWindowSurface: native_window_api_connect (win=0x7f7b100e10) failed (0xffffffea) (already connected to another API?)
03-30 09:21:32.983 21942-26083/com.example E/libEGL: eglCreateWindowSurface:485 error 3003 (EGL_BAD_ALLOC)
03-30 09:21:32.993 21942-26083/com.example E/GL Error:: EGL_BAD_ALLOC

这是我的代码。

import android.graphics.SurfaceTexture;
import android.opengl.GLUtils;
import android.os.Handler;
import android.util.Log;

import javax.microedition.khronos.egl.*;

/**
* Renderer which initializes OpenGL 2.0 context on a passed surface and starts
* a rendering thread
*
* This class has to be subclassed to be used properly
*/
public abstract class TextureSurfaceRenderer implements Runnable {
private static final int EGL_OPENGL_ES2_BIT = 4;
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private static final String TAG = TextureSurfaceRenderer.class.getSimpleName();
protected final SurfaceTexture texture;
protected Handler myHandler;
private EGL10 egl;
private EGLDisplay eglDisplay;
private EGLContext eglContext;
private EGLSurface eglSurface;

protected int width;
protected int height;
private boolean running;

/**
* @param texture
* Surface texture on which to render. This has to be called
* AFTER the texture became available
* @param width
* Width of the passed surface
* @param height
* Height of the passed surface
*/
public TextureSurfaceRenderer(SurfaceTexture texture, int width, int height,Handler myHandler) {
this.myHandler = myHandler;
this.texture = texture;
this.width = width;
this.height = height;
this.running = true;
Thread thrd = new Thread(this);
thrd.start();
}

@Override
public void run() {
initGL();
if(eglSurface != null){
initGLComponents();
Log.d(TAG, "OpenGL init OK.");

while (running) {
long loopStart = System.currentTimeMillis();
pingFps();
boolean isDraw = draw();
// Log.e(TAG, "eglSwapBuffers Before Draw " + isDraw);
if (isDraw) {
if(!egl.eglSwapBuffers(eglDisplay, eglSurface)){
int error = egl.eglGetError();
Log.e(TAG, "eglSwapBuffers Error " + GLUtils.getEGLErrorString(error));
}

}
// Targeting 60 fps, no need for faster
long waitDelta = 16 - (System.currentTimeMillis() - loopStart);
if (waitDelta > 0) {
try {
Thread.sleep(waitDelta);
} catch (InterruptedException e) {
MyApplication.getInstance().trackException(e);
continue;
}
}
}

deInitGLComponents();
deinitGL();
} else {
Log.e(TAG, "Egl Surface is null " + GLUtils.getEGLErrorString(egl.eglGetError()));
}
}

/**
* Main draw function, subclass this and add custom drawing code here. The
* rendering thread will attempt to limit FPS to 60 to keep CPU usage low.
*/
protected abstract boolean draw();

/**
* OpenGL component initialization funcion. This is called after OpenGL
* context has been initialized on the rendering thread. Subclass this and
* initialize shaders / textures / other GL related components here.
*/
public abstract void initGLComponents();
public abstract void deInitGLComponents();

private long lastFpsOutput = 0;
private int frames;

private void pingFps() {
if (lastFpsOutput == 0)
lastFpsOutput = System.currentTimeMillis();

frames++;

if (System.currentTimeMillis() - lastFpsOutput > 1000) {
Log.d(TAG, "FPS: " + frames);
lastFpsOutput = System.currentTimeMillis();
frames = 0;
}
}

/**
* Call when activity pauses. This stops the rendering thread and
* deinitializes OpenGL.
*/
public void onPause() {
running = false;
}

private void initGL() {
egl = (EGL10) EGLContext.getEGL();
eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

int[] version = new int[2];
egl.eglInitialize(eglDisplay, version);
EGLConfig eglConfig = chooseEglConfig();
eglContext = createContext(egl, eglDisplay, eglConfig);

eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, texture,
null); // Here I am getting null eglSurface first Time.
if (eglSurface == null || eglSurface == EGL10.EGL_NO_SURFACE) {
myHandler.sendEmptyMessage(0);
Log.e("GL Error: "
,""+ GLUtils.getEGLErrorString(egl.eglGetError()));
return;
}

if (!egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
myHandler.sendEmptyMessage(0);
Log.e("GL Make current error: "
, "" + GLUtils.getEGLErrorString(egl.eglGetError()));
return;
}
}

private void deinitGL() {
egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
egl.eglDestroySurface(eglDisplay, eglSurface);
egl.eglDestroyContext(eglDisplay, eglContext);
egl.eglTerminate(eglDisplay);
Log.d(TAG, "OpenGL deinit OK.");
}

private EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay,
EGLConfig eglConfig) {
int[] attribList = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
EGLContext eglContext = egl.eglCreateContext(eglDisplay, eglConfig,
EGL10.EGL_NO_CONTEXT, attribList);
if(eglContext.getGL().equals(EGL10.EGL_NO_CONTEXT)){
Log.e(TAG, "createContext: EGL10.EGL_NO_CONTEXT");
} else {
Log.e(TAG, "createContext: EGL10.EGL_YES_CONTEXT " + eglContext.getGL().toString());
}
return eglContext;
}

private EGLConfig chooseEglConfig() {
int[] configsCount = new int[1];
EGLConfig[] configs = new EGLConfig[1];
int[] configSpec = getConfig();

if (!egl.eglChooseConfig(eglDisplay, configSpec, configs, 1,
configsCount)) {
Log.e(TAG , "Failed to choose config: " + GLUtils.getEGLErrorString(egl.eglGetError()));
} else if (configsCount[0] > 0) {
return configs[0];
}
return null;
}

private int[] getConfig() {
return new int[] { EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL10.EGL_RED_SIZE, 8, EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8, EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_DEPTH_SIZE, 0, EGL10.EGL_STENCIL_SIZE, 0, EGL10.EGL_NONE };
}

@Override
protected void finalize() throws Throwable {
super.finalize();
running = false;
}
}

最佳答案

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

所以你的 Activity 的 onPause() 应该是这样的:

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

并且您将 GLSurfaceView 恢复到层次结构不是来自 onResume() 而是来自 onWindowFocusChanged() :

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

另见:

eglCreateWindowSurface: native_window_api_connect failed

fixing-common-android-lifecycle-issues

链接

GLSurfaceView , GLSurfaceView.Renderer

关于android - Opengls eglCreateWindowSurface GL 错误 EGL_BAD_ALLOC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312209/

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