Rendering with GLTextureView
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
GLTextureView has the same API as GLSurfaceView: configure the EGL context, set a
GLSurfaceView.Renderer, and forward the lifecycle events of its host. The difference is that it
is a TextureView, so it can be composed with any other view.
Every Kotlin example on this page is compiled and run by DocumentationExamplesTest in the
lib module.
|
Subclassing GLTextureView
The usual pattern is a subclass that configures the view and installs its renderer in init.
The @JvmOverloads constructor lets the view be inflated from XML.
/**
* Translucent view drawing an OpenGL ES 2.0 scene. It can be declared in an XML layout.
*/
class SceneTextureView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : GLTextureView(context, attrs, defStyleAttr) {
val renderer = SceneRenderer()
init {
// must be called before setRenderer
setEGLContextClientVersion(2)
// RGBA_8888 with a 16-bit depth buffer, so that the background can be transparent
setEGLConfigChooser(8, 8, 8, 8, 16, 0)
isOpaque = false
setRenderer(renderer)
// only draw when requestRender() is called, to save battery
renderMode = RENDER_MODE_WHEN_DIRTY
}
}
All configuration methods (setEGLContextClientVersion, setEGLConfigChooser,
setEGLContextFactory, setEGLWindowSurfaceFactory) must be called before
setRenderer; calling them afterwards throws IllegalStateException. setRenderer itself can
only be called once, and renderMode only takes effect after it.
Writing a renderer
Any GLSurfaceView.Renderer works. Its callbacks run on a dedicated GL thread owned by the view.
This renderer uses a CameraConverter to obtain the
model-view-projection matrix for its shaders:
/**
* Any GLSurfaceView.Renderer works with GLTextureView.
*/
class SceneRenderer : GLSurfaceView.Renderer {
// only modified on the GL thread, through GLTextureView.queueEvent
var camera: PinholeCamera? = null
var lastModelViewProjectionMatrix: FloatArray? = null
private set
private var converter: CameraConverter? = null
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
// fully transparent background, so that views behind this one remain visible
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
GLES20.glEnable(GLES20.GL_DEPTH_TEST)
// compile shaders, create buffers and textures here
}
override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
GLES20.glViewport(0, 0, width, height)
val converter = CameraConverter(width, height)
converter.isModelViewProjectionMatrixComputationEnabled = true
this.converter = converter
}
override fun onDrawFrame(gl: GL10?) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
val converter = converter ?: return
val camera = camera ?: return
if (converter.camera !== camera) {
converter.camera = camera
}
val modelViewProjectionMatrix = converter.modelViewProjectionMatrix ?: return
drawScene(modelViewProjectionMatrix)
}
private fun drawScene(modelViewProjectionMatrix: FloatArray) {
lastModelViewProjectionMatrix = modelViewProjectionMatrix
// use your shader program, upload the matrix with
// GLES20.glUniformMatrix4fv(mvpHandle, 1, false, modelViewProjectionMatrix, 0)
// and draw your geometry
}
}
To let views behind the scene show through, three things are needed: an EGL config with an alpha
channel (setEGLConfigChooser(8, 8, 8, 8, 16, 0)), isOpaque = false on the view, and a clear
color with alpha 0.
Composing with other views
Declare the view in a layout like any other. Here the OpenGL scene is drawn on top of an image:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- regular views drawn behind the OpenGL scene -->
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<!-- translucent OpenGL scene composed on top of them -->
<com.example.scene.SceneTextureView
android:id="@+id/scene"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.9" />
</FrameLayout>
Or create it in code. Because it is a regular view, it can be animated:
val view = GLTextureView(context)
view.setEGLContextClientVersion(2)
view.setEGLConfigChooser(8, 8, 8, 8, 16, 0)
view.isOpaque = false
view.setRenderer(renderer)
view.renderMode = GLTextureView.RENDER_MODE_WHEN_DIRTY
container.addView(view)
// GLTextureView is a regular view: it can be animated, faded or transformed
view.animate().alpha(0.5f).rotation(45.0f).setDuration(300L)
Lifecycle
The host must forward onPause and onResume, so that the rendering thread stops when the
activity is not visible and the EGL context is recreated when it comes back:
/**
* Forwards activity lifecycle events to the view, so that its rendering thread is paused and
* resumed.
*/
class SceneActivity : Activity() {
private lateinit var sceneView: SceneTextureView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sceneView = SceneTextureView(this)
setContentView(sceneView)
}
override fun onResume() {
super.onResume()
sceneView.onResume()
}
override fun onPause() {
sceneView.onPause()
super.onPause()
}
}
The rendering thread is started when the view is attached to a window and stopped when it is detached.
Render modes and updating the scene
| Mode | Behaviour |
|---|---|
|
|
|
A frame is drawn only when the surface is created or resized, or when |
State read by the renderer should be changed on the GL thread. queueEvent runs a block there;
requestRender can be called from any thread:
// runs on the GL thread, so the renderer state is never touched concurrently
view.queueEvent { renderer.camera = camera }
// with RENDER_MODE_WHEN_DIRTY, a new frame is only drawn when requested
view.requestRender()
Advanced configuration
| Method or property | Use |
|---|---|
|
OpenGL ES version of the context created by the default factories ( |
|
RGB_888 surface, with or without a 16-bit depth buffer. This (with depth) is the default. |
|
Exact color component sizes and minimum depth and stencil sizes. |
|
Fully custom EGL setup through the |
|
Wraps the |
|
|
|
Tries to keep the EGL context (and the textures, buffers and shaders it owns) while paused. Whether it is actually kept depends on the device. |
// check glGetError after every call and log every call made by the renderer
view.debugFlags = GLTextureView.DEBUG_CHECK_GL_ERROR or GLTextureView.DEBUG_LOG_GL_CALLS
// try to keep the EGL context (and its textures, buffers and shaders) while paused
view.preserveEGLContextOnPause = true
Migrating from GLSurfaceView
-
Replace
GLSurfaceViewwithcom.irurueta.android.glutils.GLTextureViewin layouts and subclasses. -
Replace
GLSurfaceView.RENDERMODE_WHEN_DIRTY/RENDERMODE_CONTINUOUSLYwithGLTextureView.RENDER_MODE_WHEN_DIRTY/RENDER_MODE_CONTINUOUSLY. -
Keep your existing
GLSurfaceView.Rendererimplementation as is.