• 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 "jank_detector/rs_jank_detector.h"
30 #include "pipeline/rs_canvas_render_node.h"
31 #include "pipeline/rs_render_thread_visitor.h"
32 #include "platform/drawing/rs_vsync_client.h"
33 #ifdef NEW_RENDER_CONTEXT
34 #include "render_backend/render_context_factory.h"
35 #include "drawing_context.h"
36 #else
37 #include "render_context/render_context.h"
38 #endif
39 #include "transaction/rs_transaction_proxy.h"
40 #include "vsync_receiver.h"
41 
42 namespace OHOS {
43 namespace Rosen {
44 class HighContrastObserver;
45 class RSRenderThread final {
46 public:
47     static RSRenderThread& Instance();
48 
49     void Start();
50     void Stop();
51 
52     void Detach(NodeId id);
53     void RecvTransactionData(std::unique_ptr<RSTransactionData>& transactionData);
54     void RequestNextVSync();
55     void PostTask(RSTaskMessage::RSTask task);
56     void PostSyncTask(RSTaskMessage::RSTask task);
57     void PostPreTask();
58     void UpdateWindowStatus(bool active);
59 
60     int32_t GetTid();
61 
62     std::string DumpRenderTree() const;
63 #ifdef NEW_RENDER_CONTEXT
GetRenderContext()64     std::shared_ptr<RenderContextBase> GetRenderContext() const
65     {
66         return renderContext_;
67     }
GetDrawingContext()68     std::shared_ptr<DrawingContext> GetDrawingContext() const
69     {
70         return drawingContext_;
71     }
72 #else
GetRenderContext()73     RenderContext* GetRenderContext()
74     {
75         return renderContext_;
76     }
77 #endif
GetContext()78     RSContext& GetContext()
79     {
80         return *context_;
81     }
GetContext()82     const RSContext& GetContext() const
83     {
84         return *context_;
85     }
GetUITimestamp()86     uint64_t GetUITimestamp() const
87     {
88         return uiTimestamp_;
89     }
SetHighContrast(bool enabled)90     void SetHighContrast(bool enabled)
91     {
92         isHighContrastEnabled_  = enabled;
93         isHighContrastChanged_ = true;
94     }
IsHighContrastEnabled()95     bool IsHighContrastEnabled() const
96     {
97         return isHighContrastEnabled_;
98     }
IsHighContrastChanged()99     bool IsHighContrastChanged() const
100     {
101         return isHighContrastChanged_;
102     }
ResetHighContrastChanged()103     void ResetHighContrastChanged()
104     {
105         isHighContrastChanged_ = false;
106     }
SetCacheDir(const std::string & filePath)107     void SetCacheDir(const std::string& filePath)
108     {
109         cacheDir_ = filePath;
110     }
111 
112     // If disabled partial render, rt forces to render whole frame
SetRTRenderForced(bool isRenderForced)113     void SetRTRenderForced(bool isRenderForced)
114     {
115         if ((isRTRenderForced_ != isRenderForced)) {
116             isRTRenderForced_ = isRenderForced;
117         }
118     }
119 
AddSurfaceChangedCallBack(uint64_t id,const std::function<void (float,float,float,float)> & callback)120     void AddSurfaceChangedCallBack(uint64_t id,
121         const std::function<void(float, float, float, float)>& callback)
122     {
123         if (visitor_ == nullptr) {
124             visitor_ = std::make_shared<RSRenderThreadVisitor>();
125         }
126         visitor_->AddSurfaceChangedCallBack(id, callback);
127     }
128 
RemoveSurfaceChangedCallBack(uint64_t id)129     void RemoveSurfaceChangedCallBack(uint64_t id)
130     {
131         if (visitor_) {
132             visitor_->RemoveSurfaceChangedCallBack(id);
133         }
134     }
135 
136 private:
137     RSRenderThread();
138     ~RSRenderThread();
139 
140     RSRenderThread(const RSRenderThread&) = delete;
141     RSRenderThread(const RSRenderThread&&) = delete;
142     RSRenderThread& operator=(const RSRenderThread&) = delete;
143     RSRenderThread& operator=(const RSRenderThread&&) = delete;
144 
145     void RenderLoop();
146     void CreateAndInitRenderContextIfNeed();
147 
148     void OnVsync(uint64_t timestamp, int64_t frameCount);
149     void ProcessCommands();
150     void Animate(uint64_t timestamp);
151     void Render();
152     void SendCommands();
153     void ReleasePixelMapInBackgroundThread();
154 #ifdef CROSS_PLATFORM
155     void PrepareCommandForCrossPlatform(std::vector<std::unique_ptr<RSTransactionData>>& cmds);
156 #endif
157 
158     std::atomic_bool running_ = false;
159     std::atomic_bool hasSkipVsync_ = false;
160     std::atomic_int activeWindowCnt_ = 0;
161     std::unique_ptr<std::thread> thread_ = nullptr;
162     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
163     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
164     std::shared_ptr<VSyncReceiver> receiver_ = nullptr;
165     RSTaskMessage::RSTask preTask_ = nullptr;
166     RSTaskMessage::RSTask mainFunc_;
167 
168     std::mutex mutex_;
169     std::mutex cmdMutex_;
170     std::mutex rtMutex_;
171     std::vector<std::unique_ptr<RSTransactionData>> cmds_;
172     bool hasRunningAnimation_ = false;
173     std::shared_ptr<RSRenderThreadVisitor> visitor_;
174 
175     uint64_t timestamp_ = 0;
176     uint64_t prevTimestamp_ = 0;
177     uint64_t lastAnimateTimestamp_ = 0;
178     int32_t tid_ = -1;
179     uint64_t mValue = 0;
180 
181     uint64_t uiTimestamp_ = 0;
182     uint64_t commandTimestamp_ = 0;
183 
184     // for overdraw
185     bool isOverDrawEnabledOfCurFrame_ = false;
186     bool isOverDrawEnabledOfLastFrame_ = false;
187 
188     // for jank frame detector
189     std::shared_ptr<RSJankDetector> jankDetector_;
190 
191     std::shared_ptr<RSContext> context_;
192 
193 #ifdef NEW_RENDER_CONTEXT
194     std::shared_ptr<RenderContextBase> renderContext_;
195     std::shared_ptr<Rosen::DrawingContext> drawingContext_;
196 #else
197     RenderContext* renderContext_ = nullptr;
198 #endif
199     std::shared_ptr<HighContrastObserver> highContrastObserver_;
200     std::atomic_bool isHighContrastEnabled_ = false;
201     std::atomic_bool isHighContrastChanged_ = false;
202 
203     std::string cacheDir_;
204     bool isRTRenderForced_ = false;
205 #ifdef ROSEN_PREVIEW
206     std::atomic_bool isRunning_ = false;
207 #endif
208 };
209 } // namespace Rosen
210 } // namespace OHOS
211 
212 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_THREAD_H
213