Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I share a external texture between 2 OpenGL contexts, Android

I'm creating 2 threads. Each thread has its own EGL contexts. One thread is in native, where I will render to a texture, another thread is in Java, in which I want to sample from that texture and render to the screen / to an encoder (doesn't matter). I can't make it work. I've tried generating the texture on either of the threads. What I noticed is the texture Ids are duplicated on both threads (I have other textures that are not meant to be shared).

My question is, is it possible to share a texture between 2 threads (and contexts)?

EDIT: SOLUTION

Thanks to Andon and some Googling, I was able to make it work. I created one context in Java on thread one, and the called eglGetCurrentContext() to get the EGLContext in C++. Later, I created the second context in C++ on the second thread with:

eglCreateContext(mEglDisplay, mEglConfig, sharedContext, contextAttribs);

Where sharedContext is the first context.

like image 458
Michael P Avatar asked Aug 12 '15 01:08

Michael P


People also ask

What is shared context in OpenGL?

A context's objects can be shared with other contexts. Most OpenGL objects are sharable, including Sync Objects and GLSL Objects. Container Objects are not sharable, nor are Query Objects. Any object sharing must be made explicitly, either as the context is created or before a newly created context creates any objects.

How do I change the texture of OpenGL?

To replace texture data, just bind the texture and call glTexImage2D with the new data. bindTexture, at least the opengl one, just binds a texture name to a specific target, GL_TEXTURE_2D here. It does not delete or replace anything, just put a particular texture in use.

What is OpenGL es texture?

The texture is an OpenGL object that contains one or more images that all have the same image format. To draw texture in a square shape, we need to tell each vertex of the square which part of the texture it corresponds to. So each vertex of the shape has a texture coordinate associated with them.


1 Answers

Yes, resource sharing between contexts is possible.

The command streams in shared contexts are not synchronized though; if you upload data in one thread and use it in another you have to be extra careful that the upload is actually finished first (glFinish (...) followed by some synchronization construct of your own, like a semaphore, will do).

Now, the million dollar question—do you have any control over the creation of these contexts? That is necessary to do resource sharing in EGL. You only need to create one yourself; if you have the other given to you already, you can use it as your share context when creating the second.

like image 159
Andon M. Coleman Avatar answered Sep 20 '22 13:09

Andon M. Coleman