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 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 98 | ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 99 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 100 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 101 | // This should succeed even with allocation disabled since it will have |
| 102 | // received the buffer back from the output BufferQueue |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 103 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 104 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 105 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(StreamSplitterTest, OneInputMultipleOutputs) { |
| 109 | const int NUM_OUTPUTS = 4; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 110 | |
| 111 | sp<IGraphicBufferProducer> inputProducer; |
| 112 | sp<IGraphicBufferConsumer> inputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 113 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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], |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 119 | &outputConsumers[output]); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 120 | ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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])); |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 128 | |
| 129 | // Never allow the output BufferQueues to allocate a buffer |
| 130 | ASSERT_EQ(OK, outputProducers[output]->allowAllocation(false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 134 | ASSERT_EQ(OK, |
| 135 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 136 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 137 | |
| 138 | int slot; |
| 139 | sp<Fence> fence; |
| 140 | sp<GraphicBuffer> buffer; |
| 141 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 142 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 143 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 149 | *dataIn = TEST_DATA; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 150 | ASSERT_EQ(OK, buffer->unlock()); |
| 151 | |
| 152 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 153 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 154 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 155 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 156 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 157 | // Now that we have dequeued/allocated one buffer, prevent any further |
| 158 | // allocations |
| 159 | ASSERT_EQ(OK, inputProducer->allowAllocation(false)); |
| 160 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 161 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 162 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 168 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 169 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 170 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 171 | ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mSlot, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 172 | item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, |
| 173 | Fence::NO_FENCE)); |
| 174 | } |
| 175 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 176 | // This should succeed even with allocation disabled since it will have |
| 177 | // received the buffer back from the output BufferQueues |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 178 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 179 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 180 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 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); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 191 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 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; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame] | 199 | ASSERT_EQ(OK, |
| 200 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 201 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 202 | |
| 203 | int slot; |
| 204 | sp<Fence> fence; |
| 205 | sp<GraphicBuffer> buffer; |
| 206 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 207 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 208 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 209 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 210 | |
| 211 | // Abandon the output |
| 212 | outputConsumer->consumerDisconnect(); |
| 213 | |
| 214 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 215 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 216 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 217 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 218 | |
| 219 | // Input should be abandoned |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 220 | ASSERT_EQ(NO_INIT, |
| 221 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 222 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace android |