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 #include <chrono>
16 #include <thread>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <native_window.h>
21 #include "external_window.h"
22 #include "iconsumer_surface.h"
23 #include "event_handler.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS::Rosen {
29 constexpr uint32_t QUEUE_SIZE = 5;
30 class NativeWindowTest : public testing::Test, public IBufferConsumerListenerClazz {
31 public:
32 static void SetUpTestCase();
33 void OnBufferAvailable() override;
34 void SetData(NativeWindowBuffer *nativeWindowBuffer, NativeWindow *nativeWindow);
35 bool GetData(sptr<SurfaceBuffer> &buffer);
36
37 // OH_NativeWindow_CreateNativeWindow001
38 int32_t ThreadNativeWindowProcess001(int32_t *pipeFd, sptr<IBufferProducer> producer);
39 int32_t CreateNativeWindowAndRequestBuffer001(sptr<IBufferProducer> producer, NativeWindow **nativeWindow);
40
41 // OH_NativeWindow_CreateNativeWindow002
42 int32_t ThreadNativeWindowProcess002(int32_t *pipeFd, sptr<IBufferProducer> producer);
43 int32_t CreateNativeWindowAndRequestBuffer002(sptr<IBufferProducer> producer, NativeWindow **nativeWindow);
44
45 // OH_NativeWindow_CreateNativeWindowFromSurfaceId001
46 int32_t ThreadNativeWindowProcess003(int32_t *pipeFd, uint64_t uniqueId);
47 int32_t CreateNativeWindowAndRequestBuffer003(uint64_t uniqueId, NativeWindow **nativeWindow);
48
49 // OH_NativeWindow_CreateNativeWindowFromSurfaceId002
50 int32_t ThreadNativeWindowProcess004(int32_t *pipeFd, uint64_t uniqueId);
51 int32_t CreateNativeWindowAndRequestBuffer004(uint64_t uniqueId, NativeWindow **nativeWindow);
52 int32_t RequestBuffer001(NativeWindow *nativeWindow);
53
54 // OH_NativeWindow_GetLastFlushedBufferV2001
55 int32_t ThreadNativeWindowProcess005(int32_t *pipeFd, uint64_t uniqueId);
56 int32_t CreateNativeWindowAndRequestBuffer005(uint64_t uniqueId, NativeWindow **nativeWindow);
57
58 // OH_NativeWindow_NativeObjectReference001
59 int32_t ThreadNativeWindowProcess006(int32_t *pipeFd, uint64_t uniqueId);
60
61 // OH_NativeWindow_GetSurfaceId001
62 int32_t ThreadNativeWindowProcess007(int32_t *pipeFd, sptr<IBufferProducer> producer, uint64_t *uniqueId);
63 int32_t CreateNativeWindowAndRequestBuffer007(sptr<IBufferProducer> producer, NativeWindow **nativeWindow);
64
65 // OH_NativeWindow_NativeWindowAttachBuffer001
66 int32_t ThreadNativeWindowProcess008(int32_t *pipeFd, uint64_t uniqueId);
67 int32_t CreateNativeWindowAndRequestBuffer008(uint64_t uniqueId, NativeWindow **nativeWindow);
68 int32_t CreateNativeWindowAndAttachBuffer001(NativeWindow *nativeWindow);
69
70 int32_t g_onBufferAvailable_ = 0;
71 };
72
73 class OnBufferAvailableTest : public IBufferConsumerListenerClazz, public RefBase {
74 public:
75 void OnBufferAvailable() override;
76 };
77
OnBufferAvailable()78 void OnBufferAvailableTest::OnBufferAvailable() {};
79
SetUpTestCase()80 void NativeWindowTest::SetUpTestCase() {}
81
OnBufferAvailable()82 void NativeWindowTest::OnBufferAvailable()
83 {
84 g_onBufferAvailable_++;
85 }
86
CreateNativeWindowAndRequestBuffer001(sptr<IBufferProducer> producer,NativeWindow ** nativeWindow)87 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer001(sptr<IBufferProducer> producer,
88 NativeWindow **nativeWindow)
89 {
90 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
91 *nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
92 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
93
94 int32_t code = SET_BUFFER_GEOMETRY;
95 int32_t height = 0x100;
96 int32_t width = 0x100;
97 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
98
99 int32_t fenceFd = -1;
100 auto ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
101 if (ret != OHOS::GSERROR_OK) {
102 return ret;
103 }
104
105 struct Region *region = new Region();
106 struct Region::Rect *rect = new Region::Rect();
107 rect->w = 0x100;
108 rect->h = 0x100;
109 region->rects = rect;
110 region->rectNumber = 1;
111 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
112 if (ret != OHOS::GSERROR_OK) {
113 delete rect;
114 delete region;
115 return ret;
116 }
117 delete rect;
118 delete region;
119 return OHOS::GSERROR_OK;
120 }
121
ThreadNativeWindowProcess001(int32_t * pipeFd,sptr<IBufferProducer> producer)122 int32_t NativeWindowTest::ThreadNativeWindowProcess001(int32_t *pipeFd, sptr<IBufferProducer> producer)
123 {
124 int64_t data;
125 NativeWindow *nativeWindow = nullptr;
126 int32_t ret = CreateNativeWindowAndRequestBuffer001(producer, &nativeWindow);
127 if (ret != OHOS::GSERROR_OK) {
128 data = ret;
129 write(pipeFd[1], &data, sizeof(data));
130 return -1;
131 }
132
133 data = ret;
134 write(pipeFd[1], &data, sizeof(data));
135 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
136 read(pipeFd[0], &data, sizeof(data));
137 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
138 return 0;
139 }
140
141 HWTEST_F(NativeWindowTest, OH_NativeWindow_CreateNativeWindow001, Function | MediumTest | Level2)
142 {
143 int32_t pipeFd[2] = {};
144 pipe(pipeFd);
145
146 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_CreateNativeWindow001");
147 cSurface->RegisterConsumerListener(this);
148 auto producer = cSurface->GetProducer();
149
__anon656395190102() 150 std::thread thread([this, pipeFd, producer]() {
151 int32_t ret = this->ThreadNativeWindowProcess001((int32_t*)(pipeFd), producer);
152 EXPECT_EQ(ret, OHOS::GSERROR_OK);
153 });
154
155 int64_t data = 0;
156 read(pipeFd[0], &data, sizeof(data));
157 EXPECT_EQ(data, OHOS::GSERROR_OK);
158
159 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
160 int32_t fence = -1;
161 int64_t timestamp;
162 Rect damage;
163 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
164 EXPECT_EQ(ret, OHOS::GSERROR_OK);
165 EXPECT_NE(buffer, nullptr);
166
167 ret = cSurface->ReleaseBuffer(buffer, -1);
168 EXPECT_EQ(ret, OHOS::GSERROR_OK);
169
170 write(pipeFd[1], &data, sizeof(data));
171 close(pipeFd[0]);
172 close(pipeFd[1]);
173 if (thread.joinable()) {
174 thread.join();
175 }
176 producer = nullptr;
177 cSurface = nullptr;
178 }
179
SetData(NativeWindowBuffer * nativeWindowBuffer,NativeWindow * nativeWindow)180 void NativeWindowTest::SetData(NativeWindowBuffer *nativeWindowBuffer, NativeWindow *nativeWindow)
181 {
182 nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("123", 0x123);
183 nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("345", (int64_t)0x345);
184 nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("567", "567");
185 }
186
GetData(sptr<SurfaceBuffer> & buffer)187 bool NativeWindowTest::GetData(sptr<SurfaceBuffer> &buffer)
188 {
189 int32_t int32;
190 int64_t int64;
191 std::string str;
192 buffer->GetExtraData()->ExtraGet("123", int32);
193 buffer->GetExtraData()->ExtraGet("345", int64);
194 buffer->GetExtraData()->ExtraGet("567", str);
195 if ((int32 != 0x123) || (int64 != 0x345) || (str != "567")) {
196 return false;
197 }
198
199 return true;
200 }
201
202 HWTEST_F(NativeWindowTest, OH_NativeWindow_NativeWindowAttachBuffer001, Function | MediumTest | Level2)
203 {
204 int32_t pipeFd[2] = {};
205 pipe(pipeFd);
206
207 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_NativeWindowAttachBuffer001");
208 cSurface->RegisterConsumerListener(this);
209 auto producer = cSurface->GetProducer();
210 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
211 EXPECT_NE(pSurface, nullptr);
212 EXPECT_EQ(cSurface->SetQueueSize(QUEUE_SIZE), OHOS::GSERROR_OK);
213
214 uint64_t uniqueId = cSurface->GetUniqueId();
__anon656395190202() 215 std::thread thread([this, pipeFd, uniqueId]() {
216 int32_t ret = this->ThreadNativeWindowProcess008((int32_t*)(pipeFd), uniqueId);
217 EXPECT_EQ(ret, OHOS::GSERROR_OK);
218 });
219
220 int64_t data = 0;
221 read(pipeFd[0], &data, sizeof(data));
222 EXPECT_EQ(data, OHOS::GSERROR_OK);
223
224 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
225 int32_t fence = -1;
226 int64_t timestamp;
227 Rect damage;
228 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
229 while (g_onBufferAvailable_ > 0) {
230 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
231 EXPECT_EQ(ret, OHOS::GSERROR_OK);
232 EXPECT_NE(buffer, nullptr);
233 EXPECT_EQ(GetData(buffer), true);
234
235 ret = cSurface->ReleaseBuffer(buffer, -1);
236 EXPECT_EQ(ret, OHOS::GSERROR_OK);
237 g_onBufferAvailable_--;
238 }
239
240 write(pipeFd[1], &data, sizeof(data));
241 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
242 read(pipeFd[0], &data, sizeof(data));
243 EXPECT_EQ(data, OHOS::GSERROR_OK);
244 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
245 g_onBufferAvailable_ = 0;
246 close(pipeFd[0]);
247 close(pipeFd[1]);
248 if (thread.joinable()) {
249 thread.join();
250 }
251 producer = nullptr;
252 cSurface = nullptr;
253 }
254
ThreadNativeWindowProcess002(int32_t * pipeFd,sptr<IBufferProducer> producer)255 int32_t NativeWindowTest::ThreadNativeWindowProcess002(int32_t *pipeFd, sptr<IBufferProducer> producer)
256 {
257 int64_t data;
258 NativeWindow *nativeWindow = nullptr;
259 int32_t ret = CreateNativeWindowAndRequestBuffer002(producer, &nativeWindow);
260 if (ret != OHOS::GSERROR_OK) {
261 data = ret;
262 write(pipeFd[1], &data, sizeof(data));
263 return -1;
264 }
265
266 data = ret;
267 write(pipeFd[1], &data, sizeof(data));
268 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
269 read(pipeFd[0], &data, sizeof(data));
270 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
271 return 0;
272 }
273
ThreadNativeWindowProcess003(int32_t * pipeFd,uint64_t uniqueId)274 int32_t NativeWindowTest::ThreadNativeWindowProcess003(int32_t *pipeFd, uint64_t uniqueId)
275 {
276 int64_t data;
277 NativeWindow *nativeWindow = nullptr;
278 int32_t ret = CreateNativeWindowAndRequestBuffer003(uniqueId, &nativeWindow);
279 if (ret != OHOS::GSERROR_OK) {
280 data = ret;
281 write(pipeFd[1], &data, sizeof(data));
282 return -1;
283 }
284
285 data = ret;
286 write(pipeFd[1], &data, sizeof(data));
287 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
288 read(pipeFd[0], &data, sizeof(data));
289 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
290 return 0;
291 }
292
293 HWTEST_F(NativeWindowTest, OH_NativeWindow_CreateNativeWindow002, Function | MediumTest | Level2)
294 {
295 int32_t pipeFd[2] = {};
296 pipe(pipeFd);
297
298 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_CreateNativeWindow002");
299 cSurface->RegisterConsumerListener(this);
300 auto producer = cSurface->GetProducer();
301
__anon656395190302() 302 std::thread thread([this, pipeFd, producer]() {
303 int32_t ret = this->ThreadNativeWindowProcess002((int32_t*)(pipeFd), producer);
304 EXPECT_EQ(ret, OHOS::GSERROR_OK);
305 });
306
307 int64_t data = 0;
308 read(pipeFd[0], &data, sizeof(data));
309 EXPECT_EQ(data, OHOS::GSERROR_OK);
310
311 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
312 int32_t fence = -1;
313 int64_t timestamp;
314 Rect damage;
315 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
316 EXPECT_EQ(ret, OHOS::GSERROR_OK);
317 EXPECT_NE(buffer, nullptr);
318 EXPECT_EQ(GetData(buffer), true);
319
320 ret = cSurface->ReleaseBuffer(buffer, -1);
321 EXPECT_EQ(ret, OHOS::GSERROR_OK);
322
323 write(pipeFd[1], &data, sizeof(data));
324 close(pipeFd[0]);
325 close(pipeFd[1]);
326 if (thread.joinable()) {
327 thread.join();
328 }
329 producer = nullptr;
330 cSurface = nullptr;
331 }
332
CreateNativeWindowAndRequestBuffer002(sptr<IBufferProducer> producer,NativeWindow ** nativeWindow)333 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer002(sptr<IBufferProducer> producer,
334 NativeWindow **nativeWindow)
335 {
336 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
337 *nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
338 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
339
340 int32_t code = SET_BUFFER_GEOMETRY;
341 int32_t height = 0x100;
342 int32_t width = 0x100;
343 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
344 code = SET_FORMAT;
345 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
346 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, format);
347
348 int32_t fenceFd = -1;
349 auto ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
350 if (ret != OHOS::GSERROR_OK) {
351 return ret;
352 }
353 SetData(nativeWindowBuffer, *nativeWindow);
354
355 struct Region *region = new Region();
356 struct Region::Rect *rect = new Region::Rect();
357 rect->w = 0x100;
358 rect->h = 0x100;
359 region->rects = rect;
360 region->rectNumber = 1;
361 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
362 if (ret != OHOS::GSERROR_OK) {
363 delete rect;
364 delete region;
365 return ret;
366 }
367 delete rect;
368 delete region;
369 return OHOS::GSERROR_OK;
370 }
371
CreateNativeWindowAndRequestBuffer003(uint64_t uniqueId,NativeWindow ** nativeWindow)372 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer003(uint64_t uniqueId, NativeWindow **nativeWindow)
373 {
374 int32_t ret = OH_NativeWindow_CreateNativeWindowFromSurfaceId(uniqueId, nativeWindow);
375 if (ret != OHOS::GSERROR_OK) {
376 return ret;
377 }
378 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
379
380 int32_t code = SET_BUFFER_GEOMETRY;
381 int32_t height = 0x100;
382 int32_t width = 0x100;
383 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
384 code = SET_FORMAT;
385 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
386 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, format);
387
388 int32_t fenceFd = -1;
389 struct Region *region = new Region();
390 struct Region::Rect *rect = new Region::Rect();
391 rect->w = 0x100;
392 rect->h = 0x100;
393 region->rects = rect;
394 region->rectNumber = 1;
395 for (int32_t i = 0; i < QUEUE_SIZE; i++) {
396 ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
397 if (ret != OHOS::GSERROR_OK) {
398 delete rect;
399 delete region;
400 return ret;
401 }
402 SetData(nativeWindowBuffer, *nativeWindow);
403
404 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
405 if (ret != OHOS::GSERROR_OK) {
406 delete rect;
407 delete region;
408 return ret;
409 }
410 }
411 delete rect;
412 delete region;
413 return OHOS::GSERROR_OK;
414 }
415
416 HWTEST_F(NativeWindowTest, OH_NativeWindow_CreateNativeWindowFromSurfaceId001, Function | MediumTest | Level2)
417 {
418 int32_t pipeFd[2] = {};
419 pipe(pipeFd);
420
421 sptr<OHOS::IConsumerSurface> cSurface =
422 IConsumerSurface::Create("OH_NativeWindow_CreateNativeWindowFromSurfaceId001");
423 cSurface->RegisterConsumerListener(this);
424 auto producer = cSurface->GetProducer();
425 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
426 EXPECT_NE(pSurface, nullptr);
427 EXPECT_EQ(cSurface->SetQueueSize(QUEUE_SIZE), OHOS::GSERROR_OK);
428
429 uint64_t uniqueId = cSurface->GetUniqueId();
__anon656395190402() 430 std::thread thread([this, pipeFd, uniqueId]() {
431 int32_t ret = this->ThreadNativeWindowProcess003((int32_t*)(pipeFd), uniqueId);
432 EXPECT_EQ(ret, OHOS::GSERROR_OK);
433 });
434
435 int64_t data = 0;
436 read(pipeFd[0], &data, sizeof(data));
437 EXPECT_EQ(data, OHOS::GSERROR_OK);
438
439 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
440 int32_t fence = -1;
441 int64_t timestamp;
442 Rect damage;
443 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
444 while (g_onBufferAvailable_ > 0) {
445 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
446 EXPECT_EQ(ret, OHOS::GSERROR_OK);
447 EXPECT_NE(buffer, nullptr);
448 EXPECT_EQ(GetData(buffer), true);
449
450 ret = cSurface->ReleaseBuffer(buffer, -1);
451 EXPECT_EQ(ret, OHOS::GSERROR_OK);
452 g_onBufferAvailable_--;
453 }
454
455 g_onBufferAvailable_ = 0;
456 write(pipeFd[1], &data, sizeof(data));
457 close(pipeFd[0]);
458 close(pipeFd[1]);
459 if (thread.joinable()) {
460 thread.join();
461 }
462 producer = nullptr;
463 cSurface = nullptr;
464 }
465
RequestBuffer001(NativeWindow * nativeWindow)466 int32_t NativeWindowTest::RequestBuffer001(NativeWindow *nativeWindow)
467 {
468 int32_t fenceFd = -1;
469 struct Region *region = new Region();
470 struct Region::Rect *rect = new Region::Rect();
471 rect->w = 0x100;
472 rect->h = 0x100;
473 region->rects = rect;
474 region->rectNumber = 1;
475 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
476 int32_t ret;
477 for (int32_t i = 0; i < QUEUE_SIZE; i++) {
478 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
479 if (ret != OHOS::GSERROR_OK) {
480 delete rect;
481 delete region;
482 return ret;
483 }
484 SetData(nativeWindowBuffer, nativeWindow);
485
486 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, -1, *region);
487 if (ret != OHOS::GSERROR_OK) {
488 delete rect;
489 delete region;
490 return ret;
491 }
492 }
493 delete rect;
494 delete region;
495 return OHOS::GSERROR_OK;
496 }
497
498 HWTEST_F(NativeWindowTest, OH_NativeWindow_CreateNativeWindowFromSurfaceId002, Function | MediumTest | Level2)
499 {
500 int32_t pipeFd[2] = {};
501 pipe(pipeFd);
502
503 sptr<OHOS::IConsumerSurface> cSurface =
504 IConsumerSurface::Create("OH_NativeWindow_CreateNativeWindowFromSurfaceId002");
505 cSurface->RegisterConsumerListener(this);
506 auto producer = cSurface->GetProducer();
507 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
508 EXPECT_NE(pSurface, nullptr);
509 EXPECT_EQ(cSurface->SetQueueSize(QUEUE_SIZE), OHOS::GSERROR_OK);
510
511 uint64_t uniqueId = cSurface->GetUniqueId();
__anon656395190502() 512 std::thread thread([this, pipeFd, uniqueId]() {
513 int32_t ret = this->ThreadNativeWindowProcess004((int32_t*)(pipeFd), uniqueId);
514 EXPECT_EQ(ret, OHOS::GSERROR_OK);
515 });
516
517 int64_t data = 0;
518 read(pipeFd[0], &data, sizeof(data));
519 EXPECT_EQ(data, OHOS::GSERROR_OK);
520
521 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
522 int32_t fence = -1;
523 int64_t timestamp;
524 Rect damage;
525 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
526 while (g_onBufferAvailable_ > 0) {
527 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
528 EXPECT_EQ(ret, OHOS::GSERROR_OK);
529 EXPECT_NE(buffer, nullptr);
530 EXPECT_EQ(GetData(buffer), true);
531
532 ret = cSurface->ReleaseBuffer(buffer, -1);
533 EXPECT_EQ(ret, OHOS::GSERROR_OK);
534 g_onBufferAvailable_--;
535 }
536
537 write(pipeFd[1], &data, sizeof(data));
538 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
539 read(pipeFd[0], &data, sizeof(data));
540 EXPECT_EQ(data, OHOS::GSERROR_OK);
541 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
542 g_onBufferAvailable_ = 0;
543 close(pipeFd[0]);
544 close(pipeFd[1]);
545 if (thread.joinable()) {
546 thread.join();
547 }
548 producer = nullptr;
549 cSurface = nullptr;
550 }
551
CreateNativeWindowAndRequestBuffer004(uint64_t uniqueId,NativeWindow ** nativeWindow)552 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer004(uint64_t uniqueId, NativeWindow **nativeWindow)
553 {
554 int32_t ret = OH_NativeWindow_CreateNativeWindowFromSurfaceId(uniqueId, nativeWindow);
555 if (ret != OHOS::GSERROR_OK) {
556 return ret;
557 }
558 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
559
560 int32_t code = SET_BUFFER_GEOMETRY;
561 int32_t height = 0x100;
562 int32_t width = 0x100;
563 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
564 code = SET_FORMAT;
565 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
566 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, format);
567
568 int32_t fenceFd = -1;
569 struct Region *region = new Region();
570 struct Region::Rect *rect = new Region::Rect();
571 rect->w = 0x100;
572 rect->h = 0x100;
573 region->rects = rect;
574 region->rectNumber = 1;
575 for (int32_t i = 0; i < QUEUE_SIZE; i++) {
576 ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
577 if (ret != OHOS::GSERROR_OK) {
578 delete rect;
579 delete region;
580 return ret;
581 }
582 SetData(nativeWindowBuffer, *nativeWindow);
583
584 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
585 if (ret != OHOS::GSERROR_OK) {
586 delete rect;
587 delete region;
588 return ret;
589 }
590 }
591 delete rect;
592 delete region;
593 return OHOS::GSERROR_OK;
594 }
595
ThreadNativeWindowProcess005(int32_t * pipeFd,uint64_t uniqueId)596 int32_t NativeWindowTest::ThreadNativeWindowProcess005(int32_t *pipeFd, uint64_t uniqueId)
597 {
598 int64_t data;
599 NativeWindow *nativeWindow = nullptr;
600 int32_t ret = CreateNativeWindowAndRequestBuffer005(uniqueId, &nativeWindow);
601 if (ret != OHOS::GSERROR_OK) {
602 data = ret;
603 write(pipeFd[1], &data, sizeof(data));
604 return -1;
605 }
606
607 data = ret;
608 write(pipeFd[1], &data, sizeof(data));
609 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
610 read(pipeFd[0], &data, sizeof(data));
611 ret = RequestBuffer001(nativeWindow);
612 if (ret != OHOS::GSERROR_OK) {
613 data = ret;
614 write(pipeFd[1], &data, sizeof(data));
615 return -1;
616 }
617 data = ret;
618 write(pipeFd[1], &data, sizeof(data));
619 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
620 return 0;
621 }
622
CreateNativeWindowAndRequestBuffer005(uint64_t uniqueId,NativeWindow ** nativeWindow)623 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer005(uint64_t uniqueId, NativeWindow **nativeWindow)
624 {
625 int32_t ret = OH_NativeWindow_CreateNativeWindowFromSurfaceId(uniqueId, nativeWindow);
626 if (ret != OHOS::GSERROR_OK) {
627 return ret;
628 }
629
630 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
631 struct NativeWindowBuffer *lastNativeWindowBuffer = nullptr;
632
633 int32_t code = SET_BUFFER_GEOMETRY;
634 int32_t height = 0x100;
635 int32_t width = 0x100;
636 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
637 code = SET_FORMAT;
638 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
639 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, format);
640
641 int32_t fenceFd = -1;
642 struct Region *region = new Region();
643 struct Region::Rect *rect = new Region::Rect();
644 rect->w = 0x100;
645 rect->h = 0x100;
646 region->rects = rect;
647 region->rectNumber = 1;
648 float matrix[16] = {0};
649 for (int32_t i = 0; i < QUEUE_SIZE; i++) {
650 ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
651 if (ret != OHOS::GSERROR_OK) {
652 delete rect;
653 delete region;
654 return ret;
655 }
656 SetData(nativeWindowBuffer, *nativeWindow);
657
658 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
659 if (ret != OHOS::GSERROR_OK) {
660 delete rect;
661 delete region;
662 return ret;
663 }
664 ret = OH_NativeWindow_GetLastFlushedBufferV2(*nativeWindow, &lastNativeWindowBuffer, &fenceFd, matrix);
665 if (ret != OHOS::GSERROR_OK) {
666 delete rect;
667 delete region;
668 return ret;
669 }
670 if (!GetData(lastNativeWindowBuffer->sfbuffer)) {
671 delete rect;
672 delete region;
673 return -1;
674 }
675 }
676 delete rect;
677 delete region;
678 return OHOS::GSERROR_OK;
679 }
680
681 HWTEST_F(NativeWindowTest, OH_NativeWindow_GetLastFlushedBufferV2001, Function | MediumTest | Level2)
682 {
683 int32_t pipeFd[2] = {};
684 pipe(pipeFd);
685
686 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_GetLastFlushedBufferV2001");
687 cSurface->RegisterConsumerListener(this);
688 auto producer = cSurface->GetProducer();
689 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
690 EXPECT_NE(pSurface, nullptr);
691 EXPECT_EQ(cSurface->SetQueueSize(QUEUE_SIZE), OHOS::GSERROR_OK);
692
693 uint64_t uniqueId = cSurface->GetUniqueId();
__anon656395190602() 694 std::thread thread([this, pipeFd, uniqueId]() {
695 int32_t ret = this->ThreadNativeWindowProcess005((int32_t*)(pipeFd), uniqueId);
696 EXPECT_EQ(ret, OHOS::GSERROR_OK);
697 });
698
699 int64_t data = 0;
700 read(pipeFd[0], &data, sizeof(data));
701 EXPECT_EQ(data, OHOS::GSERROR_OK);
702
703 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
704 int32_t fence = -1;
705 int64_t timestamp;
706 Rect damage;
707 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
708 while (g_onBufferAvailable_ > 0) {
709 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
710 EXPECT_EQ(ret, OHOS::GSERROR_OK);
711 EXPECT_NE(buffer, nullptr);
712 EXPECT_EQ(GetData(buffer), true);
713
714 ret = cSurface->ReleaseBuffer(buffer, -1);
715 EXPECT_EQ(ret, OHOS::GSERROR_OK);
716 g_onBufferAvailable_--;
717 }
718
719 write(pipeFd[1], &data, sizeof(data));
720 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
721 read(pipeFd[0], &data, sizeof(data));
722 EXPECT_EQ(data, OHOS::GSERROR_OK);
723 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
724 g_onBufferAvailable_ = 0;
725 close(pipeFd[0]);
726 close(pipeFd[1]);
727 if (thread.joinable()) {
728 thread.join();
729 }
730 producer = nullptr;
731 cSurface = nullptr;
732 }
733
734 HWTEST_F(NativeWindowTest, OH_NativeWindow_NativeObjectReference001, Function | MediumTest | Level2)
735 {
736 int32_t pipeFd[2] = {};
737 pipe(pipeFd);
738
739 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_NativeObjectReference001");
740 cSurface->RegisterConsumerListener(this);
741 auto producer = cSurface->GetProducer();
742 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
743 EXPECT_NE(pSurface, nullptr);
744 EXPECT_EQ(cSurface->SetQueueSize(QUEUE_SIZE), OHOS::GSERROR_OK);
745
746 uint64_t uniqueId = cSurface->GetUniqueId();
__anon656395190702() 747 std::thread thread([this, pipeFd, uniqueId]() {
748 int32_t ret = this->ThreadNativeWindowProcess006((int32_t*)(pipeFd), uniqueId);
749 EXPECT_EQ(ret, OHOS::GSERROR_OK);
750 });
751
752 int64_t data = 0;
753 read(pipeFd[0], &data, sizeof(data));
754 EXPECT_EQ(data, OHOS::GSERROR_OK);
755
756 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
757 int32_t fence = -1;
758 int64_t timestamp;
759 Rect damage;
760 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
761 while (g_onBufferAvailable_ > 0) {
762 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
763 EXPECT_EQ(ret, OHOS::GSERROR_OK);
764 EXPECT_NE(buffer, nullptr);
765 EXPECT_EQ(GetData(buffer), true);
766
767 ret = cSurface->ReleaseBuffer(buffer, -1);
768 EXPECT_EQ(ret, OHOS::GSERROR_OK);
769 g_onBufferAvailable_--;
770 }
771
772 write(pipeFd[1], &data, sizeof(data));
773 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
774 read(pipeFd[0], &data, sizeof(data));
775 EXPECT_EQ(data, OHOS::GSERROR_OK);
776 EXPECT_EQ(g_onBufferAvailable_, QUEUE_SIZE);
777 g_onBufferAvailable_ = 0;
778 close(pipeFd[0]);
779 close(pipeFd[1]);
780 if (thread.joinable()) {
781 thread.join();
782 }
783 producer = nullptr;
784 cSurface = nullptr;
785 }
786
ThreadNativeWindowProcess007(int32_t * pipeFd,sptr<IBufferProducer> producer,uint64_t * uniqueId)787 int32_t NativeWindowTest::ThreadNativeWindowProcess007(int32_t *pipeFd,
788 sptr<IBufferProducer> producer, uint64_t *uniqueId)
789 {
790 int64_t data;
791 NativeWindow *nativeWindow = nullptr;
792 int32_t ret = CreateNativeWindowAndRequestBuffer007(producer, &nativeWindow);
793 if (ret != OHOS::GSERROR_OK) {
794 data = ret;
795 write(pipeFd[1], &data, sizeof(data));
796 return -1;
797 }
798 ret = OH_NativeWindow_GetSurfaceId(nativeWindow, uniqueId);
799 if (ret != OHOS::GSERROR_OK) {
800 data = ret;
801 write(pipeFd[1], &data, sizeof(data));
802 return -1;
803 }
804
805 data = ret;
806 write(pipeFd[1], &data, sizeof(data));
807 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
808 read(pipeFd[0], &data, sizeof(data));
809 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
810 return 0;
811 }
812
ThreadNativeWindowProcess006(int32_t * pipeFd,uint64_t uniqueId)813 int32_t NativeWindowTest::ThreadNativeWindowProcess006(int32_t *pipeFd, uint64_t uniqueId)
814 {
815 int64_t data;
816 NativeWindow *nativeWindow = nullptr;
817 int32_t ret = CreateNativeWindowAndRequestBuffer005(uniqueId, &nativeWindow);
818 if (ret != OHOS::GSERROR_OK) {
819 data = ret;
820 write(pipeFd[1], &data, sizeof(data));
821 return -1;
822 }
823 ret = OH_NativeWindow_NativeObjectReference(nativeWindow);
824 if (ret != OHOS::GSERROR_OK) {
825 data = ret;
826 write(pipeFd[1], &data, sizeof(data));
827 return -1;
828 }
829 OH_NativeWindow_DestroyNativeWindow(nativeWindow);
830
831 data = ret;
832 write(pipeFd[1], &data, sizeof(data));
833 usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
834 read(pipeFd[0], &data, sizeof(data));
835 ret = RequestBuffer001(nativeWindow);
836 if (ret != OHOS::GSERROR_OK) {
837 data = ret;
838 write(pipeFd[1], &data, sizeof(data));
839 return -1;
840 }
841 data = ret;
842 write(pipeFd[1], &data, sizeof(data));
843 ret = OH_NativeWindow_NativeObjectUnreference(nativeWindow);
844 if (ret != OHOS::GSERROR_OK) {
845 return -1;
846 }
847 return 0;
848 }
849
CreateNativeWindowAndRequestBuffer007(sptr<IBufferProducer> producer,NativeWindow ** nativeWindow)850 int32_t NativeWindowTest::CreateNativeWindowAndRequestBuffer007(sptr<IBufferProducer> producer,
851 NativeWindow **nativeWindow)
852 {
853 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
854 *nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
855 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
856
857 int32_t code = SET_BUFFER_GEOMETRY;
858 int32_t height = 0x100;
859 int32_t width = 0x100;
860 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, height, width);
861 code = SET_FORMAT;
862 int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
863 OH_NativeWindow_NativeWindowHandleOpt(*nativeWindow, code, format);
864
865 int32_t fenceFd = -1;
866 auto ret = OH_NativeWindow_NativeWindowRequestBuffer(*nativeWindow, &nativeWindowBuffer, &fenceFd);
867 if (ret != OHOS::GSERROR_OK) {
868 return ret;
869 }
870 SetData(nativeWindowBuffer, *nativeWindow);
871
872 struct Region *region = new Region();
873 struct Region::Rect *rect = new Region::Rect();
874 rect->w = 0x100;
875 rect->h = 0x100;
876 region->rects = rect;
877 region->rectNumber = 1;
878 ret = OH_NativeWindow_NativeWindowFlushBuffer(*nativeWindow, nativeWindowBuffer, -1, *region);
879 if (ret != OHOS::GSERROR_OK) {
880 delete rect;
881 delete region;
882 return ret;
883 }
884 delete rect;
885 delete region;
886 return OHOS::GSERROR_OK;
887 }
888
889 HWTEST_F(NativeWindowTest, OH_NativeWindow_GetSurfaceId001, Function | MediumTest | Level2)
890 {
891 int32_t pipeFd[2] = {};
892 pipe(pipeFd);
893
894 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("OH_NativeWindow_CreateNativeWindow002");
895 cSurface->RegisterConsumerListener(this);
896 auto producer = cSurface->GetProducer();
897 uint64_t cUniqueId = cSurface->GetUniqueId();
898
__anon656395190802() 899 std::thread thread([this, pipeFd, producer, cUniqueId]() {
900 uint64_t uniqueId = 0;
901 int32_t ret = this->ThreadNativeWindowProcess007((int32_t*)(pipeFd), producer, &uniqueId);
902 EXPECT_EQ(ret, OHOS::GSERROR_OK);
903 EXPECT_EQ(uniqueId, cUniqueId);
904 });
905
906 int64_t data = 0;
907 read(pipeFd[0], &data, sizeof(data));
908 EXPECT_EQ(data, OHOS::GSERROR_OK);
909
910 OHOS::sptr<SurfaceBuffer> buffer = nullptr;
911 int32_t fence = -1;
912 int64_t timestamp;
913 Rect damage;
914 auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
915 EXPECT_EQ(ret, OHOS::GSERROR_OK);
916 EXPECT_NE(buffer, nullptr);
917 EXPECT_EQ(GetData(buffer), true);
918
919 ret = cSurface->ReleaseBuffer(buffer, -1);
920 EXPECT_EQ(ret, OHOS::GSERROR_OK);
921
922 write(pipeFd[1], &data, sizeof(data));
923 close(pipeFd[0]);
924 close(pipeFd[1]);
925 if (thread.joinable()) {
926 thread.join();
927 }
928 producer = nullptr;
929 cSurface = nullptr;
930 }
931
CreateNativeWindowAndAttachBuffer001(NativeWindow * nativeWindow)932 int32_t NativeWindowTest::CreateNativeWindowAndAttachBuffer001(NativeWindow *nativeWindow)
933 {
934 sptr<OnBufferAvailableTest> onBufferAvailableTest = new OnBufferAvailableTest();
935 sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create("CreateNativeWindowAndAttachBuffer001");
936 cSurface->RegisterConsumerListener(onBufferAvailableTest);
937 auto producer = cSurface->GetProducer();
938 cSurface->SetQueueSize(QUEUE_SIZE);
939 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
940 if (pSurface == nullptr) {
941 return -1;
942 }
943
944 NativeWindow *nativeWindowNew = OH_NativeWindow_CreateNativeWindow(&pSurface);
945 if (nativeWindowNew == nullptr) {
946 return -1;
947 }
948 int32_t code = SET_BUFFER_GEOMETRY;
949 int32_t height = 0x100;
950 int32_t width = 0x100;
951 OH_NativeWindow_NativeWindowHandleOpt(nativeWindowNew, code, height, width);
952 int32_t fenceFd = -1;
953 struct Region *region = new Region();
954 struct Region::Rect *rect = new Region::Rect();
955 rect->w = 0x100;
956 rect->h = 0x100;
957 region->rects = rect;
958 region->rectNumber = 1;
959 struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
960 int32_t ret;
961 for (int32_t i = 0; i < QUEUE_SIZE; i++) {
962 ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindowNew, &nativeWindowBuffer, &fenceFd);
963 if (ret != OHOS::GSERROR_OK) {
964 delete rect;
965 delete region;
966 return ret;
967 }
968 SetData(nativeWindowBuffer, nativeWindowNew);
969 ret = OH_NativeWindow_NativeWindowDetachBuffer(nativeWindowNew, nativeWindowBuffer);
970 if (ret != OHOS::GSERROR_OK) {
971 delete rect;
972 delete region;
973 return ret;
974 }
975 ret = OH_NativeWindow_NativeWindowAttachBuffer(nativeWindow, nativeWindowBuffer);
976 if (ret != OHOS::GSERROR_OK) {
977 delete rect;
978 delete region;
979 return ret;
980 }
981 ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, -1, *region);
982 if (ret != OHOS::GSERROR_OK) {
983 delete rect;
984 delete region;
985 return ret;
986 }
987 }
988 delete rect;
989 delete region;
990
991 OH_NativeWindow_DestroyNativeWindow(nativeWindowNew);
992 return OHOS::GSERROR_OK;
993 }
994 }
995