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