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