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
16 #include <map>
17 #include <gtest/gtest.h>
18
19 #include <surface.h>
20 #include <display_type.h>
21 #include "buffer_extra_data_impl.h"
22 #include "buffer_queue.h"
23 #include "buffer_consumer_listener.h"
24 #include "buffer_manager.h"
25 #include "buffer_log.h"
26 #include "test_header.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS::Rosen {
32 class BufferSharedTest : public testing::Test, public IBufferConsumerListenerClazz {
33 public:
34 static void SetUpTestCase();
35 virtual void OnBufferAvailable() override;
36 static void TearDownTestCase();
37
38 static inline sptr<Surface> surf = nullptr;
39 static inline sptr<Surface> producerSurface1 = nullptr;
40 static inline sptr<Surface> producerSurface2 = nullptr;
41 static inline sptr<SurfaceBuffer> buffer1 = nullptr;
42 static inline sptr<SurfaceBuffer> buffer2 = nullptr;
43 static inline sptr<SurfaceBuffer> sbuffer1 = nullptr;
44 static inline sptr<SurfaceBuffer> sbuffer2 = nullptr;
45 };
46
SetUpTestCase()47 void BufferSharedTest::SetUpTestCase()
48 {
49 GTEST_LOG_(INFO) << getpid() << std::endl;
50 surf = Surface::CreateSurfaceAsConsumer("shared", true);
51 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
52 surf->RegisterConsumerListener(listener);
53 auto producer1 = surf->GetProducer();
54 producerSurface1 = Surface::CreateSurfaceAsProducer(producer1);
55 auto producer2 = surf->GetProducer();
56 producerSurface2 = Surface::CreateSurfaceAsProducer(producer2);
57 }
58
OnBufferAvailable()59 void BufferSharedTest::OnBufferAvailable() {}
60
TearDownTestCase()61 void BufferSharedTest::TearDownTestCase()
62 {
63 }
64
65 /*
66 * Function: RequestBuffer
67 * Type: Reliability
68 * Rank: Important(2)
69 * EnvConditions: N/A
70 * CaseDescription: 1. call RequestBuffer with buffer=buffer1, buffer2,the param is same
71 * 2. check ret1 and ret2 are OHOS::GSERROR_OK, check buffer1 and buffer2 is not nullptr
72 * 3. check the addr of buffer1 EQ buffer2
73 * */
74
75 HWTEST_F(BufferSharedTest, RequestBuffer001, Function | MediumTest | Level2)
76 {
77 PART("REQUEST BUFFER TWO TIMES") {
78 GSError ret1, ret2;
79 STEP("1: request buffer") {
80 BufferRequestConfig requestConfig = {
81 .width = 0x100,
82 .height = 0x100,
83 .strideAlignment = 0x8,
84 .format = PIXEL_FMT_RGBA_8888,
85 .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
86 .timeout = 0,
87 };
88 int releaseFence = -1;
89 ret1 = producerSurface1->RequestBuffer(buffer1, releaseFence, requestConfig);
90 ret2 = producerSurface2->RequestBuffer(buffer2, releaseFence, requestConfig);
91 }
92 STEP("2: check ret1 ret2 buffer1 buffer2") {
93 STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
94 STEP_ASSERT_NE(buffer1, nullptr);
95 STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
96 STEP_ASSERT_NE(buffer2, nullptr);
97 }
98 STEP("3: check buffer addr") {
99 STEP_ASSERT_EQ(buffer2, buffer1);
100 }
101 }
102 }
103 /*
104 * Function: RequestBuffer with different requestconfig
105 * Type: Reliability
106 * Rank: Important(2)
107 * EnvConditions: N/A
108 * CaseDescription: 1. call RequestBuffer with buffer=bufferDiff,
109 * the requestconfig is not same with buffer1
110 * 2. check ret1 is GSERROR_INVALID_ARGUMENTS
111 * */
112
113 HWTEST_F(BufferSharedTest, RequestBufferDiff001, Function | MediumTest | Level2)
114 {
115 PART("REQUEST BUFFER with different requestconfig") {
116 GSError ret1;
117 sptr<SurfaceBuffer> bufferDiff = nullptr;
118 STEP("1: request buffer") {
119 BufferRequestConfig diffRequestConfig = {
120 .width = 0x200,
121 .height = 0x100,
122 .strideAlignment = 0x8,
123 .format = PIXEL_FMT_RGBA_8888,
124 .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
125 .timeout = 0,
126 };
127 int releaseFence = -1;
128 ret1 = producerSurface1->RequestBuffer(bufferDiff, releaseFence, diffRequestConfig);
129 }
130 STEP("2: check ret1") {
131 STEP_ASSERT_EQ(ret1, GSERROR_INVALID_ARGUMENTS);
132 }
133 }
134 }
135 /*
136 * Function: FlushBuffer
137 * Type: Reliability
138 * Rank: Important(2)
139 * EnvConditions: N/A
140 * CaseDescription: 1. call FlushBuffer with buffer=buffer1, buffer2
141 * 2. check ret1 and ret2 is OHOS::GSERROR_OK
142 * */
143 HWTEST_F(BufferSharedTest, FlushBuffer001, Function | MediumTest | Level2)
144 {
145 PART("FlushBuffer") {
146 GSError ret1, ret2;
147 STEP("1: FlushBuffer two times") {
148 BufferFlushConfig flushConfig = { .damage = { .w = 0x100, .h = 0x100, }, };
149 ret1 = producerSurface1->FlushBuffer(buffer1, -1, flushConfig);
150 ret2 = producerSurface2->FlushBuffer(buffer2, -1, flushConfig);
151 }
152 STEP("2: check ret1 ret2") {
153 STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
154 STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
155 }
156 }
157 }
158 /*
159 * Function: AquiredBuffer
160 * Type: Reliability
161 * Rank: Important(2)
162 * EnvConditions: N/A
163 * CaseDescription: 1. call AcquireBuffer with buffer=sbuffer1, sbuffer2
164 * 2. check ret1 and ret2 are GSERROR_INVALID_ARGUMENTS
165 * */
166 HWTEST_F(BufferSharedTest, AquiredBuffer001, Function | MediumTest | Level2)
167 {
168 PART("AquiredBuffer") {
169 GSError ret1, ret2;
170 STEP("1: AcquireBuffer two times") {
171 int64_t timestamp = 0;
172 Rect damage = {};
173 int32_t fence = -1;
174
175 ret1 = surf->AcquireBuffer(sbuffer1, fence, timestamp, damage);
176 ret2 = surf->AcquireBuffer(sbuffer2, fence, timestamp, damage);
177 }
178 STEP("2: check ret1 ret2") {
179 STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
180 STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
181 }
182 STEP("3: check addr sbuffer1 and sbuffer2") {
183 STEP_ASSERT_EQ(sbuffer1, sbuffer2);
184 }
185 }
186 }
187 /*
188 * Function: CancelBuffer
189 * Type: Reliability
190 * Rank: Important(2)
191 * EnvConditions: N/A
192 * CaseDescription: 1. call cancelBuffer with buffer=buffer1
193 * 2. check ret1 is GSERROR_INVALID_OPERATING
194 * 3. call cancelBuffer with buffer=buffer2
195 * 4. check ret2 is GSERROR_INVALID_OPERATING
196 * */
197 HWTEST_F(BufferSharedTest, CancelBuffer001, Function | MediumTest | Level2)
198 {
199 PART("CancelBuffer") {
200 GSError ret1, ret2;
201 STEP("1: Cancel buffer1") {
202 ret1 = producerSurface1->CancelBuffer(buffer1);
203 }
204 STEP("2: check ret1") {
205 STEP_ASSERT_EQ(ret1, GSERROR_INVALID_OPERATING);
206 }
207 STEP("3: Cancel buffer2") {
208 ret2 = producerSurface2->CancelBuffer(buffer2);
209 }
210 STEP("4: check ret2") {
211 STEP_ASSERT_EQ(ret2, GSERROR_INVALID_OPERATING);
212 }
213 }
214 }
215 /*
216 * Function: RelaseBuffer
217 * Type: Reliability
218 * Rank: Important(2)
219 * EnvConditions: N/A
220 * CaseDescription: 1. releaseBuffer two times
221 * 2. check ret1 is GSERROR_INVALID_OPERATING, check ret1 is OHOS::GSERROR_OK
222 * */
223 HWTEST_F(BufferSharedTest, ReleaseBuffer001, Function | MediumTest | Level2)
224 {
225 PART("ReleaseBuffer") {
226 GSError ret1, ret2;
227 STEP("1: releaseBuffer two times") {
228 ret1 = surf->ReleaseBuffer(sbuffer1, -1);
229 ret2 = surf->ReleaseBuffer(sbuffer2, -1);
230 }
231 STEP("2: check ret1, ret2") {
232 STEP_ASSERT_EQ(ret1, OHOS::GSERROR_OK);
233 STEP_ASSERT_EQ(ret2, OHOS::GSERROR_OK);
234 }
235 }
236 }
237 }
238