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 | |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 19 | #include "Caches.h" |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 20 | #include "DeviceInfo.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 22 | #include "RenderThread.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 23 | #include "renderstate/RenderState.h" |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 24 | #include "utils/StringUtils.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 25 | #include <cutils/log.h> |
| 26 | #include <cutils/properties.h> |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 27 | #include <EGL/eglext.h> |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 30 | #define GLES_VERSION 2 |
| 31 | |
| 32 | // Android-specific addition that is used to show when frames began in systrace |
| 33 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | namespace renderthread { |
| 38 | |
| 39 | #define ERROR_CASE(x) case x: return #x; |
| 40 | static const char* egl_error_str(EGLint error) { |
| 41 | switch (error) { |
| 42 | ERROR_CASE(EGL_SUCCESS) |
| 43 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 44 | ERROR_CASE(EGL_BAD_ACCESS) |
| 45 | ERROR_CASE(EGL_BAD_ALLOC) |
| 46 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 47 | ERROR_CASE(EGL_BAD_CONFIG) |
| 48 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 49 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 50 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 51 | ERROR_CASE(EGL_BAD_MATCH) |
| 52 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 53 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 54 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 55 | ERROR_CASE(EGL_BAD_SURFACE) |
| 56 | ERROR_CASE(EGL_CONTEXT_LOST) |
| 57 | default: |
| 58 | return "Unknown error"; |
| 59 | } |
| 60 | } |
| 61 | static const char* egl_error_str() { |
| 62 | return egl_error_str(eglGetError()); |
| 63 | } |
| 64 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 65 | static struct { |
| 66 | bool bufferAge = false; |
| 67 | bool setDamage = false; |
| 68 | } EglExtensions; |
| 69 | |
| 70 | void Frame::map(const SkRect& in, EGLint* out) const { |
| 71 | /* The rectangles are specified relative to the bottom-left of the surface |
| 72 | * and the x and y components of each rectangle specify the bottom-left |
| 73 | * position of that rectangle. |
| 74 | * |
| 75 | * HWUI does everything with 0,0 being top-left, so need to map |
| 76 | * the rect |
| 77 | */ |
| 78 | SkIRect idirty; |
| 79 | in.roundOut(&idirty); |
| 80 | EGLint y = mHeight - (idirty.y() + idirty.height()); |
| 81 | // layout: {x, y, width, height} |
| 82 | out[0] = idirty.x(); |
| 83 | out[1] = y; |
| 84 | out[2] = idirty.width(); |
| 85 | out[3] = idirty.height(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 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) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 92 | , mEglContext(EGL_NO_CONTEXT) |
| 93 | , mPBufferSurface(EGL_NO_SURFACE) |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame^] | 94 | , mCurrentSurface(EGL_NO_SURFACE) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 95 | } |
| 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); |
| 103 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 104 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
| 105 | |
| 106 | EGLint major, minor; |
| 107 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 108 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 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) { |
| 116 | if (Properties::useBufferAge && EglExtensions.bufferAge) { |
| 117 | mSwapBehavior = SwapBehavior::BufferAge; |
| 118 | } else { |
| 119 | mSwapBehavior = SwapBehavior::Preserved; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | loadConfig(); |
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(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 129 | } |
| 130 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 131 | void EglManager::initExtensions() { |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 132 | auto extensions = StringUtils::split( |
| 133 | eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
John Reck | 8733bff | 2016-09-27 14:45:28 -0700 | [diff] [blame] | 134 | // For our purposes we don't care if EGL_BUFFER_AGE is a result of |
| 135 | // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered |
| 136 | // under EGL_KHR_partial_update and we don't need the expanded scope |
| 137 | // that EGL_EXT_buffer_age provides. |
| 138 | EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age") |
| 139 | || extensions.has("EGL_KHR_partial_update"); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 140 | EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); |
John Reck | 708b668 | 2015-10-22 13:11:00 -0700 | [diff] [blame] | 141 | LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), |
| 142 | "Missing required extension EGL_KHR_swap_buffers_with_damage"); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 143 | } |
| 144 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 145 | bool EglManager::hasEglContext() { |
| 146 | return mEglDisplay != EGL_NO_DISPLAY; |
| 147 | } |
| 148 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 149 | void EglManager::loadConfig() { |
| 150 | ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior)); |
| 151 | EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved) |
| 152 | ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 153 | EGLint attribs[] = { |
| 154 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 155 | EGL_RED_SIZE, 8, |
| 156 | EGL_GREEN_SIZE, 8, |
| 157 | EGL_BLUE_SIZE, 8, |
| 158 | EGL_ALPHA_SIZE, 8, |
| 159 | EGL_DEPTH_SIZE, 0, |
| 160 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 161 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 162 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 163 | EGL_NONE |
| 164 | }; |
| 165 | |
| 166 | EGLint num_configs = 1; |
| 167 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 168 | || num_configs != 1) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 169 | if (mSwapBehavior == SwapBehavior::Preserved) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 170 | // Try again without dirty regions enabled |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 171 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 172 | mSwapBehavior = SwapBehavior::Discard; |
| 173 | loadConfig(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 174 | } else { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 175 | // Failed to get a valid config |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 176 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void EglManager::createContext() { |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 182 | EGLint attribs[] = { |
| 183 | EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, |
| 184 | EGL_NONE |
| 185 | }; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 186 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
| 187 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 188 | "Failed to create context, error = %s", egl_error_str()); |
| 189 | } |
| 190 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 191 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 192 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 193 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
| 194 | |
| 195 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 196 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 197 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 198 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | EGLSurface EglManager::createSurface(EGLNativeWindowType window) { |
| 202 | initialize(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame^] | 203 | |
| 204 | EGLint attribs[] = { |
| 205 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 206 | EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR, |
| 207 | EGL_COLORSPACE, EGL_COLORSPACE_sRGB, |
| 208 | #endif |
| 209 | EGL_NONE |
| 210 | }; |
| 211 | |
| 212 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, attribs); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 213 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 214 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 215 | (void*) window, egl_error_str()); |
Christian Poetzsch | 9a878a6 | 2016-02-05 14:22:01 +0000 | [diff] [blame] | 216 | |
| 217 | if (mSwapBehavior != SwapBehavior::Preserved) { |
| 218 | LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE, |
| 219 | "Failed to set swap behavior to destroyed for window %p, eglErr = %s", |
| 220 | (void*) window, egl_error_str()); |
| 221 | } |
| 222 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 223 | return surface; |
| 224 | } |
| 225 | |
| 226 | void EglManager::destroySurface(EGLSurface surface) { |
| 227 | if (isCurrent(surface)) { |
| 228 | makeCurrent(EGL_NO_SURFACE); |
| 229 | } |
| 230 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 231 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void EglManager::destroy() { |
| 236 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 237 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 238 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 239 | eglDestroyContext(mEglDisplay, mEglContext); |
| 240 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 241 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 242 | eglTerminate(mEglDisplay); |
| 243 | eglReleaseThread(); |
| 244 | |
| 245 | mEglDisplay = EGL_NO_DISPLAY; |
| 246 | mEglContext = EGL_NO_CONTEXT; |
| 247 | mPBufferSurface = EGL_NO_SURFACE; |
| 248 | mCurrentSurface = EGL_NO_SURFACE; |
| 249 | } |
| 250 | |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 251 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 252 | if (isCurrent(surface)) return false; |
| 253 | |
| 254 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 255 | // Ensure we always have a valid surface & context |
| 256 | surface = mPBufferSurface; |
| 257 | } |
| 258 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 259 | if (errOut) { |
| 260 | *errOut = eglGetError(); |
| 261 | ALOGW("Failed to make current on surface %p, error=%s", |
| 262 | (void*)surface, egl_error_str(*errOut)); |
| 263 | } else { |
| 264 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 265 | (void*)surface, egl_error_str()); |
| 266 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 267 | } |
| 268 | mCurrentSurface = surface; |
| 269 | return true; |
| 270 | } |
| 271 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 272 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 273 | switch (mSwapBehavior) { |
| 274 | case SwapBehavior::Discard: |
| 275 | return 0; |
| 276 | case SwapBehavior::Preserved: |
| 277 | return 1; |
| 278 | case SwapBehavior::BufferAge: |
| 279 | EGLint bufferAge; |
| 280 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 281 | return bufferAge; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 287 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 288 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 289 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 290 | Frame frame; |
| 291 | frame.mSurface = surface; |
| 292 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 293 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 294 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 295 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 296 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 297 | } |
| 298 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 299 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 300 | #ifdef EGL_KHR_partial_update |
| 301 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 302 | EGLint rects[4]; |
| 303 | frame.map(dirty, rects); |
| 304 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 305 | LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s", |
| 306 | (void*)frame.mSurface, egl_error_str()); |
| 307 | } |
| 308 | } |
| 309 | #endif |
| 310 | } |
| 311 | |
John Reck | c96955d | 2016-02-26 14:56:44 -0800 | [diff] [blame] | 312 | bool EglManager::damageRequiresSwap() { |
| 313 | return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; |
| 314 | } |
| 315 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 316 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 317 | |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 318 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 319 | ATRACE_NAME("Finishing GPU work"); |
| 320 | fence(); |
| 321 | } |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 322 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 323 | EGLint rects[4]; |
| 324 | frame.map(screenDirty, rects); |
| 325 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, |
| 326 | screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 327 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 328 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 329 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 330 | return true; |
| 331 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 332 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 333 | // For some reason our surface was destroyed out from under us |
| 334 | // This really shouldn't happen, but if it does we can recover easily |
| 335 | // by just not trying to use the surface anymore |
John Reck | d1ddcf1 | 2016-02-05 15:59:55 -0800 | [diff] [blame] | 336 | ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", |
| 337 | err, frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", |
| 341 | err, egl_error_str(err)); |
| 342 | // Impossible to hit this, but the compiler doesn't know that |
| 343 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 344 | } |
| 345 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 346 | void EglManager::fence() { |
| 347 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
| 348 | eglClientWaitSyncKHR(mEglDisplay, fence, |
| 349 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
| 350 | eglDestroySyncKHR(mEglDisplay, fence); |
| 351 | } |
| 352 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 353 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 354 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 355 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 356 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 357 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
| 358 | if (!preserved) { |
| 359 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 360 | (void*) surface, egl_error_str()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 361 | // Maybe it's already set? |
| 362 | EGLint swapBehavior; |
| 363 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 364 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 365 | } else { |
| 366 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 367 | (void*) surface, egl_error_str()); |
| 368 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 369 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 370 | |
| 371 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 372 | } |
| 373 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 374 | } /* namespace renderthread */ |
| 375 | } /* namespace uirenderer */ |
| 376 | } /* namespace android */ |