blob: a7b781a21514635905b6a48dd4243751c25cd1ed [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 Reck18f16e62014-05-02 16:46:41 -070033DrawFrameTask::DrawFrameTask()
34 : mRenderThread(NULL)
35 , mContext(NULL)
36 , mFrameTimeNanos(NULL) {
John Reck668f0e32014-03-26 15:10:40 -070037}
38
39DrawFrameTask::~DrawFrameTask() {
40}
41
John Reck18f16e62014-05-02 16:46:41 -070042void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
43 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070044 mContext = context;
45}
46
John Reck668f0e32014-03-26 15:10:40 -070047void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070048 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
49
John Reck668f0e32014-03-26 15:10:40 -070050 mLayers.push(layer);
51}
52
53void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
54 for (size_t i = 0; i < mLayers.size(); i++) {
55 if (mLayers[i] == layer) {
56 mLayers.removeAt(i);
57 break;
58 }
59 }
60}
61
John Reck668f0e32014-03-26 15:10:40 -070062void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
63 mDirty.set(left, top, right, bottom);
64}
65
John Reck18f16e62014-05-02 16:46:41 -070066void DrawFrameTask::drawFrame(nsecs_t frameTimeNanos) {
John Reck668f0e32014-03-26 15:10:40 -070067 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
68
John Reck18f16e62014-05-02 16:46:41 -070069 mFrameTimeNanos = frameTimeNanos;
70 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070071
72 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070073 mFrameTimeNanos = 0;
John Reck668f0e32014-03-26 15:10:40 -070074 mDirty.setEmpty();
John Reck668f0e32014-03-26 15:10:40 -070075}
76
John Reck18f16e62014-05-02 16:46:41 -070077void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070078 AutoMutex _lock(mLock);
John Reck18f16e62014-05-02 16:46:41 -070079 mRenderThread->queue(this);
John Reck087bc0c2014-04-04 16:20:08 -070080 mSignal.wait(mLock);
81}
82
John Reck668f0e32014-03-26 15:10:40 -070083void DrawFrameTask::run() {
84 ATRACE_NAME("DrawFrame");
85
John Reckf4198b72014-04-09 17:00:04 -070086 bool canUnblockUiThread = syncFrameState();
John Reck668f0e32014-03-26 15:10:40 -070087
88 // Grab a copy of everything we need
John Recke45b1fd2014-04-15 09:50:16 -070089 Rect dirty(mDirty);
John Reck668f0e32014-03-26 15:10:40 -070090 CanvasContext* context = mContext;
91
John Reck668f0e32014-03-26 15:10:40 -070092 // From this point on anything in "this" is *UNSAFE TO ACCESS*
93 if (canUnblockUiThread) {
94 unblockUiThread();
95 }
96
John Recke45b1fd2014-04-15 09:50:16 -070097 context->draw(&dirty);
John Reck668f0e32014-03-26 15:10:40 -070098
99 if (!canUnblockUiThread) {
100 unblockUiThread();
101 }
102}
103
John Recke45b1fd2014-04-15 09:50:16 -0700104static void initTreeInfo(TreeInfo& info) {
John Reck860d1552014-04-11 19:15:05 -0700105 info.prepareTextures = true;
John Recke45b1fd2014-04-15 09:50:16 -0700106 info.performStagingPush = true;
107 info.evaluateAnimations = true;
John Reck860d1552014-04-11 19:15:05 -0700108}
109
John Reckf4198b72014-04-09 17:00:04 -0700110bool DrawFrameTask::syncFrameState() {
John Reck668f0e32014-03-26 15:10:40 -0700111 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700112 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700113 mContext->makeCurrent();
114 Caches::getInstance().textureCache.resetMarkInUse();
115 TreeInfo info;
John Recke45b1fd2014-04-15 09:50:16 -0700116 initTreeInfo(info);
John Reck860d1552014-04-11 19:15:05 -0700117 mContext->processLayerUpdates(&mLayers, info);
John Recke45b1fd2014-04-15 09:50:16 -0700118 mContext->prepareTree(info);
John Reck52244ff2014-05-01 21:27:37 -0700119 if (info.hasAnimations) {
120 // TODO: dirty calculations, for now just do a full-screen inval
121 mDirty.setEmpty();
122 }
John Reck860d1552014-04-11 19:15:05 -0700123 // If prepareTextures is false, we ran out of texture cache space
124 return !info.hasFunctors && info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700125}
126
127void DrawFrameTask::unblockUiThread() {
128 AutoMutex _lock(mLock);
129 mSignal.signal();
130}
131
John Reck668f0e32014-03-26 15:10:40 -0700132} /* namespace renderthread */
133} /* namespace uirenderer */
134} /* namespace android */