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 "foundation/window/window_manager/interfaces/innerkits/wm/window.h"
20
21 #include "image_source.h"
22
23
24 #include "pixel_map.h"
25
26 #include "transaction/rs_transaction.h"
27 #include "ui/rs_root_node.h"
28 #include "ui/rs_display_node.h"
29 #include "ui/rs_surface_node.h"
30 #include "ui/rs_surface_extractor.h"
31 #include "ui/rs_ui_director.h"
32
33 using namespace OHOS;
34 using namespace OHOS::Rosen;
35 using namespace std;
36
37 namespace {
38 shared_ptr<RSNode> rootNode;
39 shared_ptr<RSCanvasNode> canvasNode;
Init(shared_ptr<RSUIDirector> rsUiDirector,int width,int height)40 void Init(shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
41 {
42 cout << "rs pixelmap demo Init Rosen Backend!" << endl;
43
44 rootNode = RSRootNode::Create();
45 rootNode->SetBounds(0, 0, width, height);
46 rootNode->SetFrame(0, 0, width, height);
47 rootNode->SetBackgroundColor(Drawing::Color::COLOR_GREEN);
48
49 rsUiDirector->SetRoot(rootNode->GetId());
50 canvasNode = RSCanvasNode::Create();
51 canvasNode->SetBounds(50, 50, width - 100, height - 100);
52 canvasNode->SetFrame(50, 50, width - 100, height - 100);
53 canvasNode->SetBorderColor(0xaaff0000);
54 canvasNode->SetBorderWidth(20);
55 canvasNode->SetBorderStyle(BorderStyle::SOLID);
56 rootNode->AddChild(canvasNode, -1);
57 }
58
DecodePixelMap(const string & pathName,const Media::AllocatorType & allocatorType)59 shared_ptr<Media::PixelMap> DecodePixelMap(const string& pathName, const Media::AllocatorType& allocatorType)
60 {
61 cout << "decode start: ------------ " << pathName << endl;
62 cout << "decode 1: CreateImageSource" << endl;
63 uint32_t errCode = 0;
64 unique_ptr<Media::ImageSource> imageSource =
65 Media::ImageSource::CreateImageSource(pathName, Media::SourceOptions(), errCode);
66 if (imageSource == nullptr || errCode != 0) {
67 cout << "imageSource : " << (imageSource != nullptr) << ", err:" << errCode << endl;
68 return nullptr;
69 }
70
71 cout << "decode 2: CreatePixelMap" << endl;
72 Media::DecodeOptions decodeOpt;
73 decodeOpt.allocatorType = allocatorType;
74 shared_ptr<Media::PixelMap> pixelmap = imageSource->CreatePixelMap(decodeOpt, errCode);
75 if (pixelmap == nullptr || errCode != 0) {
76 cout << "pixelmap == nullptr, err:" << errCode << endl;
77 return nullptr;
78 }
79
80 cout << "w x h: " << pixelmap->GetWidth() << "x" << pixelmap->GetHeight() << endl;
81 cout << "AllocatorType: " << (int)pixelmap->GetAllocatorType() << endl;
82 cout << "fd: " << (!pixelmap->GetFd() ? "null" : to_string(*(int*)pixelmap->GetFd())) << endl;
83 cout << "decode success: ------------" << endl;
84 return pixelmap;
85 }
86 } // namespace
87
main()88 int main()
89 {
90 cout << "rs pixelmap demo start!" << endl;
91 sptr<WindowOption> option = new WindowOption();
92 option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
93 option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
94 option->SetWindowRect({0, 0, 1260, 2720});
95 string demoName = "pixelmap_demo";
96 RSSystemProperties::GetUniRenderEnabled();
97 auto window = Window::Create(demoName, option);
98
99 window->Show();
100 sleep(2);
101 auto rect = window->GetRect();
102 while (rect.width_ == 0 && rect.height_ == 0) {
103 cout << "rs demo create window failed: " << rect.width_ << " " << rect.height_ << endl;
104 window->Hide();
105 window->Destroy();
106 window = Window::Create(demoName, option);
107 window->Show();
108 rect = window->GetRect();
109 }
110 cout << "rs demo create window success: " << rect.width_ << " " << rect.height_ << endl;
111 auto surfaceNode = window->GetSurfaceNode();
112
113 auto rsUiDirector = RSUIDirector::Create();
114 rsUiDirector->Init();
115 RSTransaction::FlushImplicitTransaction();
116 sleep(1);
117
118 cout << "rs demo unirender enable : " << RSSystemProperties::GetUniRenderEnabled() << endl;
119 cout << "rs pixelmap demo stage 1: init" << endl;
120 rsUiDirector->SetRSSurfaceNode(surfaceNode);
121 Init(rsUiDirector, rect.width_, rect.height_);
122 rsUiDirector->SendMessages();
123 sleep(1);
124
125 cout << "rs pixelmap demo stage 2: decode pixelmap" << endl;
126 auto allocatorType = Media::AllocatorType::SHARE_MEM_ALLOC;
127 shared_ptr<Media::PixelMap> bgpixelmap = DecodePixelMap("/data/local/tmp/test_bg.jpg", allocatorType);
128 shared_ptr<Media::PixelMap> maskPixelmap = DecodePixelMap("/data/local/tmp/mask.jpg", allocatorType);
129 if (bgpixelmap == nullptr) {
130 return -1;
131 }
132
133 cout << "rs pixelmap demo stage 3: bgImage" << endl;
134 canvasNode->SetBgImageWidth(500);
135 canvasNode->SetBgImageHeight(600);
136 canvasNode->SetBgImagePositionX(10);
137 canvasNode->SetBgImagePositionY(10);
138
139 auto rosenImage = make_shared<Rosen::RSImage>();
140 rosenImage->SetPixelMap(bgpixelmap);
141 rosenImage->SetImageRepeat(3);
142 rosenImage->SetImageFit((int)ImageFit::NONE);
143 cout << "SetBgImage" << endl;
144 canvasNode->SetBgImage(rosenImage);
145
146 rsUiDirector->SendMessages();
147 sleep(2);
148
149 cout << "rs pixelmap demo stage 4: bgImage SetBgImageInnerRect" << endl;
150 canvasNode->SetBgImageInnerRect({ 100, 100, 25, 25});
151
152 rsUiDirector->SendMessages();
153 sleep(3);
154
155 cout << "rs pixelmap demo stage 5: mask" << endl;
156 auto mask = RSMask::CreatePixelMapMask(maskPixelmap);
157 surfaceNode->SetMask(mask);
158 rsUiDirector->SendMessages();
159 sleep(10);
160
161 cout << "rs pixelmap demo end!" << endl;
162 window->Hide();
163 window->Destroy();
164 return 0;
165 }
166