Share OpenGL context on Android?

Share OpenGL context on Android?
0.0 0


Guys, I’ve failed to find the solution, either on this forum or in internet.

I want to load resources in another thread. On iOS this piece of code was enough:

@ EAGLContext *k_context = [[[EAGLContext alloc]
initWithAPI :kEAGLRenderingAPIOpenGLES1
sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]] autorelease];
[EAGLContext setCurrentContext:k_context];@

How do I achieve it on Android? I see there is no access to open GL context in cocos2d-x classes, right?


I don’t know how to share opengl context on android.
But i think you can use CCTextureCache::addImageAsync().


Thanks. This will required to rewrite a lot of code. And actually I do not see where is the difference - as addImageAsync() also uses threads, it should have the same problems. But as I assume it works, then there should be a solution for my case.

I see that addImageAsync() uses this function to just load the file, so probably no OpenGL interaction at all:


My sprite loading code looks like this:

CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage(nameFile); CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(tex, CCRectMake(0.0f, 0.0f, size.width, size.height)); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(frame, nameSprite);

As it does not work, I assume there is some openGL dependecy inside of these calls.


The new thread only load image, and create texture in the main thread.


As Minggo Zhang said, The new thread only load image, and create texture in the main thread.
So I think this is the core thought for rendering in cocos2dx, always create texture in the main thread because of platform compatibility, also some andriod gpu not support multi thread.
But if I want to load ccbi file in thread ,there seems to be impossible now because ccbreader do the textures loading when reading ccb file.
The possible way to loading ccb file in thread which I can image is something like:
1.Read ccb file to memory in thread ( can do )
2.Pre init ccb buffer, init all sequences ,and also let’s know the path of images( no such function )
3.Loading images in thread through for example CCTextureCache::addImageAsync() ( can do )
4.After init ccb buffer node graph ( no such function )
Am I right?


You may be able to create a shared context by overriding the GLSurfaceView class. Also you probably have to deal with some low-leve EGL stuff.
You can create a new EGL context using eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list)
There you can place your main threads context as shared context.

However, there may be some deviced that do not support multiple OpenGL contexts (depending on the GPU).