As we know,in the OpenGL ES the class Renderer
on android has 3 function:onDrawFrame, onSurfaceChanged, onSurfaceCreated.
and we can load the texture in the onSurfaceCreated and onDrawFrame.
I want to know can i load the texture before the "Renderer".Likely, I have a class named "Map",and can i load the images texture in the "Map" before the "GLSurfaceView" created. or can i load the images texture before the function "onSurfaceCreated" and "onDrawFrame".
if someone know it,please help me.
Thanks!!
Not easily. But (almost) anything is possible.
The critical concept to understand here is that OpenGL calls operate on a current context. Before you can make any OpenGL calls, you need to create a context, and make it current. The context being current applies to a thread, so different threads can have different current contexts. I don't think it's clearly defined what happens if you attempt to make OpenGL calls without having a current context, but it certainly isn't anything useful. The most likely outcomes would be nothing at all, or a crash.
GLSurfaceView
handles all of the complexity of setting up contexts for you. It creates a rendering thread that runs independently from the main (UI) thread, creates a context, and makes that context current in the rendering thread. Once methods on your GLSurfaceView.Renderer
implementation are invoked, you already have a current context, and you're ready to make OpenGL calls.
Now, if you want to make OpenGL calls before all of that GLSurfaceView
magic happens, you have to do a lot of this yourself. You can call methods of the EGL14 class to create your own context, and make it current. It's slightly painful, but not terribly difficult. I don't have full code handy, but you should be able to either find examples, or figure it out from documentation. You will use methods like eglChooseConfig()
, eglCreateContext()
, eglMakeCurrent()
, and probably a handful more on the way.
Once you're done with that, you can make your OpenGL calls for creating textures, etc. But unfortunately, that's not the whole story yet.
Once your GLSurfaceView
comes to life, it will create its own context as usual. Different OpenGL contexts can share resources (like textures), but they don't do that by default. Since GLSurfaceView
knows nothing about the context you already created, it won't share resources with your context.
To enable resource sharing, you have to interfere in the creation of the context by GLSurfaceView
. You can do this with the setEGLContextFactory
method. This allows you to hook up your own context creation for the GLSurfaceView
, where you can now create a context that shares resources with the context you created earlier, and used for your texture loading.