1page.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 29<p>To implement the Android graphics HAL, review the following requirements, 30implementation details, and testing advice.</p> 31 32<h2 id=requirements>Requirements</h2> 33 34<p>Android graphics support requires the following components:</p> 35 36<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>Vulkan (optional)</li> 42 <li>Gralloc HAL implementation</li> 43 <li>Hardware Composer HAL implementation</li> 44</ul> 45 46<h2 id=implementation>Implementation</h2> 47 48<h3 id=opengl_and_egl_drivers>OpenGL and EGL drivers</h3> 49 50<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> 52 53<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> 70 71<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> 74 75<h3 id=pre-rotation>Pre-rotation</h3> 76 77<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> 85 86<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> 90 91<h3 id=gralloc_hal>Gralloc HAL</h3> 92 93<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/include/hardware/gralloc.h</code>.</p> 96 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 101overlay planes are the only way to display DRM content (DRM-protected buffers 102cannot be accessed by SurfaceFlinger or the OpenGL ES driver).</p> 103 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 106running 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> 110 111<p>For details on protected content, see 112<a href="{@docRoot}devices/drm.html">DRM</a>.</p> 113 114<h3 id=hardware_composer_hal>Hardware Composer HAL</h3> 115 116<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> 121 122<h3 id=vsync>VSYNC</h3> 123 124<p>VSYNC synchronizes certain events to the refresh cycle of the display. 125Applications 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> 130 131<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 141<h3 id=virtual_displays>Virtual displays</h3> 142 143<p>Android added platform support for virtual displays in Hardware Composer v1.3. 144The virtual display composition is similar to the physical display: Input 145layers are described in prepare(), SurfaceFlinger conducts GPU composition, and 146layers 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> 150 151<h2 id=testing>Testing</h2> 152 153<p>For benchmarking, use the following flow by phase:</p> 154 155<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> 167 168<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> 173 174<p class="note"><strong>Note:</strong> Flatland uses the synchronization 175framework to measure time, so your implementation must support the 176synchronization framework.</p> 177