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