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 <surface.h>
18
19 #include "wm/window.h"
20
21
22 #include "transaction/rs_transaction.h"
23 #include "ui/rs_root_node.h"
24 #include "ui/rs_display_node.h"
25 #include "ui/rs_surface_node.h"
26 #include "ui/rs_surface_extractor.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
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)36 void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
37 {
38 std::cout << "rs app demo Init Rosen Backend!" << std::endl;
39
40 rootNode = RSRootNode::Create();
41 rootNode->SetBounds(0, 0, width, height);
42 rootNode->SetFrame(0, 0, width, height);
43 rootNode->SetBackgroundColor(Drawing::Color::COLOR_RED);
44
45 rsUiDirector->SetRoot(rootNode->GetId());
46 }
47
main()48 int main()
49 {
50 std::cout << "rs app demo start!" << std::endl;
51 sptr<WindowOption> option = new WindowOption();
52 option->SetWindowType(WindowType::WINDOW_TYPE_STATUS_BAR);
53 option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
54 option->SetWindowRect({0, 0, 2560, 112});
55 auto window = Window::Create("app_demo", option);
56
57 window->Show();
58 auto rect = window->GetRect();
59 while (rect.width_ == 0 && rect.height_ == 0) {
60 std::cout << "rs app demo create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
61 window->Hide();
62 window->Destroy();
63 window = Window::Create("app_demo", option);
64 window->Show();
65 rect = window->GetRect();
66 }
67 std::cout << "rs app demo create window " << rect.width_ << " " << rect.height_ << std::endl;
68 auto surfaceNode = window->GetSurfaceNode();
69
70 auto rsUiDirector = RSUIDirector::Create();
71 rsUiDirector->Init();
72 RSTransaction::FlushImplicitTransaction();
73 sleep(1);
74
75 std::cout << "rs app demo stage 1 " << std::endl;
76 rsUiDirector->SetRSSurfaceNode(surfaceNode);
77 Init(rsUiDirector, rect.width_, rect.height_);
78 rsUiDirector->SendMessages();
79 sleep(1);
80
81 std::cout << "rs app demo stage 2 " << std::endl;
82 int resizeH = 1600;
83 window->Resize(2560, resizeH);
84 rootNode->SetBounds(0, 0, 2560, resizeH);
85 rootNode->SetBackgroundColor(Drawing::Color::COLOR_YELLOW);
86 rsUiDirector->SendMessages();
87 sleep(4);
88
89 std::cout << "rs app demo stage 3 " << std::endl;
90 rootNode->SetBackgroundColor(Drawing::Color::COLOR_BLUE);
91 rsUiDirector->SendMessages();
92 sleep(1);
93
94 std::cout << "rs app demo stage 4 bufferAvailble" << std::endl;
95 surfaceNode->SetIsNotifyUIBufferAvailable(false);
96 surfaceNode->SetBufferAvailableCallback([]() {
97 std::cout << "SetBufferAvailableCallback 1" << std::endl;
98 });
99 rootNode->SetBackgroundColor(Drawing::Color::COLOR_YELLOW);
100 rsUiDirector->SendMessages();
101 sleep(1);
102
103 std::cout << "rs app demo stage 5 bufferAvailble " << std::endl;
104 surfaceNode->SetIsNotifyUIBufferAvailable(false);
105 surfaceNode->SetBufferAvailableCallback([]() {
106 std::cout << "SetBufferAvailableCallback 2" << std::endl;
107 });
108 rootNode->SetBackgroundColor(Drawing::Color::COLOR_BLUE);
109 rsUiDirector->SendMessages();
110 sleep(1);
111
112 return 0;
113 }
114