Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 20 | #include <gui/BufferItem.h> |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 27 | #include <system/window.h> |
| 28 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Nolan Scobie | cfa41bf | 2023-06-14 15:13:18 -0400 | [diff] [blame] | 33 | class StreamSplitterTest : public ::testing::Test {}; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 34 | |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 35 | struct FakeListener : public BnConsumerListener { |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 36 | virtual void onFrameAvailable(const BufferItem& /* item */) {} |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 37 | virtual void onBuffersReleased() {} |
| 38 | virtual void onSidebandStreamChanged() {} |
| 39 | }; |
| 40 | |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 41 | static const uint32_t TEST_DATA = 0x12345678u; |
| 42 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 43 | TEST_F(StreamSplitterTest, OneInputOneOutput) { |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 44 | sp<IGraphicBufferProducer> inputProducer; |
| 45 | sp<IGraphicBufferConsumer> inputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 46 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 47 | |
| 48 | sp<IGraphicBufferProducer> outputProducer; |
| 49 | sp<IGraphicBufferConsumer> outputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 50 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 51 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 58 | // Never allow the output BufferQueue to allocate a buffer |
| 59 | ASSERT_EQ(OK, outputProducer->allowAllocation(false)); |
| 60 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 61 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 62 | ASSERT_EQ(OK, |
| 63 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 64 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 65 | |
| 66 | int slot; |
| 67 | sp<Fence> fence; |
| 68 | sp<GraphicBuffer> buffer; |
| 69 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 70 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 71 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 77 | *dataIn = TEST_DATA; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 78 | ASSERT_EQ(OK, buffer->unlock()); |
| 79 | |
| 80 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 81 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 82 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 83 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 84 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 85 | // Now that we have dequeued/allocated one buffer, prevent any further |
| 86 | // allocations |
| 87 | ASSERT_EQ(OK, inputProducer->allowAllocation(false)); |
| 88 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 89 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 95 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 96 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 97 | |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 98 | ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, Fence::NO_FENCE)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 99 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 100 | // This should succeed even with allocation disabled since it will have |
| 101 | // received the buffer back from the output BufferQueue |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 102 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 103 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 104 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST_F(StreamSplitterTest, OneInputMultipleOutputs) { |
| 108 | const int NUM_OUTPUTS = 4; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 109 | |
| 110 | sp<IGraphicBufferProducer> inputProducer; |
| 111 | sp<IGraphicBufferConsumer> inputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 112 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 113 | |
| 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 Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 118 | &outputConsumers[output]); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 119 | ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 120 | } |
| 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 Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 127 | |
| 128 | // Never allow the output BufferQueues to allocate a buffer |
| 129 | ASSERT_EQ(OK, outputProducers[output]->allowAllocation(false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 133 | ASSERT_EQ(OK, |
| 134 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 135 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 136 | |
| 137 | int slot; |
| 138 | sp<Fence> fence; |
| 139 | sp<GraphicBuffer> buffer; |
| 140 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 141 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 142 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 143 | 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 Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 148 | *dataIn = TEST_DATA; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 149 | ASSERT_EQ(OK, buffer->unlock()); |
| 150 | |
| 151 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 152 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 153 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 154 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 155 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 156 | // Now that we have dequeued/allocated one buffer, prevent any further |
| 157 | // allocations |
| 158 | ASSERT_EQ(OK, inputProducer->allowAllocation(false)); |
| 159 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 160 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 161 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 162 | 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 Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 167 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 168 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 169 | |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 170 | ASSERT_EQ(OK, |
| 171 | outputConsumers[output]->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 172 | Fence::NO_FENCE)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 175 | // This should succeed even with allocation disabled since it will have |
| 176 | // received the buffer back from the output BufferQueues |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 177 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 178 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 179 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST_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 Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 190 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 191 | |
| 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 Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 198 | ASSERT_EQ(OK, |
| 199 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 200 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 201 | |
| 202 | int slot; |
| 203 | sp<Fence> fence; |
| 204 | sp<GraphicBuffer> buffer; |
| 205 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 206 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 207 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 208 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 209 | |
| 210 | // Abandon the output |
| 211 | outputConsumer->consumerDisconnect(); |
| 212 | |
| 213 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 214 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 215 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 216 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 217 | |
| 218 | // Input should be abandoned |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 219 | ASSERT_EQ(NO_INIT, |
| 220 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 221 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } // namespace android |