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 #ifdef NEW_RENDER_CONTEXT
25 #include "rs_render_surface.h"
26 #else
27 #include "platform/drawing/rs_surface.h"
28 #endif
29 
30 #include "common/rs_common_def.h"
31 #include "drawable/rs_render_node_drawable.h"
32 #include "params/rs_surface_render_params.h"
33 #include "pipeline/rs_base_render_engine.h"
34 #include "params/rs_display_render_params.h"
35 #include "pipeline/rs_surface_render_node.h"
36 
37 namespace OHOS::Rosen {
38 class RSRenderThreadParams;
39 class RSSurfaceRenderNode;
40 class RSSurfaceRenderParams;
41 namespace DrawableV2 {
42 class RSDisplayRenderNodeDrawable;
43 struct UIFirstParams {
44     uint32_t submittedSubThreadIndex_ = INT_MAX;
45     std::atomic<CacheProcessStatus> cacheProcessStatus_ = CacheProcessStatus::UNKNOWN;
46     std::atomic<bool> isNeedSubmitSubThread_ = true;
47 };
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     void ClipRoundRect(Drawing::Canvas& canvas);
61 
62     void UpdateCacheSurface();
63     void SetCacheSurfaceProcessedStatus(CacheProcessStatus cacheProcessStatus);
64     CacheProcessStatus GetCacheSurfaceProcessedStatus() const;
65 
GetName()66     const std::string& GetName() const
67     {
68         return name_;
69     }
70 
71     // Dma Buffer
72     bool UseDmaBuffer();
73 
IsSurfaceCreated()74     bool IsSurfaceCreated() const
75     {
76         return surfaceCreated_;
77     }
78 
79     void ClearBufferQueue();
80 
81 #ifndef ROSEN_CROSS_PLATFORM
82     bool CreateSurface();
83 #endif
84     BufferRequestConfig GetFrameBufferRequestConfig();
85     std::unique_ptr<RSRenderFrame> RequestFrame(
86         RenderContext* renderContext, std::shared_ptr<Drawing::GPUContext> grContext);
87 
88     // UIFirst
SetSubmittedSubThreadIndex(uint32_t index)89     void SetSubmittedSubThreadIndex(uint32_t index)
90     {
91         uiFirstParams.submittedSubThreadIndex_ = index;
92     }
93 
GetSubmittedSubThreadIndex()94     uint32_t GetSubmittedSubThreadIndex() const
95     {
96         return uiFirstParams.submittedSubThreadIndex_;
97     }
98 
GetDebugInfo()99     std::string GetDebugInfo()
100     {
101         std::string res = "pid_";
102         res.append("_name_");
103         res.append(name_);
104         return res;
105     }
106 
107     void UpdateCompletedCacheSurface();
108     void ClearCacheSurfaceInThread();
109     void ClearCacheSurface(bool isClearCompletedCacheSurface = true);
110 
111     std::shared_ptr<Drawing::Surface> GetCacheSurface(uint32_t threadIndex, bool needCheckThread,
112         bool releaseAfterGet = false);
113     bool NeedInitCacheSurface();
114     std::shared_ptr<Drawing::Image> GetCompletedImage(RSPaintFilterCanvas& canvas, uint32_t threadIndex,
115         bool isUIFirst);
116     using ClearCacheSurfaceFunc =
117         std::function<void(std::shared_ptr<Drawing::Surface>&&,
118         std::shared_ptr<Drawing::Surface>&&, uint32_t, uint32_t)>;
119     void InitCacheSurface(Drawing::GPUContext* grContext, ClearCacheSurfaceFunc func = nullptr,
120         uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isHdrOn = false);
121 
ResetUifirst(bool isNotClearCompleteCacheSurface)122     void ResetUifirst(bool isNotClearCompleteCacheSurface)
123     {
124         if (isNotClearCompleteCacheSurface) {
125             ClearCacheSurfaceOnly();
126         } else {
127             ClearCacheSurfaceInThread();
128         }
129     }
130 
131     bool IsCurFrameStatic(DeviceType deviceType);
132 
133     Vector2f GetGravityTranslate(float imgWidth, float imgHeight);
134 
135     bool HasCachedTexture() const;
136 
137     void SetTextureValidFlag(bool isValid);
SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)138     void SetCacheSurfaceNeedUpdated(bool isCacheSurfaceNeedUpdate)
139     {
140         isCacheSurfaceNeedUpdate_ = isCacheSurfaceNeedUpdate;
141     }
142 
GetCacheSurfaceNeedUpdated()143     bool GetCacheSurfaceNeedUpdated() const
144     {
145         return isCacheSurfaceNeedUpdate_;
146     }
147 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
148     void UpdateBackendTexture();
149 #endif
150 
151     bool DrawCacheSurface(RSPaintFilterCanvas& canvas, const Vector2f& boundSize,
152         uint32_t threadIndex = UNI_MAIN_THREAD_INDEX, bool isUIFirst = false);
153     void DrawableCache(std::shared_ptr<Drawing::GPUContext> grContext_);
154 
SetLastFrameUsedThreadIndex(pid_t tid)155     void SetLastFrameUsedThreadIndex(pid_t tid)
156     {
157         lastFrameUsedThreadIndex_ = tid;
158     }
159 
GetLastFrameUsedThreadIndex()160     pid_t GetLastFrameUsedThreadIndex()
161     {
162         return lastFrameUsedThreadIndex_;
163     }
164 
SetRenderCachePriority(NodePriorityType type)165     void SetRenderCachePriority(NodePriorityType type)
166     {
167         priority_ = type;
168     }
169 
GetRenderCachePriority()170     NodePriorityType GetRenderCachePriority() const
171     {
172         return priority_;
173     }
174 
SetHDRPresent(bool hasHdrPresent)175     void SetHDRPresent(bool hasHdrPresent)
176     {
177         hasHdrPresent_ = hasHdrPresent;
178     }
179 
GetHDRPresent()180     bool GetHDRPresent() const
181     {
182         return hasHdrPresent_;
183     }
184 
GetScreenId()185     ScreenId GetScreenId() const
186     {
187         return screenId_;
188     }
189 
SetScreenId(ScreenId screenId)190     void SetScreenId(ScreenId screenId)
191     {
192         screenId_ = screenId;
193     }
194 
GetTargetColorGamut()195     GraphicColorGamut GetTargetColorGamut() const
196     {
197         return targetColorGamut_;
198     }
199 
SetTargetColorGamut(GraphicColorGamut colorGamut)200     void SetTargetColorGamut(GraphicColorGamut colorGamut)
201     {
202         targetColorGamut_ = colorGamut;
203     }
204 
205     void SetSubThreadSkip(bool isSubThreadSkip);
IsSubThreadSkip()206     bool IsSubThreadSkip() const
207     {
208         return isSubThreadSkip_;
209     }
210     void SetTaskFrameCount(uint64_t frameCount);
211 
212     uint64_t GetTaskFrameCount() const;
213 
214     const Occlusion::Region& GetVisibleDirtyRegion() const;
215     void SetVisibleDirtyRegion(const Occlusion::Region& region);
216     void SetAlignedVisibleDirtyRegion(const Occlusion::Region& region);
217     void SetGlobalDirtyRegion(Occlusion::Region region);
218     const Occlusion::Region& GetGlobalDirtyRegion() const;
219     void SetDirtyRegionAlignedEnable(bool enable);
220     void SetDirtyRegionBelowCurrentLayer(Occlusion::Region& region);
221     std::shared_ptr<RSDirtyRegionManager> GetSyncDirtyManager() const override;
222     GraphicColorGamut GetAncestorDisplayColorGamut(const RSSurfaceRenderParams& surfaceParams);
223     void DealWithSelfDrawingNodeBuffer(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
224     void ClearCacheSurfaceOnly();
225 
226     bool PrepareOffscreenRender();
227     void FinishOffscreenRender(const Drawing::SamplingOptions& sampling);
228     bool IsHardwareEnabled();
GetMutableRSSurfaceHandlerUiFirstOnDraw()229     std::shared_ptr<RSSurfaceHandler> GetMutableRSSurfaceHandlerUiFirstOnDraw()
230     {
231         return surfaceHandlerUiFirst_;
232     }
233 #ifndef ROSEN_CROSS_PLATFORM
GetConsumerOnDraw()234     sptr<IConsumerSurface> GetConsumerOnDraw() const
235     {
236         return consumerOnDraw_;
237     }
238     void RegisterDeleteBufferListenerOnSync(sptr<IConsumerSurface> consumer) override;
239 #endif
240 
241     bool IsHardwareEnabledTopSurface() const;
242 
CheckCacheSurface()243     inline bool CheckCacheSurface()
244     {
245         return cacheSurface_ ? true : false;
246     }
247 private:
248     explicit RSSurfaceRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node);
249     bool DealWithUIFirstCache(
250         RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams, RSRenderThreadParams& uniParams);
251     void OnGeneralProcess(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams, bool isSelfDrawingSurface);
252     void CaptureSurface(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
253 
254     void MergeDirtyRegionBelowCurSurface(RSRenderThreadParams& uniParam, Drawing::Region& region);
255     void MergeSubSurfaceNodesDirtyRegionForMainWindow(
256         RSSurfaceRenderParams& surfaceParams, Occlusion::Region& surfaceDirtyRegion) const;
257     Drawing::Region CalculateVisibleDirtyRegion(RSRenderThreadParams& uniParam, RSSurfaceRenderParams& surfaceParams,
258         RSSurfaceRenderNodeDrawable& surfaceDrawable, bool isOffscreen) const;
259     bool HasCornerRadius(const RSSurfaceRenderParams& surfaceParams) const;
260     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::SURFACE_NODE, OnGenerate>;
261     static Registrar instance_;
262 
263     bool DrawUIFirstCache(RSPaintFilterCanvas& rscanvas, bool canSkipWait);
264     bool DrawUIFirstCacheWithStarting(RSPaintFilterCanvas& rscanvas, NodeId id);
265     // To be deleted after captureWindow being deleted
266     bool CheckIfNeedResetRotate(RSPaintFilterCanvas& canvas);
267     NodeId FindInstanceChildOfDisplay(std::shared_ptr<RSRenderNode> node);
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     void ClipHoleForSelfDrawingNode(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
283     void DrawBufferForRotationFixed(RSPaintFilterCanvas& canvas, RSSurfaceRenderParams& surfaceParams);
284 
285     std::string name_;
286     RSSurfaceNodeType surfaceNodeType_ = RSSurfaceNodeType::DEFAULT;
287 #ifndef ROSEN_CROSS_PLATFORM
288     sptr<IBufferConsumerListener> consumerListener_ = nullptr;
289 #endif
290 #ifdef NEW_RENDER_CONTEXT
291     std::shared_ptr<RSRenderSurface> surface_ = nullptr;
292 #else
293     std::shared_ptr<RSSurface> surface_ = nullptr;
294 #endif
295     bool surfaceCreated_ = false;
296 
297     // UIFIRST
298     std::shared_ptr<RSSurfaceHandler> surfaceHandlerUiFirst_ = nullptr;
299     UIFirstParams uiFirstParams;
300     ClearCacheSurfaceFunc clearCacheSurfaceFunc_ = nullptr;
301     uint32_t cacheSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX;
302     uint32_t completedSurfaceThreadIndex_ = UNI_MAIN_THREAD_INDEX;
303     mutable std::recursive_mutex completeResourceMutex_; // only lock complete Resource
304     std::shared_ptr<Drawing::Surface> cacheSurface_ = nullptr;
305     std::shared_ptr<Drawing::Surface> cacheCompletedSurface_ = nullptr;
306 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
307     Drawing::BackendTexture cacheBackendTexture_;
308     Drawing::BackendTexture cacheCompletedBackendTexture_;
309 #ifdef RS_ENABLE_VK
310     NativeBufferUtils::VulkanCleanupHelper* cacheCleanupHelper_ = nullptr;
311     NativeBufferUtils::VulkanCleanupHelper* cacheCompletedCleanupHelper_ = nullptr;
312 #endif
313     std::atomic<bool> isCacheSurfaceNeedUpdate_ = false;
314 #endif
315     std::atomic<bool> isTextureValid_ = false;
316     pid_t lastFrameUsedThreadIndex_ = UNI_MAIN_THREAD_INDEX;
317     NodePriorityType priority_ = NodePriorityType::MAIN_PRIORITY;
318     bool hasHdrPresent_ = false;
319     // hdr
320     GraphicColorGamut targetColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
321     ScreenId screenId_ = INVALID_SCREEN_ID;
322     uint64_t frameCount_ = 0;
323     bool isSubThreadSkip_ = false;
324 
325     RSPaintFilterCanvas* curCanvas_ = nullptr;
326     std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr; // temporary holds offscreen surface
327     int releaseCount_ = 0;
328     static constexpr int MAX_RELEASE_FRAME = 10;
329     RSPaintFilterCanvas* canvasBackup_ = nullptr; // backup current canvas before offscreen rende
330     std::shared_ptr<RSPaintFilterCanvas> offscreenCanvas_ = nullptr;
331     int maxRenderSize_ = 0;
332     std::unique_ptr<RSAutoCanvasRestore> arc_ = nullptr;
333 
334 #ifndef ROSEN_CROSS_PLATFORM
335     sptr<IConsumerSurface> consumerOnDraw_ = nullptr;
336 #endif
337 
338     // dirty manager
339     std::shared_ptr<RSDirtyRegionManager> syncDirtyManager_ = nullptr;
340     Occlusion::Region visibleDirtyRegion_;
341     Occlusion::Region alignedVisibleDirtyRegion_;
342     bool isDirtyRegionAlignedEnable_ = false;
343     Occlusion::Region globalDirtyRegion_;
344     bool globalDirtyRegionIsEmpty_ = false;
345 
346     // if a there a dirty layer under transparent clean layer, transparent layer should refreshed
347     Occlusion::Region dirtyRegionBelowCurrentLayer_;
348     bool dirtyRegionBelowCurrentLayerIsEmpty_ = false;
349     bool vmaCacheOff_ = false;
350 };
351 } // namespace DrawableV2
352 } // namespace OHOS::Rosen
353 #endif // RENDER_SERVICE_DRAWABLE_RS_SURFACE_RENDER_NODE_DRAWABLE_H
354