CanvasContext.cpp revision 0d4ab958c11801f5453e999f930416c87c63d100
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 "CanvasContext.h" 18 19#include <algorithm> 20#include <private/hwui/DrawGlInfo.h> 21#include <strings.h> 22 23#include "EglManager.h" 24#include "RenderThread.h" 25#include "../AnimationContext.h" 26#include "../Caches.h" 27#include "../DeferredLayerUpdater.h" 28#include "../RenderState.h" 29#include "../LayerRenderer.h" 30#include "../OpenGLRenderer.h" 31#include "../Stencil.h" 32 33#define TRIM_MEMORY_COMPLETE 80 34#define TRIM_MEMORY_UI_HIDDEN 20 35 36namespace android { 37namespace uirenderer { 38namespace renderthread { 39 40CanvasContext::CanvasContext(RenderThread& thread, bool translucent, 41 RenderNode* rootRenderNode, IContextFactory* contextFactory) 42 : mRenderThread(thread) 43 , mEglManager(thread.eglManager()) 44 , mEglSurface(EGL_NO_SURFACE) 45 , mBufferPreserved(false) 46 , mSwapBehavior(kSwap_default) 47 , mOpaque(!translucent) 48 , mCanvas(NULL) 49 , mHaveNewSurface(false) 50 , mRootRenderNode(rootRenderNode) { 51 mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord()); 52 mRenderThread.renderState().registerCanvasContext(this); 53} 54 55CanvasContext::~CanvasContext() { 56 destroy(); 57 delete mAnimationContext; 58 mRenderThread.renderState().unregisterCanvasContext(this); 59} 60 61void CanvasContext::destroy() { 62 stopDrawing(); 63 setSurface(NULL); 64 mEglManager.usePBufferSurface(); 65 freePrefetechedLayers(); 66 destroyHardwareResources(); 67 mAnimationContext->destroy(); 68 if (mCanvas) { 69 delete mCanvas; 70 mCanvas = 0; 71 } 72} 73 74void CanvasContext::setSurface(ANativeWindow* window) { 75 ATRACE_CALL(); 76 77 mNativeWindow = window; 78 79 if (mEglSurface != EGL_NO_SURFACE) { 80 mEglManager.destroySurface(mEglSurface); 81 mEglSurface = EGL_NO_SURFACE; 82 } 83 84 if (window) { 85 mEglSurface = mEglManager.createSurface(window); 86 } 87 88 if (mEglSurface != EGL_NO_SURFACE) { 89 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer); 90 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); 91 mHaveNewSurface = true; 92 makeCurrent(); 93 } else { 94 mRenderThread.removeFrameCallback(this); 95 } 96} 97 98void CanvasContext::swapBuffers() { 99 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) { 100 setSurface(NULL); 101 } 102 mHaveNewSurface = false; 103} 104 105void CanvasContext::requireSurface() { 106 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, 107 "requireSurface() called but no surface set!"); 108 makeCurrent(); 109} 110 111void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) { 112 mSwapBehavior = swapBehavior; 113} 114 115bool CanvasContext::initialize(ANativeWindow* window) { 116 setSurface(window); 117 if (mCanvas) return false; 118 mCanvas = new OpenGLRenderer(mRenderThread.renderState()); 119 mCanvas->initProperties(); 120 return true; 121} 122 123void CanvasContext::updateSurface(ANativeWindow* window) { 124 setSurface(window); 125} 126 127void CanvasContext::pauseSurface(ANativeWindow* /* window */) { 128 stopDrawing(); 129} 130 131// TODO: don't pass viewport size, it's automatic via EGL 132void CanvasContext::setup(int /* width */, int /* height */, const Vector3& lightCenter, 133 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { 134 if (!mCanvas) return; 135 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha); 136} 137 138void CanvasContext::setOpaque(bool opaque) { 139 mOpaque = opaque; 140} 141 142void CanvasContext::makeCurrent() { 143 // TODO: Figure out why this workaround is needed, see b/13913604 144 // In the meantime this matches the behavior of GLRenderer, so it is not a regression 145 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface); 146} 147 148void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) { 149 bool success = layerUpdater->apply(); 150 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!"); 151 if (layerUpdater->backingLayer()->deferredUpdateScheduled) { 152 mCanvas->pushLayerUpdate(layerUpdater->backingLayer()); 153 } 154} 155 156void CanvasContext::prepareTree(TreeInfo& info) { 157 mRenderThread.removeFrameCallback(this); 158 159 info.damageAccumulator = &mDamageAccumulator; 160 info.renderer = mCanvas; 161 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) { 162 info.canvasContext = this; 163 } 164 mAnimationContext->startFrame(info.mode); 165 mRootRenderNode->prepareTree(info); 166 mAnimationContext->runRemainingAnimations(info); 167 168 if (info.canvasContext) { 169 freePrefetechedLayers(); 170 } 171 172 if (CC_UNLIKELY(!mNativeWindow.get())) { 173 info.out.canDrawThisFrame = false; 174 return; 175 } 176 177 int runningBehind = 0; 178 // TODO: This query is moderately expensive, investigate adding some sort 179 // of fast-path based off when we last called eglSwapBuffers() as well as 180 // last vsync time. Or something. 181 mNativeWindow->query(mNativeWindow.get(), 182 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind); 183 info.out.canDrawThisFrame = !runningBehind; 184 185 if (info.out.hasAnimations || !info.out.canDrawThisFrame) { 186 if (!info.out.requiresUiRedraw) { 187 // If animationsNeedsRedraw is set don't bother posting for an RT anim 188 // as we will just end up fighting the UI thread. 189 mRenderThread.postFrameCallback(this); 190 } 191 } 192} 193 194void CanvasContext::stopDrawing() { 195 mRenderThread.removeFrameCallback(this); 196} 197 198void CanvasContext::notifyFramePending() { 199 ATRACE_CALL(); 200 mRenderThread.pushBackFrameCallback(this); 201} 202 203void CanvasContext::draw() { 204 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE, 205 "drawRenderNode called on a context with no canvas or surface!"); 206 207 profiler().markPlaybackStart(); 208 209 SkRect dirty; 210 mDamageAccumulator.finish(&dirty); 211 212 EGLint width, height; 213 mEglManager.beginFrame(mEglSurface, &width, &height); 214 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) { 215 mCanvas->setViewport(width, height); 216 dirty.setEmpty(); 217 } else if (!mBufferPreserved || mHaveNewSurface) { 218 dirty.setEmpty(); 219 } else { 220 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) { 221 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?", 222 SK_RECT_ARGS(dirty), width, height); 223 dirty.setEmpty(); 224 } 225 profiler().unionDirty(&dirty); 226 } 227 228 status_t status; 229 if (!dirty.isEmpty()) { 230 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop, 231 dirty.fRight, dirty.fBottom, mOpaque); 232 } else { 233 status = mCanvas->prepare(mOpaque); 234 } 235 236 Rect outBounds; 237 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds); 238 239 profiler().draw(mCanvas); 240 241 mCanvas->finish(); 242 243 profiler().markPlaybackEnd(); 244 245 if (status & DrawGlInfo::kStatusDrew) { 246 swapBuffers(); 247 } else { 248 mEglManager.cancelFrame(); 249 } 250 251 profiler().finishFrame(); 252} 253 254// Called by choreographer to do an RT-driven animation 255void CanvasContext::doFrame() { 256 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) { 257 return; 258 } 259 260 ATRACE_CALL(); 261 262 profiler().startFrame(); 263 264 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState()); 265 prepareTree(info); 266 if (info.out.canDrawThisFrame) { 267 draw(); 268 } 269} 270 271void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) { 272 ATRACE_CALL(); 273 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; 274 if (thread.eglManager().hasEglContext()) { 275 thread.eglManager().requireGlContext(); 276 mode = DrawGlInfo::kModeProcess; 277 } 278 279 thread.renderState().invokeFunctor(functor, mode, NULL); 280} 281 282void CanvasContext::markLayerInUse(RenderNode* node) { 283 if (mPrefetechedLayers.erase(node)) { 284 node->decStrong(0); 285 } 286} 287 288static void destroyPrefetechedNode(RenderNode* node) { 289 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName()); 290 node->destroyHardwareResources(); 291 node->decStrong(0); 292} 293 294void CanvasContext::freePrefetechedLayers() { 295 if (mPrefetechedLayers.size()) { 296 requireGlContext(); 297 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode); 298 mPrefetechedLayers.clear(); 299 } 300} 301 302void CanvasContext::buildLayer(RenderNode* node) { 303 ATRACE_CALL(); 304 if (!mEglManager.hasEglContext() || !mCanvas) { 305 return; 306 } 307 requireGlContext(); 308 // buildLayer() will leave the tree in an unknown state, so we must stop drawing 309 stopDrawing(); 310 311 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState()); 312 info.damageAccumulator = &mDamageAccumulator; 313 info.renderer = mCanvas; 314 info.runAnimations = false; 315 node->prepareTree(info); 316 SkRect ignore; 317 mDamageAccumulator.finish(&ignore); 318 // Tickle the GENERIC property on node to mark it as dirty for damaging 319 // purposes when the frame is actually drawn 320 node->setPropertyFieldsDirty(RenderNode::GENERIC); 321 322 mCanvas->markLayersAsBuildLayers(); 323 mCanvas->flushLayerUpdates(); 324 325 node->incStrong(0); 326 mPrefetechedLayers.insert(node); 327} 328 329bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 330 requireGlContext(); 331 layer->apply(); 332 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap); 333} 334 335void CanvasContext::destroyHardwareResources() { 336 stopDrawing(); 337 if (mEglManager.hasEglContext()) { 338 requireGlContext(); 339 freePrefetechedLayers(); 340 mRootRenderNode->destroyHardwareResources(); 341 Caches::getInstance().flush(Caches::kFlushMode_Layers); 342 } 343} 344 345void CanvasContext::trimMemory(RenderThread& thread, int level) { 346 // No context means nothing to free 347 if (!thread.eglManager().hasEglContext()) return; 348 349 ATRACE_CALL(); 350 thread.eglManager().requireGlContext(); 351 if (level >= TRIM_MEMORY_COMPLETE) { 352 Caches::getInstance().flush(Caches::kFlushMode_Full); 353 thread.eglManager().destroy(); 354 } else if (level >= TRIM_MEMORY_UI_HIDDEN) { 355 Caches::getInstance().flush(Caches::kFlushMode_Moderate); 356 } 357} 358 359void CanvasContext::runWithGlContext(RenderTask* task) { 360 requireGlContext(); 361 task->run(); 362} 363 364Layer* CanvasContext::createTextureLayer() { 365 requireSurface(); 366 return LayerRenderer::createTextureLayer(mRenderThread.renderState()); 367} 368 369void CanvasContext::requireGlContext() { 370 mEglManager.requireGlContext(); 371} 372 373void CanvasContext::setTextureAtlas(RenderThread& thread, 374 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) { 375 thread.eglManager().setTextureAtlas(buffer, map, mapSize); 376} 377 378} /* namespace renderthread */ 379} /* namespace uirenderer */ 380} /* namespace android */ 381