开发者

Sharing the GLES20 context and textures between different GLSurfaceViews?

开发者 https://www.devze.com 2023-02-25 05:46 出处:网络
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?

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 sam开发者_开发知识库e 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?


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


  • 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) {
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消