• 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 <message_parcel.h>
18 #include <sstream>
19 #include <string>
20 #include <surface.h>
21 
22 #include "image_source.h"
23 #include "include/core/SkCanvas.h"
24 #include "include/core/SkImageInfo.h"
25 #include "pixel_map.h"
26 #include "wm/window.h"
27 
28 #include "pipeline/rs_recording_canvas.h"
29 #include "platform/common/rs_system_properties.h"
30 #include "transaction/rs_marshalling_helper.h"
31 #include "transaction/rs_transaction.h"
32 #include "ui/rs_display_node.h"
33 #include "ui/rs_root_node.h"
34 #include "ui/rs_surface_node.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(SK_ColorWHITE);
52 
53     rsUiDirector->SetRoot(rootNode->GetId());
54     canvasNode = RSCanvasNode::Create();
55     canvasNode->SetFrame(0, 0, 600, 800);
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_STATUS_BAR);
93     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
94     option->SetWindowRect({ 20, 40, 680, 1500 });
95     string demoName = "pixelmap_demo";
96     RSSystemProperties::GetUniRenderEnabled();
97     auto window = Window::Create(demoName, option);
98 
99     window->Show();
100     auto rect = window->GetRect();
101     while (rect.width_ == 0 && rect.height_ == 0) {
102         cout << "rs demo create window failed: " << rect.width_ << " " << rect.height_ << endl;
103         window->Hide();
104         window->Destroy();
105         window = Window::Create(demoName, option);
106         window->Show();
107         rect = window->GetRect();
108     }
109     cout << "rs demo create window success: " << rect.width_ << " " << rect.height_ << endl;
110     auto surfaceNode = window->GetSurfaceNode();
111 
112     auto rsUiDirector = RSUIDirector::Create();
113     rsUiDirector->Init();
114     RSTransaction::FlushImplicitTransaction();
115     sleep(1);
116 
117     cout << "rs demo unirender enable : " << RSSystemProperties::GetUniRenderEnabled() << endl;
118     cout << "rs pixelmap demo stage 1: init" << endl;
119     rsUiDirector->SetRSSurfaceNode(surfaceNode);
120     Init(rsUiDirector, rect.width_, rect.height_);
121     rsUiDirector->SendMessages();
122     sleep(1);
123 
124     cout << "rs pixelmap demo stage 2: decode pixelmap" << endl;
125     auto allocatorType = Media::AllocatorType::SHARE_MEM_ALLOC;
126     shared_ptr<Media::PixelMap> pixelmap = DecodePixelMap("/data/local/tmp/test.jpg", allocatorType);
127     shared_ptr<Media::PixelMap> bgpixelmap = DecodePixelMap("/data/local/tmp/test_bg.jpg", allocatorType);
128     if (pixelmap == nullptr || bgpixelmap == nullptr) {
129         return -1;
130     }
131 
132     cout << "rs pixelmap demo stage 3: canvas draw" << endl;
133     SkPaint paint;
134     auto canvas = static_cast<RSRecordingCanvas*>(canvasNode->BeginRecording(600, 1200));
135     cout << "DrawPixelMap" << endl;
136     canvas->DrawPixelMap(pixelmap, 100, 200);
137     cout << "DrawPixelMapRect" << endl;
138     canvas->DrawPixelMapRect(pixelmap, SkRect::MakeXYWH(10, 10, 200, 200), SkRect::MakeXYWH(20, 300, 400, 600), &paint);
139 
140     SkVector radii_[4] = { { 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 10 } };
141     RsImageInfo rsImageInfo(5, 1, radii_, 0, 0, 0, 0);
142     cout << "DrawPixelMapWithParm" << endl;
143     canvas->DrawPixelMapWithParm(pixelmap, rsImageInfo, paint);
144     cout << "FinishRecording" << endl;
145     canvasNode->FinishRecording();
146     cout << "SendMessages" << endl;
147     rsUiDirector->SendMessages();
148     sleep(2);
149 
150     cout << "rs pixelmap demo stage 4: bgImage" << endl;
151     canvasNode->SetBgImageWidth(500);
152     canvasNode->SetBgImageHeight(600);
153     canvasNode->SetBgImagePositionX(10);
154     canvasNode->SetBgImagePositionY(10);
155 
156     auto rosenImage = make_shared<Rosen::RSImage>();
157     rosenImage->SetPixelMap(bgpixelmap);
158     rosenImage->SetImageRepeat(2);
159     cout << "SetBgImage" << endl;
160     canvasNode->SetBgImage(rosenImage);
161 
162     rsUiDirector->SendMessages();
163     sleep(200);
164 
165     cout << "rs pixelmap demo end!" << endl;
166     window->Hide();
167     window->Destroy();
168     return 0;
169 }
170