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 <gtest/gtest.h>
17 #include <sys/mman.h>
18 
19 #include "native_window.h"
20 #include "pixel_map_from_surface.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Media {
26 static constexpr int DEFAULT_WIDTH = 1280;
27 static constexpr int DEFAULT_HEIGHT = 800;
28 
29 class EglImageTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33 
34     static inline sptr<OHOS::Surface> cSurface;
35     class ConsumerSurfaceListener : public IBufferConsumerListener {
36     public:
ConsumerSurfaceListener(const sptr<OHOS::Surface> & cSurface)37         explicit ConsumerSurfaceListener(const sptr<OHOS::Surface> &cSurface) : surface(cSurface) {}
OnBufferAvailable()38         void OnBufferAvailable()
39         {
40             if (surface == nullptr) {
41                 return;
42             }
43             // acquire buffer
44             sptr<SurfaceBuffer> buffer = nullptr;
45             int fence = -1;
46             int64_t timestamp;
47             OHOS::Rect damage;
48             auto ret = surface->AcquireBuffer(buffer, fence, timestamp, damage);
49             if (ret != SURFACE_ERROR_OK) {
50                 close(fence);
51                 return;
52             }
53             close(fence);
54             surface->ReleaseBuffer(lastBuffer, -1);
55             lastBuffer = buffer;
56         };
57     private:
58         sptr<OHOS::Surface> surface;
59         sptr<SurfaceBuffer> lastBuffer;
60     };
61     static inline sptr<OHOS::IBufferConsumerListener> consumerListener;
62     static inline sptr<OHOS::Surface> pSurface;
63     static inline NativeWindow* nativeWindow = nullptr;
64 
65     static void Draw();
66 };
67 
Draw()68 void EglImageTest::Draw()
69 {
70     Region::Rect srcRect = {0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
71     Region region;
72     region.rects = &srcRect;
73     region.rectNumber = 1;
74     if (nativeWindow == nullptr) {
75         return;
76     }
77 
78     int fence = -1;
79     NativeWindowBuffer *buffer;
80     int ret = NativeWindowRequestBuffer(nativeWindow, &buffer, &fence);
81     if (ret != 0 || buffer == nullptr) {
82         close(fence);
83         return;
84     }
85 
86     close(fence);
87     NativeObjectReference(buffer);
88 
89     // draw pixels.
90     auto bufferHandle = GetBufferHandleFromNative(buffer);
91     if (bufferHandle != nullptr) {
92         uint8_t *data = reinterpret_cast<uint8_t *>(
93             mmap(bufferHandle->virAddr, bufferHandle->size, PROT_READ | PROT_WRITE, MAP_SHARED, bufferHandle->fd, 0));
94         if (data != MAP_FAILED) {
95             uint8_t val = 128;
96             for (int i = 0; i < bufferHandle->size; ++i) {
97                 data[i] = val;
98             }
99             munmap(data, bufferHandle->size);
100         }
101     }
102 
103     NativeWindowFlushBuffer(nativeWindow, buffer, -1, region);
104     NativeObjectUnreference(buffer);
105 }
106 
SetUpTestCase()107 void EglImageTest::SetUpTestCase()
108 {
109     cSurface = Surface::CreateSurfaceAsConsumer("imageFrameWorkEglImageTestSurface");
110     consumerListener = sptr<OHOS::IBufferConsumerListener>(new ConsumerSurfaceListener(cSurface));
111 
112     if (cSurface != nullptr) {
113         auto producer = cSurface->GetProducer();
114         pSurface = Surface::CreateSurfaceAsProducer(producer);
115         cSurface->RegisterConsumerListener(consumerListener);
116     }
117     if (pSurface != nullptr) {
118         nativeWindow = CreateNativeWindowFromSurface(&pSurface);
119         NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
120         SurfaceUtils::GetInstance()->Add(pSurface->GetUniqueId(), pSurface);
121     }
122 }
123 
TearDownTestCase()124 void EglImageTest::TearDownTestCase()
125 {
126     if (nativeWindow != nullptr) {
127         DestoryNativeWindow(nativeWindow);
128         nativeWindow = nullptr;
129     }
130     if (pSurface != nullptr) {
131         SurfaceUtils::GetInstance()->Remove(pSurface->GetUniqueId());
132     }
133 }
134 
135 /**
136  * @tc.name: RenderContextTest001
137  * @tc.desc: Test of RenderContext
138  * @tc.type: FUNC
139  */
140 HWTEST_F(EglImageTest, RenderContextTest001, TestSize.Level1)
141 {
142     auto renderContext = std::make_unique<RenderContext>();
143     auto ret = renderContext->Init();
144     EXPECT_EQ(ret, true);
145 }
146 
147 /**
148  * @tc.name: RenderContextTest002
149  * @tc.desc: Test of RenderContext
150  * @tc.type: FUNC
151  */
152 HWTEST_F(EglImageTest, RenderContextTest002, TestSize.Level1)
153 {
154     auto renderContext = std::make_unique<RenderContext>();
155     EXPECT_EQ(renderContext->GetGrContext(), nullptr);
156     EXPECT_EQ(renderContext->GetEGLContext(), EGL_NO_CONTEXT);
157     EXPECT_EQ(renderContext->GetEGLDisplay(), EGL_NO_DISPLAY);
158     auto ret = renderContext->Init();
159     EXPECT_EQ(ret, true);
160     EXPECT_NE(renderContext->GetGrContext(), nullptr);
161     EXPECT_NE(renderContext->GetEGLContext(), EGL_NO_CONTEXT);
162     EXPECT_NE(renderContext->GetEGLDisplay(), EGL_NO_DISPLAY);
163 }
164 
165 /**
166  * @tc.name: RenderContextTest003
167  * @tc.desc: Test of RenderContext
168  * @tc.type: FUNC
169  */
170 HWTEST_F(EglImageTest, RenderContextTest003, TestSize.Level1)
171 {
172     auto renderContext = std::make_unique<RenderContext>();
173     auto ret = renderContext->Init();
174     EXPECT_EQ(ret, true);
175     renderContext->MakeCurrent(EGL_NO_SURFACE);
176     auto currSurface = eglGetCurrentSurface(EGL_DRAW);
177     // even though MakeCurrent(EGL_NO_SURFACE), current surface is still not EGL_NO_SURFACE
178     // in our renderContext, it will be a pbufferSurface.
179     EXPECT_NE(currSurface, EGL_NO_SURFACE);
180 }
181 
182 /**
183  * @tc.name: RenderContextTest004
184  * @tc.desc: Test of RenderContext
185  * @tc.type: FUNC
186  */
187 HWTEST_F(EglImageTest, RenderContextTest004, TestSize.Level1)
188 {
189     auto renderContext = std::make_unique<RenderContext>();
190     auto ret = renderContext->Init();
191     EXPECT_EQ(ret, true);
192     renderContext->MakeCurrent(EGL_NO_SURFACE);
193     auto currSurface = eglGetCurrentSurface(EGL_DRAW);
194     // even though MakeCurrent(EGL_NO_SURFACE), current surface is still not EGL_NO_SURFACE
195     // in our renderContext, it will be a pbufferSurface.
196     EXPECT_NE(currSurface, EGL_NO_SURFACE);
197 }
198 
199 /**
200  * @tc.name: RenderContextTest005
201  * @tc.desc: Test of RenderContext
202  * @tc.type: FUNC
203  */
204 HWTEST_F(EglImageTest, RenderContextTest005, TestSize.Level1)
205 {
206     auto renderContext = std::make_unique<RenderContext>();
207     auto ret = renderContext->Init();
208     EXPECT_EQ(ret, true);
209     renderContext->MakeCurrent(EGL_NO_SURFACE);
210     auto currSurface = eglGetCurrentSurface(EGL_DRAW);
211     // even though MakeCurrent(EGL_NO_SURFACE), current surface is still not EGL_NO_SURFACE
212     // in our renderContext, it will be a pbufferSurface.
213     EXPECT_NE(currSurface, EGL_NO_SURFACE);
214     EGLSurface surface = eglCreateWindowSurface(
215         renderContext->GetEGLDisplay(), renderContext->GetEGLConfig(),
216         static_cast<EGLNativeWindowType>(nativeWindow), nullptr);
217     EXPECT_NE(surface, EGL_NO_SURFACE);
218     renderContext->MakeCurrent(surface);
219     EXPECT_EQ(surface, eglGetCurrentSurface(EGL_DRAW));
220 }
221 
222 /**
223  * @tc.name: PixelMapFromSurfaceTest001
224  * @tc.desc: Test of PixelMapFromSurface
225  * @tc.type: FUNC
226  */
227 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest001, TestSize.Level3)
228 {
229     Rect srcRect = {0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
230     // pass an invalid surfaceId to expect to get an invalid result.
231     auto pixelMap = CreatePixelMapFromSurfaceId(0, srcRect);
232     EXPECT_EQ(pixelMap, nullptr);
233 }
234 
235 
236 /**
237  * @tc.name: PixelMapFromSurfaceTest002
238  * @tc.desc: Test of PixelMapFromSurface
239  * @tc.type: FUNC
240  */
241 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest002, TestSize.Level3)
242 {
243     EglImageTest::Draw();
244     // pass a valid srcRect to expect to get a valid result.
245     Rect srcRect = {0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
246     if (pSurface != nullptr) {
247         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
248         EXPECT_NE(pixelMap, nullptr);
249     }
250 }
251 
252 /**
253  * @tc.name: PixelMapFromSurfaceTest003
254  * @tc.desc: Test of PixelMapFromSurface
255  * @tc.type: FUNC
256  */
257 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest003, TestSize.Level3)
258 {
259     EglImageTest::Draw();
260     // pass an invalid srcRect to expect to get an invalid result.
261     Rect srcRect = {-1, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
262     if (pSurface != nullptr) {
263         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
264         EXPECT_EQ(pixelMap, nullptr);
265     }
266 }
267 
268 /**
269  * @tc.name: PixelMapFromSurfaceTest004
270  * @tc.desc: Test of PixelMapFromSurface
271  * @tc.type: FUNC
272  */
273 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest004, TestSize.Level3)
274 {
275     EglImageTest::Draw();
276     // pass an invalid srcRect to expect to get an invalid result.
277     Rect srcRect = {0, -1, DEFAULT_WIDTH, DEFAULT_HEIGHT};
278     if (pSurface != nullptr) {
279         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
280         EXPECT_EQ(pixelMap, nullptr);
281     }
282 }
283 
284 /**
285  * @tc.name: PixelMapFromSurfaceTest005
286  * @tc.desc: Test of PixelMapFromSurface
287  * @tc.type: FUNC
288  */
289 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest005, TestSize.Level3)
290 {
291     EglImageTest::Draw();
292     // pass an invalid srcRect to expect to get an invalid result.
293     Rect srcRect = {-1, -1, DEFAULT_WIDTH, DEFAULT_HEIGHT};
294     if (pSurface != nullptr) {
295         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
296         EXPECT_EQ(pixelMap, nullptr);
297     }
298 }
299 
300 /**
301  * @tc.name: PixelMapFromSurfaceTest006
302  * @tc.desc: Test of PixelMapFromSurface
303  * @tc.type: FUNC
304  */
305 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest006, TestSize.Level3)
306 {
307     EglImageTest::Draw();
308     // pass an invalid srcRect to expect to get an invalid result.
309     Rect srcRect = {DEFAULT_WIDTH, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
310     if (pSurface != nullptr) {
311         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
312         EXPECT_EQ(pixelMap, nullptr);
313     }
314 }
315 
316 /**
317  * @tc.name: PixelMapFromSurfaceTest007
318  * @tc.desc: Test of PixelMapFromSurface
319  * @tc.type: FUNC
320  */
321 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest007, TestSize.Level3)
322 {
323     EglImageTest::Draw();
324     // pass an invalid srcRect to expect to get an invalid result.
325     Rect srcRect = {0, DEFAULT_HEIGHT + 1, DEFAULT_WIDTH, DEFAULT_HEIGHT};
326     if (pSurface != nullptr) {
327         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
328         EXPECT_EQ(pixelMap, nullptr);
329     }
330 }
331 
332 /**
333  * @tc.name: PixelMapFromSurfaceTest008
334  * @tc.desc: Test of PixelMapFromSurface
335  * @tc.type: FUNC
336  */
337 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest008, TestSize.Level3)
338 {
339     EglImageTest::Draw();
340     // pass an invalid srcRect to expect to get an invalid result.
341     Rect srcRect = {DEFAULT_WIDTH + 1, DEFAULT_HEIGHT, DEFAULT_WIDTH, DEFAULT_HEIGHT};
342     if (pSurface != nullptr) {
343         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
344         EXPECT_EQ(pixelMap, nullptr);
345     }
346 }
347 
348 /**
349  * @tc.name: PixelMapFromSurfaceTest009
350  * @tc.desc: Test of PixelMapFromSurface
351  * @tc.type: FUNC
352  */
353 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest009, TestSize.Level3)
354 {
355     EglImageTest::Draw();
356     // pass an invalid srcRect to expect to get an invalid result.
357     Rect srcRect = {0, 0, 0, DEFAULT_HEIGHT};
358     if (pSurface != nullptr) {
359         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
360         EXPECT_EQ(pixelMap, nullptr);
361     }
362 }
363 
364 /**
365  * @tc.name: PixelMapFromSurfaceTest010
366  * @tc.desc: Test of PixelMapFromSurface
367  * @tc.type: FUNC
368  */
369 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest010, TestSize.Level3)
370 {
371     EglImageTest::Draw();
372     // pass an invalid srcRect to expect to get an invalid result.
373     Rect srcRect = {0, 0, 0, 0};
374     if (pSurface != nullptr) {
375         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
376         EXPECT_EQ(pixelMap, nullptr);
377     }
378 }
379 
380 /**
381  * @tc.name: PixelMapFromSurfaceTest011
382  * @tc.desc: Test of PixelMapFromSurface
383  * @tc.type: FUNC
384  */
385 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest011, TestSize.Level3)
386 {
387     EglImageTest::Draw();
388     // pass an invalid srcRect to expect to get an invalid result.
389     Rect srcRect = {0, 0, DEFAULT_WIDTH, 0};
390     if (pSurface != nullptr) {
391         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
392         EXPECT_EQ(pixelMap, nullptr);
393     }
394 }
395 
396 /**
397  * @tc.name: PixelMapFromSurfaceTest012
398  * @tc.desc: Test of PixelMapFromSurface
399  * @tc.type: FUNC
400  */
401 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest012, TestSize.Level3)
402 {
403     EglImageTest::Draw();
404     // pass an invalid srcRect to expect to get an invalid result.
405     Rect srcRect = {0, 0, DEFAULT_WIDTH + 1, DEFAULT_HEIGHT};
406     if (pSurface != nullptr) {
407         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
408         EXPECT_EQ(pixelMap, nullptr);
409     }
410 }
411 
412 /**
413  * @tc.name: PixelMapFromSurfaceTest013
414  * @tc.desc: Test of PixelMapFromSurface
415  * @tc.type: FUNC
416  */
417 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest013, TestSize.Level3)
418 {
419     EglImageTest::Draw();
420     // pass an invalid srcRect to expect to get an invalid result.
421     Rect srcRect = {0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT + 1};
422     if (pSurface != nullptr) {
423         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
424         EXPECT_EQ(pixelMap, nullptr);
425     }
426 }
427 
428 /**
429  * @tc.name: PixelMapFromSurfaceTest014
430  * @tc.desc: Test of PixelMapFromSurface
431  * @tc.type: FUNC
432  */
433 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest014, TestSize.Level3)
434 {
435     EglImageTest::Draw();
436     // pass an invalid srcRect to expect to get an invalid result.
437     // DEFAULT_WIDTH / 2 + DEFAULT_WIDTH > DEFAULT_WIDTH
438     Rect srcRect = {DEFAULT_WIDTH / 2, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT};
439     if (pSurface != nullptr) {
440         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
441         EXPECT_EQ(pixelMap, nullptr);
442     }
443 }
444 
445 /**
446  * @tc.name: PixelMapFromSurfaceTest015
447  * @tc.desc: Test of PixelMapFromSurface
448  * @tc.type: FUNC
449  */
450 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest015, TestSize.Level3)
451 {
452     EglImageTest::Draw();
453     // pass an invalid srcRect to expect to get an invalid result.
454     // DEFAULT_HEIGHT / 2 + DEFAULT_HEIGHT > DEFAULT_HEIGHT
455     Rect srcRect = {0, DEFAULT_HEIGHT / 2, DEFAULT_WIDTH, DEFAULT_HEIGHT};
456     if (pSurface != nullptr) {
457         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
458         EXPECT_EQ(pixelMap, nullptr);
459     }
460 }
461 
462 /**
463  * @tc.name: PixelMapFromSurfaceTest016
464  * @tc.desc: Test of PixelMapFromSurface
465  * @tc.type: FUNC
466  */
467 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest016, TestSize.Level3)
468 {
469     EglImageTest::Draw();
470     // pass a valid srcRect to expect to get a valid result.
471     // DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 3 are valid rect sizes.
472     Rect srcRect = {0, 0, DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 3};
473     if (pSurface != nullptr) {
474         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
475         EXPECT_NE(pixelMap, nullptr);
476     }
477 }
478 
479 /**
480  * @tc.name: PixelMapFromSurfaceTest017
481  * @tc.desc: Test of PixelMapFromSurface
482  * @tc.type: FUNC
483  */
484 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest017, TestSize.Level3)
485 {
486     EglImageTest::Draw();
487     // pass a valid srcRect to expect to get a valid result.
488     // DEFAULT_WIDTH / 2 + DEFAULT_WIDTH / 2 <= DEFAULT_WIDTH, DEFAULT_HEIGHT / 3 + DEFAULT_HEIGHT / 3 < DEFAULT_HEIGHT.
489     Rect srcRect = {DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 3, DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 3};
490     if (pSurface != nullptr) {
491         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
492         EXPECT_NE(pixelMap, nullptr);
493     }
494 }
495 
496 /**
497  * @tc.name: PixelMapFromSurfaceTest018
498  * @tc.desc: Test of PixelMapFromSurface
499  * @tc.type: FUNC
500  */
501 HWTEST_F(EglImageTest, PixelMapFromSurfaceTest018, TestSize.Level3)
502 {
503     EglImageTest::Draw();
504     // pass a valid srcRect to expect to get a valid result.
505     // DEFAULT_WIDTH / 2 + DEFAULT_WIDTH / 3 < DEFAULT_WIDTH, 0 + DEFAULT_HEIGHT / 3 < DEFAULT_HEIGHT.
506     Rect srcRect = {DEFAULT_WIDTH / 2, 0, DEFAULT_WIDTH / 3, DEFAULT_HEIGHT / 3};
507     if (pSurface != nullptr) {
508         auto pixelMap = CreatePixelMapFromSurfaceId(pSurface->GetUniqueId(), srcRect);
509         EXPECT_NE(pixelMap, nullptr);
510     }
511 }
512 
513 /**
514  * @tc.name: Clear001
515  * @tc.desc: Test of Clear
516  * @tc.type: FUNC
517  */
518 HWTEST_F(EglImageTest, Clear001, TestSize.Level3)
519 {
520     auto renderContext = std::make_unique<RenderContext>();
521     renderContext->eglDisplay_ = EGL_NO_DISPLAY;
522     renderContext->Clear();
523     EXPECT_EQ(renderContext->eglDisplay_, EGL_NO_DISPLAY);
524 }
525 
526 /**
527  * @tc.name: CreatePbufferSurface001
528  * @tc.desc: Test of CreatePbufferSurface
529  * @tc.type: FUNC
530  */
531 HWTEST_F(EglImageTest, CreatePbufferSurface001, TestSize.Level3)
532 {
533     auto renderContext = std::make_unique<RenderContext>();
534     renderContext->pbufferSurface_ = EGL_NO_SURFACE;
535     bool ret = renderContext->CreatePbufferSurface();
536     EXPECT_EQ(ret, false);
537 }
538 }
539 }
540