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 | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 23 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 24 | #include <cutils/log.h> |
| 25 | #include <cutils/properties.h> |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 26 | #include <EGL/eglext.h> |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 27 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 28 | #define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions" |
| 29 | #define GLES_VERSION 2 |
| 30 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 31 | #define WAIT_FOR_GPU_COMPLETION 0 |
| 32 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 33 | // Android-specific addition that is used to show when frames began in systrace |
| 34 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 35 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | namespace renderthread { |
| 39 | |
| 40 | #define ERROR_CASE(x) case x: return #x; |
| 41 | static const char* egl_error_str(EGLint error) { |
| 42 | switch (error) { |
| 43 | ERROR_CASE(EGL_SUCCESS) |
| 44 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 45 | ERROR_CASE(EGL_BAD_ACCESS) |
| 46 | ERROR_CASE(EGL_BAD_ALLOC) |
| 47 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 48 | ERROR_CASE(EGL_BAD_CONFIG) |
| 49 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 50 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 51 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 52 | ERROR_CASE(EGL_BAD_MATCH) |
| 53 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 54 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 55 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 56 | ERROR_CASE(EGL_BAD_SURFACE) |
| 57 | ERROR_CASE(EGL_CONTEXT_LOST) |
| 58 | default: |
| 59 | return "Unknown error"; |
| 60 | } |
| 61 | } |
| 62 | static const char* egl_error_str() { |
| 63 | return egl_error_str(eglGetError()); |
| 64 | } |
| 65 | |
| 66 | static bool load_dirty_regions_property() { |
| 67 | char buf[PROPERTY_VALUE_MAX]; |
| 68 | int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true"); |
| 69 | return !strncasecmp("true", buf, len); |
| 70 | } |
| 71 | |
| 72 | EglManager::EglManager(RenderThread& thread) |
| 73 | : mRenderThread(thread) |
| 74 | , mEglDisplay(EGL_NO_DISPLAY) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 75 | , mEglConfig(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 76 | , mEglContext(EGL_NO_CONTEXT) |
| 77 | , mPBufferSurface(EGL_NO_SURFACE) |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 78 | , mAllowPreserveBuffer(load_dirty_regions_property()) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 79 | , mCurrentSurface(EGL_NO_SURFACE) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 80 | , mAtlasMap(nullptr) |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame^] | 81 | , mAtlasMapSize(0) { |
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(); |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame^] | 103 | createPBufferSurface(); |
| 104 | makeCurrent(mPBufferSurface); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 105 | mRenderThread.renderState().onGLContextCreated(); |
| 106 | initAtlas(); |
| 107 | } |
| 108 | |
| 109 | bool EglManager::hasEglContext() { |
| 110 | return mEglDisplay != EGL_NO_DISPLAY; |
| 111 | } |
| 112 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 113 | void EglManager::loadConfig() { |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 114 | EGLint swapBehavior = mCanSetPreserveBuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 115 | EGLint attribs[] = { |
| 116 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 117 | EGL_RED_SIZE, 8, |
| 118 | EGL_GREEN_SIZE, 8, |
| 119 | EGL_BLUE_SIZE, 8, |
| 120 | EGL_ALPHA_SIZE, 8, |
| 121 | EGL_DEPTH_SIZE, 0, |
| 122 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 123 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 124 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 125 | EGL_NONE |
| 126 | }; |
| 127 | |
| 128 | EGLint num_configs = 1; |
| 129 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 130 | || num_configs != 1) { |
| 131 | // Failed to get a valid config |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 132 | if (mCanSetPreserveBuffer) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 133 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 134 | // Try again without dirty regions enabled |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 135 | mCanSetPreserveBuffer = false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 136 | loadConfig(); |
| 137 | } else { |
| 138 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void EglManager::createContext() { |
| 144 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 145 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
| 146 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 147 | "Failed to create context, error = %s", egl_error_str()); |
| 148 | } |
| 149 | |
| 150 | void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 151 | int64_t* map, size_t mapSize) { |
| 152 | |
| 153 | // Already initialized |
| 154 | if (mAtlasBuffer.get()) { |
| 155 | ALOGW("Multiple calls to setTextureAtlas!"); |
| 156 | delete map; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | mAtlasBuffer = buffer; |
| 161 | mAtlasMap = map; |
| 162 | mAtlasMapSize = mapSize; |
| 163 | |
| 164 | if (hasEglContext()) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 165 | initAtlas(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void EglManager::initAtlas() { |
| 170 | if (mAtlasBuffer.get()) { |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 171 | mRenderThread.renderState().assetAtlas().init(mAtlasBuffer, |
| 172 | mAtlasMap, mAtlasMapSize); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame^] | 176 | void EglManager::createPBufferSurface() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 177 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 178 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
| 179 | |
| 180 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 181 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 182 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 183 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | EGLSurface EglManager::createSurface(EGLNativeWindowType window) { |
| 187 | initialize(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 188 | EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 189 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 190 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 191 | (void*) window, egl_error_str()); |
| 192 | return surface; |
| 193 | } |
| 194 | |
| 195 | void EglManager::destroySurface(EGLSurface surface) { |
| 196 | if (isCurrent(surface)) { |
| 197 | makeCurrent(EGL_NO_SURFACE); |
| 198 | } |
| 199 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 200 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void EglManager::destroy() { |
| 205 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 206 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 207 | mRenderThread.renderState().onGLContextDestroyed(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 208 | eglDestroyContext(mEglDisplay, mEglContext); |
| 209 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 210 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 211 | eglTerminate(mEglDisplay); |
| 212 | eglReleaseThread(); |
| 213 | |
| 214 | mEglDisplay = EGL_NO_DISPLAY; |
| 215 | mEglContext = EGL_NO_CONTEXT; |
| 216 | mPBufferSurface = EGL_NO_SURFACE; |
| 217 | mCurrentSurface = EGL_NO_SURFACE; |
| 218 | } |
| 219 | |
| 220 | bool EglManager::makeCurrent(EGLSurface surface) { |
| 221 | if (isCurrent(surface)) return false; |
| 222 | |
| 223 | if (surface == EGL_NO_SURFACE) { |
John Reck | d7db4d7 | 2015-05-20 07:18:55 -0700 | [diff] [blame^] | 224 | // Ensure we always have a valid surface & context |
| 225 | surface = mPBufferSurface; |
| 226 | } |
| 227 | if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 228 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 229 | (void*)surface, egl_error_str()); |
| 230 | } |
| 231 | mCurrentSurface = surface; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 236 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 237 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 238 | makeCurrent(surface); |
| 239 | if (width) { |
| 240 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
| 241 | } |
| 242 | if (height) { |
| 243 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 244 | } |
| 245 | eglBeginFrame(mEglDisplay, surface); |
| 246 | } |
| 247 | |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 248 | bool EglManager::swapBuffers(EGLSurface surface, const SkRect& dirty, |
| 249 | EGLint width, EGLint height) { |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 250 | |
| 251 | #if WAIT_FOR_GPU_COMPLETION |
| 252 | { |
| 253 | ATRACE_NAME("Finishing GPU work"); |
| 254 | fence(); |
| 255 | } |
| 256 | #endif |
| 257 | |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 258 | #ifdef EGL_KHR_swap_buffers_with_damage |
| 259 | if (CC_UNLIKELY(Properties::swapBuffersWithDamage)) { |
| 260 | SkIRect idirty; |
| 261 | dirty.roundOut(&idirty); |
| 262 | /* |
| 263 | * EGL_KHR_swap_buffers_with_damage spec states: |
| 264 | * |
| 265 | * The rectangles are specified relative to the bottom-left of the surface |
| 266 | * and the x and y components of each rectangle specify the bottom-left |
| 267 | * position of that rectangle. |
| 268 | * |
| 269 | * HWUI does everything with 0,0 being top-left, so need to map |
| 270 | * the rect |
| 271 | */ |
| 272 | EGLint y = height - (idirty.y() + idirty.height()); |
| 273 | // layout: {x, y, width, height} |
| 274 | EGLint rects[4] = { idirty.x(), y, idirty.width(), idirty.height() }; |
| 275 | EGLint numrects = dirty.isEmpty() ? 0 : 1; |
| 276 | // TODO: Remove prior to enabling this path by default |
| 277 | ALOGD("Swap buffers with damage %d: %d, %d, %d, %d (src=" |
| 278 | RECT_STRING ")", |
| 279 | dirty.isEmpty() ? 0 : 1, rects[0], rects[1], rects[2], rects[3], |
| 280 | SK_RECT_ARGS(dirty)); |
| 281 | eglSwapBuffersWithDamageKHR(mEglDisplay, surface, rects, numrects); |
| 282 | } else { |
| 283 | eglSwapBuffers(mEglDisplay, surface); |
| 284 | } |
| 285 | #else |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 286 | eglSwapBuffers(mEglDisplay, surface); |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 287 | #endif |
| 288 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 289 | EGLint err = eglGetError(); |
John Reck | 2cdbc7d | 2014-09-17 16:06:36 -0700 | [diff] [blame] | 290 | if (CC_LIKELY(err == EGL_SUCCESS)) { |
| 291 | return true; |
| 292 | } |
| 293 | if (err == EGL_BAD_SURFACE) { |
| 294 | // For some reason our surface was destroyed out from under us |
| 295 | // This really shouldn't happen, but if it does we can recover easily |
| 296 | // by just not trying to use the surface anymore |
| 297 | ALOGW("swapBuffers encountered EGL_BAD_SURFACE on %p, halting rendering...", surface); |
| 298 | return false; |
| 299 | } |
| 300 | LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", |
| 301 | err, egl_error_str(err)); |
| 302 | // Impossible to hit this, but the compiler doesn't know that |
| 303 | return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 304 | } |
| 305 | |
John Reck | 5515637 | 2015-01-21 07:46:37 -0800 | [diff] [blame] | 306 | void EglManager::fence() { |
| 307 | EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); |
| 308 | eglClientWaitSyncKHR(mEglDisplay, fence, |
| 309 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); |
| 310 | eglDestroySyncKHR(mEglDisplay, fence); |
| 311 | } |
| 312 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 313 | bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { |
| 314 | if (CC_UNLIKELY(!mAllowPreserveBuffer)) return false; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 315 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 316 | bool preserved = false; |
| 317 | if (mCanSetPreserveBuffer) { |
| 318 | preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, |
| 319 | preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); |
| 320 | if (CC_UNLIKELY(!preserved)) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 321 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 322 | (void*) surface, egl_error_str()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 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 | if (CC_UNLIKELY(!preserved)) { |
| 326 | // Maybe it's already set? |
| 327 | EGLint swapBehavior; |
| 328 | if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { |
| 329 | preserved = (swapBehavior == EGL_BUFFER_PRESERVED); |
| 330 | } else { |
| 331 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 332 | (void*) surface, egl_error_str()); |
| 333 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 334 | } |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 335 | |
| 336 | return preserved; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | } /* namespace renderthread */ |
| 340 | } /* namespace uirenderer */ |
| 341 | } /* namespace android */ |