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 | dd26416 | 2015-03-12 13:58:47 -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 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class StreamSplitterTest : public ::testing::Test { |
| 32 | |
| 33 | protected: |
| 34 | StreamSplitterTest() { |
| 35 | const ::testing::TestInfo* const testInfo = |
| 36 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 37 | ALOGV("Begin test: %s.%s", testInfo->test_case_name(), |
| 38 | testInfo->name()); |
| 39 | } |
| 40 | |
| 41 | ~StreamSplitterTest() { |
| 42 | const ::testing::TestInfo* const testInfo = |
| 43 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 44 | ALOGV("End test: %s.%s", testInfo->test_case_name(), |
| 45 | testInfo->name()); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | struct DummyListener : public BnConsumerListener { |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 50 | virtual void onFrameAvailable(const BufferItem& /* item */) {} |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 51 | virtual void onBuffersReleased() {} |
| 52 | virtual void onSidebandStreamChanged() {} |
| 53 | }; |
| 54 | |
| 55 | class CountedAllocator : public BnGraphicBufferAlloc { |
| 56 | public: |
| 57 | CountedAllocator() : mAllocCount(0) { |
| 58 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 59 | mAllocator = composer->createGraphicBufferAlloc(); |
| 60 | } |
| 61 | |
| 62 | virtual ~CountedAllocator() {} |
| 63 | |
| 64 | virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, |
| 65 | PixelFormat format, uint32_t usage, status_t* error) { |
| 66 | ++mAllocCount; |
| 67 | sp<GraphicBuffer> buffer = mAllocator->createGraphicBuffer(w, h, format, |
| 68 | usage, error); |
| 69 | return buffer; |
| 70 | } |
| 71 | |
| 72 | int getAllocCount() const { return mAllocCount; } |
| 73 | |
| 74 | private: |
| 75 | sp<IGraphicBufferAlloc> mAllocator; |
| 76 | int mAllocCount; |
| 77 | }; |
| 78 | |
| 79 | TEST_F(StreamSplitterTest, OneInputOneOutput) { |
| 80 | sp<CountedAllocator> allocator(new CountedAllocator); |
| 81 | |
| 82 | sp<IGraphicBufferProducer> inputProducer; |
| 83 | sp<IGraphicBufferConsumer> inputConsumer; |
| 84 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator); |
| 85 | |
| 86 | sp<IGraphicBufferProducer> outputProducer; |
| 87 | sp<IGraphicBufferConsumer> outputConsumer; |
| 88 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer, allocator); |
| 89 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false)); |
| 90 | |
| 91 | sp<StreamSplitter> splitter; |
| 92 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 93 | ASSERT_EQ(OK, status); |
| 94 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); |
| 95 | |
| 96 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
| 97 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, |
| 98 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); |
| 99 | |
| 100 | int slot; |
| 101 | sp<Fence> fence; |
| 102 | sp<GraphicBuffer> buffer; |
| 103 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 104 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, |
| 105 | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 106 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 107 | |
| 108 | uint32_t* dataIn; |
| 109 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 110 | reinterpret_cast<void**>(&dataIn))); |
| 111 | *dataIn = 0x12345678; |
| 112 | ASSERT_EQ(OK, buffer->unlock()); |
| 113 | |
| 114 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 115 | HAL_DATASPACE_UNKNOWN, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 116 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, |
| 117 | Fence::NO_FENCE); |
| 118 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 119 | |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame^] | 120 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 121 | ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0)); |
| 122 | |
| 123 | uint32_t* dataOut; |
| 124 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 125 | reinterpret_cast<void**>(&dataOut))); |
| 126 | ASSERT_EQ(*dataOut, 0x12345678); |
| 127 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 128 | |
| 129 | ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mBuf, item.mFrameNumber, |
| 130 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 131 | |
| 132 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 133 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, |
| 134 | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 135 | |
| 136 | ASSERT_EQ(1, allocator->getAllocCount()); |
| 137 | } |
| 138 | |
| 139 | TEST_F(StreamSplitterTest, OneInputMultipleOutputs) { |
| 140 | const int NUM_OUTPUTS = 4; |
| 141 | sp<CountedAllocator> allocator(new CountedAllocator); |
| 142 | |
| 143 | sp<IGraphicBufferProducer> inputProducer; |
| 144 | sp<IGraphicBufferConsumer> inputConsumer; |
| 145 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator); |
| 146 | |
| 147 | sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {}; |
| 148 | sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {}; |
| 149 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
| 150 | BufferQueue::createBufferQueue(&outputProducers[output], |
| 151 | &outputConsumers[output], allocator); |
| 152 | ASSERT_EQ(OK, outputConsumers[output]->consumerConnect( |
| 153 | new DummyListener, false)); |
| 154 | } |
| 155 | |
| 156 | sp<StreamSplitter> splitter; |
| 157 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 158 | ASSERT_EQ(OK, status); |
| 159 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
| 160 | ASSERT_EQ(OK, splitter->addOutput(outputProducers[output])); |
| 161 | } |
| 162 | |
| 163 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
| 164 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, |
| 165 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); |
| 166 | |
| 167 | int slot; |
| 168 | sp<Fence> fence; |
| 169 | sp<GraphicBuffer> buffer; |
| 170 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 171 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, |
| 172 | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 173 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 174 | |
| 175 | uint32_t* dataIn; |
| 176 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 177 | reinterpret_cast<void**>(&dataIn))); |
| 178 | *dataIn = 0x12345678; |
| 179 | ASSERT_EQ(OK, buffer->unlock()); |
| 180 | |
| 181 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 182 | HAL_DATASPACE_UNKNOWN, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 183 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, |
| 184 | Fence::NO_FENCE); |
| 185 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 186 | |
| 187 | for (int output = 0; output < NUM_OUTPUTS; ++output) { |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame^] | 188 | BufferItem item; |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 189 | ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0)); |
| 190 | |
| 191 | uint32_t* dataOut; |
| 192 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 193 | reinterpret_cast<void**>(&dataOut))); |
| 194 | ASSERT_EQ(*dataOut, 0x12345678); |
| 195 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 196 | |
| 197 | ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mBuf, |
| 198 | item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, |
| 199 | Fence::NO_FENCE)); |
| 200 | } |
| 201 | |
| 202 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 203 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, |
| 204 | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 205 | |
| 206 | ASSERT_EQ(1, allocator->getAllocCount()); |
| 207 | } |
| 208 | |
| 209 | TEST_F(StreamSplitterTest, OutputAbandonment) { |
| 210 | sp<IGraphicBufferProducer> inputProducer; |
| 211 | sp<IGraphicBufferConsumer> inputConsumer; |
| 212 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); |
| 213 | |
| 214 | sp<IGraphicBufferProducer> outputProducer; |
| 215 | sp<IGraphicBufferConsumer> outputConsumer; |
| 216 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer); |
| 217 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false)); |
| 218 | |
| 219 | sp<StreamSplitter> splitter; |
| 220 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); |
| 221 | ASSERT_EQ(OK, status); |
| 222 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); |
| 223 | |
| 224 | IGraphicBufferProducer::QueueBufferOutput qbOutput; |
| 225 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, |
| 226 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); |
| 227 | |
| 228 | int slot; |
| 229 | sp<Fence> fence; |
| 230 | sp<GraphicBuffer> buffer; |
| 231 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 232 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, |
| 233 | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 234 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); |
| 235 | |
| 236 | // Abandon the output |
| 237 | outputConsumer->consumerDisconnect(); |
| 238 | |
| 239 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, |
Eino-Ville Talvala | 5b75a51 | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 240 | HAL_DATASPACE_UNKNOWN, |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 241 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, |
| 242 | Fence::NO_FENCE); |
| 243 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); |
| 244 | |
| 245 | // Input should be abandoned |
| 246 | ASSERT_EQ(NO_INIT, inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, |
| 247 | 0, GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 248 | } |
| 249 | |
| 250 | } // namespace android |