blob: c8f11e849dd0ffb72d2e48482fdcdea5feff6f5a [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Graphics
2@jd:body
3
4<!--
Clay Murphy74643ca2014-09-02 17:30:57 -07005 Copyright 2014 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 Murphy74643ca2014-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
Clay Murphy74643ca2014-09-02 17:30:57 -070028<p>The Android framework offers a variety of graphics rendering APIs for 2D and
293D that interact with manufacturer implementations of graphics drivers, so it
30is important to have a good understanding of how those APIs work at a higher
31level. This page introduces the graphics hardware abstraction layer (HAL) upon
32which those drivers are built.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080033
Clay Murphy74643ca2014-09-02 17:30:57 -070034<p>Application developers draw images to the screen in two ways: with Canvas or
35OpenGL. See <a
36href="{@docRoot}devices/graphics/architecture.html">System-level graphics
37architecture</a> for a detailed description of Android graphics
38components.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080039
Clay Murphy74643ca2014-09-02 17:30:57 -070040<p><a
41href="http://developer.android.com/reference/android/graphics/Canvas.html">android.graphics.Canvas</a>
42is a 2D graphics API and is the most popular graphics API among developers.
43Canvas operations draw all the stock and custom <a
44href="http://developer.android.com/reference/android/view/View.html">android.view.View</a>s
45in Android. In Android, hardware acceleration for Canvas APIs is accomplished
46with a drawing library called OpenGLRenderer that translates Canvas operations
47to OpenGL operations so they can execute on the GPU.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080048
Clay Murphy74643ca2014-09-02 17:30:57 -070049<p>Beginning in Android 4.0, hardware-accelerated Canvas is enabled by default.
50Consequently, a hardware GPU that supports OpenGL ES 2.0 is mandatory for
51Android 4.0 and later devices. See the <a
52href="https://developer.android.com/guide/topics/graphics/hardware-accel.html">Hardware
53Acceleration guide</a> for an explanation of how the hardware-accelerated
54drawing path works and the differences in its behavior from that of the
55software drawing path.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080056
Clay Murphy74643ca2014-09-02 17:30:57 -070057<p>In addition to Canvas, the other main way that developers render graphics is
58by using OpenGL ES to directly render to a surface. Android provides OpenGL ES
59interfaces in the <a
60href="http://developer.android.com/reference/android/opengl/package-summary.html">android.opengl</a>
61package that developers can use to call into their GL implementations with the
62SDK or with native APIs provided in the <a
63href="https://developer.android.com/tools/sdk/ndk/index.html">Android
64NDK</a>.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080065
Clay Murphy74643ca2014-09-02 17:30:57 -070066<h2 id=android_graphics_components>Android graphics components</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -080067
Clay Murphy74643ca2014-09-02 17:30:57 -070068<p>No matter what rendering API developers use, everything is rendered onto a
69"surface." The surface represents the producer side of a buffer queue that is
70often consumed by SurfaceFlinger. Every window that is created on the Android
71platform is backed by a surface. All of the visible surfaces rendered are
72composited onto the display by SurfaceFlinger.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080073
Clay Murphy74643ca2014-09-02 17:30:57 -070074<p>The following diagram shows how the key components work together:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080075
Clay Murphy74643ca2014-09-02 17:30:57 -070076<img src="graphics/images/graphics_surface.png" alt="image-rendering components">
Robert Ly35f2fda2013-01-29 16:27:05 -080077
Clay Murphy74643ca2014-09-02 17:30:57 -070078<p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080079
Clay Murphy74643ca2014-09-02 17:30:57 -070080<p>The main components are described below:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080081
Clay Murphy74643ca2014-09-02 17:30:57 -070082<h3 id=image_stream_producers>Image Stream Producers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080083
Clay Murphy74643ca2014-09-02 17:30:57 -070084<p>An image stream producer can be anything that produces graphic buffers for
85consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video
86decoders.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080087
Clay Murphy74643ca2014-09-02 17:30:57 -070088<h3 id=image_stream_consumers>Image Stream Consumers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080089
Clay Murphy74643ca2014-09-02 17:30:57 -070090<p>The most common consumer of image streams is SurfaceFlinger, the system
91service that consumes the currently visible surfaces and composites them onto
92the display using information provided by the Window Manager. SurfaceFlinger is
93the only service that can modify the content of the display. SurfaceFlinger
94uses OpenGL and the Hardware Composer to compose a group of surfaces.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080095
Clay Murphy74643ca2014-09-02 17:30:57 -070096<p>Other OpenGL ES apps can consume image streams as well, such as the camera
97app consuming a camera preview image stream. Non-GL applications can be
98consumers too, for example the ImageReader class.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080099
Clay Murphy74643ca2014-09-02 17:30:57 -0700100<h3 id=window_manager>Window Manager</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800101
Clay Murphy74643ca2014-09-02 17:30:57 -0700102<p>The Android system service that controls a window, which is a container for
103views. A window is always backed by a surface. This service oversees
104lifecycles, input and focus events, screen orientation, transitions,
105animations, position, transforms, z-order, and many other aspects of a window.
106The Window Manager sends all of the window metadata to SurfaceFlinger so
107SurfaceFlinger can use that data to composite surfaces on the display.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800108
Clay Murphy74643ca2014-09-02 17:30:57 -0700109<h3 id=hardware_composer>Hardware Composer</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800110
Clay Murphy74643ca2014-09-02 17:30:57 -0700111<p>The hardware abstraction for the display subsystem. SurfaceFlinger can
112delegate certain composition work to the Hardware Composer to offload work from
113OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So
114when SurfaceFlinger is actively compositing one buffer or two into a third, for
115instance, it is using OpenGL ES. This makes compositing lower power than having
116the GPU conduct all computation.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800117
Clay Murphy74643ca2014-09-02 17:30:57 -0700118<p>The Hardware Composer HAL conducts the other half of the work. This HAL is
119the central point for all Android graphics rendering. Hardware Composer must
120support events, one of which is VSYNC. Another is hotplug for plug-and-play
121HDMI support.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800122
Clay Murphy74643ca2014-09-02 17:30:57 -0700123<p>See the <a href="{@docRoot}devices/graphics.html#hardware_composer_hal">Hardware Composer
124HAL</a> section for more information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800125
Clay Murphy74643ca2014-09-02 17:30:57 -0700126<h3 id=gralloc>Gralloc</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800127
Clay Murphy74643ca2014-09-02 17:30:57 -0700128<p>The graphics memory allocator is needed to allocate memory that is requested
129by image producers. See the <a
130href="{@docRoot}devices/graphics.html#gralloc">Gralloc HAL</a> section for more
131information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800132
Clay Murphy74643ca2014-09-02 17:30:57 -0700133<h2 id=data_flow>Data flow</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -0800134
Clay Murphy74643ca2014-09-02 17:30:57 -0700135<p>See the following diagram for a depiction of the Android graphics
136pipeline:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800137
Clay Murphy74643ca2014-09-02 17:30:57 -0700138<img src="graphics/images/graphics_pipeline.png" alt="graphics data flow">
Robert Ly35f2fda2013-01-29 16:27:05 -0800139
Clay Murphy74643ca2014-09-02 17:30:57 -0700140<p class="img-caption"><strong>Figure 2.</strong> How graphic data flow through
141Android</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800142
Clay Murphy74643ca2014-09-02 17:30:57 -0700143<p>The objects on the left are renderers producing graphics buffers, such as
144the home screen, status bar, and system UI. SurfaceFlinger is the compositor
145and Hardware Composer is the composer.</p>
146
147<h3 id=bufferqueue>BufferQueue</h3>
148
149<p>BufferQueues provide the glue between the Android graphics components. These
150are a pair of queues that mediate the constant cycle of buffers from the
151producer to the consumer. Once the producers hand off their buffers,
152SurfaceFlinger is responsible for compositing everything onto the display.</p>
153
154<p>See the following diagram for the BufferQueue communication process.</p>
155
156<img src="graphics/images/bufferqueue.png" alt="BufferQueue communication process">
157
158<p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication
159process</p>
160
161<p>BufferQueue contains the logic that ties image stream producers and image
162stream consumers together. Some examples of image producers are the camera
163previews produced by the camera HAL or OpenGL ES games. Some examples of image
164consumers are SurfaceFlinger or another app that displays an OpenGL ES stream,
165such as the camera app displaying the camera viewfinder.</p>
166
167<p>BufferQueue is a data structure that combines a buffer pool with a queue and
168uses Binder IPC to pass buffers between processes. The producer interface, or
169what you pass to somebody who wants to generate graphic buffers, is
170IGraphicBufferProducer (part of <a
171href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>).
172BufferQueue is often used to render to a Surface and consume with a GL
173Consumer, among other tasks.
174
175BufferQueue can operate in three different modes:</p>
176
177<p><em>Synchronous-like mode</em> - BufferQueue by default operates in a
178synchronous-like mode, in which every buffer that comes in from the producer
179goes out at the consumer. No buffer is ever discarded in this mode. And if the
180producer is too fast and creates buffers faster than they are being drained, it
181will block and wait for free buffers.</p>
182
183<p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking
184mode where it generates an error rather than waiting for a buffer in those
185cases. No buffer is ever discarded in this mode either. This is useful for
186avoiding potential deadlocks in application software that may not understand
187the complex dependencies of the graphics framework.</p>
188
189<p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard
190old buffers rather than generate errors or wait. For instance, if conducting GL
191rendering to a texture view and drawing as quickly as possible, buffers must be
192dropped.</p>
193
194<p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES
195client. So when SurfaceFlinger is actively compositing one buffer or two into a
196third, for instance, it is using OpenGL ES.</p>
197
198<p>The Hardware Composer HAL conducts the other half of the work. This HAL acts
199as the central point for all Android graphics rendering.</p>
200
201<h3 id=synchronization_framework>Synchronization framework</h3>
202
203<p>Since Android graphics offer no explicit parallelism, vendors have long
204implemented their own implicit synchronization within their own drivers. This
205is no longer required with the Android graphics synchronization framework. See
206the <a href="#explicit_synchronization">Explicit synchronization</a> section
207for implementation instructions.</p>
208
209<p>The synchronization framework explicitly describes dependencies between
210different asynchronous operations in the system. The framework provides a
211simple API that lets components signal when buffers are released. It also
212allows synchronization primitives to be passed between drivers from the kernel
213to userspace and between userspace processes themselves.</p>
214
215<p>For example, an application may queue up work to be carried out in the GPU.
216The GPU then starts drawing that image. Although the image hasnt been drawn
217into memory yet, the buffer pointer can still be passed to the window
218compositor along with a fence that indicates when the GPU work will be
219finished. The window compositor may then start processing ahead of time and
220hand off the work to the display controller. In this manner, the CPU work can
221be done ahead of time. Once the GPU finishes, the display controller can
222immediately display the image.</p>
223
224<p>The synchronization framework also allows implementers to leverage
225synchronization resources in their own hardware components. Finally, the
226framework provides visibility into the graphics pipeline to aid in
227debugging.</p>