blob: 45f5cb057ee4e93d4b622cbf07fb27d33686f0e9 [file] [log] [blame]
John Reck668f0e32014-03-26 15:10:40 -07001/*
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 ATRACE_TAG ATRACE_TAG_VIEW
18
19#include "DrawFrameTask.h"
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include "../DisplayList.h"
25#include "../RenderNode.h"
26#include "CanvasContext.h"
27#include "RenderThread.h"
28
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
John Recke45b1fd2014-04-15 09:50:16 -070033DrawFrameTask::DrawFrameTask() : mContext(0) {
John Reck668f0e32014-03-26 15:10:40 -070034}
35
36DrawFrameTask::~DrawFrameTask() {
37}
38
39void DrawFrameTask::setContext(CanvasContext* context) {
40 mContext = context;
41}
42
John Reck668f0e32014-03-26 15:10:40 -070043void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070044 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
45
John Reck668f0e32014-03-26 15:10:40 -070046 mLayers.push(layer);
47}
48
49void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
50 for (size_t i = 0; i < mLayers.size(); i++) {
51 if (mLayers[i] == layer) {
52 mLayers.removeAt(i);
53 break;
54 }
55 }
56}
57
John Reck668f0e32014-03-26 15:10:40 -070058void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
59 mDirty.set(left, top, right, bottom);
60}
61
62void DrawFrameTask::drawFrame(RenderThread* renderThread) {
John Reck668f0e32014-03-26 15:10:40 -070063 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
64
John Reck8ca3eec2014-04-10 10:28:45 -070065 postAndWait(renderThread);
John Reck668f0e32014-03-26 15:10:40 -070066
67 // Reset the single-frame data
68 mDirty.setEmpty();
John Reck668f0e32014-03-26 15:10:40 -070069}
70
John Reck8ca3eec2014-04-10 10:28:45 -070071void DrawFrameTask::postAndWait(RenderThread* renderThread) {
John Reck087bc0c2014-04-04 16:20:08 -070072 AutoMutex _lock(mLock);
73 renderThread->queue(this);
74 mSignal.wait(mLock);
75}
76
John Reck668f0e32014-03-26 15:10:40 -070077void DrawFrameTask::run() {
78 ATRACE_NAME("DrawFrame");
79
John Reckf4198b72014-04-09 17:00:04 -070080 bool canUnblockUiThread = syncFrameState();
John Reck668f0e32014-03-26 15:10:40 -070081
82 // Grab a copy of everything we need
John Recke45b1fd2014-04-15 09:50:16 -070083 Rect dirty(mDirty);
John Reck668f0e32014-03-26 15:10:40 -070084 CanvasContext* context = mContext;
85
John Reck668f0e32014-03-26 15:10:40 -070086 // From this point on anything in "this" is *UNSAFE TO ACCESS*
87 if (canUnblockUiThread) {
88 unblockUiThread();
89 }
90
John Recke45b1fd2014-04-15 09:50:16 -070091 context->draw(&dirty);
John Reck668f0e32014-03-26 15:10:40 -070092
93 if (!canUnblockUiThread) {
94 unblockUiThread();
95 }
96}
97
John Recke45b1fd2014-04-15 09:50:16 -070098static void initTreeInfo(TreeInfo& info) {
John Reck860d1552014-04-11 19:15:05 -070099 info.prepareTextures = true;
John Recke45b1fd2014-04-15 09:50:16 -0700100 info.performStagingPush = true;
101 info.evaluateAnimations = true;
102 // TODO: Get this from Choreographer
103 nsecs_t frameTimeNs = systemTime(CLOCK_MONOTONIC);
104 info.frameTimeMs = nanoseconds_to_milliseconds(frameTimeNs);
John Reck860d1552014-04-11 19:15:05 -0700105}
106
John Reckf4198b72014-04-09 17:00:04 -0700107bool DrawFrameTask::syncFrameState() {
John Reck668f0e32014-03-26 15:10:40 -0700108 ATRACE_CALL();
John Reck860d1552014-04-11 19:15:05 -0700109 mContext->makeCurrent();
110 Caches::getInstance().textureCache.resetMarkInUse();
111 TreeInfo info;
John Recke45b1fd2014-04-15 09:50:16 -0700112 initTreeInfo(info);
John Reck860d1552014-04-11 19:15:05 -0700113 mContext->processLayerUpdates(&mLayers, info);
John Recke45b1fd2014-04-15 09:50:16 -0700114 mContext->prepareTree(info);
John Reck52244ff2014-05-01 21:27:37 -0700115 if (info.hasAnimations) {
116 // TODO: dirty calculations, for now just do a full-screen inval
117 mDirty.setEmpty();
118 }
John Reck860d1552014-04-11 19:15:05 -0700119 // If prepareTextures is false, we ran out of texture cache space
120 return !info.hasFunctors && info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700121}
122
123void DrawFrameTask::unblockUiThread() {
124 AutoMutex _lock(mLock);
125 mSignal.signal();
126}
127
John Reck668f0e32014-03-26 15:10:40 -0700128} /* namespace renderthread */
129} /* namespace uirenderer */
130} /* namespace android */