blob: 67e9d12995e49203e4a2f439a91fa7dbd892bae4 [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Graphics
2@jd:body
3
4<!--
Clay Murphye3ae3962014-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 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
Clay Murphye3ae3962014-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 Murphye3ae3962014-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 Murphye3ae3962014-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 Murphye3ae3962014-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 Murphye3ae3962014-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
Bert McMeen14031e92015-02-25 15:21:37 -080066<p>Android implementers can test OpenGL ES functionality using the <a href="DeqpUserGuide.pdf">drawElements Quality Program</a>, also known as deqp.</p>
67
Clay Murphye3ae3962014-09-02 17:30:57 -070068<h2 id=android_graphics_components>Android graphics components</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -080069
Clay Murphye3ae3962014-09-02 17:30:57 -070070<p>No matter what rendering API developers use, everything is rendered onto a
71"surface." The surface represents the producer side of a buffer queue that is
72often consumed by SurfaceFlinger. Every window that is created on the Android
73platform is backed by a surface. All of the visible surfaces rendered are
74composited onto the display by SurfaceFlinger.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080075
Clay Murphye3ae3962014-09-02 17:30:57 -070076<p>The following diagram shows how the key components work together:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080077
Clay Murphy0ddab122014-12-01 16:22:40 -080078<img src="images/graphics_surface.png" alt="image-rendering components">
Robert Ly35f2fda2013-01-29 16:27:05 -080079
Clay Murphye3ae3962014-09-02 17:30:57 -070080<p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080081
Clay Murphye3ae3962014-09-02 17:30:57 -070082<p>The main components are described below:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080083
Clay Murphye3ae3962014-09-02 17:30:57 -070084<h3 id=image_stream_producers>Image Stream Producers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080085
Clay Murphye3ae3962014-09-02 17:30:57 -070086<p>An image stream producer can be anything that produces graphic buffers for
87consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video
88decoders.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080089
Clay Murphye3ae3962014-09-02 17:30:57 -070090<h3 id=image_stream_consumers>Image Stream Consumers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080091
Clay Murphye3ae3962014-09-02 17:30:57 -070092<p>The most common consumer of image streams is SurfaceFlinger, the system
93service that consumes the currently visible surfaces and composites them onto
94the display using information provided by the Window Manager. SurfaceFlinger is
95the only service that can modify the content of the display. SurfaceFlinger
96uses OpenGL and the Hardware Composer to compose a group of surfaces.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080097
Clay Murphye3ae3962014-09-02 17:30:57 -070098<p>Other OpenGL ES apps can consume image streams as well, such as the camera
99app consuming a camera preview image stream. Non-GL applications can be
100consumers too, for example the ImageReader class.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800101
Clay Murphye3ae3962014-09-02 17:30:57 -0700102<h3 id=window_manager>Window Manager</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800103
Clay Murphye3ae3962014-09-02 17:30:57 -0700104<p>The Android system service that controls a window, which is a container for
105views. A window is always backed by a surface. This service oversees
106lifecycles, input and focus events, screen orientation, transitions,
107animations, position, transforms, z-order, and many other aspects of a window.
108The Window Manager sends all of the window metadata to SurfaceFlinger so
109SurfaceFlinger can use that data to composite surfaces on the display.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800110
Clay Murphye3ae3962014-09-02 17:30:57 -0700111<h3 id=hardware_composer>Hardware Composer</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800112
Clay Murphye3ae3962014-09-02 17:30:57 -0700113<p>The hardware abstraction for the display subsystem. SurfaceFlinger can
114delegate certain composition work to the Hardware Composer to offload work from
115OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So
116when SurfaceFlinger is actively compositing one buffer or two into a third, for
117instance, it is using OpenGL ES. This makes compositing lower power than having
118the GPU conduct all computation.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800119
Clay Murphye3ae3962014-09-02 17:30:57 -0700120<p>The Hardware Composer HAL conducts the other half of the work. This HAL is
121the central point for all Android graphics rendering. Hardware Composer must
122support events, one of which is VSYNC. Another is hotplug for plug-and-play
123HDMI support.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800124
Clay Murphye3ae3962014-09-02 17:30:57 -0700125<p>See the <a href="{@docRoot}devices/graphics.html#hardware_composer_hal">Hardware Composer
126HAL</a> section for more information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800127
Clay Murphye3ae3962014-09-02 17:30:57 -0700128<h3 id=gralloc>Gralloc</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800129
Clay Murphye3ae3962014-09-02 17:30:57 -0700130<p>The graphics memory allocator is needed to allocate memory that is requested
131by image producers. See the <a
132href="{@docRoot}devices/graphics.html#gralloc">Gralloc HAL</a> section for more
133information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800134
Clay Murphye3ae3962014-09-02 17:30:57 -0700135<h2 id=data_flow>Data flow</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -0800136
Clay Murphye3ae3962014-09-02 17:30:57 -0700137<p>See the following diagram for a depiction of the Android graphics
138pipeline:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800139
Clay Murphy0ddab122014-12-01 16:22:40 -0800140<img src="images/graphics_pipeline.png" alt="graphics data flow">
Robert Ly35f2fda2013-01-29 16:27:05 -0800141
Clay Murphye3ae3962014-09-02 17:30:57 -0700142<p class="img-caption"><strong>Figure 2.</strong> How graphic data flow through
143Android</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800144
Clay Murphye3ae3962014-09-02 17:30:57 -0700145<p>The objects on the left are renderers producing graphics buffers, such as
146the home screen, status bar, and system UI. SurfaceFlinger is the compositor
147and Hardware Composer is the composer.</p>
148
149<h3 id=bufferqueue>BufferQueue</h3>
150
151<p>BufferQueues provide the glue between the Android graphics components. These
152are a pair of queues that mediate the constant cycle of buffers from the
153producer to the consumer. Once the producers hand off their buffers,
154SurfaceFlinger is responsible for compositing everything onto the display.</p>
155
156<p>See the following diagram for the BufferQueue communication process.</p>
157
Clay Murphy0ddab122014-12-01 16:22:40 -0800158<img src="images/bufferqueue.png" alt="BufferQueue communication process">
Clay Murphye3ae3962014-09-02 17:30:57 -0700159
160<p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication
161process</p>
162
163<p>BufferQueue contains the logic that ties image stream producers and image
164stream consumers together. Some examples of image producers are the camera
165previews produced by the camera HAL or OpenGL ES games. Some examples of image
166consumers are SurfaceFlinger or another app that displays an OpenGL ES stream,
167such 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
170uses Binder IPC to pass buffers between processes. The producer interface, or
171what you pass to somebody who wants to generate graphic buffers, is
172IGraphicBufferProducer (part of <a
173href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>).
174BufferQueue is often used to render to a Surface and consume with a GL
175Consumer, among other tasks.
176
177BufferQueue can operate in three different modes:</p>
178
179<p><em>Synchronous-like mode</em> - BufferQueue by default operates in a
180synchronous-like mode, in which every buffer that comes in from the producer
181goes out at the consumer. No buffer is ever discarded in this mode. And if the
182producer is too fast and creates buffers faster than they are being drained, it
183will block and wait for free buffers.</p>
184
185<p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking
186mode where it generates an error rather than waiting for a buffer in those
187cases. No buffer is ever discarded in this mode either. This is useful for
188avoiding potential deadlocks in application software that may not understand
189the complex dependencies of the graphics framework.</p>
190
191<p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard
192old buffers rather than generate errors or wait. For instance, if conducting GL
193rendering to a texture view and drawing as quickly as possible, buffers must be
194dropped.</p>
195
196<p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES
197client. So when SurfaceFlinger is actively compositing one buffer or two into a
198third, 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
201as the central point for all Android graphics rendering.</p>
202
203<h3 id=synchronization_framework>Synchronization framework</h3>
204
205<p>Since Android graphics offer no explicit parallelism, vendors have long
206implemented their own implicit synchronization within their own drivers. This
207is no longer required with the Android graphics synchronization framework. See
208the <a href="#explicit_synchronization">Explicit synchronization</a> section
209for implementation instructions.</p>
210
211<p>The synchronization framework explicitly describes dependencies between
212different asynchronous operations in the system. The framework provides a
213simple API that lets components signal when buffers are released. It also
214allows synchronization primitives to be passed between drivers from the kernel
215to userspace and between userspace processes themselves.</p>
216
217<p>For example, an application may queue up work to be carried out in the GPU.
218The GPU then starts drawing that image. Although the image hasnt been drawn
219into memory yet, the buffer pointer can still be passed to the window
220compositor along with a fence that indicates when the GPU work will be
221finished. The window compositor may then start processing ahead of time and
222hand off the work to the display controller. In this manner, the CPU work can
223be done ahead of time. Once the GPU finishes, the display controller can
224immediately display the image.</p>
225
226<p>The synchronization framework also allows implementers to leverage
227synchronization resources in their own hardware components. Finally, the
228framework provides visibility into the graphics pipeline to aid in
229debugging.</p>