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 "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "wm/window.h"
22
23 #include "include/core/SkCanvas.h"
24 #include "include/core/SkImageInfo.h"
25 #include "platform/common/rs_system_properties.h"
26 #include "render/rs_filter.h"
27 #include "transaction/rs_transaction.h"
28 #include "ui/rs_root_node.h"
29 #include "ui/rs_display_node.h"
30 #include "ui/rs_surface_node.h"
31 #include "ui/rs_ui_director.h"
32
33 using namespace OHOS;
34 using namespace OHOS::Rosen;
35 using namespace std;
36
37 static std::shared_ptr<RSNode> rootNode;
38 static std::shared_ptr<RSCanvasNode> canvasNode;
39
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)40 static void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
41 {
42 std::cout << "rs uni render demo Init Rosen Backend!" << std::endl;
43
44 rootNode = RSRootNode::Create();
45 rootNode->SetBounds(0, 0, width, height);
46 rootNode->SetFrame(0, 0, width, height);
47 rootNode->SetBackgroundColor(SK_ColorRED);
48
49 canvasNode = RSCanvasNode::Create();
50 canvasNode->SetBounds(100, 100, 300, 200);
51 canvasNode->SetFrame(100, 100, 300, 200);
52 canvasNode->SetBackgroundColor(0x6600ff00);
53
54 rootNode->AddChild(canvasNode, -1);
55
56 rsUiDirector->SetRoot(rootNode->GetId());
57 }
58
main()59 int main()
60 {
61 std::cout << "rs uni render demo start!" << std::endl;
62 RSSystemProperties::GetUniRenderEnabled();
63 sptr<WindowOption> option = new WindowOption();
64 option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
65 option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
66 option->SetWindowRect({ 0, 0, 500, 800 });
67 auto window = Window::Create("uni_render_demo", option);
68
69 window->Show();
70 auto rect = window->GetRect();
71 while (rect.width_ == 0 && rect.height_ == 0) {
72 std::cout << "rs uni render demo create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
73 window->Hide();
74 window->Destroy();
75 window = Window::Create("uni_render_demo", option);
76 window->Show();
77 rect = window->GetRect();
78 }
79
80 std::cout << "rs uni render demo create window " << rect.width_ << " " << rect.height_ << std::endl;
81 auto surfaceNode = window->GetSurfaceNode();
82 if (!RSSystemProperties::GetUniRenderEnabled() || surfaceNode->GetSurface() != nullptr) {
83 std::cout << "rs uni render demo not in uni render mode, exit! " << std::endl;
84 return 0;
85 }
86
87 auto rsUiDirector = RSUIDirector::Create();
88 rsUiDirector->Init();
89 RSTransaction::FlushImplicitTransaction();
90 sleep(1);
91
92 std::cout << "rs uni render demo stage 1 " << std::endl;
93 rsUiDirector->SetRSSurfaceNode(surfaceNode);
94 Init(rsUiDirector, rect.width_, rect.height_);
95 rsUiDirector->SendMessages();
96 sleep(1);
97
98 std::cout << "rs uni render demo stage 2 drawTextBlob" << std::endl;
99 sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
100 SkFont font;
101 font.setSize(35);
102 font.setTypeface(tf);
103 sk_sp<SkTextBlob> textBlob = SkTextBlob::MakeFromString("UniRenderDemo", font);
104 auto canvas = canvasNode->BeginRecording(rect.width_, rect.height_);
105 SkPaint paint;
106 paint.setColor(SK_ColorGREEN);
107 paint.setAntiAlias(true);
108 canvas->drawTextBlob(textBlob, 25.f, 100.f, paint);
109 canvasNode->FinishRecording();
110 rsUiDirector->SendMessages();
111 sleep(2);
112
113 std::cout << "rs uni render demo stage 3 SetFilter" << std::endl;
114 auto filter = RSFilter::CreateBlurFilter(5.f, 5.f);
115 canvasNode->SetFilter(filter);
116 rsUiDirector->SendMessages();
117 sleep(2);
118
119 std::cout << "rs uni render demo stage 4 SetBounds" << std::endl;
120 surfaceNode->SetBoundsWidth(250);
121 surfaceNode->SetBoundsHeight(400);
122 RSTransaction::FlushImplicitTransaction();
123 sleep(2);
124
125 std::cout << "rs uni render demo end!" << std::endl;
126 window->Hide();
127 window->Destroy();
128 return 0;
129 }
130