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 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 19 | #include "../Caches.h" |
| 20 | #include "../renderstate/RenderState.h" |
| 21 | #include "RenderThread.h" |
| 22 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 23 | #include <cutils/log.h> |
| 24 | #include <cutils/properties.h> |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 25 | #include <EGL/eglext.h> |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 26 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 27 | #define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions" |
| 28 | #define GLES_VERSION 2 |
| 29 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 30 | #define WAIT_FOR_GPU_COMPLETION 0 |
| 31 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 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 | |
| 65 | static bool load_dirty_regions_property() { |
| 66 | char buf[PROPERTY_VALUE_MAX]; |
| 67 | int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true"); |
| 68 | return !strncasecmp("true", buf, len); |
| 69 | } |
| 70 | |
| 71 | EglManager::EglManager(RenderThread& thread) |
| 72 | : mRenderThread(thread) |
| 73 | , mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 74 | , mEglConfig(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 75 | , mEglContext(EGL_NO_CONTEXT) |
| 76 | , mPBufferSurface(EGL_NO_SURFACE) |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 77 | , mAllowPreserveBuffer(load_dirty_regions_property()) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 78 | , mCurrentSurface(EGL_NO_SURFACE) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 79 | , mAtlasMap(nullptr) |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 80 | , mAtlasMapSize(0) |
| 81 | , mInFrame(false) { |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 82 | mCanSetPreserveBuffer = mAllowPreserveBuffer; |
| 83 | ALOGD("Use EGL_SWAP_BEHAVIOR_PRESERVED: %s", mAllowPreserveBuffer ? "true" : "false"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void EglManager::initialize() { |
| 87 | if (hasEglContext()) return; |
| 88 | |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 89 | ATRACE_NAME("Creating EGLContext"); |
| 90 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 91 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 92 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 93 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
| 94 | |
| 95 | EGLint major, minor; |
| 96 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 97 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 98 | |
| 99 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 100 | |
| 101 | loadConfig(); |
| 102 | createContext(); |
| 103 | usePBufferSurface(); |
| 104 | mRenderThread.renderState().onGLContextCreated(); |
| 105 | initAtlas(); |
| 106 | } |
| 107 | |
| 108 | bool EglManager::hasEglContext() { |
| 109 | return mEglDisplay != EGL_NO_DISPLAY; |
| 110 | } |
| 111 | |
| 112 | void EglManager::requireGlContext() { |
| 113 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "No EGL context"); |
| 114 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 115 | if (!mInFrame) { |
| 116 | // We can't be certain about the state of the current surface (whether |
| 117 | // or not it is destroyed, for example), so err on the side of using |
| 118 | // the pbuffer surface which we fully control |
| 119 | usePBufferSurface(); |
| 120 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void EglManager::loadConfig() { |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 124 | EGLint swapBehavior = mCanSetPreserveBuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 125 | EGLint attribs[] = { |
| 126 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 127 | EGL_RED_SIZE, 8, |
| 128 | EGL_GREEN_SIZE, 8, |
| 129 | EGL_BLUE_SIZE, 8, |
| 130 | EGL_ALPHA_SIZE, 8, |
| 131 | EGL_DEPTH_SIZE, 0, |
| 132 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 133 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 134 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 135 | EGL_NONE |
| 136 | }; |
| 137 | |
| 138 | EGLint num_configs = 1; |
| 139 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 140 | || num_configs != 1) { |
| 141 | // Failed to get a valid config |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 142 | if (mCanSetPreserveBuffer) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 143 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 144 | // Try again without dirty regions enabled |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 145 | mCanSetPreserveBuffer = false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 146 | loadConfig(); |
| 147 | } else { |
| 148 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void EglManager::createContext() { |
| 154 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 155 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
| 156 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 157 | "Failed to create context, error = %s", egl_error_str()); |
| 158 | } |
| 159 | |
| 160 | void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 161 | int64_t* map, size_t mapSize) { |
| 162 | |
| 163 | // Already initialized |
| 164 | if (mAtlasBuffer.get()) { |
| 165 | ALOGW("Multiple calls to setTextureAtlas!"); |
| 166 | delete map; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | mAtlasBuffer = buffer; |
| 171 | mAtlasMap = map; |
| 172 | mAtlasMapSize = mapSize; |
| 173 | |
| 174 | if (hasEglContext()) { |
| 175 | usePBufferSurface(); |
| 176 | initAtlas(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void EglManager::initAtlas() { |
| 181 | if (mAtlasBuffer.get()) { |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 182 | mRenderThread.renderState().assetAtlas().init(mAtlasBuffer, |
| 183 | mAtlasMap, mAtlasMapSize); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void EglManager::usePBufferSurface() { |
| 188 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 189 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
| 190 | |
| 191 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 192 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 193 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 194 | } |
| 195 | makeCurrent(mPBufferSurface); |
| 196 | } |
| 197 | |
| 198 | EGLSurface EglManager::createSurface(EGLNativeWindowType window) { |
| 199 | initialize(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 200 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 201 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 202 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 203 | (void*) window, egl_error_str()); |
| 204 | return surface; |
| 205 | } |
| 206 | |
| 207 | void EglManager::destroySurface(EGLSurface surface) { |
| 208 | if (isCurrent(surface)) { |
| 209 | makeCurrent(EGL_NO_SURFACE); |
| 210 | } |
| 211 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 212 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void EglManager::destroy() { |
| 217 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 218 | |
| 219 | usePBufferSurface(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 220 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 221 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 222 | eglDestroyContext(mEglDisplay, mEglContext); |
| 223 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 224 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 225 | eglTerminate(mEglDisplay); |
| 226 | eglReleaseThread(); |
| 227 | |
| 228 | mEglDisplay = EGL_NO_DISPLAY; |
| 229 | mEglContext = EGL_NO_CONTEXT; |
| 230 | mPBufferSurface = EGL_NO_SURFACE; |
| 231 | mCurrentSurface = EGL_NO_SURFACE; |
| 232 | } |
| 233 | |
| 234 | bool EglManager::makeCurrent(EGLSurface surface) { |
| 235 | if (isCurrent(surface)) return false; |
| 236 | |
| 237 | if (surface == EGL_NO_SURFACE) { |
| 238 | // If we are setting EGL_NO_SURFACE we don't care about any of the potential |
| 239 | // return errors, which would only happen if mEglDisplay had already been |
| 240 | // destroyed in which case the current context is already NO_CONTEXT |
| 241 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 242 | } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
| 243 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 244 | (void*)surface, egl_error_str()); |
| 245 | } |
| 246 | mCurrentSurface = surface; |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 251 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 252 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 253 | makeCurrent(surface); |
| 254 | if (width) { |
| 255 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
| 256 | } |
| 257 | if (height) { |
| 258 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 259 | } |
| 260 | eglBeginFrame(mEglDisplay, surface); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 261 | mInFrame = true; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 262 | } |
| 263 | |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 264 | bool EglManager::swapBuffers(EGLSurface surface) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 265 | mInFrame = false; |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 266 | |
| 267 | #if WAIT_FOR_GPU_COMPLETION |
| 268 | { |
| 269 | ATRACE_NAME("Finishing GPU work"); |
| 270 | fence(); |
| 271 | } |
| 272 | #endif |
| 273 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 274 | eglSwapBuffers(mEglDisplay, surface); |
| 275 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 276 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 277 | return true; |
| 278 | } |
| 279 | if (err == EGL_BAD_SURFACE) { |
| 280 | // For some reason our surface was destroyed out from under us |
| 281 | // This really shouldn't happen, but if it does we can recover easily |
| 282 | // by just not trying to use the surface anymore |
| 283 | ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", surface); |
| 284 | return false; |
| 285 | } |
| 286 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", |
| 287 | err, egl_error_str(err)); |
| 288 | // Impossible to hit this, but the compiler doesn't know that |
| 289 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 290 | } |
| 291 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 292 | void EglManager::fence() { |
| 293 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
| 294 | eglClientWaitSyncKHR(mEglDisplay, fence, |
| 295 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
| 296 | eglDestroySyncKHR(mEglDisplay, fence); |
| 297 | } |
| 298 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 299 | void EglManager::cancelFrame() { |
| 300 | mInFrame = false; |
| 301 | } |
| 302 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 303 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
| 304 | if (CC_UNLIKELY(!mAllowPreserveBuffer)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 305 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 306 | bool preserved = false; |
| 307 | if (mCanSetPreserveBuffer) { |
| 308 | preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 309 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
| 310 | if (CC_UNLIKELY(!preserved)) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 311 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 312 | (void*) surface, egl_error_str()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 313 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 314 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 315 | if (CC_UNLIKELY(!preserved)) { |
| 316 | // Maybe it's already set? |
| 317 | EGLint swapBehavior; |
| 318 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 319 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 320 | } else { |
| 321 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 322 | (void*) surface, egl_error_str()); |
| 323 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 324 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 325 | |
| 326 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | } /* namespace renderthread */ |
| 330 | } /* namespace uirenderer */ |
| 331 | } /* namespace android */ |