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