• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H
16 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <functional>
21 #include <mutex>
22 #include <thread>
23 #include <unordered_map>
24 #include <vector>
25 #include <event_handler.h>
26 
27 #include "common/rs_thread_handler.h"
28 #include "common/rs_thread_looper.h"
29 #include "pipeline/rs_canvas_render_node.h"
30 #include "render_thread/rs_render_thread_visitor.h"
31 #include "platform/drawing/rs_vsync_client.h"
32 #ifdef RS_ENABLE_GPU
33 #include "render_context/render_context.h"
34 #endif
35 #include "render_thread/jank_detector/rs_jank_detector.h"
36 #include "transaction/rs_transaction_proxy.h"
37 #include "vsync_receiver.h"
38 
39 namespace OHOS {
40 namespace Rosen {
41 class HighContrastObserver;
42 class RSRenderThread final {
43 public:
44     static RSRenderThread& Instance();
45 
46     void Start();
47     void Stop();
48 
49     void Detach(NodeId id);
50     void RecvTransactionData(std::unique_ptr<RSTransactionData>& transactionData);
51     void RequestNextVSync();
52     void PostTask(RSTaskMessage::RSTask task);
53     void PostSyncTask(RSTaskMessage::RSTask task);
54     void PostPreTask();
55     void UpdateWindowStatus(bool active);
56 
57     int32_t GetTid();
58 
59     std::string DumpRenderTree() const;
60 #ifdef RS_ENABLE_GPU
GetRenderContext()61     RenderContext* GetRenderContext()
62     {
63         return renderContext_;
64     }
65 #endif
GetContext()66     RSContext& GetContext()
67     {
68         return *context_;
69     }
GetContext()70     const RSContext& GetContext() const
71     {
72         return *context_;
73     }
GetUITimestamp()74     uint64_t GetUITimestamp() const
75     {
76         return uiTimestamp_;
77     }
SetHighContrast(bool enabled)78     void SetHighContrast(bool enabled)
79     {
80         isHighContrastEnabled_  = enabled;
81         isHighContrastChanged_ = true;
82     }
IsHighContrastEnabled()83     bool IsHighContrastEnabled() const
84     {
85         return isHighContrastEnabled_;
86     }
IsHighContrastChanged()87     bool IsHighContrastChanged() const
88     {
89         return isHighContrastChanged_;
90     }
ResetHighContrastChanged()91     void ResetHighContrastChanged()
92     {
93         isHighContrastChanged_ = false;
94     }
SetCacheDir(const std::string & filePath)95     void SetCacheDir(const std::string& filePath)
96     {
97         cacheDir_ = filePath;
98     }
99 
100     // If disabled partial render, rt forces to render whole frame
SetRTRenderForced(bool isRenderForced)101     void SetRTRenderForced(bool isRenderForced)
102     {
103         if ((isRTRenderForced_ != isRenderForced)) {
104             isRTRenderForced_ = isRenderForced;
105         }
106     }
107 
AddSurfaceChangedCallBack(uint64_t id,const std::function<void (float,float,float,float)> & callback)108     void AddSurfaceChangedCallBack(uint64_t id,
109         const std::function<void(float, float, float, float)>& callback)
110     {
111         if (visitor_ == nullptr) {
112             visitor_ = std::make_shared<RSRenderThreadVisitor>();
113         }
114         visitor_->AddSurfaceChangedCallBack(id, callback);
115     }
116 
RemoveSurfaceChangedCallBack(uint64_t id)117     void RemoveSurfaceChangedCallBack(uint64_t id)
118     {
119         if (visitor_) {
120             visitor_->RemoveSurfaceChangedCallBack(id);
121         }
122     }
123 
124 private:
125     RSRenderThread();
126     ~RSRenderThread();
127 
128     RSRenderThread(const RSRenderThread&) = delete;
129     RSRenderThread(const RSRenderThread&&) = delete;
130     RSRenderThread& operator=(const RSRenderThread&) = delete;
131     RSRenderThread& operator=(const RSRenderThread&&) = delete;
132 
133     void RenderLoop();
134     void CreateAndInitRenderContextIfNeed();
135 
136     void OnVsync(uint64_t timestamp, int64_t frameCount);
137     void ProcessCommands();
138     void Animate(uint64_t timestamp);
139     void Render();
140     void SendCommands();
141     void ReleasePixelMapInBackgroundThread();
142 #ifdef CROSS_PLATFORM
143     void PrepareCommandForCrossPlatform(std::vector<std::unique_ptr<RSTransactionData>>& cmds);
144 #endif
145 
146     std::atomic_bool running_ = false;
147     std::atomic_bool hasSkipVsync_ = false;
148     std::atomic_int activeWindowCnt_ = 0;
149     std::unique_ptr<std::thread> thread_ = nullptr;
150     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
151     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
152     std::shared_ptr<VSyncReceiver> receiver_ = nullptr;
153     RSTaskMessage::RSTask preTask_ = nullptr;
154     RSTaskMessage::RSTask mainFunc_;
155 
156     std::mutex mutex_;
157     std::mutex cmdMutex_;
158     std::mutex rtMutex_;
159     std::vector<std::unique_ptr<RSTransactionData>> cmds_;
160     bool hasRunningAnimation_ = false;
161     std::shared_ptr<RSRenderThreadVisitor> visitor_;
162 
163     uint64_t timestamp_ = 0;
164     uint64_t prevTimestamp_ = 0;
165     uint64_t lastAnimateTimestamp_ = 0;
166     int32_t tid_ = -1;
167     uint64_t mValue = 0;
168 
169     uint64_t uiTimestamp_ = 0;
170     uint64_t commandTimestamp_ = 0;
171 
172     // for overdraw
173     bool isOverDrawEnabledOfCurFrame_ = false;
174     bool isOverDrawEnabledOfLastFrame_ = false;
175 
176     // for jank frame detector
177     std::shared_ptr<RSJankDetector> jankDetector_;
178 
179     std::shared_ptr<RSContext> context_;
180 #ifdef RS_ENABLE_GPU
181     RenderContext* renderContext_ = nullptr;
182 #endif
183     std::shared_ptr<HighContrastObserver> highContrastObserver_;
184     std::atomic_bool isHighContrastEnabled_ = false;
185     std::atomic_bool isHighContrastChanged_ = false;
186 
187     std::string cacheDir_;
188     bool isRTRenderForced_ = false;
189 #ifdef ROSEN_PREVIEW
190     std::atomic_bool isRunning_ = false;
191 #endif
192 };
193 } // namespace Rosen
194 } // namespace OHOS
195 
196 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H
197