• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #ifndef RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_H
17 #define RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_H
18 
19 #ifndef ROSEN_CROSS_PLATFORM
20 #include <ibuffer_consumer_listener.h>
21 #include <iconsumer_surface.h>
22 #include <surface.h>
23 #endif
24 #include "platform/drawing/rs_surface.h"
25 
26 #include "common/rs_common_def.h"
27 #include "drawable/rs_render_node_drawable.h"
28 #include "params/rs_surface_render_params.h"
29 #include "params/rs_display_render_params.h"
30 #include "pipeline/render_thread/rs_base_render_engine.h"
31 #include "pipeline/rs_surface_render_node.h"
32 #include "pipeline/rs_draw_window_cache.h"
33 
34 namespace OHOS::Rosen {
35 class RSRenderThreadParams;
36 class RSSurfaceRenderNode;
37 class RSSurfaceRenderParams;
38 namespace DrawableV2 {
39 class RSDisplayRenderNodeDrawable;
40 class RSRcdSurfaceRenderNodeDrawable;
41 struct UIFirstParams {
42     uint32_t submittedSubThreadIndex_ = INT_MAX;
43     std::atomic<CacheProcessStatus> cacheProcessStatus_ = CacheProcessStatus::UNKNOWN;
44     std::atomic<bool> isNeedSubmitSubThread_ = true;
45 };
46 
47 // remove this when rcd node is replaced by common hardware composer node in OH 6.0 rcd refactoring
48 class RSSurfaceRenderNodeDrawable : public RSRenderNodeDrawable {
49 public:
50     ~RSSurfaceRenderNodeDrawable() override;
51 
52     static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node);
53     void OnDraw(Drawing::Canvas& canvas) override;
54     void OnCapture(Drawing::Canvas& canvas) override;
55     bool CheckIfSurfaceSkipInMirror(const RSSurfaceRenderParams& surfaceParams);
56     void SetVirtualScreenWhiteListRootId(const std::unordered_set<NodeId>& whiteList, NodeId id);
57     void ResetVirtualScreenWhiteListRootId(NodeId id);
58 
59     void SubDraw(Drawing::Canvas& canvas);
60 
61     void UpdateCacheSurface();
62     void SetCacheSurfaceProcessedStatus(CacheProcessStatus cacheProcessStatus);
63     CacheProcessStatus GetCacheSurfaceProcessedStatus() const;
64 
GetName()65     const std::string& GetName() const
66     {
67         return name_;
68     }
69 
70     // UIFirst
SetSubmittedSubThreadIndex(uint32_t index)71     void SetSubmittedSubThreadIndex(uint32_t index)
72     {
73         uiFirstParams.submittedSubThreadIndex_ = index;
74     }
75 
GetSubmittedSubThreadIndex()76     uint32_t GetSubmittedSubThreadIndex() const
77     {
78         return uiFirstParams.submittedSubThreadIndex_;
79     }
80 
GetDebugInfo()81     std::string GetDebugInfo()
82     {
83         std::string res = "pid_";
84         res.append("_name_");
85         res.append(name_);
86         return res;
87     }
88 
89     bool BufferFormatNeedUpdate(std::shared_ptr<Drawing::Surface> cacheSurface, bool isNeedFP16);
90     void UpdateCompletedCacheSurface();
91     void ClearCacheSurfaceInThread();
92     void ClearCacheSurface(bool isClearCompletedCacheSurface = true);
93 
94     std::shared_ptr<Drawing::Surface> GetCacheSurface(uint32_t threadIndex, bool needCheckThread,
95         bool releaseAfterGet = false);
96     bool NeedInitCacheSurface();
97     std::shared_ptr<Drawing::Image> GetCompletedImage(RSPaintFilterCanvas& canvas, uint32_t threadIndex,
98         bool isUIFirst);
99     using ClearCacheSurfaceFunc =
100         std::function<void(std::shared_ptr<Drawing::Surface>&&,
101         std::shared_ptr<Drawing::Surface>&&, uint32_t, uint32_t)>;
102     void InitCacheSurface(Drawing::GPUContext* grContext, ClearCacheSurfaceFunc func = nullptr,
103         uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isNeedFP16 = false);
104 
ResetUifirst(bool isNotClearCompleteCacheSurface)105     void ResetUifirst(bool isNotClearCompleteCacheSurface)
106     {
107         if (isNotClearCompleteCacheSurface) {
108             ClearCacheSurfaceOnly();
109         } else {
110             ClearCacheSurfaceInThread();
111         }
112         drawWindowCache_.ClearCache();
113     }
114 
115     bool IsCurFrameStatic();
116 
117     Drawing::Matrix GetGravityMatrix(float imgWidth, float imgHeight);
118 
119     bool HasCachedTexture() const;
120 
121     void SetTextureValidFlag(bool isValid);
122 #ifdef RS_ENABLE_GPU
SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)123     void SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)
124     {
125         isCacheSurfaceNeedUpdate_ = isCacheSurfaceNeedUpdate;
126     }
127 
GetCacheSurfaceNeedUpdated()128     bool GetCacheSurfaceNeedUpdated() const
129     {
130         return isCacheSurfaceNeedUpdate_;
131     }
132 #endif
133 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
134     void UpdateBackendTexture();
135 #endif
136 
137     bool DrawCacheSurface(RSPaintFilterCanvas& canvas, const Vector2f& boundSize,
138         uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isUIFirst = false);
139     void DrawableCache(std::shared_ptr<Drawing::GPUContext> grContext_);
140 
SetLastFrameUsedThreadIndex(pid_t tid)141     void SetLastFrameUsedThreadIndex(pid_t tid)
142     {
143         lastFrameUsedThreadIndex_ = tid;
144     }
145 
GetLastFrameUsedThreadIndex()146     pid_t GetLastFrameUsedThreadIndex()
147     {
148         return lastFrameUsedThreadIndex_;
149     }
150 
SetRenderCachePriority(NodePriorityType type)151     void SetRenderCachePriority(NodePriorityType type)
152     {
153         priority_ = type;
154     }
155 
GetRenderCachePriority()156     NodePriorityType GetRenderCachePriority() const
157     {
158         return priority_;
159     }
160 
SetHDRPresent(bool hasHdrPresent)161     void SetHDRPresent(bool hasHdrPresent)
162     {
163         hasHdrPresent_ = hasHdrPresent;
164     }
165 
GetHDRPresent()166     bool GetHDRPresent() const
167     {
168         return hasHdrPresent_;
169     }
170 
GetScreenId()171     ScreenId GetScreenId() const
172     {
173         return screenId_;
174     }
175 
SetScreenId(ScreenId screenId)176     void SetScreenId(ScreenId screenId)
177     {
178         screenId_ = screenId;
179     }
180 
GetTargetColorGamut()181     GraphicColorGamut GetTargetColorGamut() const
182     {
183         return targetColorGamut_;
184     }
185 
SetTargetColorGamut(GraphicColorGamut colorGamut)186     void SetTargetColorGamut(GraphicColorGamut colorGamut)
187     {
188         targetColorGamut_ = colorGamut;
189     }
190 
191     void SetSubThreadSkip(bool isSubThreadSkip);
IsSubThreadSkip()192     bool IsSubThreadSkip() const
193     {
194         return isSubThreadSkip_;
195     }
196     void SetTaskFrameCount(uint64_t frameCount);
197 
198     uint64_t GetTaskFrameCount() const;
199 
200     const Occlusion::Region& GetVisibleDirtyRegion() const;
201     void SetVisibleDirtyRegion(const Occlusion::Region& region);
202     void SetAlignedVisibleDirtyRegion(const Occlusion::Region& region);
203     void SetGlobalDirtyRegion(Occlusion::Region region);
204     const Occlusion::Region& GetGlobalDirtyRegion() const;
205     void SetDirtyRegionAlignedEnable(bool enable);
206     void SetDirtyRegionBelowCurrentLayer(Occlusion::Region& region);
207     std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const override;
208     GraphicColorGamut GetAncestorDisplayColorGamut(const RSSurfaceRenderParams& surfaceParams);
209     void DealWithSelfDrawingNodeBuffer(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
210     void ClearCacheSurfaceOnly();
211 
SetUIExtensionNeedToDraw(bool needToDraw)212     void SetUIExtensionNeedToDraw(bool needToDraw) override
213     {
214         uiExtensionNeedToDraw_ = needToDraw;
215     }
216 
UIExtensionNeedToDraw()217     bool UIExtensionNeedToDraw() const override
218     {
219         return uiExtensionNeedToDraw_;
220     }
221 
222     bool PrepareOffscreenRender();
223     void FinishOffscreenRender(const Drawing::SamplingOptions& sampling);
224     bool IsHardwareEnabled();
225 
226     uint32_t GetUifirstPostOrder() const;
227     void SetUifirstPostOrder(uint32_t order);
228 
229 #ifndef ROSEN_CROSS_PLATFORM
GetConsumerOnDraw()230     sptr<IConsumerSurface> GetConsumerOnDraw() const
231     {
232         return consumerOnDraw_;
233     }
234     void RegisterDeleteBufferListenerOnSync(sptr<IConsumerSurface> consumer) override;
235 #endif
236 
237     bool IsHardwareEnabledTopSurface() const;
238 
CheckCacheSurface()239     inline bool CheckCacheSurface()
240     {
241         return cacheSurface_ ? true : false;
242     }
243     int GetTotalProcessedSurfaceCount() const;
244     void UpdateCacheSurfaceInfo();
245 private:
246     explicit RSSurfaceRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node);
247     bool DealWithUIFirstCache(
248         RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams, RSRenderThreadParams& uniParams);
249     void OnGeneralProcess(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams,
250         RSRenderThreadParams& uniParams, bool isSelfDrawingSurface);
251     void CaptureSurface(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
252 
253     Drawing::Region CalculateVisibleDirtyRegion(RSRenderThreadParams& uniParam, RSSurfaceRenderParams& surfaceParams,
254         RSSurfaceRenderNodeDrawable& surfaceDrawable, bool isOffscreen) const;
255     void CrossDisplaySurfaceDirtyRegionConversion(
256         const RSRenderThreadParams& uniParam, const RSSurfaceRenderParams& surfaceParam, RectI& surfaceDirtyRect) const;
257     bool HasCornerRadius(const RSSurfaceRenderParams& surfaceParams) const;
258     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::SURFACE_NODE, OnGenerate>;
259     static Registrar instance_;
260 
261     bool DrawUIFirstCache(RSPaintFilterCanvas& rscanvas, bool canSkipWait);
262     bool DrawUIFirstCacheWithStarting(RSPaintFilterCanvas& rscanvas, NodeId id);
263     bool CheckDrawAndCacheWindowContent(RSSurfaceRenderParams& surfaceParams,
264         RSRenderThreadParams& uniParams) const;
265     void TotalProcessedSurfaceCountInc(RSPaintFilterCanvas& canvas);
266     void ClearTotalProcessedSurfaceCount();
267     void PreprocessUnobscuredUEC(RSPaintFilterCanvas& canvas);
268 
269     void DrawUIFirstDfx(RSPaintFilterCanvas& canvas, MultiThreadCacheType enableType,
270         RSSurfaceRenderParams& surfaceParams, bool drawCacheSuccess);
271     void EnableGpuOverDrawDrawBufferOptimization(Drawing::Canvas& canvas, RSSurfaceRenderParams* surfaceParams);
272 
273     // dirty manager
274     void UpdateDisplayDirtyManager(int32_t bufferage, bool useAlignedDirtyRegion = false);
275 
276     // DMA Buffer
277     bool DrawUIFirstCacheWithDma(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
278     void DrawDmaBufferWithGPU(RSPaintFilterCanvas& canvas);
279     void DrawSelfDrawingNodeBuffer(RSPaintFilterCanvas& canvas,
280         const RSSurfaceRenderParams& surfaceParams, BufferDrawParam& params);
281 
282     // Draw cloneNode
283     bool DrawCloneNode(RSPaintFilterCanvas& canvas, RSRenderThreadParams& uniParam,
284         RSSurfaceRenderParams& surfaceParams, bool isCapture = false);
285 
286     // Watermark
287     void DrawWatermark(RSPaintFilterCanvas& canvas, const RSSurfaceRenderParams& surfaceParams);
288 
289     bool RecordTimestamp(NodeId id, uint32_t seqNum);
290 
291     bool DrawCacheImageForMultiScreenView(RSPaintFilterCanvas& canvas, const RSSurfaceRenderParams& surfaceParams);
292 
293     void ClipHoleForSelfDrawingNode(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
294     void DrawBufferForRotationFixed(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
295 
296     std::string name_;
297     RSSurfaceNodeType surfaceNodeType_ = RSSurfaceNodeType::DEFAULT;
298 #ifndef ROSEN_CROSS_PLATFORM
299     sptr<IBufferConsumerListener> consumerListener_ = nullptr;
300 #endif
301     std::shared_ptr<RSSurface> surface_ = nullptr;
302     bool surfaceCreated_ = false;
303     bool uiExtensionNeedToDraw_ = false;
304 
305     // UIFIRST
306     UIFirstParams uiFirstParams;
307     ClearCacheSurfaceFunc clearCacheSurfaceFunc_ = nullptr;
308     uint32_t cacheSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX;
309     uint32_t completedSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX;
310     mutable std::recursive_mutex completeResourceMutex_; // only lock complete Resource
311     struct CacheSurfaceInfo {
312         int processedSurfaceCount = -1;
313         int processedNodeCount = -1;
314         float alpha = -1.f;
315     };
316     CacheSurfaceInfo cacheSurfaceInfo_;
317     CacheSurfaceInfo cacheCompletedSurfaceInfo_;
318     std::shared_ptr<Drawing::Surface> cacheSurface_ = nullptr;
319     std::shared_ptr<Drawing::Surface> cacheCompletedSurface_ = nullptr;
320 #ifdef RS_ENABLE_GPU
321     Drawing::BackendTexture cacheBackendTexture_;
322     Drawing::BackendTexture cacheCompletedBackendTexture_;
323 #ifdef RS_ENABLE_VK
324     NativeBufferUtils::VulkanCleanupHelper* cacheCleanupHelper_ = nullptr;
325     NativeBufferUtils::VulkanCleanupHelper* cacheCompletedCleanupHelper_ = nullptr;
326 #endif
327     std::atomic<bool> isCacheSurfaceNeedUpdate_ = false;
328 #endif
329     std::atomic<bool> isTextureValid_ = false;
330     pid_t lastFrameUsedThreadIndex_ = UNI_MAIN_THREAD_INDEX;
331     NodePriorityType priority_ = NodePriorityType::MAIN_PRIORITY;
332     bool hasHdrPresent_ = false;
333     // hdr
334     GraphicColorGamut targetColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
335     ScreenId screenId_ = INVALID_SCREEN_ID;
336     uint64_t frameCount_ = 0;
337     bool isSubThreadSkip_ = false;
338 
339     RSPaintFilterCanvas* curCanvas_ = nullptr;
340     std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporary holds offscreen surface
341     int releaseCount_ = 0;
342     static constexpr int MAX_RELEASE_FRAME = 10;
343     RSPaintFilterCanvas* canvasBackup_ = nullptr; // backup current canvas before offscreen rende
344     std::shared_ptr<RSPaintFilterCanvas> offscreenCanvas_ = nullptr;
345     int maxRenderSize_ = 0;
346     std::unique_ptr<RSAutoCanvasRestore> arc_ = nullptr;
347 
348 #ifndef ROSEN_CROSS_PLATFORM
349     sptr<IConsumerSurface> consumerOnDraw_ = nullptr;
350 #endif
351 
352     // dirty manager
353     std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr;
354     Occlusion::Region visibleDirtyRegion_;
355     Occlusion::Region alignedVisibleDirtyRegion_;
356     bool isDirtyRegionAlignedEnable_ = false;
357     Occlusion::Region globalDirtyRegion_;
358 
359     // if a there a dirty layer under transparent clean layer, transparent layer should refreshed
360     Occlusion::Region dirtyRegionBelowCurrentLayer_;
361     bool dirtyRegionBelowCurrentLayerIsEmpty_ = false;
362 
363     RSDrawWindowCache drawWindowCache_;
364     friend class OHOS::Rosen::RSDrawWindowCache;
365     bool vmaCacheOff_ = false;
366     static inline std::atomic<int> totalProcessedSurfaceCount_ = 0;
367     uint32_t uifirstPostOrder_ = 0;
368 
369     static inline bool isInRotationFixed_ = false;
370     bool lastGlobalPositionEnabled_ = false;
371 };
372 } // namespace DrawableV2
373 } // namespace OHOS::Rosen
374 #endif // RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_H
375