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