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