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