1 /*
2 * Copyright (c) 2021-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 <event_handler.h>
17 #include <iostream>
18
19 #include "animation/rs_curve_animation.h"
20 #include "animation/rs_transition.h"
21 #include "include/core/SkCanvas.h"
22 #include "include/core/SkImageInfo.h"
23 #include "render_context/render_context.h"
24 #include "transaction/rs_transaction.h"
25 #include "ui/rs_display_node.h"
26 #include "ui/rs_root_node.h"
27 #include "ui/rs_surface_extractor.h"
28 #include "ui/rs_surface_node.h"
29 #include "ui/rs_ui_director.h"
30 #include "wm/window.h"
31 #include "wm/window_scene.h"
32
33 using namespace OHOS;
34 using namespace OHOS::Rosen;
35 using namespace std;
36
37 std::shared_ptr<RSNode> rootNode;
38 std::vector<std::shared_ptr<RSCanvasNode>> nodes;
39
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)40 void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
41 {
42 std::cout << "rs app 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 rsUiDirector->SetRoot(rootNode->GetId());
50 }
51
52 std::unique_ptr<RSSurfaceFrame> framePtr;
53 RenderContext* rc_ = nullptr;
54
DrawSurface(SkRect surfaceGeometry,uint32_t color,SkRect shapeGeometry,std::shared_ptr<RSSurfaceNode> surfaceNode)55 void DrawSurface(
56 SkRect surfaceGeometry, uint32_t color, SkRect shapeGeometry, std::shared_ptr<RSSurfaceNode> surfaceNode)
57 {
58 auto x = surfaceGeometry.x();
59 auto y = surfaceGeometry.y();
60 auto width = surfaceGeometry.width();
61 auto height = surfaceGeometry.height();
62 surfaceNode->SetBounds(x, y, width, height);
63 std::shared_ptr<RSSurface> rsSurface = RSSurfaceExtractor::ExtractRSSurface(surfaceNode);
64 if (rsSurface == nullptr) {
65 return;
66 }
67 if (rc_) {
68 rsSurface->SetRenderContext(rc_);
69 }
70 auto frame = rsSurface->RequestFrame(width, height);
71 framePtr = std::move(frame);
72 if (!framePtr) {
73 printf("DrawSurface frameptr is nullptr");
74 return;
75 }
76 auto canvas = framePtr->GetCanvas();
77 if (!canvas) {
78 printf("DrawSurface canvas is nullptr");
79 return;
80 }
81 SkPaint paint;
82 paint.setAntiAlias(true);
83 paint.setStyle(SkPaint::kFill_Style);
84 paint.setStrokeWidth(20);
85 paint.setStrokeJoin(SkPaint::kRound_Join);
86 paint.setColor(color);
87
88 canvas->drawRect(shapeGeometry, paint);
89 framePtr->SetDamageRegion(0, 0, width, height);
90 auto framePtr1 = std::move(framePtr);
91 rsSurface->FlushFrame(framePtr1);
92 }
93
main()94 int main()
95 {
96 std::cout << "rs app demo start!" << std::endl;
97
98 sptr<WindowOption> option = new WindowOption();
99 option->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
100 option->SetWindowRect({0, 0, 2560, 112});
101
102 auto scene = new WindowScene();
103
104 std::shared_ptr<AbilityRuntime::Context> context = nullptr;
105 sptr<IWindowLifeCycle> listener = nullptr;
106 scene->Init(0, context, listener, option);
107 auto window = scene->GetMainWindow();
108 scene->GoForeground();
109 auto surfaceNode = window->GetSurfaceNode();
110
111 auto rsUiDirector = RSUIDirector::Create();
112 rsUiDirector->Init();
113 auto runner = OHOS::AppExecFwk::EventRunner::Create(true);
114 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
115 rsUiDirector->SetUITaskRunner(
116 [handler](const std::function<void()>& task) {
117 handler->PostTask(task);
118 });
119 runner->Run();
120
121 RSTransaction::FlushImplicitTransaction();
122 DrawSurface(SkRect::MakeXYWH(0, 0, 2800, 1600), 0xffffe4c4, SkRect::MakeXYWH(0, 0, 2800, 1600), surfaceNode);
123
124 std::cout << "rs app demo set up finished!" << std::endl;
125 RSTransaction::FlushImplicitTransaction();
126 sleep(5);
127
128 std::cout << "adding animation" << std::endl;
129
130 RSTransaction::FlushImplicitTransaction();
131 sleep(5);
132
133 std::cout << "adding transition" << std::endl;
134 auto animation2 = std::make_shared<RSTransition>(RSTransitionEffect::OPACITY, true);
135 animation2->SetDuration(100);
136 animation2->SetTimingCurve(RSAnimationTimingCurve::EASE_IN_OUT);
137 animation2->SetFinishCallback([]() {
138 std::cout << "animation2 finish" << std::endl;
139 });
140 surfaceNode->AddAnimation(animation2);
141
142 RSTransaction::FlushImplicitTransaction();
143 sleep(5);
144
145 std::cout << "rs app demo end!" << std::endl;
146 window->Hide();
147 window->Destroy();
148 return 0;
149 }
150