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