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