blob: 178f4b876be2d79ea1e62f9dcc49faf69a596ded [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>
41 <li>Gralloc HAL implementation</li>
42 <li>Hardware Composer HAL implementation</li>
43</ul>
Clay Murphye3ae3962014-09-02 17:30:57 -070044
45<h2 id=implementation>Implementation</h2>
46
47<h3 id=opengl_and_egl_drivers>OpenGL and EGL drivers</h3>
48
Heidi von Markhamfd022c72016-06-30 10:15:28 -070049<p>You must provide drivers for EGL, OpenGL ES 1.x, and OpenGL ES 2.0 (support
50for OpenGL 3.x is optional). Key considerations include:</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070051
Heidi von Markhamfd022c72016-06-30 10:15:28 -070052<ul>
53 <li>GL driver must be robust and conformant to OpenGL ES standards.</li>
54 <li>Do not limit the number of GL contexts. Because Android allows apps in
55 the background and tries to keep GL contexts alive, you should not limit the
56 number of contexts in your driver.</li>
57 <li> It is common to have 20-30 active GL contexts at once, so be
58 mindful of the amount of memory allocated for each context.</li>
59 <li>Support the YV12 image format and other YUV image formats that come from
60 other components in the system, such as media codecs or the camera.</li>
61 <li>Support the mandatory extensions: <code>GL_OES_texture_external</code>,
62 <code>EGL_ANDROID_image_native_buffer</code>, and
63 <code>EGL_ANDROID_recordable</code>. In addition, the
64 <code>EGL_ANDROID_framebuffer_target</code> extension is required for
65 Hardware Composer v1.1 and higher.</li>
66 </ul>
67<p>We highly recommend also supporting <code>EGL_ANDROID_blob_cache</code>,
68<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 -070069
Heidi von Markhamfd022c72016-06-30 10:15:28 -070070<p class="note"><strong>Note</strong>: The OpenGL API exposed to app developers
71differs from the OpenGL implemented on the device. Apps cannot directly access
72the GL driver layer and must go through the interface provided by the APIs.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070073
74<h3 id=pre-rotation>Pre-rotation</h3>
75
Heidi von Markhamfd022c72016-06-30 10:15:28 -070076<p>Many hardware overlays do not support rotation (and even if they do it costs
77processing power); the solution is to pre-transform the buffer before it reaches
78SurfaceFlinger. Android supports a query hint
79(<code>NATIVE_WINDOW_TRANSFORM_HINT</code>) in <code>ANativeWindow</code> to
80represent the most likely transform to be applied to the buffer by
81SurfaceFlinger. GL drivers can use this hint to pre-transform the buffer
82before it reaches SurfaceFlinger so when the buffer arrives, it is correctly
83transformed.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070084
Heidi von Markhamfd022c72016-06-30 10:15:28 -070085<p>For example, when receiving a hint to rotate 90 degrees, generate and apply a
86matrix to the buffer to prevent it from running off the end of the page. To save
87power, do this pre-rotation. For details, see the <code>ANativeWindow</code>
88interface defined in <code>system/core/include/system/window.h</code>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070089
90<h3 id=gralloc_hal>Gralloc HAL</h3>
91
Heidi von Markhamfd022c72016-06-30 10:15:28 -070092<p>The graphics memory allocator allocates memory requested by image producers.
93You can find the interface definition of the HAL at
94<code>hardware/libhardware/modules/gralloc.h</code>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -070095
96<h3 id=protected_buffers>Protected buffers</h3>
97
98<p>The gralloc usage flag <code>GRALLOC_USAGE_PROTECTED</code> allows the
99graphics buffer to be displayed only through a hardware-protected path. These
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700100overlay planes are the only way to display DRM content (DRM-protected buffers
101cannot be accessed by SurfaceFlinger or the OpenGL ES driver).</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700102
103<p>DRM-protected video can be presented only on an overlay plane. Video players
104that support protected content must be implemented with SurfaceView. Software
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700105running on unprotected hardware cannot read or write the buffer;
106hardware-protected paths must appear on the Hardware Composer overlay (i.e.,
107protected videos will disappear from the display if Hardware Composer switches
108to OpenGL ES composition).</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700109
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700110<p>For details on protected content, see
111<a href="{@docRoot}devices/drm.html">DRM</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700112
113<h3 id=hardware_composer_hal>Hardware Composer HAL</h3>
114
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700115<p>The Hardware Composer HAL (HWC) is used by SurfaceFlinger to composite
116surfaces to the screen. It abstracts objects such as overlays and 2D blitters
117and helps offload some work that would normally be done with OpenGL. For details
118on the HWC, see <a href="{@docRoot}devices/graphics/implement-hwc.html">Hardware
119Composer HAL</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700120
121<h3 id=vsync>VSYNC</h3>
122
123<p>VSYNC synchronizes certain events to the refresh cycle of the display.
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700124Applications always start drawing on a VSYNC boundary, and SurfaceFlinger always
125composites on a VSYNC boundary. This eliminates stutters and improves visual
126performance of graphics. For details on VSYNC, see
127<a href="{@docRoot}devices/graphics/implement-vsync.html">Implementing
128VSYNC</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700129
130<h3 id=virtual_displays>Virtual displays</h3>
131
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700132<p>Android added platform support for virtual displays in Hardware Composer v1.3.
133The virtual display composition is similar to the physical display: Input
Clay Murphye3ae3962014-09-02 17:30:57 -0700134layers are described in prepare(), SurfaceFlinger conducts GPU composition, and
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700135layers and GPU framebuffer are provided to Hardware Composer in set(). For
136details on virtual displays, see
137<a href="{@docRoot}devices/graphics/implement-vdisplays.html">Implementing
138Virtual Displays</a>.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700139
140<h2 id=testing>Testing</h2>
141
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700142<p>For benchmarking, use the following flow by phase:</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700143
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700144<ul>
145 <li><em>Specification</em>. When initially specifying the device (such as when
146 using immature drivers), use predefined (fixed) clocks and workloads to
147 measure frames per second (fps) rendered. This gives a clear view of hardware
148 capabilities.</li>
149 <li><em>Development</em>. As drivers mature, use a fixed set of user actions
150 to measure the number of visible stutters (janks) in animations.</li>
151 <li><em>Production</em>. When a device is ready for comparison against
152 competitors, increase the workload until stutters increase. Determine if the
153 current clock settings can keep up with the load. This can help you identify
154 where to slow the clocks and reduce power use.</li>
155</ul>
Clay Murphye3ae3962014-09-02 17:30:57 -0700156
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700157<p>For help deriving device capabilities during the specification phase, use the
158Flatland tool at <code>platform/frameworks/native/cmds/flatland/</code>.
159Flatland relies upon fixed clocks and shows the throughput achievable with
160composition-based workloads. It uses gralloc buffers to simulate multiple window
161scenarios, filling in the window with GL then measuring the compositing.</p>
Clay Murphye3ae3962014-09-02 17:30:57 -0700162
Heidi von Markhamfd022c72016-06-30 10:15:28 -0700163<p class="note"><strong>Note:</strong> Flatland uses the synchronization
164framework to measure time, so your implementation must support the
165synchronization framework.</p>