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