From 7ea777f097784492f880623067becac1b276f884 Mon Sep 17 00:00:00 2001 From: Igor Murashkin Date: Mon, 18 Nov 2013 16:58:36 -0800 Subject: [PATCH] gui: Add tests for IGraphicBufferProducer * Basic tests only. Needs more complicated queue/dequeue tests. * Also needs consumer-side tests to really be thorough. Change-Id: I1099dd56d65b6e9dfa15377726d6054ce657c0ca --- include/gui/BufferQueue.h | 2 +- include/gui/IGraphicBufferProducer.h | 11 +- libs/gui/BufferQueue.cpp | 14 +- libs/gui/tests/Android.mk | 1 + libs/gui/tests/BufferQueue_test.cpp | 28 +- .../gui/tests/IGraphicBufferProducer_test.cpp | 568 ++++++++++++++++++ 6 files changed, 611 insertions(+), 13 deletions(-) create mode 100644 libs/gui/tests/IGraphicBufferProducer_test.cpp diff --git a/include/gui/BufferQueue.h b/include/gui/BufferQueue.h index cab7e8684..15dc64524 100644 --- a/include/gui/BufferQueue.h +++ b/include/gui/BufferQueue.h @@ -107,7 +107,7 @@ public: // // This will fail if the producer has dequeued any buffers, or if // bufferCount is invalid. bufferCount must generally be a value - // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS + // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS // (inclusive). It may also be set to zero (the default) to indicate // that the producer does not wish to set a value. The minimum value // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, diff --git a/include/gui/IGraphicBufferProducer.h b/include/gui/IGraphicBufferProducer.h index 461be6f6e..70025308c 100644 --- a/include/gui/IGraphicBufferProducer.h +++ b/include/gui/IGraphicBufferProducer.h @@ -84,9 +84,13 @@ public: // This function should not be called when there are any dequeued buffer // slots, doing so will result in a BAD_VALUE error returned. // - // The buffer count should be at most NUM_BUFFER_SLOTS, but at least - // the minimum undequeued buffer count (inclusive). The minimum value - // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS); + // The buffer count should be at most NUM_BUFFER_SLOTS (inclusive), but at least + // the minimum undequeued buffer count (exclusive). The minimum value + // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS). + // In particular the range is (minUndequeudBuffers, NUM_BUFFER_SLOTS]. + // + // The buffer count may also be set to 0 (the default), to indicate that + // the producer does not wish to set a value. // // Return of a value other than NO_ERROR means an error has occurred: // * NO_INIT - the buffer queue has been abandoned. @@ -315,6 +319,7 @@ public: // * BAD_VALUE - one of the following has occurred: // * the producer is already connected // * api was out of range (see above). + // * output was NULL. // * DEAD_OBJECT - the token is hosted by an already-dead process // // Additional negative errors may be returned by the internals, they diff --git a/libs/gui/BufferQueue.cpp b/libs/gui/BufferQueue.cpp index a95ac64bf..2fa0433ce 100644 --- a/libs/gui/BufferQueue.cpp +++ b/libs/gui/BufferQueue.cpp @@ -195,6 +195,11 @@ int BufferQueue::query(int what, int* outValue) ATRACE_CALL(); Mutex::Autolock lock(mMutex); + if (outValue == NULL) { + ST_LOGE("query: outValue was NULL"); + return BAD_VALUE; + } + if (mAbandoned) { ST_LOGE("query: BufferQueue has been abandoned!"); return NO_INIT; @@ -654,10 +659,15 @@ retry: return NO_INIT; } + if (output == NULL) { + ST_LOGE("connect: output was NULL"); + return BAD_VALUE; + } + if (mConnectedApi != NO_CONNECTED_API) { ST_LOGE("connect: already connected (cur=%d, req=%d)", mConnectedApi, api); - return -EINVAL; + return BAD_VALUE; } // If we disconnect and reconnect quickly, we can be in a state where our slots are @@ -693,7 +703,7 @@ retry: } break; default: - err = -EINVAL; + err = BAD_VALUE; break; } diff --git a/libs/gui/tests/Android.mk b/libs/gui/tests/Android.mk index 21bd875e3..045c7b571 100644 --- a/libs/gui/tests/Android.mk +++ b/libs/gui/tests/Android.mk @@ -9,6 +9,7 @@ LOCAL_MODULE_TAGS := tests LOCAL_SRC_FILES := \ BufferQueue_test.cpp \ CpuConsumer_test.cpp \ + IGraphicBufferProducer_test.cpp \ SurfaceTextureClient_test.cpp \ SurfaceTexture_test.cpp \ Surface_test.cpp \ diff --git a/libs/gui/tests/BufferQueue_test.cpp b/libs/gui/tests/BufferQueue_test.cpp index 03c1a29a7..06f9a9297 100644 --- a/libs/gui/tests/BufferQueue_test.cpp +++ b/libs/gui/tests/BufferQueue_test.cpp @@ -52,6 +52,12 @@ protected: testInfo->name()); } + void GetMinUndequeuedBufferCount(int* bufferCount) { + ASSERT_NE((void*)NULL, bufferCount); + ASSERT_EQ(OK, mBQ->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, bufferCount)); + ASSERT_LE(0, *bufferCount); // non-negative + } + sp mBQ; }; @@ -97,20 +103,28 @@ TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithIllegalValues_ReturnsError) sp dc(new DummyConsumer); mBQ->consumerConnect(dc, false); - ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(0)); - ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(-3)); - ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount( + int minBufferCount; + ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); + EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(minBufferCount - 1)); + + EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(0)); + EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(-3)); + EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount( BufferQueue::MAX_MAX_ACQUIRED_BUFFERS+1)); - ASSERT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(100)); + EXPECT_EQ(BAD_VALUE, mBQ->setMaxAcquiredBufferCount(100)); } TEST_F(BufferQueueTest, SetMaxAcquiredBufferCountWithLegalValues_Succeeds) { sp dc(new DummyConsumer); mBQ->consumerConnect(dc, false); - ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount(1)); - ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount(2)); - ASSERT_EQ(OK, mBQ->setMaxAcquiredBufferCount( + int minBufferCount; + ASSERT_NO_FATAL_FAILURE(GetMinUndequeuedBufferCount(&minBufferCount)); + + EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(1)); + EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(2)); + EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount(minBufferCount)); + EXPECT_EQ(OK, mBQ->setMaxAcquiredBufferCount( BufferQueue::MAX_MAX_ACQUIRED_BUFFERS)); } diff --git a/libs/gui/tests/IGraphicBufferProducer_test.cpp b/libs/gui/tests/IGraphicBufferProducer_test.cpp new file mode 100644 index 000000000..d16177b14 --- /dev/null +++ b/libs/gui/tests/IGraphicBufferProducer_test.cpp @@ -0,0 +1,568 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "IGraphicBufferProducer_test" +//#define LOG_NDEBUG 0 + +#include + +#include +#include + +#include +#include + +#include + +#include + +#define ASSERT_OK(x) ASSERT_EQ(OK, (x)) +#define EXPECT_OK(x) EXPECT_EQ(OK, (x)) + +#define TEST_TOKEN ((IBinder*)(NULL)) +#define TEST_API NATIVE_WINDOW_API_CPU +#define TEST_API_OTHER NATIVE_WINDOW_API_EGL // valid API that's not TEST_API +#define TEST_CONTROLLED_BY_APP false +#define TEST_PRODUCER_USAGE_BITS (0) + +// TODO: Make these public constants in a header +enum { + // Default dimensions before setDefaultBufferSize is called + DEFAULT_WIDTH = 1, + DEFAULT_HEIGHT = 1, + + // Default format before setDefaultBufferFormat is called + DEFAULT_FORMAT = HAL_PIXEL_FORMAT_RGBA_8888, + + // Default transform hint before setTransformHint is called + DEFAULT_TRANSFORM_HINT = 0, +}; + +namespace android { + +namespace { +// Parameters for a generic "valid" input for queueBuffer. +const int64_t QUEUE_BUFFER_INPUT_TIMESTAMP = 1384888611; +const bool QUEUE_BUFFER_INPUT_IS_AUTO_TIMESTAMP = false; +const Rect QUEUE_BUFFER_INPUT_RECT = Rect(DEFAULT_WIDTH, DEFAULT_HEIGHT); +const int QUEUE_BUFFER_INPUT_SCALING_MODE = 0; +const int QUEUE_BUFFER_INPUT_TRANSFORM = 0; +const bool QUEUE_BUFFER_INPUT_ASYNC = false; +const sp QUEUE_BUFFER_INPUT_FENCE = Fence::NO_FENCE; +}; // namespace anonymous + +struct DummyConsumer : public BnConsumerListener { + virtual void onFrameAvailable() {} + virtual void onBuffersReleased() {} +}; + +class IGraphicBufferProducerTest : public ::testing::Test { +protected: + + IGraphicBufferProducerTest() {} + + virtual void SetUp() { + const ::testing::TestInfo* const testInfo = + ::testing::UnitTest::GetInstance()->current_test_info(); + ALOGV("Begin test: %s.%s", testInfo->test_case_name(), + testInfo->name()); + + mBQ = new BufferQueue(); + mDC = new DummyConsumer; + + mProducer = mBQ; + mConsumer = mBQ; + + // Test check: Can't connect producer if no consumer yet + ASSERT_EQ(NO_INIT, TryConnectProducer()); + + // Must connect consumer before producer connects will succeed. + ASSERT_OK(mConsumer->consumerConnect(mDC, /*controlledByApp*/false)); + } + + virtual void TearDown() { + const ::testing::TestInfo* const testInfo = + ::testing::UnitTest::GetInstance()->current_test_info(); + ALOGV("End test: %s.%s", testInfo->test_case_name(), + testInfo->name()); + } + + status_t TryConnectProducer() { + IGraphicBufferProducer::QueueBufferOutput output; + return mProducer->connect(TEST_TOKEN, + TEST_API, + TEST_CONTROLLED_BY_APP, + &output); + // TODO: use params to vary token, api, producercontrolledbyapp, etc + } + + // Connect to a producer in a 'correct' fashion. + // Precondition: Consumer is connected. + void ConnectProducer() { + ASSERT_OK(TryConnectProducer()); + } + + // Create a generic "valid" input for queueBuffer + // -- uses the default buffer format, width, etc. + static IGraphicBufferProducer::QueueBufferInput CreateBufferInput() { + return QueueBufferInputBuilder().build(); + } + + // Builder pattern to slightly vary *almost* correct input + // -- avoids copying and pasting + struct QueueBufferInputBuilder { + QueueBufferInputBuilder() { + timestamp = QUEUE_BUFFER_INPUT_TIMESTAMP; + isAutoTimestamp = QUEUE_BUFFER_INPUT_IS_AUTO_TIMESTAMP; + crop = QUEUE_BUFFER_INPUT_RECT; + scalingMode = QUEUE_BUFFER_INPUT_SCALING_MODE; + transform = QUEUE_BUFFER_INPUT_TRANSFORM; + async = QUEUE_BUFFER_INPUT_ASYNC; + fence = QUEUE_BUFFER_INPUT_FENCE; + } + + IGraphicBufferProducer::QueueBufferInput build() { + return IGraphicBufferProducer::QueueBufferInput( + timestamp, + isAutoTimestamp, + crop, + scalingMode, + transform, + async, + fence); + } + + QueueBufferInputBuilder& setTimestamp(int64_t timestamp) { + this->timestamp = timestamp; + return *this; + } + + QueueBufferInputBuilder& setIsAutoTimestamp(bool isAutoTimestamp) { + this->isAutoTimestamp = isAutoTimestamp; + return *this; + } + + QueueBufferInputBuilder& setCrop(Rect crop) { + this->crop = crop; + return *this; + } + + QueueBufferInputBuilder& setScalingMode(int scalingMode) { + this->scalingMode = scalingMode; + return *this; + } + + QueueBufferInputBuilder& setTransform(uint32_t transform) { + this->transform = transform; + return *this; + } + + QueueBufferInputBuilder& setAsync(bool async) { + this->async = async; + return *this; + } + + QueueBufferInputBuilder& setFence(sp fence) { + this->fence = fence; + return *this; + } + + private: + int64_t timestamp; + bool isAutoTimestamp; + Rect crop; + int scalingMode; + uint32_t transform; + int async; + sp fence; + }; // struct QueueBufferInputBuilder + + // To easily store dequeueBuffer results into containers + struct DequeueBufferResult { + int slot; + sp fence; + }; + + status_t dequeueBuffer(bool async, uint32_t w, uint32_t h, uint32_t format, uint32_t usage, DequeueBufferResult* result) { + return mProducer->dequeueBuffer(&result->slot, &result->fence, async, w, h, format, usage); + } + +private: // hide from test body + sp mBQ; + sp mDC; + +protected: // accessible from test body + sp mProducer; + sp mConsumer; +}; + +TEST_F(IGraphicBufferProducerTest, ConnectFirst_ReturnsError) { + IGraphicBufferProducer::QueueBufferOutput output; + + // NULL output returns BAD_VALUE + EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN, + TEST_API, + TEST_CONTROLLED_BY_APP, + /*output*/NULL)); + + // Invalid API returns bad value + EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN, + /*api*/0xDEADBEEF, + TEST_CONTROLLED_BY_APP, + &output)); + + // TODO: get a token from a dead process somehow +} + +TEST_F(IGraphicBufferProducerTest, ConnectAgain_ReturnsError) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + // Can't connect when there is already a producer connected + IGraphicBufferProducer::QueueBufferOutput output; + EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN, + TEST_API, + TEST_CONTROLLED_BY_APP, + &output)); + + ASSERT_OK(mConsumer->consumerDisconnect()); + // Can't connect when IGBP is abandoned + EXPECT_EQ(NO_INIT, mProducer->connect(TEST_TOKEN, + TEST_API, + TEST_CONTROLLED_BY_APP, + &output)); +} + +TEST_F(IGraphicBufferProducerTest, Disconnect_Succeeds) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + ASSERT_OK(mProducer->disconnect(TEST_API)); +} + + +TEST_F(IGraphicBufferProducerTest, Disconnect_ReturnsError) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + // Must disconnect with same API number + ASSERT_EQ(BAD_VALUE, mProducer->disconnect(TEST_API_OTHER)); + // API must not be out of range + ASSERT_EQ(BAD_VALUE, mProducer->disconnect(/*api*/0xDEADBEEF)); + + // TODO: somehow kill mProducer so that this returns DEAD_OBJECT +} + +TEST_F(IGraphicBufferProducerTest, Query_Succeeds) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + // TODO: Make these constants in header + const int DEFAULT_CONSUMER_USAGE_BITS = 0; + + int value = -1; + EXPECT_OK(mProducer->query(NATIVE_WINDOW_WIDTH, &value)); + EXPECT_EQ(DEFAULT_WIDTH, value); + + EXPECT_OK(mProducer->query(NATIVE_WINDOW_HEIGHT, &value)); + EXPECT_EQ(DEFAULT_HEIGHT, value); + + EXPECT_OK(mProducer->query(NATIVE_WINDOW_FORMAT, &value)); + EXPECT_EQ(DEFAULT_FORMAT, value); + + EXPECT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &value)); + EXPECT_LE(0, value); + EXPECT_GE(BufferQueue::NUM_BUFFER_SLOTS, value); + + EXPECT_OK(mProducer->query(NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value)); + EXPECT_FALSE(value); // Can't run behind when we haven't touched the queue + + EXPECT_OK(mProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &value)); + EXPECT_EQ(DEFAULT_CONSUMER_USAGE_BITS, value); + +} + +TEST_F(IGraphicBufferProducerTest, Query_ReturnsError) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + // One past the end of the last 'query' enum value. Update this if we add more enums. + const int NATIVE_WINDOW_QUERY_LAST_OFF_BY_ONE = NATIVE_WINDOW_CONSUMER_USAGE_BITS + 1; + + int value; + // What was out of range + EXPECT_EQ(BAD_VALUE, mProducer->query(/*what*/-1, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(/*what*/0xDEADBEEF, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_QUERY_LAST_OFF_BY_ONE, &value)); + + // Some enums from window.h are 'invalid' + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_CONCRETE_TYPE, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_DEFAULT_WIDTH, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_DEFAULT_HEIGHT, &value)); + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_TRANSFORM_HINT, &value)); + // TODO: Consider documented the above enums as unsupported or make a new enum for IGBP + + // Value was NULL + EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_FORMAT, /*value*/NULL)); + + ASSERT_OK(mConsumer->consumerDisconnect()); + + // BQ was abandoned + EXPECT_EQ(NO_INIT, mProducer->query(NATIVE_WINDOW_FORMAT, &value)); + + // TODO: other things in window.h that are supported by Surface::query + // but not by BufferQueue::query +} + +// TODO: queue under more complicated situations not involving just a single buffer +TEST_F(IGraphicBufferProducerTest, Queue_Succeeds) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + int dequeuedSlot = -1; + sp dequeuedFence; + + // XX: OK to assume first call returns this flag or not? Not really documented. + ASSERT_EQ(OK | IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, + mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence, + QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS)); + + EXPECT_LE(0, dequeuedSlot); + EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, dequeuedSlot); + + // Request the buffer (pre-requisite for queueing) + sp dequeuedBuffer; + ASSERT_OK(mProducer->requestBuffer(dequeuedSlot, &dequeuedBuffer)); + + // A generic "valid" input + IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput(); + IGraphicBufferProducer::QueueBufferOutput output; + + // Queue the buffer back into the BQ + ASSERT_OK(mProducer->queueBuffer(dequeuedSlot, input, &output)); + + { + uint32_t width; + uint32_t height; + uint32_t transformHint; + uint32_t numPendingBuffers; + + output.deflate(&width, &height, &transformHint, &numPendingBuffers); + + EXPECT_EQ(DEFAULT_WIDTH, width); + EXPECT_EQ(DEFAULT_HEIGHT, height); + EXPECT_EQ(DEFAULT_TRANSFORM_HINT, transformHint); + EXPECT_EQ(1, numPendingBuffers); // since queueBuffer was called exactly once + } + + // Buffer was not in the dequeued state + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); +} + +TEST_F(IGraphicBufferProducerTest, Queue_ReturnsError) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + // Invalid slot number + { + // A generic "valid" input + IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/-1, input, &output)); + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/0xDEADBEEF, input, &output)); + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(BufferQueue::NUM_BUFFER_SLOTS, + input, &output)); + } + + // Slot was not in the dequeued state (all slots start out in Free state) + { + IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/0, input, &output)); + } + + // Put the slot into the "dequeued" state for the rest of the test + int dequeuedSlot = -1; + sp dequeuedFence; + + ASSERT_EQ(OK | IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, + mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence, + QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS)); + + // Slot was enqueued without requesting a buffer + { + IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); + } + + // Request the buffer so that the rest of the tests don't fail on earlier checks. + sp dequeuedBuffer; + ASSERT_OK(mProducer->requestBuffer(dequeuedSlot, &dequeuedBuffer)); + + // Fence was NULL + { + sp nullFence = NULL; + + IGraphicBufferProducer::QueueBufferInput input = + QueueBufferInputBuilder().setFence(nullFence).build(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); + } + + // Scaling mode was unknown + { + IGraphicBufferProducer::QueueBufferInput input = + QueueBufferInputBuilder().setScalingMode(-1).build(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); + + input = QueueBufferInputBuilder().setScalingMode(0xDEADBEEF).build(); + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); + } + + // Crop rect is out of bounds of the buffer dimensions + { + IGraphicBufferProducer::QueueBufferInput input = + QueueBufferInputBuilder().setCrop(Rect(DEFAULT_WIDTH + 1, DEFAULT_HEIGHT + 1)) + .build(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output)); + } + + // Abandon the buffer queue so that the last test fails + ASSERT_OK(mConsumer->consumerDisconnect()); + + // The buffer queue has been abandoned. + { + IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput(); + IGraphicBufferProducer::QueueBufferOutput output; + + EXPECT_EQ(NO_INIT, mProducer->queueBuffer(dequeuedSlot, input, &output)); + } +} + +TEST_F(IGraphicBufferProducerTest, CancelBuffer_DoesntCrash) { + ASSERT_NO_FATAL_FAILURE(ConnectProducer()); + + int dequeuedSlot = -1; + sp dequeuedFence; + + ASSERT_EQ(OK | IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION, + mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence, + QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS)); + + // No return code, but at least test that it doesn't blow up... + // TODO: add a return code + mProducer->cancelBuffer(dequeuedSlot, dequeuedFence); +} + +TEST_F(IGraphicBufferProducerTest, SetBufferCount_Succeeds) { + + // The producer does not wish to set a buffer count + EXPECT_OK(mProducer->setBufferCount(0)) << "bufferCount: " << 0; + // TODO: how to test "0" buffer count? + + int minBuffers; + ASSERT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minBuffers)); + + // The MIN_UNDEQUEUED_BUFFERS limit is exclusive, so need to increment by at least 1 + minBuffers++; + + ASSERT_OK(mProducer->setBufferCount(minBuffers)) << "bufferCount: " << minBuffers; + + std::vector dequeueList; + + // Should now be able to dequeue up to minBuffers times + for (int i = 0; i < minBuffers; ++i) { + DequeueBufferResult result; + + EXPECT_LE(OK, + dequeueBuffer(QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS, &result)) + << "iteration: " << i << ", slot: " << result.slot; + + dequeueList.push_back(result); + } + + // Cancel every buffer, so we can set buffer count again + for (int i = 0; i < minBuffers; ++i) { + DequeueBufferResult& result = dequeueList[i]; + mProducer->cancelBuffer(result.slot, result.fence); + } + + ASSERT_OK(mProducer->setBufferCount(BufferQueue::NUM_BUFFER_SLOTS)); + + // Should now be able to dequeue up to NUM_BUFFER_SLOTS times + for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; ++i) { + int dequeuedSlot = -1; + sp dequeuedFence; + + EXPECT_LE(OK, + mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence, + QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS)) + << "iteration: " << i << ", slot: " << dequeuedSlot; + } +} + +TEST_F(IGraphicBufferProducerTest, SetBufferCount_Fails) { + int minBuffers; + ASSERT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minBuffers)); + + // The MIN_UNDEQUEUED_BUFFERS limit is exclusive, so need to increment by at least 1 + minBuffers++; + + // Buffer count was out of range + EXPECT_EQ(BAD_VALUE, mProducer->setBufferCount(-1)) << "bufferCount: " << -1; + EXPECT_EQ(BAD_VALUE, mProducer->setBufferCount(minBuffers - 1)) << "bufferCount: " << minBuffers - 1; + EXPECT_EQ(BAD_VALUE, mProducer->setBufferCount(BufferQueue::NUM_BUFFER_SLOTS + 1)) + << "bufferCount: " << BufferQueue::NUM_BUFFER_SLOTS + 1; + + // Pre-requisite to fail out a valid setBufferCount call + { + int dequeuedSlot = -1; + sp dequeuedFence; + + ASSERT_LE(OK, + mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence, + QUEUE_BUFFER_INPUT_ASYNC, + DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT, + TEST_PRODUCER_USAGE_BITS)) + << "slot: " << dequeuedSlot; + } + + // Client has one or more buffers dequeued + EXPECT_EQ(BAD_VALUE, mProducer->setBufferCount(minBuffers)) << "bufferCount: " << minBuffers; + + // Abandon buffer queue + ASSERT_OK(mConsumer->consumerDisconnect()); + + // Fail because the buffer queue was abandoned + EXPECT_EQ(NO_INIT, mProducer->setBufferCount(minBuffers)) << "bufferCount: " << minBuffers; + +} + +} // namespace android