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: ImageGetWidthTest001
78 * @tc.desc:
79 * @tc.type: FUNC
80 * @tc.require:AR000GGNV3
81 * @tc.author:
82 */
83 HWTEST_F(ImageTest, ImageGetWidthTest001, TestSize.Level1)
84 {
85 Bitmap bitmap;
86 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
87 bitmap.Build(10, 10, bitmapFormat);
88 Image image;
89 ASSERT_EQ(0, image.GetWidth());
90 image.BuildFromBitmap(bitmap);
91 ASSERT_EQ(10, image.GetWidth());
92 }
93
94 /**
95 * @tc.name: ImageGetWidthTest002
96 * @tc.desc:
97 * @tc.type: FUNC
98 * @tc.require:AR000GGNV3
99 * @tc.author:
100 */
101 HWTEST_F(ImageTest, ImageGetWidthTest002, TestSize.Level1)
102 {
103 Bitmap bitmap;
104 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
105 bitmap.Build(15, 15, bitmapFormat);
106 Image image;
107 ASSERT_EQ(0, image.GetWidth());
108 image.BuildFromBitmap(bitmap);
109 ASSERT_EQ(15, image.GetWidth());
110 }
111
112 /**
113 * @tc.name: ImageGetHeightTest001
114 * @tc.desc:
115 * @tc.type: FUNC
116 * @tc.require:AR000GGNV3
117 * @tc.author:
118 */
119 HWTEST_F(ImageTest, ImageGetHeightTest001, TestSize.Level1)
120 {
121 Bitmap bitmap;
122 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
123 bitmap.Build(10, 10, bitmapFormat);
124 Image image;
125 image.BuildFromBitmap(bitmap);
126 ASSERT_EQ(10, image.GetHeight());
127 }
128
129 /**
130 * @tc.name: ImageGetHeightTest002
131 * @tc.desc:
132 * @tc.type: FUNC
133 * @tc.require:AR000GGNV3
134 * @tc.author:
135 */
136 HWTEST_F(ImageTest, ImageGetHeightTest002, TestSize.Level1)
137 {
138 Bitmap bitmap;
139 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
140 bitmap.Build(15, 15, bitmapFormat);
141 Image image;
142 image.BuildFromBitmap(bitmap);
143 ASSERT_EQ(15, image.GetHeight());
144 }
145
146 /**
147 * @tc.name: ImageGetUniqueIDTest001
148 * @tc.desc: test for geting the unique Id of Image.
149 * @tc.type: FUNC
150 * @tc.require: I70OWN
151 */
152 HWTEST_F(ImageTest, ImageGetUniqueIDTest001, TestSize.Level1)
153 {
154 Bitmap bitmap;
155 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
156 bitmap.Build(15, 15, bitmapFormat);
157 Image image;
158 ASSERT_EQ(0, image.GetUniqueID());
159 image.BuildFromBitmap(bitmap);
160 ASSERT_EQ(10, image.GetUniqueID());
161 }
162
163 /**
164 * @tc.name: ReadPixelsTest001
165 * @tc.desc: test for Coping a Rect of pixels from Image to Bitmap.
166 * @tc.type: FUNC
167 * @tc.require: I70OWN
168 */
169 HWTEST_F(ImageTest, ReadPixelsTest001, TestSize.Level1)
170 {
171 Bitmap bitmap;
172 Image image;
173 int x = 0;
174 int y = 0;
175 EXPECT_FALSE(image.ReadPixels(bitmap, x, y));
176 }
177
178 /**
179 * @tc.name: ReadPixelsTest002
180 * @tc.desc: test for Coping a Rect of pixels from Image to Bitmap.
181 * @tc.type: FUNC
182 * @tc.require: I70OWN
183 */
184 HWTEST_F(ImageTest, ReadPixelsTest002, TestSize.Level1)
185 {
186 Bitmap bitmap;
187 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
188 bitmap.Build(15, 15, bitmapFormat);
189 Image image;
190 image.BuildFromBitmap(bitmap);
191 int x = 0;
192 int y = 0;
193 EXPECT_TRUE(image.ReadPixels(bitmap, x, y));
194 }
195
196 /**
197 * @tc.name: IsTextureBackedTest001
198 * @tc.desc: test for IsTextureBacked function.
199 * @tc.type: FUNC
200 * @tc.require: I70OWN
201 */
202 HWTEST_F(ImageTest, IsTextureBackedTest001, TestSize.Level1)
203 {
204 Bitmap bitmap;
205 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
206 bitmap.Build(15, 15, bitmapFormat);
207 Image image;
208 image.BuildFromBitmap(bitmap);
209 EXPECT_FALSE(image.IsTextureBacked());
210 }
211
212 #ifdef ACE_ENABLE_GPU
213 /**
214 * @tc.name: BuildFromCompressedTest001
215 * @tc.desc: test for creating a GPU-backed Image from compressed data.
216 * @tc.type: FUNC
217 * @tc.require: I70OWN
218 */
219 HWTEST_F(ImageTest, BuildFromCompressedTest001, TestSize.Level1)
220 {
221 GPUContext gpuContext;
222 const std::shared_ptr<Data> data = nullptr;
223 std::unique_ptr<Image> image = std::make_unique<Image>();
224 ASSERT_TRUE(image != nullptr);
225 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ASTC_RGBA8_4x4);
226 }
227
228 /**
229 * @tc.name: BuildFromCompressedTest002
230 * @tc.desc: test for creating a GPU-backed Image from compressed data.
231 * @tc.type: FUNC
232 * @tc.require: I70OWN
233 */
234 HWTEST_F(ImageTest, BuildFromCompressedTest002, TestSize.Level1)
235 {
236 GPUContext gpuContext;
237 std::shared_ptr<Data> data = std::make_shared<Data>();
238 ASSERT_TRUE(data != nullptr);
239 std::unique_ptr<Image> image = std::make_unique<Image>();
240 ASSERT_TRUE(image != nullptr);
241 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ASTC_RGBA8_4x4);
242 }
243
244 /**
245 * @tc.name: BuildFromCompressedTest003
246 * @tc.desc: test for creating a GPU-backed Image from compressed data.
247 * @tc.type: FUNC
248 * @tc.require: I70OWN
249 */
250 HWTEST_F(ImageTest, BuildFromCompressedTest003, TestSize.Level1)
251 {
252 GPUContext gpuContext;
253 std::shared_ptr<Data> data = std::make_shared<Data>();
254 ASSERT_TRUE(data != nullptr);
255 std::unique_ptr<Image> image = std::make_unique<Image>();
256 ASSERT_TRUE(image != nullptr);
257 image->BuildFromCompressed(gpuContext, data, 15, 15, CompressedType::ETC2_RGB8_UNORM);
258 }
259
260 /**
261 * @tc.name: BuildFromBitmapTest001
262 * @tc.desc: test for creating Image from Bitmap. Image is uploaded to GPU back-end using context.
263 * @tc.type: FUNC
264 * @tc.require: I70OWN
265 */
266 HWTEST_F(ImageTest, BuildFromBitmapTest001, TestSize.Level1)
267 {
268 GPUContext gpuContext;
269 Bitmap bitmap;
270 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
271 bitmap.Build(15, 15, bitmapFormat);
272 std::unique_ptr<Image> image = std::make_unique<Image>();
273 ASSERT_TRUE(image != nullptr);
274 image->BuildFromBitmap(gpuContext, bitmap);
275 }
276
277 /**
278 * @tc.name: BuildFromTextureTest001
279 * @tc.desc: test for creating Image from GPU texture associated with context.
280 * @tc.type: FUNC
281 * @tc.require: I70OWN
282 */
283 HWTEST_F(ImageTest, BuildFromTextureTest001, TestSize.Level1)
284 {
285 GPUContext gpuContext;
286 TextureInfo info;
287 info.SetWidth(10);
288 BitmapFormat bitmapFormat { COLORTYPE_UNKNOWN, ALPHATYPE_UNKNOWN };
289 auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::NO_TYPE);
290 std::unique_ptr<Image> image = std::make_unique<Image>();
291 ASSERT_TRUE(image != nullptr);
292 image->BuildFromTexture(gpuContext, info, TextureOrigin::TOP_LEFT, bitmapFormat, nullptr);
293
294 bitmapFormat = { COLORTYPE_ALPHA_8, ALPHATYPE_OPAQUE };
295 image->BuildFromTexture(gpuContext, info, TextureOrigin::TOP_LEFT, bitmapFormat, colorSpace);
296
297 bitmapFormat = { COLORTYPE_RGB_565, ALPHATYPE_PREMUL };
298 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
299
300 bitmapFormat = { COLORTYPE_ARGB_4444, ALPHATYPE_UNPREMUL };
301 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
302
303 bitmapFormat.colorType = COLORTYPE_RGBA_8888;
304 bitmapFormat.alphaType = static_cast<AlphaType>(-1);
305 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
306
307 bitmapFormat.colorType = COLORTYPE_BGRA_8888;
308 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
309
310 bitmapFormat.colorType = COLORTYPE_N32;
311 image->BuildFromTexture(gpuContext, info, TextureOrigin::BOTTOM_LEFT, bitmapFormat, colorSpace);
312
313 bitmapFormat.colorType = static_cast<ColorType>(-1);
314 image->BuildFromTexture(gpuContext, info, static_cast<TextureOrigin>(-1), bitmapFormat, colorSpace);
315 }
316
317 /**
318 * @tc.name: SerializeAndDeserializeTest001
319 * @tc.desc: test for serialize and deserialize Image.
320 * @tc.type: FUNC
321 * @tc.require: I782P9
322 */
323 HWTEST_F(ImageTest, SerializeAndDeserializeTest001, TestSize.Level1)
324 {
325 auto image = std::make_shared<Image>();
326 auto data = image->Serialize();
327 #ifdef ROSEN_OHOS
328 ASSERT_TRUE(data == nullptr);
329 EXPECT_FALSE(image->Deserialize(data));
330
331 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
332 auto bitmap = std::make_shared<Bitmap>();
333 bitmap->Build(10, 10, bitmapFormat);
334 image->BuildFromBitmap(*bitmap);
335 data = image->Serialize();
336 ASSERT_TRUE(data != nullptr);
337 EXPECT_TRUE(image->Deserialize(data));
338 #else
339 ASSERT_TRUE(data == nullptr);
340 EXPECT_FALSE(image->Deserialize(data));
341 #endif
342 }
343 #endif
344 } // namespace Drawing
345 } // namespace Rosen
346 } // namespace OHOS