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 "gtest/gtest.h"
17 #include "include/core/SkSurface.h"
18 #include "message_parcel.h"
19 #include "render/rs_image.h"
20 #include "render/rs_image_cache.h"
21 #include "pixel_map.h"
22 #include "transaction/rs_marshalling_helper.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS::Rosen {
28 class RSImageTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void RSImageTest::SetUpTestCase() {}
TearDownTestCase()37 void RSImageTest::TearDownTestCase() {}
SetUp()38 void RSImageTest::SetUp() {}
TearDown()39 void RSImageTest::TearDown() {}
40
41 /**
42 * @tc.name: IsEqual001
43 * @tc.desc:
44 * @tc.type:FUNC
45 */
46 HWTEST_F(RSImageTest, IsEqual001, TestSize.Level1)
47 {
48 /**
49 * @tc.steps: step1. create RSMask by Gradient
50 */
51 RSImage rsImage;
52 int res = rsImage.IsEqual(rsImage);
53 ASSERT_TRUE(res);
54 RSImage other;
55 other.SetScale(2.f);
56 res = rsImage.IsEqual(other);
57 ASSERT_FALSE(res);
58 }
59
CreatePixelMap(int width,int height)60 static std::shared_ptr<Media::PixelMap> CreatePixelMap(int width, int height)
61 {
62 Media::InitializationOptions opts;
63 opts.size.width = width;
64 opts.size.height = height;
65 auto pixelmap = Media::PixelMap::Create(opts);
66 auto address = const_cast<uint32_t*>(pixelmap->GetPixel32(0, 0));
67 if (address == nullptr) {
68 return nullptr;
69 }
70 SkImageInfo info =
71 SkImageInfo::Make(pixelmap->GetWidth(), pixelmap->GetHeight(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
72 auto surface = SkSurface::MakeRasterDirect(info, address, pixelmap->GetRowBytes());
73 auto canvas = surface->getCanvas();
74 canvas->clear(SK_ColorYELLOW);
75 SkPaint paint;
76 paint.setColor(SK_ColorRED);
77 canvas->drawRect(SkRect::MakeXYWH(width / 4, height / 4, width / 2, height / 2), paint);
78 return pixelmap;
79 }
80
81 /**
82 * @tc.name: LifeCycle001
83 * @tc.desc:
84 * @tc.type:FUNC
85 */
86 HWTEST_F(RSImageTest, LifeCycle001, TestSize.Level1)
87 {
88 /**
89 * @tc.steps: step1. create RSMask by Gradient
90 */
91 RSImage rsImage;
92 SkCanvas canvas;
93 float fLeft = 1.0f;
94 float ftop = 1.0f;
95 float fRight = 1.0f;
96 float fBottom = 1.0f;
97 rsImage.SetImageFit(0);
98 SkRect rect { fLeft, ftop, fRight, fBottom };
99 SkPaint paint;
100 std::shared_ptr<Media::PixelMap> pixelmap;
101 rsImage.SetPixelMap(pixelmap);
102 int width = 200;
103 int height = 300;
104 pixelmap = CreatePixelMap(width, height);
105 rsImage.SetPixelMap(pixelmap);
106 #ifdef NEW_SKIA
107 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, false);
108 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
109 rsImage.SetImageFit(1);
110 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
111 rsImage.SetImageFit(2);
112 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
113 rsImage.SetImageFit(3);
114 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
115 rsImage.SetImageFit(4);
116 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
117 rsImage.SetImageFit(5);
118 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
119 rsImage.SetImageFit(6);
120 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
121 rsImage.SetImageFit(0);
122 rsImage.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, true);
123 #else
124 rsImage.CanvasDrawImage(canvas, rect, paint, false);
125 rsImage.CanvasDrawImage(canvas, rect, paint, true);
126 rsImage.SetImageFit(1);
127 rsImage.CanvasDrawImage(canvas, rect, paint, true);
128 rsImage.SetImageFit(2);
129 rsImage.CanvasDrawImage(canvas, rect, paint, true);
130 rsImage.SetImageFit(3);
131 rsImage.CanvasDrawImage(canvas, rect, paint, true);
132 rsImage.SetImageFit(4);
133 rsImage.CanvasDrawImage(canvas, rect, paint, true);
134 rsImage.SetImageFit(5);
135 rsImage.CanvasDrawImage(canvas, rect, paint, true);
136 rsImage.SetImageFit(6);
137 rsImage.CanvasDrawImage(canvas, rect, paint, true);
138 rsImage.SetImageFit(0);
139 rsImage.CanvasDrawImage(canvas, rect, paint, true);
140 #endif
141 }
142
143 /**
144 * @tc.name: LifeCycle002
145 * @tc.desc:
146 * @tc.type:FUNC
147 */
148 HWTEST_F(RSImageTest, LifeCycle002, TestSize.Level1)
149 {
150 /**
151 * @tc.steps: step1. create RSMask by Gradient
152 */
153 Parcel parcel;
154 ASSERT_TRUE(RSImage::Unmarshalling(parcel) == nullptr);
155 parcel.WriteInt32(1);
156 ASSERT_TRUE(RSImage::Unmarshalling(parcel) == nullptr);
157 }
158
159 /**
160 * @tc.name: TestRSImage001
161 * @tc.desc: IsEqual test.
162 * @tc.type: FUNC
163 */
164 HWTEST_F(RSImageTest, TestRSImage001, TestSize.Level1)
165 {
166 RSImage image;
167 RSImage other;
168 image.IsEqual(other);
169 }
170
171 /**
172 * @tc.name: TestRSImage002
173 * @tc.desc: CanvasDrawImage test.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(RSImageTest, TestRSImage002, TestSize.Level1)
177 {
178 RSImage image;
179 SkCanvas canvas;
180 SkRect rect;
181 SkPaint paint;
182 bool isBackground = false;
183 #ifdef NEW_SKIA
184 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
185 isBackground = true;
186 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
187 #else
188 image.CanvasDrawImage(canvas, rect, paint, isBackground);
189 isBackground = true;
190 image.CanvasDrawImage(canvas, rect, paint, isBackground);
191 #endif
192 }
193
194 /**
195 * @tc.name: TestRSImage003
196 * @tc.desc: ApplyImageFit test.
197 * @tc.type: FUNC
198 */
199 HWTEST_F(RSImageTest, TestRSImage003, TestSize.Level1)
200 {
201 RSImage image;
202 SkCanvas canvas;
203 SkRect rect;
204 SkPaint paint;
205 bool isBackground = false;
206 image.SetImageFit(0);
207 #ifdef NEW_SKIA
208 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
209 image.SetImageFit(5);
210 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
211 image.SetImageFit(2);
212 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
213 image.SetImageFit(3);
214 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
215 image.SetImageFit(4);
216 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
217 image.SetImageFit(6);
218 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
219 image.SetImageFit(1);
220 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
221 image.SetImageFit(7);
222 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint, isBackground);
223 #else
224 image.CanvasDrawImage(canvas, rect, paint, isBackground);
225 image.SetImageFit(5);
226 image.CanvasDrawImage(canvas, rect, paint, isBackground);
227 image.SetImageFit(2);
228 image.CanvasDrawImage(canvas, rect, paint, isBackground);
229 image.SetImageFit(3);
230 image.CanvasDrawImage(canvas, rect, paint, isBackground);
231 image.SetImageFit(4);
232 image.CanvasDrawImage(canvas, rect, paint, isBackground);
233 image.SetImageFit(6);
234 image.CanvasDrawImage(canvas, rect, paint, isBackground);
235 image.SetImageFit(1);
236 image.CanvasDrawImage(canvas, rect, paint, isBackground);
237 image.SetImageFit(7);
238 image.CanvasDrawImage(canvas, rect, paint, isBackground);
239 #endif
240 }
241
242 /**
243 * @tc.name: TestRSImage004
244 * @tc.desc: SetImageRepeat test.
245 * @tc.type: FUNC
246 */
247 HWTEST_F(RSImageTest, TestRSImage004, TestSize.Level1)
248 {
249 RSImage image;
250 SkCanvas canvas;
251 SkRect rect = SkRect::MakeWH(100, 100);
252 RectF dstRect(10, 10, 80, 80);
253 SkPaint paint;
254 image.SetDstRect(dstRect);
255 image.SetImageRepeat(1);
256 #ifdef NEW_SKIA
257 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint);
258 image.SetImageRepeat(2);
259 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint);
260 image.SetImageRepeat(3);
261 image.CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint);
262 #else
263 image.CanvasDrawImage(canvas, rect, paint);
264 image.SetImageRepeat(2);
265 image.CanvasDrawImage(canvas, rect, paint);
266 image.SetImageRepeat(3);
267 image.CanvasDrawImage(canvas, rect, paint);
268 #endif
269 }
270
271 /**
272 * @tc.name: RSImageBase001
273 * @tc.desc: RSImageBase test.
274 * @tc.type: FUNC
275 */
276 HWTEST_F(RSImageTest, RSImageBase001, TestSize.Level1)
277 {
278 RSImageBase imageBase;
279 RectF rect(0, 0, 100, 100);
280 imageBase.SetSrcRect(rect);
281 imageBase.SetDstRect(rect);
282 SkCanvas canvas;
283 SkPaint paint;
284 #ifdef NEW_SKIA
285 SkSamplingOptions samplingOptions = SkSamplingOptions();
286 imageBase.DrawImage(canvas, samplingOptions, paint);
287 #else
288 imageBase.DrawImage(canvas, paint);
289 #endif
290 }
291
292 /**
293 * @tc.name: RSImageCache001
294 * @tc.desc: RSImageBase test.
295 * @tc.type: FUNC
296 */
297 HWTEST_F(RSImageTest, RSImageCache001, TestSize.Level1)
298 {
299 auto rsImage = std::make_shared<RSImage>();
300 std::shared_ptr<Media::PixelMap> pixelMap;
301 int width = 200;
302 int height = 300;
303 pixelMap = CreatePixelMap(width, height);
304 rsImage->SetPixelMap(pixelMap);
305
306 MessageParcel parcel;
307 EXPECT_EQ(RSMarshallingHelper::Marshalling(parcel, rsImage), true);
308 std::shared_ptr<RSImage> newImage;
309 EXPECT_EQ(RSMarshallingHelper::Unmarshalling(parcel, newImage), true);
310
311 SkCanvas canvas;
312 SkPaint paint;
313 SkRect rect = SkRect::MakeWH(100, 100);
314 #ifdef NEW_SKIA
315 newImage->CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint);
316 newImage->CanvasDrawImage(canvas, rect, SkSamplingOptions(), paint);
317 #else
318 newImage->CanvasDrawImage(canvas, rect, paint);
319 newImage->CanvasDrawImage(canvas, rect, paint);
320 #endif
321
322 MessageParcel parcel2;
323 EXPECT_EQ(RSMarshallingHelper::Marshalling(parcel2, rsImage), true);
324 std::shared_ptr<RSImage> newImage2;
325 EXPECT_EQ(RSMarshallingHelper::Unmarshalling(parcel2, newImage2), true);
326
327 RSImageCache::Instance().CachePixelMap(1, nullptr);
328 RSImageCache::Instance().CachePixelMap(0, pixelMap);
329 RSImageCache::Instance().CacheRenderSkiaImageByPixelMapId(1, nullptr);
330 RSImageCache::Instance().CacheRenderSkiaImageByPixelMapId(0, nullptr);
331 }
332 } // namespace OHOS::Rosen
333