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 | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 94 | // Returns true if the current surface changed, false if it was already current |
| 95 | bool makeCurrent(EGLSurface surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 96 | void beginFrame(EGLSurface surface, EGLint* width, EGLint* height); |
| 97 | void swapBuffers(EGLSurface surface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 98 | |
| 99 | bool enableDirtyRegions(EGLSurface surface); |
| 100 | |
| 101 | private: |
| 102 | GlobalContext(); |
| 103 | // GlobalContext is never destroyed, method is purposely not implemented |
| 104 | ~GlobalContext(); |
| 105 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 106 | void loadConfig(); |
| 107 | void createContext(); |
| 108 | void initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 109 | |
| 110 | static GlobalContext* sContext; |
| 111 | |
| 112 | EGLDisplay mEglDisplay; |
| 113 | EGLConfig mEglConfig; |
| 114 | EGLContext mEglContext; |
| 115 | EGLSurface mPBufferSurface; |
| 116 | |
| 117 | const bool mRequestDirtyRegions; |
| 118 | bool mCanSetDirtyRegions; |
| 119 | |
| 120 | EGLSurface mCurrentSurface; |
| 121 | }; |
| 122 | |
| 123 | GlobalContext* GlobalContext::sContext = 0; |
| 124 | |
| 125 | GlobalContext* GlobalContext::get() { |
| 126 | if (!sContext) { |
| 127 | sContext = new GlobalContext(); |
| 128 | } |
| 129 | return sContext; |
| 130 | } |
| 131 | |
| 132 | GlobalContext::GlobalContext() |
| 133 | : mEglDisplay(EGL_NO_DISPLAY) |
| 134 | , mEglConfig(0) |
| 135 | , mEglContext(EGL_NO_CONTEXT) |
| 136 | , mPBufferSurface(EGL_NO_SURFACE) |
| 137 | , mRequestDirtyRegions(load_dirty_regions_property()) |
| 138 | , mCurrentSurface(EGL_NO_SURFACE) { |
| 139 | mCanSetDirtyRegions = mRequestDirtyRegions; |
| 140 | ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false"); |
| 141 | } |
| 142 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 143 | void GlobalContext::initialize() { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 144 | if (hasContext()) return; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 145 | |
| 146 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 147 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 148 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 149 | |
| 150 | EGLint major, minor; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 151 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 152 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 153 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 154 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 155 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 156 | loadConfig(); |
| 157 | createContext(); |
| 158 | usePBufferSurface(); |
| 159 | Caches::getInstance().init(); |
| 160 | initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 161 | } |
| 162 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 163 | bool GlobalContext::hasContext() { |
| 164 | return mEglDisplay != EGL_NO_DISPLAY; |
| 165 | } |
| 166 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 167 | void GlobalContext::loadConfig() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 168 | EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 169 | EGLint attribs[] = { |
| 170 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 171 | EGL_RED_SIZE, 8, |
| 172 | EGL_GREEN_SIZE, 8, |
| 173 | EGL_BLUE_SIZE, 8, |
| 174 | EGL_ALPHA_SIZE, 8, |
| 175 | EGL_DEPTH_SIZE, 0, |
| 176 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 177 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 178 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 179 | EGL_NONE |
| 180 | }; |
| 181 | |
| 182 | EGLint num_configs = 1; |
| 183 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 184 | || num_configs != 1) { |
| 185 | // Failed to get a valid config |
| 186 | if (mCanSetDirtyRegions) { |
| 187 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 188 | // Try again without dirty regions enabled |
| 189 | mCanSetDirtyRegions = false; |
| 190 | loadConfig(); |
| 191 | } else { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 192 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 195 | } |
| 196 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 197 | void GlobalContext::createContext() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 198 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 199 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 200 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 201 | "Failed to create context, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 202 | } |
| 203 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 204 | void GlobalContext::initAtlas() { |
| 205 | // TODO implement |
| 206 | // For now just run without an atlas |
| 207 | } |
| 208 | |
| 209 | void GlobalContext::usePBufferSurface() { |
| 210 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 211 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 212 | |
| 213 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 214 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 215 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 216 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 217 | makeCurrent(mPBufferSurface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | EGLSurface GlobalContext::createSurface(EGLNativeWindowType window) { |
| 221 | initialize(); |
| 222 | return eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL); |
| 223 | } |
| 224 | |
| 225 | void GlobalContext::destroySurface(EGLSurface surface) { |
| 226 | if (isCurrent(surface)) { |
| 227 | makeCurrent(EGL_NO_SURFACE); |
| 228 | } |
| 229 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 230 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void GlobalContext::destroy() { |
| 235 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 236 | |
| 237 | usePBufferSurface(); |
| 238 | if (Caches::hasInstance()) { |
| 239 | Caches::getInstance().terminate(); |
| 240 | } |
| 241 | |
| 242 | eglDestroyContext(mEglDisplay, mEglContext); |
| 243 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 244 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 245 | eglTerminate(mEglDisplay); |
| 246 | eglReleaseThread(); |
| 247 | |
| 248 | mEglDisplay = EGL_NO_DISPLAY; |
| 249 | mEglContext = EGL_NO_CONTEXT; |
| 250 | mPBufferSurface = EGL_NO_SURFACE; |
| 251 | mCurrentSurface = EGL_NO_SURFACE; |
| 252 | } |
| 253 | |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 254 | bool GlobalContext::makeCurrent(EGLSurface surface) { |
| 255 | if (isCurrent(surface)) return false; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 256 | |
| 257 | if (surface == EGL_NO_SURFACE) { |
| 258 | // If we are setting EGL_NO_SURFACE we don't care about any of the potential |
| 259 | // return errors, which would only happen if mEglDisplay had already been |
| 260 | // destroyed in which case the current context is already NO_CONTEXT |
| 261 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 262 | } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 263 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 264 | (void*)surface, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 265 | } |
| 266 | mCurrentSurface = surface; |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 267 | return true; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 268 | } |
| 269 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 270 | void GlobalContext::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 271 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 272 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 273 | makeCurrent(surface); |
| 274 | if (width) { |
| 275 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 276 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 277 | if (height) { |
| 278 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 279 | } |
| 280 | eglBeginFrame(mEglDisplay, surface); |
| 281 | } |
| 282 | |
| 283 | void GlobalContext::swapBuffers(EGLSurface surface) { |
| 284 | eglSwapBuffers(mEglDisplay, surface); |
| 285 | EGLint err = eglGetError(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 286 | LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS, |
| 287 | "Encountered EGL error %d %s during rendering", err, egl_error_str(err)); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | bool GlobalContext::enableDirtyRegions(EGLSurface surface) { |
| 291 | if (!mRequestDirtyRegions) return false; |
| 292 | |
| 293 | if (mCanSetDirtyRegions) { |
| 294 | if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) { |
| 295 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 296 | (void*) surface, egl_error_str()); |
| 297 | return false; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | // Perhaps it is already enabled? |
| 302 | EGLint value; |
| 303 | if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) { |
| 304 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 305 | (void*) surface, egl_error_str()); |
| 306 | return false; |
| 307 | } |
| 308 | return value == EGL_BUFFER_PRESERVED; |
| 309 | } |
| 310 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 311 | CanvasContext::CanvasContext(bool translucent, RenderNode* rootRenderNode) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 312 | : mRenderThread(RenderThread::getInstance()) |
| 313 | , mEglSurface(EGL_NO_SURFACE) |
| 314 | , mDirtyRegionsEnabled(false) |
| 315 | , mOpaque(!translucent) |
| 316 | , mCanvas(0) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 317 | , mHaveNewSurface(false) |
| 318 | , mRootRenderNode(rootRenderNode) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 319 | mGlobalContext = GlobalContext::get(); |
| 320 | } |
| 321 | |
| 322 | CanvasContext::~CanvasContext() { |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 323 | destroyCanvasAndSurface(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 324 | mRenderThread.removeFrameCallback(this); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 325 | } |
| 326 | |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 327 | void CanvasContext::destroyCanvasAndSurface() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 328 | if (mCanvas) { |
| 329 | delete mCanvas; |
| 330 | mCanvas = 0; |
| 331 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 332 | setSurface(NULL); |
| 333 | } |
| 334 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 335 | void CanvasContext::setSurface(EGLNativeWindowType window) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 336 | if (mEglSurface != EGL_NO_SURFACE) { |
| 337 | mGlobalContext->destroySurface(mEglSurface); |
| 338 | mEglSurface = EGL_NO_SURFACE; |
| 339 | } |
| 340 | |
| 341 | if (window) { |
| 342 | mEglSurface = mGlobalContext->createSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 343 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 344 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 345 | (void*) window, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (mEglSurface != EGL_NO_SURFACE) { |
| 349 | mDirtyRegionsEnabled = mGlobalContext->enableDirtyRegions(mEglSurface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 350 | mHaveNewSurface = true; |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 351 | makeCurrent(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 352 | } |
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 | void CanvasContext::swapBuffers() { |
| 356 | mGlobalContext->swapBuffers(mEglSurface); |
| 357 | mHaveNewSurface = false; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 358 | } |
| 359 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 360 | void CanvasContext::requireSurface() { |
| 361 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 362 | "requireSurface() called but no surface set!"); |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 363 | makeCurrent(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 364 | } |
| 365 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 366 | bool CanvasContext::initialize(EGLNativeWindowType window) { |
| 367 | if (mCanvas) return false; |
| 368 | setSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 369 | mCanvas = new OpenGLRenderer(); |
| 370 | mCanvas->initProperties(); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void CanvasContext::updateSurface(EGLNativeWindowType window) { |
| 375 | setSurface(window); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void CanvasContext::pauseSurface(EGLNativeWindowType window) { |
| 379 | // TODO: For now we just need a fence, in the future suspend any animations |
| 380 | // and such to prevent from trying to render into this surface |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void CanvasContext::setup(int width, int height) { |
| 384 | if (!mCanvas) return; |
| 385 | mCanvas->setViewport(width, height); |
| 386 | } |
| 387 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 388 | void CanvasContext::makeCurrent() { |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 389 | // TODO: Figure out why this workaround is needed, see b/13913604 |
| 390 | // In the meantime this matches the behavior of GLRenderer, so it is not a regression |
| 391 | mHaveNewSurface |= mGlobalContext->makeCurrent(mEglSurface); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters, |
| 395 | TreeInfo& info) { |
| 396 | LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot process layer updates without a canvas!"); |
| 397 | makeCurrent(); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 398 | for (size_t i = 0; i < layerUpdaters->size(); i++) { |
| 399 | DeferredLayerUpdater* update = layerUpdaters->itemAt(i); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 400 | bool success = update->apply(info); |
| 401 | LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!"); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 402 | if (update->backingLayer()->deferredUpdateScheduled) { |
| 403 | mCanvas->pushLayerUpdate(update->backingLayer()); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 408 | void CanvasContext::prepareTree(TreeInfo& info) { |
| 409 | mRootRenderNode->prepareTree(info); |
| 410 | |
| 411 | if (info.hasAnimations && !info.hasFunctors) { |
| 412 | // TODO: Functors |
| 413 | mRenderThread.postFrameCallback(this); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void CanvasContext::draw(Rect* dirty) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 418 | LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE, |
| 419 | "drawDisplayList called on a context with no canvas or surface!"); |
| 420 | |
| 421 | EGLint width, height; |
| 422 | mGlobalContext->beginFrame(mEglSurface, &width, &height); |
| 423 | if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) { |
| 424 | mCanvas->setViewport(width, height); |
| 425 | dirty = NULL; |
| 426 | } else if (!mDirtyRegionsEnabled || mHaveNewSurface) { |
| 427 | dirty = NULL; |
| 428 | } |
| 429 | |
| 430 | status_t status; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 431 | if (dirty && !dirty->isEmpty()) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 432 | status = mCanvas->prepareDirty(dirty->left, dirty->top, |
| 433 | dirty->right, dirty->bottom, mOpaque); |
| 434 | } else { |
| 435 | status = mCanvas->prepare(mOpaque); |
| 436 | } |
| 437 | |
| 438 | Rect outBounds; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 439 | status |= mCanvas->drawDisplayList(mRootRenderNode.get(), outBounds); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 440 | |
| 441 | // TODO: Draw debug info |
| 442 | // TODO: Performance tracking |
| 443 | |
| 444 | mCanvas->finish(); |
| 445 | |
| 446 | if (status & DrawGlInfo::kStatusDrew) { |
| 447 | swapBuffers(); |
| 448 | } |
| 449 | } |
| 450 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame^] | 451 | // Called by choreographer to do an RT-driven animation |
| 452 | void CanvasContext::doFrame(nsecs_t frameTimeNanos) { |
| 453 | ATRACE_CALL(); |
| 454 | |
| 455 | TreeInfo info; |
| 456 | info.evaluateAnimations = true; |
| 457 | info.frameTimeMs = nanoseconds_to_milliseconds(frameTimeNanos); |
| 458 | info.performStagingPush = false; |
| 459 | info.prepareTextures = false; |
| 460 | |
| 461 | prepareTree(info); |
| 462 | draw(NULL); |
| 463 | } |
| 464 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 465 | void CanvasContext::invokeFunctor(Functor* functor) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 466 | ATRACE_CALL(); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 467 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 468 | if (mGlobalContext->hasContext()) { |
| 469 | requireGlContext(); |
| 470 | mode = DrawGlInfo::kModeProcess; |
| 471 | } |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 472 | // TODO: Remove the dummy info in the future |
| 473 | DrawGlInfo dummyInfo; |
| 474 | memset(&dummyInfo, 0, sizeof(DrawGlInfo)); |
| 475 | (*functor)(mode, &dummyInfo); |
John Reck | 6f07a0d | 2014-04-16 21:31:25 -0700 | [diff] [blame] | 476 | |
| 477 | if (mCanvas) { |
| 478 | mCanvas->resume(); |
| 479 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 480 | } |
| 481 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 482 | bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 483 | requireGlContext(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 484 | TreeInfo info; |
| 485 | layer->apply(info); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 486 | return LayerRenderer::copyLayer(layer->backingLayer(), bitmap); |
| 487 | } |
| 488 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 489 | void CanvasContext::runWithGlContext(RenderTask* task) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 490 | requireGlContext(); |
| 491 | task->run(); |
| 492 | } |
| 493 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 494 | Layer* CanvasContext::createRenderLayer(int width, int height) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 495 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 496 | return LayerRenderer::createRenderLayer(width, height); |
| 497 | } |
| 498 | |
| 499 | Layer* CanvasContext::createTextureLayer() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 500 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 501 | return LayerRenderer::createTextureLayer(); |
| 502 | } |
| 503 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 504 | void CanvasContext::requireGlContext() { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 505 | if (mEglSurface != EGL_NO_SURFACE) { |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 506 | makeCurrent(); |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 507 | } else { |
| 508 | mGlobalContext->usePBufferSurface(); |
| 509 | } |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 510 | } |
| 511 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 512 | } /* namespace renderthread */ |
| 513 | } /* namespace uirenderer */ |
| 514 | } /* namespace android */ |