DrawFrameTask.cpp revision 945961f78a78eced823d5ba78505c781b079703d
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 "DrawFrameTask.h"
18
19#include <utils/Log.h>
20#include <utils/Trace.h>
21
22#include "../DeferredLayerUpdater.h"
23#include "../DisplayList.h"
24#include "../RenderNode.h"
25#include "CanvasContext.h"
26#include "RenderThread.h"
27
28namespace android {
29namespace uirenderer {
30namespace renderthread {
31
32DrawFrameTask::DrawFrameTask()
33        : mRenderThread(nullptr)
34        , mContext(nullptr)
35        , mSyncResult(kSync_OK) {
36}
37
38DrawFrameTask::~DrawFrameTask() {
39}
40
41void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context,
42        RenderNode* targetNode) {
43    mRenderThread = thread;
44    mContext = context;
45    mTargetNode = targetNode;
46}
47
48void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
49    LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
50
51    for (size_t i = 0; i < mLayers.size(); i++) {
52        if (mLayers[i].get() == layer) {
53            return;
54        }
55    }
56    mLayers.push_back(layer);
57}
58
59void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
60    for (size_t i = 0; i < mLayers.size(); i++) {
61        if (mLayers[i].get() == layer) {
62            mLayers.erase(mLayers.begin() + i);
63            return;
64        }
65    }
66}
67
68int DrawFrameTask::drawFrame() {
69    LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
70
71    mSyncResult = kSync_OK;
72    mSyncQueued = systemTime(CLOCK_MONOTONIC);
73    postAndWait();
74
75    return mSyncResult;
76}
77
78void DrawFrameTask::postAndWait() {
79    AutoMutex _lock(mLock);
80    mRenderThread->queue(this);
81    mSignal.wait(mLock);
82}
83
84void DrawFrameTask::run() {
85    ATRACE_NAME("DrawFrame");
86
87    bool canUnblockUiThread;
88    bool canDrawThisFrame;
89    {
90        TreeInfo info(TreeInfo::MODE_FULL, *mContext);
91        canUnblockUiThread = syncFrameState(info);
92        canDrawThisFrame = info.out.canDrawThisFrame;
93    }
94
95    // Grab a copy of everything we need
96    CanvasContext* context = mContext;
97
98    // From this point on anything in "this" is *UNSAFE TO ACCESS*
99    if (canUnblockUiThread) {
100        unblockUiThread();
101    }
102
103    if (CC_LIKELY(canDrawThisFrame)) {
104        context->draw();
105    }
106
107    if (!canUnblockUiThread) {
108        unblockUiThread();
109    }
110}
111
112bool DrawFrameTask::syncFrameState(TreeInfo& info) {
113    ATRACE_CALL();
114    int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)];
115    mRenderThread->timeLord().vsyncReceived(vsync);
116    bool canDraw = mContext->makeCurrent();
117    Caches::getInstance().textureCache.resetMarkInUse(mContext);
118
119    for (size_t i = 0; i < mLayers.size(); i++) {
120        mLayers[i]->apply();
121    }
122    mLayers.clear();
123    mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode);
124
125    // This is after the prepareTree so that any pending operations
126    // (RenderNode tree state, prefetched layers, etc...) will be flushed.
127    if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) {
128        mSyncResult |= kSync_LostSurfaceRewardIfFound;
129        info.out.canDrawThisFrame = false;
130    }
131
132    if (info.out.hasAnimations) {
133        if (info.out.requiresUiRedraw) {
134            mSyncResult |= kSync_UIRedrawRequired;
135        }
136    }
137    // If prepareTextures is false, we ran out of texture cache space
138    return info.prepareTextures;
139}
140
141void DrawFrameTask::unblockUiThread() {
142    AutoMutex _lock(mLock);
143    mSignal.signal();
144}
145
146} /* namespace renderthread */
147} /* namespace uirenderer */
148} /* namespace android */
149