blob: 763e72747e830c1bff6d810b5a7eb8fe59f6a2e7 [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
John Reckd72e0a32014-05-29 18:56:11 -070024#include "../DeferredLayerUpdater.h"
John Reck668f0e32014-03-26 15:10:40 -070025#include "../DisplayList.h"
26#include "../RenderNode.h"
27#include "CanvasContext.h"
28#include "RenderThread.h"
29
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
John Reck18f16e62014-05-02 16:46:41 -070034DrawFrameTask::DrawFrameTask()
35 : mRenderThread(NULL)
36 , mContext(NULL)
John Reckf9be7792014-05-02 18:21:16 -070037 , mFrameTimeNanos(0)
John Reckfe5e7b72014-05-23 17:42:28 -070038 , mRecordDurationNanos(0)
39 , mDensity(1.0f) // safe enough default
John Reckf9be7792014-05-02 18:21:16 -070040 , mSyncResult(kSync_OK) {
John Reck668f0e32014-03-26 15:10:40 -070041}
42
43DrawFrameTask::~DrawFrameTask() {
44}
45
John Reck18f16e62014-05-02 16:46:41 -070046void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) {
47 mRenderThread = thread;
John Reck668f0e32014-03-26 15:10:40 -070048 mContext = context;
49}
50
John Reckd72e0a32014-05-29 18:56:11 -070051void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) {
52 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!");
John Reck087bc0c2014-04-04 16:20:08 -070053
John Reckd72e0a32014-05-29 18:56:11 -070054 for (size_t i = 0; i < mLayers.size(); i++) {
55 if (mLayers[i].get() == layer) {
56 return;
57 }
58 }
59 mLayers.push_back(layer);
John Reck668f0e32014-03-26 15:10:40 -070060}
61
John Reckd72e0a32014-05-29 18:56:11 -070062void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -070063 for (size_t i = 0; i < mLayers.size(); i++) {
John Reckd72e0a32014-05-29 18:56:11 -070064 if (mLayers[i].get() == layer) {
65 mLayers.erase(mLayers.begin() + i);
66 return;
John Reck668f0e32014-03-26 15:10:40 -070067 }
68 }
69}
70
John Reckfe5e7b72014-05-23 17:42:28 -070071int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) {
John Reck668f0e32014-03-26 15:10:40 -070072 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
73
John Reckf9be7792014-05-02 18:21:16 -070074 mSyncResult = kSync_OK;
John Reck18f16e62014-05-02 16:46:41 -070075 mFrameTimeNanos = frameTimeNanos;
John Reckfe5e7b72014-05-23 17:42:28 -070076 mRecordDurationNanos = recordDurationNanos;
John Reck18f16e62014-05-02 16:46:41 -070077 postAndWait();
John Reck668f0e32014-03-26 15:10:40 -070078
79 // Reset the single-frame data
John Reck18f16e62014-05-02 16:46:41 -070080 mFrameTimeNanos = 0;
John Reckfe5e7b72014-05-23 17:42:28 -070081 mRecordDurationNanos = 0;
John Reckf9be7792014-05-02 18:21:16 -070082
83 return mSyncResult;
John Reck668f0e32014-03-26 15:10:40 -070084}
85
John Reck18f16e62014-05-02 16:46:41 -070086void DrawFrameTask::postAndWait() {
John Reck087bc0c2014-04-04 16:20:08 -070087 AutoMutex _lock(mLock);
John Reck73b7a4d2014-07-23 14:54:04 -070088 mRenderThread->queueAndWait(this, mSignal, mLock);
John Reck087bc0c2014-04-04 16:20:08 -070089}
90
John Reck668f0e32014-03-26 15:10:40 -070091void DrawFrameTask::run() {
92 ATRACE_NAME("DrawFrame");
93
John Reckfe5e7b72014-05-23 17:42:28 -070094 mContext->profiler().setDensity(mDensity);
95 mContext->profiler().startFrame(mRecordDurationNanos);
96
John Recka5dda642014-05-22 15:43:54 -070097 bool canUnblockUiThread;
98 bool canDrawThisFrame;
99 {
John Reck3b202512014-06-23 13:13:08 -0700100 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState());
John Recka5dda642014-05-22 15:43:54 -0700101 canUnblockUiThread = syncFrameState(info);
102 canDrawThisFrame = info.out.canDrawThisFrame;
103 }
John Reck668f0e32014-03-26 15:10:40 -0700104
105 // Grab a copy of everything we need
John Reck668f0e32014-03-26 15:10:40 -0700106 CanvasContext* context = mContext;
107
John Reck668f0e32014-03-26 15:10:40 -0700108 // From this point on anything in "this" is *UNSAFE TO ACCESS*
109 if (canUnblockUiThread) {
110 unblockUiThread();
111 }
112
John Recka5dda642014-05-22 15:43:54 -0700113 if (CC_LIKELY(canDrawThisFrame)) {
John Recke4267ea2014-06-03 15:53:15 -0700114 context->draw();
John Recka5dda642014-05-22 15:43:54 -0700115 }
John Reck668f0e32014-03-26 15:10:40 -0700116
117 if (!canUnblockUiThread) {
118 unblockUiThread();
119 }
120}
121
John Recka5dda642014-05-22 15:43:54 -0700122bool DrawFrameTask::syncFrameState(TreeInfo& info) {
John Reck668f0e32014-03-26 15:10:40 -0700123 ATRACE_CALL();
John Reck18f16e62014-05-02 16:46:41 -0700124 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos);
John Reck860d1552014-04-11 19:15:05 -0700125 mContext->makeCurrent();
126 Caches::getInstance().textureCache.resetMarkInUse();
John Reckd72e0a32014-05-29 18:56:11 -0700127
128 for (size_t i = 0; i < mLayers.size(); i++) {
John Reck68bfe0a2014-06-24 15:34:58 -0700129 mContext->processLayerUpdate(mLayers[i].get());
John Reckd72e0a32014-05-29 18:56:11 -0700130 }
131 mLayers.clear();
John Reckd72e0a32014-05-29 18:56:11 -0700132 mContext->prepareTree(info);
133
John Reckf9be7792014-05-02 18:21:16 -0700134 if (info.out.hasAnimations) {
John Reckf9be7792014-05-02 18:21:16 -0700135 if (info.out.requiresUiRedraw) {
136 mSyncResult |= kSync_UIRedrawRequired;
137 }
John Reck52244ff2014-05-01 21:27:37 -0700138 }
John Reck860d1552014-04-11 19:15:05 -0700139 // If prepareTextures is false, we ran out of texture cache space
Bo Liu826b5642014-05-13 16:47:27 -0700140 return info.prepareTextures;
John Reck668f0e32014-03-26 15:10:40 -0700141}
142
143void DrawFrameTask::unblockUiThread() {
144 AutoMutex _lock(mLock);
145 mSignal.signal();
146}
147
John Reck668f0e32014-03-26 15:10:40 -0700148} /* namespace renderthread */
149} /* namespace uirenderer */
150} /* namespace android */