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