1 /*
2 * Copyright (c) 2022-2023 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, Hardware
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
18 #include "draw/brush.h"
19 #include "effect/color_space.h"
20 #include "drawing/engine_adapter/impl_factory.h"
21 #include "image/bitmap.h"
22 #include "image/gpu_context.h"
23 #include "image/image.h"
24 #include "image/picture.h"
25 #include "utils/matrix.h"
26 #include "utils/size.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace Rosen {
33 namespace Drawing {
34 class ImageTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40 };
41
SetUpTestCase()42 void ImageTest::SetUpTestCase() {}
TearDownTestCase()43 void ImageTest::TearDownTestCase() {}
SetUp()44 void ImageTest::SetUp() {}
TearDown()45 void ImageTest::TearDown() {}
46
47 /**
48 * @tc.name: CreateAndDestroy001
49 * @tc.desc:
50 * @tc.type: FUNC
51 * @tc.require:AR000GGNV3
52 * @tc.author:
53 */
54 HWTEST_F(ImageTest, CreateAndDestroy001, TestSize.Level1)
55 {
56 // The best way to create Image.
57 std::unique_ptr<Image> image = std::make_unique<Image>();
58 ASSERT_TRUE(image != nullptr);
59 }
60
61 /**
62 * @tc.name: BuildFromBitmap001
63 * @tc.desc:
64 * @tc.type: FUNC
65 * @tc.require:AR000GGNV3
66 * @tc.author:
67 */
68 HWTEST_F(ImageTest, BuildFromBitmap001, TestSize.Level1)
69 {
70 std::unique_ptr<Image> image = std::make_unique<Image>();
71 ASSERT_TRUE(image != nullptr);
72 Bitmap bitmap;
73 image->BuildFromBitmap(bitmap);
74 }
75
76 /**
77 * @tc.name: BuildFromPicture001
78 * @tc.desc: test for creating Image from Picture.
79 * @tc.type: FUNC
80 * @tc.require: I70OWN
81 */
82 HWTEST_F(ImageTest, BuildFromPicture001, TestSize.Level1)
83 {
84 std::unique_ptr<Image> image = std::make_unique<Image>();
85 ASSERT_TRUE(image != nullptr);
86 Picture picture;
87 SizeI dimensions;
88 Matrix matrix;
89 Brush brush;
90 BitDepth bitDepth = BitDepth::KU8;
91 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
92 image->BuildFromPicture(picture, dimensions, matrix, brush, bitDepth, colorSpace);
93 }
94
95 /**
96 * @tc.name: ImageGetWidthTest001
97 * @tc.desc:
98 * @tc.type: FUNC
99 * @tc.require:AR000GGNV3
100 * @tc.author:
101 */
102 HWTEST_F(ImageTest, ImageGetWidthTest001, TestSize.Level1)
103 {
104 Bitmap bitmap;
105 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
106 bitmap.Build(10, 10, bitmapFormat);
107 Image image;
108 ASSERT_EQ(0, image.GetWidth());
109 image.BuildFromBitmap(bitmap);
110 ASSERT_EQ(10, image.GetWidth());
111 }
112
113 /**
114 * @tc.name: ImageGetWidthTest002
115 * @tc.desc:
116 * @tc.type: FUNC
117 * @tc.require:AR000GGNV3
118 * @tc.author:
119 */
120 HWTEST_F(ImageTest, ImageGetWidthTest002, TestSize.Level1)
121 {
122 Bitmap bitmap;
123 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
124 bitmap.Build(15, 15, bitmapFormat);
125 Image image;
126 ASSERT_EQ(0, image.GetWidth());
127 image.BuildFromBitmap(bitmap);
128 ASSERT_EQ(15, image.GetWidth());
129 }
130
131 /**
132 * @tc.name: ImageGetHeightTest001
133 * @tc.desc:
134 * @tc.type: FUNC
135 * @tc.require:AR000GGNV3
136 * @tc.author:
137 */
138 HWTEST_F(ImageTest, ImageGetHeightTest001, TestSize.Level1)
139 {
140 Bitmap bitmap;
141 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
142 bitmap.Build(10, 10, bitmapFormat);
143 Image image;
144 image.BuildFromBitmap(bitmap);
145 ASSERT_EQ(10, image.GetHeight());
146 }
147
148 /**
149 * @tc.name: ImageGetHeightTest002
150 * @tc.desc:
151 * @tc.type: FUNC
152 * @tc.require:AR000GGNV3
153 * @tc.author:
154 */
155 HWTEST_F(ImageTest, ImageGetHeightTest002, TestSize.Level1)
156 {
157 Bitmap bitmap;
158 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
159 bitmap.Build(15, 15, bitmapFormat);
160 Image image;
161 image.BuildFromBitmap(bitmap);
162 ASSERT_EQ(15, image.GetHeight());
163 }
164
165 /**
166 * @tc.name: ImageGetUniqueIDTest001
167 * @tc.desc: test for geting the unique Id of Image.
168 * @tc.type: FUNC
169 * @tc.require: I70OWN
170 */
171 HWTEST_F(ImageTest, ImageGetUniqueIDTest001, TestSize.Level1)
172 {
173 Bitmap bitmap;
174 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
175 bitmap.Build(15, 15, bitmapFormat);
176 Image image;
177 ASSERT_EQ(0, image.GetUniqueID());
178 image.BuildFromBitmap(bitmap);
179 ASSERT_EQ(10, image.GetUniqueID());
180 }
181
182 /**
183 * @tc.name: ReadPixelsTest001
184 * @tc.desc: test for Coping a Rect of pixels from Image to Bitmap.
185 * @tc.type: FUNC
186 * @tc.require: I70OWN
187 */
188 HWTEST_F(ImageTest, ReadPixelsTest001, TestSize.Level1)
189 {
190 Bitmap bitmap;
191 Image image;
192 int x = 0;
193 int y = 0;
194 EXPECT_FALSE(image.ReadPixels(bitmap, x, y));
195 }
196
197 /**
198 * @tc.name: ReadPixelsTest002
199 * @tc.desc: test for Coping a Rect of pixels from Image to Bitmap.
200 * @tc.type: FUNC
201 * @tc.require: I70OWN
202 */
203 HWTEST_F(ImageTest, ReadPixelsTest002, TestSize.Level1)
204 {
205 Bitmap bitmap;
206 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
207 bitmap.Build(15, 15, bitmapFormat);
208 Image image;
209 image.BuildFromBitmap(bitmap);
210 int x = 0;
211 int y = 0;
212 EXPECT_TRUE(image.ReadPixels(bitmap, x, y));
213 }
214
215 /**
216 * @tc.name: IsTextureBackedTest001
217 * @tc.desc: test for IsTextureBacked function.
218 * @tc.type: FUNC
219 * @tc.require: I70OWN
220 */
221 HWTEST_F(ImageTest, IsTextureBackedTest001, TestSize.Level1)
222 {
223 Bitmap bitmap;
224 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
225 bitmap.Build(15, 15, bitmapFormat);
226 Image image;
227 image.BuildFromBitmap(bitmap);
228 EXPECT_FALSE(image.IsTextureBacked());
229 }
230
231 #ifdef ACE_ENABLE_GPU
232 /**
233 * @tc.name: BuildFromCompressedTest001
234 * @tc.desc: test for creating a GPU-backed Image from compressed data.
235 * @tc.type: FUNC
236 * @tc.require: I70OWN
237 */
238 HWTEST_F(ImageTest, BuildFromCompressedTest001, TestSize.Level1)
239 {
240 GPUContext gpuContext;
241 const std::shared_ptr<Data> data = nullptr;
242 std::unique_ptr<Image> image = std::make_unique<Image>();
243 ASSERT_TRUE(image != nullptr);
244 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ASTC);
245 }
246
247 /**
248 * @tc.name: BuildFromCompressedTest002
249 * @tc.desc: test for creating a GPU-backed Image from compressed data.
250 * @tc.type: FUNC
251 * @tc.require: I70OWN
252 */
253 HWTEST_F(ImageTest, BuildFromCompressedTest002, TestSize.Level1)
254 {
255 GPUContext gpuContext;
256 std::shared_ptr<Data> data = std::make_shared<Data>();
257 ASSERT_TRUE(data != nullptr);
258 std::unique_ptr<Image> image = std::make_unique<Image>();
259 ASSERT_TRUE(image != nullptr);
260 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ASTC);
261 }
262
263 /**
264 * @tc.name: BuildFromCompressedTest003
265 * @tc.desc: test for creating a GPU-backed Image from compressed data.
266 * @tc.type: FUNC
267 * @tc.require: I70OWN
268 */
269 HWTEST_F(ImageTest, BuildFromCompressedTest003, TestSize.Level1)
270 {
271 GPUContext gpuContext;
272 std::shared_ptr<Data> data = std::make_shared<Data>();
273 ASSERT_TRUE(data != nullptr);
274 std::unique_ptr<Image> image = std::make_unique<Image>();
275 ASSERT_TRUE(image != nullptr);
276 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ETC1);
277 }
278
279 /**
280 * @tc.name: BuildFromBitmapTest001
281 * @tc.desc: test for creating Image from Bitmap. Image is uploaded to GPU back-end using context.
282 * @tc.type: FUNC
283 * @tc.require: I70OWN
284 */
285 HWTEST_F(ImageTest, BuildFromBitmapTest001, TestSize.Level1)
286 {
287 GPUContext gpuContext;
288 Bitmap bitmap;
289 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
290 bitmap.Build(15, 15, bitmapFormat);
291 std::unique_ptr<Image> image = std::make_unique<Image>();
292 ASSERT_TRUE(image != nullptr);
293 image->BuildFromBitmap(gpuContext, bitmap);
294 }
295
296 /**
297 * @tc.name: BuildFromTextureTest001
298 * @tc.desc: test for creating Image from GPU texture associated with context.
299 * @tc.type: FUNC
300 * @tc.require: I70OWN
301 */
302 HWTEST_F(ImageTest, BuildFromTextureTest001, TestSize.Level1)
303 {
304 GPUContext gpuContext;
305 TextureInfo info;
306 info.SetWidth(10);
307 BitmapFormat bitmapFormat { COLORTYPE_UNKNOWN, ALPHATYPE_UNKNOWN };
308 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
309 std::unique_ptr<Image> image = std::make_unique<Image>();
310 ASSERT_TRUE(image != nullptr);
311 image->BuildFromTexture(gpuContext, info, TextureOrigin::TOP_LEFT, bitmapFormat, nullptr);
312
313 bitmapFormat = { COLORTYPE_ALPHA_8, ALPHATYPE_OPAQUE };
314 image->BuildFromTexture(gpuContext, info, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace);
315
316 bitmapFormat = { COLORTYPE_RGB_565, ALPHATYPE_PREMUL };
317 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
318
319 bitmapFormat = { COLORTYPE_ARGB_4444, ALPHATYPE_UNPREMUL };
320 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
321
322 bitmapFormat.colorType = COLORTYPE_RGBA_8888;
323 bitmapFormat.alphaType = static_cast<AlphaType>(-1);
324 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
325
326 bitmapFormat.colorType = COLORTYPE_BGRA_8888;
327 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
328
329 bitmapFormat.colorType = COLORTYPE_N32;
330 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
331
332 bitmapFormat.colorType = static_cast<ColorType>(-1);
333 image->BuildFromTexture(gpuContext, info, static_cast<TextureOrigin>(-1), bitmapFormat, colorSpace);
334 }
335
336 /**
337 * @tc.name: SerializeAndDeserializeTest001
338 * @tc.desc: test for serialize and deserialize Image.
339 * @tc.type: FUNC
340 * @tc.require: I782P9
341 */
342 HWTEST_F(ImageTest, SerializeAndDeserializeTest001, TestSize.Level1)
343 {
344 auto image = std::make_shared<Image>();
345 auto data = image->Serialize();
346 #ifdef ROSEN_OHOS
347 ASSERT_TRUE(data == nullptr);
348 EXPECT_FALSE(image->Deserialize(data));
349
350 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
351 auto bitmap = std::make_shared<Bitmap>();
352 bitmap->Build(10, 10, bitmapFormat);
353 image->BuildFromBitmap(*bitmap);
354 data = image->Serialize();
355 ASSERT_TRUE(data != nullptr);
356 EXPECT_TRUE(image->Deserialize(data));
357 #else
358 ASSERT_TRUE(data == nullptr);
359 EXPECT_FALSE(image->Deserialize(data));
360 #endif
361 }
362 #endif
363 } // namespace Drawing
364 } // namespace Rosen
365 } // namespace OHOS