The renderer is responsible for making OpenGL calls to render a frame.
GLSurfaceView clients typically create their own classes that implement this interface, and then call GLSurfaceView.setRenderer to register the renderer with the GLSurfaceView.
Threading
The renderer will be called on a separate thread, so that rendering performance is decoupled from the UI thread. Clients typically need to communicate with the renderer from the UI thread, because that's where input events are received. Clients can communicate using any of the standard Java techniques for cross-thread communication, or they can use the GLSurfaceView.queueEvent(Runnable) convenience method.
EGL Context Lost
There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(javax.microedition.khronos.opengles.GL10,javax.microedition.khronos.egl.EGLConfig) method is a convenient place to do this.
abstractfun onSurfaceChanged( gl:GL10!, width:Int, height:Int ): Unit
Called when the surface changed size.
Called after the surface is created and whenever the OpenGL ES surface size changes.
Typically you will set your viewport here. If your camera is fixed then you could also set your projection matrix here:
voidonSurfaceChanged(GL10gl,intwidth,intheight){gl.glViewport(0,0,width,height);// for a fixed camera, set the projection toofloatratio=(float)width/height;gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();gl.glFrustumf(-ratio,ratio,-1,1,1,10);}
Parameters
gl
GL10!: the GL interface. Use instanceof to test if the interface supports GL11 or higher interfaces.
Called when the rendering thread starts and whenever the EGL context is lost. The EGL context will typically be lost when the Android device awakes after going to sleep.
Since this method is called at the beginning of rendering, as well as every time the EGL context is lost, this method is a convenient place to put code to create resources that need to be created when the rendering starts, and that need to be recreated when the EGL context is lost. Textures are an example of a resource that you might want to create here.
Note that when the EGL context is lost, all OpenGL resources associated with that context will be automatically deleted. You do not need to call the corresponding "glDelete" methods such as glDeleteTextures to manually delete these lost resources.
Parameters
gl
GL10!: the GL interface. Use instanceof to test if the interface supports GL11 or higher interfaces.
config
EGLConfig!: the EGLConfig of the created surface. Can be used to create matching pbuffers.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# GLSurfaceView.Renderer\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nRenderer\n========\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/opengl/GLSurfaceView.Renderer \"View this page in Java\") \n\n```\ninterface Renderer\n```\n\n|--------------------------------------------|\n| [android.opengl.GLSurfaceView.Renderer](#) |\n\nA generic renderer interface.\n\nThe renderer is responsible for making OpenGL calls to render a frame.\n\nGLSurfaceView clients typically create their own classes that implement this interface, and then call [GLSurfaceView.setRenderer](/reference/kotlin/android/opengl/GLSurfaceView#setRenderer(android.opengl.GLSurfaceView.Renderer)) to register the renderer with the GLSurfaceView.\n\n| ### Developer Guides\n|\n| For more information about how to use OpenGL, read the [OpenGL](https://developer.android.com/guide/topics/graphics/opengl.html) developer guide.\n\n### Threading\n\nThe renderer will be called on a separate thread, so that rendering performance is decoupled from the UI thread. Clients typically need to communicate with the renderer from the UI thread, because that's where input events are received. Clients can communicate using any of the standard Java techniques for cross-thread communication, or they can use the [GLSurfaceView.queueEvent(Runnable)](/reference/kotlin/android/opengl/GLSurfaceView#queueEvent(java.lang.Runnable)) convenience method.\n\n### EGL Context Lost\n\nThere are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The [onSurfaceCreated(javax.microedition.khronos.opengles.GL10,javax.microedition.khronos.egl.EGLConfig)](#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,%20javax.microedition.khronos.egl.EGLConfig)) method is a convenient place to do this.\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onDrawFrame](#onDrawFrame(javax.microedition.khronos.opengles.GL10))`(`gl:` `[GL10](../../javax/microedition/khronos/opengles/GL10.html#)!`)` Called to draw the current frame. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onSurfaceChanged](#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,%20kotlin.Int,%20kotlin.Int))`(`gl:` `[GL10](../../javax/microedition/khronos/opengles/GL10.html#)!`, `width:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `height:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Called when the surface changed size. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onSurfaceCreated](#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,%20javax.microedition.khronos.egl.EGLConfig))`(`gl:` `[GL10](../../javax/microedition/khronos/opengles/GL10.html#)!`, `config:` `[EGLConfig](../../javax/microedition/khronos/egl/EGLConfig.html#)!`)` Called when the surface is created or recreated. |\n\nPublic methods\n--------------\n\n### onDrawFrame\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onDrawFrame(gl: GL10!): Unit\n```\n\nCalled to draw the current frame.\n\nThis method is responsible for drawing the current frame.\n\nThe implementation of this method typically looks like this: \n\n```kotlin\nvoid onDrawFrame(GL10 gl) {\n gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);\n //... other gl calls to render the scene ...\n }\n \n```\n\n| Parameters ||\n|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `gl` | [GL10](../../javax/microedition/khronos/opengles/GL10.html#)!: the GL interface. Use `instanceof` to test if the interface supports GL11 or higher interfaces. |\n\n### onSurfaceChanged\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onSurfaceChanged(\n gl: GL10!, \n width: Int, \n height: Int\n): Unit\n```\n\nCalled when the surface changed size.\n\nCalled after the surface is created and whenever the OpenGL ES surface size changes.\n\nTypically you will set your viewport here. If your camera is fixed then you could also set your projection matrix here: \n\n```kotlin\nvoid onSurfaceChanged(GL10 gl, int width, int height) {\n gl.glViewport(0, 0, width, height);\n // for a fixed camera, set the projection too\n float ratio = (float) width / height;\n gl.glMatrixMode(GL10.GL_PROJECTION);\n gl.glLoadIdentity();\n gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);\n }\n \n```\n\n| Parameters ||\n|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `gl` | [GL10](../../javax/microedition/khronos/opengles/GL10.html#)!: the GL interface. Use `instanceof` to test if the interface supports GL11 or higher interfaces. |\n| `width` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): |\n| `height` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): |\n\n### onSurfaceCreated\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onSurfaceCreated(\n gl: GL10!, \n config: EGLConfig!\n): Unit\n```\n\nCalled when the surface is created or recreated.\n\nCalled when the rendering thread starts and whenever the EGL context is lost. The EGL context will typically be lost when the Android device awakes after going to sleep.\n\nSince this method is called at the beginning of rendering, as well as every time the EGL context is lost, this method is a convenient place to put code to create resources that need to be created when the rendering starts, and that need to be recreated when the EGL context is lost. Textures are an example of a resource that you might want to create here.\n\nNote that when the EGL context is lost, all OpenGL resources associated with that context will be automatically deleted. You do not need to call the corresponding \"glDelete\" methods such as glDeleteTextures to manually delete these lost resources.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `gl` | [GL10](../../javax/microedition/khronos/opengles/GL10.html#)!: the GL interface. Use `instanceof` to test if the interface supports GL11 or higher interfaces. |\n| `config` | [EGLConfig](../../javax/microedition/khronos/egl/EGLConfig.html#)!: the EGLConfig of the created surface. Can be used to create matching pbuffers. |"]]