1 /*
2 * Copyright (c) 2021 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 <gtest/gtest.h>
16 #include <surface.h>
17 #include <buffer_queue_consumer.h>
18 #include "buffer_consumer_listener.h"
19 #include "buffer_extra_data_impl.h"
20 #include "sync_fence.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Rosen {
26 class BufferQueueConsumerTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30
31 static inline BufferRequestConfig requestConfig = {
32 .width = 0x100,
33 .height = 0x100,
34 .strideAlignment = 0x8,
35 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
36 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
37 .timeout = 0,
38 };
39 static inline BufferFlushConfigWithDamages flushConfig = {
40 .damages = {
41 {
42 .w = 0x100,
43 .h = 0x100,
44 }
45 },
46 };
47 static inline int64_t timestamp = 0;
48 static inline std::vector<Rect> damages = {};
49 static inline sptr<BufferQueue> bq = nullptr;
50 static inline sptr<BufferQueueConsumer> bqc = nullptr;
51 static inline sptr<BufferExtraData> bedata = nullptr;
52 };
53
SetUpTestCase()54 void BufferQueueConsumerTest::SetUpTestCase()
55 {
56 bq = new BufferQueue("test");
57 bq->Init();
58 bqc = new BufferQueueConsumer(bq);
59 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
60 bqc->RegisterConsumerListener(listener);
61 bedata = new BufferExtraDataImpl;
62 }
63
TearDownTestCase()64 void BufferQueueConsumerTest::TearDownTestCase()
65 {
66 bq = nullptr;
67 bqc = nullptr;
68 }
69
70 /*
71 * Function: AcquireBuffer and ReleaseBuffer
72 * Type: Function
73 * Rank: Important(2)
74 * EnvConditions: N/A
75 * CaseDescription: 1. call RequestBuffer and FlushBuffer
76 * 2. call AcquireBuffer and ReleaseBuffer
77 * 3. check ret
78 */
79 HWTEST_F(BufferQueueConsumerTest, AcqRel001, Function | MediumTest | Level2)
80 {
81 IBufferProducer::RequestBufferReturnValue retval;
82 GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
83 ASSERT_EQ(ret, OHOS::GSERROR_OK);
84 ASSERT_GE(retval.sequence, 0);
85 ASSERT_NE(retval.buffer, nullptr);
86
87 uint8_t *addr1 = reinterpret_cast<uint8_t*>(retval.buffer->GetVirAddr());
88 ASSERT_NE(addr1, nullptr);
89
90 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
91 ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
92 ASSERT_EQ(ret, OHOS::GSERROR_OK);
93
94 ret = bqc->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
95 ASSERT_EQ(ret, OHOS::GSERROR_OK);
96
97 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
98 ret = bqc->ReleaseBuffer(retval.buffer, releaseFence);
99 ASSERT_EQ(ret, OHOS::GSERROR_OK);
100 }
101
102 /*
103 * Function: AcquireBuffer and ReleaseBuffer
104 * Type: Function
105 * Rank: Important(2)
106 * EnvConditions: N/A
107 * CaseDescription: 1. call RequestBuffer and FlushBuffer
108 * 2. call AcquireBuffer and ReleaseBuffer
109 * 3. call ReleaseBuffer again
110 * 4. check ret
111 */
112 HWTEST_F(BufferQueueConsumerTest, AcqRel002, Function | MediumTest | Level2)
113 {
114 IBufferProducer::RequestBufferReturnValue retval;
115 GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
116 ASSERT_EQ(ret, OHOS::GSERROR_OK);
117 ASSERT_GE(retval.sequence, 0);
118 ASSERT_EQ(retval.buffer, nullptr);
119
120 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
121 ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
122 ASSERT_EQ(ret, OHOS::GSERROR_OK);
123
124 sptr<SurfaceBuffer>& buffer = retval.buffer;
125 ret = bqc->AcquireBuffer(buffer, retval.fence, timestamp, damages);
126 ASSERT_EQ(ret, OHOS::GSERROR_OK);
127
128 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
129 ret = bqc->ReleaseBuffer(buffer, releaseFence);
130 ASSERT_EQ(ret, OHOS::GSERROR_OK);
131
132 ret = bqc->ReleaseBuffer(buffer, releaseFence);
133 ASSERT_NE(ret, OHOS::GSERROR_OK);
134 }
135 }
136