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 <android/performance_hint.h> 20 #include <utils/Condition.h> 21 #include <utils/Mutex.h> 22 #include <utils/StrongPointer.h> 23 24 #include <optional> 25 #include <vector> 26 27 #include "../FrameInfo.h" 28 #include "../Rect.h" 29 #include "../TreeInfo.h" 30 #include "RenderTask.h" 31 32 namespace android { 33 namespace uirenderer { 34 35 class DeferredLayerUpdater; 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, 65 int32_t uiThreadId, int32_t renderThreadId); setContentDrawBounds(int left,int top,int right,int bottom)66 void setContentDrawBounds(int left, int top, int right, int bottom) { 67 mContentDrawBounds.set(left, top, right, bottom); 68 } 69 70 void pushLayerUpdate(DeferredLayerUpdater* layer); 71 void removeLayerUpdate(DeferredLayerUpdater* layer); 72 73 int drawFrame(); 74 frameInfo()75 int64_t* frameInfo() { return mFrameInfo; } 76 77 void run(); 78 setFrameCallback(std::function<std::function<void (bool)> (int32_t,int64_t)> && callback)79 void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) { 80 mFrameCallback = std::move(callback); 81 } 82 setFrameCommitCallback(std::function<void (bool)> && callback)83 void setFrameCommitCallback(std::function<void(bool)>&& callback) { 84 mFrameCommitCallback = std::move(callback); 85 } 86 setFrameCompleteCallback(std::function<void ()> && callback)87 void setFrameCompleteCallback(std::function<void()>&& callback) { 88 mFrameCompleteCallback = std::move(callback); 89 } 90 forceDrawNextFrame()91 void forceDrawNextFrame() { mForceDrawFrame = true; } 92 93 void createHintSession(pid_t uiThreadId, pid_t renderThreadId); 94 95 private: 96 class HintSessionWrapper { 97 public: 98 HintSessionWrapper(int32_t uiThreadId, int32_t renderThreadId); 99 ~HintSessionWrapper(); 100 101 void updateTargetWorkDuration(long targetDurationNanos); 102 void reportActualWorkDuration(long actualDurationNanos); 103 104 private: 105 APerformanceHintSession* mHintSession = nullptr; 106 }; 107 108 void postAndWait(); 109 bool syncFrameState(TreeInfo& info); 110 void unblockUiThread(); 111 112 Mutex mLock; 113 Condition mSignal; 114 115 RenderThread* mRenderThread; 116 CanvasContext* mContext; 117 RenderNode* mTargetNode = nullptr; 118 int32_t mUiThreadId = -1; 119 int32_t mRenderThreadId = -1; 120 Rect mContentDrawBounds; 121 122 /********************************************* 123 * Single frame data 124 *********************************************/ 125 std::vector<sp<DeferredLayerUpdater> > mLayers; 126 127 int mSyncResult; 128 int64_t mSyncQueued; 129 130 int64_t mFrameInfo[UI_THREAD_FRAME_INFO_SIZE]; 131 132 std::function<std::function<void(bool)>(int32_t, int64_t)> mFrameCallback; 133 std::function<void(bool)> mFrameCommitCallback; 134 std::function<void()> mFrameCompleteCallback; 135 136 nsecs_t mLastDequeueBufferDuration = 0; 137 nsecs_t mLastTargetWorkDuration = 0; 138 std::optional<HintSessionWrapper> mHintSessionWrapper; 139 140 bool mForceDrawFrame = false; 141 }; 142 143 } /* namespace renderthread */ 144 } /* namespace uirenderer */ 145 } /* namespace android */ 146 147 #endif /* DRAWFRAMETASK_H */ 148