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 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 19 | #include <EGL/eglext.h> |
| 20 | #include <GLES/gl.h> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
| 22 | #include <log/log.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 23 | #include <sync/sync.h> |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 24 | #include <utils/Trace.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 25 | |
| 26 | #include <string> |
| 27 | #include <vector> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 29 | #include "Frame.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 30 | #include "Properties.h" |
Nader Jawad | 086645d | 2021-09-24 13:42:47 -0700 | [diff] [blame] | 31 | #include "RenderEffectCapabilityQuery.h" |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 32 | #include "utils/Color.h" |
| 33 | #include "utils/StringUtils.h" |
Derek Sollenberger | 7e044fe | 2016-11-07 10:57:59 -0500 | [diff] [blame] | 34 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 35 | #define GLES_VERSION 2 |
| 36 | |
| 37 | // Android-specific addition that is used to show when frames began in systrace |
| 38 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 39 | |
John Reck | 4bd8d8a | 2023-12-08 11:27:19 -0500 | [diff] [blame^] | 40 | static constexpr auto P3_XRB = static_cast<android_dataspace>( |
| 41 | ADATASPACE_STANDARD_DCI_P3 | ADATASPACE_TRANSFER_SRGB | ADATASPACE_RANGE_EXTENDED); |
| 42 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace uirenderer { |
| 45 | namespace renderthread { |
| 46 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 47 | #define ERROR_CASE(x) \ |
| 48 | case x: \ |
| 49 | return #x; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 50 | static const char* egl_error_str(EGLint error) { |
| 51 | switch (error) { |
| 52 | ERROR_CASE(EGL_SUCCESS) |
| 53 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 54 | ERROR_CASE(EGL_BAD_ACCESS) |
| 55 | ERROR_CASE(EGL_BAD_ALLOC) |
| 56 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 57 | ERROR_CASE(EGL_BAD_CONFIG) |
| 58 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 59 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 60 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 61 | ERROR_CASE(EGL_BAD_MATCH) |
| 62 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 63 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 64 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 65 | ERROR_CASE(EGL_BAD_SURFACE) |
| 66 | ERROR_CASE(EGL_CONTEXT_LOST) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | default: |
| 68 | return "Unknown error"; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 71 | const char* EglManager::eglErrorString() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 72 | return egl_error_str(eglGetError()); |
| 73 | } |
| 74 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 75 | static struct { |
| 76 | bool bufferAge = false; |
| 77 | bool setDamage = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 78 | bool noConfigContext = false; |
| 79 | bool pixelFormatFloat = false; |
| 80 | bool glColorSpace = false; |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 81 | bool scRGB = false; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 82 | bool displayP3 = false; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 83 | bool hdr = false; |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 84 | bool contextPriority = false; |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 85 | bool surfacelessContext = false; |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 86 | bool nativeFenceSync = false; |
| 87 | bool fenceSync = false; |
| 88 | bool waitSync = false; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 89 | } EglExtensions; |
| 90 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 91 | EglManager::EglManager() |
| 92 | : mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 93 | , mEglConfig(nullptr) |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 94 | , mEglConfigF16(nullptr) |
| 95 | , mEglConfig1010102(nullptr) |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 96 | , mEglConfigA8(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 97 | , mEglContext(EGL_NO_CONTEXT) |
| 98 | , mPBufferSurface(EGL_NO_SURFACE) |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 99 | , mCurrentSurface(EGL_NO_SURFACE) |
| 100 | , mHasWideColorGamutSupport(false) {} |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 101 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 102 | EglManager::~EglManager() { |
John Reck | 6104cea | 2019-01-10 14:37:17 -0800 | [diff] [blame] | 103 | if (hasEglContext()) { |
| 104 | ALOGW("~EglManager() leaked an EGL context"); |
| 105 | } |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 106 | } |
| 107 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 108 | void EglManager::initialize() { |
| 109 | if (hasEglContext()) return; |
| 110 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 111 | ATRACE_NAME("Creating EGLContext"); |
| 112 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 113 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s", |
| 115 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 116 | |
| 117 | EGLint major, minor; |
| 118 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 120 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 121 | ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 122 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 123 | initExtensions(); |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 124 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 125 | // Now that extensions are loaded, pick a swap behavior |
| 126 | if (Properties::enablePartialUpdates) { |
Matt Sarett | b77c94a | 2016-12-07 12:22:37 -0500 | [diff] [blame] | 127 | // An Adreno driver bug is causing rendering problems for SkiaGL with |
| 128 | // buffer age swap behavior (b/31957043). To temporarily workaround, |
| 129 | // we will use preserved swap behavior. |
Derek Sollenberger | b5a12bd | 2017-06-14 15:23:19 -0400 | [diff] [blame] | 130 | if (Properties::useBufferAge && EglExtensions.bufferAge) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 131 | mSwapBehavior = SwapBehavior::BufferAge; |
| 132 | } else { |
| 133 | mSwapBehavior = SwapBehavior::Preserved; |
| 134 | } |
| 135 | } |
| 136 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 137 | loadConfigs(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 138 | createContext(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 139 | createPBufferSurface(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 140 | makeCurrent(mPBufferSurface, nullptr, /* force */ true); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 141 | |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 142 | skcms_Matrix3x3 wideColorGamut; |
| 143 | LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut), |
| 144 | "Could not get gamut matrix from wideColorSpace"); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 145 | bool hasWideColorSpaceExtension = false; |
Mike Reed | 7ac1af3 | 2020-05-26 10:50:21 -0400 | [diff] [blame] | 146 | if (memcmp(&wideColorGamut, &SkNamedGamut::kDisplayP3, sizeof(wideColorGamut)) == 0) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 147 | hasWideColorSpaceExtension = EglExtensions.displayP3; |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 148 | } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 149 | hasWideColorSpaceExtension = EglExtensions.scRGB; |
| 150 | } else { |
| 151 | LOG_ALWAYS_FATAL("Unsupported wide color space."); |
| 152 | } |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 153 | mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension; |
Nader Jawad | 4a6b60a | 2021-09-20 21:22:50 -0700 | [diff] [blame] | 154 | |
| 155 | auto* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR)); |
Nader Jawad | 086645d | 2021-09-24 13:42:47 -0700 | [diff] [blame] | 156 | auto* version = reinterpret_cast<const char*>(glGetString(GL_VERSION)); |
| 157 | Properties::enableRenderEffectCache = supportsRenderEffectCache( |
| 158 | vendor, version); |
| 159 | ALOGV("RenderEffectCache supported %d on driver version %s", |
| 160 | Properties::enableRenderEffectCache, version); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) { |
| 164 | EGLint eglSwapBehavior = |
| 165 | (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 166 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 167 | EGL_OPENGL_ES2_BIT, |
| 168 | EGL_RED_SIZE, |
| 169 | 8, |
| 170 | EGL_GREEN_SIZE, |
| 171 | 8, |
| 172 | EGL_BLUE_SIZE, |
| 173 | 8, |
| 174 | EGL_ALPHA_SIZE, |
| 175 | 8, |
| 176 | EGL_DEPTH_SIZE, |
| 177 | 0, |
| 178 | EGL_CONFIG_CAVEAT, |
| 179 | EGL_NONE, |
| 180 | EGL_STENCIL_SIZE, |
| 181 | STENCIL_BUFFER_SIZE, |
| 182 | EGL_SURFACE_TYPE, |
| 183 | EGL_WINDOW_BIT | eglSwapBehavior, |
| 184 | EGL_NONE}; |
| 185 | EGLConfig config = EGL_NO_CONFIG_KHR; |
| 186 | EGLint numConfigs = 1; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 187 | if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 188 | return EGL_NO_CONFIG_KHR; |
| 189 | } |
| 190 | return config; |
| 191 | } |
| 192 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 193 | EGLConfig EglManager::load1010102Config(EGLDisplay display, SwapBehavior swapBehavior) { |
| 194 | EGLint eglSwapBehavior = |
| 195 | (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 196 | // If we reached this point, we have a valid swap behavior |
| 197 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 198 | EGL_OPENGL_ES2_BIT, |
| 199 | EGL_RED_SIZE, |
| 200 | 10, |
| 201 | EGL_GREEN_SIZE, |
| 202 | 10, |
| 203 | EGL_BLUE_SIZE, |
| 204 | 10, |
| 205 | EGL_ALPHA_SIZE, |
| 206 | 2, |
| 207 | EGL_DEPTH_SIZE, |
| 208 | 0, |
| 209 | EGL_STENCIL_SIZE, |
| 210 | STENCIL_BUFFER_SIZE, |
| 211 | EGL_SURFACE_TYPE, |
| 212 | EGL_WINDOW_BIT | eglSwapBehavior, |
| 213 | EGL_NONE}; |
| 214 | EGLConfig config = EGL_NO_CONFIG_KHR; |
| 215 | EGLint numConfigs = 1; |
| 216 | if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) { |
| 217 | return EGL_NO_CONFIG_KHR; |
| 218 | } |
| 219 | return config; |
| 220 | } |
| 221 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 222 | EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) { |
| 223 | EGLint eglSwapBehavior = |
| 224 | (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 225 | // If we reached this point, we have a valid swap behavior |
| 226 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 227 | EGL_OPENGL_ES2_BIT, |
| 228 | EGL_COLOR_COMPONENT_TYPE_EXT, |
| 229 | EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT, |
| 230 | EGL_RED_SIZE, |
| 231 | 16, |
| 232 | EGL_GREEN_SIZE, |
| 233 | 16, |
| 234 | EGL_BLUE_SIZE, |
| 235 | 16, |
| 236 | EGL_ALPHA_SIZE, |
| 237 | 16, |
| 238 | EGL_DEPTH_SIZE, |
| 239 | 0, |
| 240 | EGL_STENCIL_SIZE, |
| 241 | STENCIL_BUFFER_SIZE, |
| 242 | EGL_SURFACE_TYPE, |
| 243 | EGL_WINDOW_BIT | eglSwapBehavior, |
| 244 | EGL_NONE}; |
| 245 | EGLConfig config = EGL_NO_CONFIG_KHR; |
| 246 | EGLint numConfigs = 1; |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 247 | if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) { |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 248 | return EGL_NO_CONFIG_KHR; |
| 249 | } |
| 250 | return config; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 253 | EGLConfig EglManager::loadA8Config(EGLDisplay display, EglManager::SwapBehavior swapBehavior) { |
| 254 | EGLint eglSwapBehavior = |
| 255 | (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 256 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 257 | EGL_OPENGL_ES2_BIT, |
| 258 | EGL_RED_SIZE, |
| 259 | 8, |
| 260 | EGL_GREEN_SIZE, |
| 261 | 0, |
| 262 | EGL_BLUE_SIZE, |
| 263 | 0, |
| 264 | EGL_ALPHA_SIZE, |
| 265 | 0, |
| 266 | EGL_DEPTH_SIZE, |
| 267 | 0, |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 268 | EGL_SURFACE_TYPE, |
| 269 | EGL_WINDOW_BIT | eglSwapBehavior, |
| 270 | EGL_NONE}; |
| 271 | EGLint numConfigs = 1; |
| 272 | if (!eglChooseConfig(display, attribs, nullptr, numConfigs, &numConfigs)) { |
| 273 | return EGL_NO_CONFIG_KHR; |
| 274 | } |
| 275 | |
| 276 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); |
| 277 | if (!eglChooseConfig(display, attribs, configs.data(), numConfigs, &numConfigs)) { |
| 278 | return EGL_NO_CONFIG_KHR; |
| 279 | } |
| 280 | |
| 281 | // The component sizes passed to eglChooseConfig are minimums, so configs |
| 282 | // contains entries that exceed them. Choose one that matches the sizes |
| 283 | // exactly. |
| 284 | for (EGLConfig config : configs) { |
| 285 | EGLint r{0}, g{0}, b{0}, a{0}; |
| 286 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 287 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 288 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 289 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 290 | if (8 == r && 0 == g && 0 == b && 0 == a) { |
| 291 | return config; |
| 292 | } |
| 293 | } |
| 294 | return EGL_NO_CONFIG_KHR; |
| 295 | } |
| 296 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 297 | void EglManager::initExtensions() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 298 | auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 299 | |
John Reck | 8733bff | 2016-09-27 14:45:28 -0700 | [diff] [blame] | 300 | // For our purposes we don't care if EGL_BUFFER_AGE is a result of |
| 301 | // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered |
| 302 | // under EGL_KHR_partial_update and we don't need the expanded scope |
| 303 | // that EGL_EXT_buffer_age provides. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 304 | EglExtensions.bufferAge = |
| 305 | extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update"); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 306 | EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); |
John Reck | 708b668 | 2015-10-22 13:11:00 -0700 | [diff] [blame] | 307 | LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 308 | "Missing required extension EGL_KHR_swap_buffers_with_damage"); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 309 | |
| 310 | EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace"); |
| 311 | EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context"); |
| 312 | EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float"); |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 313 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb"); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 314 | EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough"); |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 315 | EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq"); |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 316 | EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority"); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 317 | EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context"); |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 318 | EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync"); |
| 319 | EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync"); |
Yiwei Zhang | 7f43013 | 2020-08-12 15:07:51 -0700 | [diff] [blame] | 320 | EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync"); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 321 | } |
| 322 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 323 | bool EglManager::hasEglContext() { |
| 324 | return mEglDisplay != EGL_NO_DISPLAY; |
| 325 | } |
| 326 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 327 | void EglManager::loadConfigs() { |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 328 | // Note: The default pixel format is RGBA_8888, when other formats are |
| 329 | // available, we should check the target pixel format and configure the |
| 330 | // attributes list properly. |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 331 | mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior); |
| 332 | if (mEglConfig == EGL_NO_CONFIG_KHR) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 333 | if (mSwapBehavior == SwapBehavior::Preserved) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 334 | // Try again without dirty regions enabled |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 335 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 336 | mSwapBehavior = SwapBehavior::Discard; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 337 | mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 338 | } else { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 339 | // Failed to get a valid config |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 340 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 343 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 344 | // When we reach this point, we have a valid swap behavior |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 345 | if (EglExtensions.pixelFormatFloat) { |
| 346 | mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior); |
| 347 | if (mEglConfigF16 == EGL_NO_CONFIG_KHR) { |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 348 | ALOGE("Device claims wide gamut support, cannot find matching config, error = %s", |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 349 | eglErrorString()); |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 350 | EglExtensions.pixelFormatFloat = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 351 | } |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 352 | } |
| 353 | mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior); |
| 354 | if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) { |
| 355 | ALOGW("Failed to initialize 101010-2 format, error = %s", |
| 356 | eglErrorString()); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 357 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void EglManager::createContext() { |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 361 | std::vector<EGLint> contextAttributes; |
| 362 | contextAttributes.reserve(5); |
| 363 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 364 | contextAttributes.push_back(GLES_VERSION); |
| 365 | if (Properties::contextPriority != 0 && EglExtensions.contextPriority) { |
| 366 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 367 | contextAttributes.push_back(Properties::contextPriority); |
| 368 | } |
| 369 | contextAttributes.push_back(EGL_NONE); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 370 | mEglContext = eglCreateContext( |
| 371 | mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig, |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 372 | EGL_NO_CONTEXT, contextAttributes.data()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 373 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s", |
| 374 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 375 | } |
| 376 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 377 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 378 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 379 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 380 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 381 | if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 382 | EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 383 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
Roman Kiryanov | 6073613 | 2019-08-02 14:37:53 -0700 | [diff] [blame] | 384 | LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE, |
| 385 | "Failed to create a pixel buffer display=%p, " |
| 386 | "mEglConfig=%p, error=%s", |
| 387 | mEglDisplay, mEglConfig, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 388 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 391 | Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window, |
| 392 | ColorMode colorMode, |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 393 | sk_sp<SkColorSpace> colorSpace) { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 394 | LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized"); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 395 | |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 396 | if (!EglExtensions.noConfigContext) { |
| 397 | // The caller shouldn't use A8 if we cannot switch modes. |
| 398 | LOG_ALWAYS_FATAL_IF(colorMode == ColorMode::A8, |
| 399 | "Cannot use A8 without EGL_KHR_no_config_context!"); |
| 400 | |
| 401 | // Cannot switch modes without EGL_KHR_no_config_context. |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 402 | colorMode = ColorMode::Default; |
| 403 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 404 | // The color space we want to use depends on whether linear blending is turned |
| 405 | // on and whether the app has requested wide color gamut rendering. When wide |
| 406 | // color gamut rendering is off, the app simply renders in the display's native |
| 407 | // color gamut. |
| 408 | // |
| 409 | // When wide gamut rendering is off: |
| 410 | // - Blending is done by default in gamma space, which requires using a |
| 411 | // linear EGL color space (the GPU uses the color values as is) |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 412 | // - If linear blending is on, we must use the non-linear EGL color space |
| 413 | // (the GPU will perform sRGB to linear and linear to SRGB conversions |
| 414 | // before and after blending) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 415 | // |
| 416 | // When wide gamut rendering is on we cannot rely on the GPU performing |
| 417 | // linear blending for us. We use two different color spaces to tag the |
| 418 | // surface appropriately for SurfaceFlinger: |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 419 | // - Gamma blending (default) requires the use of the non-linear color space |
| 420 | // - Linear blending requires the use of the linear color space |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 421 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 422 | // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 423 | // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value. |
| 424 | // According to section 3.4.1 of the EGL specification, the attributes |
| 425 | // list is considered empty if the first entry is EGL_NONE |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 426 | EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE}; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 427 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 428 | EGLConfig config = mEglConfig; |
John Reck | 52d4595 | 2023-04-05 15:36:15 -0400 | [diff] [blame] | 429 | bool overrideWindowDataSpaceForHdr = false; |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 430 | if (colorMode == ColorMode::A8) { |
| 431 | // A8 doesn't use a color space |
Leon Scroggins III | e040b29 | 2021-12-14 17:22:25 -0500 | [diff] [blame] | 432 | if (!mEglConfigA8) { |
| 433 | mEglConfigA8 = loadA8Config(mEglDisplay, mSwapBehavior); |
| 434 | LOG_ALWAYS_FATAL_IF(!mEglConfigA8, |
| 435 | "Requested ColorMode::A8, but EGL lacks support! error = %s", |
| 436 | eglErrorString()); |
| 437 | } |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 438 | config = mEglConfigA8; |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 439 | } else { |
| 440 | if (!mHasWideColorGamutSupport) { |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 441 | colorMode = ColorMode::Default; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 442 | } |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 443 | |
| 444 | if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) { |
| 445 | if (mEglConfigF16 == EGL_NO_CONFIG_KHR) { |
| 446 | colorMode = ColorMode::Default; |
| 447 | } else { |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 448 | config = mEglConfigF16; |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | if (EglExtensions.glColorSpace) { |
| 452 | attribs[0] = EGL_GL_COLORSPACE_KHR; |
| 453 | switch (colorMode) { |
| 454 | case ColorMode::Default: |
| 455 | attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR; |
| 456 | break; |
John Reck | 52d4595 | 2023-04-05 15:36:15 -0400 | [diff] [blame] | 457 | // We don't have an EGL colorspace for extended range P3 that's used for HDR |
| 458 | // So override it after configuring the EGL context |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 459 | case ColorMode::Hdr: |
John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 460 | case ColorMode::Hdr10: |
John Reck | 52d4595 | 2023-04-05 15:36:15 -0400 | [diff] [blame] | 461 | overrideWindowDataSpaceForHdr = true; |
| 462 | attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT; |
| 463 | break; |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 464 | case ColorMode::WideColorGamut: { |
| 465 | skcms_Matrix3x3 colorGamut; |
| 466 | LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut), |
| 467 | "Could not get gamut matrix from color space"); |
| 468 | if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) { |
| 469 | attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT; |
| 470 | } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) { |
| 471 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT; |
| 472 | } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == |
| 473 | 0) { |
| 474 | attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT; |
| 475 | } else { |
| 476 | LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space."); |
| 477 | } |
| 478 | break; |
| 479 | } |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 480 | case ColorMode::A8: |
| 481 | LOG_ALWAYS_FATAL("Unreachable: A8 doesn't use a color space"); |
| 482 | break; |
| 483 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 484 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 485 | } |
| 486 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 487 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs); |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 488 | if (surface == EGL_NO_SURFACE) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 489 | return Error<EGLint>{eglGetError()}; |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 490 | } |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 491 | |
| 492 | if (mSwapBehavior != SwapBehavior::Preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 493 | LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 494 | EGL_BUFFER_DESTROYED) == EGL_FALSE, |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 495 | "Failed to set swap behavior to destroyed for window %p, eglErr = %s", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 496 | (void*)window, eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 497 | } |
| 498 | |
John Reck | 52d4595 | 2023-04-05 15:36:15 -0400 | [diff] [blame] | 499 | if (overrideWindowDataSpaceForHdr) { |
| 500 | // This relies on knowing that EGL will not re-set the dataspace after the call to |
| 501 | // eglCreateWindowSurface. Since the handling of the colorspace extension is largely |
| 502 | // implemented in libEGL in the platform, we can safely assume this is the case |
John Reck | 4bd8d8a | 2023-12-08 11:27:19 -0500 | [diff] [blame^] | 503 | int32_t err = ANativeWindow_setBuffersDataSpace(window, P3_XRB); |
John Reck | 52d4595 | 2023-04-05 15:36:15 -0400 | [diff] [blame] | 504 | LOG_ALWAYS_FATAL_IF(err, "Failed to ANativeWindow_setBuffersDataSpace %d", err); |
| 505 | } |
| 506 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 507 | return surface; |
| 508 | } |
| 509 | |
| 510 | void EglManager::destroySurface(EGLSurface surface) { |
| 511 | if (isCurrent(surface)) { |
| 512 | makeCurrent(EGL_NO_SURFACE); |
| 513 | } |
| 514 | if (!eglDestroySurface(mEglDisplay, surface)) { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 515 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | void EglManager::destroy() { |
| 520 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 521 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 522 | eglDestroyContext(mEglDisplay, mEglContext); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 523 | if (mPBufferSurface != EGL_NO_SURFACE) { |
| 524 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 525 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 526 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 527 | eglTerminate(mEglDisplay); |
| 528 | eglReleaseThread(); |
| 529 | |
| 530 | mEglDisplay = EGL_NO_DISPLAY; |
| 531 | mEglContext = EGL_NO_CONTEXT; |
| 532 | mPBufferSurface = EGL_NO_SURFACE; |
| 533 | mCurrentSurface = EGL_NO_SURFACE; |
| 534 | } |
| 535 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 536 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) { |
| 537 | if (!force && isCurrent(surface)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 538 | |
| 539 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 540 | // Ensure we always have a valid surface & context |
| 541 | surface = mPBufferSurface; |
| 542 | } |
| 543 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 544 | if (errOut) { |
| 545 | *errOut = eglGetError(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 546 | ALOGW("Failed to make current on surface %p, error=%s", (void*)surface, |
| 547 | egl_error_str(*errOut)); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 548 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 549 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface, |
| 550 | eglErrorString()); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 551 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 552 | } |
| 553 | mCurrentSurface = surface; |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 554 | if (Properties::disableVsync) { |
| 555 | eglSwapInterval(mEglDisplay, 0); |
| 556 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 560 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 561 | switch (mSwapBehavior) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 562 | case SwapBehavior::Discard: |
| 563 | return 0; |
| 564 | case SwapBehavior::Preserved: |
| 565 | return 1; |
| 566 | case SwapBehavior::BufferAge: |
| 567 | EGLint bufferAge; |
| 568 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 569 | return bufferAge; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 570 | } |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 575 | 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] | 576 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 577 | Frame frame; |
| 578 | frame.mSurface = surface; |
| 579 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 580 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 581 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 582 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 583 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 584 | } |
| 585 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 586 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 587 | #ifdef EGL_KHR_partial_update |
| 588 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 589 | EGLint rects[4]; |
| 590 | frame.map(dirty, rects); |
| 591 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 592 | 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] | 593 | (void*)frame.mSurface, eglErrorString()); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | #endif |
| 597 | } |
| 598 | |
John Reck | c96955d | 2016-02-26 14:56:44 -0800 | [diff] [blame] | 599 | bool EglManager::damageRequiresSwap() { |
| 600 | return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; |
| 601 | } |
| 602 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 603 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 604 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 605 | ATRACE_NAME("Finishing GPU work"); |
| 606 | fence(); |
| 607 | } |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 608 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 609 | EGLint rects[4]; |
| 610 | frame.map(screenDirty, rects); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 611 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 612 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 613 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 614 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 615 | return true; |
| 616 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 617 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 618 | // For some reason our surface was destroyed out from under us |
| 619 | // This really shouldn't happen, but if it does we can recover easily |
| 620 | // by just not trying to use the surface anymore |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 621 | ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err, |
| 622 | frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 623 | return false; |
| 624 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 625 | 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] | 626 | // Impossible to hit this, but the compiler doesn't know that |
| 627 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 628 | } |
| 629 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 630 | void EglManager::fence() { |
| 631 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 632 | eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 633 | eglDestroySyncKHR(mEglDisplay, fence); |
| 634 | } |
| 635 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 636 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 637 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 638 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 639 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 640 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 641 | if (!preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 642 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface, |
| 643 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 644 | // Maybe it's already set? |
| 645 | EGLint swapBehavior; |
| 646 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 647 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 648 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 649 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface, |
| 650 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 651 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 652 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 653 | |
| 654 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 657 | static status_t waitForeverOnFence(int fence, const char* logname) { |
| 658 | ATRACE_CALL(); |
| 659 | if (fence == -1) { |
| 660 | return NO_ERROR; |
| 661 | } |
| 662 | constexpr int warningTimeout = 3000; |
| 663 | int err = sync_wait(fence, warningTimeout); |
| 664 | if (err < 0 && errno == ETIME) { |
| 665 | ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout); |
| 666 | err = sync_wait(fence, -1); |
| 667 | } |
| 668 | return err < 0 ? -errno : status_t(NO_ERROR); |
| 669 | } |
| 670 | |
| 671 | status_t EglManager::fenceWait(int fence) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 672 | if (!hasEglContext()) { |
| 673 | ALOGE("EglManager::fenceWait: EGLDisplay not initialized"); |
| 674 | return INVALID_OPERATION; |
| 675 | } |
| 676 | |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 677 | if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 678 | // Block GPU on the fence. |
| 679 | // Create an EGLSyncKHR from the current fence. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 680 | int fenceFd = ::dup(fence); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 681 | if (fenceFd == -1) { |
| 682 | ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno); |
| 683 | return -errno; |
| 684 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 685 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 686 | EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 687 | if (sync == EGL_NO_SYNC_KHR) { |
| 688 | close(fenceFd); |
| 689 | ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError()); |
| 690 | return UNKNOWN_ERROR; |
| 691 | } |
| 692 | |
| 693 | // XXX: The spec draft is inconsistent as to whether this should |
| 694 | // return an EGLint or void. Ignore the return value for now, as |
| 695 | // it's not strictly needed. |
| 696 | eglWaitSyncKHR(mEglDisplay, sync, 0); |
| 697 | EGLint eglErr = eglGetError(); |
| 698 | eglDestroySyncKHR(mEglDisplay, sync); |
| 699 | if (eglErr != EGL_SUCCESS) { |
| 700 | ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr); |
| 701 | return UNKNOWN_ERROR; |
| 702 | } |
| 703 | } else { |
| 704 | // Block CPU on the fence. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 705 | status_t err = waitForeverOnFence(fence, "EglManager::fenceWait"); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 706 | if (err != NO_ERROR) { |
| 707 | ALOGE("EglManager::fenceWait: error waiting for fence: %d", err); |
| 708 | return err; |
| 709 | } |
| 710 | } |
| 711 | return OK; |
| 712 | } |
| 713 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 714 | status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) { |
| 715 | *nativeFence = -1; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 716 | if (!hasEglContext()) { |
| 717 | ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized"); |
| 718 | return INVALID_OPERATION; |
| 719 | } |
| 720 | |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 721 | if (EglExtensions.nativeFenceSync) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 722 | EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 723 | if (sync == EGL_NO_SYNC_KHR) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 724 | ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 725 | return UNKNOWN_ERROR; |
| 726 | } |
| 727 | glFlush(); |
| 728 | int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync); |
| 729 | eglDestroySyncKHR(mEglDisplay, sync); |
| 730 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 731 | ALOGE("EglManager::createReleaseFence: error dup'ing native fence " |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 732 | "fd: %#x", |
| 733 | eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 734 | return UNKNOWN_ERROR; |
| 735 | } |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 736 | *nativeFence = fenceFd; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 737 | *eglFence = EGL_NO_SYNC_KHR; |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 738 | } else if (useFenceSync && EglExtensions.fenceSync) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 739 | if (*eglFence != EGL_NO_SYNC_KHR) { |
| 740 | // There is already a fence for the current slot. We need to |
| 741 | // wait on that before replacing it with another fence to |
| 742 | // ensure that all outstanding buffer accesses have completed |
| 743 | // before the producer accesses it. |
| 744 | EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000); |
| 745 | if (result == EGL_FALSE) { |
| 746 | ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x", |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 747 | eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 748 | return UNKNOWN_ERROR; |
| 749 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 750 | ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence"); |
| 751 | return TIMED_OUT; |
| 752 | } |
| 753 | eglDestroySyncKHR(mEglDisplay, *eglFence); |
| 754 | } |
| 755 | |
| 756 | // Create a fence for the outstanding accesses in the current |
| 757 | // OpenGL ES context. |
| 758 | *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
| 759 | if (*eglFence == EGL_NO_SYNC_KHR) { |
| 760 | ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError()); |
| 761 | return UNKNOWN_ERROR; |
| 762 | } |
| 763 | glFlush(); |
| 764 | } |
| 765 | return OK; |
| 766 | } |
| 767 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 768 | } /* namespace renderthread */ |
| 769 | } /* namespace uirenderer */ |
| 770 | } /* namespace android */ |