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