1 /*
2 * Copyright (c) 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, 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 <memory>
17 #include "gtest/gtest.h"
18
19 #include "draw/surface.h"
20 #include "draw/canvas.h"
21 #include "effect/color_space.h"
22 #include "effect/runtime_shader_builder.h"
23 #include "effect/runtime_effect.h"
24 #include "image/gpu_context.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 class CanvasMakeImage : public Canvas {
33 public:
34 explicit CanvasMakeImage(Surface* surface);
35 Drawing::Surface* GetSurface() const override;
36 private:
37 Surface* surface_ = nullptr;
38 };
39
CanvasMakeImage(Surface * surface)40 CanvasMakeImage::CanvasMakeImage(Surface* surface)
41 : surface_(surface)
42 {
43 }
GetSurface() const44 Drawing::Surface* CanvasMakeImage::GetSurface() const
45 {
46 return surface_;
47 }
48
49 class SurfaceTest : public testing::Test {
50 public:
51 static void SetUpTestCase();
52 static void TearDownTestCase();
53 void SetUp() override;
54 void TearDown() override;
55 };
56
SetUpTestCase()57 void SurfaceTest::SetUpTestCase() {}
TearDownTestCase()58 void SurfaceTest::TearDownTestCase() {}
SetUp()59 void SurfaceTest::SetUp() {}
TearDown()60 void SurfaceTest::TearDown() {}
61
62 /**
63 * @tc.name: Surface001
64 * @tc.desc: Test for creating Surface.
65 * @tc.type: FUNC
66 * @tc.require:I774GD
67 */
68 HWTEST_F(SurfaceTest, Surface001, TestSize.Level1)
69 {
70 auto surface = std::make_unique<Surface>();
71 ASSERT_TRUE(surface != nullptr);
72 }
73
74 /**
75 * @tc.name: SurfaceBind001
76 * @tc.desc: Test for binding raster Surface.
77 * @tc.type: FUNC
78 * @tc.require:I774GD
79 */
80 HWTEST_F(SurfaceTest, SurfaceBind001, TestSize.Level1)
81 {
82 auto surface = std::make_unique<Surface>();
83 ASSERT_TRUE(surface != nullptr);
84 Bitmap bitmap;
85 ASSERT_FALSE(surface->Bind(bitmap));
86 }
87
88 /**
89 * @tc.name: SurfaceBind002
90 * @tc.desc: Test for binding raster Surface.
91 * @tc.type: FUNC
92 * @tc.require:I774GD
93 */
94 HWTEST_F(SurfaceTest, SurfaceBind002, TestSize.Level1)
95 {
96 auto surface = std::make_unique<Surface>();
97 ASSERT_TRUE(surface != nullptr);
98 Bitmap bitmap;
99 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
100 bitmap.Build(10, 10, bitmapFormat);
101 ASSERT_TRUE(surface->Bind(bitmap));
102 }
103
104 #ifdef RS_ENABLE_GPU
105 /**
106 * @tc.name: SurfaceBind003
107 * @tc.desc: Test for binding texture Surface.
108 * @tc.type: FUNC
109 * @tc.require:I774GD
110 */
111 HWTEST_F(SurfaceTest, SurfaceBind003, TestSize.Level1)
112 {
113 auto surface = std::make_unique<Surface>();
114 ASSERT_TRUE(surface != nullptr);
115 Image image;
116 ASSERT_FALSE(surface->Bind(image));
117 }
118
119 /**
120 * @tc.name: SurfaceBind004
121 * @tc.desc: Test for binding FrameBuffer Surface.
122 * @tc.type: FUNC
123 * @tc.require:I774GD
124 */
125 HWTEST_F(SurfaceTest, SurfaceBind004, TestSize.Level1)
126 {
127 auto surface = std::make_unique<Surface>();
128 ASSERT_TRUE(surface != nullptr);
129 FrameBuffer info;
130 ASSERT_FALSE(surface->Bind(info));
131
132 info.gpuContext = std::make_shared<GPUContext>();
133 ASSERT_FALSE(surface->Bind(info));
134
135 info.colorSpace = ColorSpace::CreateSRGB();
136 ASSERT_FALSE(surface->Bind(info));
137 }
138 #endif
139
140 /**
141 * @tc.name: GetCanvas001
142 * @tc.desc: Test for geting Canvas that draws into Surface.
143 * @tc.type: FUNC
144 * @tc.require:I774GD
145 */
146 HWTEST_F(SurfaceTest, GetCanvas001, TestSize.Level1)
147 {
148 auto surface = std::make_unique<Surface>();
149 ASSERT_TRUE(surface != nullptr);
150 auto ret = surface->GetCanvas();
151 ASSERT_EQ(ret, nullptr);
152 }
153
154 /**
155 * @tc.name: GetCanvas002
156 * @tc.desc: Test for geting Canvas that draws into Surface.
157 * @tc.type: FUNC
158 * @tc.require:I774GD
159 */
160 HWTEST_F(SurfaceTest, GetCanvas002, TestSize.Level1)
161 {
162 auto surface = std::make_unique<Surface>();
163 ASSERT_TRUE(surface != nullptr);
164 Bitmap bitmap;
165 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
166 bitmap.Build(10, 10, bitmapFormat);
167 ASSERT_TRUE(surface->Bind(bitmap));
168 auto ret = surface->GetCanvas();
169 ASSERT_TRUE(ret != nullptr);
170 }
171
172 /**
173 * @tc.name: GetImageSnapshot001
174 * @tc.desc: Test for geting Image capturing Surface contents.
175 * @tc.type: FUNC
176 * @tc.require:I774GD
177 */
178 HWTEST_F(SurfaceTest, GetImageSnapshot001, TestSize.Level1)
179 {
180 auto surface = std::make_unique<Surface>();
181 ASSERT_TRUE(surface != nullptr);
182 auto ret = surface->GetImageSnapshot();
183 ASSERT_EQ(ret, nullptr);
184 }
185
186 /**
187 * @tc.name: GetImageSnapshot002
188 * @tc.desc: Test for geting Image capturing Surface contents.
189 * @tc.type: FUNC
190 * @tc.require:I774GD
191 */
192 HWTEST_F(SurfaceTest, GetImageSnapshot002, TestSize.Level1)
193 {
194 auto surface = std::make_unique<Surface>();
195 ASSERT_TRUE(surface != nullptr);
196 Bitmap bitmap;
197 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
198 bitmap.Build(10, 10, bitmapFormat);
199 ASSERT_TRUE(surface->Bind(bitmap));
200 auto ret = surface->GetImageSnapshot();
201 ASSERT_TRUE(ret != nullptr);
202 }
203
204 /**
205 * @tc.name: GetImageSnapshot003
206 * @tc.desc: Test for geting Image capturing Surface contents.
207 * @tc.type: FUNC
208 * @tc.require:I774GD
209 */
210 HWTEST_F(SurfaceTest, GetImageSnapshot003, TestSize.Level1)
211 {
212 auto surface = std::make_unique<Surface>();
213 ASSERT_TRUE(surface != nullptr);
214 RectI bounds;
215 auto ret = surface->GetImageSnapshot(bounds);
216 ASSERT_EQ(ret, nullptr);
217 }
218
219 /**
220 * @tc.name: GetImageSnapshot004
221 * @tc.desc: Test for geting Image capturing Surface contents.
222 * @tc.type: FUNC
223 * @tc.require:I774GD
224 */
225 HWTEST_F(SurfaceTest, GetImageSnapshot004, TestSize.Level1)
226 {
227 auto surface = std::make_unique<Surface>();
228 ASSERT_TRUE(surface != nullptr);
229 Bitmap bitmap;
230 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
231 bitmap.Build(10, 10, bitmapFormat);
232 ASSERT_TRUE(surface->Bind(bitmap));
233 RectI bounds(1, 1, 15, 20);
234 auto ret = surface->GetImageSnapshot(bounds);
235 ASSERT_TRUE(ret != nullptr);
236 }
237
238 /**
239 * @tc.name: GetImageSnapshot005
240 * @tc.desc: Test for geting Image capturing Surface contents, when bounds does not intersect the surface.
241 * @tc.type: FUNC
242 * @tc.require:I774GD
243 */
244 HWTEST_F(SurfaceTest, GetImageSnapshot005, TestSize.Level1)
245 {
246 auto surface = std::make_unique<Surface>();
247 ASSERT_TRUE(surface != nullptr);
248 Bitmap bitmap;
249 BitmapFormat bitmapFormat { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
250 bitmap.Build(10, 10, bitmapFormat);
251 ASSERT_TRUE(surface->Bind(bitmap));
252 RectI bounds(70, 75, 100, 100);
253 auto ret = surface->GetImageSnapshot(bounds);
254 ASSERT_TRUE(ret == nullptr);
255 }
256
257 #ifdef RS_ENABLE_GL
258 /**
259 * @tc.name: Wait006
260 * @tc.desc: Test wait for the surface.
261 * @tc.type: FUNC
262 * @tc.require:IALEIN
263 */
264 HWTEST_F(SurfaceTest, Wait006, TestSize.Level1)
265 {
266 auto surface = std::make_unique<Surface>();
267 ASSERT_TRUE(surface != nullptr);
268 std::vector<GrGLsync> syncs;
269 surface->Wait(syncs);
270 }
271 #endif
272
273 #ifdef RS_ENABLE_VK
274 /**
275 * @tc.name: ClearDrawingArea001
276 * @tc.desc: Test wait for the surface.
277 * @tc.type: FUNC
278 * @tc.require:IALEIN
279 */
280 HWTEST_F(SurfaceTest, ClearDrawingArea001, TestSize.Level1)
281 {
282 auto surface = std::make_unique<Surface>();
283 ASSERT_TRUE(surface != nullptr);
284 surface->ClearDrawingArea();
285 }
286 #endif
287
288 /**
289 * @tc.name: GetHeadroom001
290 * @tc.desc: Test for getting headroom from surface.
291 * @tc.type: FUNC
292 * @tc.require:I774GD
293 */
294 HWTEST_F(SurfaceTest, GetHeadroom001, TestSize.Level1)
295 {
296 auto surface = std::make_unique<Surface>();
297 ASSERT_TRUE(surface != nullptr);
298 ASSERT_EQ(surface->GetHeadroom(), 1.0f);
299 }
300
301 /**
302 * @tc.name: SetHeadroom001
303 * @tc.desc: Test for setting headroom to surface.
304 * @tc.type: FUNC
305 * @tc.require:I774GD
306 */
307 HWTEST_F(SurfaceTest, SetHeadroom001, TestSize.Level1)
308 {
309 auto surface = std::make_unique<Surface>();
310 ASSERT_TRUE(surface != nullptr);
311 surface->SetHeadroom(1.5f);
312 ASSERT_EQ(surface->GetHeadroom(), 1.0f);
313 }
314
315 /**
316 * @tc.name: MakeImageWithCanvas001
317 * @tc.desc: Test for makeImage with canvas.
318 * @tc.type: FUNC
319 * @tc.require:I774GD
320 */
321 HWTEST_F(SurfaceTest, MakeImageWithCanvas001, TestSize.Level1)
322 {
323 const char* glsl = R"(
324 uniform shader imageshader0;
325 vec4 main(vec2 drawing_coord) {
326 return imageshader0.eval(drawing_coord);
327 }
328 )";
329 Matrix localMatrix;
330 auto effect = RuntimeEffect::CreateForShader(glsl);
331 RuntimeShaderBuilder builder(effect);
332 auto gpuContext = std::make_shared<GPUContext>();
333 ImageInfo imageInfo;
334 imageInfo = imageInfo.MakeN32Premul(256, 256); // 256 mean size
335 std::shared_ptr<Canvas> canvas = std::make_shared<Canvas>();
336 auto image = builder.MakeImage(*canvas, &localMatrix, imageInfo, false);
337 EXPECT_TRUE(image == nullptr);
338 }
339
340 /**
341 * @tc.name: MakeImageWithCanvas002
342 * @tc.desc: Test for makeImage with canvas.
343 * @tc.type: FUNC
344 * @tc.require:I774GD
345 */
346 HWTEST_F(SurfaceTest, MakeImageWithCanvas002, TestSize.Level1)
347 {
348 const char* glsl = R"(
349 uniform shader imageshader0;
350 vec4 main(vec2 drawing_coord) {
351 return imageshader0.eval(drawing_coord);
352 }
353 )";
354 Matrix localMatrix;
355 auto effect = RuntimeEffect::CreateForShader(glsl);
356 RuntimeShaderBuilder builder(effect);
357 auto gpuContext = std::make_shared<GPUContext>();
358 ImageInfo imageInfo;
359 imageInfo = imageInfo.MakeN32Premul(256, 256); // 256 mean size
360 std::shared_ptr<Canvas> canvas = std::make_shared<Canvas>();
361 auto overDrawCanvas = std::make_shared<OverDrawCanvas>(canvas);
362 overDrawCanvas->SetGrContext(gpuContext);
363 auto image = builder.MakeImage(*overDrawCanvas, &localMatrix, imageInfo, false);
364 EXPECT_TRUE(image == nullptr);
365 }
366
367 /**
368 * @tc.name: MakeImageWithCanvas003
369 * @tc.desc: Test for makeImage with canvas.
370 * @tc.type: FUNC
371 * @tc.require:I774GD
372 */
373 HWTEST_F(SurfaceTest, MakeImageWithCanvas003, TestSize.Level1)
374 {
375 const char* glsl = R"(
376 uniform shader imageshader0;
377 vec4 main(vec2 drawing_coord) {
378 return imageshader0.eval(drawing_coord);
379 }
380 )";
381 Matrix localMatrix;
382 auto effect = RuntimeEffect::CreateForShader(glsl);
383 RuntimeShaderBuilder builder(effect);
384 auto gpuContext = std::make_shared<GPUContext>();
385 ImageInfo imageInfo;
386 imageInfo = imageInfo.MakeN32Premul(256, 256); // 256 mean size
387 auto surface = std::make_unique<Surface>();
388 std::shared_ptr<CanvasMakeImage> canvas = std::make_shared<CanvasMakeImage>(surface.get());
389 auto image = builder.MakeImage(*canvas, &localMatrix, imageInfo, false);
390 EXPECT_TRUE(image == nullptr);
391 }
392 } // namespace Drawing
393 } // namespace Rosen
394 } // namespace OHOS