• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "accesstoken_kit.h"
26 #ifdef SUPPORT_ACCESS_TOKEN
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 #endif
30 #include "transaction/rs_transaction.h"
31 #include "ui/rs_root_node.h"
32 #include "ui/rs_display_node.h"
33 #include "ui/rs_surface_node.h"
34 #include "ui/rs_surface_extractor.h"
35 #include "ui/rs_ui_director.h"
36 
37 using namespace OHOS;
38 using namespace OHOS::Rosen;
39 using namespace std;
40 
41 namespace {
42 shared_ptr<RSNode> rootNode;
43 shared_ptr<RSCanvasNode> canvasNode;
Init(shared_ptr<RSUIDirector> rsUiDirector,int width,int height)44 void Init(shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
45 {
46     cout << "rs pixelmap demo Init Rosen Backend!" << endl;
47 
48     rootNode = RSRootNode::Create();
49     rootNode->SetBounds(0, 0, width, height);
50     rootNode->SetFrame(0, 0, width, height);
51     rootNode->SetBackgroundColor(Drawing::Color::COLOR_GREEN);
52 
53     rsUiDirector->SetRSRootNode(rootNode->ReinterpretCastTo<RSRootNode>());
54     canvasNode = RSCanvasNode::Create();
55     canvasNode->SetBounds(50, 50, width - 100, height - 100);
56     canvasNode->SetFrame(50, 50, width - 100, height - 100);
57     canvasNode->SetBorderColor(0xaaff0000);
58     canvasNode->SetBorderWidth(20);
59     canvasNode->SetBorderStyle(BorderStyle::SOLID);
60     rootNode->AddChild(canvasNode, -1);
61 }
62 
DecodePixelMap(const string & pathName,const Media::AllocatorType & allocatorType)63 shared_ptr<Media::PixelMap> DecodePixelMap(const string& pathName, const Media::AllocatorType& allocatorType)
64 {
65     cout << "decode start: ------------ " << pathName << endl;
66     cout << "decode 1: CreateImageSource" << endl;
67     uint32_t errCode = 0;
68     unique_ptr<Media::ImageSource> imageSource =
69         Media::ImageSource::CreateImageSource(pathName, Media::SourceOptions(), errCode);
70     if (imageSource == nullptr || errCode != 0) {
71         cout << "imageSource : " << (imageSource != nullptr) << ", err:" << errCode << endl;
72         return nullptr;
73     }
74 
75     cout << "decode 2: CreatePixelMap" << endl;
76     Media::DecodeOptions decodeOpt;
77     decodeOpt.allocatorType = allocatorType;
78     shared_ptr<Media::PixelMap> pixelmap = imageSource->CreatePixelMap(decodeOpt, errCode);
79     if (pixelmap == nullptr || errCode != 0) {
80         cout << "pixelmap == nullptr, err:" << errCode << endl;
81         return nullptr;
82     }
83 
84     cout << "w x h: " << pixelmap->GetWidth() << "x" << pixelmap->GetHeight() << endl;
85     cout << "AllocatorType: " << (int)pixelmap->GetAllocatorType() << endl;
86     cout << "fd: " << (!pixelmap->GetFd() ? "null" : to_string(*(int*)pixelmap->GetFd())) << endl;
87     cout << "decode success: ------------" << endl;
88     return pixelmap;
89 }
90 
InitNativeTokenInfo()91 void InitNativeTokenInfo()
92 {
93 #ifdef SUPPORT_ACCESS_TOKEN
94     uint64_t tokenId;
95     const char *perms[1];
96     perms[0] = "ohos.permission.SYSTEM_FLOAT_WINDOW";
97     NativeTokenInfoParams infoInstance = {
98         .dcapsNum = 0,
99         .permsNum = 1,
100         .aclsNum = 0,
101         .dcaps = NULL,
102         .perms = perms,
103         .acls = NULL,
104         .processName = "rs_uni_render_pixelmap_demo",
105         .aplStr = "system_basic",
106     };
107     tokenId = GetAccessTokenId(&infoInstance);
108     SetSelfTokenID(tokenId);
109     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
110 #endif
111 }
112 } // namespace
113 
main()114 int main()
115 {
116 #ifdef SUPPORT_ACCESS_TOKEN
117     InitNativeTokenInfo();
118 
119     cout << "rs pixelmap demo start!" << endl;
120     sptr<WindowOption> option = new WindowOption();
121     option->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
122     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
123     option->SetWindowRect({0, 0, 1260, 2720});
124     string demoName = "pixelmap_demo";
125     RSSystemProperties::GetUniRenderEnabled();
126     auto window = Window::Create(demoName, option);
127 
128     window->Show();
129     sleep(2);
130     auto rect = window->GetRect();
131     while (rect.width_ == 0 && rect.height_ == 0) {
132         cout << "rs demo create window failed: " << rect.width_ << " " << rect.height_ << endl;
133         window->Hide();
134         window->Destroy();
135         window = Window::Create(demoName, option);
136         window->Show();
137         rect = window->GetRect();
138     }
139     cout << "rs demo create window success: " << rect.width_ << " " << rect.height_ << endl;
140     auto surfaceNode = window->GetSurfaceNode();
141 
142     auto rsUiDirector = RSUIDirector::Create();
143     rsUiDirector->Init();
144     RSTransaction::FlushImplicitTransaction();
145     sleep(1);
146 
147     cout << "rs demo unirender enable : " << RSSystemProperties::GetUniRenderEnabled() << endl;
148     cout << "rs pixelmap demo stage 1: init" << endl;
149     rsUiDirector->SetRSSurfaceNode(surfaceNode);
150     Init(rsUiDirector, rect.width_, rect.height_);
151     rsUiDirector->SendMessages();
152     sleep(1);
153 
154     cout << "rs pixelmap demo stage 2: decode pixelmap" << endl;
155     auto allocatorType = Media::AllocatorType::SHARE_MEM_ALLOC;
156     shared_ptr<Media::PixelMap> bgpixelmap = DecodePixelMap("/data/local/tmp/test_bg.jpg", allocatorType);
157     shared_ptr<Media::PixelMap> maskPixelmap = DecodePixelMap("/data/local/tmp/mask.jpg", allocatorType);
158 
159     cout << "rs pixelmap demo stage 3: bgImage" << endl;
160     canvasNode->SetBgImageWidth(500);
161     canvasNode->SetBgImageHeight(600);
162     canvasNode->SetBgImagePositionX(10);
163     canvasNode->SetBgImagePositionY(10);
164 
165     auto rosenImage = make_shared<Rosen::RSImage>();
166     rosenImage->SetPixelMap(bgpixelmap);
167     rosenImage->SetImageRepeat(3);
168     rosenImage->SetImageFit((int)ImageFit::NONE);
169     cout << "SetBgImage" << endl;
170     canvasNode->SetBgImage(rosenImage);
171 
172     rsUiDirector->SendMessages();
173     sleep(2);
174 
175     cout << "rs pixelmap demo stage 4: bgImage SetBgImageInnerRect" << endl;
176     canvasNode->SetBgImageInnerRect({ 100, 100, 25, 25});
177 
178     rsUiDirector->SendMessages();
179     sleep(3);
180 
181     cout << "rs pixelmap demo stage 5: mask" << endl;
182     auto mask = RSMask::CreatePixelMapMask(maskPixelmap);
183     surfaceNode->SetMask(mask);
184     std::condition_variable m_cv;
185     std::mutex m_mutex;
186     rsUiDirector->SendMessages([&m_cv]() {
187         m_cv.notify_all();
188     });
189     {
190         using namespace std::chrono_literals;
191         std::unique_lock<std::mutex> m_lock { m_mutex };
192         auto status = m_cv.wait_for(m_lock, 2s);
193         if (status == std::cv_status::timeout) {
194             std::cout << ">>>N \n";
195         } else {
196             std::cout << ">>>Y \n";
197         }
198     }
199     sleep(10);
200 
201     cout << "rs pixelmap demo end!" << endl;
202     window->Hide();
203     window->Destroy();
204 #endif
205     return 0;
206 }
207