CanvasContext.cpp revision c79c3246c9a3e0d2aa34afd18fddc95a6aff0f30
15cbbce535744b89df5ecea95de21ee3733298260Romain Guy/*
25cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Copyright (C) 2014 The Android Open Source Project
35cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
45cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
55cbbce535744b89df5ecea95de21ee3733298260Romain Guy * you may not use this file except in compliance with the License.
65cbbce535744b89df5ecea95de21ee3733298260Romain Guy * You may obtain a copy of the License at
75cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
85cbbce535744b89df5ecea95de21ee3733298260Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
95cbbce535744b89df5ecea95de21ee3733298260Romain Guy *
105cbbce535744b89df5ecea95de21ee3733298260Romain Guy * Unless required by applicable law or agreed to in writing, software
115cbbce535744b89df5ecea95de21ee3733298260Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
125cbbce535744b89df5ecea95de21ee3733298260Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135cbbce535744b89df5ecea95de21ee3733298260Romain Guy * See the License for the specific language governing permissions and
145cbbce535744b89df5ecea95de21ee3733298260Romain Guy * limitations under the License.
155cbbce535744b89df5ecea95de21ee3733298260Romain Guy */
165cbbce535744b89df5ecea95de21ee3733298260Romain Guy
175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#include <GpuMemoryTracker.h>
185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#include "CanvasContext.h"
195cbbce535744b89df5ecea95de21ee3733298260Romain Guy
205cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "AnimationContext.h"
215cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "Caches.h"
225cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "DeferredLayerUpdater.h"
235cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "EglManager.h"
245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#include "LayerUpdateQueue.h"
255cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "LayerRenderer.h"
26ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy#include "OpenGLRenderer.h"
27079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy#include "Properties.h"
28dda570201ac851dd85af3861f7e575721d3345daRomain Guy#include "RenderThread.h"
295cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "hwui/Canvas.h"
305cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "renderstate/RenderState.h"
315cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "renderstate/Stencil.h"
325cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "protos/hwui.pb.h"
335cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "utils/GLUtils.h"
345cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include "utils/TimeUtils.h"
355cbbce535744b89df5ecea95de21ee3733298260Romain Guy
365cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <cutils/properties.h>
375cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <google/protobuf/io/zero_copy_stream_impl.h>
385cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <private/hwui/DrawGlInfo.h>
395cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <strings.h>
405cbbce535744b89df5ecea95de21ee3733298260Romain Guy
415cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <algorithm>
425cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <fcntl.h>
435cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <sys/stat.h>
445cbbce535744b89df5ecea95de21ee3733298260Romain Guy
455cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <cstdlib>
465cbbce535744b89df5ecea95de21ee3733298260Romain Guy
47ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy#define TRIM_MEMORY_COMPLETE 80
48ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy#define TRIM_MEMORY_UI_HIDDEN 20
495cbbce535744b89df5ecea95de21ee3733298260Romain Guy
505cbbce535744b89df5ecea95de21ee3733298260Romain Guy#define ENABLE_RENDERNODE_SERIALIZATION false
51ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy
525cbbce535744b89df5ecea95de21ee3733298260Romain Guy#define LOG_FRAMETIME_MMA 0
535cbbce535744b89df5ecea95de21ee3733298260Romain Guy
545cbbce535744b89df5ecea95de21ee3733298260Romain Guy#if LOG_FRAMETIME_MMA
555cbbce535744b89df5ecea95de21ee3733298260Romain Guystatic float sBenchMma = 0;
565cbbce535744b89df5ecea95de21ee3733298260Romain Guystatic int sFrameCount = 0;
575cbbce535744b89df5ecea95de21ee3733298260Romain Guystatic const float NANOS_PER_MILLIS_F = 1000000.0f;
585cbbce535744b89df5ecea95de21ee3733298260Romain Guy#endif
595cbbce535744b89df5ecea95de21ee3733298260Romain Guy
605cbbce535744b89df5ecea95de21ee3733298260Romain Guynamespace android {
615cbbce535744b89df5ecea95de21ee3733298260Romain Guynamespace uirenderer {
625cbbce535744b89df5ecea95de21ee3733298260Romain Guynamespace renderthread {
63079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy
64f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain GuyCanvasContext::CanvasContext(RenderThread& thread, bool translucent,
65eb99356a0548684a501766e6a524529ab93304c8Romain Guy        RenderNode* rootRenderNode, IContextFactory* contextFactory)
66eb99356a0548684a501766e6a524529ab93304c8Romain Guy        : mRenderThread(thread)
67eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mEglManager(thread.eglManager())
68eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mOpaque(!translucent)
69eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
70eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mJankTracker(thread.timeLord().frameIntervalNanos())
71eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mProfiler(mFrames)
72eb99356a0548684a501766e6a524529ab93304c8Romain Guy        , mContentDrawBounds(0, 0, 0, 0) {
73ed7a8fc768df158241819f062a12dafdaf8a628dRomain Guy    mRenderNodes.emplace_back(rootRenderNode);
745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mRenderThread.renderState().registerCanvasContext(this);
755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
78ed7a8fc768df158241819f062a12dafdaf8a628dRomain GuyCanvasContext::~CanvasContext() {
795cbbce535744b89df5ecea95de21ee3733298260Romain Guy    destroy(nullptr);
805cbbce535744b89df5ecea95de21ee3733298260Romain Guy    mRenderThread.renderState().unregisterCanvasContext(this);
815cbbce535744b89df5ecea95de21ee3733298260Romain Guy}
82f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
83f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guyvoid CanvasContext::destroy(TreeObserver* observer) {
84f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    stopDrawing();
853d58c03de0d8877b36cdb78b0ca8b5cac7f600e2Romain Guy    setSurface(nullptr);
86f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    freePrefetchedLayers(observer);
87ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    destroyHardwareResources(observer);
88f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    mAnimationContext->destroy();
89f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy#if !HWUI_NEW_OPS
90f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    if (mCanvas) {
91f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        delete mCanvas;
92f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        mCanvas = nullptr;
93f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    }
94ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy#endif
955cbbce535744b89df5ecea95de21ee3733298260Romain Guy}
965cbbce535744b89df5ecea95de21ee3733298260Romain Guy
978ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guyvoid CanvasContext::setSurface(Surface* surface) {
988ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy    ATRACE_CALL();
998ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy
1008ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy    mNativeSurface = surface;
1018ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy
1028ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy    if (mEglSurface != EGL_NO_SURFACE) {
103d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        mEglManager.destroySurface(mEglSurface);
104d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        mEglSurface = EGL_NO_SURFACE;
105ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    }
106959c91f7f7b4f921d341264f5b4ef54e702a0df0Romain Guy
107ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    if (surface) {
108ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy        mEglSurface = mEglManager.createSurface(surface);
109ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    }
110ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy
1113f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    if (mEglSurface != EGL_NO_SURFACE) {
1123f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
1133f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
1143f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mHaveNewSurface = true;
1153f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mSwapHistory.clear();
1163f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    } else {
117d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        mRenderThread.removeFrameCallback(this);
118ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    }
119ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy}
120ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy
121ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guyvoid CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
122eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mSwapBehavior = swapBehavior;
123ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy}
124ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy
125ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guyvoid CanvasContext::initialize(Surface* surface) {
126ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    setSurface(surface);
127eb99356a0548684a501766e6a524529ab93304c8Romain Guy#if !HWUI_NEW_OPS
128ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    if (mCanvas) return;
129ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
130ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    mCanvas->initProperties();
131ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy#endif
132ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy}
133af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy
1348b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guyvoid CanvasContext::updateSurface(Surface* surface) {
135a3dc55f83ab583e0a66b893c71b849afa046770aRomain Guy    setSurface(surface);
136a3dc55f83ab583e0a66b893c71b849afa046770aRomain Guy}
137a3dc55f83ab583e0a66b893c71b849afa046770aRomain Guy
138a3dc55f83ab583e0a66b893c71b849afa046770aRomain Guybool CanvasContext::pauseSurface(Surface* surface) {
139a3dc55f83ab583e0a66b893c71b849afa046770aRomain Guy    return mRenderThread.removeFrameCallback(this);
1405cbbce535744b89df5ecea95de21ee3733298260Romain Guy}
1415cbbce535744b89df5ecea95de21ee3733298260Romain Guy
1425cbbce535744b89df5ecea95de21ee3733298260Romain Guyvoid CanvasContext::setStopped(bool stopped) {
1435cbbce535744b89df5ecea95de21ee3733298260Romain Guy    if (mStopped != stopped) {
1445cbbce535744b89df5ecea95de21ee3733298260Romain Guy        mStopped = stopped;
1455cbbce535744b89df5ecea95de21ee3733298260Romain Guy        if (mStopped) {
1465cbbce535744b89df5ecea95de21ee3733298260Romain Guy            mRenderThread.removeFrameCallback(this);
1475cbbce535744b89df5ecea95de21ee3733298260Romain Guy            if (mEglManager.isCurrent(mEglSurface)) {
1485cbbce535744b89df5ecea95de21ee3733298260Romain Guy                mEglManager.makeCurrent(EGL_NO_SURFACE);
1495cbbce535744b89df5ecea95de21ee3733298260Romain Guy            }
1508ce00301a023eecaeb8891ce906f67b513ebb42aRomain Guy        }
151ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    }
152ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy}
1535cbbce535744b89df5ecea95de21ee3733298260Romain Guy
154dda570201ac851dd85af3861f7e575721d3345daRomain Guy// TODO: don't pass viewport size, it's automatic via EGL
155f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guyvoid CanvasContext::setup(int width, int height, float lightRadius,
1568aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
157421458aad764cd9d1403d2540ab979b336b02341Romain Guy#if HWUI_NEW_OPS
158421458aad764cd9d1403d2540ab979b336b02341Romain Guy    mLightGeometry.radius = lightRadius;
159eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
160eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mLightInfo.spotShadowAlpha = spotShadowAlpha;
161eb99356a0548684a501766e6a524529ab93304c8Romain Guy#else
162eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (!mCanvas) return;
163dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
164af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy#endif
165af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy}
166dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
167dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guyvoid CanvasContext::setLightCenter(const Vector3& lightCenter) {
168dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy#if HWUI_NEW_OPS
169dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    mLightGeometry.center = lightCenter;
170af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy#else
171af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (!mCanvas) return;
172af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    mCanvas->setLightCenter(lightCenter);
173af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy#endif
174af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy}
175af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy
176eb99356a0548684a501766e6a524529ab93304c8Romain Guyvoid CanvasContext::setOpaque(bool opaque) {
177eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mOpaque = opaque;
178eb99356a0548684a501766e6a524529ab93304c8Romain Guy}
179eb99356a0548684a501766e6a524529ab93304c8Romain Guy
180eb99356a0548684a501766e6a524529ab93304c8Romain Guybool CanvasContext::makeCurrent() {
181eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (mStopped) return false;
182eb99356a0548684a501766e6a524529ab93304c8Romain Guy
183eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // TODO: Figure out why this workaround is needed, see b/13913604
184eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
185eb99356a0548684a501766e6a524529ab93304c8Romain Guy    EGLint error = 0;
186eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
187eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (error) {
188eb99356a0548684a501766e6a524529ab93304c8Romain Guy        setSurface(nullptr);
189eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
190eb99356a0548684a501766e6a524529ab93304c8Romain Guy    return !error;
1918aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy}
1928aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy
193ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guystatic bool wasSkipped(FrameInfo* info) {
194ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
195ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy}
1968aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy
1978aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guyvoid CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
1988aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        int64_t syncQueued, RenderNode* target) {
1998aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mRenderThread.removeFrameCallback(this);
200967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy
2018aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    // If the previous frame was dropped we don't need to hold onto it, so
202ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    // just keep using the previous frame's structure instead
203ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    if (!wasSkipped(mCurrentFrameInfo)) {
204ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy        mCurrentFrameInfo = &mFrames.next();
2058aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    }
2068aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
2078aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
2085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mCurrentFrameInfo->markSyncStart();
209967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy
210967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy    info.damageAccumulator = &mDamageAccumulator;
211967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy#if HWUI_NEW_OPS
212967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy    info.layerUpdateQueue = &mLayerUpdateQueue;
213967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy#else
214967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy    info.renderer = mCanvas;
2150baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy#endif
216967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy
217967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy    if (CC_LIKELY(mNativeSurface.get())) {
218f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        info.frameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber());
219ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    }
220ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy
221ada4d53d50dc869b8278573ad640dc44118d3bcfRomain Guy    mAnimationContext->startFrame(info.mode);
2225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    for (const sp<RenderNode>& node : mRenderNodes) {
2235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Only the primary target node will be drawn full - all other nodes would get drawn in
2245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // real time mode. In case of a window, the primary node is the window content and the other
225db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase        // node(s) are non client / filler nodes.
226db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase        info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
227db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase        node->prepareTree(info);
228db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase        GL_CHECKPOINT(MODERATE);
229db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase    }
230db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase    mAnimationContext->runRemainingAnimations(info);
231db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase    GL_CHECKPOINT(MODERATE);
232db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase
233db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase    freePrefetchedLayers(info.observer);
234db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase    GL_CHECKPOINT(MODERATE);
235db8c9a6a4d9bf8c39f834b25611926caf21380f6Chet Haase
2365f803623559aab395a29d575c37c4e39c23a4b4eChris Craik    if (CC_UNLIKELY(!mNativeSurface.get())) {
2375f803623559aab395a29d575c37c4e39c23a4b4eChris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
2385cbbce535744b89df5ecea95de21ee3733298260Romain Guy        info.out.canDrawThisFrame = false;
239967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy        return;
240967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy    }
241967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy
2420baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy    if (CC_LIKELY(mSwapHistory.size())) {
243967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy        nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
2448aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        const SwapHistory& lastSwap = mSwapHistory.back();
2458aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
2463f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        // The slight fudge-factor is to deal with cases where
247079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy        // the vsync was estimated due to being slow handling the signal.
2480baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy        // See the logic in TimeLord#computeFrameTimeNanos or in
249967e2bf3ac8943a8e8a374bf86021915445cda67Romain Guy        // Choreographer.java for details on when this happens
2505cbbce535744b89df5ecea95de21ee3733298260Romain Guy        if (vsyncDelta < 2_ms) {
2515cbbce535744b89df5ecea95de21ee3733298260Romain Guy            // Already drew for this vsync pulse, UI draw request missed
2525cbbce535744b89df5ecea95de21ee3733298260Romain Guy            // the deadline for RT animations
2535cbbce535744b89df5ecea95de21ee3733298260Romain Guy            info.out.canDrawThisFrame = false;
2545cbbce535744b89df5ecea95de21ee3733298260Romain Guy        } else if (lastSwap.swapTime < latestVsync) {
2555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            info.out.canDrawThisFrame = true;
256        } else {
257            // We're maybe behind? Find out for sure
258            int runningBehind = 0;
259            // TODO: Have this method be on Surface, too, not just ANativeWindow...
260            ANativeWindow* window = mNativeSurface.get();
261            window->query(window, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
262            info.out.canDrawThisFrame = !runningBehind;
263        }
264    } else {
265        info.out.canDrawThisFrame = true;
266    }
267
268    if (!info.out.canDrawThisFrame) {
269        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
270    }
271
272    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
273        if (!info.out.requiresUiRedraw) {
274            // If animationsNeedsRedraw is set don't bother posting for an RT anim
275            // as we will just end up fighting the UI thread.
276            mRenderThread.postFrameCallback(this);
277        }
278    }
279}
280
281void CanvasContext::stopDrawing() {
282    mRenderThread.removeFrameCallback(this);
283}
284
285void CanvasContext::notifyFramePending() {
286    ATRACE_CALL();
287    mRenderThread.pushBackFrameCallback(this);
288}
289
290void CanvasContext::draw() {
291#if !HWUI_NEW_OPS
292    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
293            "drawRenderNode called on a context with no canvas or surface!");
294#endif
295
296    SkRect dirty;
297    mDamageAccumulator.finish(&dirty);
298
299    // TODO: Re-enable after figuring out cause of b/22592975
300//    if (dirty.isEmpty() && Properties::skipEmptyFrames) {
301//        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
302//        return;
303//    }
304
305    mCurrentFrameInfo->markIssueDrawCommandsStart();
306
307    Frame frame = mEglManager.beginFrame(mEglSurface);
308
309    if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
310        // can't rely on prior content of window if viewport size changes
311        dirty.setEmpty();
312        mLastFrameWidth = frame.width();
313        mLastFrameHeight = frame.height();
314    } else if (mHaveNewSurface || frame.bufferAge() == 0) {
315        // New surface needs a full draw
316        dirty.setEmpty();
317    } else {
318        if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
319            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
320                    SK_RECT_ARGS(dirty), frame.width(), frame.height());
321            dirty.setEmpty();
322        }
323        profiler().unionDirty(&dirty);
324    }
325
326    if (dirty.isEmpty()) {
327        dirty.set(0, 0, frame.width(), frame.height());
328    }
329
330    // At this point dirty is the area of the screen to update. However,
331    // the area of the frame we need to repaint is potentially different, so
332    // stash the screen area for later
333    SkRect screenDirty(dirty);
334
335    // If the buffer age is 0 we do a full-screen repaint (handled above)
336    // If the buffer age is 1 the buffer contents are the same as they were
337    // last frame so there's nothing to union() against
338    // Therefore we only care about the > 1 case.
339    if (frame.bufferAge() > 1) {
340        if (frame.bufferAge() > (int) mSwapHistory.size()) {
341            // We don't have enough history to handle this old of a buffer
342            // Just do a full-draw
343            dirty.set(0, 0, frame.width(), frame.height());
344        } else {
345            // At this point we haven't yet added the latest frame
346            // to the damage history (happens below)
347            // So we need to damage
348            for (int i = mSwapHistory.size() - 1;
349                    i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
350                dirty.join(mSwapHistory[i].damage);
351            }
352        }
353    }
354
355    mEglManager.damageFrame(frame, dirty);
356
357#if HWUI_NEW_OPS
358    auto& caches = Caches::getInstance();
359    FrameBuilder frameBuilder(dirty, frame.width(), frame.height(), mLightGeometry, caches);
360
361    frameBuilder.deferLayers(mLayerUpdateQueue);
362    mLayerUpdateQueue.clear();
363
364    frameBuilder.deferRenderNodeScene(mRenderNodes, mContentDrawBounds);
365
366    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
367            mOpaque, mLightInfo);
368    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
369    profiler().draw(&renderer);
370    bool drew = renderer.didDraw();
371
372    // post frame cleanup
373    caches.clearGarbage();
374    caches.pathCache.trim();
375    caches.tessellationCache.trim();
376
377#if DEBUG_MEMORY_USAGE
378    mCaches.dumpMemoryUsage();
379#else
380    if (CC_UNLIKELY(Properties::debugLevel & kDebugMemory)) {
381        caches.dumpMemoryUsage();
382    }
383#endif
384
385#else
386    mCanvas->prepareDirty(frame.width(), frame.height(),
387            dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
388
389    Rect outBounds;
390    // It there are multiple render nodes, they are laid out as follows:
391    // #0 - backdrop (content + caption)
392    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
393    // #2 - additional overlay nodes
394    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
395    // resizing however it might become partially visible. The following render loop will crop the
396    // backdrop against the content and draw the remaining part of it. It will then draw the content
397    // cropped to the backdrop (since that indicates a shrinking of the window).
398    //
399    // Additional nodes will be drawn on top with no particular clipping semantics.
400
401    // The bounds of the backdrop against which the content should be clipped.
402    Rect backdropBounds = mContentDrawBounds;
403    // Usually the contents bounds should be mContentDrawBounds - however - we will
404    // move it towards the fixed edge to give it a more stable appearance (for the moment).
405    Rect contentBounds;
406    // If there is no content bounds we ignore the layering as stated above and start with 2.
407    int layer = (mContentDrawBounds.isEmpty() || mRenderNodes.size() == 1) ? 2 : 0;
408    // Draw all render nodes. Note that
409    for (const sp<RenderNode>& node : mRenderNodes) {
410        if (layer == 0) { // Backdrop.
411            // Draw the backdrop clipped to the inverse content bounds, but assume that the content
412            // was moved to the upper left corner.
413            const RenderProperties& properties = node->properties();
414            Rect targetBounds(properties.getLeft(), properties.getTop(),
415                              properties.getRight(), properties.getBottom());
416            // Move the content bounds towards the fixed corner of the backdrop.
417            const int x = targetBounds.left;
418            const int y = targetBounds.top;
419            contentBounds.set(x, y, x + mContentDrawBounds.getWidth(),
420                                    y + mContentDrawBounds.getHeight());
421            // Remember the intersection of the target bounds and the intersection bounds against
422            // which we have to crop the content.
423            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
424            backdropBounds.doIntersect(targetBounds);
425            // Check if we have to draw something on the left side ...
426            if (targetBounds.left < contentBounds.left) {
427                mCanvas->save(SaveFlags::Clip);
428                if (mCanvas->clipRect(targetBounds.left, targetBounds.top,
429                                      contentBounds.left, targetBounds.bottom,
430                                      SkRegion::kIntersect_Op)) {
431                    mCanvas->drawRenderNode(node.get(), outBounds);
432                }
433                // Reduce the target area by the area we have just painted.
434                targetBounds.left = std::min(contentBounds.left, targetBounds.right);
435                mCanvas->restore();
436            }
437            // ... or on the right side ...
438            if (targetBounds.right > contentBounds.right &&
439                !targetBounds.isEmpty()) {
440                mCanvas->save(SaveFlags::Clip);
441                if (mCanvas->clipRect(contentBounds.right, targetBounds.top,
442                                      targetBounds.right, targetBounds.bottom,
443                                      SkRegion::kIntersect_Op)) {
444                    mCanvas->drawRenderNode(node.get(), outBounds);
445                }
446                // Reduce the target area by the area we have just painted.
447                targetBounds.right = std::max(targetBounds.left, contentBounds.right);
448                mCanvas->restore();
449            }
450            // ... or at the top ...
451            if (targetBounds.top < contentBounds.top &&
452                !targetBounds.isEmpty()) {
453                mCanvas->save(SaveFlags::Clip);
454                if (mCanvas->clipRect(targetBounds.left, targetBounds.top, targetBounds.right,
455                                      contentBounds.top,
456                                      SkRegion::kIntersect_Op)) {
457                    mCanvas->drawRenderNode(node.get(), outBounds);
458                }
459                // Reduce the target area by the area we have just painted.
460                targetBounds.top = std::min(contentBounds.top, targetBounds.bottom);
461                mCanvas->restore();
462            }
463            // ... or at the bottom.
464            if (targetBounds.bottom > contentBounds.bottom &&
465                !targetBounds.isEmpty()) {
466                mCanvas->save(SaveFlags::Clip);
467                if (mCanvas->clipRect(targetBounds.left, contentBounds.bottom, targetBounds.right,
468                                      targetBounds.bottom, SkRegion::kIntersect_Op)) {
469                    mCanvas->drawRenderNode(node.get(), outBounds);
470                }
471                mCanvas->restore();
472            }
473        } else if (layer == 1) { // Content
474            // It gets cropped against the bounds of the backdrop to stay inside.
475            mCanvas->save(SaveFlags::MatrixClip);
476
477            // We shift and clip the content to match its final location in the window.
478            const float left = mContentDrawBounds.left;
479            const float top = mContentDrawBounds.top;
480            const float dx = backdropBounds.left - left;
481            const float dy = backdropBounds.top - top;
482            const float width = backdropBounds.getWidth();
483            const float height = backdropBounds.getHeight();
484
485            mCanvas->translate(dx, dy);
486            if (mCanvas->clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op)) {
487                mCanvas->drawRenderNode(node.get(), outBounds);
488            }
489            mCanvas->restore();
490        } else { // draw the rest on top at will!
491            mCanvas->drawRenderNode(node.get(), outBounds);
492        }
493        layer++;
494    }
495
496    profiler().draw(mCanvas);
497
498    bool drew = mCanvas->finish();
499#endif
500
501    waitOnFences();
502
503    GL_CHECKPOINT(LOW);
504
505    // Even if we decided to cancel the frame, from the perspective of jank
506    // metrics the frame was swapped at this point
507    mCurrentFrameInfo->markSwapBuffers();
508
509    if (drew || mEglManager.damageRequiresSwap()) {
510        if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
511            setSurface(nullptr);
512        }
513        SwapHistory& swap = mSwapHistory.next();
514        swap.damage = screenDirty;
515        swap.swapTime = systemTime(CLOCK_MONOTONIC);
516        swap.vsyncTime = mRenderThread.timeLord().latestVsync();
517        mHaveNewSurface = false;
518    }
519
520    // TODO: Use a fence for real completion?
521    mCurrentFrameInfo->markFrameCompleted();
522
523#if LOG_FRAMETIME_MMA
524    float thisFrame = mCurrentFrameInfo->duration(
525            FrameInfoIndex::IssueDrawCommandsStart,
526            FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
527    if (sFrameCount) {
528        sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
529    } else {
530        sBenchMma = thisFrame;
531    }
532    if (++sFrameCount == 10) {
533        sFrameCount = 1;
534        ALOGD("Average frame time: %.4f", sBenchMma);
535    }
536#endif
537
538    mJankTracker.addFrame(*mCurrentFrameInfo);
539    mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
540    if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
541        mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data());
542    }
543
544    GpuMemoryTracker::onFrameCompleted();
545}
546
547// Called by choreographer to do an RT-driven animation
548void CanvasContext::doFrame() {
549#if HWUI_NEW_OPS
550    if (CC_UNLIKELY(mEglSurface == EGL_NO_SURFACE)) return;
551#else
552    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) return;
553#endif
554    prepareAndDraw(nullptr);
555}
556
557void CanvasContext::prepareAndDraw(RenderNode* node) {
558    ATRACE_CALL();
559
560    nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
561    int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
562    UiFrameInfoBuilder(frameInfo)
563        .addFlag(FrameInfoFlags::RTAnimation)
564        .setVsync(vsync, vsync);
565
566    TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
567    prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
568    if (info.out.canDrawThisFrame) {
569        draw();
570    }
571}
572
573void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
574    ATRACE_CALL();
575    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
576    if (thread.eglManager().hasEglContext()) {
577        mode = DrawGlInfo::kModeProcess;
578    }
579
580    thread.renderState().invokeFunctor(functor, mode, nullptr);
581}
582
583void CanvasContext::markLayerInUse(RenderNode* node) {
584    if (mPrefetchedLayers.erase(node)) {
585        node->decStrong(nullptr);
586    }
587}
588
589void CanvasContext::freePrefetchedLayers(TreeObserver* observer) {
590    if (mPrefetchedLayers.size()) {
591        for (auto& node : mPrefetchedLayers) {
592            ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
593                    node->getName());
594            node->destroyHardwareResources(observer);
595            node->decStrong(observer);
596        }
597        mPrefetchedLayers.clear();
598    }
599}
600
601void CanvasContext::buildLayer(RenderNode* node, TreeObserver* observer) {
602    ATRACE_CALL();
603    if (!mEglManager.hasEglContext()) return;
604#if !HWUI_NEW_OPS
605    if (!mCanvas) return;
606#endif
607
608    // buildLayer() will leave the tree in an unknown state, so we must stop drawing
609    stopDrawing();
610
611    TreeInfo info(TreeInfo::MODE_FULL, *this);
612    info.damageAccumulator = &mDamageAccumulator;
613    info.observer = observer;
614#if HWUI_NEW_OPS
615    info.layerUpdateQueue = &mLayerUpdateQueue;
616#else
617    info.renderer = mCanvas;
618#endif
619    info.runAnimations = false;
620    node->prepareTree(info);
621    SkRect ignore;
622    mDamageAccumulator.finish(&ignore);
623    // Tickle the GENERIC property on node to mark it as dirty for damaging
624    // purposes when the frame is actually drawn
625    node->setPropertyFieldsDirty(RenderNode::GENERIC);
626
627#if HWUI_NEW_OPS
628    static const std::vector< sp<RenderNode> > emptyNodeList;
629    auto& caches = Caches::getInstance();
630    FrameBuilder frameBuilder(mLayerUpdateQueue, mLightGeometry, caches);
631    mLayerUpdateQueue.clear();
632    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
633            mOpaque, mLightInfo);
634    LOG_ALWAYS_FATAL_IF(renderer.didDraw(), "shouldn't draw in buildlayer case");
635    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
636#else
637    mCanvas->markLayersAsBuildLayers();
638    mCanvas->flushLayerUpdates();
639#endif
640
641    node->incStrong(nullptr);
642    mPrefetchedLayers.insert(node);
643}
644
645bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
646    layer->apply();
647    return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
648}
649
650void CanvasContext::destroyHardwareResources(TreeObserver* observer) {
651    stopDrawing();
652    if (mEglManager.hasEglContext()) {
653        freePrefetchedLayers(observer);
654        for (const sp<RenderNode>& node : mRenderNodes) {
655            node->destroyHardwareResources(observer);
656        }
657        Caches& caches = Caches::getInstance();
658        // Make sure to release all the textures we were owning as there won't
659        // be another draw
660        caches.textureCache.resetMarkInUse(this);
661        mRenderThread.renderState().flush(Caches::FlushMode::Layers);
662    }
663}
664
665void CanvasContext::trimMemory(RenderThread& thread, int level) {
666    // No context means nothing to free
667    if (!thread.eglManager().hasEglContext()) return;
668
669    ATRACE_CALL();
670    if (level >= TRIM_MEMORY_COMPLETE) {
671        thread.renderState().flush(Caches::FlushMode::Full);
672        thread.eglManager().destroy();
673    } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
674        thread.renderState().flush(Caches::FlushMode::Moderate);
675    }
676}
677
678void CanvasContext::runWithGlContext(RenderTask* task) {
679    LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(),
680            "GL context not initialized!");
681    task->run();
682}
683
684Layer* CanvasContext::createTextureLayer() {
685    mEglManager.initialize();
686    return LayerRenderer::createTextureLayer(mRenderThread.renderState());
687}
688
689void CanvasContext::setTextureAtlas(RenderThread& thread,
690        const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
691    thread.eglManager().setTextureAtlas(buffer, map, mapSize);
692}
693
694void CanvasContext::dumpFrames(int fd) {
695    FILE* file = fdopen(fd, "a");
696    fprintf(file, "\n\n---PROFILEDATA---\n");
697    for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
698        fprintf(file, "%s", FrameInfoNames[i].c_str());
699        fprintf(file, ",");
700    }
701    for (size_t i = 0; i < mFrames.size(); i++) {
702        FrameInfo& frame = mFrames[i];
703        if (frame[FrameInfoIndex::SyncStart] == 0) {
704            continue;
705        }
706        fprintf(file, "\n");
707        for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
708            fprintf(file, "%" PRId64 ",", frame[i]);
709        }
710    }
711    fprintf(file, "\n---PROFILEDATA---\n\n");
712    fflush(file);
713}
714
715void CanvasContext::resetFrameStats() {
716    mFrames.clear();
717    mRenderThread.jankTracker().reset();
718}
719
720void CanvasContext::serializeDisplayListTree() {
721#if ENABLE_RENDERNODE_SERIALIZATION
722    using namespace google::protobuf::io;
723    char package[128];
724    // Check whether tracing is enabled for this process.
725    FILE * file = fopen("/proc/self/cmdline", "r");
726    if (file) {
727        if (!fgets(package, 128, file)) {
728            ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
729            fclose(file);
730            return;
731        }
732        fclose(file);
733    } else {
734        ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
735                errno);
736        return;
737    }
738    char path[1024];
739    snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
740    int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
741    if (fd == -1) {
742        ALOGD("Failed to open '%s'", path);
743        return;
744    }
745    proto::RenderNode tree;
746    // TODO: Streaming writes?
747    mRootRenderNode->copyTo(&tree);
748    std::string data = tree.SerializeAsString();
749    write(fd, data.c_str(), data.length());
750    close(fd);
751#endif
752}
753
754void CanvasContext::waitOnFences() {
755    if (mFrameFences.size()) {
756        ATRACE_CALL();
757        for (auto& fence : mFrameFences) {
758            fence->getResult();
759        }
760        mFrameFences.clear();
761    }
762}
763
764class CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> {
765public:
766    FuncTaskProcessor(Caches& caches)
767            : TaskProcessor<bool>(&caches.tasks) {}
768
769    virtual void onProcess(const sp<Task<bool> >& task) override {
770        FuncTask* t = static_cast<FuncTask*>(task.get());
771        t->func();
772        task->setResult(true);
773    }
774};
775
776void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
777    if (!mFrameWorkProcessor.get()) {
778        mFrameWorkProcessor = new FuncTaskProcessor(Caches::getInstance());
779    }
780    sp<FuncTask> task(new FuncTask());
781    task->func = func;
782    mFrameWorkProcessor->add(task);
783}
784
785} /* namespace renderthread */
786} /* namespace uirenderer */
787} /* namespace android */
788