| 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 |  | 
|  | 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 |  | 
| Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 79 | static const uint32_t TEST_DATA = 0x12345678u; | 
|  | 80 |  | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 81 | TEST_F(StreamSplitterTest, OneInputOneOutput) { | 
|  | 82 | sp<CountedAllocator> allocator(new CountedAllocator); | 
|  | 83 |  | 
|  | 84 | sp<IGraphicBufferProducer> inputProducer; | 
|  | 85 | sp<IGraphicBufferConsumer> inputConsumer; | 
|  | 86 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator); | 
|  | 87 |  | 
|  | 88 | sp<IGraphicBufferProducer> outputProducer; | 
|  | 89 | sp<IGraphicBufferConsumer> outputConsumer; | 
|  | 90 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer, allocator); | 
|  | 91 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false)); | 
|  | 92 |  | 
|  | 93 | sp<StreamSplitter> splitter; | 
|  | 94 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); | 
|  | 95 | ASSERT_EQ(OK, status); | 
|  | 96 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); | 
|  | 97 |  | 
|  | 98 | IGraphicBufferProducer::QueueBufferOutput qbOutput; | 
|  | 99 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, | 
|  | 100 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); | 
|  | 101 |  | 
|  | 102 | int slot; | 
|  | 103 | sp<Fence> fence; | 
|  | 104 | sp<GraphicBuffer> buffer; | 
|  | 105 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, | 
|  | 106 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, | 
|  | 107 | GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 108 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); | 
|  | 109 |  | 
|  | 110 | uint32_t* dataIn; | 
|  | 111 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, | 
|  | 112 | reinterpret_cast<void**>(&dataIn))); | 
| Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 113 | *dataIn = TEST_DATA; | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 114 | ASSERT_EQ(OK, buffer->unlock()); | 
|  | 115 |  | 
|  | 116 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 117 | HAL_DATASPACE_UNKNOWN, | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 118 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, | 
|  | 119 | Fence::NO_FENCE); | 
|  | 120 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); | 
|  | 121 |  | 
| Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 122 | BufferItem item; | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 123 | ASSERT_EQ(OK, outputConsumer->acquireBuffer(&item, 0)); | 
|  | 124 |  | 
|  | 125 | uint32_t* dataOut; | 
|  | 126 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 127 | reinterpret_cast<void**>(&dataOut))); | 
| Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 128 | ASSERT_EQ(*dataOut, TEST_DATA); | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 129 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); | 
|  | 130 |  | 
|  | 131 | ASSERT_EQ(OK, outputConsumer->releaseBuffer(item.mBuf, item.mFrameNumber, | 
|  | 132 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); | 
|  | 133 |  | 
|  | 134 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, | 
|  | 135 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, | 
|  | 136 | GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 137 |  | 
|  | 138 | ASSERT_EQ(1, allocator->getAllocCount()); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | TEST_F(StreamSplitterTest, OneInputMultipleOutputs) { | 
|  | 142 | const int NUM_OUTPUTS = 4; | 
|  | 143 | sp<CountedAllocator> allocator(new CountedAllocator); | 
|  | 144 |  | 
|  | 145 | sp<IGraphicBufferProducer> inputProducer; | 
|  | 146 | sp<IGraphicBufferConsumer> inputConsumer; | 
|  | 147 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer, allocator); | 
|  | 148 |  | 
|  | 149 | sp<IGraphicBufferProducer> outputProducers[NUM_OUTPUTS] = {}; | 
|  | 150 | sp<IGraphicBufferConsumer> outputConsumers[NUM_OUTPUTS] = {}; | 
|  | 151 | for (int output = 0; output < NUM_OUTPUTS; ++output) { | 
|  | 152 | BufferQueue::createBufferQueue(&outputProducers[output], | 
|  | 153 | &outputConsumers[output], allocator); | 
|  | 154 | ASSERT_EQ(OK, outputConsumers[output]->consumerConnect( | 
|  | 155 | new DummyListener, false)); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | sp<StreamSplitter> splitter; | 
|  | 159 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); | 
|  | 160 | ASSERT_EQ(OK, status); | 
|  | 161 | for (int output = 0; output < NUM_OUTPUTS; ++output) { | 
|  | 162 | ASSERT_EQ(OK, splitter->addOutput(outputProducers[output])); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | IGraphicBufferProducer::QueueBufferOutput qbOutput; | 
|  | 166 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, | 
|  | 167 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); | 
|  | 168 |  | 
|  | 169 | int slot; | 
|  | 170 | sp<Fence> fence; | 
|  | 171 | sp<GraphicBuffer> buffer; | 
|  | 172 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, | 
|  | 173 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, | 
|  | 174 | GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 175 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); | 
|  | 176 |  | 
|  | 177 | uint32_t* dataIn; | 
|  | 178 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, | 
|  | 179 | reinterpret_cast<void**>(&dataIn))); | 
| Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 180 | *dataIn = TEST_DATA; | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 181 | ASSERT_EQ(OK, buffer->unlock()); | 
|  | 182 |  | 
|  | 183 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 184 | HAL_DATASPACE_UNKNOWN, | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 185 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, | 
|  | 186 | Fence::NO_FENCE); | 
|  | 187 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); | 
|  | 188 |  | 
|  | 189 | for (int output = 0; output < NUM_OUTPUTS; ++output) { | 
| Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 190 | BufferItem item; | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 191 | ASSERT_EQ(OK, outputConsumers[output]->acquireBuffer(&item, 0)); | 
|  | 192 |  | 
|  | 193 | uint32_t* dataOut; | 
|  | 194 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 195 | reinterpret_cast<void**>(&dataOut))); | 
| Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 196 | ASSERT_EQ(*dataOut, TEST_DATA); | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 197 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); | 
|  | 198 |  | 
|  | 199 | ASSERT_EQ(OK, outputConsumers[output]->releaseBuffer(item.mBuf, | 
|  | 200 | item.mFrameNumber, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, | 
|  | 201 | Fence::NO_FENCE)); | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, | 
|  | 205 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, | 
|  | 206 | GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 207 |  | 
|  | 208 | ASSERT_EQ(1, allocator->getAllocCount()); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | TEST_F(StreamSplitterTest, OutputAbandonment) { | 
|  | 212 | sp<IGraphicBufferProducer> inputProducer; | 
|  | 213 | sp<IGraphicBufferConsumer> inputConsumer; | 
|  | 214 | BufferQueue::createBufferQueue(&inputProducer, &inputConsumer); | 
|  | 215 |  | 
|  | 216 | sp<IGraphicBufferProducer> outputProducer; | 
|  | 217 | sp<IGraphicBufferConsumer> outputConsumer; | 
|  | 218 | BufferQueue::createBufferQueue(&outputProducer, &outputConsumer); | 
|  | 219 | ASSERT_EQ(OK, outputConsumer->consumerConnect(new DummyListener, false)); | 
|  | 220 |  | 
|  | 221 | sp<StreamSplitter> splitter; | 
|  | 222 | status_t status = StreamSplitter::createSplitter(inputConsumer, &splitter); | 
|  | 223 | ASSERT_EQ(OK, status); | 
|  | 224 | ASSERT_EQ(OK, splitter->addOutput(outputProducer)); | 
|  | 225 |  | 
|  | 226 | IGraphicBufferProducer::QueueBufferOutput qbOutput; | 
|  | 227 | ASSERT_EQ(OK, inputProducer->connect(new DummyProducerListener, | 
|  | 228 | NATIVE_WINDOW_API_CPU, false, &qbOutput)); | 
|  | 229 |  | 
|  | 230 | int slot; | 
|  | 231 | sp<Fence> fence; | 
|  | 232 | sp<GraphicBuffer> buffer; | 
|  | 233 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, | 
|  | 234 | inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, 0, | 
|  | 235 | GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 236 | ASSERT_EQ(OK, inputProducer->requestBuffer(slot, &buffer)); | 
|  | 237 |  | 
|  | 238 | // Abandon the output | 
|  | 239 | outputConsumer->consumerDisconnect(); | 
|  | 240 |  | 
|  | 241 | IGraphicBufferProducer::QueueBufferInput qbInput(0, false, | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 242 | HAL_DATASPACE_UNKNOWN, | 
| Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 243 | Rect(0, 0, 1, 1), NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, | 
|  | 244 | Fence::NO_FENCE); | 
|  | 245 | ASSERT_EQ(OK, inputProducer->queueBuffer(slot, qbInput, &qbOutput)); | 
|  | 246 |  | 
|  | 247 | // Input should be abandoned | 
|  | 248 | ASSERT_EQ(NO_INIT, inputProducer->dequeueBuffer(&slot, &fence, false, 0, 0, | 
|  | 249 | 0, GRALLOC_USAGE_SW_WRITE_OFTEN)); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | } // namespace android |