1 /*
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 
19 #include <utils/Condition.h>
20 #include <utils/Mutex.h>
21 #include <utils/StrongPointer.h>
22 
23 #include <optional>
24 #include <vector>
25 
26 #include "../FrameInfo.h"
27 #include "../Rect.h"
28 #include "../TreeInfo.h"
29 #include "RenderTask.h"
30 #include "SkColorSpace.h"
31 #include "SwapBehavior.h"
32 #include "utils/TimeUtils.h"
33 #ifdef __ANDROID__  // Layoutlib does not support hardware acceleration
34 #include <android/hardware_buffer.h>
35 #endif
36 #include "HardwareBufferRenderParams.h"
37 
38 namespace android {
39 
40 namespace uirenderer {
41 
42 class DeferredLayerUpdater;
43 class RenderNode;
44 
45 namespace renderthread {
46 
47 class CanvasContext;
48 class RenderThread;
49 
50 namespace SyncResult {
51 enum {
52     OK = 0,
53     UIRedrawRequired = 1 << 0,
54     LostSurfaceRewardIfFound = 1 << 1,
55     ContextIsStopped = 1 << 2,
56     FrameDropped = 1 << 3,
57 };
58 }
59 
60 /*
61  * This is a special Super Task. It is re-used multiple times by RenderProxy,
62  * and contains state (such as layer updaters & new DisplayLists) that is
63  * tracked across many frames not just a single frame.
64  * It is the sync-state task, and will kick off the post-sync draw
65  */
66 class DrawFrameTask {
67 public:
68     DrawFrameTask();
69     virtual ~DrawFrameTask();
70 
71     void setContext(RenderThread* thread, CanvasContext* context, RenderNode* targetNode);
setContentDrawBounds(int left,int top,int right,int bottom)72     void setContentDrawBounds(int left, int top, int right, int bottom) {
73         mContentDrawBounds.set(left, top, right, bottom);
74     }
75 
76     void pushLayerUpdate(DeferredLayerUpdater* layer);
77     void removeLayerUpdate(DeferredLayerUpdater* layer);
78 
79     int drawFrame();
80 
frameInfo()81     int64_t* frameInfo() { return mFrameInfo; }
82 
83     void run();
84 
setFrameCallback(std::function<std::function<void (bool)> (int32_t,int64_t)> && callback)85     void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) {
86         mFrameCallback = std::move(callback);
87     }
88 
setFrameCommitCallback(std::function<void (bool)> && callback)89     void setFrameCommitCallback(std::function<void(bool)>&& callback) {
90         mFrameCommitCallback = std::move(callback);
91     }
92 
setFrameCompleteCallback(std::function<void ()> && callback)93     void setFrameCompleteCallback(std::function<void()>&& callback) {
94         mFrameCompleteCallback = std::move(callback);
95     }
96 
forceDrawNextFrame()97     void forceDrawNextFrame() { mForceDrawFrame = true; }
98 
setHardwareBufferRenderParams(const HardwareBufferRenderParams & params)99     void setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) {
100         mHardwareBufferParams = params;
101     }
102 
setRenderSdrHdrRatio(float ratio)103     void setRenderSdrHdrRatio(float ratio) { mRenderSdrHdrRatio = ratio; }
104 
105 private:
106     void postAndWait();
107     bool syncFrameState(TreeInfo& info);
108     void unblockUiThread();
109 
110     Mutex mLock;
111     Condition mSignal;
112 
113     RenderThread* mRenderThread;
114     CanvasContext* mContext;
115     RenderNode* mTargetNode = nullptr;
116     Rect mContentDrawBounds;
117     float mRenderSdrHdrRatio = 1.f;
118 
119     /*********************************************
120      *  Single frame data
121      *********************************************/
122     std::vector<sp<DeferredLayerUpdater> > mLayers;
123 
124     int mSyncResult;
125     int64_t mSyncQueued;
126 
127     int64_t mFrameInfo[UI_THREAD_FRAME_INFO_SIZE];
128 
129     HardwareBufferRenderParams mHardwareBufferParams;
130     std::function<std::function<void(bool)>(int32_t, int64_t)> mFrameCallback;
131     std::function<void(bool)> mFrameCommitCallback;
132     std::function<void()> mFrameCompleteCallback;
133 
134     bool mForceDrawFrame = false;
135 };
136 
137 } /* namespace renderthread */
138 } /* namespace uirenderer */
139 } /* namespace android */
140 
141 #endif /* DRAWFRAMETASK_H */
142