Sharing the GLES20 context and textures between different GLSurfaceViews?

不想你离开。 提交于 2019-12-01 05:32:39

问题


Is it possible to share the GLES20 context between different GLSurfaceViews (within one Activity)? Alternatively, how would one share a set of texture between different GLSurfaceViews?

On iOS, if you want to conserve memory and reuse (large) textures in different CAEAGLLayer-backed UIViews, you can pass around a EAGLContext object between them or use different EAGLContexts which share a common EAGLSharegroup object.

I wonder how to accomplish this on Android. Is there any equivalent technique?

Edit1

The initial suggestion, to implement your own EGLContextFactory, which will return the same EGLContext, doesn't work since every GLSurfaceViews dispatches the rendering to it's own private gl render thread and sharing the same EGLContext between different threads is not possible.

To rephrase my initial question: You have several GLSurfaceViews in one screen (one Activity) and you need to access a set of common but large texture in the individual EGLContext of every surfaces, but loading your textures multiple times exceeds the memory of your device. How would you share your textures between GLSurfaceViews then?


回答1:


The following code works on some of the devices, but not all of them:

public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
    EGLContext shared = .....;

    int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
    EGLContext context = egl.eglCreateContext(display, eglConfig, shared == null ? EGL10.EGL_NO_CONTEXT : shared,
        attrib_list);

    return context;
  }
}



回答2:


  • GLSurfaceView setEGLContextFactory
  • GLSurfaceView.java

It seems that setEGLContextFactory enables to use the same GLES20 context between different GLSurfaceViews.

pseudo code:

private class MyEGLContextFactory implements EGLContextFactory {
    private static EGLContext mEGLContext;

    public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
        /* create EGLContext for GLES20 in first time */
        return mEGLContext;
    }

    public void destroyContext(EGL10 egl, EGLDisplay display,
            EGLContext context) {
    }
}


来源:https://stackoverflow.com/questions/5675355/sharing-the-gles20-context-and-textures-between-different-glsurfaceviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!