John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 17 | #include "EglManager.h" |
| 18 | |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <cutils/properties.h> |
| 20 | #include <log/log.h> |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 21 | #include <utils/Trace.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "utils/StringUtils.h" |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 23 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 24 | #include "DeviceInfo.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 25 | #include "Frame.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 26 | #include "Properties.h" |
Mark Salyzyn | 173215d | 2017-01-12 08:27:11 -0800 | [diff] [blame] | 27 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 28 | #include <EGL/eglext.h> |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 29 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
Derek Sollenberger | 7e044fe | 2016-11-07 10:57:59 -0500 | [diff] [blame] | 32 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 33 | #define GLES_VERSION 2 |
| 34 | |
| 35 | // Android-specific addition that is used to show when frames began in systrace |
| 36 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 37 | |
| 38 | namespace android { |
| 39 | namespace uirenderer { |
| 40 | namespace renderthread { |
| 41 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | #define ERROR_CASE(x) \ |
| 43 | case x: \ |
| 44 | return #x; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 45 | static const char* egl_error_str(EGLint error) { |
| 46 | switch (error) { |
| 47 | ERROR_CASE(EGL_SUCCESS) |
| 48 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 49 | ERROR_CASE(EGL_BAD_ACCESS) |
| 50 | ERROR_CASE(EGL_BAD_ALLOC) |
| 51 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 52 | ERROR_CASE(EGL_BAD_CONFIG) |
| 53 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 54 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 55 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 56 | ERROR_CASE(EGL_BAD_MATCH) |
| 57 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 58 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 59 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 60 | ERROR_CASE(EGL_BAD_SURFACE) |
| 61 | ERROR_CASE(EGL_CONTEXT_LOST) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 62 | default: |
| 63 | return "Unknown error"; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 66 | const char* EglManager::eglErrorString() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 67 | return egl_error_str(eglGetError()); |
| 68 | } |
| 69 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 70 | static struct { |
| 71 | bool bufferAge = false; |
| 72 | bool setDamage = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 73 | bool noConfigContext = false; |
| 74 | bool pixelFormatFloat = false; |
| 75 | bool glColorSpace = false; |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 76 | bool scRGB = false; |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 77 | bool contextPriority = false; |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 78 | bool surfacelessContext = false; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 79 | } EglExtensions; |
| 80 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 81 | EglManager::EglManager() |
| 82 | : mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 83 | , mEglConfig(nullptr) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 84 | , mEglConfigWideGamut(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 85 | , mEglContext(EGL_NO_CONTEXT) |
| 86 | , mPBufferSurface(EGL_NO_SURFACE) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | , mCurrentSurface(EGL_NO_SURFACE) {} |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 88 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 89 | EglManager::~EglManager() { |
| 90 | destroy(); |
| 91 | } |
| 92 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 93 | void EglManager::initialize() { |
| 94 | if (hasEglContext()) return; |
| 95 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 96 | ATRACE_NAME("Creating EGLContext"); |
| 97 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 98 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 99 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s", |
| 100 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 101 | |
| 102 | EGLint major, minor; |
| 103 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 104 | "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 105 | |
| 106 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 107 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 108 | initExtensions(); |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 109 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 110 | // Now that extensions are loaded, pick a swap behavior |
| 111 | if (Properties::enablePartialUpdates) { |
Matt Sarett | b77c94a | 2016-12-07 12:22:37 -0500 | [diff] [blame] | 112 | // An Adreno driver bug is causing rendering problems for SkiaGL with |
| 113 | // buffer age swap behavior (b/31957043). To temporarily workaround, |
| 114 | // we will use preserved swap behavior. |
Derek Sollenberger | b5a12bd | 2017-06-14 15:23:19 -0400 | [diff] [blame] | 115 | if (Properties::useBufferAge && EglExtensions.bufferAge) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 116 | mSwapBehavior = SwapBehavior::BufferAge; |
| 117 | } else { |
| 118 | mSwapBehavior = SwapBehavior::Preserved; |
| 119 | } |
| 120 | } |
| 121 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 122 | loadConfigs(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 123 | createContext(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 124 | createPBufferSurface(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 125 | makeCurrent(mPBufferSurface, nullptr, /* force */ true); |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 126 | DeviceInfo::initialize(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 127 | } |
| 128 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 129 | void EglManager::initExtensions() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 131 | |
John Reck | 8733bff | 2016-09-27 14:45:28 -0700 | [diff] [blame] | 132 | // For our purposes we don't care if EGL_BUFFER_AGE is a result of |
| 133 | // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered |
| 134 | // under EGL_KHR_partial_update and we don't need the expanded scope |
| 135 | // that EGL_EXT_buffer_age provides. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 136 | EglExtensions.bufferAge = |
| 137 | extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update"); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 138 | EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); |
John Reck | 708b668 | 2015-10-22 13:11:00 -0700 | [diff] [blame] | 139 | LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 140 | "Missing required extension EGL_KHR_swap_buffers_with_damage"); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 141 | |
| 142 | EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace"); |
| 143 | EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context"); |
| 144 | EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float"); |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 145 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 146 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear"); |
| 147 | #else |
| 148 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb"); |
| 149 | #endif |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 150 | EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority"); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 151 | EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context"); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 152 | } |
| 153 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 154 | bool EglManager::hasEglContext() { |
| 155 | return mEglDisplay != EGL_NO_DISPLAY; |
| 156 | } |
| 157 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 158 | void EglManager::loadConfigs() { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 159 | ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior)); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | EGLint swapBehavior = |
| 161 | (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 162 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 163 | EGL_OPENGL_ES2_BIT, |
| 164 | EGL_RED_SIZE, |
| 165 | 8, |
| 166 | EGL_GREEN_SIZE, |
| 167 | 8, |
| 168 | EGL_BLUE_SIZE, |
| 169 | 8, |
| 170 | EGL_ALPHA_SIZE, |
| 171 | 8, |
| 172 | EGL_DEPTH_SIZE, |
| 173 | 0, |
| 174 | EGL_CONFIG_CAVEAT, |
| 175 | EGL_NONE, |
| 176 | EGL_STENCIL_SIZE, |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 177 | STENCIL_BUFFER_SIZE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 178 | EGL_SURFACE_TYPE, |
| 179 | EGL_WINDOW_BIT | swapBehavior, |
| 180 | EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 181 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 182 | EGLint numConfigs = 1; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 183 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) || |
| 184 | numConfigs != 1) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 185 | if (mSwapBehavior == SwapBehavior::Preserved) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 186 | // Try again without dirty regions enabled |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 187 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 188 | mSwapBehavior = SwapBehavior::Discard; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 189 | loadConfigs(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | return; // the call to loadConfigs() we just made picks the wide gamut config |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 191 | } else { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 192 | // Failed to get a valid config |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 193 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 196 | |
| 197 | if (EglExtensions.pixelFormatFloat) { |
| 198 | // If we reached this point, we have a valid swap behavior |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 199 | EGLint attribs16F[] = {EGL_RENDERABLE_TYPE, |
| 200 | EGL_OPENGL_ES2_BIT, |
| 201 | EGL_COLOR_COMPONENT_TYPE_EXT, |
| 202 | EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT, |
| 203 | EGL_RED_SIZE, |
| 204 | 16, |
| 205 | EGL_GREEN_SIZE, |
| 206 | 16, |
| 207 | EGL_BLUE_SIZE, |
| 208 | 16, |
| 209 | EGL_ALPHA_SIZE, |
| 210 | 16, |
| 211 | EGL_DEPTH_SIZE, |
| 212 | 0, |
| 213 | EGL_STENCIL_SIZE, |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 214 | STENCIL_BUFFER_SIZE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 215 | EGL_SURFACE_TYPE, |
| 216 | EGL_WINDOW_BIT | swapBehavior, |
| 217 | EGL_NONE}; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 218 | |
| 219 | numConfigs = 1; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 220 | if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs, |
| 221 | &numConfigs) || |
| 222 | numConfigs != 1) { |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 223 | ALOGE("Device claims wide gamut support, cannot find matching config, error = %s", |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame] | 224 | eglErrorString()); |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 225 | EglExtensions.pixelFormatFloat = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void EglManager::createContext() { |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 231 | std::vector<EGLint> contextAttributes; |
| 232 | contextAttributes.reserve(5); |
| 233 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 234 | contextAttributes.push_back(GLES_VERSION); |
| 235 | if (Properties::contextPriority != 0 && EglExtensions.contextPriority) { |
| 236 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 237 | contextAttributes.push_back(Properties::contextPriority); |
| 238 | } |
| 239 | contextAttributes.push_back(EGL_NONE); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 240 | mEglContext = eglCreateContext( |
| 241 | mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig, |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 242 | EGL_NO_CONTEXT, contextAttributes.data()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 243 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s", |
| 244 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 245 | } |
| 246 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 247 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 248 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 249 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 250 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 251 | if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 252 | EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 253 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 254 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 257 | EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 258 | LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized"); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 259 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 260 | wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB && |
| 261 | EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 262 | |
| 263 | // The color space we want to use depends on whether linear blending is turned |
| 264 | // on and whether the app has requested wide color gamut rendering. When wide |
| 265 | // color gamut rendering is off, the app simply renders in the display's native |
| 266 | // color gamut. |
| 267 | // |
| 268 | // When wide gamut rendering is off: |
| 269 | // - Blending is done by default in gamma space, which requires using a |
| 270 | // linear EGL color space (the GPU uses the color values as is) |
| 271 | // - If linear blending is on, we must use the sRGB EGL color space (the |
| 272 | // GPU will perform sRGB to linear and linear to SRGB conversions before |
| 273 | // and after blending) |
| 274 | // |
| 275 | // When wide gamut rendering is on we cannot rely on the GPU performing |
| 276 | // linear blending for us. We use two different color spaces to tag the |
| 277 | // surface appropriately for SurfaceFlinger: |
| 278 | // - Gamma blending (default) requires the use of the scRGB-nl color space |
| 279 | // - Linear blending requires the use of the scRGB color space |
| 280 | |
| 281 | // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension |
| 282 | // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value. |
| 283 | // According to section 3.4.1 of the EGL specification, the attributes |
| 284 | // list is considered empty if the first entry is EGL_NONE |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 285 | EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE}; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 286 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 287 | if (EglExtensions.glColorSpace) { |
| 288 | attribs[0] = EGL_GL_COLORSPACE_KHR; |
| 289 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 290 | if (wideColorGamut) { |
| 291 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT; |
| 292 | } else { |
| 293 | attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR; |
| 294 | } |
| 295 | #else |
| 296 | if (wideColorGamut) { |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 297 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 298 | } else { |
| 299 | attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR; |
| 300 | } |
| 301 | #endif |
| 302 | } |
| 303 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 304 | EGLSurface surface = eglCreateWindowSurface( |
| 305 | mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 306 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 307 | "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window, |
| 308 | eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 309 | |
| 310 | if (mSwapBehavior != SwapBehavior::Preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 311 | LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 312 | EGL_BUFFER_DESTROYED) == EGL_FALSE, |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 313 | "Failed to set swap behavior to destroyed for window %p, eglErr = %s", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 314 | (void*)window, eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 317 | return surface; |
| 318 | } |
| 319 | |
| 320 | void EglManager::destroySurface(EGLSurface surface) { |
| 321 | if (isCurrent(surface)) { |
| 322 | makeCurrent(EGL_NO_SURFACE); |
| 323 | } |
| 324 | if (!eglDestroySurface(mEglDisplay, surface)) { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 325 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | void EglManager::destroy() { |
| 330 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 331 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 332 | eglDestroyContext(mEglDisplay, mEglContext); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 333 | if (mPBufferSurface != EGL_NO_SURFACE) { |
| 334 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 335 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 336 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 337 | eglTerminate(mEglDisplay); |
| 338 | eglReleaseThread(); |
| 339 | |
| 340 | mEglDisplay = EGL_NO_DISPLAY; |
| 341 | mEglContext = EGL_NO_CONTEXT; |
| 342 | mPBufferSurface = EGL_NO_SURFACE; |
| 343 | mCurrentSurface = EGL_NO_SURFACE; |
| 344 | } |
| 345 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 346 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) { |
| 347 | if (!force && isCurrent(surface)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 348 | |
| 349 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 350 | // Ensure we always have a valid surface & context |
| 351 | surface = mPBufferSurface; |
| 352 | } |
| 353 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 354 | if (errOut) { |
| 355 | *errOut = eglGetError(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 356 | ALOGW("Failed to make current on surface %p, error=%s", (void*)surface, |
| 357 | egl_error_str(*errOut)); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 358 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 359 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface, |
| 360 | eglErrorString()); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 361 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 362 | } |
| 363 | mCurrentSurface = surface; |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 364 | if (Properties::disableVsync) { |
| 365 | eglSwapInterval(mEglDisplay, 0); |
| 366 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 370 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 371 | switch (mSwapBehavior) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 372 | case SwapBehavior::Discard: |
| 373 | return 0; |
| 374 | case SwapBehavior::Preserved: |
| 375 | return 1; |
| 376 | case SwapBehavior::BufferAge: |
| 377 | EGLint bufferAge; |
| 378 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 379 | return bufferAge; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 385 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 386 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 387 | Frame frame; |
| 388 | frame.mSurface = surface; |
| 389 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 390 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 391 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 392 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 393 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 394 | } |
| 395 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 396 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 397 | #ifdef EGL_KHR_partial_update |
| 398 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 399 | EGLint rects[4]; |
| 400 | frame.map(dirty, rects); |
| 401 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 402 | LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 403 | (void*)frame.mSurface, eglErrorString()); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | #endif |
| 407 | } |
| 408 | |
John Reck | c96955d | 2016-02-26 14:56:44 -0800 | [diff] [blame] | 409 | bool EglManager::damageRequiresSwap() { |
| 410 | return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; |
| 411 | } |
| 412 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 413 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 414 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 415 | ATRACE_NAME("Finishing GPU work"); |
| 416 | fence(); |
| 417 | } |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 418 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 419 | EGLint rects[4]; |
| 420 | frame.map(screenDirty, rects); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 421 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 422 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 423 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 424 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 425 | return true; |
| 426 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 427 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 428 | // For some reason our surface was destroyed out from under us |
| 429 | // This really shouldn't happen, but if it does we can recover easily |
| 430 | // by just not trying to use the surface anymore |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 431 | ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err, |
| 432 | frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 435 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err)); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 436 | // Impossible to hit this, but the compiler doesn't know that |
| 437 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 438 | } |
| 439 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 440 | void EglManager::fence() { |
| 441 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 442 | eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 443 | eglDestroySyncKHR(mEglDisplay, fence); |
| 444 | } |
| 445 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 446 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 447 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 448 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 449 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 450 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 451 | if (!preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 452 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface, |
| 453 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 454 | // Maybe it's already set? |
| 455 | EGLint swapBehavior; |
| 456 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 457 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 458 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 459 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface, |
| 460 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 461 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 462 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 463 | |
| 464 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 465 | } |
| 466 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 467 | } /* namespace renderthread */ |
| 468 | } /* namespace uirenderer */ |
| 469 | } /* namespace android */ |