1 /*
2  * Copyright (c) 2022 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 #include "window_extension_connection_ohos_ng.h"
16 
17 #include <functional>
18 #include <memory>
19 
20 #include "common/rs_color.h"
21 #include "element_name.h"
22 #include "render_service_client/core/ui/rs_surface_node.h"
23 
24 #include "base/memory/ace_type.h"
25 #include "base/memory/referenced.h"
26 #include "core/common/ace_engine.h"
27 #include "core/common/container.h"
28 #include "core/common/container_scope.h"
29 #include "core/components_ng/pattern/ability_component/ability_component_pattern.h"
30 #include "core/components_ng/render/adapter/rosen_render_context.h"
31 #include "frameworks/base/json/json_util.h"
32 
33 #ifdef OS_ACCOUNT_EXISTS
34 #include "os_account_manager.h"
35 #endif
36 
37 namespace OHOS::Ace {
38 class NGConnectionCallback : public Rosen::IWindowExtensionCallback {
39 public:
40     ACE_DISALLOW_COPY_AND_MOVE(NGConnectionCallback);
NGConnectionCallback(const WeakPtr<NG::FrameNode> & originNode,int32_t instanceId)41     NGConnectionCallback(const WeakPtr<NG::FrameNode>& originNode, int32_t instanceId)
42         : originNode_(originNode), instanceId_(instanceId)
43     {}
44 
45     ~NGConnectionCallback() override = default;
OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode> & rsSurfaceNode)46     void OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode) override
47     {
48         LOGI("OnWindowReady called");
49         auto task = [weak = wptr<NGConnectionCallback>(this), weakOriginNode = originNode_, rsNode = rsSurfaceNode,
50                         instanceId = instanceId_]() {
51             ContainerScope scope(instanceId);
52             CHECK_NULL_VOID(rsNode);
53             auto nodeStrong = weakOriginNode.Upgrade();
54             CHECK_NULL_VOID(nodeStrong);
55             auto context = nodeStrong->GetRenderContext();
56             CHECK_NULL_VOID(context);
57             auto extensionCallback = weak.promote();
58             CHECK_NULL_VOID(extensionCallback);
59             extensionCallback->rsOriginNode_ = std::static_pointer_cast<Rosen::RSSurfaceNode>(
60                 AceType::DynamicCast<NG::RosenRenderContext>(context)->GetRSNode());
61             UpdateFrameNodeTree(nodeStrong, rsNode);
62             auto pattern = AceType::DynamicCast<NG::AbilityComponentPattern>(nodeStrong->GetPattern());
63             if (pattern) {
64                 pattern->FireConnect();
65             }
66         };
67         PostTaskToUI(std::move(task), "ArkUIWindowExtensionConnect");
68     }
69 
OnExtensionDisconnected()70     void OnExtensionDisconnected() override
71     {
72         LOGI("window extension disconnect");
73         auto task = [weak = originNode_, rsNode = rsOriginNode_, instanceId = instanceId_]() {
74             ContainerScope scope(instanceId);
75             auto node = weak.Upgrade();
76             CHECK_NULL_VOID(node);
77             UpdateFrameNodeTree(node, rsNode);
78             auto pattern = AceType::DynamicCast<NG::AbilityComponentPattern>(node->GetPattern());
79             if (pattern) {
80                 pattern->FireDisConnect();
81             }
82         };
83         PostTaskToUI(std::move(task), "ArkUIWindowExtensionDisconnect");
84     }
85 
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)86     void OnKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event) override {}
OnPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)87     void OnPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event) override {}
OnBackPress()88     void OnBackPress() override {}
89 
90 private:
PostTaskToUI(const std::function<void ()> && task,const std::string & name) const91     void PostTaskToUI(const std::function<void()>&& task, const std::string& name) const
92     {
93         CHECK_NULL_VOID(task);
94         auto container = AceEngine::Get().GetContainer(instanceId_);
95         CHECK_NULL_VOID(container);
96         auto context = container->GetPipelineContext();
97         CHECK_NULL_VOID(context);
98         auto taskExecutor = context->GetTaskExecutor();
99         CHECK_NULL_VOID(taskExecutor);
100         taskExecutor->PostTask(task, TaskExecutor::TaskType::UI, name);
101     }
102 
UpdateFrameNodeTree(const RefPtr<NG::FrameNode> & node,const std::shared_ptr<Rosen::RSSurfaceNode> & rsSurfaceNode)103     static void UpdateFrameNodeTree(
104         const RefPtr<NG::FrameNode>& node, const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode)
105     {
106         CHECK_NULL_VOID(rsSurfaceNode);
107         rsSurfaceNode->CreateNodeInRenderThread();
108         auto context = node->GetRenderContext();
109         CHECK_NULL_VOID(context);
110         auto geometryNode = node->GetGeometryNode();
111         auto size = geometryNode->GetFrameSize();
112         geometryNode->SetFrameOffset(context->GetPaintRectWithTransform().GetOffset());
113         node->SetGeometryNode(geometryNode);
114         auto offset = node->GetGeometryNode()->GetFrameOffset();
115         LOGI("OnWindowReady surface size:%{public}f %{public}f %{public}f %{public}f", offset.GetX(), offset.GetY(),
116             size.Width(), size.Height());
117         rsSurfaceNode->SetBounds(offset.GetX(), offset.GetY(), size.Width(), size.Height());
118         AceType::DynamicCast<NG::RosenRenderContext>(context)->SetRSNode(rsSurfaceNode);
119         auto parent = node->GetParent();
120         CHECK_NULL_VOID(parent);
121         parent->MarkNeedSyncRenderTree();
122         parent->RebuildRenderContextTree();
123         context->RequestNextFrame();
124     }
125 
126     WeakPtr<NG::FrameNode> originNode_;
127     std::shared_ptr<Rosen::RSSurfaceNode> rsOriginNode_;
128     int32_t instanceId_ = -1;
129 };
130 
RectConverterNG(const Rect & rect,Rosen::Rect & rosenRect)131 void RectConverterNG(const Rect& rect, Rosen::Rect& rosenRect)
132 {
133     rosenRect.posX_ = static_cast<int>(rect.GetOffset().GetX());
134     rosenRect.posY_ = static_cast<int>(rect.GetOffset().GetY());
135     rosenRect.width_ = static_cast<uint32_t>(rect.GetSize().Width());
136     rosenRect.height_ = static_cast<uint32_t>(rect.GetSize().Height());
137 }
138 
WantConverterNG(const std::string & want,AppExecFwk::ElementName & element)139 void WantConverterNG(const std::string& want, AppExecFwk::ElementName& element)
140 {
141     auto json = JsonUtil::ParseJsonString(want);
142     element.SetAbilityName(json->GetValue("abilityName")->GetString());
143     element.SetBundleName(json->GetValue("bundleName")->GetString());
144 }
145 
ConnectExtension(const RefPtr<NG::FrameNode> & node,int32_t windowId)146 void WindowExtensionConnectionAdapterOhosNG::ConnectExtension(const RefPtr<NG::FrameNode>& node, int32_t windowId)
147 {
148 #if defined(ENABLE_ROSEN_BACKEND) && defined(OS_ACCOUNT_EXISTS)
149     LOGI("connect to windows extension begin");
150     if (!windowExtension_) {
151         windowExtension_ = std::make_unique<Rosen::WindowExtensionConnection>();
152     }
153     std::vector<int32_t> userIds;
154     ErrCode code = AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds);
155     if (code != ERR_OK) {
156         LOGE("fail to queryAccountId, so cannot connect extension");
157         return;
158     }
159     auto size = node->GetGeometryNode()->GetFrameSize();
160     auto offset = node->GetGeometryNode()->GetFrameOffset() + node->GetGeometryNode()->GetParentGlobalOffset();
161 
162     Rosen::Rect rosenRect = {
163         static_cast<int32_t>(offset.GetX()),
164         static_cast<int32_t>(offset.GetY()),
165         static_cast<uint32_t>(size.Width()),
166         static_cast<uint32_t>(size.Height()),
167     };
168     auto renderProperty = node->GetPaintProperty<NG::AbilityComponentRenderProperty>();
169     std::string want = renderProperty->GetWantValue();
170     AppExecFwk::ElementName element;
171     WantConverterNG(want, element);
172 
173     int32_t instanceId = -1;
174     auto container = Container::Current();
175     if (container) {
176         instanceId = container->GetInstanceId();
177     }
178     sptr<Rosen::IWindowExtensionCallback> callback = new NGConnectionCallback(node, instanceId);
179     if (extensionSession_ != nullptr) {
180         windowExtension_->ConnectExtension(element, rosenRect, userIds.front(), windowId, callback, extensionSession_);
181     } else {
182         windowExtension_->ConnectExtension(element, rosenRect, userIds.front(), windowId, callback);
183     }
184 #endif
185 }
186 
RemoveExtension()187 void WindowExtensionConnectionAdapterOhosNG::RemoveExtension()
188 {
189     if (windowExtension_) {
190         LOGI("remove extension");
191         windowExtension_->DisconnectExtension();
192     }
193 }
194 
Show()195 void WindowExtensionConnectionAdapterOhosNG::Show()
196 {
197     LOGI("show WindowExtensionConnectionAdapterOhos");
198     if (windowExtension_) {
199         windowExtension_->Show();
200     }
201 }
202 
Hide()203 void WindowExtensionConnectionAdapterOhosNG::Hide()
204 {
205     LOGI("hide WindowExtensionConnectionAdapterOhos");
206     if (windowExtension_) {
207         windowExtension_->Hide();
208     }
209 }
210 
UpdateRect(const Rect & rect)211 void WindowExtensionConnectionAdapterOhosNG::UpdateRect(const Rect& rect)
212 {
213     if (windowExtension_) {
214         Rosen::Rect rosenRect;
215         RectConverterNG(rect, rosenRect);
216         LOGI("UpdateRect rect: %{public}s", rect.ToString().c_str());
217         windowExtension_->SetBounds(rosenRect);
218     }
219 }
220 } // namespace OHOS::Ace
221