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 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include <utils/String8.h> |
| 23 | #include <utils/threads.h> |
| 24 | |
| 25 | #include <ui/GraphicBuffer.h> |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 26 | |
| 27 | #include <gui/BufferQueue.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class BufferQueueTest : public ::testing::Test { |
| 32 | protected: |
| 33 | |
| 34 | BufferQueueTest() {} |
| 35 | |
| 36 | virtual void SetUp() { |
| 37 | const ::testing::TestInfo* const testInfo = |
| 38 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 39 | ALOGV("Begin test: %s.%s", testInfo->test_case_name(), |
| 40 | testInfo->name()); |
| 41 | |
| 42 | mBQ = new BufferQueue(); |
| 43 | } |
| 44 | |
| 45 | virtual void TearDown() { |
| 46 | mBQ.clear(); |
| 47 | |
| 48 | const ::testing::TestInfo* const testInfo = |
| 49 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 50 | ALOGV("End test: %s.%s", testInfo->test_case_name(), |
| 51 | testInfo->name()); |
| 52 | } |
| 53 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 54 | void GetMinUndequeuedBufferCount(int* bufferCount) { |
| 55 | ASSERT_NE((void*)NULL, bufferCount); |
| 56 | ASSERT_EQ(OK, mBQ->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, bufferCount)); |
| 57 | ASSERT_LE(0, *bufferCount); // non-negative |
| 58 | } |
| 59 | |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 60 | sp<BufferQueue> mBQ; |
| 61 | }; |
| 62 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 63 | struct DummyConsumer : public BnConsumerListener { |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 64 | virtual void onFrameAvailable() {} |
| 65 | virtual void onBuffersReleased() {} |
| 66 | }; |
| 67 | |
| 68 | TEST_F(BufferQueueTest, AcquireBuffer_ExceedsMaxAcquireCount_Fails) { |
| 69 | sp<DummyConsumer> dc(new DummyConsumer); |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 70 | mBQ->consumerConnect(dc, false); |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 71 | IGraphicBufferProducer::QueueBufferOutput qbo; |
Mathias Agopian | 799f512 | 2013-09-17 15:55:18 -0700 | [diff] [blame] | 72 | mBQ->connect(NULL, NATIVE_WINDOW_API_CPU, false, &qbo); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 73 | mBQ->setBufferCount(4); |
| 74 | |
| 75 | int slot; |
| 76 | sp<Fence> fence; |
| 77 | sp<GraphicBuffer> buf; |
Andy McFadden | 8b308ed | 2013-08-19 08:56:07 -0700 | [diff] [blame] | 78 | IGraphicBufferProducer::QueueBufferInput qbi(0, false, Rect(0, 0, 1, 1), |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 79 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, false, Fence::NO_FENCE); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 80 | BufferQueue::BufferItem item; |
| 81 | |
| 82 | for (int i = 0; i < 2; i++) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 83 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 84 | mBQ->dequeueBuffer(&slot, &fence, false, 1, 1, 0, |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 85 | GRALLOC_USAGE_SW_READ_OFTEN)); |
| 86 | ASSERT_EQ(OK, mBQ->requestBuffer(slot, &buf)); |
| 87 | ASSERT_EQ(OK, mBQ->queueBuffer(slot, qbi, &qbo)); |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 88 | ASSERT_EQ(OK, mBQ->acquireBuffer(&item, 0)); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 91 | ASSERT_EQ(IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 92 | mBQ->dequeueBuffer(&slot, &fence, false, 1, 1, 0, |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 93 | GRALLOC_USAGE_SW_READ_OFTEN)); |
| 94 | ASSERT_EQ(OK, mBQ->requestBuffer(slot, &buf)); |
| 95 | ASSERT_EQ(OK, mBQ->queueBuffer(slot, qbi, &qbo)); |
| 96 | |
| 97 | // Acquire the third buffer, which should fail. |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 98 | ASSERT_EQ(INVALID_OPERATION, mBQ->acquireBuffer(&item, 0)); |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 101 | TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithIllegalValues_ReturnsError) { |
| 102 | sp<DummyConsumer> dc(new DummyConsumer); |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 103 | mBQ->consumerConnect(dc, false); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 104 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 105 | int minBufferCount; |
| 106 | ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); |
| 107 | EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(minBufferCount - 1)); |
| 108 | |
| 109 | EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(0)); |
| 110 | EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(-3)); |
| 111 | EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount( |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 112 | BufferQueue::MAX_MAX_ACQUIRED_BUFFERS+1)); |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 113 | EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(100)); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithLegalValues_Succeeds) { |
| 117 | sp<DummyConsumer> dc(new DummyConsumer); |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 118 | mBQ->consumerConnect(dc, false); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 119 | |
Igor Murashkin | 7ea777f | 2013-11-18 16:58:36 -0800 | [diff] [blame] | 120 | int minBufferCount; |
| 121 | ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); |
| 122 | |
| 123 | EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(1)); |
| 124 | EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(2)); |
| 125 | EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(minBufferCount)); |
| 126 | EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount( |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 127 | BufferQueue::MAX_MAX_ACQUIRED_BUFFERS)); |
| 128 | } |
| 129 | |
Jamie Gennis | 9e75ddd | 2012-08-31 15:32:45 -0700 | [diff] [blame] | 130 | } // namespace android |