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