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