EglManager.cpp revision 8733bff058e12075e50c15c0c56c20298cc5f44f
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 , mAtlasMap(nullptr) 96 , mAtlasMapSize(0) { 97} 98 99void EglManager::initialize() { 100 if (hasEglContext()) return; 101 102 ATRACE_NAME("Creating EGLContext"); 103 104 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 105 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 106 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); 107 108 EGLint major, minor; 109 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, 110 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); 111 112 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); 113 114 initExtensions(); 115 116 // Now that extensions are loaded, pick a swap behavior 117 if (Properties::enablePartialUpdates) { 118 if (Properties::useBufferAge && EglExtensions.bufferAge) { 119 mSwapBehavior = SwapBehavior::BufferAge; 120 } else { 121 mSwapBehavior = SwapBehavior::Preserved; 122 } 123 } 124 125 loadConfig(); 126 createContext(); 127 createPBufferSurface(); 128 makeCurrent(mPBufferSurface); 129 DeviceInfo::initialize(); 130 mRenderThread.renderState().onGLContextCreated(); 131 initAtlas(); 132} 133 134void EglManager::initExtensions() { 135 auto extensions = StringUtils::split( 136 eglQueryString(mEglDisplay, EGL_EXTENSIONS)); 137 // For our purposes we don't care if EGL_BUFFER_AGE is a result of 138 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered 139 // under EGL_KHR_partial_update and we don't need the expanded scope 140 // that EGL_EXT_buffer_age provides. 141 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age") 142 || extensions.has("EGL_KHR_partial_update"); 143 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update"); 144 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"), 145 "Missing required extension EGL_KHR_swap_buffers_with_damage"); 146} 147 148bool EglManager::hasEglContext() { 149 return mEglDisplay != EGL_NO_DISPLAY; 150} 151 152void EglManager::loadConfig() { 153 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior)); 154 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved) 155 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; 156 EGLint attribs[] = { 157 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 158 EGL_RED_SIZE, 8, 159 EGL_GREEN_SIZE, 8, 160 EGL_BLUE_SIZE, 8, 161 EGL_ALPHA_SIZE, 8, 162 EGL_DEPTH_SIZE, 0, 163 EGL_CONFIG_CAVEAT, EGL_NONE, 164 EGL_STENCIL_SIZE, Stencil::getStencilSize(), 165 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, 166 EGL_NONE 167 }; 168 169 EGLint num_configs = 1; 170 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) 171 || num_configs != 1) { 172 if (mSwapBehavior == SwapBehavior::Preserved) { 173 // Try again without dirty regions enabled 174 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); 175 mSwapBehavior = SwapBehavior::Discard; 176 loadConfig(); 177 } else { 178 // Failed to get a valid config 179 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); 180 } 181 } 182} 183 184void EglManager::createContext() { 185 EGLint attribs[] = { 186 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, 187 EGL_NONE 188 }; 189 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); 190 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, 191 "Failed to create context, error = %s", egl_error_str()); 192} 193 194void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, 195 int64_t* map, size_t mapSize) { 196 197 // Already initialized 198 if (mAtlasBuffer.get()) { 199 ALOGW("Multiple calls to setTextureAtlas!"); 200 delete map; 201 return; 202 } 203 204 mAtlasBuffer = buffer; 205 mAtlasMap = map; 206 mAtlasMapSize = mapSize; 207 208 if (hasEglContext()) { 209 initAtlas(); 210 } 211} 212 213void EglManager::initAtlas() { 214 if (mAtlasBuffer.get()) { 215 mRenderThread.renderState().assetAtlas().init(mAtlasBuffer, 216 mAtlasMap, mAtlasMapSize); 217 } 218} 219 220void EglManager::createPBufferSurface() { 221 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 222 "usePBufferSurface() called on uninitialized GlobalContext!"); 223 224 if (mPBufferSurface == EGL_NO_SURFACE) { 225 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; 226 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); 227 } 228} 229 230EGLSurface EglManager::createSurface(EGLNativeWindowType window) { 231 initialize(); 232 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, nullptr); 233 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 234 "Failed to create EGLSurface for window %p, eglErr = %s", 235 (void*) window, egl_error_str()); 236 237 if (mSwapBehavior != SwapBehavior::Preserved) { 238 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE, 239 "Failed to set swap behavior to destroyed for window %p, eglErr = %s", 240 (void*) window, egl_error_str()); 241 } 242 243 return surface; 244} 245 246void EglManager::destroySurface(EGLSurface surface) { 247 if (isCurrent(surface)) { 248 makeCurrent(EGL_NO_SURFACE); 249 } 250 if (!eglDestroySurface(mEglDisplay, surface)) { 251 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); 252 } 253} 254 255void EglManager::destroy() { 256 if (mEglDisplay == EGL_NO_DISPLAY) return; 257 258 mRenderThread.renderState().onGLContextDestroyed(); 259 eglDestroyContext(mEglDisplay, mEglContext); 260 eglDestroySurface(mEglDisplay, mPBufferSurface); 261 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 262 eglTerminate(mEglDisplay); 263 eglReleaseThread(); 264 265 mEglDisplay = EGL_NO_DISPLAY; 266 mEglContext = EGL_NO_CONTEXT; 267 mPBufferSurface = EGL_NO_SURFACE; 268 mCurrentSurface = EGL_NO_SURFACE; 269} 270 271bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) { 272 if (isCurrent(surface)) return false; 273 274 if (surface == EGL_NO_SURFACE) { 275 // Ensure we always have a valid surface & context 276 surface = mPBufferSurface; 277 } 278 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { 279 if (errOut) { 280 *errOut = eglGetError(); 281 ALOGW("Failed to make current on surface %p, error=%s", 282 (void*)surface, egl_error_str(*errOut)); 283 } else { 284 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", 285 (void*)surface, egl_error_str()); 286 } 287 } 288 mCurrentSurface = surface; 289 return true; 290} 291 292EGLint EglManager::queryBufferAge(EGLSurface surface) { 293 switch (mSwapBehavior) { 294 case SwapBehavior::Discard: 295 return 0; 296 case SwapBehavior::Preserved: 297 return 1; 298 case SwapBehavior::BufferAge: 299 EGLint bufferAge; 300 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge); 301 return bufferAge; 302 } 303 return 0; 304} 305 306Frame EglManager::beginFrame(EGLSurface surface) { 307 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 308 "Tried to beginFrame on EGL_NO_SURFACE!"); 309 makeCurrent(surface); 310 Frame frame; 311 frame.mSurface = surface; 312 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth); 313 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight); 314 frame.mBufferAge = queryBufferAge(surface); 315 eglBeginFrame(mEglDisplay, surface); 316 return frame; 317} 318 319void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) { 320#ifdef EGL_KHR_partial_update 321 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) { 322 EGLint rects[4]; 323 frame.map(dirty, rects); 324 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) { 325 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s", 326 (void*)frame.mSurface, egl_error_str()); 327 } 328 } 329#endif 330} 331 332bool EglManager::damageRequiresSwap() { 333 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge; 334} 335 336bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) { 337 338 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { 339 ATRACE_NAME("Finishing GPU work"); 340 fence(); 341 } 342 343 EGLint rects[4]; 344 frame.map(screenDirty, rects); 345 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, 346 screenDirty.isEmpty() ? 0 : 1); 347 348 EGLint err = eglGetError(); 349 if (CC_LIKELY(err == EGL_SUCCESS)) { 350 return true; 351 } 352 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) { 353 // For some reason our surface was destroyed out from under us 354 // This really shouldn't happen, but if it does we can recover easily 355 // by just not trying to use the surface anymore 356 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", 357 err, frame.mSurface); 358 return false; 359 } 360 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", 361 err, egl_error_str(err)); 362 // Impossible to hit this, but the compiler doesn't know that 363 return false; 364} 365 366void EglManager::fence() { 367 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL); 368 eglClientWaitSyncKHR(mEglDisplay, fence, 369 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR); 370 eglDestroySyncKHR(mEglDisplay, fence); 371} 372 373bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) { 374 if (mSwapBehavior != SwapBehavior::Preserved) return false; 375 376 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, 377 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED); 378 if (!preserved) { 379 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", 380 (void*) surface, egl_error_str()); 381 // Maybe it's already set? 382 EGLint swapBehavior; 383 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) { 384 preserved = (swapBehavior == EGL_BUFFER_PRESERVED); 385 } else { 386 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", 387 (void*) surface, egl_error_str()); 388 } 389 } 390 391 return preserved; 392} 393 394} /* namespace renderthread */ 395} /* namespace uirenderer */ 396} /* namespace android */ 397