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) |
| 76 | , mAtlasMapSize(0) { |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 77 | mCanSetPreserveBuffer = mAllowPreserveBuffer; |
| 78 | ALOGD("Use EGL_SWAP_BEHAVIOR_PRESERVED: %s", mAllowPreserveBuffer ? "true" : "false"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void EglManager::initialize() { |
| 82 | if (hasEglContext()) return; |
| 83 | |
| 84 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 85 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 86 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
| 87 | |
| 88 | EGLint major, minor; |
| 89 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 90 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 91 | |
| 92 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 93 | |
| 94 | loadConfig(); |
| 95 | createContext(); |
| 96 | usePBufferSurface(); |
| 97 | mRenderThread.renderState().onGLContextCreated(); |
| 98 | initAtlas(); |
| 99 | } |
| 100 | |
| 101 | bool EglManager::hasEglContext() { |
| 102 | return mEglDisplay != EGL_NO_DISPLAY; |
| 103 | } |
| 104 | |
| 105 | void EglManager::requireGlContext() { |
| 106 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "No EGL context"); |
| 107 | |
| 108 | // We don't care *WHAT* surface is active, just that one is active to give |
| 109 | // us access to the GL context |
| 110 | if (mCurrentSurface == EGL_NO_SURFACE) { |
| 111 | usePBufferSurface(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void EglManager::loadConfig() { |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 116 | EGLint swapBehavior = mCanSetPreserveBuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 117 | EGLint attribs[] = { |
| 118 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 119 | EGL_RED_SIZE, 8, |
| 120 | EGL_GREEN_SIZE, 8, |
| 121 | EGL_BLUE_SIZE, 8, |
| 122 | EGL_ALPHA_SIZE, 8, |
| 123 | EGL_DEPTH_SIZE, 0, |
| 124 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 125 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 126 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 127 | EGL_NONE |
| 128 | }; |
| 129 | |
| 130 | EGLint num_configs = 1; |
| 131 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 132 | || num_configs != 1) { |
| 133 | // Failed to get a valid config |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 134 | if (mCanSetPreserveBuffer) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 135 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 136 | // Try again without dirty regions enabled |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 137 | mCanSetPreserveBuffer = false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 138 | loadConfig(); |
| 139 | } else { |
| 140 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void EglManager::createContext() { |
| 146 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 147 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
| 148 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 149 | "Failed to create context, error = %s", egl_error_str()); |
| 150 | } |
| 151 | |
| 152 | void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 153 | int64_t* map, size_t mapSize) { |
| 154 | |
| 155 | // Already initialized |
| 156 | if (mAtlasBuffer.get()) { |
| 157 | ALOGW("Multiple calls to setTextureAtlas!"); |
| 158 | delete map; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | mAtlasBuffer = buffer; |
| 163 | mAtlasMap = map; |
| 164 | mAtlasMapSize = mapSize; |
| 165 | |
| 166 | if (hasEglContext()) { |
| 167 | usePBufferSurface(); |
| 168 | initAtlas(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void EglManager::initAtlas() { |
| 173 | if (mAtlasBuffer.get()) { |
| 174 | Caches::getInstance().assetAtlas.init(mAtlasBuffer, mAtlasMap, mAtlasMapSize); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void EglManager::usePBufferSurface() { |
| 179 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 180 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
| 181 | |
| 182 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 183 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 184 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 185 | } |
| 186 | makeCurrent(mPBufferSurface); |
| 187 | } |
| 188 | |
| 189 | EGLSurface EglManager::createSurface(EGLNativeWindowType window) { |
| 190 | initialize(); |
| 191 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL); |
| 192 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 193 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 194 | (void*) window, egl_error_str()); |
| 195 | return surface; |
| 196 | } |
| 197 | |
| 198 | void EglManager::destroySurface(EGLSurface surface) { |
| 199 | if (isCurrent(surface)) { |
| 200 | makeCurrent(EGL_NO_SURFACE); |
| 201 | } |
| 202 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 203 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void EglManager::destroy() { |
| 208 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 209 | |
| 210 | usePBufferSurface(); |
| 211 | if (Caches::hasInstance()) { |
| 212 | Caches::getInstance().terminate(); |
| 213 | } |
| 214 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 215 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 216 | eglDestroyContext(mEglDisplay, mEglContext); |
| 217 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 218 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 219 | eglTerminate(mEglDisplay); |
| 220 | eglReleaseThread(); |
| 221 | |
| 222 | mEglDisplay = EGL_NO_DISPLAY; |
| 223 | mEglContext = EGL_NO_CONTEXT; |
| 224 | mPBufferSurface = EGL_NO_SURFACE; |
| 225 | mCurrentSurface = EGL_NO_SURFACE; |
| 226 | } |
| 227 | |
| 228 | bool EglManager::makeCurrent(EGLSurface surface) { |
| 229 | if (isCurrent(surface)) return false; |
| 230 | |
| 231 | if (surface == EGL_NO_SURFACE) { |
| 232 | // If we are setting EGL_NO_SURFACE we don't care about any of the potential |
| 233 | // return errors, which would only happen if mEglDisplay had already been |
| 234 | // destroyed in which case the current context is already NO_CONTEXT |
| 235 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 236 | } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
| 237 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 238 | (void*)surface, egl_error_str()); |
| 239 | } |
| 240 | mCurrentSurface = surface; |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 245 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 246 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 247 | makeCurrent(surface); |
| 248 | if (width) { |
| 249 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
| 250 | } |
| 251 | if (height) { |
| 252 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 253 | } |
| 254 | eglBeginFrame(mEglDisplay, surface); |
| 255 | } |
| 256 | |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 257 | bool EglManager::swapBuffers(EGLSurface surface) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 258 | eglSwapBuffers(mEglDisplay, surface); |
| 259 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 260 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 261 | return true; |
| 262 | } |
| 263 | if (err == EGL_BAD_SURFACE) { |
| 264 | // For some reason our surface was destroyed out from under us |
| 265 | // This really shouldn't happen, but if it does we can recover easily |
| 266 | // by just not trying to use the surface anymore |
| 267 | ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", surface); |
| 268 | return false; |
| 269 | } |
| 270 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", |
| 271 | err, egl_error_str(err)); |
| 272 | // Impossible to hit this, but the compiler doesn't know that |
| 273 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 274 | } |
| 275 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 276 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
| 277 | if (CC_UNLIKELY(!mAllowPreserveBuffer)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 278 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 279 | bool preserved = false; |
| 280 | if (mCanSetPreserveBuffer) { |
| 281 | preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 282 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
| 283 | if (CC_UNLIKELY(!preserved)) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 284 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 285 | (void*) surface, egl_error_str()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 286 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 287 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 288 | if (CC_UNLIKELY(!preserved)) { |
| 289 | // Maybe it's already set? |
| 290 | EGLint swapBehavior; |
| 291 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 292 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 293 | } else { |
| 294 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 295 | (void*) surface, egl_error_str()); |
| 296 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 297 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame^] | 298 | |
| 299 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | } /* namespace renderthread */ |
| 303 | } /* namespace uirenderer */ |
| 304 | } /* namespace android */ |