blob: be13572906c447846b402d78b75b1d6dcb21430c [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
Heidi von Markham40a90832015-03-17 13:27:16 -070069<p>Android implementers can test OpenGL ES functionality using the
70<a href="DeqpUserGuide.pdf">drawElements Quality Program</a>, also known as
71deqp.</p>
Bert McMeen14031e92015-02-25 15:21:37 -080072
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070073<h2 id="android_graphics_components">Android graphics components</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -080074
Clay Murphye3ae3962014-09-02 17:30:57 -070075<p>No matter what rendering API developers use, everything is rendered onto a
76"surface." The surface represents the producer side of a buffer queue that is
77often consumed by SurfaceFlinger. Every window that is created on the Android
78platform is backed by a surface. All of the visible surfaces rendered are
79composited onto the display by SurfaceFlinger.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080080
Clay Murphye3ae3962014-09-02 17:30:57 -070081<p>The following diagram shows how the key components work together:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080082
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070083<img src="images/ape_fwk_graphics.png" alt="image-rendering components">
Robert Ly35f2fda2013-01-29 16:27:05 -080084
Clay Murphye3ae3962014-09-02 17:30:57 -070085<p class="img-caption"><strong>Figure 1.</strong> How surfaces are rendered</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080086
Clay Murphye3ae3962014-09-02 17:30:57 -070087<p>The main components are described below:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080088
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070089<h3 id="image_stream_producers">Image Stream Producers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080090
Clay Murphye3ae3962014-09-02 17:30:57 -070091<p>An image stream producer can be anything that produces graphic buffers for
92consumption. Examples include OpenGL ES, Canvas 2D, and mediaserver video
93decoders.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -080094
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070095<h3 id="image_stream_consumers">Image Stream Consumers</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -080096
Clay Murphye3ae3962014-09-02 17:30:57 -070097<p>The most common consumer of image streams is SurfaceFlinger, the system
98service that consumes the currently visible surfaces and composites them onto
99the display using information provided by the Window Manager. SurfaceFlinger is
100the only service that can modify the content of the display. SurfaceFlinger
101uses OpenGL and the Hardware Composer to compose a group of surfaces.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800102
Clay Murphye3ae3962014-09-02 17:30:57 -0700103<p>Other OpenGL ES apps can consume image streams as well, such as the camera
104app consuming a camera preview image stream. Non-GL applications can be
105consumers too, for example the ImageReader class.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800106
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700107<h3 id="window_manager">Window Manager</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800108
Clay Murphye3ae3962014-09-02 17:30:57 -0700109<p>The Android system service that controls a window, which is a container for
110views. A window is always backed by a surface. This service oversees
111lifecycles, input and focus events, screen orientation, transitions,
112animations, position, transforms, z-order, and many other aspects of a window.
113The Window Manager sends all of the window metadata to SurfaceFlinger so
114SurfaceFlinger can use that data to composite surfaces on the display.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800115
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700116<h3 id="hardware_composer">Hardware Composer</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800117
Clay Murphye3ae3962014-09-02 17:30:57 -0700118<p>The hardware abstraction for the display subsystem. SurfaceFlinger can
119delegate certain composition work to the Hardware Composer to offload work from
120OpenGL and the GPU. SurfaceFlinger acts as just another OpenGL ES client. So
121when SurfaceFlinger is actively compositing one buffer or two into a third, for
122instance, it is using OpenGL ES. This makes compositing lower power than having
123the GPU conduct all computation.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800124
Clay Murphye3ae3962014-09-02 17:30:57 -0700125<p>The Hardware Composer HAL conducts the other half of the work. This HAL is
126the central point for all Android graphics rendering. Hardware Composer must
127support events, one of which is VSYNC. Another is hotplug for plug-and-play
128HDMI support.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800129
Heidi von Markham40a90832015-03-17 13:27:16 -0700130<p>See the
131<a href="{@docRoot}devices/graphics.html#hardware_composer_hal">Hardware
132Composer HAL</a> section for more information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800133
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700134<h3 id="gralloc">Gralloc</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800135
Clay Murphye3ae3962014-09-02 17:30:57 -0700136<p>The graphics memory allocator is needed to allocate memory that is requested
137by image producers. See the <a
138href="{@docRoot}devices/graphics.html#gralloc">Gralloc HAL</a> section for more
139information.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800140
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700141<h2 id="data_flow">Data flow</h2>
Robert Ly35f2fda2013-01-29 16:27:05 -0800142
Clay Murphye3ae3962014-09-02 17:30:57 -0700143<p>See the following diagram for a depiction of the Android graphics
144pipeline:</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800145
Clay Murphy0ddab122014-12-01 16:22:40 -0800146<img src="images/graphics_pipeline.png" alt="graphics data flow">
Robert Ly35f2fda2013-01-29 16:27:05 -0800147
Heidi von Markham40a90832015-03-17 13:27:16 -0700148<p class="img-caption"><strong>Figure 2.</strong> Graphic data flow through
Clay Murphye3ae3962014-09-02 17:30:57 -0700149Android</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800150
Clay Murphye3ae3962014-09-02 17:30:57 -0700151<p>The objects on the left are renderers producing graphics buffers, such as
152the home screen, status bar, and system UI. SurfaceFlinger is the compositor
153and Hardware Composer is the composer.</p>
154
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700155<h3 id="bufferqueue">BufferQueue</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700156
157<p>BufferQueues provide the glue between the Android graphics components. These
158are a pair of queues that mediate the constant cycle of buffers from the
159producer to the consumer. Once the producers hand off their buffers,
160SurfaceFlinger is responsible for compositing everything onto the display.</p>
161
162<p>See the following diagram for the BufferQueue communication process.</p>
163
Heidi von Markham40a90832015-03-17 13:27:16 -0700164<img src="images/graphics_bufferqueue.png"
165alt="BufferQueue communication process">
Clay Murphye3ae3962014-09-02 17:30:57 -0700166
167<p class="img-caption"><strong>Figure 3.</strong> BufferQueue communication
168process</p>
169
170<p>BufferQueue contains the logic that ties image stream producers and image
171stream consumers together. Some examples of image producers are the camera
172previews produced by the camera HAL or OpenGL ES games. Some examples of image
173consumers are SurfaceFlinger or another app that displays an OpenGL ES stream,
174such as the camera app displaying the camera viewfinder.</p>
175
176<p>BufferQueue is a data structure that combines a buffer pool with a queue and
177uses Binder IPC to pass buffers between processes. The producer interface, or
178what you pass to somebody who wants to generate graphic buffers, is
179IGraphicBufferProducer (part of <a
180href="http://developer.android.com/reference/android/graphics/SurfaceTexture.html">SurfaceTexture</a>).
181BufferQueue is often used to render to a Surface and consume with a GL
182Consumer, among other tasks.
183
184BufferQueue can operate in three different modes:</p>
185
186<p><em>Synchronous-like mode</em> - BufferQueue by default operates in a
187synchronous-like mode, in which every buffer that comes in from the producer
188goes out at the consumer. No buffer is ever discarded in this mode. And if the
189producer is too fast and creates buffers faster than they are being drained, it
190will block and wait for free buffers.</p>
191
192<p><em>Non-blocking mode</em> - BufferQueue can also operate in a non-blocking
193mode where it generates an error rather than waiting for a buffer in those
194cases. No buffer is ever discarded in this mode either. This is useful for
195avoiding potential deadlocks in application software that may not understand
196the complex dependencies of the graphics framework.</p>
197
198<p><em>Discard mode</em> - Finally, BufferQueue may be configured to discard
199old buffers rather than generate errors or wait. For instance, if conducting GL
200rendering to a texture view and drawing as quickly as possible, buffers must be
201dropped.</p>
202
203<p>To conduct most of this work, SurfaceFlinger acts as just another OpenGL ES
204client. So when SurfaceFlinger is actively compositing one buffer or two into a
205third, for instance, it is using OpenGL ES.</p>
206
207<p>The Hardware Composer HAL conducts the other half of the work. This HAL acts
208as the central point for all Android graphics rendering.</p>
209
Heidi von Markham1e7b8b72015-03-09 10:13:48 -0700210<h3 id="synchronization_framework">Synchronization framework</h3>
Clay Murphye3ae3962014-09-02 17:30:57 -0700211
212<p>Since Android graphics offer no explicit parallelism, vendors have long
213implemented their own implicit synchronization within their own drivers. This
214is no longer required with the Android graphics synchronization framework. See
215the <a href="#explicit_synchronization">Explicit synchronization</a> section
216for implementation instructions.</p>
217
218<p>The synchronization framework explicitly describes dependencies between
219different asynchronous operations in the system. The framework provides a
220simple API that lets components signal when buffers are released. It also
221allows synchronization primitives to be passed between drivers from the kernel
222to userspace and between userspace processes themselves.</p>
223
224<p>For example, an application may queue up work to be carried out in the GPU.
225The GPU then starts drawing that image. Although the image hasnt been drawn
226into memory yet, the buffer pointer can still be passed to the window
227compositor along with a fence that indicates when the GPU work will be
228finished. The window compositor may then start processing ahead of time and
229hand off the work to the display controller. In this manner, the CPU work can
230be done ahead of time. Once the GPU finishes, the display controller can
231immediately display the image.</p>
232
233<p>The synchronization framework also allows implementers to leverage
234synchronization resources in their own hardware components. Finally, the
235framework provides visibility into the graphics pipeline to aid in
Heidi von Markham40a90832015-03-17 13:27:16 -0700236debugging.</p>