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