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