blob: 1c439cdb457e72f12c533cc3ed4ce6ff3e688354 [file] [log] [blame]
Dan Stoza99b18b42014-03-28 15:34:33 -07001/*
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
Dan Stozacf3834d2015-03-11 14:04:22 -070020#include <gui/BufferItem.h>
Dan Stoza99b18b42014-03-28 15:34:33 -070021#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
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070027#include <system/window.h>
28
Dan Stoza99b18b42014-03-28 15:34:33 -070029#include <gtest/gtest.h>
30
31namespace android {
32
Nolan Scobiecfa41bf2023-06-14 15:13:18 -040033class StreamSplitterTest : public ::testing::Test {};
Dan Stoza99b18b42014-03-28 15:34:33 -070034
Peiyong Lind8460c82020-07-28 16:04:22 -070035struct FakeListener : public BnConsumerListener {
Dan Stoza8dc55392014-11-04 11:37:46 -080036 virtual void onFrameAvailable(const BufferItem& /* item */) {}
Dan Stoza99b18b42014-03-28 15:34:33 -070037 virtual void onBuffersReleased() {}
38 virtual void onSidebandStreamChanged() {}
39};
40
Dan Stozaf8cebe52015-04-20 12:09:38 -070041static const uint32_t TEST_DATA = 0x12345678u;
42
Dan Stoza99b18b42014-03-28 15:34:33 -070043TEST_F(StreamSplitterTest, OneInputOneOutput) {
Dan Stoza99b18b42014-03-28 15:34:33 -070044 sp<IGraphicBufferProducer> inputProducer;
45 sp<IGraphicBufferConsumer> inputConsumer;
Dan Stoza3bb0c882016-01-06 14:21:52 -080046 BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
Dan Stoza99b18b42014-03-28 15:34:33 -070047
48 sp<IGraphicBufferProducer> outputProducer;
49 sp<IGraphicBufferConsumer> outputConsumer;
Dan Stoza3bb0c882016-01-06 14:21:52 -080050 BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
Peiyong Lind8460c82020-07-28 16:04:22 -070051 ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false));
Dan Stoza99b18b42014-03-28 15:34:33 -070052
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
Dan Stoza3bb0c882016-01-06 14:21:52 -080058 // Never allow the output BufferQueue to allocate a buffer
59 ASSERT_EQ(OK, outputProducer->allowAllocation(false));
60
Dan Stoza99b18b42014-03-28 15:34:33 -070061 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Peiyong Lind8460c82020-07-28 16:04:22 -070062 ASSERT_EQ(OK,
63 inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
64 &qbOutput));
Dan Stoza99b18b42014-03-28 15:34:33 -070065
66 int slot;
67 sp<Fence> fence;
68 sp<GraphicBuffer> buffer;
69 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
Ian Elliottd11b0442017-07-18 11:05:49 -060070 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
71 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -070072 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)));
Dan Stozaf8cebe52015-04-20 12:09:38 -070077 *dataIn = TEST_DATA;
Dan Stoza99b18b42014-03-28 15:34:33 -070078 ASSERT_EQ(OK, buffer->unlock());
79
80 IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -070081 HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
82 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
Dan Stoza99b18b42014-03-28 15:34:33 -070083 ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
84
Dan Stoza3bb0c882016-01-06 14:21:52 -080085 // Now that we have dequeued/allocated one buffer, prevent any further
86 // allocations
87 ASSERT_EQ(OK, inputProducer->allowAllocation(false));
88
Dan Stozacf3834d2015-03-11 14:04:22 -070089 BufferItem item;
Dan Stoza99b18b42014-03-28 15:34:33 -070090 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)));
Dan Stozaf8cebe52015-04-20 12:09:38 -070095 ASSERT_EQ(*dataOut, TEST_DATA);
Dan Stoza99b18b42014-03-28 15:34:33 -070096 ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
97
Jim Shargo52108082024-11-15 16:53:57 +000098 ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, Fence::NO_FENCE));
Dan Stoza99b18b42014-03-28 15:34:33 -070099
Dan Stoza3bb0c882016-01-06 14:21:52 -0800100 // This should succeed even with allocation disabled since it will have
101 // received the buffer back from the output BufferQueue
Dan Stoza99b18b42014-03-28 15:34:33 -0700102 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
Ian Elliottd11b0442017-07-18 11:05:49 -0600103 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
104 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -0700105}
106
107TEST_F(StreamSplitterTest, OneInputMultipleOutputs) {
108 const int NUM_OUTPUTS = 4;
Dan Stoza99b18b42014-03-28 15:34:33 -0700109
110 sp<IGraphicBufferProducer> inputProducer;
111 sp<IGraphicBufferConsumer> inputConsumer;
Dan Stoza3bb0c882016-01-06 14:21:52 -0800112 BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
Dan Stoza99b18b42014-03-28 15:34:33 -0700113
114 sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {};
115 sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {};
116 for (int output = 0; output < NUM_OUTPUTS; ++output) {
117 BufferQueue::createBufferQueue(&outputProducers[output],
Dan Stoza3bb0c882016-01-06 14:21:52 -0800118 &outputConsumers[output]);
Peiyong Lind8460c82020-07-28 16:04:22 -0700119 ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(new FakeListener, false));
Dan Stoza99b18b42014-03-28 15:34:33 -0700120 }
121
122 sp<StreamSplitter> splitter;
123 status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
124 ASSERT_EQ(OK, status);
125 for (int output = 0; output < NUM_OUTPUTS; ++output) {
126 ASSERT_EQ(OK, splitter->addOutput(outputProducers[output]));
Dan Stoza3bb0c882016-01-06 14:21:52 -0800127
128 // Never allow the output BufferQueues to allocate a buffer
129 ASSERT_EQ(OK, outputProducers[output]->allowAllocation(false));
Dan Stoza99b18b42014-03-28 15:34:33 -0700130 }
131
132 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Peiyong Lind8460c82020-07-28 16:04:22 -0700133 ASSERT_EQ(OK,
134 inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
135 &qbOutput));
Dan Stoza99b18b42014-03-28 15:34:33 -0700136
137 int slot;
138 sp<Fence> fence;
139 sp<GraphicBuffer> buffer;
140 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
Ian Elliottd11b0442017-07-18 11:05:49 -0600141 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
142 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -0700143 ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
144
145 uint32_t* dataIn;
146 ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,
147 reinterpret_cast<void**>(&dataIn)));
Dan Stozaf8cebe52015-04-20 12:09:38 -0700148 *dataIn = TEST_DATA;
Dan Stoza99b18b42014-03-28 15:34:33 -0700149 ASSERT_EQ(OK, buffer->unlock());
150
151 IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700152 HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
153 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
Dan Stoza99b18b42014-03-28 15:34:33 -0700154 ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
155
Dan Stoza3bb0c882016-01-06 14:21:52 -0800156 // Now that we have dequeued/allocated one buffer, prevent any further
157 // allocations
158 ASSERT_EQ(OK, inputProducer->allowAllocation(false));
159
Dan Stoza99b18b42014-03-28 15:34:33 -0700160 for (int output = 0; output < NUM_OUTPUTS; ++output) {
Dan Stozacf3834d2015-03-11 14:04:22 -0700161 BufferItem item;
Dan Stoza99b18b42014-03-28 15:34:33 -0700162 ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0));
163
164 uint32_t* dataOut;
165 ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN,
166 reinterpret_cast<void**>(&dataOut)));
Dan Stozaf8cebe52015-04-20 12:09:38 -0700167 ASSERT_EQ(*dataOut, TEST_DATA);
Dan Stoza99b18b42014-03-28 15:34:33 -0700168 ASSERT_EQ(OK, item.mGraphicBuffer->unlock());
169
Jim Shargo52108082024-11-15 16:53:57 +0000170 ASSERT_EQ(OK,
171 outputConsumers[output]->releaseBuffer(item.mSlot, item.mFrameNumber,
172 Fence::NO_FENCE));
Dan Stoza99b18b42014-03-28 15:34:33 -0700173 }
174
Dan Stoza3bb0c882016-01-06 14:21:52 -0800175 // This should succeed even with allocation disabled since it will have
176 // received the buffer back from the output BufferQueues
Dan Stoza99b18b42014-03-28 15:34:33 -0700177 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
Ian Elliottd11b0442017-07-18 11:05:49 -0600178 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
179 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -0700180}
181
182TEST_F(StreamSplitterTest, OutputAbandonment) {
183 sp<IGraphicBufferProducer> inputProducer;
184 sp<IGraphicBufferConsumer> inputConsumer;
185 BufferQueue::createBufferQueue(&inputProducer, &inputConsumer);
186
187 sp<IGraphicBufferProducer> outputProducer;
188 sp<IGraphicBufferConsumer> outputConsumer;
189 BufferQueue::createBufferQueue(&outputProducer, &outputConsumer);
Peiyong Lind8460c82020-07-28 16:04:22 -0700190 ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false));
Dan Stoza99b18b42014-03-28 15:34:33 -0700191
192 sp<StreamSplitter> splitter;
193 status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter);
194 ASSERT_EQ(OK, status);
195 ASSERT_EQ(OK, splitter->addOutput(outputProducer));
196
197 IGraphicBufferProducer::QueueBufferOutput qbOutput;
Peiyong Lind8460c82020-07-28 16:04:22 -0700198 ASSERT_EQ(OK,
199 inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false,
200 &qbOutput));
Dan Stoza99b18b42014-03-28 15:34:33 -0700201
202 int slot;
203 sp<Fence> fence;
204 sp<GraphicBuffer> buffer;
205 ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION,
Ian Elliottd11b0442017-07-18 11:05:49 -0600206 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
207 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -0700208 ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer));
209
210 // Abandon the output
211 outputConsumer->consumerDisconnect();
212
213 IGraphicBufferProducer::QueueBufferInput qbInput(0, false,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700214 HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1),
215 NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
Dan Stoza99b18b42014-03-28 15:34:33 -0700216 ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput));
217
218 // Input should be abandoned
Ian Elliottd11b0442017-07-18 11:05:49 -0600219 ASSERT_EQ(NO_INIT,
220 inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
221 nullptr, nullptr));
Dan Stoza99b18b42014-03-28 15:34:33 -0700222}
223
224} // namespace android