blob: 9a1fe018c3177062b7a1448883db52f123763449 [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 Markham40a90832015-03-17 13:27:16 -070028<img style="float: right; margin: 0px 15px 15px 0px;"
29src="images/ape_fwk_hal_graphics.png" alt="Android Graphics HAL icon"
30width="175" />
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070031
Clay Murphye3ae3962014-09-02 17:30:57 -070032<p>The Android framework offers a variety of graphics rendering APIs for 2D and
333D that interact with manufacturer implementations of graphics drivers, so it
34is important to have a good understanding of how those APIs work at a higher
35level. This page introduces the graphics hardware abstraction layer (HAL) upon
36which those drivers are built.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080037
Clay Murphye3ae3962014-09-02 17:30:57 -070038<p>Application developers draw images to the screen in two ways: with Canvas or
39OpenGL. See <a
40href="{@docRoot}devices/graphics/architecture.html">System-level graphics
41architecture</a> for a detailed description of Android graphics
42components.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080043
Clay Murphye3ae3962014-09-02 17:30:57 -070044<p><a
45href="http://developer.android.com/reference/android/graphics/Canvas.html">android.graphics.Canvas</a>
46is a 2D graphics API and is the most popular graphics API among developers.
47Canvas operations draw all the stock and custom <a
48href="http://developer.android.com/reference/android/view/View.html">android.view.View</a>s
49in Android. In Android, hardware acceleration for Canvas APIs is accomplished
50with a drawing library called OpenGLRenderer that translates Canvas operations
51to OpenGL operations so they can execute on the GPU.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080052
Clay Murphye3ae3962014-09-02 17:30:57 -070053<p>Beginning in Android 4.0, hardware-accelerated Canvas is enabled by default.
54Consequently, a hardware GPU that supports OpenGL ES 2.0 is mandatory for
Heidi von Markham40a90832015-03-17 13:27:16 -070055Android 4.0 and later devices. See the
56<a href="https://developer.android.com/guide/topics/graphics/hardware-accel.html">Hardware Acceleration guide</a> for an explanation of how the
57hardware-accelerated drawing path works and the differences in its behavior
58from that of the software drawing path.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080059
Clay Murphye3ae3962014-09-02 17:30:57 -070060<p>In addition to Canvas, the other main way that developers render graphics is
61by using OpenGL ES to directly render to a surface. Android provides OpenGL ES
Heidi von Markham40a90832015-03-17 13:27:16 -070062interfaces in the
63<a href="http://developer.android.com/reference/android/opengl/package-summary.html">android.opengl</a>
Clay Murphye3ae3962014-09-02 17:30:57 -070064package that developers can use to call into their GL implementations with the
65SDK or with native APIs provided in the <a
66href="https://developer.android.com/tools/sdk/ndk/index.html">Android
67NDK</a>.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080068
Bert McMeenf902a282015-03-06 14:44:22 -080069<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 -080070
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070071<h2 id="android_graphics_components">Android graphics components</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -080072
Clay Murphye3ae3962014-09-02 17:30:57 -070073<p>No matter what rendering API developers use, everything is rendered onto a
74"surface." The surface represents the producer side of a buffer queue that is
75often consumed by SurfaceFlinger. Every window that is created on the Android
76platform is backed by a surface. All of the visible surfaces rendered are
77composited onto the display by SurfaceFlinger.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080078
Clay Murphye3ae3962014-09-02 17:30:57 -070079<p>The following diagram shows how the key components work together:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080080
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070081<img src="images/ape_fwk_graphics.png" alt="image-rendering components">
Robert Ly35f2fda2013-01-29 16:27:05 -080082
Clay Murphye3ae3962014-09-02 17:30:57 -070083<p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080084
Clay Murphye3ae3962014-09-02 17:30:57 -070085<p>The main components are described below:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080086
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070087<h3 id="image_stream_producers">Image Stream Producers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080088
Clay Murphye3ae3962014-09-02 17:30:57 -070089<p>An image stream producer can be anything that produces graphic buffers for
90consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video
91decoders.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080092
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070093<h3 id="image_stream_consumers">Image Stream Consumers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080094
Clay Murphye3ae3962014-09-02 17:30:57 -070095<p>The most common consumer of image streams is SurfaceFlinger, the system
96service that consumes the currently visible surfaces and composites them onto
97the display using information provided by the Window Manager. SurfaceFlinger is
98the only service that can modify the content of the display. SurfaceFlinger
99uses OpenGL and the Hardware Composer to compose a group of surfaces.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800100
Clay Murphye3ae3962014-09-02 17:30:57 -0700101<p>Other OpenGL ES apps can consume image streams as well, such as the camera
102app consuming a camera preview image stream. Non-GL applications can be
103consumers too, for example the ImageReader class.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800104
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700105<h3 id="window_manager">Window Manager</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800106
Clay Murphye3ae3962014-09-02 17:30:57 -0700107<p>The Android system service that controls a window, which is a container for
108views. A window is always backed by a surface. This service oversees
109lifecycles, input and focus events, screen orientation, transitions,
110animations, position, transforms, z-order, and many other aspects of a window.
111The Window Manager sends all of the window metadata to SurfaceFlinger so
112SurfaceFlinger can use that data to composite surfaces on the display.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800113
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700114<h3 id="hardware_composer">Hardware Composer</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800115
Clay Murphye3ae3962014-09-02 17:30:57 -0700116<p>The hardware abstraction for the display subsystem. SurfaceFlinger can
117delegate certain composition work to the Hardware Composer to offload work from
118OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So
119when SurfaceFlinger is actively compositing one buffer or two into a third, for
120instance, it is using OpenGL ES. This makes compositing lower power than having
121the GPU conduct all computation.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800122
Clay Murphye3ae3962014-09-02 17:30:57 -0700123<p>The Hardware Composer HAL conducts the other half of the work. This HAL is
124the central point for all Android graphics rendering. Hardware Composer must
125support events, one of which is VSYNC. Another is hotplug for plug-and-play
126HDMI support.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800127
Heidi von Markham40a90832015-03-17 13:27:16 -0700128<p>See the
129<a href="{@docRoot}devices/graphics.html#hardware_composer_hal">Hardware
130Composer HAL</a> section for more information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800131
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700132<h3 id="gralloc">Gralloc</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800133
Clay Murphye3ae3962014-09-02 17:30:57 -0700134<p>The graphics memory allocator is needed to allocate memory that is requested
135by image producers. See the <a
136href="{@docRoot}devices/graphics.html#gralloc">Gralloc HAL</a> section for more
137information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800138
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700139<h2 id="data_flow">Data flow</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -0800140
Clay Murphye3ae3962014-09-02 17:30:57 -0700141<p>See the following diagram for a depiction of the Android graphics
142pipeline:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800143
Clay Murphy0ddab122014-12-01 16:22:40 -0800144<img src="images/graphics_pipeline.png" alt="graphics data flow">
Robert Ly35f2fda2013-01-29 16:27:05 -0800145
Heidi von Markham40a90832015-03-17 13:27:16 -0700146<p class="img-caption"><strong>Figure 2.</strong> Graphic data flow through
Clay Murphye3ae3962014-09-02 17:30:57 -0700147Android</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800148
Clay Murphye3ae3962014-09-02 17:30:57 -0700149<p>The objects on the left are renderers producing graphics buffers, such as
150the home screen, status bar, and system UI. SurfaceFlinger is the compositor
151and Hardware Composer is the composer.</p>
152
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700153<h3 id="bufferqueue">BufferQueue</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700154
155<p>BufferQueues provide the glue between the Android graphics components. These
156are a pair of queues that mediate the constant cycle of buffers from the
157producer to the consumer. Once the producers hand off their buffers,
158SurfaceFlinger is responsible for compositing everything onto the display.</p>
159
160<p>See the following diagram for the BufferQueue communication process.</p>
161
Heidi von Markham40a90832015-03-17 13:27:16 -0700162<img src="images/graphics_bufferqueue.png"
163alt="BufferQueue communication process">
Clay Murphye3ae3962014-09-02 17:30:57 -0700164
165<p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication
166process</p>
167
168<p>BufferQueue contains the logic that ties image stream producers and image
169stream consumers together. Some examples of image producers are the camera
170previews produced by the camera HAL or OpenGL ES games. Some examples of image
171consumers are SurfaceFlinger or another app that displays an OpenGL ES stream,
172such as the camera app displaying the camera viewfinder.</p>
173
174<p>BufferQueue is a data structure that combines a buffer pool with a queue and
175uses Binder IPC to pass buffers between processes. The producer interface, or
176what you pass to somebody who wants to generate graphic buffers, is
177IGraphicBufferProducer (part of <a
178href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>).
179BufferQueue is often used to render to a Surface and consume with a GL
180Consumer, among other tasks.
181
182BufferQueue can operate in three different modes:</p>
183
184<p><em>Synchronous-like mode</em> - BufferQueue by default operates in a
185synchronous-like mode, in which every buffer that comes in from the producer
186goes out at the consumer. No buffer is ever discarded in this mode. And if the
187producer is too fast and creates buffers faster than they are being drained, it
188will block and wait for free buffers.</p>
189
190<p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking
191mode where it generates an error rather than waiting for a buffer in those
192cases. No buffer is ever discarded in this mode either. This is useful for
193avoiding potential deadlocks in application software that may not understand
194the complex dependencies of the graphics framework.</p>
195
196<p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard
197old buffers rather than generate errors or wait. For instance, if conducting GL
198rendering to a texture view and drawing as quickly as possible, buffers must be
199dropped.</p>
200
201<p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES
202client. So when SurfaceFlinger is actively compositing one buffer or two into a
203third, for instance, it is using OpenGL ES.</p>
204
205<p>The Hardware Composer HAL conducts the other half of the work. This HAL acts
206as the central point for all Android graphics rendering.</p>
207
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700208<h3 id="synchronization_framework">Synchronization framework</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700209
210<p>Since Android graphics offer no explicit parallelism, vendors have long
211implemented their own implicit synchronization within their own drivers. This
212is no longer required with the Android graphics synchronization framework. See
213the <a href="#explicit_synchronization">Explicit synchronization</a> section
214for implementation instructions.</p>
215
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
Heidi von Markham40a90832015-03-17 13:27:16 -0700234debugging.</p>