John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [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 | |
| 17 | #define LOG_TAG "CanvasContext" |
| 18 | |
| 19 | #include "CanvasContext.h" |
| 20 | |
| 21 | #include <cutils/properties.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 22 | #include <private/hwui/DrawGlInfo.h> |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 23 | #include <strings.h> |
| 24 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 25 | #include "RenderThread.h" |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 26 | #include "../Caches.h" |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 27 | #include "../DeferredLayerUpdater.h" |
| 28 | #include "../LayerRenderer.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 29 | #include "../OpenGLRenderer.h" |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 30 | #include "../Stencil.h" |
| 31 | |
| 32 | #define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions" |
| 33 | #define GLES_VERSION 2 |
| 34 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 35 | #ifdef USE_OPENGL_RENDERER |
| 36 | // Android-specific addition that is used to show when frames began in systrace |
| 37 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 38 | #endif |
| 39 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace uirenderer { |
| 42 | namespace renderthread { |
| 43 | |
| 44 | #define ERROR_CASE(x) case x: return #x; |
| 45 | static const char* egl_error_str(EGLint error) { |
| 46 | switch (error) { |
| 47 | ERROR_CASE(EGL_SUCCESS) |
| 48 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 49 | ERROR_CASE(EGL_BAD_ACCESS) |
| 50 | ERROR_CASE(EGL_BAD_ALLOC) |
| 51 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 52 | ERROR_CASE(EGL_BAD_CONFIG) |
| 53 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 54 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 55 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 56 | ERROR_CASE(EGL_BAD_MATCH) |
| 57 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 58 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 59 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 60 | ERROR_CASE(EGL_BAD_SURFACE) |
| 61 | ERROR_CASE(EGL_CONTEXT_LOST) |
| 62 | default: |
| 63 | return "Unknown error"; |
| 64 | } |
| 65 | } |
| 66 | static const char* egl_error_str() { |
| 67 | return egl_error_str(eglGetError()); |
| 68 | } |
| 69 | |
| 70 | static bool load_dirty_regions_property() { |
| 71 | char buf[PROPERTY_VALUE_MAX]; |
| 72 | int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true"); |
| 73 | return !strncasecmp("true", buf, len); |
| 74 | } |
| 75 | |
| 76 | // This class contains the shared global EGL objects, such as EGLDisplay |
| 77 | // and EGLConfig, which are re-used by CanvasContext |
| 78 | class GlobalContext { |
| 79 | public: |
| 80 | static GlobalContext* get(); |
| 81 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 82 | // Returns true on success, false on failure |
| 83 | void initialize(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 84 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 85 | bool hasContext(); |
| 86 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 87 | void usePBufferSurface(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 88 | EGLSurface createSurface(EGLNativeWindowType window); |
| 89 | void destroySurface(EGLSurface surface); |
| 90 | |
| 91 | void destroy(); |
| 92 | |
| 93 | bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 94 | void makeCurrent(EGLSurface surface); |
| 95 | void beginFrame(EGLSurface surface, EGLint* width, EGLint* height); |
| 96 | void swapBuffers(EGLSurface surface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 97 | |
| 98 | bool enableDirtyRegions(EGLSurface surface); |
| 99 | |
| 100 | private: |
| 101 | GlobalContext(); |
| 102 | // GlobalContext is never destroyed, method is purposely not implemented |
| 103 | ~GlobalContext(); |
| 104 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 105 | void loadConfig(); |
| 106 | void createContext(); |
| 107 | void initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 108 | |
| 109 | static GlobalContext* sContext; |
| 110 | |
| 111 | EGLDisplay mEglDisplay; |
| 112 | EGLConfig mEglConfig; |
| 113 | EGLContext mEglContext; |
| 114 | EGLSurface mPBufferSurface; |
| 115 | |
| 116 | const bool mRequestDirtyRegions; |
| 117 | bool mCanSetDirtyRegions; |
| 118 | |
| 119 | EGLSurface mCurrentSurface; |
| 120 | }; |
| 121 | |
| 122 | GlobalContext* GlobalContext::sContext = 0; |
| 123 | |
| 124 | GlobalContext* GlobalContext::get() { |
| 125 | if (!sContext) { |
| 126 | sContext = new GlobalContext(); |
| 127 | } |
| 128 | return sContext; |
| 129 | } |
| 130 | |
| 131 | GlobalContext::GlobalContext() |
| 132 | : mEglDisplay(EGL_NO_DISPLAY) |
| 133 | , mEglConfig(0) |
| 134 | , mEglContext(EGL_NO_CONTEXT) |
| 135 | , mPBufferSurface(EGL_NO_SURFACE) |
| 136 | , mRequestDirtyRegions(load_dirty_regions_property()) |
| 137 | , mCurrentSurface(EGL_NO_SURFACE) { |
| 138 | mCanSetDirtyRegions = mRequestDirtyRegions; |
| 139 | ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false"); |
| 140 | } |
| 141 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 142 | void GlobalContext::initialize() { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 143 | if (hasContext()) return; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 144 | |
| 145 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 146 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 147 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 148 | |
| 149 | EGLint major, minor; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 150 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 151 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 152 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 153 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 154 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 155 | loadConfig(); |
| 156 | createContext(); |
| 157 | usePBufferSurface(); |
| 158 | Caches::getInstance().init(); |
| 159 | initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 160 | } |
| 161 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 162 | bool GlobalContext::hasContext() { |
| 163 | return mEglDisplay != EGL_NO_DISPLAY; |
| 164 | } |
| 165 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 166 | void GlobalContext::loadConfig() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 167 | EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 168 | EGLint attribs[] = { |
| 169 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 170 | EGL_RED_SIZE, 8, |
| 171 | EGL_GREEN_SIZE, 8, |
| 172 | EGL_BLUE_SIZE, 8, |
| 173 | EGL_ALPHA_SIZE, 8, |
| 174 | EGL_DEPTH_SIZE, 0, |
| 175 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 176 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 177 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 178 | EGL_NONE |
| 179 | }; |
| 180 | |
| 181 | EGLint num_configs = 1; |
| 182 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 183 | || num_configs != 1) { |
| 184 | // Failed to get a valid config |
| 185 | if (mCanSetDirtyRegions) { |
| 186 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 187 | // Try again without dirty regions enabled |
| 188 | mCanSetDirtyRegions = false; |
| 189 | loadConfig(); |
| 190 | } else { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 191 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 194 | } |
| 195 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 196 | void GlobalContext::createContext() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 197 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 198 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 199 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 200 | "Failed to create context, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 203 | void GlobalContext::initAtlas() { |
| 204 | // TODO implement |
| 205 | // For now just run without an atlas |
| 206 | } |
| 207 | |
| 208 | void GlobalContext::usePBufferSurface() { |
| 209 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 210 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 211 | |
| 212 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 213 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 214 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 215 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 216 | makeCurrent(mPBufferSurface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | EGLSurface GlobalContext::createSurface(EGLNativeWindowType window) { |
| 220 | initialize(); |
| 221 | return eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL); |
| 222 | } |
| 223 | |
| 224 | void GlobalContext::destroySurface(EGLSurface surface) { |
| 225 | if (isCurrent(surface)) { |
| 226 | makeCurrent(EGL_NO_SURFACE); |
| 227 | } |
| 228 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 229 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void GlobalContext::destroy() { |
| 234 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 235 | |
| 236 | usePBufferSurface(); |
| 237 | if (Caches::hasInstance()) { |
| 238 | Caches::getInstance().terminate(); |
| 239 | } |
| 240 | |
| 241 | eglDestroyContext(mEglDisplay, mEglContext); |
| 242 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 243 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 244 | eglTerminate(mEglDisplay); |
| 245 | eglReleaseThread(); |
| 246 | |
| 247 | mEglDisplay = EGL_NO_DISPLAY; |
| 248 | mEglContext = EGL_NO_CONTEXT; |
| 249 | mPBufferSurface = EGL_NO_SURFACE; |
| 250 | mCurrentSurface = EGL_NO_SURFACE; |
| 251 | } |
| 252 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 253 | void GlobalContext::makeCurrent(EGLSurface surface) { |
| 254 | if (isCurrent(surface)) return; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 255 | |
| 256 | if (surface == EGL_NO_SURFACE) { |
| 257 | // If we are setting EGL_NO_SURFACE we don't care about any of the potential |
| 258 | // return errors, which would only happen if mEglDisplay had already been |
| 259 | // destroyed in which case the current context is already NO_CONTEXT |
| 260 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 261 | } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 262 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 263 | (void*)surface, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 264 | } |
| 265 | mCurrentSurface = surface; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 266 | } |
| 267 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 268 | void GlobalContext::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 269 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 270 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 271 | makeCurrent(surface); |
| 272 | if (width) { |
| 273 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 274 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 275 | if (height) { |
| 276 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 277 | } |
| 278 | eglBeginFrame(mEglDisplay, surface); |
| 279 | } |
| 280 | |
| 281 | void GlobalContext::swapBuffers(EGLSurface surface) { |
| 282 | eglSwapBuffers(mEglDisplay, surface); |
| 283 | EGLint err = eglGetError(); |
| 284 | // TODO: Check whether we need to special case EGL_CONTEXT_LOST |
| 285 | LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS, |
| 286 | "Encountered EGL error %d %s during rendering", err, egl_error_str(err)); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool GlobalContext::enableDirtyRegions(EGLSurface surface) { |
| 290 | if (!mRequestDirtyRegions) return false; |
| 291 | |
| 292 | if (mCanSetDirtyRegions) { |
| 293 | if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) { |
| 294 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 295 | (void*) surface, egl_error_str()); |
| 296 | return false; |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | // Perhaps it is already enabled? |
| 301 | EGLint value; |
| 302 | if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) { |
| 303 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 304 | (void*) surface, egl_error_str()); |
| 305 | return false; |
| 306 | } |
| 307 | return value == EGL_BUFFER_PRESERVED; |
| 308 | } |
| 309 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 310 | CanvasContext::CanvasContext(bool translucent) |
| 311 | : mRenderThread(RenderThread::getInstance()) |
| 312 | , mEglSurface(EGL_NO_SURFACE) |
| 313 | , mDirtyRegionsEnabled(false) |
| 314 | , mOpaque(!translucent) |
| 315 | , mCanvas(0) |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 316 | , mHaveNewSurface(false) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 317 | mGlobalContext = GlobalContext::get(); |
| 318 | } |
| 319 | |
| 320 | CanvasContext::~CanvasContext() { |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 321 | destroyCanvasAndSurface(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 322 | } |
| 323 | |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 324 | void CanvasContext::destroyCanvasAndSurface() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 325 | if (mCanvas) { |
| 326 | delete mCanvas; |
| 327 | mCanvas = 0; |
| 328 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 329 | setSurface(NULL); |
| 330 | } |
| 331 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 332 | void CanvasContext::setSurface(EGLNativeWindowType window) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 333 | if (mEglSurface != EGL_NO_SURFACE) { |
| 334 | mGlobalContext->destroySurface(mEglSurface); |
| 335 | mEglSurface = EGL_NO_SURFACE; |
| 336 | } |
| 337 | |
| 338 | if (window) { |
| 339 | mEglSurface = mGlobalContext->createSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 340 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 341 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 342 | (void*) window, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (mEglSurface != EGL_NO_SURFACE) { |
| 346 | mDirtyRegionsEnabled = mGlobalContext->enableDirtyRegions(mEglSurface); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 347 | mGlobalContext->makeCurrent(mEglSurface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 348 | mHaveNewSurface = true; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 349 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 350 | } |
| 351 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 352 | void CanvasContext::swapBuffers() { |
| 353 | mGlobalContext->swapBuffers(mEglSurface); |
| 354 | mHaveNewSurface = false; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 355 | } |
| 356 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 357 | void CanvasContext::requireSurface() { |
| 358 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 359 | "requireSurface() called but no surface set!"); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 360 | mGlobalContext->makeCurrent(mEglSurface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 361 | } |
| 362 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 363 | bool CanvasContext::initialize(EGLNativeWindowType window) { |
| 364 | if (mCanvas) return false; |
| 365 | setSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 366 | mCanvas = new OpenGLRenderer(); |
| 367 | mCanvas->initProperties(); |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void CanvasContext::updateSurface(EGLNativeWindowType window) { |
| 372 | setSurface(window); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void CanvasContext::pauseSurface(EGLNativeWindowType window) { |
| 376 | // TODO: For now we just need a fence, in the future suspend any animations |
| 377 | // and such to prevent from trying to render into this surface |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void CanvasContext::setup(int width, int height) { |
| 381 | if (!mCanvas) return; |
| 382 | mCanvas->setViewport(width, height); |
| 383 | } |
| 384 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 385 | void CanvasContext::makeCurrent() { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 386 | mGlobalContext->makeCurrent(mEglSurface); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters, |
| 390 | TreeInfo& info) { |
| 391 | LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot process layer updates without a canvas!"); |
| 392 | makeCurrent(); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 393 | for (size_t i = 0; i < layerUpdaters->size(); i++) { |
| 394 | DeferredLayerUpdater* update = layerUpdaters->itemAt(i); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 395 | bool success = update->apply(info); |
| 396 | LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!"); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 397 | if (update->backingLayer()->deferredUpdateScheduled) { |
| 398 | mCanvas->pushLayerUpdate(update->backingLayer()); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 403 | void CanvasContext::drawDisplayList(RenderNode* displayList, Rect* dirty) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 404 | LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE, |
| 405 | "drawDisplayList called on a context with no canvas or surface!"); |
| 406 | |
| 407 | EGLint width, height; |
| 408 | mGlobalContext->beginFrame(mEglSurface, &width, &height); |
| 409 | if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) { |
| 410 | mCanvas->setViewport(width, height); |
| 411 | dirty = NULL; |
| 412 | } else if (!mDirtyRegionsEnabled || mHaveNewSurface) { |
| 413 | dirty = NULL; |
| 414 | } |
| 415 | |
| 416 | status_t status; |
| 417 | if (dirty) { |
| 418 | status = mCanvas->prepareDirty(dirty->left, dirty->top, |
| 419 | dirty->right, dirty->bottom, mOpaque); |
| 420 | } else { |
| 421 | status = mCanvas->prepare(mOpaque); |
| 422 | } |
| 423 | |
| 424 | Rect outBounds; |
| 425 | status |= mCanvas->drawDisplayList(displayList, outBounds); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 426 | |
| 427 | // TODO: Draw debug info |
| 428 | // TODO: Performance tracking |
| 429 | |
| 430 | mCanvas->finish(); |
| 431 | |
| 432 | if (status & DrawGlInfo::kStatusDrew) { |
| 433 | swapBuffers(); |
| 434 | } |
| 435 | } |
| 436 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 437 | void CanvasContext::invokeFunctor(Functor* functor) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 438 | ATRACE_CALL(); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 439 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 440 | if (mGlobalContext->hasContext()) { |
| 441 | requireGlContext(); |
| 442 | mode = DrawGlInfo::kModeProcess; |
| 443 | } |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 444 | // TODO: Remove the dummy info in the future |
| 445 | DrawGlInfo dummyInfo; |
| 446 | memset(&dummyInfo, 0, sizeof(DrawGlInfo)); |
| 447 | (*functor)(mode, &dummyInfo); |
John Reck | 6f07a0d | 2014-04-16 21:31:25 -0700 | [diff] [blame^] | 448 | |
| 449 | if (mCanvas) { |
| 450 | mCanvas->resume(); |
| 451 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 452 | } |
| 453 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 454 | bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 455 | requireGlContext(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 456 | TreeInfo info; |
| 457 | layer->apply(info); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 458 | return LayerRenderer::copyLayer(layer->backingLayer(), bitmap); |
| 459 | } |
| 460 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 461 | void CanvasContext::runWithGlContext(RenderTask* task) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 462 | requireGlContext(); |
| 463 | task->run(); |
| 464 | } |
| 465 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 466 | Layer* CanvasContext::createRenderLayer(int width, int height) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 467 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 468 | return LayerRenderer::createRenderLayer(width, height); |
| 469 | } |
| 470 | |
| 471 | Layer* CanvasContext::createTextureLayer() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 472 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 473 | return LayerRenderer::createTextureLayer(); |
| 474 | } |
| 475 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 476 | void CanvasContext::requireGlContext() { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 477 | if (mEglSurface != EGL_NO_SURFACE) { |
| 478 | mGlobalContext->makeCurrent(mEglSurface); |
| 479 | } else { |
| 480 | mGlobalContext->usePBufferSurface(); |
| 481 | } |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 482 | } |
| 483 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 484 | } /* namespace renderthread */ |
| 485 | } /* namespace uirenderer */ |
| 486 | } /* namespace android */ |