1 /*
2 * Copyright (c) 2025 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 #include "core/pipeline/base/rs_node_adapter.h"
17
18 #include "core/pipeline/pipeline_base.h"
19 #include "core/pipeline/pipeline_context.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21 #include "render_service_client/core/ui/rs_node.h"
22 #include "render_service_client/core/ui/rs_root_node.h"
23 #include "render_service_client/core/ui/rs_surface_node.h"
24 #include "render_service_client/core/ui/rs_ui_context.h"
25 #include "render_service_client/core/ui/rs_ui_director.h"
26
27 namespace OHOS::Ace {
28
CreateCanvasNode()29 std::shared_ptr<Rosen::RSNode> RsNodeAdapter::CreateCanvasNode()
30 {
31 auto rsUIContext = GetRSUIContext();
32 if (rsUIContext) {
33 auto canvasNode = Rosen::RSCanvasNode::Create(false, false, rsUIContext);
34 if (canvasNode) {
35 canvasNode->SetSkipCheckInMultiInstance(true);
36 }
37 return canvasNode;
38 }
39 return Rosen::RSCanvasNode::Create();
40 }
41
CreateRootNode()42 std::shared_ptr<Rosen::RSNode> RsNodeAdapter::CreateRootNode()
43 {
44 auto rsUIContext = GetRSUIContext();
45 if (rsUIContext) {
46 auto rootNode = Rosen::RSRootNode::Create(false, false, rsUIContext);
47 if (rootNode) {
48 rootNode->SetSkipCheckInMultiInstance(true);
49 }
50 return rootNode;
51 }
52 return Rosen::RSRootNode::Create();
53 }
54
CreateSurfaceNode(const Rosen::RSSurfaceNodeConfig & surfaceNodeConfig)55 std::shared_ptr<Rosen::RSNode> RsNodeAdapter::CreateSurfaceNode(
56 const Rosen::RSSurfaceNodeConfig& surfaceNodeConfig)
57 {
58 auto rsUIContext = GetRSUIContext();
59 if (rsUIContext) {
60 auto surfaceNode = Rosen::RSSurfaceNode::Create(surfaceNodeConfig, false, rsUIContext);
61 if (surfaceNode) {
62 surfaceNode->SetSkipCheckInMultiInstance(true);
63 }
64 return surfaceNode;
65 }
66 return Rosen::RSSurfaceNode::Create(surfaceNodeConfig, false);
67 }
68
GetRSUIContext()69 std::shared_ptr<Rosen::RSUIContext> RsNodeAdapter::GetRSUIContext()
70 {
71 std::shared_ptr<Rosen::RSUIDirector> rsUIDirector;
72 auto currentContainer = Container::CurrentSafelyWithCheck();
73 CHECK_NULL_RETURN(currentContainer, nullptr);
74 auto context = AceType::DynamicCast<PipelineContext>(currentContainer->GetPipelineContext());
75 if (context) {
76 rsUIDirector = context->GetRSUIDirector();
77 } else {
78 // Add JS Card use NG::PipelineContext
79 auto contextNG = AceType::DynamicCast<NG::PipelineContext>(currentContainer->GetPipelineContext());
80 if (contextNG) {
81 rsUIDirector = contextNG->GetRSUIDirector();
82 }
83 }
84 if (rsUIDirector) {
85 return rsUIDirector->GetRSUIContext();
86 }
87 return nullptr;
88 }
89 }