blob: 6a61a2bb645f7ef4ac5df5ced9fb993ed00cb780 [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#ifndef DRAWFRAMETASK_H
17#define DRAWFRAMETASK_H
18
Bo Liu6a3fc602021-07-17 16:42:13 -040019#include <optional>
John Reckd72e0a32014-05-29 18:56:11 -070020#include <vector>
21
Bo Liu6a3fc602021-07-17 16:42:13 -040022#include <performance_hint_private.h>
John Reck668f0e32014-03-26 15:10:40 -070023#include <utils/Condition.h>
24#include <utils/Mutex.h>
John Reck087bc0c2014-04-04 16:20:08 -070025#include <utils/StrongPointer.h>
John Reck668f0e32014-03-26 15:10:40 -070026
27#include "RenderTask.h"
28
John Reckba6adf62015-02-19 14:36:50 -080029#include "../FrameInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070030#include "../Rect.h"
John Recka5dda642014-05-22 15:43:54 -070031#include "../TreeInfo.h"
John Reck668f0e32014-03-26 15:10:40 -070032
33namespace android {
34namespace uirenderer {
35
36class DeferredLayerUpdater;
John Reck668f0e32014-03-26 15:10:40 -070037class RenderNode;
38
39namespace renderthread {
40
41class CanvasContext;
42class RenderThread;
43
John Reck9a17da82016-04-18 11:17:52 -070044namespace SyncResult {
45enum {
46 OK = 0,
47 UIRedrawRequired = 1 << 0,
48 LostSurfaceRewardIfFound = 1 << 1,
49 ContextIsStopped = 1 << 2,
John Reckcc2eee82018-05-17 10:44:00 -070050 FrameDropped = 1 << 3,
John Reckf9be7792014-05-02 18:21:16 -070051};
John Reck9a17da82016-04-18 11:17:52 -070052}
John Reckf9be7792014-05-02 18:21:16 -070053
John Reck668f0e32014-03-26 15:10:40 -070054/*
55 * This is a special Super Task. It is re-used multiple times by RenderProxy,
Chris Craik003cc3d2015-10-16 10:24:55 -070056 * and contains state (such as layer updaters & new DisplayLists) that is
John Reck668f0e32014-03-26 15:10:40 -070057 * tracked across many frames not just a single frame.
58 * It is the sync-state task, and will kick off the post-sync draw
59 */
John Reckf8441e62017-10-23 13:10:41 -070060class DrawFrameTask {
John Reck668f0e32014-03-26 15:10:40 -070061public:
62 DrawFrameTask();
63 virtual ~DrawFrameTask();
64
Bo Liu6a3fc602021-07-17 16:42:13 -040065 void setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode,
66 int32_t uiThreadId, int32_t renderThreadId);
John Reckf138b172017-09-08 11:00:42 -070067 void setContentDrawBounds(int left, int top, int right, int bottom) {
68 mContentDrawBounds.set(left, top, right, bottom);
69 }
John Reck668f0e32014-03-26 15:10:40 -070070
John Reckd72e0a32014-05-29 18:56:11 -070071 void pushLayerUpdate(DeferredLayerUpdater* layer);
72 void removeLayerUpdate(DeferredLayerUpdater* layer);
John Reck668f0e32014-03-26 15:10:40 -070073
John Reck2de950d2017-01-25 10:58:30 -080074 int drawFrame();
John Reckba6adf62015-02-19 14:36:50 -080075
76 int64_t* frameInfo() { return mFrameInfo; }
John Reck668f0e32014-03-26 15:10:40 -070077
John Reckf8441e62017-10-23 13:10:41 -070078 void run();
John Reck668f0e32014-03-26 15:10:40 -070079
Mihai Popa95688002018-02-23 16:10:11 +000080 void setFrameCallback(std::function<void(int64_t)>&& callback) {
81 mFrameCallback = std::move(callback);
82 }
83
John Reckcc2eee82018-05-17 10:44:00 -070084 void setFrameCompleteCallback(std::function<void(int64_t)>&& callback) {
85 mFrameCompleteCallback = std::move(callback);
86 }
87
John Reck668f0e32014-03-26 15:10:40 -070088private:
Bo Liu6a3fc602021-07-17 16:42:13 -040089 class HintSessionWrapper {
90 public:
91 HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId);
92 ~HintSessionWrapper();
93
94 void updateTargetWorkDuration(long targetDurationNanos);
95 void reportActualWorkDuration(long actualDurationNanos);
96
97 private:
98 APerformanceHintSession* mHintSession = nullptr;
99 };
100
John Reck18f16e62014-05-02 16:46:41 -0700101 void postAndWait();
John Recka5dda642014-05-22 15:43:54 -0700102 bool syncFrameState(TreeInfo& info);
John Reck668f0e32014-03-26 15:10:40 -0700103 void unblockUiThread();
John Reck668f0e32014-03-26 15:10:40 -0700104
John Reck668f0e32014-03-26 15:10:40 -0700105 Mutex mLock;
106 Condition mSignal;
107
John Reck18f16e62014-05-02 16:46:41 -0700108 RenderThread* mRenderThread;
John Reck668f0e32014-03-26 15:10:40 -0700109 CanvasContext* mContext;
Skuhneea7a7fb2015-08-28 07:10:31 -0700110 RenderNode* mTargetNode = nullptr;
Bo Liu6a3fc602021-07-17 16:42:13 -0400111 int32_t mUiThreadId = -1;
112 int32_t mRenderThreadId = -1;
John Reckf138b172017-09-08 11:00:42 -0700113 Rect mContentDrawBounds;
John Reck668f0e32014-03-26 15:10:40 -0700114
115 /*********************************************
116 * Single frame data
117 *********************************************/
John Reck1bcacfd2017-11-03 10:12:19 -0700118 std::vector<sp<DeferredLayerUpdater> > mLayers;
John Reck668f0e32014-03-26 15:10:40 -0700119
John Reckf9be7792014-05-02 18:21:16 -0700120 int mSyncResult;
John Reckbe3fba02015-07-06 13:49:58 -0700121 int64_t mSyncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800122
123 int64_t mFrameInfo[UI_THREAD_FRAME_INFO_SIZE];
Mihai Popa95688002018-02-23 16:10:11 +0000124
125 std::function<void(int64_t)> mFrameCallback;
John Reckcc2eee82018-05-17 10:44:00 -0700126 std::function<void(int64_t)> mFrameCompleteCallback;
Bo Liu027b2182021-03-18 16:50:38 -0400127
Bo Liu4d0f1f12021-05-06 16:49:40 -0400128 nsecs_t mLastDequeueBufferDuration = 0;
Bo Liu027b2182021-03-18 16:50:38 -0400129 nsecs_t mLastTargetWorkDuration = 0;
Bo Liu6a3fc602021-07-17 16:42:13 -0400130 std::optional<HintSessionWrapper> mHintSessionWrapper;
John Reck668f0e32014-03-26 15:10:40 -0700131};
132
133} /* namespace renderthread */
134} /* namespace uirenderer */
135} /* namespace android */
136
137#endif /* DRAWFRAMETASK_H */