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 #include <iostream>
16
17 #include "dm/display_manager.h"
18 #include "securec.h"
19
20 #include "draw/canvas.h"
21 #include "image/bitmap.h"
22
23 #include "transaction/rs_interfaces.h"
24 #include "transaction/rs_transaction.h"
25 #include "ui/rs_canvas_node.h"
26 #include "ui/rs_root_node.h"
27 #include "ui/rs_surface_node.h"
28 #include "ui/rs_ui_director.h"
29
30 using namespace OHOS;
31 using namespace OHOS::Rosen;
32
33 constexpr uint32_t SLEEP_TIME = 3;
34 constexpr uint32_t LIMIT = 100;
35 constexpr uint32_t POINTER_WIDTH = 100;
36 constexpr uint32_t POINTER_HEIGHT = 200;
37 constexpr uint32_t POINTER_WINDOW_INIT_SIZE = 64;
38 constexpr uint32_t NODE_POSITION_X = 100;
39 constexpr uint32_t NODE_POSITION_Y = 300;
40 constexpr uint32_t UIEXTENSION_POSITION_X = 150;
41 constexpr uint32_t UIEXTENSION_POSITION_Y = 350;
42 std::shared_ptr<RSSurfaceNode> surfaceNode;
43 uint64_t screenId = 0;
44
45 namespace {
46
InitSurface()47 bool InitSurface()
48 {
49 std::cout << "InitSurface" << std::endl;
50 RSSurfaceNodeConfig surfaceNodeConfig;
51 surfaceNodeConfig.SurfaceNodeName = "AppMain_Window";
52 RSSurfaceNodeType surfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
53 std::cout << "RSSurfaceNode::Create" <<std::endl;
54 surfaceNode = RSSurfaceNode::Create(surfaceNodeConfig, surfaceNodeType);
55 if (!surfaceNode) {
56 return false;
57 }
58
59 surfaceNode->SetFrameGravity(Gravity::RESIZE_ASPECT_FILL);
60 surfaceNode->SetPositionZ(RSSurfaceNode::POINTER_WINDOW_POSITION_Z);
61 int width = (POINTER_WIDTH / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 3;
62 int height = (POINTER_HEIGHT / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 3;
63 surfaceNode->SetBounds(NODE_POSITION_X, NODE_POSITION_Y, width, height);
64 surfaceNode->SetBackgroundColor(SK_ColorGREEN);
65
66 std::shared_ptr<RSNode> rootNode = RSRootNode::Create();
67 auto node1 = RSCanvasNode::Create();
68 auto node2 = RSCanvasNode::Create();
69 auto node3 = RSCanvasNode::Create();
70 auto node4 = RSCanvasNode::Create();
71 rootNode->AddChild(node1);
72 rootNode->AddChild(node2);
73 node1->AddChild(node3);
74 node3->AddChild(node4);
75 auto rsUiDirector = RSUIDirector::Create();
76 rsUiDirector->Init();
77 rsUiDirector->SetRoot(rootNode->GetId());
78 rsUiDirector->SetRSSurfaceNode(surfaceNode);
79 rsUiDirector->SendMessages();
80 sleep(1);
81
82 RSSurfaceNodeConfig config;
83 surfaceNodeConfig.SurfaceNodeName = "UIExtension";
84 RSSurfaceNodeType nodeType = RSSurfaceNodeType::UI_EXTENSION_COMMON_NODE;
85 std::shared_ptr<RSSurfaceNode> uiExtension = RSSurfaceNode::Create(surfaceNodeConfig, nodeType, true, true);
86 int width1 = (POINTER_WIDTH / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 2;
87 int height1 = (POINTER_HEIGHT / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 2;
88 uiExtension->SetBounds(NODE_POSITION_X, NODE_POSITION_Y, width1, height1);
89 uiExtension->SetBackgroundColor(SK_ColorBLUE);
90 node4->AddChild(uiExtension);
91
92 surfaceNodeConfig.SurfaceNodeName = "SubUIExtension";
93 std::shared_ptr<RSSurfaceNode> subUIExtension = RSSurfaceNode::Create(surfaceNodeConfig, nodeType, true, true);
94 int width2 = (POINTER_WIDTH / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE;
95 int height2 = (POINTER_HEIGHT / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE;
96 subUIExtension->SetBounds(UIEXTENSION_POSITION_X, UIEXTENSION_POSITION_Y, width2, height2);
97 subUIExtension->SetBackgroundColor(SK_ColorRED);
98 auto node5 = RSCanvasNode::Create();
99 uiExtension->AddChild(node5, -1);
100 node5->AddChild(subUIExtension);
101
102 std::cout << "ScreenId: " << screenId << std::endl;
103 surfaceNode->AttachToDisplay(screenId);
104 RSTransaction::FlushImplicitTransaction();
105 std::cout << "RSTranscation::FlushImplicitTransaction" << std::endl;
106 sleep(SLEEP_TIME);
107
108 node4->RemoveChild(uiExtension);
109 RSTransaction::FlushImplicitTransaction();
110 std::cout << "RSTranscation::removechild" << std::endl;
111 sleep(SLEEP_TIME);
112
113 for (int i = 0; i < LIMIT; i++) {
114 node4->AddChild(uiExtension);
115 RSTransaction::FlushImplicitTransaction();
116 sleep(SLEEP_TIME);
117 node4->RemoveChild(uiExtension);
118 RSTransaction::FlushImplicitTransaction();
119 sleep(SLEEP_TIME);
120 }
121 return true;
122 }
123 }
124
main()125 int main()
126 {
127 std::cout << "rs pointer window demo start!" << std::endl;
128 InitSurface();
129 std::cout << "rs pointer window demo end!" << std::endl;
130 return 0;
131 }