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 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 32 | #define WAIT_FOR_GPU_COMPLETION 0 |
| 33 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 34 | // Android-specific addition that is used to show when frames began in systrace |
| 35 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 36 | |
| 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | namespace renderthread { |
| 40 | |
| 41 | #define ERROR_CASE(x) case x: return #x; |
| 42 | static const char* egl_error_str(EGLint error) { |
| 43 | switch (error) { |
| 44 | ERROR_CASE(EGL_SUCCESS) |
| 45 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 46 | ERROR_CASE(EGL_BAD_ACCESS) |
| 47 | ERROR_CASE(EGL_BAD_ALLOC) |
| 48 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 49 | ERROR_CASE(EGL_BAD_CONFIG) |
| 50 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 51 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 52 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 53 | ERROR_CASE(EGL_BAD_MATCH) |
| 54 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 55 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 56 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 57 | ERROR_CASE(EGL_BAD_SURFACE) |
| 58 | ERROR_CASE(EGL_CONTEXT_LOST) |
| 59 | default: |
| 60 | return "Unknown error"; |
| 61 | } |
| 62 | } |
| 63 | static const char* egl_error_str() { |
| 64 | return egl_error_str(eglGetError()); |
| 65 | } |
| 66 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 67 | static struct { |
| 68 | bool bufferAge = false; |
| 69 | bool setDamage = false; |
| 70 | } EglExtensions; |
| 71 | |
| 72 | void Frame::map(const SkRect& in, EGLint* out) const { |
| 73 | /* The rectangles are specified relative to the bottom-left of the surface |
| 74 | * and the x and y components of each rectangle specify the bottom-left |
| 75 | * position of that rectangle. |
| 76 | * |
| 77 | * HWUI does everything with 0,0 being top-left, so need to map |
| 78 | * the rect |
| 79 | */ |
| 80 | SkIRect idirty; |
| 81 | in.roundOut(&idirty); |
| 82 | EGLint y = mHeight - (idirty.y() + idirty.height()); |
| 83 | // layout: {x, y, width, height} |
| 84 | out[0] = idirty.x(); |
| 85 | out[1] = y; |
| 86 | out[2] = idirty.width(); |
| 87 | out[3] = idirty.height(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | EglManager::EglManager(RenderThread& thread) |
| 91 | : mRenderThread(thread) |
| 92 | , mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 93 | , mEglConfig(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 94 | , mEglContext(EGL_NO_CONTEXT) |
| 95 | , mPBufferSurface(EGL_NO_SURFACE) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 96 | , mCurrentSurface(EGL_NO_SURFACE) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 97 | , mAtlasMap(nullptr) |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 98 | , mAtlasMapSize(0) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void EglManager::initialize() { |
| 102 | if (hasEglContext()) return; |
| 103 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 104 | ATRACE_NAME("Creating EGLContext"); |
| 105 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 106 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 107 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 108 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
| 109 | |
| 110 | EGLint major, minor; |
| 111 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 112 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 113 | |
| 114 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 115 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 116 | initExtensions(); |
Season Li | 13d1b4a | 2015-07-29 17:16:19 -0700 | [diff] [blame] | 117 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 118 | // Now that extensions are loaded, pick a swap behavior |
| 119 | if (Properties::enablePartialUpdates) { |
| 120 | if (Properties::useBufferAge && EglExtensions.bufferAge) { |
| 121 | mSwapBehavior = SwapBehavior::BufferAge; |
| 122 | } else { |
| 123 | mSwapBehavior = SwapBehavior::Preserved; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | loadConfig(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 128 | createContext(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 129 | createPBufferSurface(); |
| 130 | makeCurrent(mPBufferSurface); |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 131 | DeviceInfo::initialize(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 132 | mRenderThread.renderState().onGLContextCreated(); |
| 133 | initAtlas(); |
| 134 | } |
| 135 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 136 | void EglManager::initExtensions() { |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 137 | auto extensions = StringUtils::split( |
| 138 | eglQueryString(mEglDisplay, EGL_EXTENSIONS)); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 139 | EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age"); |
| 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() { |
| 182 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 183 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
| 184 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 185 | "Failed to create context, error = %s", egl_error_str()); |
| 186 | } |
| 187 | |
| 188 | void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 189 | int64_t* map, size_t mapSize) { |
| 190 | |
| 191 | // Already initialized |
| 192 | if (mAtlasBuffer.get()) { |
| 193 | ALOGW("Multiple calls to setTextureAtlas!"); |
| 194 | delete map; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | mAtlasBuffer = buffer; |
| 199 | mAtlasMap = map; |
| 200 | mAtlasMapSize = mapSize; |
| 201 | |
| 202 | if (hasEglContext()) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 203 | initAtlas(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void EglManager::initAtlas() { |
| 208 | if (mAtlasBuffer.get()) { |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 209 | mRenderThread.renderState().assetAtlas().init(mAtlasBuffer, |
| 210 | mAtlasMap, mAtlasMapSize); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 214 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 215 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 216 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
| 217 | |
| 218 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 219 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 220 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 221 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | EGLSurface EglManager::createSurface(EGLNativeWindowType window) { |
| 225 | initialize(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 226 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 227 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 228 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 229 | (void*) window, egl_error_str()); |
| 230 | return surface; |
| 231 | } |
| 232 | |
| 233 | void EglManager::destroySurface(EGLSurface surface) { |
| 234 | if (isCurrent(surface)) { |
| 235 | makeCurrent(EGL_NO_SURFACE); |
| 236 | } |
| 237 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 238 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void EglManager::destroy() { |
| 243 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 244 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 245 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 246 | eglDestroyContext(mEglDisplay, mEglContext); |
| 247 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 248 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 249 | eglTerminate(mEglDisplay); |
| 250 | eglReleaseThread(); |
| 251 | |
| 252 | mEglDisplay = EGL_NO_DISPLAY; |
| 253 | mEglContext = EGL_NO_CONTEXT; |
| 254 | mPBufferSurface = EGL_NO_SURFACE; |
| 255 | mCurrentSurface = EGL_NO_SURFACE; |
| 256 | } |
| 257 | |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 258 | bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 259 | if (isCurrent(surface)) return false; |
| 260 | |
| 261 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame] | 262 | // Ensure we always have a valid surface & context |
| 263 | surface = mPBufferSurface; |
| 264 | } |
| 265 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 266 | if (errOut) { |
| 267 | *errOut = eglGetError(); |
| 268 | ALOGW("Failed to make current on surface %p, error=%s", |
| 269 | (void*)surface, egl_error_str(*errOut)); |
| 270 | } else { |
| 271 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 272 | (void*)surface, egl_error_str()); |
| 273 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 274 | } |
| 275 | mCurrentSurface = surface; |
| 276 | return true; |
| 277 | } |
| 278 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 279 | EGLint EglManager::queryBufferAge(EGLSurface surface) { |
| 280 | switch (mSwapBehavior) { |
| 281 | case SwapBehavior::Discard: |
| 282 | return 0; |
| 283 | case SwapBehavior::Preserved: |
| 284 | return 1; |
| 285 | case SwapBehavior::BufferAge: |
| 286 | EGLint bufferAge; |
| 287 | eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); |
| 288 | return bufferAge; |
| 289 | } |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | Frame EglManager::beginFrame(EGLSurface surface) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 294 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 295 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 296 | makeCurrent(surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 297 | Frame frame; |
| 298 | frame.mSurface = surface; |
| 299 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); |
| 300 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); |
| 301 | frame.mBufferAge = queryBufferAge(surface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 302 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 303 | return frame; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 304 | } |
| 305 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 306 | void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { |
| 307 | #ifdef EGL_KHR_partial_update |
| 308 | if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { |
| 309 | EGLint rects[4]; |
| 310 | frame.map(dirty, rects); |
| 311 | if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { |
| 312 | LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s", |
| 313 | (void*)frame.mSurface, egl_error_str()); |
| 314 | } |
| 315 | } |
| 316 | #endif |
| 317 | } |
| 318 | |
| 319 | bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 320 | |
| 321 | #if WAIT_FOR_GPU_COMPLETION |
| 322 | { |
| 323 | ATRACE_NAME("Finishing GPU work"); |
| 324 | fence(); |
| 325 | } |
| 326 | #endif |
| 327 | |
John Reck | a672f6b | 2015-10-22 09:53:26 -0700 | [diff] [blame] | 328 | EGLint rects[4]; |
| 329 | frame.map(screenDirty, rects); |
| 330 | eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, |
| 331 | screenDirty.isEmpty() ? 0 : 1); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 332 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 333 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 334 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 335 | return true; |
| 336 | } |
John Reck | c2547fa | 2015-10-26 13:52:52 -0700 | [diff] [blame] | 337 | if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 338 | // For some reason our surface was destroyed out from under us |
| 339 | // This really shouldn't happen, but if it does we can recover easily |
| 340 | // by just not trying to use the surface anymore |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 341 | ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", |
| 342 | frame.mSurface); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", |
| 346 | err, egl_error_str(err)); |
| 347 | // Impossible to hit this, but the compiler doesn't know that |
| 348 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 349 | } |
| 350 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 351 | void EglManager::fence() { |
| 352 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
| 353 | eglClientWaitSyncKHR(mEglDisplay, fence, |
| 354 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
| 355 | eglDestroySyncKHR(mEglDisplay, fence); |
| 356 | } |
| 357 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 358 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 359 | if (mSwapBehavior != SwapBehavior::Preserved) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 360 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 361 | bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 362 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
| 363 | if (!preserved) { |
| 364 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 365 | (void*) surface, egl_error_str()); |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 366 | // Maybe it's already set? |
| 367 | EGLint swapBehavior; |
| 368 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 369 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 370 | } else { |
| 371 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 372 | (void*) surface, egl_error_str()); |
| 373 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 374 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 375 | |
| 376 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 377 | } |
| 378 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 379 | } /* namespace renderthread */ |
| 380 | } /* namespace uirenderer */ |
| 381 | } /* namespace android */ |