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