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 
22 #include "render_context/render_context.h"
23 #include "transaction/rs_transaction.h"
24 #include "ui/rs_display_node.h"
25 #include "ui/rs_root_node.h"
26 #include "ui/rs_surface_extractor.h"
27 #include "ui/rs_surface_node.h"
28 #include "ui/rs_ui_director.h"
29 #include "window.h"
30 #include "window_scene.h"
31 
32 using namespace OHOS;
33 using namespace OHOS::Rosen;
34 using namespace std;
35 
36 constexpr uint32_t SLEEP_TIME = 5;
37 
38 std::shared_ptr<RSNode> rootNode;
39 std::vector<std::shared_ptr<RSCanvasNode>> nodes;
40 
Init(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)41 void Init(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
42 {
43     std::cout << "rs app 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     rsUiDirector->SetRoot(rootNode->GetId());
51 }
52 
53 std::unique_ptr<RSSurfaceFrame> framePtr;
54 RenderContext* rc_ = nullptr;
55 
DrawSurface(Drawing::Rect surfaceGeometry,uint32_t color,Drawing::Rect shapeGeometry,std::shared_ptr<RSSurfaceNode> surfaceNode)56 void DrawSurface(Drawing::Rect surfaceGeometry,
57     uint32_t color, Drawing::Rect shapeGeometry, std::shared_ptr<RSSurfaceNode> surfaceNode)
58 {
59     auto x = surfaceGeometry.GetLeft();
60     auto y = surfaceGeometry.GetTop();
61     auto width = surfaceGeometry.GetWidth();
62     auto height = surfaceGeometry.GetHeight();
63     surfaceNode->SetBounds(x, y, width, height);
64     std::shared_ptr<RSSurface> rsSurface = RSSurfaceExtractor::ExtractRSSurface(surfaceNode);
65     if (rsSurface == nullptr) {
66         return;
67     }
68     if (rc_) {
69         rsSurface->SetRenderContext(rc_);
70     }
71     auto frame = rsSurface->RequestFrame(width, height);
72     framePtr = std::move(frame);
73     if (!framePtr) {
74         printf("DrawSurface frameptr is nullptr");
75         return;
76     }
77     auto canvas = framePtr->GetCanvas();
78     if (!canvas) {
79         printf("DrawSurface canvas is nullptr");
80         return;
81     }
82     Drawing::Brush brush;
83     brush.SetAntiAlias(true);
84     brush.SetColor(color);
85 
86     canvas->AttachBrush(brush);
87     canvas->DrawRect(shapeGeometry);
88     canvas->DetachBrush();
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, uint32_t delay) { handler->PostTask(task); });
117     runner->Run();
118 
119     RSTransaction::FlushImplicitTransaction();
120     DrawSurface(Drawing::Rect(0, 0, 2800, 1600), 0xffffe4c4, Drawing::Rect(0, 0, 2800, 1600), surfaceNode);
121     std::cout << "rs app demo set up finished!" << std::endl;
122     RSTransaction::FlushImplicitTransaction();
123     sleep(SLEEP_TIME);
124 
125     std::cout << "adding animation" << std::endl;
126 
127     RSTransaction::FlushImplicitTransaction();
128     sleep(SLEEP_TIME);
129 
130     std::cout << "adding transition" << std::endl;
131     auto animation2 = std::make_shared<RSTransition>(RSTransitionEffect::OPACITY, true);
132     animation2->SetDuration(100);
133     animation2->SetTimingCurve(RSAnimationTimingCurve::EASE_IN_OUT);
134     animation2->SetFinishCallback([]() {
135         std::cout << "animation2 finish" << std::endl;
136     });
137     surfaceNode->AddAnimation(animation2);
138 
139     RSTransaction::FlushImplicitTransaction();
140     sleep(SLEEP_TIME);
141 
142     std::cout << "rs app demo end!" << std::endl;
143     window->Hide();
144     window->Destroy();
145     return 0;
146 }
147