blob: 3b8786c3fb94dac02b4b525cb5775604c203cb9c [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)
John Reckf9be7792014-05-02 18:21:16 -070036 , mFrameTimeNanos(0)
37 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070038}
39
40DrawFrameTask::~DrawFrameTask() {
41}
42
John Reck18f16e62014-05-02 16:46:41 -070043void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
44 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070045 mContext = context;
46}
47
John Reck668f0e32014-03-26 15:10:40 -070048void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) {
John Reck087bc0c2014-04-04 16:20:08 -070049 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!");
50
John Reck668f0e32014-03-26 15:10:40 -070051 mLayers.push(layer);
52}
53
54void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) {
55 for (size_t i = 0; i < mLayers.size(); i++) {
56 if (mLayers[i] == layer) {
57 mLayers.removeAt(i);
58 break;
59 }
60 }
61}
62
John Reck668f0e32014-03-26 15:10:40 -070063void DrawFrameTask::setDirty(int left, int top, int right, int bottom) {
64 mDirty.set(left, top, right, bottom);
65}
66
John Reckf9be7792014-05-02 18:21:16 -070067int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos) {
John Reck668f0e32014-03-26 15:10:40 -070068 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
69
John Reckf9be7792014-05-02 18:21:16 -070070 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070071 mFrameTimeNanos = frameTimeNanos;
72 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070073
74 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070075 mFrameTimeNanos = 0;
John Reck668f0e32014-03-26 15:10:40 -070076 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -070077
78 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070079}
80
John Reck18f16e62014-05-02 16:46:41 -070081void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070082 AutoMutex _lock(mLock);
John Reck18f16e62014-05-02 16:46:41 -070083 mRenderThread->queue(this);
John Reck087bc0c2014-04-04 16:20:08 -070084 mSignal.wait(mLock);
85}
86
John Reck668f0e32014-03-26 15:10:40 -070087void DrawFrameTask::run() {
88 ATRACE_NAME("DrawFrame");
89
John Reckf4198b72014-04-09 17:00:04 -070090 bool canUnblockUiThread = syncFrameState();
John Reck668f0e32014-03-26 15:10:40 -070091
92 // Grab a copy of everything we need
John Recke45b1fd2014-04-15 09:50:16 -070093 Rect dirty(mDirty);
John Reck668f0e32014-03-26 15:10:40 -070094 CanvasContext* context = mContext;
95
John Reck668f0e32014-03-26 15:10:40 -070096 // From this point on anything in "this" is *UNSAFE TO ACCESS*
97 if (canUnblockUiThread) {
98 unblockUiThread();
99 }
100
John Recke45b1fd2014-04-15 09:50:16 -0700101 context->draw(&dirty);
John Reck668f0e32014-03-26 15:10:40 -0700102
103 if (!canUnblockUiThread) {
104 unblockUiThread();
105 }
106}
107
John Recke45b1fd2014-04-15 09:50:16 -0700108static void initTreeInfo(TreeInfo& info) {
John Reck860d1552014-04-11 19:15:05 -0700109 info.prepareTextures = true;
John Recke45b1fd2014-04-15 09:50:16 -0700110 info.performStagingPush = true;
111 info.evaluateAnimations = true;
John Reck860d1552014-04-11 19:15:05 -0700112}
113
John Reckf4198b72014-04-09 17:00:04 -0700114bool DrawFrameTask::syncFrameState() {
John Reck668f0e32014-03-26 15:10:40 -0700115 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700116 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700117 mContext->makeCurrent();
118 Caches::getInstance().textureCache.resetMarkInUse();
119 TreeInfo info;
John Recke45b1fd2014-04-15 09:50:16 -0700120 initTreeInfo(info);
John Reckf9be7792014-05-02 18:21:16 -0700121 mContext->prepareDraw(&mLayers, info);
122 if (info.out.hasAnimations) {
John Reck52244ff2014-05-01 21:27:37 -0700123 // TODO: dirty calculations, for now just do a full-screen inval
124 mDirty.setEmpty();
John Reckf9be7792014-05-02 18:21:16 -0700125 if (info.out.requiresUiRedraw) {
126 mSyncResult |= kSync_UIRedrawRequired;
127 }
John Reck52244ff2014-05-01 21:27:37 -0700128 }
John Reck860d1552014-04-11 19:15:05 -0700129 // If prepareTextures is false, we ran out of texture cache space
John Reckf9be7792014-05-02 18:21:16 -0700130 return !info.out.hasFunctors && info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700131}
132
133void DrawFrameTask::unblockUiThread() {
134 AutoMutex _lock(mLock);
135 mSignal.signal();
136}
137
John Reck668f0e32014-03-26 15:10:40 -0700138} /* namespace renderthread */
139} /* namespace uirenderer */
140} /* namespace android */