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