Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "BufferQueue_test" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 20 | #include "DummyConsumer.h" |
| 21 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 22 | #include <gui/BufferItem.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 23 | #include <gui/BufferQueue.h> |
| 24 | #include <gui/IProducerListener.h> |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 25 | |
| 26 | #include <ui/GraphicBuffer.h> |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 27 | |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 30 | #include <binder/ProcessState.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 31 | |
| 32 | #include <utils/String8.h> |
| 33 | #include <utils/threads.h> |
| 34 | |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 35 | #include <system/window.h> |
| 36 | |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 37 | #include <gtest/gtest.h> |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 38 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 39 | #include <thread> |
| 40 | |
| 41 | using namespace std::chrono_literals; |
| 42 | |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
| 45 | class BufferQueueTest : public ::testing::Test { |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 46 | |
| 47 | public: |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 48 | protected: |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 49 | BufferQueueTest() { |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 50 | const ::testing::TestInfo* const testInfo = |
| 51 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 52 | ALOGV("Begin test: %s.%s", testInfo->test_case_name(), |
| 53 | testInfo->name()); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 56 | ~BufferQueueTest() { |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 57 | const ::testing::TestInfo* const testInfo = |
| 58 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 59 | ALOGV("End test: %s.%s", testInfo->test_case_name(), |
| 60 | testInfo->name()); |
| 61 | } |
| 62 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 63 | void GetMinUndequeuedBufferCount(int* bufferCount) { |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 64 | ASSERT_TRUE(bufferCount != NULL); |
| 65 | ASSERT_EQ(OK, mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, |
| 66 | bufferCount)); |
| 67 | ASSERT_GE(*bufferCount, 0); |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 70 | void createBufferQueue() { |
| 71 | BufferQueue::createBufferQueue(&mProducer, &mConsumer); |
| 72 | } |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 73 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 74 | void testBufferItem(const IGraphicBufferProducer::QueueBufferInput& input, |
| 75 | const BufferItem& item) { |
| 76 | int64_t timestamp; |
| 77 | bool isAutoTimestamp; |
| 78 | android_dataspace dataSpace; |
| 79 | Rect crop; |
| 80 | int scalingMode; |
| 81 | uint32_t transform; |
| 82 | sp<Fence> fence; |
| 83 | |
| 84 | input.deflate(×tamp, &isAutoTimestamp, &dataSpace, &crop, |
| 85 | &scalingMode, &transform, &fence, NULL); |
| 86 | ASSERT_EQ(timestamp, item.mTimestamp); |
| 87 | ASSERT_EQ(isAutoTimestamp, item.mIsAutoTimestamp); |
| 88 | ASSERT_EQ(dataSpace, item.mDataSpace); |
| 89 | ASSERT_EQ(crop, item.mCrop); |
| 90 | ASSERT_EQ(static_cast<uint32_t>(scalingMode), item.mScalingMode); |
| 91 | ASSERT_EQ(transform, item.mTransform); |
| 92 | ASSERT_EQ(fence, item.mFence); |
| 93 | } |
| 94 | |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 95 | sp<IGraphicBufferProducer> mProducer; |
| 96 | sp<IGraphicBufferConsumer> mConsumer; |
| 97 | }; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 98 | |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 99 | static const uint32_t TEST_DATA = 0x12345678u; |
| 100 | |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 101 | // XXX: Tests that fork a process to hold the BufferQueue must run before tests |
| 102 | // that use a local BufferQueue, or else Binder will get unhappy |
Kalle Raita | 88752d7 | 2017-03-27 14:11:54 -0700 | [diff] [blame] | 103 | // |
| 104 | // In one instance this was a crash in the createBufferQueue where the |
| 105 | // binder call to create a buffer allocator apparently got garbage back. |
| 106 | // See b/36592665. |
| 107 | TEST_F(BufferQueueTest, DISABLED_BufferQueueInAnotherProcess) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 108 | const String16 PRODUCER_NAME = String16("BQTestProducer"); |
| 109 | const String16 CONSUMER_NAME = String16("BQTestConsumer"); |
| 110 | |
| 111 | pid_t forkPid = fork(); |
| 112 | ASSERT_NE(forkPid, -1); |
| 113 | |
| 114 | if (forkPid == 0) { |
| 115 | // Child process |
Dan Stoza | b3d0bdf | 2014-04-07 16:33:59 -0700 | [diff] [blame] | 116 | sp<IGraphicBufferProducer> producer; |
| 117 | sp<IGraphicBufferConsumer> consumer; |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 118 | BufferQueue::createBufferQueue(&producer, &consumer); |
| 119 | sp<IServiceManager> serviceManager = defaultServiceManager(); |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 120 | serviceManager->addService(PRODUCER_NAME, IInterface::asBinder(producer)); |
| 121 | serviceManager->addService(CONSUMER_NAME, IInterface::asBinder(consumer)); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 122 | ProcessState::self()->startThreadPool(); |
| 123 | IPCThreadState::self()->joinThreadPool(); |
| 124 | LOG_ALWAYS_FATAL("Shouldn't be here"); |
| 125 | } |
| 126 | |
| 127 | sp<IServiceManager> serviceManager = defaultServiceManager(); |
| 128 | sp<IBinder> binderProducer = |
| 129 | serviceManager->getService(PRODUCER_NAME); |
| 130 | mProducer = interface_cast<IGraphicBufferProducer>(binderProducer); |
| 131 | EXPECT_TRUE(mProducer != NULL); |
| 132 | sp<IBinder> binderConsumer = |
| 133 | serviceManager->getService(CONSUMER_NAME); |
| 134 | mConsumer = interface_cast<IGraphicBufferConsumer>(binderConsumer); |
| 135 | EXPECT_TRUE(mConsumer != NULL); |
| 136 | |
| 137 | sp<DummyConsumer> dc(new DummyConsumer); |
| 138 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 139 | IGraphicBufferProducer::QueueBufferOutput output; |
| 140 | ASSERT_EQ(OK, |
| 141 | mProducer->connect(NULL, NATIVE_WINDOW_API_CPU, false, &output)); |
| 142 | |
| 143 | int slot; |
| 144 | sp<Fence> fence; |
| 145 | sp<GraphicBuffer> buffer; |
| 146 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 147 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 148 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 149 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 150 | |
| 151 | uint32_t* dataIn; |
| 152 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 153 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 154 | *dataIn = TEST_DATA; |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 155 | ASSERT_EQ(OK, buffer->unlock()); |
| 156 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 157 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 158 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 159 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 160 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 161 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 162 | BufferItem item; |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 163 | ASSERT_EQ(OK, mConsumer->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 | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 169 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 170 | } |
| 171 | |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 172 | TEST_F(BufferQueueTest, AcquireBuffer_ExceedsMaxAcquireCount_Fails) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 173 | createBufferQueue(); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 174 | sp<DummyConsumer> dc(new DummyConsumer); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 175 | mConsumer->consumerConnect(dc, false); |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 176 | IGraphicBufferProducer::QueueBufferOutput qbo; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 177 | mProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 178 | &qbo); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 179 | mProducer->setMaxDequeuedBufferCount(3); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 180 | |
| 181 | int slot; |
| 182 | sp<Fence> fence; |
| 183 | sp<GraphicBuffer> buf; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 184 | IGraphicBufferProducer::QueueBufferInput qbi(0, false, |
| 185 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 186 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 187 | BufferItem item; |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 188 | |
| 189 | for (int i = 0; i < 2; i++) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 190 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 191 | mProducer->dequeueBuffer(&slot, &fence, 1, 1, 0, |
| 192 | GRALLOC_USAGE_SW_READ_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 193 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buf)); |
| 194 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, qbi, &qbo)); |
| 195 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 198 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 199 | mProducer->dequeueBuffer(&slot, &fence, 1, 1, 0, |
| 200 | GRALLOC_USAGE_SW_READ_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 201 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buf)); |
| 202 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, qbi, &qbo)); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 203 | |
| 204 | // Acquire the third buffer, which should fail. |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 205 | ASSERT_EQ(INVALID_OPERATION, mConsumer->acquireBuffer(&item, 0)); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 208 | TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithIllegalValues_ReturnsError) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 209 | createBufferQueue(); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 210 | sp<DummyConsumer> dc(new DummyConsumer); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 211 | mConsumer->consumerConnect(dc, false); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 212 | |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 213 | EXPECT_EQ(OK, mConsumer->setMaxBufferCount(10)); |
| 214 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount(10)); |
| 215 | |
| 216 | IGraphicBufferProducer::QueueBufferOutput qbo; |
| 217 | mProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 218 | &qbo); |
| 219 | mProducer->setMaxDequeuedBufferCount(3); |
| 220 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 221 | int minBufferCount; |
| 222 | ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 223 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount( |
| 224 | minBufferCount - 1)); |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 225 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 226 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount(0)); |
| 227 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount(-3)); |
| 228 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount( |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 229 | BufferQueue::MAX_MAX_ACQUIRED_BUFFERS+1)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 230 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount(100)); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 231 | |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 232 | int slot; |
| 233 | sp<Fence> fence; |
| 234 | sp<GraphicBuffer> buf; |
| 235 | IGraphicBufferProducer::QueueBufferInput qbi(0, false, |
| 236 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 237 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 238 | BufferItem item; |
| 239 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(3)); |
| 240 | for (int i = 0; i < 3; i++) { |
| 241 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 242 | mProducer->dequeueBuffer(&slot, &fence, 1, 1, 0, |
| 243 | GRALLOC_USAGE_SW_READ_OFTEN, nullptr)); |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 244 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buf)); |
| 245 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, qbi, &qbo)); |
| 246 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 247 | } |
| 248 | |
| 249 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxAcquiredBufferCount(2)); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithLegalValues_Succeeds) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 253 | createBufferQueue(); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 254 | sp<DummyConsumer> dc(new DummyConsumer); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 255 | mConsumer->consumerConnect(dc, false); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 256 | |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 257 | IGraphicBufferProducer::QueueBufferOutput qbo; |
| 258 | mProducer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false, |
| 259 | &qbo); |
| 260 | mProducer->setMaxDequeuedBufferCount(2); |
| 261 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 262 | int minBufferCount; |
| 263 | ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); |
| 264 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 265 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(1)); |
| 266 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(2)); |
| 267 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(minBufferCount)); |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 268 | |
| 269 | int slot; |
| 270 | sp<Fence> fence; |
| 271 | sp<GraphicBuffer> buf; |
| 272 | IGraphicBufferProducer::QueueBufferInput qbi(0, false, |
| 273 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 274 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 275 | BufferItem item; |
| 276 | |
| 277 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 278 | mProducer->dequeueBuffer(&slot, &fence, 1, 1, 0, |
| 279 | GRALLOC_USAGE_SW_READ_OFTEN, nullptr)); |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 280 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buf)); |
| 281 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, qbi, &qbo)); |
| 282 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 283 | |
| 284 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(3)); |
| 285 | |
| 286 | for (int i = 0; i < 2; i++) { |
| 287 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 288 | mProducer->dequeueBuffer(&slot, &fence, 1, 1, 0, |
| 289 | GRALLOC_USAGE_SW_READ_OFTEN, nullptr)); |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 290 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buf)); |
| 291 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, qbi, &qbo)); |
| 292 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 293 | } |
| 294 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 295 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount( |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 296 | BufferQueue::MAX_MAX_ACQUIRED_BUFFERS)); |
| 297 | } |
| 298 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 299 | TEST_F(BufferQueueTest, SetMaxBufferCountWithLegalValues_Succeeds) { |
| 300 | createBufferQueue(); |
| 301 | sp<DummyConsumer> dc(new DummyConsumer); |
| 302 | mConsumer->consumerConnect(dc, false); |
| 303 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 304 | // Test shared buffer mode |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 305 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(1)); |
| 306 | } |
| 307 | |
| 308 | TEST_F(BufferQueueTest, SetMaxBufferCountWithIllegalValues_ReturnsError) { |
| 309 | createBufferQueue(); |
| 310 | sp<DummyConsumer> dc(new DummyConsumer); |
| 311 | mConsumer->consumerConnect(dc, false); |
| 312 | |
| 313 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxBufferCount(0)); |
| 314 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxBufferCount( |
| 315 | BufferQueue::NUM_BUFFER_SLOTS + 1)); |
| 316 | |
| 317 | EXPECT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(5)); |
| 318 | EXPECT_EQ(BAD_VALUE, mConsumer->setMaxBufferCount(3)); |
| 319 | } |
| 320 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 321 | TEST_F(BufferQueueTest, DetachAndReattachOnProducerSide) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 322 | createBufferQueue(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 323 | sp<DummyConsumer> dc(new DummyConsumer); |
| 324 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 325 | IGraphicBufferProducer::QueueBufferOutput output; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 326 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 327 | NATIVE_WINDOW_API_CPU, false, &output)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 328 | |
| 329 | ASSERT_EQ(BAD_VALUE, mProducer->detachBuffer(-1)); // Index too low |
| 330 | ASSERT_EQ(BAD_VALUE, mProducer->detachBuffer( |
| 331 | BufferQueueDefs::NUM_BUFFER_SLOTS)); // Index too high |
| 332 | ASSERT_EQ(BAD_VALUE, mProducer->detachBuffer(0)); // Not dequeued |
| 333 | |
| 334 | int slot; |
| 335 | sp<Fence> fence; |
| 336 | sp<GraphicBuffer> buffer; |
| 337 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 338 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 339 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 340 | ASSERT_EQ(BAD_VALUE, mProducer->detachBuffer(slot)); // Not requested |
| 341 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 342 | ASSERT_EQ(OK, mProducer->detachBuffer(slot)); |
| 343 | ASSERT_EQ(BAD_VALUE, mProducer->detachBuffer(slot)); // Not dequeued |
| 344 | |
| 345 | sp<GraphicBuffer> safeToClobberBuffer; |
| 346 | // Can no longer request buffer from this slot |
| 347 | ASSERT_EQ(BAD_VALUE, mProducer->requestBuffer(slot, &safeToClobberBuffer)); |
| 348 | |
| 349 | uint32_t* dataIn; |
| 350 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 351 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 352 | *dataIn = TEST_DATA; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 353 | ASSERT_EQ(OK, buffer->unlock()); |
| 354 | |
| 355 | int newSlot; |
| 356 | ASSERT_EQ(BAD_VALUE, mProducer->attachBuffer(NULL, safeToClobberBuffer)); |
| 357 | ASSERT_EQ(BAD_VALUE, mProducer->attachBuffer(&newSlot, NULL)); |
| 358 | |
| 359 | ASSERT_EQ(OK, mProducer->attachBuffer(&newSlot, buffer)); |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 360 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 361 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 362 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 363 | ASSERT_EQ(OK, mProducer->queueBuffer(newSlot, input, &output)); |
| 364 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 365 | BufferItem item; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 366 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, static_cast<nsecs_t>(0))); |
| 367 | |
| 368 | uint32_t* dataOut; |
| 369 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 370 | reinterpret_cast<void**>(&dataOut))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 371 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 372 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | TEST_F(BufferQueueTest, DetachAndReattachOnConsumerSide) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 376 | createBufferQueue(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 377 | sp<DummyConsumer> dc(new DummyConsumer); |
| 378 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 379 | IGraphicBufferProducer::QueueBufferOutput output; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 380 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 381 | NATIVE_WINDOW_API_CPU, false, &output)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 382 | |
| 383 | int slot; |
| 384 | sp<Fence> fence; |
| 385 | sp<GraphicBuffer> buffer; |
| 386 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 387 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 388 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 389 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 390 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 391 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 392 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 393 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 394 | |
| 395 | ASSERT_EQ(BAD_VALUE, mConsumer->detachBuffer(-1)); // Index too low |
| 396 | ASSERT_EQ(BAD_VALUE, mConsumer->detachBuffer( |
| 397 | BufferQueueDefs::NUM_BUFFER_SLOTS)); // Index too high |
| 398 | ASSERT_EQ(BAD_VALUE, mConsumer->detachBuffer(0)); // Not acquired |
| 399 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 400 | BufferItem item; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 401 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, static_cast<nsecs_t>(0))); |
| 402 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 403 | ASSERT_EQ(OK, mConsumer->detachBuffer(item.mSlot)); |
| 404 | ASSERT_EQ(BAD_VALUE, mConsumer->detachBuffer(item.mSlot)); // Not acquired |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 405 | |
| 406 | uint32_t* dataIn; |
| 407 | ASSERT_EQ(OK, item.mGraphicBuffer->lock( |
| 408 | GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 409 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 410 | *dataIn = TEST_DATA; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 411 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
| 412 | |
| 413 | int newSlot; |
| 414 | sp<GraphicBuffer> safeToClobberBuffer; |
| 415 | ASSERT_EQ(BAD_VALUE, mConsumer->attachBuffer(NULL, safeToClobberBuffer)); |
| 416 | ASSERT_EQ(BAD_VALUE, mConsumer->attachBuffer(&newSlot, NULL)); |
| 417 | ASSERT_EQ(OK, mConsumer->attachBuffer(&newSlot, item.mGraphicBuffer)); |
| 418 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 419 | ASSERT_EQ(OK, mConsumer->releaseBuffer(newSlot, 0, EGL_NO_DISPLAY, |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 420 | EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 421 | |
| 422 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 423 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 424 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 425 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 426 | |
| 427 | uint32_t* dataOut; |
| 428 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 429 | reinterpret_cast<void**>(&dataOut))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 430 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 431 | ASSERT_EQ(OK, buffer->unlock()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | TEST_F(BufferQueueTest, MoveFromConsumerToProducer) { |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 435 | createBufferQueue(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 436 | sp<DummyConsumer> dc(new DummyConsumer); |
| 437 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 438 | IGraphicBufferProducer::QueueBufferOutput output; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 439 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 440 | NATIVE_WINDOW_API_CPU, false, &output)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 441 | |
| 442 | int slot; |
| 443 | sp<Fence> fence; |
| 444 | sp<GraphicBuffer> buffer; |
| 445 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 446 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 447 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 448 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 449 | |
| 450 | uint32_t* dataIn; |
| 451 | ASSERT_EQ(OK, buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, |
| 452 | reinterpret_cast<void**>(&dataIn))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 453 | *dataIn = TEST_DATA; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 454 | ASSERT_EQ(OK, buffer->unlock()); |
| 455 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 456 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 457 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 458 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 459 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 460 | |
Dan Stoza | cf3834d | 2015-03-11 14:04:22 -0700 | [diff] [blame] | 461 | BufferItem item; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 462 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, static_cast<nsecs_t>(0))); |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 463 | ASSERT_EQ(OK, mConsumer->detachBuffer(item.mSlot)); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 464 | |
| 465 | int newSlot; |
| 466 | ASSERT_EQ(OK, mProducer->attachBuffer(&newSlot, item.mGraphicBuffer)); |
| 467 | ASSERT_EQ(OK, mProducer->queueBuffer(newSlot, input, &output)); |
| 468 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, static_cast<nsecs_t>(0))); |
| 469 | |
| 470 | uint32_t* dataOut; |
| 471 | ASSERT_EQ(OK, item.mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 472 | reinterpret_cast<void**>(&dataOut))); |
Dan Stoza | f8cebe5 | 2015-04-20 12:09:38 -0700 | [diff] [blame] | 473 | ASSERT_EQ(*dataOut, TEST_DATA); |
Dan Stoza | 1a0b861 | 2014-03-20 15:36:31 -0700 | [diff] [blame] | 474 | ASSERT_EQ(OK, item.mGraphicBuffer->unlock()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 477 | TEST_F(BufferQueueTest, TestDisallowingAllocation) { |
| 478 | createBufferQueue(); |
| 479 | sp<DummyConsumer> dc(new DummyConsumer); |
| 480 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 481 | IGraphicBufferProducer::QueueBufferOutput output; |
| 482 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 483 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 484 | |
| 485 | static const uint32_t WIDTH = 320; |
| 486 | static const uint32_t HEIGHT = 240; |
| 487 | |
| 488 | ASSERT_EQ(OK, mConsumer->setDefaultBufferSize(WIDTH, HEIGHT)); |
| 489 | |
| 490 | int slot; |
| 491 | sp<Fence> fence; |
| 492 | sp<GraphicBuffer> buffer; |
| 493 | // This should return an error since it would require an allocation |
| 494 | ASSERT_EQ(OK, mProducer->allowAllocation(false)); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 495 | ASSERT_EQ(WOULD_BLOCK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, |
| 496 | 0, GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 497 | |
| 498 | // This should succeed, now that we've lifted the prohibition |
| 499 | ASSERT_EQ(OK, mProducer->allowAllocation(true)); |
| 500 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 501 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, |
| 502 | GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 503 | |
| 504 | // Release the previous buffer back to the BufferQueue |
| 505 | mProducer->cancelBuffer(slot, fence); |
| 506 | |
| 507 | // This should fail since we're requesting a different size |
| 508 | ASSERT_EQ(OK, mProducer->allowAllocation(false)); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 509 | ASSERT_EQ(WOULD_BLOCK, mProducer->dequeueBuffer(&slot, &fence, |
| 510 | WIDTH * 2, HEIGHT * 2, 0, GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr)); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 513 | TEST_F(BufferQueueTest, TestGenerationNumbers) { |
| 514 | createBufferQueue(); |
| 515 | sp<DummyConsumer> dc(new DummyConsumer); |
| 516 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 517 | IGraphicBufferProducer::QueueBufferOutput output; |
| 518 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 519 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 520 | |
| 521 | ASSERT_EQ(OK, mProducer->setGenerationNumber(1)); |
| 522 | |
| 523 | // Get one buffer to play with |
| 524 | int slot; |
| 525 | sp<Fence> fence; |
| 526 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 527 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 528 | |
| 529 | sp<GraphicBuffer> buffer; |
| 530 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 531 | |
| 532 | // Ensure that the generation number we set propagates to allocated buffers |
| 533 | ASSERT_EQ(1U, buffer->getGenerationNumber()); |
| 534 | |
| 535 | ASSERT_EQ(OK, mProducer->detachBuffer(slot)); |
| 536 | |
| 537 | ASSERT_EQ(OK, mProducer->setGenerationNumber(2)); |
| 538 | |
| 539 | // These should fail, since we've changed the generation number on the queue |
| 540 | int outSlot; |
| 541 | ASSERT_EQ(BAD_VALUE, mProducer->attachBuffer(&outSlot, buffer)); |
| 542 | ASSERT_EQ(BAD_VALUE, mConsumer->attachBuffer(&outSlot, buffer)); |
| 543 | |
| 544 | buffer->setGenerationNumber(2); |
| 545 | |
| 546 | // This should succeed now that we've changed the buffer's generation number |
| 547 | ASSERT_EQ(OK, mProducer->attachBuffer(&outSlot, buffer)); |
| 548 | |
| 549 | ASSERT_EQ(OK, mProducer->detachBuffer(outSlot)); |
| 550 | |
| 551 | // This should also succeed with the new generation number |
| 552 | ASSERT_EQ(OK, mConsumer->attachBuffer(&outSlot, buffer)); |
| 553 | } |
| 554 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 555 | TEST_F(BufferQueueTest, TestSharedBufferModeWithoutAutoRefresh) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 556 | createBufferQueue(); |
| 557 | sp<DummyConsumer> dc(new DummyConsumer); |
| 558 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 559 | IGraphicBufferProducer::QueueBufferOutput output; |
| 560 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 561 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 562 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 563 | ASSERT_EQ(OK, mProducer->setSharedBufferMode(true)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 564 | |
| 565 | // Get a buffer |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 566 | int sharedSlot; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 567 | sp<Fence> fence; |
| 568 | sp<GraphicBuffer> buffer; |
| 569 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 570 | mProducer->dequeueBuffer(&sharedSlot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 571 | ASSERT_EQ(OK, mProducer->requestBuffer(sharedSlot, &buffer)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 572 | |
| 573 | // Queue the buffer |
| 574 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 575 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 576 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 577 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 578 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 579 | // Repeatedly queue and dequeue a buffer from the producer side, it should |
| 580 | // always return the same one. And we won't run out of buffers because it's |
| 581 | // always the same one and because async mode gets enabled. |
| 582 | int slot; |
| 583 | for (int i = 0; i < 5; i++) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 584 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 585 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 586 | ASSERT_EQ(sharedSlot, slot); |
| 587 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // acquire the buffer |
| 591 | BufferItem item; |
| 592 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 593 | ASSERT_EQ(sharedSlot, item.mSlot); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 594 | testBufferItem(input, item); |
| 595 | ASSERT_EQ(true, item.mQueuedBuffer); |
| 596 | ASSERT_EQ(false, item.mAutoRefresh); |
| 597 | |
| 598 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 599 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 600 | |
| 601 | // attempt to acquire a second time should return no buffer available |
| 602 | ASSERT_EQ(IGraphicBufferConsumer::NO_BUFFER_AVAILABLE, |
| 603 | mConsumer->acquireBuffer(&item, 0)); |
| 604 | } |
| 605 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 606 | TEST_F(BufferQueueTest, TestSharedBufferModeWithAutoRefresh) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 607 | createBufferQueue(); |
| 608 | sp<DummyConsumer> dc(new DummyConsumer); |
| 609 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 610 | IGraphicBufferProducer::QueueBufferOutput output; |
| 611 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 612 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 613 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 614 | ASSERT_EQ(OK, mProducer->setSharedBufferMode(true)); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 615 | ASSERT_EQ(OK, mProducer->setAutoRefresh(true)); |
| 616 | |
| 617 | // Get a buffer |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 618 | int sharedSlot; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 619 | sp<Fence> fence; |
| 620 | sp<GraphicBuffer> buffer; |
| 621 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 622 | mProducer->dequeueBuffer(&sharedSlot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 623 | ASSERT_EQ(OK, mProducer->requestBuffer(sharedSlot, &buffer)); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 624 | |
| 625 | // Queue the buffer |
| 626 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 627 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 628 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 629 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 630 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 631 | // Repeatedly acquire and release a buffer from the consumer side, it should |
| 632 | // always return the same one. |
| 633 | BufferItem item; |
| 634 | for (int i = 0; i < 5; i++) { |
| 635 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 636 | ASSERT_EQ(sharedSlot, item.mSlot); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 637 | testBufferItem(input, item); |
| 638 | ASSERT_EQ(i == 0, item.mQueuedBuffer); |
| 639 | ASSERT_EQ(true, item.mAutoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 640 | |
| 641 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 642 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 643 | } |
| 644 | |
| 645 | // Repeatedly queue and dequeue a buffer from the producer side, it should |
| 646 | // always return the same one. |
| 647 | int slot; |
| 648 | for (int i = 0; i < 5; i++) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 649 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 650 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 651 | ASSERT_EQ(sharedSlot, slot); |
| 652 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // Repeatedly acquire and release a buffer from the consumer side, it should |
| 656 | // always return the same one. First grabbing them from the queue and then |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 657 | // when the queue is empty, returning the shared buffer. |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 658 | for (int i = 0; i < 10; i++) { |
| 659 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 660 | ASSERT_EQ(sharedSlot, item.mSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 661 | ASSERT_EQ(0, item.mTimestamp); |
| 662 | ASSERT_EQ(false, item.mIsAutoTimestamp); |
| 663 | ASSERT_EQ(HAL_DATASPACE_UNKNOWN, item.mDataSpace); |
| 664 | ASSERT_EQ(Rect(0, 0, 1, 1), item.mCrop); |
| 665 | ASSERT_EQ(NATIVE_WINDOW_SCALING_MODE_FREEZE, item.mScalingMode); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 666 | ASSERT_EQ(0u, item.mTransform); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 667 | ASSERT_EQ(Fence::NO_FENCE, item.mFence); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 668 | ASSERT_EQ(i == 0, item.mQueuedBuffer); |
| 669 | ASSERT_EQ(true, item.mAutoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 670 | |
| 671 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 672 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 673 | } |
| 674 | } |
| 675 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 676 | TEST_F(BufferQueueTest, TestSharedBufferModeUsingAlreadyDequeuedBuffer) { |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 677 | createBufferQueue(); |
| 678 | sp<DummyConsumer> dc(new DummyConsumer); |
| 679 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 680 | IGraphicBufferProducer::QueueBufferOutput output; |
| 681 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 682 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 683 | |
| 684 | // Dequeue a buffer |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 685 | int sharedSlot; |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 686 | sp<Fence> fence; |
| 687 | sp<GraphicBuffer> buffer; |
| 688 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 689 | mProducer->dequeueBuffer(&sharedSlot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 690 | ASSERT_EQ(OK, mProducer->requestBuffer(sharedSlot, &buffer)); |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 691 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 692 | // Enable shared buffer mode |
| 693 | ASSERT_EQ(OK, mProducer->setSharedBufferMode(true)); |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 694 | |
| 695 | // Queue the buffer |
| 696 | IGraphicBufferProducer::QueueBufferInput input(0, false, |
| 697 | HAL_DATASPACE_UNKNOWN, Rect(0, 0, 1, 1), |
| 698 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 699 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 700 | |
| 701 | // Repeatedly queue and dequeue a buffer from the producer side, it should |
| 702 | // always return the same one. And we won't run out of buffers because it's |
| 703 | // always the same one and because async mode gets enabled. |
| 704 | int slot; |
| 705 | for (int i = 0; i < 5; i++) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 706 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 707 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 708 | ASSERT_EQ(sharedSlot, slot); |
| 709 | ASSERT_EQ(OK, mProducer->queueBuffer(sharedSlot, input, &output)); |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // acquire the buffer |
| 713 | BufferItem item; |
| 714 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 715 | ASSERT_EQ(sharedSlot, item.mSlot); |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 716 | testBufferItem(input, item); |
| 717 | ASSERT_EQ(true, item.mQueuedBuffer); |
| 718 | ASSERT_EQ(false, item.mAutoRefresh); |
| 719 | |
| 720 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 721 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 722 | |
| 723 | // attempt to acquire a second time should return no buffer available |
| 724 | ASSERT_EQ(IGraphicBufferConsumer::NO_BUFFER_AVAILABLE, |
| 725 | mConsumer->acquireBuffer(&item, 0)); |
| 726 | } |
| 727 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 728 | TEST_F(BufferQueueTest, TestTimeouts) { |
| 729 | createBufferQueue(); |
| 730 | sp<DummyConsumer> dc(new DummyConsumer); |
| 731 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 732 | IGraphicBufferProducer::QueueBufferOutput output; |
| 733 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 734 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 735 | |
| 736 | // Fill up the queue. Since the controlledByApp flags are set to true, this |
| 737 | // queue should be in non-blocking mode, and we should be recycling the same |
| 738 | // two buffers |
| 739 | for (int i = 0; i < 5; ++i) { |
| 740 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 741 | sp<Fence> fence = Fence::NO_FENCE; |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 742 | auto result = mProducer->dequeueBuffer( |
| 743 | &slot, &fence, 0, 0, 0, 0, nullptr); |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 744 | if (i < 2) { |
| 745 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
| 746 | result); |
| 747 | } else { |
| 748 | ASSERT_EQ(OK, result); |
| 749 | } |
| 750 | sp<GraphicBuffer> buffer; |
| 751 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 752 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 753 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 754 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 755 | IGraphicBufferProducer::QueueBufferOutput output{}; |
| 756 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 757 | } |
| 758 | |
| 759 | const auto TIMEOUT = ms2ns(250); |
| 760 | mProducer->setDequeueTimeout(TIMEOUT); |
| 761 | |
| 762 | // Setting a timeout will change the BufferQueue into blocking mode (with |
| 763 | // one droppable buffer in the queue and one free from the previous |
| 764 | // dequeue/queues), so dequeue and queue two more buffers: one to replace |
| 765 | // the current droppable buffer, and a second to max out the buffer count |
| 766 | sp<GraphicBuffer> buffer; // Save a buffer to attach later |
| 767 | for (int i = 0; i < 2; ++i) { |
| 768 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 769 | sp<Fence> fence = Fence::NO_FENCE; |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 770 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 771 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 772 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 773 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 774 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 775 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 776 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 777 | } |
| 778 | |
| 779 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 780 | sp<Fence> fence = Fence::NO_FENCE; |
| 781 | auto startTime = systemTime(); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 782 | ASSERT_EQ(TIMED_OUT, mProducer->dequeueBuffer( |
| 783 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 784 | ASSERT_GE(systemTime() - startTime, TIMEOUT); |
| 785 | |
| 786 | // We're technically attaching the same buffer multiple times (since we |
| 787 | // queued it previously), but that doesn't matter for this test |
| 788 | startTime = systemTime(); |
| 789 | ASSERT_EQ(TIMED_OUT, mProducer->attachBuffer(&slot, buffer)); |
| 790 | ASSERT_GE(systemTime() - startTime, TIMEOUT); |
| 791 | } |
| 792 | |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 793 | TEST_F(BufferQueueTest, CanAttachWhileDisallowingAllocation) { |
| 794 | createBufferQueue(); |
| 795 | sp<DummyConsumer> dc(new DummyConsumer); |
| 796 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 797 | IGraphicBufferProducer::QueueBufferOutput output; |
| 798 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 799 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 800 | |
| 801 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 802 | sp<Fence> sourceFence; |
| 803 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 804 | mProducer->dequeueBuffer(&slot, &sourceFence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 805 | sp<GraphicBuffer> buffer; |
| 806 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 807 | ASSERT_EQ(OK, mProducer->detachBuffer(slot)); |
| 808 | |
| 809 | ASSERT_EQ(OK, mProducer->allowAllocation(false)); |
| 810 | |
| 811 | slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 812 | ASSERT_EQ(OK, mProducer->attachBuffer(&slot, buffer)); |
| 813 | } |
| 814 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 815 | TEST_F(BufferQueueTest, CanRetrieveLastQueuedBuffer) { |
| 816 | createBufferQueue(); |
| 817 | sp<DummyConsumer> dc(new DummyConsumer); |
| 818 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 819 | IGraphicBufferProducer::QueueBufferOutput output; |
| 820 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 821 | NATIVE_WINDOW_API_CPU, false, &output)); |
| 822 | |
| 823 | // Dequeue and queue the first buffer, storing the handle |
| 824 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 825 | sp<Fence> fence; |
| 826 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 827 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 828 | sp<GraphicBuffer> firstBuffer; |
| 829 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &firstBuffer)); |
| 830 | |
| 831 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 832 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 833 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 834 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 835 | |
| 836 | // Dequeue a second buffer |
| 837 | slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 838 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 839 | mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 840 | sp<GraphicBuffer> secondBuffer; |
| 841 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &secondBuffer)); |
| 842 | |
| 843 | // Ensure it's a new buffer |
| 844 | ASSERT_NE(firstBuffer->getNativeBuffer()->handle, |
| 845 | secondBuffer->getNativeBuffer()->handle); |
| 846 | |
| 847 | // Queue the second buffer |
| 848 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 849 | |
| 850 | // Acquire and release both buffers |
| 851 | for (size_t i = 0; i < 2; ++i) { |
| 852 | BufferItem item; |
| 853 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 854 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 855 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 856 | } |
| 857 | |
| 858 | // Make sure we got the second buffer back |
| 859 | sp<GraphicBuffer> returnedBuffer; |
| 860 | sp<Fence> returnedFence; |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 861 | float transform[16]; |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 862 | ASSERT_EQ(OK, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 863 | mProducer->getLastQueuedBuffer(&returnedBuffer, &returnedFence, |
| 864 | transform)); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 865 | ASSERT_EQ(secondBuffer->getNativeBuffer()->handle, |
| 866 | returnedBuffer->getNativeBuffer()->handle); |
| 867 | } |
| 868 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 869 | TEST_F(BufferQueueTest, TestOccupancyHistory) { |
| 870 | createBufferQueue(); |
| 871 | sp<DummyConsumer> dc(new DummyConsumer); |
| 872 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 873 | IGraphicBufferProducer::QueueBufferOutput output; |
| 874 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 875 | NATIVE_WINDOW_API_CPU, false, &output)); |
| 876 | |
| 877 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 878 | sp<Fence> fence = Fence::NO_FENCE; |
| 879 | sp<GraphicBuffer> buffer = nullptr; |
| 880 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 881 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 882 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 883 | BufferItem item{}; |
| 884 | |
| 885 | // Preallocate, dequeue, request, and cancel 3 buffers so we don't get |
| 886 | // BUFFER_NEEDS_REALLOCATION below |
| 887 | int slots[3] = {}; |
| 888 | mProducer->setMaxDequeuedBufferCount(3); |
| 889 | for (size_t i = 0; i < 3; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 890 | status_t result = mProducer->dequeueBuffer(&slots[i], &fence, |
| 891 | 0, 0, 0, 0, nullptr); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 892 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, result); |
| 893 | ASSERT_EQ(OK, mProducer->requestBuffer(slots[i], &buffer)); |
| 894 | } |
| 895 | for (size_t i = 0; i < 3; ++i) { |
| 896 | ASSERT_EQ(OK, mProducer->cancelBuffer(slots[i], Fence::NO_FENCE)); |
| 897 | } |
| 898 | |
| 899 | // Create 3 segments |
| 900 | |
| 901 | // The first segment is a two-buffer segment, so we only put one buffer into |
| 902 | // the queue at a time |
| 903 | for (size_t i = 0; i < 5; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 904 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 905 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 906 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 907 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 908 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 909 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 910 | std::this_thread::sleep_for(16ms); |
| 911 | } |
| 912 | |
| 913 | // Sleep between segments |
| 914 | std::this_thread::sleep_for(500ms); |
| 915 | |
| 916 | // The second segment is a double-buffer segment. It starts the same as the |
| 917 | // two-buffer segment, but then at the end, we put two buffers in the queue |
| 918 | // at the same time before draining it. |
| 919 | for (size_t i = 0; i < 5; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 920 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 921 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 922 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 923 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 924 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 925 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 926 | std::this_thread::sleep_for(16ms); |
| 927 | } |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 928 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 929 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 930 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 931 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 932 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 933 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 934 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 935 | std::this_thread::sleep_for(16ms); |
| 936 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 937 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 938 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 939 | |
| 940 | // Sleep between segments |
| 941 | std::this_thread::sleep_for(500ms); |
| 942 | |
| 943 | // The third segment is a triple-buffer segment, so the queue is switching |
| 944 | // between one buffer and two buffers deep. |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 945 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 946 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 947 | for (size_t i = 0; i < 5; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 948 | ASSERT_EQ(OK, mProducer->dequeueBuffer( |
| 949 | &slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 950 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 951 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 952 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 953 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 954 | std::this_thread::sleep_for(16ms); |
| 955 | } |
| 956 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 957 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 958 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 959 | |
| 960 | // Now we read the segments |
| 961 | std::vector<OccupancyTracker::Segment> history; |
| 962 | ASSERT_EQ(OK, mConsumer->getOccupancyHistory(false, &history)); |
| 963 | |
| 964 | // Since we didn't force a flush, we should only get the first two segments |
| 965 | // (since the third segment hasn't been closed out by the appearance of a |
| 966 | // new segment yet) |
| 967 | ASSERT_EQ(2u, history.size()); |
| 968 | |
| 969 | // The first segment (which will be history[1], since the newest segment |
| 970 | // should be at the front of the vector) should be a two-buffer segment, |
| 971 | // which implies that the occupancy average should be between 0 and 1, and |
| 972 | // usedThirdBuffer should be false |
| 973 | const auto& firstSegment = history[1]; |
| 974 | ASSERT_EQ(5u, firstSegment.numFrames); |
| 975 | ASSERT_LT(0, firstSegment.occupancyAverage); |
| 976 | ASSERT_GT(1, firstSegment.occupancyAverage); |
| 977 | ASSERT_EQ(false, firstSegment.usedThirdBuffer); |
| 978 | |
| 979 | // The second segment should be a double-buffered segment, which implies that |
| 980 | // the occupancy average should be between 0 and 1, but usedThirdBuffer |
| 981 | // should be true |
| 982 | const auto& secondSegment = history[0]; |
| 983 | ASSERT_EQ(7u, secondSegment.numFrames); |
| 984 | ASSERT_LT(0, secondSegment.occupancyAverage); |
| 985 | ASSERT_GT(1, secondSegment.occupancyAverage); |
| 986 | ASSERT_EQ(true, secondSegment.usedThirdBuffer); |
| 987 | |
| 988 | // If we read the segments again without flushing, we shouldn't get any new |
| 989 | // segments |
| 990 | ASSERT_EQ(OK, mConsumer->getOccupancyHistory(false, &history)); |
| 991 | ASSERT_EQ(0u, history.size()); |
| 992 | |
| 993 | // Read the segments again, this time forcing a flush so we get the third |
| 994 | // segment |
| 995 | ASSERT_EQ(OK, mConsumer->getOccupancyHistory(true, &history)); |
| 996 | ASSERT_EQ(1u, history.size()); |
| 997 | |
| 998 | // This segment should be a triple-buffered segment, which implies that the |
| 999 | // occupancy average should be between 1 and 2, and usedThirdBuffer should |
| 1000 | // be true |
| 1001 | const auto& thirdSegment = history[0]; |
| 1002 | ASSERT_EQ(6u, thirdSegment.numFrames); |
| 1003 | ASSERT_LT(1, thirdSegment.occupancyAverage); |
| 1004 | ASSERT_GT(2, thirdSegment.occupancyAverage); |
| 1005 | ASSERT_EQ(true, thirdSegment.usedThirdBuffer); |
| 1006 | } |
| 1007 | |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1008 | TEST_F(BufferQueueTest, TestDiscardFreeBuffers) { |
| 1009 | createBufferQueue(); |
| 1010 | sp<DummyConsumer> dc(new DummyConsumer); |
| 1011 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, false)); |
| 1012 | IGraphicBufferProducer::QueueBufferOutput output; |
| 1013 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 1014 | NATIVE_WINDOW_API_CPU, false, &output)); |
| 1015 | |
| 1016 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 1017 | sp<Fence> fence = Fence::NO_FENCE; |
| 1018 | sp<GraphicBuffer> buffer = nullptr; |
| 1019 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 1020 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 1021 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 1022 | BufferItem item{}; |
| 1023 | |
| 1024 | // Preallocate, dequeue, request, and cancel 4 buffers so we don't get |
| 1025 | // BUFFER_NEEDS_REALLOCATION below |
| 1026 | int slots[4] = {}; |
| 1027 | mProducer->setMaxDequeuedBufferCount(4); |
| 1028 | for (size_t i = 0; i < 4; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1029 | status_t result = mProducer->dequeueBuffer(&slots[i], &fence, |
| 1030 | 0, 0, 0, 0, nullptr); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1031 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, result); |
| 1032 | ASSERT_EQ(OK, mProducer->requestBuffer(slots[i], &buffer)); |
| 1033 | } |
| 1034 | for (size_t i = 0; i < 4; ++i) { |
| 1035 | ASSERT_EQ(OK, mProducer->cancelBuffer(slots[i], Fence::NO_FENCE)); |
| 1036 | } |
| 1037 | |
| 1038 | // Get buffers in all states: dequeued, filled, acquired, free |
| 1039 | |
| 1040 | // Fill 3 buffers |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1041 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1042 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1043 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1044 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1045 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1046 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1047 | // Dequeue 1 buffer |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1048 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1049 | |
| 1050 | // Acquire and free 1 buffer |
| 1051 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 1052 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 1053 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 1054 | // Acquire 1 buffer, leaving 1 filled buffer in queue |
| 1055 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 1056 | |
| 1057 | // Now discard the free buffers |
| 1058 | ASSERT_EQ(OK, mConsumer->discardFreeBuffers()); |
| 1059 | |
| 1060 | // Check no free buffers in dump |
| 1061 | String8 dumpString; |
Dan Stoza | 0c9a1ed | 2017-04-06 15:10:21 -0700 | [diff] [blame] | 1062 | mConsumer->dumpState(String8{}, &dumpString); |
Eino-Ville Talvala | bc2df65 | 2016-07-21 17:06:58 -0700 | [diff] [blame] | 1063 | |
| 1064 | // Parse the dump to ensure that all buffer slots that are FREE also |
| 1065 | // have a null GraphicBuffer |
| 1066 | // Fragile - assumes the following format for the dump for a buffer entry: |
| 1067 | // ":%p\][^:]*state=FREE" where %p is the buffer pointer in hex. |
| 1068 | ssize_t idx = dumpString.find("state=FREE"); |
| 1069 | while (idx != -1) { |
| 1070 | ssize_t bufferPtrIdx = idx - 1; |
| 1071 | while (bufferPtrIdx > 0) { |
| 1072 | if (dumpString[bufferPtrIdx] == ':') { |
| 1073 | bufferPtrIdx++; |
| 1074 | break; |
| 1075 | } |
| 1076 | bufferPtrIdx--; |
| 1077 | } |
| 1078 | ASSERT_GT(bufferPtrIdx, 0) << "Can't parse queue dump to validate"; |
| 1079 | ssize_t nullPtrIdx = dumpString.find("0x0]", bufferPtrIdx); |
| 1080 | ASSERT_EQ(bufferPtrIdx, nullPtrIdx) << "Free buffer not discarded"; |
| 1081 | idx = dumpString.find("FREE", idx + 1); |
| 1082 | } |
| 1083 | } |
| 1084 | |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1085 | TEST_F(BufferQueueTest, TestBufferReplacedInQueueBuffer) { |
| 1086 | createBufferQueue(); |
| 1087 | sp<DummyConsumer> dc(new DummyConsumer); |
| 1088 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 1089 | IGraphicBufferProducer::QueueBufferOutput output; |
| 1090 | ASSERT_EQ(OK, mProducer->connect(new DummyProducerListener, |
| 1091 | NATIVE_WINDOW_API_CPU, true, &output)); |
| 1092 | ASSERT_EQ(OK, mConsumer->setMaxAcquiredBufferCount(1)); |
| 1093 | |
| 1094 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 1095 | sp<Fence> fence = Fence::NO_FENCE; |
| 1096 | sp<GraphicBuffer> buffer = nullptr; |
| 1097 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 1098 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 1099 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 1100 | BufferItem item{}; |
| 1101 | |
| 1102 | // Preallocate, dequeue, request, and cancel 2 buffers so we don't get |
| 1103 | // BUFFER_NEEDS_REALLOCATION below |
| 1104 | int slots[2] = {}; |
| 1105 | ASSERT_EQ(OK, mProducer->setMaxDequeuedBufferCount(2)); |
| 1106 | for (size_t i = 0; i < 2; ++i) { |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1107 | status_t result = mProducer->dequeueBuffer(&slots[i], &fence, |
| 1108 | 0, 0, 0, 0, nullptr); |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1109 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, result); |
| 1110 | ASSERT_EQ(OK, mProducer->requestBuffer(slots[i], &buffer)); |
| 1111 | } |
| 1112 | for (size_t i = 0; i < 2; ++i) { |
| 1113 | ASSERT_EQ(OK, mProducer->cancelBuffer(slots[i], Fence::NO_FENCE)); |
| 1114 | } |
| 1115 | |
| 1116 | // Fill 2 buffers without consumer consuming them. Verify that all |
| 1117 | // queued buffer returns proper bufferReplaced flag |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1118 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1119 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1120 | ASSERT_EQ(false, output.bufferReplaced); |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1121 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1122 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1123 | ASSERT_EQ(true, output.bufferReplaced); |
| 1124 | } |
| 1125 | |
Dan Stoza | d4c6f99 | 2017-03-21 13:43:22 -0700 | [diff] [blame] | 1126 | TEST_F(BufferQueueTest, TestStaleBufferHandleSentAfterDisconnect) { |
| 1127 | createBufferQueue(); |
| 1128 | sp<DummyConsumer> dc(new DummyConsumer); |
| 1129 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 1130 | IGraphicBufferProducer::QueueBufferOutput output; |
| 1131 | sp<IProducerListener> dummyListener(new DummyProducerListener); |
| 1132 | ASSERT_EQ(OK, mProducer->connect(dummyListener, NATIVE_WINDOW_API_CPU, |
| 1133 | true, &output)); |
| 1134 | |
| 1135 | int slot = BufferQueue::INVALID_BUFFER_SLOT; |
| 1136 | sp<Fence> fence = Fence::NO_FENCE; |
| 1137 | sp<GraphicBuffer> buffer = nullptr; |
| 1138 | IGraphicBufferProducer::QueueBufferInput input(0ull, true, |
| 1139 | HAL_DATASPACE_UNKNOWN, Rect::INVALID_RECT, |
| 1140 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE); |
| 1141 | |
| 1142 | // Dequeue, request, and queue one buffer |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1143 | status_t result = mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, |
| 1144 | nullptr); |
Dan Stoza | d4c6f99 | 2017-03-21 13:43:22 -0700 | [diff] [blame] | 1145 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, result); |
| 1146 | ASSERT_EQ(OK, mProducer->requestBuffer(slot, &buffer)); |
| 1147 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1148 | |
| 1149 | // Acquire and release the buffer. Upon acquiring, the buffer handle should |
| 1150 | // be non-null since this is the first time we've acquired this slot. |
| 1151 | BufferItem item; |
| 1152 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 1153 | ASSERT_EQ(slot, item.mSlot); |
| 1154 | ASSERT_NE(nullptr, item.mGraphicBuffer.get()); |
| 1155 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 1156 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 1157 | |
| 1158 | // Dequeue and queue the buffer again |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1159 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | d4c6f99 | 2017-03-21 13:43:22 -0700 | [diff] [blame] | 1160 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1161 | |
| 1162 | // Acquire and release the buffer again. Upon acquiring, the buffer handle |
| 1163 | // should be null since this is not the first time we've acquired this slot. |
| 1164 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 1165 | ASSERT_EQ(slot, item.mSlot); |
| 1166 | ASSERT_EQ(nullptr, item.mGraphicBuffer.get()); |
| 1167 | ASSERT_EQ(OK, mConsumer->releaseBuffer(item.mSlot, item.mFrameNumber, |
| 1168 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE)); |
| 1169 | |
| 1170 | // Dequeue and queue the buffer again |
Ian Elliott | ad74480 | 2017-07-26 21:46:43 +0000 | [diff] [blame^] | 1171 | ASSERT_EQ(OK, mProducer->dequeueBuffer(&slot, &fence, 0, 0, 0, 0, nullptr)); |
Dan Stoza | d4c6f99 | 2017-03-21 13:43:22 -0700 | [diff] [blame] | 1172 | ASSERT_EQ(OK, mProducer->queueBuffer(slot, input, &output)); |
| 1173 | |
| 1174 | // Disconnect the producer end. This should clear all of the slots and mark |
| 1175 | // the buffer in the queue as stale. |
| 1176 | ASSERT_EQ(OK, mProducer->disconnect(NATIVE_WINDOW_API_CPU)); |
| 1177 | |
| 1178 | // Acquire the buffer again. Upon acquiring, the buffer handle should not be |
| 1179 | // null since the queued buffer should have been marked as stale, which |
| 1180 | // should trigger the BufferQueue to resend the buffer handle. |
| 1181 | ASSERT_EQ(OK, mConsumer->acquireBuffer(&item, 0)); |
| 1182 | ASSERT_EQ(slot, item.mSlot); |
| 1183 | ASSERT_NE(nullptr, item.mGraphicBuffer.get()); |
| 1184 | } |
| 1185 | |
Wonsik Kim | 3e198b2 | 2017-04-07 15:43:16 -0700 | [diff] [blame] | 1186 | TEST_F(BufferQueueTest, TestProducerConnectDisconnect) { |
| 1187 | createBufferQueue(); |
| 1188 | sp<DummyConsumer> dc(new DummyConsumer); |
| 1189 | ASSERT_EQ(OK, mConsumer->consumerConnect(dc, true)); |
| 1190 | IGraphicBufferProducer::QueueBufferOutput output; |
| 1191 | sp<IProducerListener> dummyListener(new DummyProducerListener); |
| 1192 | ASSERT_EQ(NO_INIT, mProducer->disconnect(NATIVE_WINDOW_API_CPU)); |
| 1193 | ASSERT_EQ(OK, mProducer->connect( |
| 1194 | dummyListener, NATIVE_WINDOW_API_CPU, true, &output)); |
| 1195 | ASSERT_EQ(BAD_VALUE, mProducer->connect( |
| 1196 | dummyListener, NATIVE_WINDOW_API_MEDIA, true, &output)); |
| 1197 | |
| 1198 | ASSERT_EQ(BAD_VALUE, mProducer->disconnect(NATIVE_WINDOW_API_MEDIA)); |
| 1199 | ASSERT_EQ(OK, mProducer->disconnect(NATIVE_WINDOW_API_CPU)); |
| 1200 | ASSERT_EQ(NO_INIT, mProducer->disconnect(NATIVE_WINDOW_API_CPU)); |
| 1201 | } |
| 1202 | |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 1203 | } // namespace android |