blob: 54b4620ba247784516a1bbbeb0af6c50f7fdc5cd [file] [log] [blame]
Clay Murphye3ae3962014-09-02 17:30:57 -07001page.title=Implementing graphics
2@jd:body
3
4<!--
5 Copyright 2014 The Android Open Source Project
6
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-->
19
20<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
28
Heidi von Markhamfd022c72016-06-30 10:15:28 -070029<p>To implement the Android graphics HAL, review the following requirements,
30implementation details, and testing advice.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070031
32<h2 id=requirements>Requirements</h2>
33
Heidi von Markhamfd022c72016-06-30 10:15:28 -070034<p>Android graphics support requires the following components:</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070035
Heidi von Markhamfd022c72016-06-30 10:15:28 -070036<ul>
37 <li>EGL driver</li>
38 <li>OpenGL ES 1.x driver</li>
39 <li>OpenGL ES 2.0 driver</li>
40 <li>OpenGL ES 3.x driver (optional)</li>
Heidi von Markham6bda3982016-06-30 17:04:44 -070041 <li>Vulkan (optional)</li>
Heidi von Markhamfd022c72016-06-30 10:15:28 -070042 <li>Gralloc HAL implementation</li>
43 <li>Hardware Composer HAL implementation</li>
44</ul>
Clay Murphye3ae3962014-09-02 17:30:57 -070045
46<h2 id=implementation>Implementation</h2>
47
48<h3 id=opengl_and_egl_drivers>OpenGL and EGL drivers</h3>
49
Heidi von Markhamfd022c72016-06-30 10:15:28 -070050<p>You must provide drivers for EGL, OpenGL ES 1.x, and OpenGL ES 2.0 (support
51for OpenGL 3.x is optional). Key considerations include:</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070052
Heidi von Markhamfd022c72016-06-30 10:15:28 -070053<ul>
54 <li>GL driver must be robust and conformant to OpenGL ES standards.</li>
55 <li>Do not limit the number of GL contexts. Because Android allows apps in
56 the background and tries to keep GL contexts alive, you should not limit the
57 number of contexts in your driver.</li>
58 <li> It is common to have 20-30 active GL contexts at once, so be
59 mindful of the amount of memory allocated for each context.</li>
60 <li>Support the YV12 image format and other YUV image formats that come from
61 other components in the system, such as media codecs or the camera.</li>
62 <li>Support the mandatory extensions: <code>GL_OES_texture_external</code>,
63 <code>EGL_ANDROID_image_native_buffer</code>, and
64 <code>EGL_ANDROID_recordable</code>. In addition, the
65 <code>EGL_ANDROID_framebuffer_target</code> extension is required for
66 Hardware Composer v1.1 and higher.</li>
67 </ul>
68<p>We highly recommend also supporting <code>EGL_ANDROID_blob_cache</code>,
69<code>EGL_KHR_fence_sync</code>, <code>EGL_KHR_wait_sync</code>, and <code>EGL_ANDROID_native_fence_sync</code>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070070
Heidi von Markhamfd022c72016-06-30 10:15:28 -070071<p class="note"><strong>Note</strong>: The OpenGL API exposed to app developers
72differs from the OpenGL implemented on the device. Apps cannot directly access
73the GL driver layer and must go through the interface provided by the APIs.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070074
75<h3 id=pre-rotation>Pre-rotation</h3>
76
Heidi von Markhamfd022c72016-06-30 10:15:28 -070077<p>Many hardware overlays do not support rotation (and even if they do it costs
78processing power); the solution is to pre-transform the buffer before it reaches
79SurfaceFlinger. Android supports a query hint
80(<code>NATIVE_WINDOW_TRANSFORM_HINT</code>) in <code>ANativeWindow</code> to
81represent the most likely transform to be applied to the buffer by
82SurfaceFlinger. GL drivers can use this hint to pre-transform the buffer
83before it reaches SurfaceFlinger so when the buffer arrives, it is correctly
84transformed.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070085
Heidi von Markhamfd022c72016-06-30 10:15:28 -070086<p>For example, when receiving a hint to rotate 90 degrees, generate and apply a
87matrix to the buffer to prevent it from running off the end of the page. To save
88power, do this pre-rotation. For details, see the <code>ANativeWindow</code>
89interface defined in <code>system/core/include/system/window.h</code>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070090
91<h3 id=gralloc_hal>Gralloc HAL</h3>
92
Heidi von Markhamfd022c72016-06-30 10:15:28 -070093<p>The graphics memory allocator allocates memory requested by image producers.
94You can find the interface definition of the HAL at
95<code>hardware/libhardware/modules/gralloc.h</code>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070096
97<h3 id=protected_buffers>Protected buffers</h3>
98
99<p>The gralloc usage flag <code>GRALLOC_USAGE_PROTECTED</code> allows the
100graphics buffer to be displayed only through a hardware-protected path. These
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700101overlay planes are the only way to display DRM content (DRM-protected buffers
102cannot be accessed by SurfaceFlinger or the OpenGL ES driver).</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700103
104<p>DRM-protected video can be presented only on an overlay plane. Video players
105that support protected content must be implemented with SurfaceView. Software
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700106running on unprotected hardware cannot read or write the buffer;
107hardware-protected paths must appear on the Hardware Composer overlay (i.e.,
108protected videos will disappear from the display if Hardware Composer switches
109to OpenGL ES composition).</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700110
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700111<p>For details on protected content, see
112<a href="{@docRoot}devices/drm.html">DRM</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700113
114<h3 id=hardware_composer_hal>Hardware Composer HAL</h3>
115
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700116<p>The Hardware Composer HAL (HWC) is used by SurfaceFlinger to composite
117surfaces to the screen. It abstracts objects such as overlays and 2D blitters
118and helps offload some work that would normally be done with OpenGL. For details
119on the HWC, see <a href="{@docRoot}devices/graphics/implement-hwc.html">Hardware
120Composer HAL</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700121
122<h3 id=vsync>VSYNC</h3>
123
124<p>VSYNC synchronizes certain events to the refresh cycle of the display.
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700125Applications always start drawing on a VSYNC boundary, and SurfaceFlinger always
126composites on a VSYNC boundary. This eliminates stutters and improves visual
127performance of graphics. For details on VSYNC, see
128<a href="{@docRoot}devices/graphics/implement-vsync.html">Implementing
129VSYNC</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700130
Heidi von Markham6bda3982016-06-30 17:04:44 -0700131<h3 id=vulkan>Vulkan</h3>
132
133<p>Vulkan is a low-overhead, cross-platform API for high-performance 3D graphics.
134Like OpenGL ES, Vulkan provides tools for creating high-quality, real-time
135graphics in applications. Vulkan advantages include reductions in CPU overhead
136and support for the <a href="https://www.khronos.org/spir">SPIR-V Binary
137Intermediate</a> language. For details on Vulkan, see
138<a href="{@docRoot}devices/graphics/implement-vulkan.html">Implementing
139Vulkan</a>.</p>
140
Clay Murphye3ae3962014-09-02 17:30:57 -0700141<h3 id=virtual_displays>Virtual displays</h3>
142
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700143<p>Android added platform support for virtual displays in Hardware Composer v1.3.
144The virtual display composition is similar to the physical display: Input
Clay Murphye3ae3962014-09-02 17:30:57 -0700145layers are described in prepare(), SurfaceFlinger conducts GPU composition, and
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700146layers and GPU framebuffer are provided to Hardware Composer in set(). For
147details on virtual displays, see
148<a href="{@docRoot}devices/graphics/implement-vdisplays.html">Implementing
149Virtual Displays</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700150
151<h2 id=testing>Testing</h2>
152
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700153<p>For benchmarking, use the following flow by phase:</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700154
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700155<ul>
156 <li><em>Specification</em>. When initially specifying the device (such as when
157 using immature drivers), use predefined (fixed) clocks and workloads to
158 measure frames per second (fps) rendered. This gives a clear view of hardware
159 capabilities.</li>
160 <li><em>Development</em>. As drivers mature, use a fixed set of user actions
161 to measure the number of visible stutters (janks) in animations.</li>
162 <li><em>Production</em>. When a device is ready for comparison against
163 competitors, increase the workload until stutters increase. Determine if the
164 current clock settings can keep up with the load. This can help you identify
165 where to slow the clocks and reduce power use.</li>
166</ul>
Clay Murphye3ae3962014-09-02 17:30:57 -0700167
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700168<p>For help deriving device capabilities during the specification phase, use the
169Flatland tool at <code>platform/frameworks/native/cmds/flatland/</code>.
170Flatland relies upon fixed clocks and shows the throughput achievable with
171composition-based workloads. It uses gralloc buffers to simulate multiple window
172scenarios, filling in the window with GL then measuring the compositing.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700173
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700174<p class="note"><strong>Note:</strong> Flatland uses the synchronization
175framework to measure time, so your implementation must support the
176synchronization framework.</p>