blob: 1c5b0258c71fcf51662cf388fa9582dbc9ba5c84 [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Graphics
2@jd:body
3
4<!--
Heidi von Markham1e7b8b72015-03-09 10:13:48 -07005 Copyright 2015 The Android Open Source Project
Robert Ly35f2fda2013-01-29 16:27:05 -08006
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 Murphye3ae3962014-09-02 17:30:57 -070019
Robert Ly35f2fda2013-01-29 16:27:05 -080020<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 Markhamb493fb62015-03-25 12:35:11 -070028<img style="float: right; margin: 0px 15px 15px 15px;"
29src="images/ape_fwk_hal_graphics.png" alt="Android Graphics HAL icon"/>
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070030
Clay Murphye3ae3962014-09-02 17:30:57 -070031<p>The Android framework offers a variety of graphics rendering APIs for 2D and
323D that interact with manufacturer implementations of graphics drivers, so it
33is important to have a good understanding of how those APIs work at a higher
34level. This page introduces the graphics hardware abstraction layer (HAL) upon
35which those drivers are built.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080036
Clay Murphye3ae3962014-09-02 17:30:57 -070037<p>Application developers draw images to the screen in two ways: with Canvas or
38OpenGL. See <a
39href="{@docRoot}devices/graphics/architecture.html">System-level graphics
40architecture</a> for a detailed description of Android graphics
41components.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080042
Clay Murphye3ae3962014-09-02 17:30:57 -070043<p><a
44href="http://developer.android.com/reference/android/graphics/Canvas.html">android.graphics.Canvas</a>
45is a 2D graphics API and is the most popular graphics API among developers.
46Canvas operations draw all the stock and custom <a
47href="http://developer.android.com/reference/android/view/View.html">android.view.View</a>s
48in Android. In Android, hardware acceleration for Canvas APIs is accomplished
49with a drawing library called OpenGLRenderer that translates Canvas operations
50to OpenGL operations so they can execute on the GPU.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080051
Clay Murphye3ae3962014-09-02 17:30:57 -070052<p>Beginning in Android 4.0, hardware-accelerated Canvas is enabled by default.
53Consequently, a hardware GPU that supports OpenGL ES 2.0 is mandatory for
Heidi von Markham40a90832015-03-17 13:27:16 -070054Android 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
56hardware-accelerated drawing path works and the differences in its behavior
57from that of the software drawing path.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080058
Clay Murphye3ae3962014-09-02 17:30:57 -070059<p>In addition to Canvas, the other main way that developers render graphics is
60by using OpenGL ES to directly render to a surface. Android provides OpenGL ES
Heidi von Markham40a90832015-03-17 13:27:16 -070061interfaces in the
62<a href="http://developer.android.com/reference/android/opengl/package-summary.html">android.opengl</a>
Clay Murphye3ae3962014-09-02 17:30:57 -070063package that developers can use to call into their GL implementations with the
64SDK or with native APIs provided in the <a
65href="https://developer.android.com/tools/sdk/ndk/index.html">Android
66NDK</a>.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080067
Bert McMeenf902a282015-03-06 14:44:22 -080068<p>Android implementers can test OpenGL ES functionality using the <a href="testing.html">drawElements Quality Program</a>, also known as deqp.</p>
Bert McMeen14031e92015-02-25 15:21:37 -080069
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070070<h2 id="android_graphics_components">Android graphics components</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -080071
Clay Murphye3ae3962014-09-02 17:30:57 -070072<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
74often consumed by SurfaceFlinger. Every window that is created on the Android
75platform is backed by a surface. All of the visible surfaces rendered are
76composited onto the display by SurfaceFlinger.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080077
Clay Murphye3ae3962014-09-02 17:30:57 -070078<p>The following diagram shows how the key components work together:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080079
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070080<img src="images/ape_fwk_graphics.png" alt="image-rendering components">
Robert Ly35f2fda2013-01-29 16:27:05 -080081
Clay Murphye3ae3962014-09-02 17:30:57 -070082<p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080083
Clay Murphye3ae3962014-09-02 17:30:57 -070084<p>The main components are described below:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080085
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070086<h3 id="image_stream_producers">Image Stream Producers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080087
Clay Murphye3ae3962014-09-02 17:30:57 -070088<p>An image stream producer can be anything that produces graphic buffers for
89consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video
90decoders.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080091
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070092<h3 id="image_stream_consumers">Image Stream Consumers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080093
Clay Murphye3ae3962014-09-02 17:30:57 -070094<p>The most common consumer of image streams is SurfaceFlinger, the system
95service that consumes the currently visible surfaces and composites them onto
96the display using information provided by the Window Manager. SurfaceFlinger is
97the only service that can modify the content of the display. SurfaceFlinger
98uses OpenGL and the Hardware Composer to compose a group of surfaces.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080099
Clay Murphye3ae3962014-09-02 17:30:57 -0700100<p>Other OpenGL ES apps can consume image streams as well, such as the camera
101app consuming a camera preview image stream. Non-GL applications can be
102consumers too, for example the ImageReader class.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800103
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700104<h3 id="window_manager">Window Manager</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800105
Clay Murphye3ae3962014-09-02 17:30:57 -0700106<p>The Android system service that controls a window, which is a container for
107views. A window is always backed by a surface. This service oversees
108lifecycles, input and focus events, screen orientation, transitions,
109animations, position, transforms, z-order, and many other aspects of a window.
110The Window Manager sends all of the window metadata to SurfaceFlinger so
111SurfaceFlinger can use that data to composite surfaces on the display.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800112
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700113<h3 id="hardware_composer">Hardware Composer</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800114
Clay Murphye3ae3962014-09-02 17:30:57 -0700115<p>The hardware abstraction for the display subsystem. SurfaceFlinger can
116delegate certain composition work to the Hardware Composer to offload work from
117OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So
118when SurfaceFlinger is actively compositing one buffer or two into a third, for
119instance, it is using OpenGL ES. This makes compositing lower power than having
120the GPU conduct all computation.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800121
Clay Murphye3ae3962014-09-02 17:30:57 -0700122<p>The Hardware Composer HAL conducts the other half of the work. This HAL is
123the central point for all Android graphics rendering. Hardware Composer must
124support events, one of which is VSYNC. Another is hotplug for plug-and-play
125HDMI support.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800126
Heidi von Markham40a90832015-03-17 13:27:16 -0700127<p>See the
128<a href="{@docRoot}devices/graphics.html#hardware_composer_hal">Hardware
129Composer HAL</a> section for more information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800130
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700131<h3 id="gralloc">Gralloc</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800132
Clay Murphye3ae3962014-09-02 17:30:57 -0700133<p>The graphics memory allocator is needed to allocate memory that is requested
134by image producers. See the <a
135href="{@docRoot}devices/graphics.html#gralloc">Gralloc HAL</a> section for more
136information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800137
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700138<h2 id="data_flow">Data flow</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -0800139
Clay Murphye3ae3962014-09-02 17:30:57 -0700140<p>See the following diagram for a depiction of the Android graphics
141pipeline:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800142
Clay Murphy0ddab122014-12-01 16:22:40 -0800143<img src="images/graphics_pipeline.png" alt="graphics data flow">
Robert Ly35f2fda2013-01-29 16:27:05 -0800144
Heidi von Markham40a90832015-03-17 13:27:16 -0700145<p class="img-caption"><strong>Figure 2.</strong> Graphic data flow through
Clay Murphye3ae3962014-09-02 17:30:57 -0700146Android</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800147
Clay Murphye3ae3962014-09-02 17:30:57 -0700148<p>The objects on the left are renderers producing graphics buffers, such as
149the home screen, status bar, and system UI. SurfaceFlinger is the compositor
150and Hardware Composer is the composer.</p>
151
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700152<h3 id="bufferqueue">BufferQueue</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700153
154<p>BufferQueues provide the glue between the Android graphics components. These
155are a pair of queues that mediate the constant cycle of buffers from the
156producer to the consumer. Once the producers hand off their buffers,
157SurfaceFlinger is responsible for compositing everything onto the display.</p>
158
159<p>See the following diagram for the BufferQueue communication process.</p>
160
Clay Murphy1c5c9262015-03-20 15:49:36 -0700161<img src="images/bufferqueue.png"
Heidi von Markham40a90832015-03-17 13:27:16 -0700162alt="BufferQueue communication process">
Clay Murphye3ae3962014-09-02 17:30:57 -0700163
164<p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication
165process</p>
166
167<p>BufferQueue contains the logic that ties image stream producers and image
168stream consumers together. Some examples of image producers are the camera
169previews produced by the camera HAL or OpenGL ES games. Some examples of image
170consumers are SurfaceFlinger or another app that displays an OpenGL ES stream,
171such as the camera app displaying the camera viewfinder.</p>
172
173<p>BufferQueue is a data structure that combines a buffer pool with a queue and
174uses Binder IPC to pass buffers between processes. The producer interface, or
175what you pass to somebody who wants to generate graphic buffers, is
176IGraphicBufferProducer (part of <a
177href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>).
178BufferQueue is often used to render to a Surface and consume with a GL
179Consumer, among other tasks.
180
181BufferQueue can operate in three different modes:</p>
182
183<p><em>Synchronous-like mode</em> - BufferQueue by default operates in a
184synchronous-like mode, in which every buffer that comes in from the producer
185goes out at the consumer. No buffer is ever discarded in this mode. And if the
186producer is too fast and creates buffers faster than they are being drained, it
187will block and wait for free buffers.</p>
188
189<p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking
190mode where it generates an error rather than waiting for a buffer in those
191cases. No buffer is ever discarded in this mode either. This is useful for
192avoiding potential deadlocks in application software that may not understand
193the complex dependencies of the graphics framework.</p>
194
195<p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard
196old buffers rather than generate errors or wait. For instance, if conducting GL
197rendering to a texture view and drawing as quickly as possible, buffers must be
198dropped.</p>
199
200<p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES
201client. So when SurfaceFlinger is actively compositing one buffer or two into a
202third, for instance, it is using OpenGL ES.</p>
203
204<p>The Hardware Composer HAL conducts the other half of the work. This HAL acts
205as the central point for all Android graphics rendering.</p>
206
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700207<h3 id="synchronization_framework">Synchronization framework</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700208
209<p>Since Android graphics offer no explicit parallelism, vendors have long
210implemented their own implicit synchronization within their own drivers. This
211is no longer required with the Android graphics synchronization framework. See
Heidi von Markham3b59cef2015-10-09 12:17:25 -0700212the
213<a href="{@docRoot}devices/graphics/implement.html#explicit_synchronization">Explicit
214synchronization</a> section for implementation instructions.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700215
216<p>The synchronization framework explicitly describes dependencies between
217different asynchronous operations in the system. The framework provides a
218simple API that lets components signal when buffers are released. It also
219allows synchronization primitives to be passed between drivers from the kernel
220to userspace and between userspace processes themselves.</p>
221
222<p>For example, an application may queue up work to be carried out in the GPU.
223The GPU then starts drawing that image. Although the image hasnt been drawn
224into memory yet, the buffer pointer can still be passed to the window
225compositor along with a fence that indicates when the GPU work will be
226finished. The window compositor may then start processing ahead of time and
227hand off the work to the display controller. In this manner, the CPU work can
228be done ahead of time. Once the GPU finishes, the display controller can
229immediately display the image.</p>
230
231<p>The synchronization framework also allows implementers to leverage
232synchronization resources in their own hardware components. Finally, the
233framework provides visibility into the graphics pipeline to aid in
Clay Murphy1c5c9262015-03-20 15:49:36 -0700234debugging.</p>