EglManager.cpp revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
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 17#include "EglManager.h" 18 19#include "Caches.h" 20#include "DeviceInfo.h" 21#include "Properties.h" 22#include "RenderThread.h" 23#include "renderstate/RenderState.h" 24#include "utils/StringUtils.h" 25#include <cutils/log.h> 26#include <cutils/properties.h> 27#include <EGL/eglext.h> 28#include <string> 29 30#define GLES_VERSION 2 31 32// Android-specific addition that is used to show when frames began in systrace 33EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); 34 35namespace android { 36namespace uirenderer { 37namespace renderthread { 38 39#define ERROR_CASE(x) case x: return #x; 40static 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} 61static const char* egl_error_str() { 62 return egl_error_str(eglGetError()); 63} 64 65static struct { 66 bool bufferAge = false; 67 bool setDamage = false; 68} EglExtensions; 69 70void Frame::map(const SkRect& in, EGLint* out) const { 71 /* The rectangles are specified relative to the bottom-left of the surface 72 * and the x and y components of each rectangle specify the bottom-left 73 * position of that rectangle. 74 * 75 * HWUI does everything with 0,0 being top-left, so need to map 76 * the rect 77 */ 78 SkIRect idirty; 79 in.roundOut(&idirty); 80 EGLint y = mHeight - (idirty.y() + idirty.height()); 81 // layout: {x, y, width, height} 82 out[0] = idirty.x(); 83 out[1] = y; 84 out[2] = idirty.width(); 85 out[3] = idirty.height(); 86} 87 88EglManager::EglManager(RenderThread& thread) 89 : mRenderThread(thread) 90 , mEglDisplay(EGL_NO_DISPLAY) 91 , mEglConfig(nullptr) 92 , mEglContext(EGL_NO_CONTEXT) 93 , mPBufferSurface(EGL_NO_SURFACE) 94 , mCurrentSurface(EGL_NO_SURFACE) { 95} 96 97void EglManager::initialize() { 98 if (hasEglContext()) return; 99 100 ATRACE_NAME("Creating EGLContext"); 101 102 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 103 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 104 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); 105 106 EGLint major, minor; 107 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, 108 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); 109 110 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); 111 112 initExtensions(); 113 114 // Now that extensions are loaded, pick a swap behavior 115 if (Properties::enablePartialUpdates) { 116 if (Properties::useBufferAge && EglExtensions.bufferAge) { 117 mSwapBehavior = SwapBehavior::BufferAge; 118 } else { 119 mSwapBehavior = SwapBehavior::Preserved; 120 } 121 } 122 123 loadConfig(); 124 createContext(); 125 createPBufferSurface(); 126 makeCurrent(mPBufferSurface); 127 DeviceInfo::initialize(); 128 mRenderThread.renderState().onGLContextCreated(); 129} 130 131void EglManager::initExtensions() { 132 auto extensions = StringUtils::split( 133 eglQueryString(mEglDisplay, EGL_EXTENSIONS)); 134 // For our purposes we don't care if EGL_BUFFER_AGE is a result of 135 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered 136 // under EGL_KHR_partial_update and we don't need the expanded scope 137 // that EGL_EXT_buffer_age provides. 138 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age") 139 || extensions.has("EGL_KHR_partial_update"); 140 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); 141 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), 142 "Missing required extension EGL_KHR_swap_buffers_with_damage"); 143} 144 145bool EglManager::hasEglContext() { 146 return mEglDisplay != EGL_NO_DISPLAY; 147} 148 149void EglManager::loadConfig() { 150 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior)); 151 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved) 152 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; 153 EGLint attribs[] = { 154 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 155 EGL_RED_SIZE, 8, 156 EGL_GREEN_SIZE, 8, 157 EGL_BLUE_SIZE, 8, 158 EGL_ALPHA_SIZE, 8, 159 EGL_DEPTH_SIZE, 0, 160 EGL_CONFIG_CAVEAT, EGL_NONE, 161 EGL_STENCIL_SIZE, Stencil::getStencilSize(), 162 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, 163 EGL_NONE 164 }; 165 166 EGLint num_configs = 1; 167 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) 168 || num_configs != 1) { 169 if (mSwapBehavior == SwapBehavior::Preserved) { 170 // Try again without dirty regions enabled 171 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); 172 mSwapBehavior = SwapBehavior::Discard; 173 loadConfig(); 174 } else { 175 // Failed to get a valid config 176 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); 177 } 178 } 179} 180 181void EglManager::createContext() { 182 EGLint attribs[] = { 183 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, 184 EGL_NONE 185 }; 186 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); 187 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, 188 "Failed to create context, error = %s", egl_error_str()); 189} 190 191void EglManager::createPBufferSurface() { 192 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 193 "usePBufferSurface() called on uninitialized GlobalContext!"); 194 195 if (mPBufferSurface == EGL_NO_SURFACE) { 196 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; 197 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); 198 } 199} 200 201EGLSurface EglManager::createSurface(EGLNativeWindowType window) { 202 initialize(); 203 204 EGLint attribs[] = { 205#ifdef ANDROID_ENABLE_LINEAR_BLENDING 206 EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR, 207 EGL_COLORSPACE, EGL_COLORSPACE_sRGB, 208#endif 209 EGL_NONE 210 }; 211 212 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, attribs); 213 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 214 "Failed to create EGLSurface for window %p, eglErr = %s", 215 (void*) window, egl_error_str()); 216 217 if (mSwapBehavior != SwapBehavior::Preserved) { 218 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE, 219 "Failed to set swap behavior to destroyed for window %p, eglErr = %s", 220 (void*) window, egl_error_str()); 221 } 222 223 return surface; 224} 225 226void EglManager::destroySurface(EGLSurface surface) { 227 if (isCurrent(surface)) { 228 makeCurrent(EGL_NO_SURFACE); 229 } 230 if (!eglDestroySurface(mEglDisplay, surface)) { 231 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); 232 } 233} 234 235void EglManager::destroy() { 236 if (mEglDisplay == EGL_NO_DISPLAY) return; 237 238 mRenderThread.renderState().onGLContextDestroyed(); 239 eglDestroyContext(mEglDisplay, mEglContext); 240 eglDestroySurface(mEglDisplay, mPBufferSurface); 241 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 242 eglTerminate(mEglDisplay); 243 eglReleaseThread(); 244 245 mEglDisplay = EGL_NO_DISPLAY; 246 mEglContext = EGL_NO_CONTEXT; 247 mPBufferSurface = EGL_NO_SURFACE; 248 mCurrentSurface = EGL_NO_SURFACE; 249} 250 251bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) { 252 if (isCurrent(surface)) return false; 253 254 if (surface == EGL_NO_SURFACE) { 255 // Ensure we always have a valid surface & context 256 surface = mPBufferSurface; 257 } 258 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { 259 if (errOut) { 260 *errOut = eglGetError(); 261 ALOGW("Failed to make current on surface %p, error=%s", 262 (void*)surface, egl_error_str(*errOut)); 263 } else { 264 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", 265 (void*)surface, egl_error_str()); 266 } 267 } 268 mCurrentSurface = surface; 269 return true; 270} 271 272EGLint EglManager::queryBufferAge(EGLSurface surface) { 273 switch (mSwapBehavior) { 274 case SwapBehavior::Discard: 275 return 0; 276 case SwapBehavior::Preserved: 277 return 1; 278 case SwapBehavior::BufferAge: 279 EGLint bufferAge; 280 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); 281 return bufferAge; 282 } 283 return 0; 284} 285 286Frame EglManager::beginFrame(EGLSurface surface) { 287 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 288 "Tried to beginFrame on EGL_NO_SURFACE!"); 289 makeCurrent(surface); 290 Frame frame; 291 frame.mSurface = surface; 292 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); 293 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); 294 frame.mBufferAge = queryBufferAge(surface); 295 eglBeginFrame(mEglDisplay, surface); 296 return frame; 297} 298 299void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { 300#ifdef EGL_KHR_partial_update 301 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { 302 EGLint rects[4]; 303 frame.map(dirty, rects); 304 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { 305 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s", 306 (void*)frame.mSurface, egl_error_str()); 307 } 308 } 309#endif 310} 311 312bool EglManager::damageRequiresSwap() { 313 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; 314} 315 316bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { 317 318 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { 319 ATRACE_NAME("Finishing GPU work"); 320 fence(); 321 } 322 323 EGLint rects[4]; 324 frame.map(screenDirty, rects); 325 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, 326 screenDirty.isEmpty() ? 0 : 1); 327 328 EGLint err = eglGetError(); 329 if (CC_LIKELY(err == EGL_SUCCESS)) { 330 return true; 331 } 332 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { 333 // For some reason our surface was destroyed out from under us 334 // This really shouldn't happen, but if it does we can recover easily 335 // by just not trying to use the surface anymore 336 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", 337 err, frame.mSurface); 338 return false; 339 } 340 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", 341 err, egl_error_str(err)); 342 // Impossible to hit this, but the compiler doesn't know that 343 return false; 344} 345 346void EglManager::fence() { 347 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); 348 eglClientWaitSyncKHR(mEglDisplay, fence, 349 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); 350 eglDestroySyncKHR(mEglDisplay, fence); 351} 352 353bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { 354 if (mSwapBehavior != SwapBehavior::Preserved) return false; 355 356 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, 357 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); 358 if (!preserved) { 359 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", 360 (void*) surface, egl_error_str()); 361 // Maybe it's already set? 362 EGLint swapBehavior; 363 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { 364 preserved = (swapBehavior == EGL_BUFFER_PRESERVED); 365 } else { 366 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", 367 (void*) surface, egl_error_str()); 368 } 369 } 370 371 return preserved; 372} 373 374} /* namespace renderthread */ 375} /* namespace uirenderer */ 376} /* namespace android */ 377