1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "StreamSplitter_test"
18 //#define LOG_NDEBUG 0
19 
20 #include <gui/BufferItem.h>
21 #include <gui/BufferQueue.h>
22 #include <gui/IConsumerListener.h>
23 #include <gui/ISurfaceComposer.h>
24 #include <gui/StreamSplitter.h>
25 #include <private/gui/ComposerService.h>
26 
27 #include <gtest/gtest.h>
28 
29 namespace android {
30 
31 class StreamSplitterTest : public ::testing::Test {
32 
33 protected:
StreamSplitterTest()34     StreamSplitterTest() {
35         const ::testing::TestInfo* const testInfo =
36             ::testing::UnitTest::GetInstance()->current_test_info();
37         ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
38                 testInfo->name());
39     }
40 
~StreamSplitterTest()41     ~StreamSplitterTest() {
42         const ::testing::TestInfo* const testInfo =
43             ::testing::UnitTest::GetInstance()->current_test_info();
44         ALOGV("End test:   %s.%s", testInfo->test_case_name(),
45                 testInfo->name());
46     }
47 };
48 
49 struct DummyListener : public BnConsumerListener {
onFrameAvailableandroid::DummyListener50     virtual void onFrameAvailable(const BufferItem& /* item */) {}
onBuffersReleasedandroid::DummyListener51     virtual void onBuffersReleased() {}
onSidebandStreamChangedandroid::DummyListener52     virtual void onSidebandStreamChanged() {}
53 };
54 
55 class CountedAllocator : public BnGraphicBufferAlloc {
56 public:
CountedAllocator()57     CountedAllocator() : mAllocCount(0) {
58         sp<ISurfaceComposer> composer(ComposerService::getComposerService());
59         mAllocator = composer->createGraphicBufferAlloc();
60     }
61 
~CountedAllocator()62     virtual ~CountedAllocator() {}
63 
createGraphicBuffer(uint32_t w,uint32_t h,PixelFormat format,uint32_t usage,status_t * error)64     virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
65             PixelFormat format, uint32_t usage, status_t* error) {
66         ++mAllocCount;
67         sp<GraphicBuffer> buffer = mAllocator->createGraphicBuffer(w, h, format,
68                 usage, error);
69         return buffer;
70     }
71 
getAllocCount() const72     int getAllocCount() const { return mAllocCount; }
73 
74 private:
75     sp<IGraphicBufferAlloc> mAllocator;
76     int mAllocCount;
77 };
78 
79 static const uint32_t TEST_DATA = 0x12345678u;
80 
TEST_F(StreamSplitterTest,OneInputOneOutput)81 TEST_F(StreamSplitterTest, OneInputOneOutput) {
82     sp<CountedAllocator> allocator(new CountedAllocator);
83 
84     sp<IGraphicBufferProducer> inputProducer;
85     sp<IGraphicBufferConsumer> inputConsumer;
86     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator);
87 
88     sp<IGraphicBufferProducer> outputProducer;
89     sp<IGraphicBufferConsumer> outputConsumer;
90     BufferQueue::createBufferQueue(&outputProducer, &outputConsumer, allocator);
91     ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false));
92 
93     sp<StreamSplitter> splitter;
94     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
95     ASSERT_EQ(OK, status);
96     ASSERT_EQ(OK, splitter->addOutput(outputProducer));
97 
98     IGraphicBufferProducer::QueueBufferOutput qbOutput;
99     ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
100             NATIVE_WINDOW_API_CPU, false, &qbOutput));
101 
102     int slot;
103     sp<Fence> fence;
104     sp<GraphicBuffer> buffer;
105     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
106             inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
107                     GRALLOC_USAGE_SW_WRITE_OFTEN));
108     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
109 
110     uint32_t* dataIn;
111     ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
112             reinterpret_cast<void**>(&dataIn)));
113     *dataIn = TEST_DATA;
114     ASSERT_EQ(OK, buffer->unlock());
115 
116     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
117             HAL_DATASPACE_UNKNOWN,
118             Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
119             Fence::NO_FENCE);
120     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
121 
122     BufferItem item;
123     ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0));
124 
125     uint32_t* dataOut;
126     ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
127             reinterpret_cast<void**>(&dataOut)));
128     ASSERT_EQ(*dataOut, TEST_DATA);
129     ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
130 
131     ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mBuf, item.mFrameNumber,
132             EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE));
133 
134     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
135             inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
136                     GRALLOC_USAGE_SW_WRITE_OFTEN));
137 
138     ASSERT_EQ(1, allocator->getAllocCount());
139 }
140 
TEST_F(StreamSplitterTest,OneInputMultipleOutputs)141 TEST_F(StreamSplitterTest, OneInputMultipleOutputs) {
142     const int NUM_OUTPUTS = 4;
143     sp<CountedAllocator> allocator(new CountedAllocator);
144 
145     sp<IGraphicBufferProducer> inputProducer;
146     sp<IGraphicBufferConsumer> inputConsumer;
147     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator);
148 
149     sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {};
150     sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {};
151     for (int output = 0; output < NUM_OUTPUTS; ++output) {
152         BufferQueue::createBufferQueue(&outputProducers[output],
153                 &outputConsumers[output], allocator);
154         ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(
155                     new DummyListener, false));
156     }
157 
158     sp<StreamSplitter> splitter;
159     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
160     ASSERT_EQ(OK, status);
161     for (int output = 0; output < NUM_OUTPUTS; ++output) {
162         ASSERT_EQ(OK, splitter->addOutput(outputProducers[output]));
163     }
164 
165     IGraphicBufferProducer::QueueBufferOutput qbOutput;
166     ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
167             NATIVE_WINDOW_API_CPU, false, &qbOutput));
168 
169     int slot;
170     sp<Fence> fence;
171     sp<GraphicBuffer> buffer;
172     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
173             inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
174                     GRALLOC_USAGE_SW_WRITE_OFTEN));
175     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
176 
177     uint32_t* dataIn;
178     ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
179             reinterpret_cast<void**>(&dataIn)));
180     *dataIn = TEST_DATA;
181     ASSERT_EQ(OK, buffer->unlock());
182 
183     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
184             HAL_DATASPACE_UNKNOWN,
185             Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
186             Fence::NO_FENCE);
187     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
188 
189     for (int output = 0; output < NUM_OUTPUTS; ++output) {
190         BufferItem item;
191         ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0));
192 
193         uint32_t* dataOut;
194         ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
195                     reinterpret_cast<void**>(&dataOut)));
196         ASSERT_EQ(*dataOut, TEST_DATA);
197         ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
198 
199         ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mBuf,
200                     item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
201                     Fence::NO_FENCE));
202     }
203 
204     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
205             inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
206                     GRALLOC_USAGE_SW_WRITE_OFTEN));
207 
208     ASSERT_EQ(1, allocator->getAllocCount());
209 }
210 
TEST_F(StreamSplitterTest,OutputAbandonment)211 TEST_F(StreamSplitterTest, OutputAbandonment) {
212     sp<IGraphicBufferProducer> inputProducer;
213     sp<IGraphicBufferConsumer> inputConsumer;
214     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
215 
216     sp<IGraphicBufferProducer> outputProducer;
217     sp<IGraphicBufferConsumer> outputConsumer;
218     BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
219     ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false));
220 
221     sp<StreamSplitter> splitter;
222     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
223     ASSERT_EQ(OK, status);
224     ASSERT_EQ(OK, splitter->addOutput(outputProducer));
225 
226     IGraphicBufferProducer::QueueBufferOutput qbOutput;
227     ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener,
228             NATIVE_WINDOW_API_CPU, false, &qbOutput));
229 
230     int slot;
231     sp<Fence> fence;
232     sp<GraphicBuffer> buffer;
233     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
234             inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0,
235                     GRALLOC_USAGE_SW_WRITE_OFTEN));
236     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
237 
238     // Abandon the output
239     outputConsumer->consumerDisconnect();
240 
241     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
242             HAL_DATASPACE_UNKNOWN,
243             Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false,
244             Fence::NO_FENCE);
245     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
246 
247     // Input should be abandoned
248     ASSERT_EQ(NO_INIT, inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0,
249             0, GRALLOC_USAGE_SW_WRITE_OFTEN));
250 }
251 
252 } // namespace android
253