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 | |
| 33 | class StreamSplitterTest : public ::testing::Test { |
| 34 | |
| 35 | protected: |
| 36 | StreamSplitterTest() { |
| 37 | const ::testing::TestInfo* const testInfo = |
| 38 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 39 | ALOGV("Begin test: %s.%s", testInfo->test_case_name(), |
| 40 | testInfo->name()); |
| 41 | } |
| 42 | |
| 43 | ~StreamSplitterTest() { |
| 44 | const ::testing::TestInfo* const testInfo = |
| 45 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 46 | ALOGV("End test: %s.%s", testInfo->test_case_name(), |
| 47 | testInfo->name()); |
| 48 | } |
| 49 | }; |
| 50 | |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 51 | struct FakeListener : public BnConsumerListener { |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 52 | virtual void onFrameAvailable(const BufferItem& /* item */) {} |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 53 | virtual void onBuffersReleased() {} |
| 54 | virtual void onSidebandStreamChanged() {} |
| 55 | }; |
| 56 | |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 57 | static const uint32_t TEST_DATA = 0x12345678u; |
| 58 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 59 | TEST_F(StreamSplitterTest, OneInputOneOutput) { |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 60 | sp<IGraphicBufferProducer> inputProducer; |
| 61 | sp<IGraphicBufferConsumer> inputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 62 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 63 | |
| 64 | sp<IGraphicBufferProducer> outputProducer; |
| 65 | sp<IGraphicBufferConsumer> outputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 66 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 67 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 68 | |
| 69 | sp<StreamSplitter> splitter; |
| 70 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 71 | ASSERT_EQ(OK, status); |
| 72 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); |
| 73 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 74 | // Never allow the output BufferQueue to allocate a buffer |
| 75 | ASSERT_EQ(OK, outputProducer->allowAllocation(false)); |
| 76 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 77 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 78 | ASSERT_EQ(OK, |
| 79 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 80 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 81 | |
| 82 | int slot; |
| 83 | sp<Fence> fence; |
| 84 | sp<GraphicBuffer> buffer; |
| 85 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 86 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 87 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 88 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 89 | |
| 90 | uint32_t* dataIn; |
| 91 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 92 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 93 | *dataIn = TEST_DATA; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 94 | ASSERT_EQ(OK, buffer->unlock()); |
| 95 | |
| 96 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 97 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 98 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 99 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 100 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 101 | // Now that we have dequeued/allocated one buffer, prevent any further |
| 102 | // allocations |
| 103 | ASSERT_EQ(OK, inputProducer->allowAllocation(false)); |
| 104 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 105 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 106 | ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0)); |
| 107 | |
| 108 | uint32_t* dataOut; |
| 109 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 110 | reinterpret_cast<void**>(&dataOut))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 111 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 112 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 113 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 114 | ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 115 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 116 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 117 | // This should succeed even with allocation disabled since it will have |
| 118 | // received the buffer back from the output BufferQueue |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 119 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 120 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 121 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(StreamSplitterTest, OneInputMultipleOutputs) { |
| 125 | const int NUM_OUTPUTS = 4; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 126 | |
| 127 | sp<IGraphicBufferProducer> inputProducer; |
| 128 | sp<IGraphicBufferConsumer> inputConsumer; |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 129 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 130 | |
| 131 | sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {}; |
| 132 | sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {}; |
| 133 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
| 134 | BufferQueue::createBufferQueue(&outputProducers[output], |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 135 | &outputConsumers[output]); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 136 | ASSERT_EQ(OK, outputConsumers[output]->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | sp<StreamSplitter> splitter; |
| 140 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 141 | ASSERT_EQ(OK, status); |
| 142 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
| 143 | ASSERT_EQ(OK, splitter->addOutput(outputProducers[output])); |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 144 | |
| 145 | // Never allow the output BufferQueues to allocate a buffer |
| 146 | ASSERT_EQ(OK, outputProducers[output]->allowAllocation(false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 150 | ASSERT_EQ(OK, |
| 151 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 152 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 153 | |
| 154 | int slot; |
| 155 | sp<Fence> fence; |
| 156 | sp<GraphicBuffer> buffer; |
| 157 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 158 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 159 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 160 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 161 | |
| 162 | uint32_t* dataIn; |
| 163 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 164 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 165 | *dataIn = TEST_DATA; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 166 | ASSERT_EQ(OK, buffer->unlock()); |
| 167 | |
| 168 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 169 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 170 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 171 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 172 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 173 | // Now that we have dequeued/allocated one buffer, prevent any further |
| 174 | // allocations |
| 175 | ASSERT_EQ(OK, inputProducer->allowAllocation(false)); |
| 176 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 177 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 178 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 179 | ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0)); |
| 180 | |
| 181 | uint32_t* dataOut; |
| 182 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 183 | reinterpret_cast<void**>(&dataOut))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 184 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 185 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 186 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 187 | ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mSlot, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 188 | item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, |
| 189 | Fence::NO_FENCE)); |
| 190 | } |
| 191 | |
Dan Stoza | 3bb0c88 | 2016-01-06 14:21:52 -0800 | [diff] [blame] | 192 | // This should succeed even with allocation disabled since it will have |
| 193 | // received the buffer back from the output BufferQueues |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 194 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 195 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 196 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST_F(StreamSplitterTest, OutputAbandonment) { |
| 200 | sp<IGraphicBufferProducer> inputProducer; |
| 201 | sp<IGraphicBufferConsumer> inputConsumer; |
| 202 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
| 203 | |
| 204 | sp<IGraphicBufferProducer> outputProducer; |
| 205 | sp<IGraphicBufferConsumer> outputConsumer; |
| 206 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer); |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 207 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new FakeListener, false)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 208 | |
| 209 | sp<StreamSplitter> splitter; |
| 210 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 211 | ASSERT_EQ(OK, status); |
| 212 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); |
| 213 | |
| 214 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
Peiyong Lin | d8460c8 | 2020-07-28 16:04:22 -0700 | [diff] [blame^] | 215 | ASSERT_EQ(OK, |
| 216 | inputProducer->connect(new StubProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 217 | &qbOutput)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 218 | |
| 219 | int slot; |
| 220 | sp<Fence> fence; |
| 221 | sp<GraphicBuffer> buffer; |
| 222 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 223 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 224 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 225 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 226 | |
| 227 | // Abandon the output |
| 228 | outputConsumer->consumerDisconnect(); |
| 229 | |
| 230 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 231 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 232 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 233 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 234 | |
| 235 | // Input should be abandoned |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 236 | ASSERT_EQ(NO_INIT, |
| 237 | inputProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 238 | nullptr, nullptr)); |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | } // namespace android |