Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 1 | page.title=Graphics |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 5 | Copyright 2015 The Android Open Source Project |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 6 | |
| 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | you may not use this file except in compliance with the License. |
| 9 | You may obtain a copy of the License at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | --> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 19 | |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 20 | <div id="qv-wrapper"> |
| 21 | <div id="qv"> |
| 22 | <h2>In this document</h2> |
| 23 | <ol id="auto-toc"> |
| 24 | </ol> |
| 25 | </div> |
| 26 | </div> |
| 27 | |
Heidi von Markham | b493fb6 | 2015-03-25 12:35:11 -0700 | [diff] [blame] | 28 | <img style="float: right; margin: 0px 15px 15px 15px;" |
| 29 | src="images/ape_fwk_hal_graphics.png" alt="Android Graphics HAL icon"/> |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 30 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 31 | <p>The Android framework offers a variety of graphics rendering APIs for 2D and |
| 32 | 3D that interact with manufacturer implementations of graphics drivers, so it |
| 33 | is important to have a good understanding of how those APIs work at a higher |
| 34 | level. This page introduces the graphics hardware abstraction layer (HAL) upon |
| 35 | which those drivers are built.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 36 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 37 | <p>Application developers draw images to the screen in two ways: with Canvas or |
| 38 | OpenGL. See <a |
| 39 | href="{@docRoot}devices/graphics/architecture.html">System-level graphics |
| 40 | architecture</a> for a detailed description of Android graphics |
| 41 | components.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 42 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 43 | <p><a |
| 44 | href="http://developer.android.com/reference/android/graphics/Canvas.html">android.graphics.Canvas</a> |
| 45 | is a 2D graphics API and is the most popular graphics API among developers. |
| 46 | Canvas operations draw all the stock and custom <a |
| 47 | href="http://developer.android.com/reference/android/view/View.html">android.view.View</a>s |
| 48 | in Android. In Android, hardware acceleration for Canvas APIs is accomplished |
| 49 | with a drawing library called OpenGLRenderer that translates Canvas operations |
| 50 | to OpenGL operations so they can execute on the GPU.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 51 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 52 | <p>Beginning in Android 4.0, hardware-accelerated Canvas is enabled by default. |
| 53 | Consequently, a hardware GPU that supports OpenGL ES 2.0 is mandatory for |
Heidi von Markham | 40a9083 | 2015-03-17 13:27:16 -0700 | [diff] [blame] | 54 | Android 4.0 and later devices. See the |
| 55 | <a href="https://developer.android.com/guide/topics/graphics/hardware-accel.html">Hardware Acceleration guide</a> for an explanation of how the |
| 56 | hardware-accelerated drawing path works and the differences in its behavior |
| 57 | from that of the software drawing path.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 58 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 59 | <p>In addition to Canvas, the other main way that developers render graphics is |
| 60 | by using OpenGL ES to directly render to a surface. Android provides OpenGL ES |
Heidi von Markham | 40a9083 | 2015-03-17 13:27:16 -0700 | [diff] [blame] | 61 | interfaces in the |
| 62 | <a href="http://developer.android.com/reference/android/opengl/package-summary.html">android.opengl</a> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 63 | package that developers can use to call into their GL implementations with the |
| 64 | SDK or with native APIs provided in the <a |
| 65 | href="https://developer.android.com/tools/sdk/ndk/index.html">Android |
| 66 | NDK</a>.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 67 | |
Bert McMeen | f902a28 | 2015-03-06 14:44:22 -0800 | [diff] [blame] | 68 | <p>Android implementers can test OpenGL ES functionality using the <a href="testing.html">drawElements Quality Program</a>, also known as deqp.</p> |
Bert McMeen | 14031e9 | 2015-02-25 15:21:37 -0800 | [diff] [blame] | 69 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 70 | <h2 id="android_graphics_components">Android graphics components</h2> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 71 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 72 | <p>No matter what rendering API developers use, everything is rendered onto a |
| 73 | "surface." The surface represents the producer side of a buffer queue that is |
| 74 | often consumed by SurfaceFlinger. Every window that is created on the Android |
| 75 | platform is backed by a surface. All of the visible surfaces rendered are |
| 76 | composited onto the display by SurfaceFlinger.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 77 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 78 | <p>The following diagram shows how the key components work together:</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 79 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 80 | <img src="images/ape_fwk_graphics.png" alt="image-rendering components"> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 81 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 82 | <p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 83 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 84 | <p>The main components are described below:</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 85 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 86 | <h3 id="image_stream_producers">Image Stream Producers</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 87 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 88 | <p>An image stream producer can be anything that produces graphic buffers for |
| 89 | consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video |
| 90 | decoders.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 91 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 92 | <h3 id="image_stream_consumers">Image Stream Consumers</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 93 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 94 | <p>The most common consumer of image streams is SurfaceFlinger, the system |
| 95 | service that consumes the currently visible surfaces and composites them onto |
| 96 | the display using information provided by the Window Manager. SurfaceFlinger is |
| 97 | the only service that can modify the content of the display. SurfaceFlinger |
| 98 | uses OpenGL and the Hardware Composer to compose a group of surfaces.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 99 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 100 | <p>Other OpenGL ES apps can consume image streams as well, such as the camera |
| 101 | app consuming a camera preview image stream. Non-GL applications can be |
| 102 | consumers too, for example the ImageReader class.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 103 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 104 | <h3 id="window_manager">Window Manager</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 105 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 106 | <p>The Android system service that controls a window, which is a container for |
| 107 | views. A window is always backed by a surface. This service oversees |
| 108 | lifecycles, input and focus events, screen orientation, transitions, |
| 109 | animations, position, transforms, z-order, and many other aspects of a window. |
| 110 | The Window Manager sends all of the window metadata to SurfaceFlinger so |
| 111 | SurfaceFlinger can use that data to composite surfaces on the display.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 112 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 113 | <h3 id="hardware_composer">Hardware Composer</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 114 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 115 | <p>The hardware abstraction for the display subsystem. SurfaceFlinger can |
| 116 | delegate certain composition work to the Hardware Composer to offload work from |
| 117 | OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So |
| 118 | when SurfaceFlinger is actively compositing one buffer or two into a third, for |
| 119 | instance, it is using OpenGL ES. This makes compositing lower power than having |
| 120 | the GPU conduct all computation.</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 121 | |
Heidi von Markham | 5246dde | 2016-01-04 12:37:10 -0800 | [diff] [blame] | 122 | <p>The <a href="{@docRoot}devices/graphics/architecture.html#hwcomposer">Hardware |
| 123 | Composer HAL</a> conducts the other half of the work and is the central point |
| 124 | for all Android graphics rendering. The Hardware Composer must support events, |
| 125 | one of which is VSYNC (another is hotplug for plug-and-playHDMI support).</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 126 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 127 | <h3 id="gralloc">Gralloc</h3> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 128 | |
Heidi von Markham | 5246dde | 2016-01-04 12:37:10 -0800 | [diff] [blame] | 129 | <p>The graphics memory allocator (Gralloc) is needed to allocate memory |
| 130 | requested by image producers. For details, see <a |
| 131 | href="{@docRoot}devices/graphics/architecture.html#gralloc_HAL">Gralloc HAL</a>. |
| 132 | </p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 133 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 134 | <h2 id="data_flow">Data flow</h2> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 135 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 136 | <p>See the following diagram for a depiction of the Android graphics |
| 137 | pipeline:</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 138 | |
Clay Murphy | 0ddab12 | 2014-12-01 16:22:40 -0800 | [diff] [blame] | 139 | <img src="images/graphics_pipeline.png" alt="graphics data flow"> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 140 | |
Heidi von Markham | 40a9083 | 2015-03-17 13:27:16 -0700 | [diff] [blame] | 141 | <p class="img-caption"><strong>Figure 2.</strong> Graphic data flow through |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 142 | Android</p> |
Robert Ly | 35f2fda | 2013-01-29 16:27:05 -0800 | [diff] [blame] | 143 | |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 144 | <p>The objects on the left are renderers producing graphics buffers, such as |
| 145 | the home screen, status bar, and system UI. SurfaceFlinger is the compositor |
| 146 | and Hardware Composer is the composer.</p> |
| 147 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 148 | <h3 id="bufferqueue">BufferQueue</h3> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 149 | |
| 150 | <p>BufferQueues provide the glue between the Android graphics components. These |
| 151 | are a pair of queues that mediate the constant cycle of buffers from the |
| 152 | producer to the consumer. Once the producers hand off their buffers, |
| 153 | SurfaceFlinger is responsible for compositing everything onto the display.</p> |
| 154 | |
| 155 | <p>See the following diagram for the BufferQueue communication process.</p> |
| 156 | |
Clay Murphy | 1c5c926 | 2015-03-20 15:49:36 -0700 | [diff] [blame] | 157 | <img src="images/bufferqueue.png" |
Heidi von Markham | 40a9083 | 2015-03-17 13:27:16 -0700 | [diff] [blame] | 158 | alt="BufferQueue communication process"> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 159 | |
| 160 | <p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication |
| 161 | process</p> |
| 162 | |
| 163 | <p>BufferQueue contains the logic that ties image stream producers and image |
| 164 | stream consumers together. Some examples of image producers are the camera |
| 165 | previews produced by the camera HAL or OpenGL ES games. Some examples of image |
| 166 | consumers are SurfaceFlinger or another app that displays an OpenGL ES stream, |
| 167 | such as the camera app displaying the camera viewfinder.</p> |
| 168 | |
| 169 | <p>BufferQueue is a data structure that combines a buffer pool with a queue and |
| 170 | uses Binder IPC to pass buffers between processes. The producer interface, or |
| 171 | what you pass to somebody who wants to generate graphic buffers, is |
| 172 | IGraphicBufferProducer (part of <a |
| 173 | href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>). |
| 174 | BufferQueue is often used to render to a Surface and consume with a GL |
| 175 | Consumer, among other tasks. |
| 176 | |
| 177 | BufferQueue can operate in three different modes:</p> |
| 178 | |
| 179 | <p><em>Synchronous-like mode</em> - BufferQueue by default operates in a |
| 180 | synchronous-like mode, in which every buffer that comes in from the producer |
| 181 | goes out at the consumer. No buffer is ever discarded in this mode. And if the |
| 182 | producer is too fast and creates buffers faster than they are being drained, it |
| 183 | will block and wait for free buffers.</p> |
| 184 | |
| 185 | <p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking |
| 186 | mode where it generates an error rather than waiting for a buffer in those |
| 187 | cases. No buffer is ever discarded in this mode either. This is useful for |
| 188 | avoiding potential deadlocks in application software that may not understand |
| 189 | the complex dependencies of the graphics framework.</p> |
| 190 | |
| 191 | <p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard |
| 192 | old buffers rather than generate errors or wait. For instance, if conducting GL |
| 193 | rendering to a texture view and drawing as quickly as possible, buffers must be |
| 194 | dropped.</p> |
| 195 | |
| 196 | <p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES |
| 197 | client. So when SurfaceFlinger is actively compositing one buffer or two into a |
| 198 | third, for instance, it is using OpenGL ES.</p> |
| 199 | |
| 200 | <p>The Hardware Composer HAL conducts the other half of the work. This HAL acts |
| 201 | as the central point for all Android graphics rendering.</p> |
| 202 | |
Heidi von Markham | 1e7b8b7 | 2015-03-09 10:13:48 -0700 | [diff] [blame] | 203 | <h3 id="synchronization_framework">Synchronization framework</h3> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 204 | |
| 205 | <p>Since Android graphics offer no explicit parallelism, vendors have long |
| 206 | implemented their own implicit synchronization within their own drivers. This |
| 207 | is no longer required with the Android graphics synchronization framework. See |
Heidi von Markham | 3b59cef | 2015-10-09 12:17:25 -0700 | [diff] [blame] | 208 | the |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame] | 209 | <a href="{@docRoot}devices/graphics/implement-vsync.html#explicit_synchronization">Explicit |
Heidi von Markham | 3b59cef | 2015-10-09 12:17:25 -0700 | [diff] [blame] | 210 | synchronization</a> section for implementation instructions.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 211 | |
| 212 | <p>The synchronization framework explicitly describes dependencies between |
| 213 | different asynchronous operations in the system. The framework provides a |
| 214 | simple API that lets components signal when buffers are released. It also |
| 215 | allows synchronization primitives to be passed between drivers from the kernel |
| 216 | to userspace and between userspace processes themselves.</p> |
| 217 | |
| 218 | <p>For example, an application may queue up work to be carried out in the GPU. |
| 219 | The GPU then starts drawing that image. Although the image hasn’t been drawn |
| 220 | into memory yet, the buffer pointer can still be passed to the window |
| 221 | compositor along with a fence that indicates when the GPU work will be |
| 222 | finished. The window compositor may then start processing ahead of time and |
| 223 | hand off the work to the display controller. In this manner, the CPU work can |
| 224 | be done ahead of time. Once the GPU finishes, the display controller can |
| 225 | immediately display the image.</p> |
| 226 | |
| 227 | <p>The synchronization framework also allows implementers to leverage |
| 228 | synchronization resources in their own hardware components. Finally, the |
| 229 | framework provides visibility into the graphics pipeline to aid in |
Clay Murphy | 1c5c926 | 2015-03-20 15:49:36 -0700 | [diff] [blame] | 230 | debugging.</p> |