1 /*
2  * Copyright (c) 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 
16 #include <iostream>
17 #include "dm/display_manager.h"
18 #include "securec.h"
19 #include "transaction/rs_transaction.h"
20 #include "ui/rs_surface_node.h"
21 
22 using namespace OHOS;
23 using namespace OHOS::Rosen;
24 
25 constexpr uint32_t SLEEP_TIME = 3;
26 constexpr uint32_t PARENT_WIDTH = 100;
27 constexpr uint32_t PARENT_HEIGHT = 200;
28 constexpr uint32_t CHILD_WIDTH = 50;
29 constexpr uint32_t CHILD_HEIGHT = 50;
30 constexpr uint32_t STRIDE = 64;
31 
Resize(std::shared_ptr<RSSurfaceNode> surfaceNode,int32_t width,int32_t height)32 void Resize(std::shared_ptr<RSSurfaceNode> surfaceNode, int32_t width, int32_t height)
33 {
34     width = (width / STRIDE + 1) * STRIDE;
35     height = (height / STRIDE + 1) * STRIDE;
36     surfaceNode->SetBoundsWidth(width);
37     surfaceNode->SetBoundsHeight(height);
38 }
39 
InitSurface(std::shared_ptr<RSSurfaceNode> & surfaceNode,const std::string & surfaceName,int32_t width,int32_t height,uint32_t color)40 bool InitSurface(std::shared_ptr<RSSurfaceNode>& surfaceNode, const std::string& surfaceName,
41     int32_t width, int32_t height, uint32_t color)
42 {
43     std::cout << "InitSurface" << std::endl;
44     RSSurfaceNodeConfig surfaceNodeConfig;
45     surfaceNodeConfig.SurfaceNodeName = surfaceName;
46     RSSurfaceNodeType surfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
47     std::cout << "surface::Create" <<std::endl;
48     surfaceNode = RSSurfaceNode::Create(surfaceNodeConfig, surfaceNodeType);
49     if (!surfaceNode) {
50         return false;
51     }
52     std::cout << "SetFrameGravity" <<std::endl;
53     surfaceNode->SetFrameGravity(Gravity::RESIZE_ASPECT_FILL);
54     float position_z;
55     uint32_t left;
56     uint32_t top;
57     if (surfaceName.find("parentNode") != std::string::npos) {
58         position_z = RSSurfaceNode::POINTER_WINDOW_POSITION_Z;
59         left = 100;  // 100 left
60         top = 300;   // 300 top
61     } else {
62         position_z = RSSurfaceNode::POINTER_WINDOW_POSITION_Z + 1;
63         left = 0;
64         top = 0;
65     }
66     surfaceNode->SetPositionZ(position_z);
67     surfaceNode->SetBounds(left, top, (width / STRIDE + 1) * STRIDE, (height / STRIDE + 1) * STRIDE);
68     surfaceNode->SetBackgroundColor(color);
69     std::cout << "InitSurface success" << std::endl;
70     return true;
71 }
72 
main()73 int main()
74 {
75     std::cout << "rs pointer window demo start!" << std::endl;
76     std::cout << "rs pointer window demo stage 1 Init" << std::endl;
77     DisplayId displayId = DisplayManager::GetInstance().GetDefaultDisplayId();
78     std::shared_ptr<RSSurfaceNode> parentSurfaceNode;
79     std::shared_ptr<RSSurfaceNode> childSurfaceNode;
80     if (!InitSurface(parentSurfaceNode, "parentNode", PARENT_WIDTH, PARENT_HEIGHT, SK_ColorGREEN)) {
81         return 0;
82     }
83     if (!InitSurface(childSurfaceNode, "childNode", CHILD_WIDTH, CHILD_HEIGHT, SK_ColorBLUE)) {
84         return 0;
85     }
86 
87     parentSurfaceNode->AddChild(childSurfaceNode, 0);
88     // Attach RSSurfaceNode to RSDisplayNode through the AttachToDisplay interface of RSSurfaceNode
89     std::cout << "GetDisplayById: " << std::endl;
90     uint64_t screenId = DisplayManager::GetInstance().GetDisplayById(displayId)->GetId();
91     std::cout << "ScreenId: " << screenId << std::endl;
92     parentSurfaceNode->AttachToDisplay(screenId);
93 
94     std::cout << "RSTranscation::FlushImplicitTransaction" << std::endl;
95     RSTransaction::FlushImplicitTransaction();
96     sleep(SLEEP_TIME);
97 
98     // Attach and Detach
99     std::cout << "rs pointer window demo stage 2 Detach" << std::endl;
100     parentSurfaceNode->DetachToDisplay(screenId);
101     RSTransaction::FlushImplicitTransaction();
102     sleep(SLEEP_TIME);
103     std::cout << "rs pointer window demo stage 3 Attach" << std::endl;
104     parentSurfaceNode->AttachToDisplay(screenId);
105     RSTransaction::FlushImplicitTransaction();
106     sleep(SLEEP_TIME);
107 
108     // Resize
109     std::cout << "rs pointer window demo stage 4 Resize" << std::endl;
110     Resize(parentSurfaceNode, PARENT_WIDTH / 2, PARENT_HEIGHT / 2);  // 2 resize 1/2
111     RSTransaction::FlushImplicitTransaction();
112     sleep(SLEEP_TIME);
113 
114     std::cout << "rs pointer window demo end!" << std::endl;
115     return 0;
116 }