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 <system/window.h>
28 
29 #include <gtest/gtest.h>
30 
31 namespace android {
32 
33 class StreamSplitterTest : public ::testing::Test {};
34 
35 struct FakeListener : public BnConsumerListener {
onFrameAvailableandroid::FakeListener36     virtual void onFrameAvailable(const BufferItem& /* item */) {}
onBuffersReleasedandroid::FakeListener37     virtual void onBuffersReleased() {}
onSidebandStreamChangedandroid::FakeListener38     virtual void onSidebandStreamChanged() {}
39 };
40 
41 static const uint32_t TEST_DATA = 0x12345678u;
42 
TEST_F(StreamSplitterTest,OneInputOneOutput)43 TEST_F(StreamSplitterTest, OneInputOneOutput) {
44     sp<IGraphicBufferProducer> inputProducer;
45     sp<IGraphicBufferConsumer> inputConsumer;
46     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
47 
48     sp<IGraphicBufferProducer> outputProducer;
49     sp<IGraphicBufferConsumer> outputConsumer;
50     BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
51     ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false));
52 
53     sp<StreamSplitter> splitter;
54     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
55     ASSERT_EQ(OK, status);
56     ASSERT_EQ(OK, splitter->addOutput(outputProducer));
57 
58     // Never allow the output BufferQueue to allocate a buffer
59     ASSERT_EQ(OK, outputProducer->allowAllocation(false));
60 
61     IGraphicBufferProducer::QueueBufferOutput qbOutput;
62     ASSERT_EQ(OK,
63               inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
64                                      &qbOutput));
65 
66     int slot;
67     sp<Fence> fence;
68     sp<GraphicBuffer> buffer;
69     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
70               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
71                                            nullptr, nullptr));
72     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
73 
74     uint32_t* dataIn;
75     ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
76             reinterpret_cast<void**>(&dataIn)));
77     *dataIn = TEST_DATA;
78     ASSERT_EQ(OK, buffer->unlock());
79 
80     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
81             HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
82             NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
83     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
84 
85     // Now that we have dequeued/allocated one buffer, prevent any further
86     // allocations
87     ASSERT_EQ(OK, inputProducer->allowAllocation(false));
88 
89     BufferItem item;
90     ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0));
91 
92     uint32_t* dataOut;
93     ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
94             reinterpret_cast<void**>(&dataOut)));
95     ASSERT_EQ(*dataOut, TEST_DATA);
96     ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
97 
98     ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mSlot, item.mFrameNumber,
99             EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE));
100 
101     // This should succeed even with allocation disabled since it will have
102     // received the buffer back from the output BufferQueue
103     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
104               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
105                                            nullptr, nullptr));
106 }
107 
TEST_F(StreamSplitterTest,OneInputMultipleOutputs)108 TEST_F(StreamSplitterTest, OneInputMultipleOutputs) {
109     const int NUM_OUTPUTS = 4;
110 
111     sp<IGraphicBufferProducer> inputProducer;
112     sp<IGraphicBufferConsumer> inputConsumer;
113     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
114 
115     sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {};
116     sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {};
117     for (int output = 0; output < NUM_OUTPUTS; ++output) {
118         BufferQueue::createBufferQueue(&outputProducers[output],
119                 &outputConsumers[output]);
120         ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(new FakeListener, false));
121     }
122 
123     sp<StreamSplitter> splitter;
124     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
125     ASSERT_EQ(OK, status);
126     for (int output = 0; output < NUM_OUTPUTS; ++output) {
127         ASSERT_EQ(OK, splitter->addOutput(outputProducers[output]));
128 
129         // Never allow the output BufferQueues to allocate a buffer
130         ASSERT_EQ(OK, outputProducers[output]->allowAllocation(false));
131     }
132 
133     IGraphicBufferProducer::QueueBufferOutput qbOutput;
134     ASSERT_EQ(OK,
135               inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
136                                      &qbOutput));
137 
138     int slot;
139     sp<Fence> fence;
140     sp<GraphicBuffer> buffer;
141     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
142               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
143                                            nullptr, nullptr));
144     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
145 
146     uint32_t* dataIn;
147     ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
148             reinterpret_cast<void**>(&dataIn)));
149     *dataIn = TEST_DATA;
150     ASSERT_EQ(OK, buffer->unlock());
151 
152     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
153             HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
154             NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
155     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
156 
157     // Now that we have dequeued/allocated one buffer, prevent any further
158     // allocations
159     ASSERT_EQ(OK, inputProducer->allowAllocation(false));
160 
161     for (int output = 0; output < NUM_OUTPUTS; ++output) {
162         BufferItem item;
163         ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0));
164 
165         uint32_t* dataOut;
166         ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
167                     reinterpret_cast<void**>(&dataOut)));
168         ASSERT_EQ(*dataOut, TEST_DATA);
169         ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
170 
171         ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mSlot,
172                     item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
173                     Fence::NO_FENCE));
174     }
175 
176     // This should succeed even with allocation disabled since it will have
177     // received the buffer back from the output BufferQueues
178     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
179               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
180                                            nullptr, nullptr));
181 }
182 
TEST_F(StreamSplitterTest,OutputAbandonment)183 TEST_F(StreamSplitterTest, OutputAbandonment) {
184     sp<IGraphicBufferProducer> inputProducer;
185     sp<IGraphicBufferConsumer> inputConsumer;
186     BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
187 
188     sp<IGraphicBufferProducer> outputProducer;
189     sp<IGraphicBufferConsumer> outputConsumer;
190     BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
191     ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false));
192 
193     sp<StreamSplitter> splitter;
194     status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
195     ASSERT_EQ(OK, status);
196     ASSERT_EQ(OK, splitter->addOutput(outputProducer));
197 
198     IGraphicBufferProducer::QueueBufferOutput qbOutput;
199     ASSERT_EQ(OK,
200               inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
201                                      &qbOutput));
202 
203     int slot;
204     sp<Fence> fence;
205     sp<GraphicBuffer> buffer;
206     ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
207               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
208                                            nullptr, nullptr));
209     ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
210 
211     // Abandon the output
212     outputConsumer->consumerDisconnect();
213 
214     IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
215             HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
216             NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
217     ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
218 
219     // Input should be abandoned
220     ASSERT_EQ(NO_INIT,
221               inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
222                                            nullptr, nullptr));
223 }
224 
225 } // namespace android
226