blob: d6fc292d5900ddca582bd3ec3b8b6604804b275f [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 Liu2b739bb2021-11-10 19:20:03 -050019#include <android/performance_hint.h>
John Reck668f0e32014-03-26 15:10:40 -070020#include <utils/Condition.h>
21#include <utils/Mutex.h>
John Reck087bc0c2014-04-04 16:20:08 -070022#include <utils/StrongPointer.h>
John Reck668f0e32014-03-26 15:10:40 -070023
Bo Liu2b739bb2021-11-10 19:20:03 -050024#include <optional>
25#include <vector>
John Reck668f0e32014-03-26 15:10:40 -070026
John Reckba6adf62015-02-19 14:36:50 -080027#include "../FrameInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070028#include "../Rect.h"
John Recka5dda642014-05-22 15:43:54 -070029#include "../TreeInfo.h"
Bo Liu2b739bb2021-11-10 19:20:03 -050030#include "RenderTask.h"
John Reck668f0e32014-03-26 15:10:40 -070031
32namespace android {
33namespace uirenderer {
34
35class DeferredLayerUpdater;
John Reck668f0e32014-03-26 15:10:40 -070036class RenderNode;
37
38namespace renderthread {
39
40class CanvasContext;
41class RenderThread;
42
John Reck9a17da82016-04-18 11:17:52 -070043namespace SyncResult {
44enum {
45 OK = 0,
46 UIRedrawRequired = 1 << 0,
47 LostSurfaceRewardIfFound = 1 << 1,
48 ContextIsStopped = 1 << 2,
John Reckcc2eee82018-05-17 10:44:00 -070049 FrameDropped = 1 << 3,
John Reckf9be7792014-05-02 18:21:16 -070050};
John Reck9a17da82016-04-18 11:17:52 -070051}
John Reckf9be7792014-05-02 18:21:16 -070052
John Reck668f0e32014-03-26 15:10:40 -070053/*
54 * This is a special Super Task. It is re-used multiple times by RenderProxy,
Chris Craik003cc3d2015-10-16 10:24:55 -070055 * and contains state (such as layer updaters & new DisplayLists) that is
John Reck668f0e32014-03-26 15:10:40 -070056 * tracked across many frames not just a single frame.
57 * It is the sync-state task, and will kick off the post-sync draw
58 */
John Reckf8441e62017-10-23 13:10:41 -070059class DrawFrameTask {
John Reck668f0e32014-03-26 15:10:40 -070060public:
61 DrawFrameTask();
62 virtual ~DrawFrameTask();
63
Bo Liu6a3fc602021-07-17 16:42:13 -040064 void setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode,
65 int32_t uiThreadId, int32_t renderThreadId);
John Reckf138b172017-09-08 11:00:42 -070066 void setContentDrawBounds(int left, int top, int right, int bottom) {
67 mContentDrawBounds.set(left, top, right, bottom);
68 }
John Reck668f0e32014-03-26 15:10:40 -070069
John Reckd72e0a32014-05-29 18:56:11 -070070 void pushLayerUpdate(DeferredLayerUpdater* layer);
71 void removeLayerUpdate(DeferredLayerUpdater* layer);
John Reck668f0e32014-03-26 15:10:40 -070072
John Reck2de950d2017-01-25 10:58:30 -080073 int drawFrame();
John Reckba6adf62015-02-19 14:36:50 -080074
75 int64_t* frameInfo() { return mFrameInfo; }
John Reck668f0e32014-03-26 15:10:40 -070076
John Reckf8441e62017-10-23 13:10:41 -070077 void run();
John Reck668f0e32014-03-26 15:10:40 -070078
chaviwb6803712021-12-13 15:46:29 -060079 void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) {
Mihai Popa95688002018-02-23 16:10:11 +000080 mFrameCallback = std::move(callback);
81 }
82
chaviw9c137532021-08-20 12:15:48 -050083 void setFrameCommitCallback(std::function<void(bool)>&& callback) {
84 mFrameCommitCallback = std::move(callback);
85 }
86
87 void setFrameCompleteCallback(std::function<void()>&& callback) {
John Reckcc2eee82018-05-17 10:44:00 -070088 mFrameCompleteCallback = std::move(callback);
89 }
90
chaviwadba0b12022-03-18 17:42:15 -050091 void forceDrawNextFrame() { mForceDrawFrame = true; }
92
John Reck668f0e32014-03-26 15:10:40 -070093private:
Bo Liu6a3fc602021-07-17 16:42:13 -040094 class HintSessionWrapper {
95 public:
96 HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId);
97 ~HintSessionWrapper();
98
99 void updateTargetWorkDuration(long targetDurationNanos);
100 void reportActualWorkDuration(long actualDurationNanos);
101
102 private:
103 APerformanceHintSession* mHintSession = nullptr;
104 };
105
John Reck18f16e62014-05-02 16:46:41 -0700106 void postAndWait();
John Recka5dda642014-05-22 15:43:54 -0700107 bool syncFrameState(TreeInfo& info);
John Reck668f0e32014-03-26 15:10:40 -0700108 void unblockUiThread();
John Reck668f0e32014-03-26 15:10:40 -0700109
John Reck668f0e32014-03-26 15:10:40 -0700110 Mutex mLock;
111 Condition mSignal;
112
John Reck18f16e62014-05-02 16:46:41 -0700113 RenderThread* mRenderThread;
John Reck668f0e32014-03-26 15:10:40 -0700114 CanvasContext* mContext;
Skuhneea7a7fb2015-08-28 07:10:31 -0700115 RenderNode* mTargetNode = nullptr;
Bo Liu6a3fc602021-07-17 16:42:13 -0400116 int32_t mUiThreadId = -1;
117 int32_t mRenderThreadId = -1;
John Reckf138b172017-09-08 11:00:42 -0700118 Rect mContentDrawBounds;
John Reck668f0e32014-03-26 15:10:40 -0700119
120 /*********************************************
121 * Single frame data
122 *********************************************/
John Reck1bcacfd2017-11-03 10:12:19 -0700123 std::vector<sp<DeferredLayerUpdater> > mLayers;
John Reck668f0e32014-03-26 15:10:40 -0700124
John Reckf9be7792014-05-02 18:21:16 -0700125 int mSyncResult;
John Reckbe3fba02015-07-06 13:49:58 -0700126 int64_t mSyncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800127
128 int64_t mFrameInfo[UI_THREAD_FRAME_INFO_SIZE];
Mihai Popa95688002018-02-23 16:10:11 +0000129
chaviwb6803712021-12-13 15:46:29 -0600130 std::function<std::function<void(bool)>(int32_t, int64_t)> mFrameCallback;
chaviw9c137532021-08-20 12:15:48 -0500131 std::function<void(bool)> mFrameCommitCallback;
132 std::function<void()> mFrameCompleteCallback;
Bo Liu027b2182021-03-18 16:50:38 -0400133
Bo Liu4d0f1f12021-05-06 16:49:40 -0400134 nsecs_t mLastDequeueBufferDuration = 0;
Bo Liu027b2182021-03-18 16:50:38 -0400135 nsecs_t mLastTargetWorkDuration = 0;
Bo Liu6a3fc602021-07-17 16:42:13 -0400136 std::optional<HintSessionWrapper> mHintSessionWrapper;
chaviwadba0b12022-03-18 17:42:15 -0500137
138 bool mForceDrawFrame = false;
John Reck668f0e32014-03-26 15:10:40 -0700139};
140
141} /* namespace renderthread */
142} /* namespace uirenderer */
143} /* namespace android */
144
145#endif /* DRAWFRAMETASK_H */