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