Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 1 | page.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 Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 29 | <p>To implement the Android graphics HAL, review the following requirements, |
| 30 | implementation details, and testing advice.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 31 | |
| 32 | <h2 id=requirements>Requirements</h2> |
| 33 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 34 | <p>Android graphics support requires the following components:</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 35 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 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>Gralloc HAL implementation</li> |
| 42 | <li>Hardware Composer HAL implementation</li> |
| 43 | </ul> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 44 | |
| 45 | <h2 id=implementation>Implementation</h2> |
| 46 | |
| 47 | <h3 id=opengl_and_egl_drivers>OpenGL and EGL drivers</h3> |
| 48 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 49 | <p>You must provide drivers for EGL, OpenGL ES 1.x, and OpenGL ES 2.0 (support |
| 50 | for OpenGL 3.x is optional). Key considerations include:</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 51 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 52 | <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 Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 69 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 70 | <p class="note"><strong>Note</strong>: The OpenGL API exposed to app developers |
| 71 | differs from the OpenGL implemented on the device. Apps cannot directly access |
| 72 | the GL driver layer and must go through the interface provided by the APIs.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 73 | |
| 74 | <h3 id=pre-rotation>Pre-rotation</h3> |
| 75 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 76 | <p>Many hardware overlays do not support rotation (and even if they do it costs |
| 77 | processing power); the solution is to pre-transform the buffer before it reaches |
| 78 | SurfaceFlinger. Android supports a query hint |
| 79 | (<code>NATIVE_WINDOW_TRANSFORM_HINT</code>) in <code>ANativeWindow</code> to |
| 80 | represent the most likely transform to be applied to the buffer by |
| 81 | SurfaceFlinger. GL drivers can use this hint to pre-transform the buffer |
| 82 | before it reaches SurfaceFlinger so when the buffer arrives, it is correctly |
| 83 | transformed.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 84 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 85 | <p>For example, when receiving a hint to rotate 90 degrees, generate and apply a |
| 86 | matrix to the buffer to prevent it from running off the end of the page. To save |
| 87 | power, do this pre-rotation. For details, see the <code>ANativeWindow</code> |
| 88 | interface defined in <code>system/core/include/system/window.h</code>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 89 | |
| 90 | <h3 id=gralloc_hal>Gralloc HAL</h3> |
| 91 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 92 | <p>The graphics memory allocator allocates memory requested by image producers. |
| 93 | You can find the interface definition of the HAL at |
| 94 | <code>hardware/libhardware/modules/gralloc.h</code>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 95 | |
| 96 | <h3 id=protected_buffers>Protected buffers</h3> |
| 97 | |
| 98 | <p>The gralloc usage flag <code>GRALLOC_USAGE_PROTECTED</code> allows the |
| 99 | graphics buffer to be displayed only through a hardware-protected path. These |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 100 | overlay planes are the only way to display DRM content (DRM-protected buffers |
| 101 | cannot be accessed by SurfaceFlinger or the OpenGL ES driver).</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 102 | |
| 103 | <p>DRM-protected video can be presented only on an overlay plane. Video players |
| 104 | that support protected content must be implemented with SurfaceView. Software |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 105 | running on unprotected hardware cannot read or write the buffer; |
| 106 | hardware-protected paths must appear on the Hardware Composer overlay (i.e., |
| 107 | protected videos will disappear from the display if Hardware Composer switches |
| 108 | to OpenGL ES composition).</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 109 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 110 | <p>For details on protected content, see |
| 111 | <a href="{@docRoot}devices/drm.html">DRM</a>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 112 | |
| 113 | <h3 id=hardware_composer_hal>Hardware Composer HAL</h3> |
| 114 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 115 | <p>The Hardware Composer HAL (HWC) is used by SurfaceFlinger to composite |
| 116 | surfaces to the screen. It abstracts objects such as overlays and 2D blitters |
| 117 | and helps offload some work that would normally be done with OpenGL. For details |
| 118 | on the HWC, see <a href="{@docRoot}devices/graphics/implement-hwc.html">Hardware |
| 119 | Composer HAL</a>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 120 | |
| 121 | <h3 id=vsync>VSYNC</h3> |
| 122 | |
| 123 | <p>VSYNC synchronizes certain events to the refresh cycle of the display. |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 124 | Applications always start drawing on a VSYNC boundary, and SurfaceFlinger always |
| 125 | composites on a VSYNC boundary. This eliminates stutters and improves visual |
| 126 | performance of graphics. For details on VSYNC, see |
| 127 | <a href="{@docRoot}devices/graphics/implement-vsync.html">Implementing |
| 128 | VSYNC</a>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 129 | |
| 130 | <h3 id=virtual_displays>Virtual displays</h3> |
| 131 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 132 | <p>Android added platform support for virtual displays in Hardware Composer v1.3. |
| 133 | The virtual display composition is similar to the physical display: Input |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 134 | layers are described in prepare(), SurfaceFlinger conducts GPU composition, and |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 135 | layers and GPU framebuffer are provided to Hardware Composer in set(). For |
| 136 | details on virtual displays, see |
| 137 | <a href="{@docRoot}devices/graphics/implement-vdisplays.html">Implementing |
| 138 | Virtual Displays</a>.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 139 | |
| 140 | <h2 id=testing>Testing</h2> |
| 141 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 142 | <p>For benchmarking, use the following flow by phase:</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 143 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 144 | <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 Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 156 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 157 | <p>For help deriving device capabilities during the specification phase, use the |
| 158 | Flatland tool at <code>platform/frameworks/native/cmds/flatland/</code>. |
| 159 | Flatland relies upon fixed clocks and shows the throughput achievable with |
| 160 | composition-based workloads. It uses gralloc buffers to simulate multiple window |
| 161 | scenarios, filling in the window with GL then measuring the compositing.</p> |
Clay Murphy | e3ae396 | 2014-09-02 17:30:57 -0700 | [diff] [blame] | 162 | |
Heidi von Markham | fd022c7 | 2016-06-30 10:15:28 -0700 | [diff] [blame^] | 163 | <p class="note"><strong>Note:</strong> Flatland uses the synchronization |
| 164 | framework to measure time, so your implementation must support the |
| 165 | synchronization framework.</p> |