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 <iostream>
17
18 #include "dm/display_manager.h"
19 #include "securec.h"
20
21 #include "transaction/rs_interfaces.h"
22 #include "transaction/rs_transaction.h"
23 #include "ui/rs_display_node.h"
24 #include "ui/rs_root_node.h"
25 #include "ui/rs_surface_node.h"
26 #include "ui/rs_ui_context.h"
27 #include "ui/rs_ui_director.h"
28
29 using namespace OHOS;
30 using namespace OHOS::Rosen;
31 using namespace std;
32
33 std::shared_ptr<RSNode> rootNode;
34 std::vector<std::shared_ptr<RSCanvasNode>> nodes;
35 uint64_t screenId = 0;
36
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height,std::vector<std::shared_ptr<RSCanvasNode>> & nodes,std::shared_ptr<RSNode> & rootNode)37 void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height,
38 std::vector<std::shared_ptr<RSCanvasNode>>& nodes, std::shared_ptr<RSNode>& rootNode)
39 {
40 std::cout << "rs app demo Init Rosen Backend!" << std::endl;
41 auto rsUIContext = rsUiDirector->GetRSUIContext();
42 rootNode = RSRootNode::Create(false, false, rsUIContext);
43 rootNode->SetBounds(0, 0, width, height);
44 rootNode->SetFrame(0, 0, width, height);
45 rootNode->SetBackgroundColor(Drawing::Color::COLOR_BLACK);
46
47 nodes.emplace_back(RSCanvasNode::Create(false, false, rsUIContext));
48 nodes[0]->SetBounds(0, 0, 100, 100);
49 nodes[0]->SetFrame(0, 0, 100, 100);
50 nodes[0]->SetBackgroundColor(Drawing::Color::COLOR_BLUE);
51
52 rootNode->AddChild(nodes[0], -1);
53 rsUiDirector->SetRSRootNode(rootNode->ReinterpretCastTo<RSRootNode>());
54 }
55
main()56 int main()
57 {
58 std::cout << "render service client first frame callback demo start!" << std::endl;
59 std::shared_ptr<RSNode> rootNode;
60 std::vector<std::shared_ptr<RSCanvasNode>> nodes;
61 std::shared_ptr<RSUIDirector> rsUiDirector = RSUIDirector::Create();
62 rsUiDirector->Init(true, true);
63 auto uiContext = rsUiDirector->GetRSUIContext();
64 RSSurfaceNodeConfig surfaceNodeConfig;
65 surfaceNodeConfig.SurfaceNodeName = "AppMain_Window";
66 RSSurfaceNodeType surfaceNodeType = RSSurfaceNodeType::APP_WINDOW_NODE;
67 auto surfaceNode = RSSurfaceNode::Create(surfaceNodeConfig, surfaceNodeType, true, false, uiContext);
68 surfaceNode->SetFrameGravity(Gravity::RESIZE_ASPECT_FILL);
69 surfaceNode->SetPositionZ(RSSurfaceNode::POINTER_WINDOW_POSITION_Z);
70 surfaceNode->SetBounds(0, 0, 600, 600);
71 std::cout << "rs app demo stage 1 " << std::endl;
72 rsUiDirector->SetRSSurfaceNode(surfaceNode);
73 Init(rsUiDirector, 300, 300, nodes, rootNode);
74 surfaceNode->AttachToDisplay(screenId);
75 auto transaction = uiContext->GetRSTransaction();
76 if (!transaction) {
77 std::cout << "!transaction " << std::endl;
78 return -1;
79 }
80 transaction->FlushImplicitTransaction();
81
82 sleep(2);
83 std::cout << "rs app demo stage 2 " << std::endl;
84 rootNode->SetBackgroundColor(Drawing::Color::COLOR_YELLOW);
85 transaction->FlushImplicitTransaction();
86 sleep(2);
87
88 std::cout << "rs app demo stage 3 " << std::endl;
89 rootNode->SetBackgroundColor(Drawing::Color::COLOR_BLUE);
90 transaction->FlushImplicitTransaction();
91 sleep(2);
92
93 std::cout << "rs app demo stage 4 bufferAvailble" << std::endl;
94 surfaceNode->SetIsNotifyUIBufferAvailable(false);
95 surfaceNode->SetBufferAvailableCallback([]() {
96 std::cout << "SetBufferAvailableCallback 1" << std::endl;
97 });
98 rootNode->SetBackgroundColor(Drawing::Color::COLOR_YELLOW);
99 transaction->FlushImplicitTransaction();
100 sleep(2);
101
102 std::cout << "rs app demo stage 5 bufferAvailble " << std::endl;
103 surfaceNode->SetIsNotifyUIBufferAvailable(false);
104 surfaceNode->SetBufferAvailableCallback([]() {
105 std::cout << "SetBufferAvailableCallback 2" << std::endl;
106 });
107 rootNode->SetBackgroundColor(Drawing::Color::COLOR_BLUE);
108 transaction->FlushImplicitTransaction();
109 sleep(4);
110
111 return 0;
112 }
113