Sharing the GLES20 context and textures between di

2019-02-17 23:34发布

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?

2条回答
家丑人穷心不美
2楼-- · 2019-02-17 23:49

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) {
    }
}
查看更多
对你真心纯属浪费
3楼-- · 2019-02-18 00:00

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;
  }
}
查看更多
登录 后发表回答