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 "accesstoken_kit.h"
22 #ifdef SUPPORT_ACCESS_TOKEN
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 #endif
26 #include "platform/common/rs_system_properties.h"
27 #include "render/rs_filter.h"
28 #include "transaction/rs_transaction.h"
29 #include "ui/rs_root_node.h"
30 #include "ui/rs_display_node.h"
31 #include "ui/rs_surface_node.h"
32 #include "ui/rs_ui_director.h"
33
34 using namespace OHOS;
35 using namespace OHOS::Rosen;
36 using namespace std;
37
38 static std::shared_ptr<RSNode> rootNode;
39 static std::shared_ptr<RSCanvasNode> canvasNode;
40
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)41 static void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
42 {
43 std::cout << "rs uni render demo Init Rosen Backend!" << std::endl;
44
45 rootNode = RSRootNode::Create();
46 rootNode->SetBounds(0, 0, width, height);
47 rootNode->SetFrame(0, 0, width, height);
48 rootNode->SetBackgroundColor(Drawing::Color::COLOR_RED);
49
50 canvasNode = RSCanvasNode::Create();
51 canvasNode->SetBounds(100, 100, 300, 200);
52 canvasNode->SetFrame(100, 100, 300, 200);
53 canvasNode->SetBackgroundColor(0x6600ff00);
54
55 rootNode->AddChild(canvasNode, -1);
56
57 rsUiDirector->SetRSRootNode(rootNode->ReinterpretCastTo<RSRootNode>());
58 }
59
InitNativeTokenInfo()60 void InitNativeTokenInfo()
61 {
62 #ifdef SUPPORT_ACCESS_TOKEN
63 uint64_t tokenId;
64 const char *perms[1];
65 perms[0] = "ohos.permission.SYSTEM_FLOAT_WINDOW";
66 NativeTokenInfoParams infoInstance = {
67 .dcapsNum = 0,
68 .permsNum = 1,
69 .aclsNum = 0,
70 .dcaps = NULL,
71 .perms = perms,
72 .acls = NULL,
73 .processName = "rs_uni_render_pixelmap_demo",
74 .aplStr = "system_basic",
75 };
76 tokenId = GetAccessTokenId(&infoInstance);
77 SetSelfTokenID(tokenId);
78 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
79 #endif
80 }
81
main()82 int main()
83 {
84 #ifdef SUPPORT_ACCESS_TOKEN
85 InitNativeTokenInfo();
86
87 std::cout << "rs uni render demo start!" << std::endl;
88 RSSystemProperties::GetUniRenderEnabled();
89 sptr<WindowOption> option = new WindowOption();
90 option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
91 option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
92 option->SetWindowRect({ 0, 0, 500, 800 });
93 auto window = Window::Create("uni_render_demo", option);
94
95 window->Show();
96 sleep(2);
97 auto rect = window->GetRect();
98 while (rect.width_ == 0 && rect.height_ == 0) {
99 std::cout << "rs uni render demo create window failed: " << rect.width_ << " " << rect.height_ << std::endl;
100 window->Hide();
101 window->Destroy();
102 window = Window::Create("uni_render_demo", option);
103 window->Show();
104 rect = window->GetRect();
105 }
106
107 std::cout << "rs uni render demo create window " << rect.width_ << " " << rect.height_ << std::endl;
108 auto surfaceNode = window->GetSurfaceNode();
109 if (!RSSystemProperties::GetUniRenderEnabled() || surfaceNode->GetSurface() != nullptr) {
110 std::cout << "rs uni render demo not in uni render mode, exit! " << std::endl;
111 return 0;
112 }
113
114 auto rsUiDirector = RSUIDirector::Create();
115 rsUiDirector->Init();
116 RSTransaction::FlushImplicitTransaction();
117 sleep(1);
118
119 std::cout << "rs uni render demo stage 1 " << std::endl;
120 rsUiDirector->SetRSSurfaceNode(surfaceNode);
121 Init(rsUiDirector, rect.width_, rect.height_);
122 rsUiDirector->SendMessages();
123 sleep(1);
124
125 std::cout << "rs uni render demo stage 2 drawTextBlob" << std::endl;
126 std::cout << "Drawing does not support TextBlob" << std::endl;
127 canvasNode->FinishRecording();
128 rsUiDirector->SendMessages();
129 sleep(2);
130
131 std::cout << "rs uni render demo stage 3 SetFilter" << std::endl;
132 auto filter = RSFilter::CreateBlurFilter(5.f, 5.f);
133 canvasNode->SetFilter(filter);
134 rsUiDirector->SendMessages();
135 sleep(2);
136
137 std::cout << "rs uni render demo stage 4 SetBounds" << std::endl;
138 surfaceNode->SetBoundsWidth(250);
139 surfaceNode->SetBoundsHeight(400);
140 RSTransaction::FlushImplicitTransaction();
141 sleep(2);
142
143 std::cout << "rs uni render demo end!" << std::endl;
144 window->Hide();
145 window->Destroy();
146 #endif
147 return 0;
148 }
149