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 | |
Alec Mouri | 49d87e5 | 2020-06-29 16:54:21 -0700 | [diff] [blame] | 241 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 242 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 243 | void EglManager::initExtensions() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 244 | auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
Alec Mouri | 49d87e5 | 2020-06-29 16:54:21 -0700 | [diff] [blame] | 245 | auto extensionsAndroid = |
| 246 | StringUtils::split(eglQueryStringImplementationANDROID(mEglDisplay, EGL_EXTENSIONS)); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 247 | |
John Reck | 8733bff | 2016-09-27 14:45:28 -0700 | [diff] [blame] | 248 | // For our purposes we don't care if EGL_BUFFER_AGE is a result of |
| 249 | // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered |
| 250 | // under EGL_KHR_partial_update and we don't need the expanded scope |
| 251 | // that EGL_EXT_buffer_age provides. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 252 | EglExtensions.bufferAge = |
| 253 | extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update"); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 254 | EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); |
John Reck | 708b668 | 2015-10-22 13:11:00 -0700 | [diff] [blame] | 255 | LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 256 | "Missing required extension EGL_KHR_swap_buffers_with_damage"); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 257 | |
| 258 | EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace"); |
| 259 | EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context"); |
| 260 | EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float"); |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 261 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb"); |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 262 | EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough"); |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 263 | EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq"); |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 264 | EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority"); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 265 | EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context"); |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 266 | EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync"); |
| 267 | EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync"); |
Alec Mouri | 49d87e5 | 2020-06-29 16:54:21 -0700 | [diff] [blame] | 268 | |
| 269 | // EGL_ANDROID_native_fence_sync is not exposed to applications, so access |
| 270 | // this through the private Android-specific query instead. |
| 271 | EglExtensions.nativeFenceSync = extensionsAndroid.has("EGL_ANDROID_native_fence_sync"); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 272 | } |
| 273 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 274 | bool EglManager::hasEglContext() { |
| 275 | return mEglDisplay != EGL_NO_DISPLAY; |
| 276 | } |
| 277 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 278 | void EglManager::loadConfigs() { |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 279 | // Note: The default pixel format is RGBA_8888, when other formats are |
| 280 | // available, we should check the target pixel format and configure the |
| 281 | // attributes list properly. |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 282 | mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior); |
| 283 | if (mEglConfig == EGL_NO_CONFIG_KHR) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 284 | if (mSwapBehavior == SwapBehavior::Preserved) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 285 | // Try again without dirty regions enabled |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 286 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 287 | mSwapBehavior = SwapBehavior::Discard; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 288 | mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 289 | } else { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 290 | // Failed to get a valid config |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 291 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 294 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 295 | // When we reach this point, we have a valid swap behavior |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 296 | if (EglExtensions.pixelFormatFloat) { |
| 297 | mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior); |
| 298 | if (mEglConfigF16 == EGL_NO_CONFIG_KHR) { |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 299 | ALOGE("Device claims wide gamut support, cannot find matching config, error = %s", |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 300 | eglErrorString()); |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 301 | EglExtensions.pixelFormatFloat = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 302 | } |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 303 | } |
| 304 | mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior); |
| 305 | if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) { |
| 306 | ALOGW("Failed to initialize 101010-2 format, error = %s", |
| 307 | eglErrorString()); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 308 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void EglManager::createContext() { |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 312 | std::vector<EGLint> contextAttributes; |
| 313 | contextAttributes.reserve(5); |
| 314 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 315 | contextAttributes.push_back(GLES_VERSION); |
| 316 | if (Properties::contextPriority != 0 && EglExtensions.contextPriority) { |
| 317 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 318 | contextAttributes.push_back(Properties::contextPriority); |
| 319 | } |
| 320 | contextAttributes.push_back(EGL_NONE); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 321 | mEglContext = eglCreateContext( |
| 322 | mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig, |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 323 | EGL_NO_CONTEXT, contextAttributes.data()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 324 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s", |
| 325 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 326 | } |
| 327 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 328 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 329 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 330 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 331 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 332 | if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 333 | EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 334 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
Roman Kiryanov | 6073613 | 2019-08-02 14:37:53 -0700 | [diff] [blame] | 335 | LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE, |
| 336 | "Failed to create a pixel buffer display=%p, " |
| 337 | "mEglConfig=%p, error=%s", |
| 338 | mEglDisplay, mEglConfig, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 339 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 342 | Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window, |
| 343 | ColorMode colorMode, |
Brian Osman | e0cf597 | 2019-01-23 10:41:20 -0500 | [diff] [blame] | 344 | sk_sp<SkColorSpace> colorSpace) { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 345 | LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized"); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 346 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 347 | if (!mHasWideColorGamutSupport || !EglExtensions.noConfigContext) { |
| 348 | colorMode = ColorMode::Default; |
| 349 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 350 | |
| 351 | // The color space we want to use depends on whether linear blending is turned |
| 352 | // on and whether the app has requested wide color gamut rendering. When wide |
| 353 | // color gamut rendering is off, the app simply renders in the display's native |
| 354 | // color gamut. |
| 355 | // |
| 356 | // When wide gamut rendering is off: |
| 357 | // - Blending is done by default in gamma space, which requires using a |
| 358 | // linear EGL color space (the GPU uses the color values as is) |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 359 | // - If linear blending is on, we must use the non-linear EGL color space |
| 360 | // (the GPU will perform sRGB to linear and linear to SRGB conversions |
| 361 | // before and after blending) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 362 | // |
| 363 | // When wide gamut rendering is on we cannot rely on the GPU performing |
| 364 | // linear blending for us. We use two different color spaces to tag the |
| 365 | // surface appropriately for SurfaceFlinger: |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 366 | // - Gamma blending (default) requires the use of the non-linear color space |
| 367 | // - Linear blending requires the use of the linear color space |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 368 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 369 | // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 370 | // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value. |
| 371 | // According to section 3.4.1 of the EGL specification, the attributes |
| 372 | // list is considered empty if the first entry is EGL_NONE |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 373 | EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE}; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 374 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 375 | EGLConfig config = mEglConfig; |
| 376 | if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) { |
| 377 | if (mEglConfigF16 == EGL_NO_CONFIG_KHR) { |
| 378 | colorMode = ColorMode::Default; |
| 379 | } else { |
| 380 | config = mEglConfigF16; |
| 381 | } |
| 382 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 383 | if (EglExtensions.glColorSpace) { |
| 384 | attribs[0] = EGL_GL_COLORSPACE_KHR; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 385 | switch (colorMode) { |
| 386 | case ColorMode::Default: |
| 387 | attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR; |
| 388 | break; |
| 389 | case ColorMode::WideColorGamut: { |
| 390 | skcms_Matrix3x3 colorGamut; |
| 391 | LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut), |
| 392 | "Could not get gamut matrix from color space"); |
| 393 | if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) { |
| 394 | attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT; |
| 395 | } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) { |
| 396 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT; |
| 397 | } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == 0) { |
| 398 | attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT; |
| 399 | } else { |
| 400 | LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space."); |
| 401 | } |
| 402 | break; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 403 | } |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 404 | case ColorMode::Hdr: |
| 405 | config = mEglConfigF16; |
| 406 | attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT; |
| 407 | break; |
| 408 | case ColorMode::Hdr10: |
| 409 | config = mEglConfig1010102; |
| 410 | attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT; |
| 411 | break; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 412 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 413 | } |
| 414 | |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame^] | 415 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs); |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 416 | if (surface == EGL_NO_SURFACE) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 417 | return Error<EGLint>{eglGetError()}; |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 418 | } |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 419 | |
| 420 | if (mSwapBehavior != SwapBehavior::Preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 421 | LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 422 | EGL_BUFFER_DESTROYED) == EGL_FALSE, |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 423 | "Failed to set swap behavior to destroyed for window %p, eglErr = %s", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 424 | (void*)window, eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 425 | } |
| 426 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 427 | return surface; |
| 428 | } |
| 429 | |
| 430 | void EglManager::destroySurface(EGLSurface surface) { |
| 431 | if (isCurrent(surface)) { |
| 432 | makeCurrent(EGL_NO_SURFACE); |
| 433 | } |
| 434 | if (!eglDestroySurface(mEglDisplay, surface)) { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 435 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | void EglManager::destroy() { |
| 440 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 441 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 442 | eglDestroyContext(mEglDisplay, mEglContext); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 443 | if (mPBufferSurface != EGL_NO_SURFACE) { |
| 444 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 445 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 446 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 447 | eglTerminate(mEglDisplay); |
| 448 | eglReleaseThread(); |
| 449 | |
| 450 | mEglDisplay = EGL_NO_DISPLAY; |
| 451 | mEglContext = EGL_NO_CONTEXT; |
| 452 | mPBufferSurface = EGL_NO_SURFACE; |
| 453 | mCurrentSurface = EGL_NO_SURFACE; |
| 454 | } |
| 455 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 456 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) { |
| 457 | if (!force && isCurrent(surface)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 458 | |
| 459 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 460 | // Ensure we always have a valid surface & context |
| 461 | surface = mPBufferSurface; |
| 462 | } |
| 463 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 464 | if (errOut) { |
| 465 | *errOut = eglGetError(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 466 | ALOGW("Failed to make current on surface %p, error=%s", (void*)surface, |
| 467 | egl_error_str(*errOut)); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 468 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 469 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface, |
| 470 | eglErrorString()); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 471 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 472 | } |
| 473 | mCurrentSurface = surface; |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 474 | if (Properties::disableVsync) { |
| 475 | eglSwapInterval(mEglDisplay, 0); |
| 476 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 477 | return true; |
| 478 | } |
| 479 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 480 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 481 | switch (mSwapBehavior) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 482 | case SwapBehavior::Discard: |
| 483 | return 0; |
| 484 | case SwapBehavior::Preserved: |
| 485 | return 1; |
| 486 | case SwapBehavior::BufferAge: |
| 487 | EGLint bufferAge; |
| 488 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 489 | return bufferAge; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 495 | 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] | 496 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 497 | Frame frame; |
| 498 | frame.mSurface = surface; |
| 499 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 500 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 501 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 502 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 503 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 504 | } |
| 505 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 506 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 507 | #ifdef EGL_KHR_partial_update |
| 508 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 509 | EGLint rects[4]; |
| 510 | frame.map(dirty, rects); |
| 511 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 512 | 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] | 513 | (void*)frame.mSurface, eglErrorString()); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | #endif |
| 517 | } |
| 518 | |
John Reck | c96955d | 2016-02-26 14:56:44 -0800 | [diff] [blame] | 519 | bool EglManager::damageRequiresSwap() { |
| 520 | return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; |
| 521 | } |
| 522 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 523 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 524 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 525 | ATRACE_NAME("Finishing GPU work"); |
| 526 | fence(); |
| 527 | } |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 528 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 529 | EGLint rects[4]; |
| 530 | frame.map(screenDirty, rects); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 531 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 532 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 533 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 534 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 535 | return true; |
| 536 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 537 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 538 | // For some reason our surface was destroyed out from under us |
| 539 | // This really shouldn't happen, but if it does we can recover easily |
| 540 | // by just not trying to use the surface anymore |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 541 | ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err, |
| 542 | frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 543 | return false; |
| 544 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 545 | 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] | 546 | // Impossible to hit this, but the compiler doesn't know that |
| 547 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 548 | } |
| 549 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 550 | void EglManager::fence() { |
| 551 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 552 | eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 553 | eglDestroySyncKHR(mEglDisplay, fence); |
| 554 | } |
| 555 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 556 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 557 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 558 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 559 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 560 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 561 | if (!preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 562 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface, |
| 563 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 564 | // Maybe it's already set? |
| 565 | EGLint swapBehavior; |
| 566 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 567 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 568 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 569 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface, |
| 570 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 571 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 572 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 573 | |
| 574 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 577 | static status_t waitForeverOnFence(int fence, const char* logname) { |
| 578 | ATRACE_CALL(); |
| 579 | if (fence == -1) { |
| 580 | return NO_ERROR; |
| 581 | } |
| 582 | constexpr int warningTimeout = 3000; |
| 583 | int err = sync_wait(fence, warningTimeout); |
| 584 | if (err < 0 && errno == ETIME) { |
| 585 | ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout); |
| 586 | err = sync_wait(fence, -1); |
| 587 | } |
| 588 | return err < 0 ? -errno : status_t(NO_ERROR); |
| 589 | } |
| 590 | |
| 591 | status_t EglManager::fenceWait(int fence) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 592 | if (!hasEglContext()) { |
| 593 | ALOGE("EglManager::fenceWait: EGLDisplay not initialized"); |
| 594 | return INVALID_OPERATION; |
| 595 | } |
| 596 | |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 597 | if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 598 | // Block GPU on the fence. |
| 599 | // Create an EGLSyncKHR from the current fence. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 600 | int fenceFd = ::dup(fence); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 601 | if (fenceFd == -1) { |
| 602 | ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno); |
| 603 | return -errno; |
| 604 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 605 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 606 | EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 607 | if (sync == EGL_NO_SYNC_KHR) { |
| 608 | close(fenceFd); |
| 609 | ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError()); |
| 610 | return UNKNOWN_ERROR; |
| 611 | } |
| 612 | |
| 613 | // XXX: The spec draft is inconsistent as to whether this should |
| 614 | // return an EGLint or void. Ignore the return value for now, as |
| 615 | // it's not strictly needed. |
| 616 | eglWaitSyncKHR(mEglDisplay, sync, 0); |
| 617 | EGLint eglErr = eglGetError(); |
| 618 | eglDestroySyncKHR(mEglDisplay, sync); |
| 619 | if (eglErr != EGL_SUCCESS) { |
| 620 | ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr); |
| 621 | return UNKNOWN_ERROR; |
| 622 | } |
| 623 | } else { |
| 624 | // Block CPU on the fence. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 625 | status_t err = waitForeverOnFence(fence, "EglManager::fenceWait"); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 626 | if (err != NO_ERROR) { |
| 627 | ALOGE("EglManager::fenceWait: error waiting for fence: %d", err); |
| 628 | return err; |
| 629 | } |
| 630 | } |
| 631 | return OK; |
| 632 | } |
| 633 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 634 | status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) { |
| 635 | *nativeFence = -1; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 636 | if (!hasEglContext()) { |
| 637 | ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized"); |
| 638 | return INVALID_OPERATION; |
| 639 | } |
| 640 | |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 641 | if (EglExtensions.nativeFenceSync) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 642 | EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 643 | if (sync == EGL_NO_SYNC_KHR) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 644 | ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 645 | return UNKNOWN_ERROR; |
| 646 | } |
| 647 | glFlush(); |
| 648 | int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync); |
| 649 | eglDestroySyncKHR(mEglDisplay, sync); |
| 650 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 651 | ALOGE("EglManager::createReleaseFence: error dup'ing native fence " |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 652 | "fd: %#x", |
| 653 | eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 654 | return UNKNOWN_ERROR; |
| 655 | } |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 656 | *nativeFence = fenceFd; |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 657 | *eglFence = EGL_NO_SYNC_KHR; |
Alec Mouri | 680414e | 2020-01-28 09:22:33 -0800 | [diff] [blame] | 658 | } else if (useFenceSync && EglExtensions.fenceSync) { |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 659 | if (*eglFence != EGL_NO_SYNC_KHR) { |
| 660 | // There is already a fence for the current slot. We need to |
| 661 | // wait on that before replacing it with another fence to |
| 662 | // ensure that all outstanding buffer accesses have completed |
| 663 | // before the producer accesses it. |
| 664 | EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000); |
| 665 | if (result == EGL_FALSE) { |
| 666 | ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x", |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 667 | eglGetError()); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 668 | return UNKNOWN_ERROR; |
| 669 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 670 | ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence"); |
| 671 | return TIMED_OUT; |
| 672 | } |
| 673 | eglDestroySyncKHR(mEglDisplay, *eglFence); |
| 674 | } |
| 675 | |
| 676 | // Create a fence for the outstanding accesses in the current |
| 677 | // OpenGL ES context. |
| 678 | *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
| 679 | if (*eglFence == EGL_NO_SYNC_KHR) { |
| 680 | ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError()); |
| 681 | return UNKNOWN_ERROR; |
| 682 | } |
| 683 | glFlush(); |
| 684 | } |
| 685 | return OK; |
| 686 | } |
| 687 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 688 | } /* namespace renderthread */ |
| 689 | } /* namespace uirenderer */ |
| 690 | } /* namespace android */ |