• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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  /**
17  * @addtogroup RenderNodeDisplay
18  * @{
19  *
20  * @brief Display render nodes.
21  */
22 
23 /**
24  * @file rs_surface_node.h
25  *
26  * @brief Defines the properties and methods for RSSurfaceNode class.
27  */
28 
29 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
30 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
31 
32 #include <parcel.h>
33 #include <refbase.h>
34 #include <string>
35 
36 #ifdef ROSEN_OHOS
37 #include "iconsumer_surface.h"
38 #include "surface.h"
39 #include "surface_delegate.h"
40 #include "surface_type.h"
41 #endif
42 
43 #ifndef ROSEN_CROSS_PLATFORM
44 #include "surface_delegate.h"
45 #endif
46 
47 #include "platform/drawing/rs_surface.h"
48 #include "platform/common/rs_surface_ext.h"
49 #include "transaction/rs_transaction_proxy.h"
50 #include "ui/rs_node.h"
51 
52 
53 namespace OHOS {
54 namespace Rosen {
55 class RSCompositeLayerUtils;
56 /**
57  * @struct RSSurfaceNodeConfig
58  * @brief Configuration structure for creating or managing a surface node.
59  */
60 struct RSSurfaceNodeConfig {
61     /* Name of the surface node. Defaults to "SurfaceNode" */
62     std::string SurfaceNodeName = "SurfaceNode";
63     /* Pointer to any additional user data. Defaults to nullptr. */
64     void* additionalData = nullptr;
65     /* Indicates if the node is used for texture export. Defaults to false. */
66     bool isTextureExportNode = false;
67     /* Unique identifier for the surface. Defaults to 0. */
68     SurfaceId surfaceId = 0;
69     /* Specifies if the node operates in synchronous mode. Defaults to true. */
70     bool isSync = true;
71     /* Type of the surface window. Defaults to SurfaceWindowType::DEFAULT_WINDOW */
72     enum SurfaceWindowType surfaceWindowType = SurfaceWindowType::DEFAULT_WINDOW;
73     /* Shared pointer to the UI context associated with this node. Defaults to nullptr. */
74     std::shared_ptr<RSUIContext> rsUIContext = nullptr;
75 };
76 
77 /**
78  * @class RSSurfaceNode
79  *
80  * @brief Represents a surface node in the rendering service.
81  */
82 class RSC_EXPORT RSSurfaceNode : public RSNode {
83 public:
84     static constexpr float POINTER_WINDOW_POSITION_Z = 9999;
85 
86     using WeakPtr = std::weak_ptr<RSSurfaceNode>;
87     using SharedPtr = std::shared_ptr<RSSurfaceNode>;
88     static inline constexpr RSUINodeType Type = RSUINodeType::SURFACE_NODE;
89     /**
90      * @brief Get the type of the RSNode.
91      *
92      * @return The type of the RSNode.
93      */
GetType()94     RSUINodeType GetType() const override
95     {
96         return Type;
97     }
98 
99     /**
100      * @brief Destructor for RSSurfaceNode.
101      */
102     ~RSSurfaceNode() override;
103 
104     /**
105      * @brief Creates a new instance of RSSurfaceNode with the specified configuration.
106      *
107      * @param surfaceNodeConfig The configuration settings for the surface node.
108      * @param isWindow Indicates whether the surface node is a window. Defaults to true.
109      * @param rsUIContext An optional shared pointer to an RSUIContext object. Defaults to nullptr.
110      * @return SharedPtr A shared pointer to the newly created RSSurfaceNode instance.
111      */
112     static SharedPtr Create(const RSSurfaceNodeConfig& surfaceNodeConfig, bool isWindow = true,
113         std::shared_ptr<RSUIContext> rsUIContext = nullptr);
114 
115     /**
116      * @brief Creates a new RSSurfaceNode instance with the specified configuration and parameters.
117      *
118      * This interface is only available for WMS
119      *
120      * @param surfaceNodeConfig The configuration settings for the surface node.
121      * @param type The type of the surface node, specified as an RSSurfaceNodeType.
122      * @param isWindow Indicates whether the surface node represents a window. Defaults to true.
123      * @param unobscured Indicates whether the surface node is unobscured. Defaults to false.
124      * @param rsUIContext An optional shared pointer to an RSUIContext instance. Defaults to nullptr.
125      * @return A shared pointer to the newly created RSSurfaceNode instance.
126      */
127     static SharedPtr Create(const RSSurfaceNodeConfig& surfaceNodeConfig, RSSurfaceNodeType type, bool isWindow = true,
128         bool unobscured = false, std::shared_ptr<RSUIContext> rsUIContext = nullptr);
129 
130     // This API is only for abilityView create RSRenderSurfaceNode in RenderThread.
131     // Do not call this API unless you are sure what you do.
132     // After calling it, this surfaceNode is disallowed to add/remove child.
133     void CreateNodeInRenderThread();
134 
135     /**
136      * @brief Adds a child node to the current node at the specified index.
137      *
138      * @param child The shared pointer to the child node to be added.
139      * @param index The position at which the child node should be inserted.
140      *              If the index is -1 (default), the child is added to the end.
141      */
142     void AddChild(std::shared_ptr<RSBaseNode> child, int index) override;
143 
144     /**
145      * @brief Removes a child node from the current node.
146      *
147      * @param child A shared pointer to the child node to be removed.
148      */
149     void RemoveChild(std::shared_ptr<RSBaseNode> child) override;
150 
151     /**
152      * @brief Removes all child nodes associated with this node.
153      */
154     void ClearChildren() override;
155 
156     void SetSecurityLayer(bool isSecurityLayer);
157     bool GetSecurityLayer() const;
158     void SetLeashPersistentId(LeashPersistentId leashPersistentId);
159     LeashPersistentId GetLeashPersistentId() const;
160     void SetSkipLayer(bool isSkipLayer);
161     bool GetSkipLayer() const;
162     void SetSnapshotSkipLayer(bool isSnapshotSkipLayer);
163     bool GetSnapshotSkipLayer() const;
164     void SetFingerprint(bool hasFingerprint);
165     bool GetFingerprint() const;
166     /**
167      * @brief Sets the background alpha value for the ability.
168      *
169      * @param alpha The alpha value to set, ranging from 0 (fully transparent) to 255 (fully opaque).
170      */
171     void SetAbilityBGAlpha(uint8_t alpha);
172     void SetIsNotifyUIBufferAvailable(bool available);
173     void MarkUIHidden(bool isHidden);
174 
175     using BufferAvailableCallback = std::function<void()>;
176     bool SetBufferAvailableCallback(BufferAvailableCallback callback);
177     bool IsBufferAvailable() const;
178     void SetBoundsChangedCallback(BoundsChangedCallback callback) override;
179     void SetAnimationFinished();
180 
181     /**
182      * @brief Serializes the RSSurfaceNode into a parcel.
183      *
184      * @param parcel The Parcel object where the RSSurfaceNode data will be written.
185      * @return true if the serialization is successful; false otherwise.
186      */
187     bool Marshalling(Parcel& parcel) const;
188 
189     /**
190      * @brief Deserializes the RSDisplayNode from a Parcel.
191      *
192      * @param parcel The Parcel object containing the serialized RSSurfaceNode data.
193      * @return A shared pointer to the deserialized RSSurfaceNode instance.
194      */
195     static SharedPtr Unmarshalling(Parcel& parcel);
196 
197     /**
198      * @brief Create RSProxyNode by unmarshalling RSSurfaceNode.
199      *
200      * return existing node if it exists in RSNodeMap
201      *
202      * @param parcel The Parcel object containing the marshalled data.
203      * @return A shared pointer to the unmarshalled RSNode object.
204      */
205     static RSNode::SharedPtr UnmarshallingAsProxyNode(Parcel& parcel);
206 
207     FollowType GetFollowType() const override;
208 
209     void AttachToDisplay(uint64_t screenId);
210     void DetachToDisplay(uint64_t screenId);
211     void SetHardwareEnabled(bool isEnabled, SelfDrawingNodeType selfDrawingType = SelfDrawingNodeType::DEFAULT,
212         bool dynamicHardwareEnable = true);
213 
214     /**
215      * @brief Enable Camera Rotation Unchanged
216      *
217      * @param flag If flag is set to true, the camera fix rotation is enabled.
218      * @return void
219      */
220     void SetForceHardwareAndFixRotation(bool flag);
221     void SetBootAnimation(bool isBootAnimation);
222     bool GetBootAnimation() const;
223     void SetTextureExport(bool isTextureExportNode) override;
224     void SetGlobalPositionEnabled(bool isEnabled);
225     bool GetGlobalPositionEnabled() const;
226     /**
227      * @brief Set whether to enable the new version of frame gravity.
228      *
229      * @param bool enable the new version of frame gravity or not.
230      */
231     void SetFrameGravityNewVersionEnabled(bool isEnabled);
232     /**
233      * @brief Get the enable status of new version of frame gravity.
234      *
235      * @return true if the new version of frame gravity enabled; false otherwise.
236      */
237     bool GetFrameGravityNewVersionEnabled() const;
238 
239 #ifndef ROSEN_CROSS_PLATFORM
240     sptr<OHOS::Surface> GetSurface() const;
241 #endif
242     void SetColorSpace(GraphicColorGamut colorSpace);
GetColorSpace()243     GraphicColorGamut GetColorSpace()
244     {
245         return colorSpace_;
246     }
247 
248     /**
249      * @brief Get the name of the node.
250      *
251      * @return A string representing the name of the node.
252      */
GetName()253     inline std::string GetName() const
254     {
255         return name_;
256     }
257 
258     void ResetContextAlpha() const;
259 
260     void SetContainerWindow(bool hasContainerWindow, RRect rrect);
261     /**
262      * @brief Sets the window ID for the surface node.
263      *
264      * @param windowId The unique identifier of the window to be set.
265      */
266     void SetWindowId(uint32_t windowId);
267 
268     /**
269      * @brief Controls surface content freezing for window snapshot or resource release
270      *
271      * Only works in Unified Render mode (UniRender)
272      *
273      * @param isFreeze Freeze control flag:
274      *                - true: Freeze current frame into static texture
275      *                - false: Resume normal buffer updates
276      */
277     void SetFreeze(bool isFreeze) override;
278 
279     // codes for arkui-x
280 #ifdef USE_SURFACE_TEXTURE
281     void SetSurfaceTexture(const RSSurfaceExtConfig& config);
282     void MarkUiFrameAvailable(bool available);
283     void SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack& attachCallback);
284     void SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack& updateCallback);
285     void SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack& initTypeCallback);
286 #endif
287     void SetForeground(bool isForeground);
288     // [Attention] The function only used for unlocking screen for PC currently
289     void SetClonedNodeInfo(NodeId nodeId, bool needOffscreen = true);
290     // Force enable UIFirst when set TRUE
291     void SetForceUIFirst(bool forceUIFirst);
292     void SetAncoFlags(uint32_t flags);
293     void SetHDRPresent(bool hdrPresent, NodeId id);
294     void SetSkipDraw(bool skip);
295     bool GetSkipDraw() const;
296     void SetAbilityState(RSSurfaceNodeAbilityState abilityState);
297     RSSurfaceNodeAbilityState GetAbilityState() const;
298 
299     void SetWatermarkEnabled(const std::string& name, bool isEnabled);
300 
301     RSInterfaceErrorCode SetHidePrivacyContent(bool needHidePrivacyContent);
302     // Specifying hardware enable is only a 'hint' to RS that
303     // the self-drawing node use hardware composer in some condition,
304     // such as transparent background.
305     void SetHardwareEnableHint(bool enable);
306 
307     /**
308      * @brief Determines whether the surfaceNode is a selfDrawing node.
309      *
310      * @return True if the surfaceNode is a selfDrawing node, otherwise false.
311      */
312     bool IsSelfDrawingNode() const;
313 
314     /**
315      * @brief Sets the surfaceNode and its subtree to generate a topLayer.
316      *
317      * @param zOrder: zOrder of topLayer
318      *
319      * @return True if the setting is successful, otherwise false.
320      */
321     bool SetCompositeLayer(TopLayerZOrder zOrder);
322     std::shared_ptr<RSCompositeLayerUtils> GetCompositeLayerUtils() const;
323 
324     /**
325      * @brief Sets the API compatible version for the surface node.
326      * @param version The API version to set for compatibility.
327      */
328     void SetApiCompatibleVersion(uint32_t version);
329 
330     void SetSourceVirtualDisplayId(ScreenId screenId);
331     void AttachToWindowContainer(ScreenId screenId);
332     void DetachFromWindowContainer(ScreenId screenId);
333     void SetRegionToBeMagnified(const Vector4<int>& regionToBeMagnified);
334 protected:
335     bool NeedForcedSendToRemote() const override;
336     RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode,
337         std::shared_ptr<RSUIContext> rsUIContext = nullptr);
338     RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode, NodeId id,
339         std::shared_ptr<RSUIContext> rsUIContext = nullptr);
340     RSSurfaceNode(const RSSurfaceNode&) = delete;
341     RSSurfaceNode(const RSSurfaceNode&&) = delete;
342     RSSurfaceNode& operator=(const RSSurfaceNode&) = delete;
343     RSSurfaceNode& operator=(const RSSurfaceNode&&) = delete;
344 
345 private:
346 #ifdef USE_SURFACE_TEXTURE
347     void CreateSurfaceExt(const RSSurfaceExtConfig& config);
348 #endif
349     bool CreateNode(const RSSurfaceRenderNodeConfig& config);
350     bool CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, SurfaceId surfaceId = 0,
351         bool unobscured = false);
352     /**
353      * @brief Called when the bounds size of the surface node changes.
354      */
355     void OnBoundsSizeChanged() const override;
356     // this function is only used in texture export
357     void SetSurfaceIdToRenderNode();
358     void CreateRenderNodeForTextureExportSwitch() override;
359     void SetIsTextureExportNode(bool isTextureExportNode);
360     std::pair<std::string, std::string> SplitSurfaceNodeName(std::string surfaceNodeName);
361     void RegisterNodeMap() override;
362     std::shared_ptr<RSSurface> surface_;
363     std::string name_;
364     std::string bundleName_;
365     mutable std::mutex mutex_;
366     BufferAvailableCallback callback_;
367     bool bufferAvailable_ = false;
368     BoundsChangedCallback boundsChangedCallback_;
369     GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
370     bool isSecurityLayer_ = false;
371     bool isSkipLayer_ = false;
372     bool isSnapshotSkipLayer_ = false;
373     bool hasFingerprint_ = false;
374     bool isChildOperationDisallowed_ { false };
375     bool isBootAnimation_ = false;
376     bool isSkipDraw_ = false;
377     RSSurfaceNodeAbilityState abilityState_ = RSSurfaceNodeAbilityState::FOREGROUND;
378     bool isGlobalPositionEnabled_ = false;
379     bool isFrameGravityNewVersionEnabled_ = false;
380     LeashPersistentId leashPersistentId_ = INVALID_LEASH_PERSISTENTID;
381     RSSurfaceNodeType surfaceNodeType_ = RSSurfaceNodeType::DEFAULT;
382     std::shared_ptr<RSCompositeLayerUtils> compositeLayerUtils_;
383 
384     uint32_t windowId_ = 0;
385 #ifndef ROSEN_CROSS_PLATFORM
386     sptr<SurfaceDelegate> surfaceDelegate_;
387     sptr<SurfaceDelegate::ISurfaceCallback> surfaceCallback_;
388 #endif
389 
390     friend class RSUIDirector;
391     friend class RSAnimation;
392     friend class RSPathAnimation;
393     friend class RSPropertyAnimation;
394     friend class RSSurfaceExtractor;
395     friend class RSSurfaceCallback;
396 };
397 } // namespace Rosen
398 } // namespace OHOS
399 
400 /** @} */
401 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
402