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 <string> |
| 20 | |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
| 22 | #include <log/log.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | #include "utils/StringUtils.h" |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 24 | |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 25 | #include "Caches.h" |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 26 | #include "DeviceInfo.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 27 | #include "Frame.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 28 | #include "Properties.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 29 | #include "RenderThread.h" |
Mark Salyzyn | 173215d | 2017-01-12 08:27:11 -0800 | [diff] [blame] | 30 | #include "Texture.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 31 | #include "renderstate/RenderState.h" |
Mark Salyzyn | 173215d | 2017-01-12 08:27:11 -0800 | [diff] [blame] | 32 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 33 | #include <EGL/eglext.h> |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 34 | #include <GrContextOptions.h> |
| 35 | #include <gl/GrGLInterface.h> |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 36 | |
Derek Sollenberger | 7e044fe | 2016-11-07 10:57:59 -0500 | [diff] [blame] | 37 | #ifdef HWUI_GLES_WRAP_ENABLED |
| 38 | #include "debug/GlesDriver.h" |
| 39 | #endif |
| 40 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 41 | #define GLES_VERSION 2 |
| 42 | |
| 43 | // Android-specific addition that is used to show when frames began in systrace |
| 44 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 45 | |
| 46 | namespace android { |
| 47 | namespace uirenderer { |
| 48 | namespace renderthread { |
| 49 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | #define ERROR_CASE(x) \ |
| 51 | case x: \ |
| 52 | return #x; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 53 | static const char* egl_error_str(EGLint error) { |
| 54 | switch (error) { |
| 55 | ERROR_CASE(EGL_SUCCESS) |
| 56 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 57 | ERROR_CASE(EGL_BAD_ACCESS) |
| 58 | ERROR_CASE(EGL_BAD_ALLOC) |
| 59 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 60 | ERROR_CASE(EGL_BAD_CONFIG) |
| 61 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 62 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 63 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 64 | ERROR_CASE(EGL_BAD_MATCH) |
| 65 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 66 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 67 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 68 | ERROR_CASE(EGL_BAD_SURFACE) |
| 69 | ERROR_CASE(EGL_CONTEXT_LOST) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 70 | default: |
| 71 | return "Unknown error"; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 74 | const char* EglManager::eglErrorString() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 75 | return egl_error_str(eglGetError()); |
| 76 | } |
| 77 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 78 | static struct { |
| 79 | bool bufferAge = false; |
| 80 | bool setDamage = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 81 | bool noConfigContext = false; |
| 82 | bool pixelFormatFloat = false; |
| 83 | bool glColorSpace = false; |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 84 | bool scRGB = false; |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 85 | bool contextPriority = false; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 86 | } EglExtensions; |
| 87 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 88 | EglManager::EglManager(RenderThread& thread) |
| 89 | : mRenderThread(thread) |
| 90 | , mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 91 | , mEglConfig(nullptr) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 92 | , mEglConfigWideGamut(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 93 | , mEglContext(EGL_NO_CONTEXT) |
| 94 | , mPBufferSurface(EGL_NO_SURFACE) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | , mCurrentSurface(EGL_NO_SURFACE) {} |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 96 | |
| 97 | void EglManager::initialize() { |
| 98 | if (hasEglContext()) return; |
| 99 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 100 | ATRACE_NAME("Creating EGLContext"); |
| 101 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 102 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s", |
| 104 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 105 | |
| 106 | EGLint major, minor; |
| 107 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 108 | "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 109 | |
| 110 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 111 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 112 | initExtensions(); |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 113 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 114 | // Now that extensions are loaded, pick a swap behavior |
| 115 | if (Properties::enablePartialUpdates) { |
Matt Sarett | b77c94a | 2016-12-07 12:22:37 -0500 | [diff] [blame] | 116 | // An Adreno driver bug is causing rendering problems for SkiaGL with |
| 117 | // buffer age swap behavior (b/31957043). To temporarily workaround, |
| 118 | // we will use preserved swap behavior. |
Derek Sollenberger | b5a12bd | 2017-06-14 15:23:19 -0400 | [diff] [blame] | 119 | if (Properties::useBufferAge && EglExtensions.bufferAge) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 120 | mSwapBehavior = SwapBehavior::BufferAge; |
| 121 | } else { |
| 122 | mSwapBehavior = SwapBehavior::Preserved; |
| 123 | } |
| 124 | } |
| 125 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 126 | loadConfigs(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 127 | createContext(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 128 | createPBufferSurface(); |
| 129 | makeCurrent(mPBufferSurface); |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 130 | DeviceInfo::initialize(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 131 | mRenderThread.renderState().onGLContextCreated(); |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 132 | |
| 133 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
Derek Sollenberger | 7e044fe | 2016-11-07 10:57:59 -0500 | [diff] [blame] | 134 | #ifdef HWUI_GLES_WRAP_ENABLED |
| 135 | debug::GlesDriver* driver = debug::GlesDriver::get(); |
| 136 | sk_sp<const GrGLInterface> glInterface(driver->getSkiaInterface()); |
| 137 | #else |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 138 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); |
Derek Sollenberger | 7e044fe | 2016-11-07 10:57:59 -0500 | [diff] [blame] | 139 | #endif |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 140 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
| 141 | |
| 142 | GrContextOptions options; |
Brian Osman | da6ad83 | 2017-08-29 16:43:43 -0400 | [diff] [blame] | 143 | options.fDisableDistanceFieldPaths = true; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 144 | mRenderThread.cacheManager().configureContext(&options); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 145 | sk_sp<GrContext> grContext(GrContext::MakeGL(std::move(glInterface), options)); |
| 146 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 147 | mRenderThread.setGrContext(grContext); |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 148 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 151 | void EglManager::initExtensions() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 152 | auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 153 | |
John Reck | 8733bff | 2016-09-27 14:45:28 -0700 | [diff] [blame] | 154 | // For our purposes we don't care if EGL_BUFFER_AGE is a result of |
| 155 | // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered |
| 156 | // under EGL_KHR_partial_update and we don't need the expanded scope |
| 157 | // that EGL_EXT_buffer_age provides. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | EglExtensions.bufferAge = |
| 159 | extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update"); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 160 | EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); |
John Reck | 708b668 | 2015-10-22 13:11:00 -0700 | [diff] [blame] | 161 | LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | "Missing required extension EGL_KHR_swap_buffers_with_damage"); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 163 | |
| 164 | EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace"); |
| 165 | EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context"); |
| 166 | EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float"); |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 167 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 168 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear"); |
| 169 | #else |
| 170 | EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb"); |
| 171 | #endif |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 172 | EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority"); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 175 | bool EglManager::hasEglContext() { |
| 176 | return mEglDisplay != EGL_NO_DISPLAY; |
| 177 | } |
| 178 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 179 | void EglManager::loadConfigs() { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 180 | ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior)); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | EGLint swapBehavior = |
| 182 | (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 183 | EGLint attribs[] = {EGL_RENDERABLE_TYPE, |
| 184 | EGL_OPENGL_ES2_BIT, |
| 185 | EGL_RED_SIZE, |
| 186 | 8, |
| 187 | EGL_GREEN_SIZE, |
| 188 | 8, |
| 189 | EGL_BLUE_SIZE, |
| 190 | 8, |
| 191 | EGL_ALPHA_SIZE, |
| 192 | 8, |
| 193 | EGL_DEPTH_SIZE, |
| 194 | 0, |
| 195 | EGL_CONFIG_CAVEAT, |
| 196 | EGL_NONE, |
| 197 | EGL_STENCIL_SIZE, |
| 198 | Stencil::getStencilSize(), |
| 199 | EGL_SURFACE_TYPE, |
| 200 | EGL_WINDOW_BIT | swapBehavior, |
| 201 | EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 202 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 203 | EGLint numConfigs = 1; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 204 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) || |
| 205 | numConfigs != 1) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 206 | if (mSwapBehavior == SwapBehavior::Preserved) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 207 | // Try again without dirty regions enabled |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 208 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 209 | mSwapBehavior = SwapBehavior::Discard; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 210 | loadConfigs(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 211 | 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] | 212 | } else { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 213 | // Failed to get a valid config |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 214 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 217 | |
| 218 | if (EglExtensions.pixelFormatFloat) { |
| 219 | // If we reached this point, we have a valid swap behavior |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 220 | EGLint attribs16F[] = {EGL_RENDERABLE_TYPE, |
| 221 | EGL_OPENGL_ES2_BIT, |
| 222 | EGL_COLOR_COMPONENT_TYPE_EXT, |
| 223 | EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT, |
| 224 | EGL_RED_SIZE, |
| 225 | 16, |
| 226 | EGL_GREEN_SIZE, |
| 227 | 16, |
| 228 | EGL_BLUE_SIZE, |
| 229 | 16, |
| 230 | EGL_ALPHA_SIZE, |
| 231 | 16, |
| 232 | EGL_DEPTH_SIZE, |
| 233 | 0, |
| 234 | EGL_STENCIL_SIZE, |
| 235 | Stencil::getStencilSize(), |
| 236 | EGL_SURFACE_TYPE, |
| 237 | EGL_WINDOW_BIT | swapBehavior, |
| 238 | EGL_NONE}; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 239 | |
| 240 | numConfigs = 1; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 241 | if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs, |
| 242 | &numConfigs) || |
| 243 | numConfigs != 1) { |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 244 | ALOGE("Device claims wide gamut support, cannot find matching config, error = %s", |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 245 | eglErrorString()); |
Rob Herring | 74883ab | 2017-11-29 09:26:31 -0600 | [diff] [blame] | 246 | EglExtensions.pixelFormatFloat = false; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void EglManager::createContext() { |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 252 | std::vector<EGLint> contextAttributes; |
| 253 | contextAttributes.reserve(5); |
| 254 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 255 | contextAttributes.push_back(GLES_VERSION); |
| 256 | if (Properties::contextPriority != 0 && EglExtensions.contextPriority) { |
| 257 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 258 | contextAttributes.push_back(Properties::contextPriority); |
| 259 | } |
| 260 | contextAttributes.push_back(EGL_NONE); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 261 | mEglContext = eglCreateContext( |
| 262 | mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig, |
Jorim Jaggi | 767e25e | 2018-04-04 23:07:35 +0200 | [diff] [blame] | 263 | EGL_NO_CONTEXT, contextAttributes.data()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 264 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s", |
| 265 | eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 266 | } |
| 267 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 268 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 269 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 270 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 271 | |
| 272 | if (mPBufferSurface == EGL_NO_SURFACE) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 274 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 275 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 278 | EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 279 | initialize(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 280 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 281 | wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB && |
| 282 | EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 283 | |
| 284 | // The color space we want to use depends on whether linear blending is turned |
| 285 | // on and whether the app has requested wide color gamut rendering. When wide |
| 286 | // color gamut rendering is off, the app simply renders in the display's native |
| 287 | // color gamut. |
| 288 | // |
| 289 | // When wide gamut rendering is off: |
| 290 | // - Blending is done by default in gamma space, which requires using a |
| 291 | // linear EGL color space (the GPU uses the color values as is) |
| 292 | // - If linear blending is on, we must use the sRGB EGL color space (the |
| 293 | // GPU will perform sRGB to linear and linear to SRGB conversions before |
| 294 | // and after blending) |
| 295 | // |
| 296 | // When wide gamut rendering is on we cannot rely on the GPU performing |
| 297 | // linear blending for us. We use two different color spaces to tag the |
| 298 | // surface appropriately for SurfaceFlinger: |
| 299 | // - Gamma blending (default) requires the use of the scRGB-nl color space |
| 300 | // - Linear blending requires the use of the scRGB color space |
| 301 | |
| 302 | // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension |
| 303 | // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value. |
| 304 | // According to section 3.4.1 of the EGL specification, the attributes |
| 305 | // list is considered empty if the first entry is EGL_NONE |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 306 | EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE}; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 307 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 308 | if (EglExtensions.glColorSpace) { |
| 309 | attribs[0] = EGL_GL_COLORSPACE_KHR; |
| 310 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 311 | if (wideColorGamut) { |
| 312 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT; |
| 313 | } else { |
| 314 | attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR; |
| 315 | } |
| 316 | #else |
| 317 | if (wideColorGamut) { |
Romain Guy | 26b6a64 | 2017-07-11 09:48:28 -0700 | [diff] [blame] | 318 | attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 319 | } else { |
| 320 | attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR; |
| 321 | } |
| 322 | #endif |
| 323 | } |
| 324 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 325 | EGLSurface surface = eglCreateWindowSurface( |
| 326 | mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 327 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 328 | "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window, |
| 329 | eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 330 | |
| 331 | if (mSwapBehavior != SwapBehavior::Preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 332 | LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 333 | EGL_BUFFER_DESTROYED) == EGL_FALSE, |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 334 | "Failed to set swap behavior to destroyed for window %p, eglErr = %s", |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 335 | (void*)window, eglErrorString()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 336 | } |
| 337 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 338 | return surface; |
| 339 | } |
| 340 | |
| 341 | void EglManager::destroySurface(EGLSurface surface) { |
| 342 | if (isCurrent(surface)) { |
| 343 | makeCurrent(EGL_NO_SURFACE); |
| 344 | } |
| 345 | if (!eglDestroySurface(mEglDisplay, surface)) { |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 346 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | void EglManager::destroy() { |
| 351 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 352 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 353 | mRenderThread.setGrContext(nullptr); |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 354 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 355 | eglDestroyContext(mEglDisplay, mEglContext); |
| 356 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 357 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 358 | eglTerminate(mEglDisplay); |
| 359 | eglReleaseThread(); |
| 360 | |
| 361 | mEglDisplay = EGL_NO_DISPLAY; |
| 362 | mEglContext = EGL_NO_CONTEXT; |
| 363 | mPBufferSurface = EGL_NO_SURFACE; |
| 364 | mCurrentSurface = EGL_NO_SURFACE; |
| 365 | } |
| 366 | |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 367 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 368 | if (isCurrent(surface)) return false; |
| 369 | |
| 370 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 371 | // Ensure we always have a valid surface & context |
| 372 | surface = mPBufferSurface; |
| 373 | } |
| 374 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 375 | if (errOut) { |
| 376 | *errOut = eglGetError(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 377 | ALOGW("Failed to make current on surface %p, error=%s", (void*)surface, |
| 378 | egl_error_str(*errOut)); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 379 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 380 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface, |
| 381 | eglErrorString()); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 382 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 383 | } |
| 384 | mCurrentSurface = surface; |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 385 | if (Properties::disableVsync) { |
| 386 | eglSwapInterval(mEglDisplay, 0); |
| 387 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 388 | return true; |
| 389 | } |
| 390 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 391 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 392 | switch (mSwapBehavior) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 393 | case SwapBehavior::Discard: |
| 394 | return 0; |
| 395 | case SwapBehavior::Preserved: |
| 396 | return 1; |
| 397 | case SwapBehavior::BufferAge: |
| 398 | EGLint bufferAge; |
| 399 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 400 | return bufferAge; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 406 | 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] | 407 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 408 | Frame frame; |
| 409 | frame.mSurface = surface; |
| 410 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 411 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 412 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 413 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 414 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 415 | } |
| 416 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 417 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 418 | #ifdef EGL_KHR_partial_update |
| 419 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 420 | EGLint rects[4]; |
| 421 | frame.map(dirty, rects); |
| 422 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 423 | 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] | 424 | (void*)frame.mSurface, eglErrorString()); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | #endif |
| 428 | } |
| 429 | |
John Reck | c96955d | 2016-02-26 14:56:44 -0800 | [diff] [blame] | 430 | bool EglManager::damageRequiresSwap() { |
| 431 | return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; |
| 432 | } |
| 433 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 434 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 435 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 436 | ATRACE_NAME("Finishing GPU work"); |
| 437 | fence(); |
| 438 | } |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 439 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 440 | EGLint rects[4]; |
| 441 | frame.map(screenDirty, rects); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 442 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 443 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 444 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 445 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 446 | return true; |
| 447 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 448 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 449 | // For some reason our surface was destroyed out from under us |
| 450 | // This really shouldn't happen, but if it does we can recover easily |
| 451 | // by just not trying to use the surface anymore |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 452 | ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err, |
| 453 | frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 454 | return false; |
| 455 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 456 | 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] | 457 | // Impossible to hit this, but the compiler doesn't know that |
| 458 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 459 | } |
| 460 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 461 | void EglManager::fence() { |
| 462 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 463 | eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 464 | eglDestroySyncKHR(mEglDisplay, fence); |
| 465 | } |
| 466 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 467 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 468 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 469 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 470 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 471 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 472 | if (!preserved) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 473 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface, |
| 474 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 475 | // Maybe it's already set? |
| 476 | EGLint swapBehavior; |
| 477 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 478 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 479 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 480 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface, |
| 481 | eglErrorString()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 482 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 483 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 484 | |
| 485 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 486 | } |
| 487 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 488 | } /* namespace renderthread */ |
| 489 | } /* namespace uirenderer */ |
| 490 | } /* namespace android */ |