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