• 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 <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 BufferFlushConfig flushConfig = {
40         .damage = {
41             .w = 0x100,
42             .h = 0x100,
43         },
44     };
45     static inline int64_t timestamp = 0;
46     static inline Rect damage = {};
47     static inline sptr<BufferQueue> bq = nullptr;
48     static inline sptr<BufferQueueConsumer> bqc = nullptr;
49     static inline sptr<BufferExtraData> bedata = nullptr;
50 };
51 
SetUpTestCase()52 void BufferQueueConsumerTest::SetUpTestCase()
53 {
54     bq = new BufferQueue("test");
55     bq->Init();
56     bqc = new BufferQueueConsumer(bq);
57     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
58     bqc->RegisterConsumerListener(listener);
59     bedata = new BufferExtraDataImpl;
60 }
61 
TearDownTestCase()62 void BufferQueueConsumerTest::TearDownTestCase()
63 {
64     bq = nullptr;
65     bqc = nullptr;
66 }
67 
68 /*
69 * Function: AcquireBuffer and ReleaseBuffer
70 * Type: Function
71 * Rank: Important(2)
72 * EnvConditions: N/A
73 * CaseDescription: 1. call RequestBuffer and FlushBuffer
74 *                  2. call AcquireBuffer and ReleaseBuffer
75 *                  3. check ret
76  */
77 HWTEST_F(BufferQueueConsumerTest, AcqRel001, Function | MediumTest | Level2)
78 {
79     IBufferProducer::RequestBufferReturnValue retval;
80     GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
81     ASSERT_EQ(ret, OHOS::GSERROR_OK);
82     ASSERT_GE(retval.sequence, 0);
83     ASSERT_NE(retval.buffer, nullptr);
84 
85     uint8_t *addr1 = reinterpret_cast<uint8_t*>(retval.buffer->GetVirAddr());
86     ASSERT_NE(addr1, nullptr);
87 
88     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
89     ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
90     ASSERT_EQ(ret, OHOS::GSERROR_OK);
91 
92     ret = bqc->AcquireBuffer(retval.buffer, retval.fence, timestamp, damage);
93     ASSERT_EQ(ret, OHOS::GSERROR_OK);
94 
95     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
96     ret = bqc->ReleaseBuffer(retval.buffer, releaseFence);
97     ASSERT_EQ(ret, OHOS::GSERROR_OK);
98 }
99 
100 /*
101 * Function: AcquireBuffer and ReleaseBuffer
102 * Type: Function
103 * Rank: Important(2)
104 * EnvConditions: N/A
105 * CaseDescription: 1. call RequestBuffer and FlushBuffer
106 *                  2. call AcquireBuffer and ReleaseBuffer
107 *                  3. call ReleaseBuffer again
108 *                  4. check ret
109  */
110 HWTEST_F(BufferQueueConsumerTest, AcqRel002, Function | MediumTest | Level2)
111 {
112     IBufferProducer::RequestBufferReturnValue retval;
113     GSError ret = bq->RequestBuffer(requestConfig, bedata, retval);
114     ASSERT_EQ(ret, OHOS::GSERROR_OK);
115     ASSERT_GE(retval.sequence, 0);
116     ASSERT_EQ(retval.buffer, nullptr);
117 
118     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
119     ret = bq->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
120     ASSERT_EQ(ret, OHOS::GSERROR_OK);
121 
122     sptr<SurfaceBuffer>& buffer = retval.buffer;
123     ret = bqc->AcquireBuffer(buffer, retval.fence, timestamp, damage);
124     ASSERT_EQ(ret, OHOS::GSERROR_OK);
125 
126     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
127     ret = bqc->ReleaseBuffer(buffer, releaseFence);
128     ASSERT_EQ(ret, OHOS::GSERROR_OK);
129 
130     ret = bqc->ReleaseBuffer(buffer, releaseFence);
131     ASSERT_NE(ret, OHOS::GSERROR_OK);
132 }
133 }
134