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 #ifndef RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
16 #define RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
17 
18 #include <parcel.h>
19 #include <refbase.h>
20 #include <string>
21 
22 #ifdef ROSEN_OHOS
23 #include "iconsumer_surface.h"
24 #include "surface.h"
25 #include "surface_delegate.h"
26 #include "surface_type.h"
27 #endif
28 
29 #ifdef NEW_RENDER_CONTEXT
30 #include "rs_render_surface.h"
31 #else
32 #include "platform/drawing/rs_surface.h"
33 #include "platform/common/rs_surface_ext.h"
34 #endif
35 #include "transaction/rs_transaction_proxy.h"
36 #include "ui/rs_node.h"
37 
38 
39 namespace OHOS {
40 namespace Rosen {
41 struct RSSurfaceNodeConfig {
42     std::string SurfaceNodeName = "SurfaceNode";
43     void* additionalData = nullptr;
44     bool isTextureExportNode = false;
45     SurfaceId surfaceId = 0;
46     bool isSync = true;
47     enum SurfaceWindowType surfaceWindowType = SurfaceWindowType::DEFAULT_WINDOW;
48 };
49 
50 class RSC_EXPORT RSSurfaceNode : public RSNode {
51 public:
52     static constexpr float POINTER_WINDOW_POSITION_Z = 9999;
53 
54     using WeakPtr = std::weak_ptr<RSSurfaceNode>;
55     using SharedPtr = std::shared_ptr<RSSurfaceNode>;
56     static inline constexpr RSUINodeType Type = RSUINodeType::SURFACE_NODE;
GetType()57     RSUINodeType GetType() const override
58     {
59         return Type;
60     }
61 
62     ~RSSurfaceNode() override;
63 
64     static SharedPtr Create(const RSSurfaceNodeConfig& surfaceNodeConfig, bool isWindow = true);
65 
66     // This interface is only available for WMS
67     static SharedPtr Create(const RSSurfaceNodeConfig& surfaceNodeConfig, RSSurfaceNodeType type, bool isWindow = true);
68 
69     // This API is only for abilityView create RSRenderSurfaceNode in RenderThread.
70     // Do not call this API unless you are sure what you do.
71     // After calling it, this surfaceNode is disallowed to add/remove child.
72     void CreateNodeInRenderThread();
73 
74     void AddChild(std::shared_ptr<RSBaseNode> child, int index) override;
75     void RemoveChild(std::shared_ptr<RSBaseNode> child) override;
76     void ClearChildren() override;
77 
78     void SetSecurityLayer(bool isSecurityLayer);
79     bool GetSecurityLayer() const;
80     void SetLeashPersistentId(LeashPersistentId leashPersistentId);
81     LeashPersistentId GetLeashPersistentId() const;
82     void SetSkipLayer(bool isSkipLayer);
83     bool GetSkipLayer() const;
84     void SetFingerprint(bool hasFingerprint);
85     bool GetFingerprint() const;
86     void SetAbilityBGAlpha(uint8_t alpha);
87     void SetIsNotifyUIBufferAvailable(bool available);
88     void MarkUIHidden(bool isHidden);
89 
90     using BufferAvailableCallback = std::function<void()>;
91     bool SetBufferAvailableCallback(BufferAvailableCallback callback);
92     bool IsBufferAvailable() const;
93     using BoundsChangedCallback = std::function<void(const Rosen::Vector4f&)>;
94     void SetBoundsChangedCallback(BoundsChangedCallback callback) override;
95     void SetAnimationFinished();
96 
97     bool Marshalling(Parcel& parcel) const;
98     static SharedPtr Unmarshalling(Parcel& parcel);
99     // Create RSProxyNode by unmarshalling RSSurfaceNode, return existing node if it exists in RSNodeMap.
100     static RSNode::SharedPtr UnmarshallingAsProxyNode(Parcel& parcel);
101 
102     FollowType GetFollowType() const override;
103 
104     void AttachToDisplay(uint64_t screenId);
105     void DetachToDisplay(uint64_t screenId);
106     void SetHardwareEnabled(bool isEnabled, SelfDrawingNodeType selfDrawingType = SelfDrawingNodeType::DEFAULT,
107         bool dynamicHardwareEnable = true);
108     void SetForceHardwareAndFixRotation(bool flag);
109     void SetBootAnimation(bool isBootAnimation);
110     bool GetBootAnimation() const;
111     void SetTextureExport(bool isTextureExportNode) override;
112 
113 #ifndef ROSEN_CROSS_PLATFORM
114     sptr<OHOS::Surface> GetSurface() const;
115 #endif
116     void SetColorSpace(GraphicColorGamut colorSpace);
GetColorSpace()117     GraphicColorGamut GetColorSpace()
118     {
119         return colorSpace_;
120     }
121 
GetName()122     inline std::string GetName() const
123     {
124         return name_;
125     }
126 
GetBundleName()127     const std::string GetBundleName() const
128     {
129         return bundleName_;
130     }
131 
132     void ResetContextAlpha() const;
133 
134     void SetContainerWindow(bool hasContainerWindow, float density);
135     void SetWindowId(uint32_t windowId);
136 
137     void SetFreeze(bool isFreeze) override;
138     // codes for arkui-x
139 #ifdef USE_SURFACE_TEXTURE
140     void SetSurfaceTexture(const RSSurfaceExtConfig& config);
141     void MarkUiFrameAvailable(bool available);
142     void SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack& attachCallback);
143     void SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack& updateCallback);
144     void SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack& initTypeCallback);
145 #endif
146     void SetForeground(bool isForeground);
147     // Force enable UIFirst when set TRUE
148     void SetForceUIFirst(bool forceUIFirst);
149     void SetAncoFlags(uint32_t flags);
150     static void SetHDRPresent(bool hdrPresent, NodeId id);
151     void SetSkipDraw(bool skip);
152     bool GetSkipDraw() const;
153     RSInterfaceErrorCode SetHidePrivacyContent(bool needHidePrivacyContent);
154     void SetAbilityState(RSSurfaceNodeAbilityState abilityState);
155     RSSurfaceNodeAbilityState GetAbilityState() const;
156 protected:
157     bool NeedForcedSendToRemote() const override;
158     RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode);
159     RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode, NodeId id);
160     RSSurfaceNode(const RSSurfaceNode&) = delete;
161     RSSurfaceNode(const RSSurfaceNode&&) = delete;
162     RSSurfaceNode& operator=(const RSSurfaceNode&) = delete;
163     RSSurfaceNode& operator=(const RSSurfaceNode&&) = delete;
164 
165 private:
166 #ifdef USE_SURFACE_TEXTURE
167     void CreateSurfaceExt(const RSSurfaceExtConfig& config);
168 #endif
169     bool CreateNode(const RSSurfaceRenderNodeConfig& config);
170     bool CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, SurfaceId surfaceId = 0);
171     void OnBoundsSizeChanged() const override;
172     // this function is only used in texture export
173     void SetSurfaceIdToRenderNode();
174     void CreateRenderNodeForTextureExportSwitch() override;
175     void SetIsTextureExportNode(bool isTextureExportNode);
176     std::pair<std::string, std::string> SplitSurfaceNodeName(std::string surfaceNodeName);
177 #ifdef NEW_RENDER_CONTEXT
178     std::shared_ptr<RSRenderSurface> surface_;
179 #else
180     std::shared_ptr<RSSurface> surface_;
181 #endif
182     std::string name_;
183     std::string bundleName_;
184     mutable std::mutex mutex_;
185     BufferAvailableCallback callback_;
186     bool bufferAvailable_ = false;
187     BoundsChangedCallback boundsChangedCallback_;
188     GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
189     bool isSecurityLayer_ = false;
190     bool isSkipLayer_ = false;
191     bool hasFingerprint_ = false;
192     bool isChildOperationDisallowed_ { false };
193     bool isBootAnimation_ = false;
194     bool isSkipDraw_ = false;
195     RSSurfaceNodeAbilityState abilityState_ = RSSurfaceNodeAbilityState::FOREGROUND;
196     LeashPersistentId leashPersistentId_ = INVALID_LEASH_PERSISTENTID;
197 
198     uint32_t windowId_ = 0;
199 #ifndef ROSEN_CROSS_PLATFORM
200     sptr<SurfaceDelegate> surfaceDelegate_;
201     sptr<SurfaceDelegate::ISurfaceCallback> surfaceCallback_;
202 #endif
203 
204     std::mutex apiInitMutex_;
205 
206     friend class RSUIDirector;
207     friend class RSAnimation;
208     friend class RSPathAnimation;
209     friend class RSPropertyAnimation;
210     friend class RSSurfaceExtractor;
211     friend class RSSurfaceCallback;
212 };
213 } // namespace Rosen
214 } // namespace OHOS
215 
216 #endif // RENDER_SERVICE_CLIENT_CORE_UI_RS_SURFACE_NODE_H
217