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