1 /*
2 * Copyright (c) 2024 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 <cstdlib>
17 #include <cstdio>
18 #include <ctime>
19 #include <dlfcn.h>
20 #include <fstream>
21 #include <memory>
22 #include <string>
23
24 #include <gtest/gtest.h>
25
26 #include "algorithm_common.h"
27 #include "algorithm_errors.h"
28
29 #include "aihdr_enhancer_video_impl.h"
30 #include "aihdr_enhancer_video.h"
31 #include "surface/window.h"
32 #include "external_window.h"
33
34 using namespace std;
35 using namespace testing::ext;
36 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
37 constexpr int64_t NANOS_IN_MICRO = 1000L;
38 constexpr uint32_t DEFAULT_WIDTH = 1920;
39 constexpr uint32_t DEFAULT_HEIGHT = 1080;
40
41 namespace OHOS {
42 namespace Media {
43 namespace VideoProcessingEngine {
44 std::shared_ptr<AihdrEnhancerVideo> aev = nullptr;
45 class AihdrEnhancerVideoCallbackImpl : public AihdrEnhancerVideoCallback {
46 public:
47 AihdrEnhancerVideoCallbackImpl() = default;
48 ~AihdrEnhancerVideoCallbackImpl() override = default;
49 AihdrEnhancerVideoCallbackImpl(const AihdrEnhancerVideoCallbackImpl&) = delete;
50 AihdrEnhancerVideoCallbackImpl& operator=(const AihdrEnhancerVideoCallbackImpl&) = delete;
51 AihdrEnhancerVideoCallbackImpl(AihdrEnhancerVideoCallbackImpl&&) = delete;
52 AihdrEnhancerVideoCallbackImpl& operator=(AihdrEnhancerVideoCallbackImpl&&) = delete;
53
54 void OnError(int32_t errorCode) override;
55 void OnState(int32_t state) override;
56 void OnOutputBufferAvailable(uint32_t index, AihdrEnhancerBufferFlag flag) override;
57 };
OnOutputBufferAvailable(uint32_t index,AihdrEnhancerBufferFlag flag)58 void AihdrEnhancerVideoCallbackImpl::OnOutputBufferAvailable(uint32_t index, AihdrEnhancerBufferFlag flag)
59 {
60 if (aev != nullptr) {
61 aev->ReleaseOutputBuffer(index, flag);
62 }
63 }
OnError(int32_t errorCode)64 void AihdrEnhancerVideoCallbackImpl::OnError(int32_t errorCode)
65 {
66 (void)errorCode;
67 }
OnState(int32_t state)68 void AihdrEnhancerVideoCallbackImpl::OnState(int32_t state)
69 {
70 (void)state;
71 }
72
73 class AihdrEnhancerVideoUnitTest : public testing::Test {
74 public:
75 static void SetUpTestCase(void);
76 static void TearDownTestCase(void);
77 void SetUp();
78 void TearDown();
79 sptr<Surface> surface;
80 OHNativeWindow *nativeWindow;
81 uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer);
82 };
83
SetUpTestCase(void)84 void AihdrEnhancerVideoUnitTest::SetUpTestCase(void)
85 {
86 cout << "[SetUpTestCase]: " << endl;
87 }
88
TearDownTestCase(void)89 void AihdrEnhancerVideoUnitTest::TearDownTestCase(void)
90 {
91 cout << "[TearDownTestCase]: " << endl;
92 }
93
SetUp(void)94 void AihdrEnhancerVideoUnitTest::SetUp(void)
95 {
96 cout << "[SetUp]: SetUp!!!" << endl;
97 }
98
TearDown(void)99 void AihdrEnhancerVideoUnitTest::TearDown(void)
100 {
101 cout << "[TearDown]: over!!!" << endl;
102 }
103
GetSystemTime()104 int64_t GetSystemTime()
105 {
106 struct timespec now;
107 (void)clock_gettime(CLOCK_BOOTTIME, &now);
108 int64_t nanoTime = reinterpret_cast<int64_t>(now.tv_sec) * NANOS_IN_SECOND + now.tv_nsec;
109
110 return nanoTime / NANOS_IN_MICRO;
111 }
112
FlushSurf(OHNativeWindowBuffer * ohNativeWindowBuffer)113 uint32_t AihdrEnhancerVideoUnitTest::FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer)
114 {
115 struct Region region;
116 struct Region::Rect *rect = new Region::Rect();
117 rect->x = 0;
118 rect->y = 0;
119 rect->w = DEFAULT_WIDTH;
120 rect->h = DEFAULT_HEIGHT;
121 region.rects = rect;
122 NativeWindowHandleOpt(nativeWindow, SET_UI_TIMESTAMP, GetSystemTime());
123 int32_t err = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, ohNativeWindowBuffer, -1, region);
124 delete rect;
125 if (err != 0) {
126 cout << "FlushBuffer failed" << endl;
127 return 1;
128 }
129 return 0;
130 }
131
132 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_init_01, TestSize.Level1)
133 {
134 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
135 EXPECT_NE(aihdrEnhancerVideo, nullptr);
136 }
137
138 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_02, TestSize.Level1)
139 {
140 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
141 std::shared_ptr<AihdrEnhancerVideoCallback> cb = nullptr;
142 auto ret = aihdrEnhancerVideo->SetCallback(cb);
143 EXPECT_NE(ret, VPE_ALGO_ERR_OK);
144 }
145
146 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_03, TestSize.Level1)
147 {
148 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
149 OHNativeWindow* window{};
150 auto ret = aihdrEnhancerVideo->GetSurface(&window);
151 EXPECT_EQ(ret, VPE_ALGO_ERR_OK);
152 }
153
154 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_04, TestSize.Level1)
155 {
156 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
157 OHNativeWindow* window{};
158 auto ret = aihdrEnhancerVideo->GetSurface(&window);
159 ret = aihdrEnhancerVideo->GetSurface(&window);
160 EXPECT_NE(ret, VPE_ALGO_ERR_OK);
161 }
162
163 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_05, TestSize.Level1)
164 {
165 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
166 OHNativeWindow* window{};
167 auto ret = aihdrEnhancerVideo->SetSurface(window);
168 EXPECT_NE(ret, VPE_ALGO_ERR_OK);
169 }
170
171 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_06, TestSize.Level1)
172 {
173 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
174
175 auto ret = aihdrEnhancerVideo->ReleaseOutputBuffer(0, true);
176 EXPECT_NE(ret, VPE_ALGO_ERR_OK);
177 }
178
179 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_07, TestSize.Level1)
180 {
181 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
182 auto ret = aihdrEnhancerVideo->NotifyEos();
183 EXPECT_NE(ret, VPE_ALGO_ERR_OK);
184 }
185
186 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_08, TestSize.Level1)
187 {
188 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
189 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
190 auto res = aihdrEnhancerVideo->SetCallback(cb);
191 EXPECT_EQ(res, VPE_ALGO_ERR_OK);
192 }
193
194 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_09, TestSize.Level1)
195 {
196 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
197 auto res = aihdrEnhancerVideo->Stop();
198 EXPECT_NE(res, VPE_ALGO_ERR_OK);
199 }
200
201 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_10, TestSize.Level1)
202 {
203 OHNativeWindowBuffer *ohNativeWindowBuffer;
204 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
205 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
206 auto res = aihdrEnhancerVideo->SetCallback(cb);
207 aihdrEnhancerVideo->GetSurface(&nativeWindow);
208
209 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
210 OHNativeWindow* window2{};
211 aihdrEnhancerVideo2->GetSurface(&window2);
212 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
213 res = aihdrEnhancerVideo->SetSurface(window2);
214 res = aihdrEnhancerVideo->Prepare();
215 res = aihdrEnhancerVideo->Start();
216 EXPECT_EQ(res, VPE_ALGO_ERR_OK);
217
218 int fenceFd = -1;
219 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
220 OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
221 auto ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &ohNativeWindowBuffer, &fenceFd);
222 ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
223 ret = FlushSurf(ohNativeWindowBuffer);
224 ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
225 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
226 }
227
228 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_11, TestSize.Level1)
229 {
230 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
231 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
232 auto res = aihdrEnhancerVideo->SetCallback(cb);
233 aihdrEnhancerVideo->GetSurface(&nativeWindow);
234
235 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
236 OHNativeWindow* window2{};
237 aihdrEnhancerVideo2->GetSurface(&window2);
238 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
239 res = aihdrEnhancerVideo->SetSurface(window2);
240 res = aihdrEnhancerVideo->Prepare();
241 res = aihdrEnhancerVideo->Start();
242 res = aihdrEnhancerVideo->Stop();
243 res = aihdrEnhancerVideo->Prepare();
244 res = aihdrEnhancerVideo->Stop();
245 EXPECT_NE(res, VPE_ALGO_ERR_OK);
246 }
247
248 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_12, TestSize.Level1)
249 {
250 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
251 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
252 auto res = aihdrEnhancerVideo->SetCallback(cb);
253 aihdrEnhancerVideo->GetSurface(&nativeWindow);
254
255 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
256 OHNativeWindow* window2{};
257 aihdrEnhancerVideo2->GetSurface(&window2);
258 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
259 res = aihdrEnhancerVideo->SetSurface(window2);
260 res = aihdrEnhancerVideo->Prepare();
261 res = aihdrEnhancerVideo->Start();
262 res = aihdrEnhancerVideo->Stop();
263 res = aihdrEnhancerVideo->Prepare();
264 res = aihdrEnhancerVideo->Stop();
265 EXPECT_NE(res, VPE_ALGO_ERR_OK);
266 }
267
268 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_13, TestSize.Level1)
269 {
270 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
271 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
272 auto res = aihdrEnhancerVideo->SetCallback(cb);
273 aihdrEnhancerVideo->GetSurface(&nativeWindow);
274
275 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
276 OHNativeWindow* window2{};
277 aihdrEnhancerVideo2->GetSurface(&window2);
278
279 auto aihdrEnhancerVideo3 = AihdrEnhancerVideo::Create();
280 OHNativeWindow* window3{};
281 aihdrEnhancerVideo3->GetSurface(&window3);
282
283 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
284 res = aihdrEnhancerVideo->SetSurface(window2);
285 res = aihdrEnhancerVideo->Prepare();
286 res = aihdrEnhancerVideo->Start();
287 res = aihdrEnhancerVideo->SetSurface(window3);
288 EXPECT_EQ(res, VPE_ALGO_ERR_OK);
289 }
290
291 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_14, TestSize.Level1)
292 {
293 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
294 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
295 auto res = aihdrEnhancerVideo->SetCallback(cb);
296 aihdrEnhancerVideo->GetSurface(&nativeWindow);
297
298 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
299 OHNativeWindow* window2{};
300 aihdrEnhancerVideo2->GetSurface(&window2);
301 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
302 res = aihdrEnhancerVideo->SetSurface(window2);
303 res = aihdrEnhancerVideo->Prepare();
304 res = aihdrEnhancerVideo->Start();
305 res = aihdrEnhancerVideo->Stop();
306 res = aihdrEnhancerVideo->SetSurface(window2);
307 EXPECT_NE(res, VPE_ALGO_ERR_OK);
308 }
309
310 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_15, TestSize.Level1)
311 {
312 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
313 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
314 auto res = aihdrEnhancerVideo->SetCallback(cb);
315 aihdrEnhancerVideo->GetSurface(&nativeWindow);
316
317 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
318 OHNativeWindow* window2{};
319 aihdrEnhancerVideo2->GetSurface(&window2);
320
321 auto aihdrEnhancerVideo3 = AihdrEnhancerVideo::Create();
322 OHNativeWindow* window3{};
323 aihdrEnhancerVideo3->GetSurface(&window3);
324
325 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
326 res = aihdrEnhancerVideo->SetSurface(window2);
327 res = aihdrEnhancerVideo->Prepare();
328 res = aihdrEnhancerVideo->Start();
329 res = aihdrEnhancerVideo->Stop();
330 res = aihdrEnhancerVideo->SetSurface(window3);
331 EXPECT_NE(res, VPE_ALGO_ERR_OK);
332 }
333
334 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_16, TestSize.Level1)
335 {
336 auto aihdrEnhancerVideo = AihdrEnhancerVideo::Create();
337 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
338 auto res = aihdrEnhancerVideo->SetCallback(cb);
339 aihdrEnhancerVideo->GetSurface(&nativeWindow);
340
341 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
342 OHNativeWindow* window2{};
343 aihdrEnhancerVideo2->GetSurface(&window2);
344
345 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
346 res = aihdrEnhancerVideo->SetSurface(window2);
347 res = aihdrEnhancerVideo->Prepare();
348 res = aihdrEnhancerVideo->Start();
349 res = aihdrEnhancerVideo->ReleaseOutputBuffer(0, true);
350
351 EXPECT_NE(res, VPE_ALGO_ERR_OK);
352 }
353
354 HWTEST_F(AihdrEnhancerVideoUnitTest, aihdrEnhancer_17, TestSize.Level1)
355 {
356 std::queue<sptr<SurfaceBuffer>> AppInBufferAvilQue;
357 sptr<SurfaceBuffer> buffer;
358 int32_t fence = -1;
359 BufferFlushConfig flushCfg_{};
360 BufferRequestConfig requestCfg_{};
361 requestCfg_.width = DEFAULT_WIDTH;
362 requestCfg_.height = DEFAULT_HEIGHT;
363 requestCfg_.timeout = 0;
364 requestCfg_.strideAlignment = 32;
365 flushCfg_.damage.x = 0;
366 flushCfg_.damage.y = 0;
367 flushCfg_.damage.w = DEFAULT_WIDTH;
368 flushCfg_.damage.h = DEFAULT_HEIGHT;
369 flushCfg_.timestamp = 0;
370
371 aev = AihdrEnhancerVideo::Create();
372 std::shared_ptr<AihdrEnhancerVideoCallback> cb = std::make_shared<AihdrEnhancerVideoCallbackImpl>();
373 auto res = aev->SetCallback(cb);
374 OHNativeWindow* window1{};
375 aev->GetSurface(&window1);
376
377 auto aihdrEnhancerVideo2 = AihdrEnhancerVideo::Create();
378 OHNativeWindow* window2{};
379 aihdrEnhancerVideo2->GetSurface(&window2);
380
381 auto aihdrEnhancerVideo3 = AihdrEnhancerVideo::Create();
382 OHNativeWindow* window3{};
383 aihdrEnhancerVideo3->GetSurface(&window3);
384
385 OH_NativeWindow_NativeWindowHandleOpt(window2, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
386 res = aev->SetSurface(window2);
387 res = aev->Prepare();
388 res = aev->Start();
389 EXPECT_EQ(res, VPE_ALGO_ERR_OK);
390 for (int i = 0; i < 3; i++) {
391 OHNativeWindow* windowTmp = (i % 2) ? window2 : window3;
392 window1->surface->RequestBuffer(buffer, fence, requestCfg_);
393 AppInBufferAvilQue.push(buffer);
394 window1->surface->FlushBuffer(buffer, -1, flushCfg_);
395 aev->SetSurface(windowTmp);
396 aev->NotifyEos();
397 sleep(2);
398 }
399 aihdrEnhancerVideo2->Release();
400 aihdrEnhancerVideo3->Release();
401 }
402 } // namespace VideoProcessingEngine
403 } // namespace Media
404 } // namespace OHOS
405