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 node4->SetBounds(NODE_POSITION_X, 0, width, height);
72 node4->SetBackgroundColor(SK_ColorYELLOW);
73 rootNode->AddChild(node1);
74 rootNode->AddChild(node2);
75 node1->AddChild(node3);
76 node3->AddChild(node4);
77 auto rsUiDirector = RSUIDirector::Create();
78 rsUiDirector->Init();
79 rsUiDirector->SetRSRootNode(rootNode->ReinterpretCastTo<RSRootNode>());
80 rsUiDirector->SetRSSurfaceNode(surfaceNode);
81 rsUiDirector->SendMessages();
82 sleep(1);
83
84 RSSurfaceNodeConfig config;
85 surfaceNodeConfig.SurfaceNodeName = "UIExtension";
86 RSSurfaceNodeType nodeType = RSSurfaceNodeType::UI_EXTENSION_COMMON_NODE;
87 std::shared_ptr<RSSurfaceNode> uiExtension = RSSurfaceNode::Create(surfaceNodeConfig, nodeType, true, true);
88 int width1 = (POINTER_WIDTH / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 2;
89 int height1 = (POINTER_HEIGHT / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE * 2;
90 uiExtension->SetBounds(NODE_POSITION_X, NODE_POSITION_Y, width1, height1);
91 uiExtension->SetBackgroundColor(SK_ColorBLUE);
92 node4->AddChild(uiExtension);
93
94 surfaceNodeConfig.SurfaceNodeName = "SubUIExtension";
95 std::shared_ptr<RSSurfaceNode> subUIExtension = RSSurfaceNode::Create(surfaceNodeConfig, nodeType, true, true);
96 int width2 = (POINTER_WIDTH / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE;
97 int height2 = (POINTER_HEIGHT / POINTER_WINDOW_INIT_SIZE + 1) * POINTER_WINDOW_INIT_SIZE;
98 subUIExtension->SetBounds(UIEXTENSION_POSITION_X, UIEXTENSION_POSITION_Y, width2, height2);
99 subUIExtension->SetBackgroundColor(SK_ColorRED);
100 auto node5 = RSCanvasNode::Create();
101 uiExtension->AddChild(node5, -1);
102 node5->AddChild(subUIExtension);
103
104 std::cout << "ScreenId: " << screenId << std::endl;
105 surfaceNode->AttachToDisplay(screenId);
106 RSTransaction::FlushImplicitTransaction();
107 std::cout << "RSTranscation::FlushImplicitTransaction" << std::endl;
108 sleep(SLEEP_TIME);
109
110 node4->RemoveChild(uiExtension);
111 RSTransaction::FlushImplicitTransaction();
112 std::cout << "RSTranscation::removechild" << std::endl;
113 sleep(SLEEP_TIME);
114
115 for (int i = 0; i < LIMIT; i++) {
116 node4->AddChild(uiExtension);
117 RSTransaction::FlushImplicitTransaction();
118 sleep(SLEEP_TIME);
119 node4->RemoveChild(uiExtension);
120 RSTransaction::FlushImplicitTransaction();
121 sleep(SLEEP_TIME);
122 }
123 return true;
124 }
125 }
126
main()127 int main()
128 {
129 std::cout << "rs pointer window demo start!" << std::endl;
130 InitSurface();
131 std::cout << "rs pointer window demo end!" << std::endl;
132 return 0;
133 }