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_extra_data_impl.h>
18 #include <buffer_queue_producer.h>
19 #include "buffer_consumer_listener.h"
20 #include "sync_fence.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Rosen {
26 class BufferQueueProducerTest : 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 std::vector<int32_t> deletingBuffers;
48 static inline int64_t timestamp = 0;
49 static inline std::vector<Rect> damages = {};
50 static inline sptr<BufferQueue> bq = nullptr;
51 static inline sptr<BufferQueueProducer> bqp = nullptr;
52 static inline sptr<BufferExtraData> bedata = nullptr;
53 };
54
SetUpTestCase()55 void BufferQueueProducerTest::SetUpTestCase()
56 {
57 bq = new BufferQueue("test");
58 bq->Init();
59 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
60 bq->RegisterConsumerListener(listener);
61 bqp = new BufferQueueProducer(bq);
62 bedata = new OHOS::BufferExtraDataImpl;
63 }
64
TearDownTestCase()65 void BufferQueueProducerTest::TearDownTestCase()
66 {
67 bq = nullptr;
68 bqp = nullptr;
69 }
70
71 /*
72 * Function: SetQueueSize and GetQueueSize
73 * Type: Function
74 * Rank: Important(2)
75 * EnvConditions: N/A
76 * CaseDescription: 1. call GetQueueSize and get default value
77 * 2. call SetQueueSize
78 * 3. call SetQueueSize again with abnormal value
79 * 4. call GetQueueSize for BufferQueueProducer and BufferQueue
80 * 5. check ret
81 */
82 HWTEST_F(BufferQueueProducerTest, QueueSize001, Function | MediumTest | Level2)
83 {
84 ASSERT_EQ(bqp->GetQueueSize(), (uint32_t)SURFACE_DEFAULT_QUEUE_SIZE);
85
86 GSError ret = bqp->SetQueueSize(2);
87 ASSERT_EQ(ret, OHOS::GSERROR_OK);
88
89 ret = bqp->SetQueueSize(SURFACE_MAX_QUEUE_SIZE + 1);
90 ASSERT_NE(ret, OHOS::GSERROR_OK);
91
92 ASSERT_EQ(bqp->GetQueueSize(), 2u);
93 ASSERT_EQ(bq->GetQueueSize(), 2u);
94 }
95
96 /*
97 * Function: RequestBuffer and CancelBuffer
98 * Type: Function
99 * Rank: Important(2)
100 * EnvConditions: N/A
101 * CaseDescription: 1. call RequestBuffer
102 * 2. call CancelBuffer
103 * 3. check ret
104 */
105 HWTEST_F(BufferQueueProducerTest, ReqCan001, Function | MediumTest | Level2)
106 {
107 IBufferProducer::RequestBufferReturnValue retval;
108 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
109 ASSERT_EQ(ret, OHOS::GSERROR_OK);
110
111 ret = bqp->CancelBuffer(retval.sequence, bedata);
112 ASSERT_EQ(ret, OHOS::GSERROR_OK);
113 }
114
115 /*
116 * Function: RequestBuffer and CancelBuffer
117 * Type: Function
118 * Rank: Important(2)
119 * EnvConditions: N/A
120 * CaseDescription: 1. call RequestBuffer
121 * 2. call CancelBuffer 2 times
122 * 3. check ret
123 */
124 HWTEST_F(BufferQueueProducerTest, ReqCan002, Function | MediumTest | Level2)
125 {
126 IBufferProducer::RequestBufferReturnValue retval;
127 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
128 ASSERT_EQ(ret, OHOS::GSERROR_OK);
129
130 ret = bqp->CancelBuffer(retval.sequence, bedata);
131 ASSERT_EQ(ret, OHOS::GSERROR_OK);
132
133 ret = bqp->CancelBuffer(retval.sequence, bedata);
134 ASSERT_NE(ret, OHOS::GSERROR_OK);
135 }
136
137 /*
138 * Function: RequestBuffer, and CancelBuffer
139 * Type: Function
140 * Rank: Important(2)
141 * EnvConditions: N/A
142 * CaseDescription: 1. call RequestBuffer and CancelBuffer by different retval
143 * 2. check ret
144 */
145 HWTEST_F(BufferQueueProducerTest, ReqCan003, Function | MediumTest | Level2)
146 {
147 IBufferProducer::RequestBufferReturnValue retval1;
148 IBufferProducer::RequestBufferReturnValue retval2;
149 IBufferProducer::RequestBufferReturnValue retval3;
150
151 auto ret = bqp->RequestBuffer(requestConfig, bedata, retval1);
152 ASSERT_EQ(ret, OHOS::GSERROR_OK);
153 ASSERT_EQ(retval1.buffer, nullptr);
154
155 ret = bqp->RequestBuffer(requestConfig, bedata, retval2);
156 ASSERT_EQ(ret, OHOS::GSERROR_OK);
157 ASSERT_NE(retval2.buffer, nullptr);
158
159 ret = bqp->RequestBuffer(requestConfig, bedata, retval3);
160 ASSERT_NE(ret, OHOS::GSERROR_OK);
161 ASSERT_EQ(retval3.buffer, nullptr);
162
163 ret = bqp->CancelBuffer(retval1.sequence, bedata);
164 ASSERT_EQ(ret, OHOS::GSERROR_OK);
165
166 ret = bqp->CancelBuffer(retval2.sequence, bedata);
167 ASSERT_EQ(ret, OHOS::GSERROR_OK);
168
169 ret = bqp->CancelBuffer(retval3.sequence, bedata);
170 ASSERT_NE(ret, OHOS::GSERROR_OK);
171 }
172
173 /*
174 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
175 * Type: Function
176 * Rank: Important(2)
177 * EnvConditions: N/A
178 * CaseDescription: 1. call RequestBuffer and FlushBuffer
179 * 2. call AcquireBuffer and ReleaseBuffer
180 * 3. check ret
181 */
182 HWTEST_F(BufferQueueProducerTest, ReqFlu001, Function | MediumTest | Level2)
183 {
184 IBufferProducer::RequestBufferReturnValue retval;
185 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
186 ASSERT_EQ(ret, OHOS::GSERROR_OK);
187
188 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
189 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
190 ASSERT_EQ(ret, OHOS::GSERROR_OK);
191
192 ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
193 ASSERT_EQ(ret, OHOS::GSERROR_OK);
194
195 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
196 ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
197 ASSERT_EQ(ret, OHOS::GSERROR_OK);
198 }
199
200 /*
201 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
202 * Type: Function
203 * Rank: Important(2)
204 * EnvConditions: N/A
205 * CaseDescription: 1. call RequestBuffer and FlushBuffer
206 * 2. call FlushBuffer again
207 * 3. call AcquireBuffer and ReleaseBuffer
208 * 4. check ret
209 */
210 HWTEST_F(BufferQueueProducerTest, ReqFlu002, Function | MediumTest | Level2)
211 {
212 IBufferProducer::RequestBufferReturnValue retval;
213 GSError ret = bqp->RequestBuffer(requestConfig, bedata, retval);
214 ASSERT_EQ(ret, OHOS::GSERROR_OK);
215
216 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
217 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
218 ASSERT_EQ(ret, OHOS::GSERROR_OK);
219
220 ret = bqp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
221 ASSERT_NE(ret, OHOS::GSERROR_OK);
222
223 ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
224 ASSERT_EQ(ret, OHOS::GSERROR_OK);
225
226 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
227 ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
228 ASSERT_EQ(ret, OHOS::GSERROR_OK);
229 }
230 }
231