1 /*
2  * Copyright (c) 2022 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 "window.h"
20 
21 #include "platform/common/rs_system_properties.h"
22 #include "render/rs_filter.h"
23 #include "transaction/rs_transaction.h"
24 #include "ui/rs_root_node.h"
25 #include "ui/rs_display_node.h"
26 #include "ui/rs_surface_node.h"
27 #include "ui/rs_ui_director.h"
28 
29 using namespace OHOS;
30 using namespace OHOS::Rosen;
31 using namespace std;
32 
33 static std::shared_ptr<RSNode> rootNode;
34 static std::shared_ptr<RSCanvasNode> canvasNode;
35 
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)36 static void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
37 {
38     std::cout << "rs uni render 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     canvasNode = RSCanvasNode::Create();
46     canvasNode->SetBounds(100, 100, 300, 200);
47     canvasNode->SetFrame(100, 100, 300, 200);
48     canvasNode->SetBackgroundColor(0x6600ff00);
49 
50     rootNode->AddChild(canvasNode, -1);
51 
52     rsUiDirector->SetRoot(rootNode->GetId());
53 }
54 
main()55 int main()
56 {
57     std::cout << "rs uni render demo start!" << std::endl;
58     RSSystemProperties::GetUniRenderEnabled();
59     sptr<WindowOption> option = new WindowOption();
60     option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
61     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
62     option->SetWindowRect({ 0, 0, 500, 800 });
63     auto window = Window::Create("uni_render_demo", option);
64 
65     window->Show();
66     auto rect = window->GetRect();
67     while (rect.width_ == 0 && rect.height_ == 0) {
68         std::cout << "rs uni render demo create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
69         window->Hide();
70         window->Destroy();
71         window = Window::Create("uni_render_demo", option);
72         window->Show();
73         rect = window->GetRect();
74     }
75 
76     std::cout << "rs uni render demo create window " << rect.width_ << " " << rect.height_ << std::endl;
77     auto surfaceNode = window->GetSurfaceNode();
78     if (!RSSystemProperties::GetUniRenderEnabled() || surfaceNode->GetSurface() != nullptr) {
79         std::cout << "rs uni render demo not in uni render mode, exit! " << std::endl;
80         return 0;
81     }
82 
83     auto rsUiDirector = RSUIDirector::Create();
84     rsUiDirector->Init();
85     RSTransaction::FlushImplicitTransaction();
86     sleep(1);
87 
88     std::cout << "rs uni render demo stage 1 " << std::endl;
89     rsUiDirector->SetRSSurfaceNode(surfaceNode);
90     Init(rsUiDirector, rect.width_, rect.height_);
91     rsUiDirector->SendMessages();
92     sleep(1);
93 
94     std::cout << "rs uni render demo stage 2 drawTextBlob" << std::endl;
95     std::cout << "Drawing does not support TextBlob" << std::endl;
96     canvasNode->FinishRecording();
97     rsUiDirector->SendMessages();
98     sleep(2);
99 
100     std::cout << "rs uni render demo stage 3 SetFilter" << std::endl;
101     auto filter = RSFilter::CreateBlurFilter(5.f, 5.f);
102     canvasNode->SetFilter(filter);
103     rsUiDirector->SendMessages();
104     sleep(2);
105 
106     std::cout << "rs uni render demo stage 4 SetBounds" << std::endl;
107     surfaceNode->SetBoundsWidth(250);
108     surfaceNode->SetBoundsHeight(400);
109     RSTransaction::FlushImplicitTransaction();
110     sleep(2);
111 
112     std::cout << "rs uni render demo end!" << std::endl;
113     window->Hide();
114     window->Destroy();
115     return 0;
116 }
117