Rendering any Android View directly to an OpenGL Texture
There are two magic classes that allow you to do this: Surface and SurfaceTexture. I would consider them both to be very poorly documented, and the documentation would have you believe they are quite limited in their usage but actually they are quite powerful. For example, SurfaceTexture's documentation states: "The image stream may come from either camera preview or video decode." This made me think no other source was possible but, as I state below, you can actually connect Surface to SurfaceTexture and that opens up your possibilities a whole lot more.
The SurfaceTexture is basically your entry point into the OpenGL layer. It is initialised with an OpenGL texture id, and performs all of it's rendering onto that texture.
The Surface class provides the abstraction required to perform View drawing onto hardware. It provides a Canvas object (through the lockCanvas
method), which is basically what an Android View uses for all it's drawing (the onDraw()
method of View actually takes in a Canvas). As it turns out, the Surface class takes in a SurfaceTexture in its constructor.
And that's basically it. The steps to render your view to opengl:
- Initialise an OpenGL texture
- Within an OpenGL context construct a SurfaceTexture with the texture id. Use
SurfaceTexture.setDefaultBufferSize(int width, int height)
to make sure you have enough space on the texture for the view to render. - Create a Surface constructed with the above SurfaceTexture.
- Within the View's onDraw, use the Canvas returned by Surface.lockCanvas to do the view drawing. You can obviously do this with any View, and not just WebView. Plus Canvas has a whole bunch of drawing methods, allowing you to do funky, funky things.
Written by Anuraag Sridhar
Related protips
5 Responses
Could you privide an simple example on how to do that? I found a good example on step 3-4 here: http://pastebin.com/J4uDgrZ8 but I'm not quite sure about the OpenGL parts 1-2 :) would really appreciate it given the poor documentation.
Any code examples for this?
I was able to implement a working solution. Posting the link to sources as well https://github.com/ArtemBogush/AndroidViewToGLRendering
here is a reference to the code with some explanation on how to use it:
https://stackoverflow.com/questions/12499396/is-it-possible-to-render-an-android-view-to-an-opengl-fbo-or-texture/29066625#29066625
The link repo shared by artembogush is very completely outdated