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