17ea777f097784492f880623067becac1b276f884Igor Murashkin/*
27ea777f097784492f880623067becac1b276f884Igor Murashkin * Copyright (C) 2013 The Android Open Source Project
37ea777f097784492f880623067becac1b276f884Igor Murashkin *
47ea777f097784492f880623067becac1b276f884Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
57ea777f097784492f880623067becac1b276f884Igor Murashkin * you may not use this file except in compliance with the License.
67ea777f097784492f880623067becac1b276f884Igor Murashkin * You may obtain a copy of the License at
77ea777f097784492f880623067becac1b276f884Igor Murashkin *
87ea777f097784492f880623067becac1b276f884Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
97ea777f097784492f880623067becac1b276f884Igor Murashkin *
107ea777f097784492f880623067becac1b276f884Igor Murashkin * Unless required by applicable law or agreed to in writing, software
117ea777f097784492f880623067becac1b276f884Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
127ea777f097784492f880623067becac1b276f884Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ea777f097784492f880623067becac1b276f884Igor Murashkin * See the License for the specific language governing permissions and
147ea777f097784492f880623067becac1b276f884Igor Murashkin * limitations under the License.
157ea777f097784492f880623067becac1b276f884Igor Murashkin */
167ea777f097784492f880623067becac1b276f884Igor Murashkin
177ea777f097784492f880623067becac1b276f884Igor Murashkin#define LOG_TAG "IGraphicBufferProducer_test"
187ea777f097784492f880623067becac1b276f884Igor Murashkin//#define LOG_NDEBUG 0
197ea777f097784492f880623067becac1b276f884Igor Murashkin
207ea777f097784492f880623067becac1b276f884Igor Murashkin#include <gtest/gtest.h>
217ea777f097784492f880623067becac1b276f884Igor Murashkin
227ea777f097784492f880623067becac1b276f884Igor Murashkin#include <utils/String8.h>
237ea777f097784492f880623067becac1b276f884Igor Murashkin#include <utils/threads.h>
247ea777f097784492f880623067becac1b276f884Igor Murashkin
257ea777f097784492f880623067becac1b276f884Igor Murashkin#include <ui/GraphicBuffer.h>
267ea777f097784492f880623067becac1b276f884Igor Murashkin
277ea777f097784492f880623067becac1b276f884Igor Murashkin#include <gui/BufferQueue.h>
28f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza#include <gui/IProducerListener.h>
297ea777f097784492f880623067becac1b276f884Igor Murashkin
307ea777f097784492f880623067becac1b276f884Igor Murashkin#include <vector>
317ea777f097784492f880623067becac1b276f884Igor Murashkin
327ea777f097784492f880623067becac1b276f884Igor Murashkin#define ASSERT_OK(x) ASSERT_EQ(OK, (x))
337ea777f097784492f880623067becac1b276f884Igor Murashkin#define EXPECT_OK(x) EXPECT_EQ(OK, (x))
347ea777f097784492f880623067becac1b276f884Igor Murashkin
35f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza#define TEST_TOKEN ((IProducerListener*)(NULL))
367ea777f097784492f880623067becac1b276f884Igor Murashkin#define TEST_API NATIVE_WINDOW_API_CPU
377ea777f097784492f880623067becac1b276f884Igor Murashkin#define TEST_API_OTHER NATIVE_WINDOW_API_EGL // valid API that's not TEST_API
387ea777f097784492f880623067becac1b276f884Igor Murashkin#define TEST_CONTROLLED_BY_APP false
397ea777f097784492f880623067becac1b276f884Igor Murashkin#define TEST_PRODUCER_USAGE_BITS (0)
407ea777f097784492f880623067becac1b276f884Igor Murashkin
41f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stozanamespace android {
42f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza
43f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stozanamespace {
447ea777f097784492f880623067becac1b276f884Igor Murashkin    // Default dimensions before setDefaultBufferSize is called
45f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const uint32_t DEFAULT_WIDTH = 1;
46f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const uint32_t DEFAULT_HEIGHT = 1;
477ea777f097784492f880623067becac1b276f884Igor Murashkin
487ea777f097784492f880623067becac1b276f884Igor Murashkin    // Default format before setDefaultBufferFormat is called
49f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const PixelFormat DEFAULT_FORMAT = HAL_PIXEL_FORMAT_RGBA_8888;
507ea777f097784492f880623067becac1b276f884Igor Murashkin
517ea777f097784492f880623067becac1b276f884Igor Murashkin    // Default transform hint before setTransformHint is called
52f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const uint32_t DEFAULT_TRANSFORM_HINT = 0;
537ea777f097784492f880623067becac1b276f884Igor Murashkin
54f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    // TODO: Make these constants in header
55f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const int DEFAULT_CONSUMER_USAGE_BITS = 0;
567ea777f097784492f880623067becac1b276f884Igor Murashkin
57f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    // Parameters for a generic "valid" input for queueBuffer.
58f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const int64_t QUEUE_BUFFER_INPUT_TIMESTAMP = 1384888611;
59f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const bool QUEUE_BUFFER_INPUT_IS_AUTO_TIMESTAMP = false;
60f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const android_dataspace QUEUE_BUFFER_INPUT_DATASPACE = HAL_DATASPACE_UNKNOWN;
61f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const Rect QUEUE_BUFFER_INPUT_RECT = Rect(DEFAULT_WIDTH, DEFAULT_HEIGHT);
62f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const int QUEUE_BUFFER_INPUT_SCALING_MODE = 0;
63f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const int QUEUE_BUFFER_INPUT_TRANSFORM = 0;
64f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    const sp<Fence> QUEUE_BUFFER_INPUT_FENCE = Fence::NO_FENCE;
657ea777f097784492f880623067becac1b276f884Igor Murashkin}; // namespace anonymous
667ea777f097784492f880623067becac1b276f884Igor Murashkin
677ea777f097784492f880623067becac1b276f884Igor Murashkinstruct DummyConsumer : public BnConsumerListener {
688dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    virtual void onFrameAvailable(const BufferItem& /* item */) {}
697ea777f097784492f880623067becac1b276f884Igor Murashkin    virtual void onBuffersReleased() {}
7049bfda1fbdf828a8ea63ae2c2fa25739f6e48527Jesse Hall    virtual void onSidebandStreamChanged() {}
717ea777f097784492f880623067becac1b276f884Igor Murashkin};
727ea777f097784492f880623067becac1b276f884Igor Murashkin
737ea777f097784492f880623067becac1b276f884Igor Murashkinclass IGraphicBufferProducerTest : public ::testing::Test {
747ea777f097784492f880623067becac1b276f884Igor Murashkinprotected:
757ea777f097784492f880623067becac1b276f884Igor Murashkin
767ea777f097784492f880623067becac1b276f884Igor Murashkin    IGraphicBufferProducerTest() {}
777ea777f097784492f880623067becac1b276f884Igor Murashkin
787ea777f097784492f880623067becac1b276f884Igor Murashkin    virtual void SetUp() {
797ea777f097784492f880623067becac1b276f884Igor Murashkin        const ::testing::TestInfo* const testInfo =
807ea777f097784492f880623067becac1b276f884Igor Murashkin            ::testing::UnitTest::GetInstance()->current_test_info();
817ea777f097784492f880623067becac1b276f884Igor Murashkin        ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
827ea777f097784492f880623067becac1b276f884Igor Murashkin                testInfo->name());
837ea777f097784492f880623067becac1b276f884Igor Murashkin
847ea777f097784492f880623067becac1b276f884Igor Murashkin        mDC = new DummyConsumer;
857ea777f097784492f880623067becac1b276f884Igor Murashkin
865603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza        BufferQueue::createBufferQueue(&mProducer, &mConsumer);
877ea777f097784492f880623067becac1b276f884Igor Murashkin
887ea777f097784492f880623067becac1b276f884Igor Murashkin        // Test check: Can't connect producer if no consumer yet
897ea777f097784492f880623067becac1b276f884Igor Murashkin        ASSERT_EQ(NO_INIT, TryConnectProducer());
907ea777f097784492f880623067becac1b276f884Igor Murashkin
917ea777f097784492f880623067becac1b276f884Igor Murashkin        // Must connect consumer before producer connects will succeed.
927ea777f097784492f880623067becac1b276f884Igor Murashkin        ASSERT_OK(mConsumer->consumerConnect(mDC, /*controlledByApp*/false));
937ea777f097784492f880623067becac1b276f884Igor Murashkin    }
947ea777f097784492f880623067becac1b276f884Igor Murashkin
957ea777f097784492f880623067becac1b276f884Igor Murashkin    virtual void TearDown() {
967ea777f097784492f880623067becac1b276f884Igor Murashkin        const ::testing::TestInfo* const testInfo =
977ea777f097784492f880623067becac1b276f884Igor Murashkin            ::testing::UnitTest::GetInstance()->current_test_info();
987ea777f097784492f880623067becac1b276f884Igor Murashkin        ALOGV("End test:   %s.%s", testInfo->test_case_name(),
997ea777f097784492f880623067becac1b276f884Igor Murashkin                testInfo->name());
1007ea777f097784492f880623067becac1b276f884Igor Murashkin    }
1017ea777f097784492f880623067becac1b276f884Igor Murashkin
1027ea777f097784492f880623067becac1b276f884Igor Murashkin    status_t TryConnectProducer() {
1037ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
1047ea777f097784492f880623067becac1b276f884Igor Murashkin        return mProducer->connect(TEST_TOKEN,
1057ea777f097784492f880623067becac1b276f884Igor Murashkin                                  TEST_API,
1067ea777f097784492f880623067becac1b276f884Igor Murashkin                                  TEST_CONTROLLED_BY_APP,
1077ea777f097784492f880623067becac1b276f884Igor Murashkin                                  &output);
1087ea777f097784492f880623067becac1b276f884Igor Murashkin        // TODO: use params to vary token, api, producercontrolledbyapp, etc
1097ea777f097784492f880623067becac1b276f884Igor Murashkin    }
1107ea777f097784492f880623067becac1b276f884Igor Murashkin
1117ea777f097784492f880623067becac1b276f884Igor Murashkin    // Connect to a producer in a 'correct' fashion.
1127ea777f097784492f880623067becac1b276f884Igor Murashkin    //   Precondition: Consumer is connected.
1137ea777f097784492f880623067becac1b276f884Igor Murashkin    void ConnectProducer() {
1147ea777f097784492f880623067becac1b276f884Igor Murashkin        ASSERT_OK(TryConnectProducer());
1157ea777f097784492f880623067becac1b276f884Igor Murashkin    }
1167ea777f097784492f880623067becac1b276f884Igor Murashkin
1177ea777f097784492f880623067becac1b276f884Igor Murashkin    // Create a generic "valid" input for queueBuffer
1187ea777f097784492f880623067becac1b276f884Igor Murashkin    // -- uses the default buffer format, width, etc.
1197ea777f097784492f880623067becac1b276f884Igor Murashkin    static IGraphicBufferProducer::QueueBufferInput CreateBufferInput() {
1207ea777f097784492f880623067becac1b276f884Igor Murashkin        return QueueBufferInputBuilder().build();
1217ea777f097784492f880623067becac1b276f884Igor Murashkin    }
1227ea777f097784492f880623067becac1b276f884Igor Murashkin
1237ea777f097784492f880623067becac1b276f884Igor Murashkin    // Builder pattern to slightly vary *almost* correct input
1247ea777f097784492f880623067becac1b276f884Igor Murashkin    // -- avoids copying and pasting
1257ea777f097784492f880623067becac1b276f884Igor Murashkin    struct QueueBufferInputBuilder {
1267ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder() {
1277ea777f097784492f880623067becac1b276f884Igor Murashkin           timestamp = QUEUE_BUFFER_INPUT_TIMESTAMP;
1287ea777f097784492f880623067becac1b276f884Igor Murashkin           isAutoTimestamp = QUEUE_BUFFER_INPUT_IS_AUTO_TIMESTAMP;
12982c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala           dataSpace = QUEUE_BUFFER_INPUT_DATASPACE;
1307ea777f097784492f880623067becac1b276f884Igor Murashkin           crop = QUEUE_BUFFER_INPUT_RECT;
1317ea777f097784492f880623067becac1b276f884Igor Murashkin           scalingMode = QUEUE_BUFFER_INPUT_SCALING_MODE;
1327ea777f097784492f880623067becac1b276f884Igor Murashkin           transform = QUEUE_BUFFER_INPUT_TRANSFORM;
1337ea777f097784492f880623067becac1b276f884Igor Murashkin           fence = QUEUE_BUFFER_INPUT_FENCE;
1347ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1357ea777f097784492f880623067becac1b276f884Igor Murashkin
1367ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput build() {
1377ea777f097784492f880623067becac1b276f884Igor Murashkin            return IGraphicBufferProducer::QueueBufferInput(
1387ea777f097784492f880623067becac1b276f884Igor Murashkin                    timestamp,
1397ea777f097784492f880623067becac1b276f884Igor Murashkin                    isAutoTimestamp,
14082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala                    dataSpace,
1417ea777f097784492f880623067becac1b276f884Igor Murashkin                    crop,
1427ea777f097784492f880623067becac1b276f884Igor Murashkin                    scalingMode,
1437ea777f097784492f880623067becac1b276f884Igor Murashkin                    transform,
1447ea777f097784492f880623067becac1b276f884Igor Murashkin                    fence);
1457ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1467ea777f097784492f880623067becac1b276f884Igor Murashkin
1477ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setTimestamp(int64_t timestamp) {
1487ea777f097784492f880623067becac1b276f884Igor Murashkin            this->timestamp = timestamp;
1497ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1507ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1517ea777f097784492f880623067becac1b276f884Igor Murashkin
1527ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setIsAutoTimestamp(bool isAutoTimestamp) {
1537ea777f097784492f880623067becac1b276f884Igor Murashkin            this->isAutoTimestamp = isAutoTimestamp;
1547ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1557ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1567ea777f097784492f880623067becac1b276f884Igor Murashkin
15782c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        QueueBufferInputBuilder& setDataSpace(android_dataspace dataSpace) {
15882c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala            this->dataSpace = dataSpace;
15982c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala            return *this;
16082c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        }
16182c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala
1627ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setCrop(Rect crop) {
1637ea777f097784492f880623067becac1b276f884Igor Murashkin            this->crop = crop;
1647ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1657ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1667ea777f097784492f880623067becac1b276f884Igor Murashkin
1677ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setScalingMode(int scalingMode) {
1687ea777f097784492f880623067becac1b276f884Igor Murashkin            this->scalingMode = scalingMode;
1697ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1707ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1717ea777f097784492f880623067becac1b276f884Igor Murashkin
1727ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setTransform(uint32_t transform) {
1737ea777f097784492f880623067becac1b276f884Igor Murashkin            this->transform = transform;
1747ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1757ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1767ea777f097784492f880623067becac1b276f884Igor Murashkin
1777ea777f097784492f880623067becac1b276f884Igor Murashkin        QueueBufferInputBuilder& setFence(sp<Fence> fence) {
1787ea777f097784492f880623067becac1b276f884Igor Murashkin            this->fence = fence;
1797ea777f097784492f880623067becac1b276f884Igor Murashkin            return *this;
1807ea777f097784492f880623067becac1b276f884Igor Murashkin        }
1817ea777f097784492f880623067becac1b276f884Igor Murashkin
1827ea777f097784492f880623067becac1b276f884Igor Murashkin    private:
1837ea777f097784492f880623067becac1b276f884Igor Murashkin        int64_t timestamp;
1847ea777f097784492f880623067becac1b276f884Igor Murashkin        bool isAutoTimestamp;
18582c6bcc9705eabcaf5b9e45bc81867b0e2d61a02Eino-Ville Talvala        android_dataspace dataSpace;
1867ea777f097784492f880623067becac1b276f884Igor Murashkin        Rect crop;
1877ea777f097784492f880623067becac1b276f884Igor Murashkin        int scalingMode;
1887ea777f097784492f880623067becac1b276f884Igor Murashkin        uint32_t transform;
1897ea777f097784492f880623067becac1b276f884Igor Murashkin        sp<Fence> fence;
1907ea777f097784492f880623067becac1b276f884Igor Murashkin    }; // struct QueueBufferInputBuilder
1917ea777f097784492f880623067becac1b276f884Igor Murashkin
1927ea777f097784492f880623067becac1b276f884Igor Murashkin    // To easily store dequeueBuffer results into containers
1937ea777f097784492f880623067becac1b276f884Igor Murashkin    struct DequeueBufferResult {
1947ea777f097784492f880623067becac1b276f884Igor Murashkin        int slot;
1957ea777f097784492f880623067becac1b276f884Igor Murashkin        sp<Fence> fence;
1967ea777f097784492f880623067becac1b276f884Igor Murashkin    };
1977ea777f097784492f880623067becac1b276f884Igor Murashkin
198567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    status_t dequeueBuffer(uint32_t w, uint32_t h, uint32_t format, uint32_t usage, DequeueBufferResult* result) {
199567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos        return mProducer->dequeueBuffer(&result->slot, &result->fence, w, h, format, usage);
2007ea777f097784492f880623067becac1b276f884Igor Murashkin    }
2017ea777f097784492f880623067becac1b276f884Igor Murashkin
202583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    void setupDequeueRequestBuffer(int *slot, sp<Fence> *fence,
203583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos            sp<GraphicBuffer> *buffer)
204583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    {
205583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_TRUE(slot != NULL);
206583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_TRUE(fence != NULL);
207583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_TRUE(buffer != NULL);
208583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
209583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_NO_FATAL_FAILURE(ConnectProducer());
210583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
211583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
212567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                (mProducer->dequeueBuffer(slot, fence, DEFAULT_WIDTH,
213567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                DEFAULT_HEIGHT, DEFAULT_FORMAT, TEST_PRODUCER_USAGE_BITS)));
214583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
215583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        EXPECT_LE(0, *slot);
216583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, *slot);
217583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
218583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        // Request the buffer (pre-requisite for queueing)
219583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_OK(mProducer->requestBuffer(*slot, buffer));
220583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    }
221583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
2227ea777f097784492f880623067becac1b276f884Igor Murashkinprivate: // hide from test body
2237ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<DummyConsumer> mDC;
2247ea777f097784492f880623067becac1b276f884Igor Murashkin
2257ea777f097784492f880623067becac1b276f884Igor Murashkinprotected: // accessible from test body
2267ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<IGraphicBufferProducer> mProducer;
2277ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<IGraphicBufferConsumer> mConsumer;
2287ea777f097784492f880623067becac1b276f884Igor Murashkin};
2297ea777f097784492f880623067becac1b276f884Igor Murashkin
2307ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, ConnectFirst_ReturnsError) {
2317ea777f097784492f880623067becac1b276f884Igor Murashkin    IGraphicBufferProducer::QueueBufferOutput output;
2327ea777f097784492f880623067becac1b276f884Igor Murashkin
2337ea777f097784492f880623067becac1b276f884Igor Murashkin    // NULL output returns BAD_VALUE
2347ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN,
2357ea777f097784492f880623067becac1b276f884Igor Murashkin                                            TEST_API,
2367ea777f097784492f880623067becac1b276f884Igor Murashkin                                            TEST_CONTROLLED_BY_APP,
2377ea777f097784492f880623067becac1b276f884Igor Murashkin                                            /*output*/NULL));
2387ea777f097784492f880623067becac1b276f884Igor Murashkin
2397ea777f097784492f880623067becac1b276f884Igor Murashkin    // Invalid API returns bad value
2407ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN,
2417ea777f097784492f880623067becac1b276f884Igor Murashkin                                            /*api*/0xDEADBEEF,
2427ea777f097784492f880623067becac1b276f884Igor Murashkin                                            TEST_CONTROLLED_BY_APP,
2437ea777f097784492f880623067becac1b276f884Igor Murashkin                                            &output));
2447ea777f097784492f880623067becac1b276f884Igor Murashkin
2457ea777f097784492f880623067becac1b276f884Igor Murashkin    // TODO: get a token from a dead process somehow
2467ea777f097784492f880623067becac1b276f884Igor Murashkin}
2477ea777f097784492f880623067becac1b276f884Igor Murashkin
2487ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, ConnectAgain_ReturnsError) {
2497ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
2507ea777f097784492f880623067becac1b276f884Igor Murashkin
2517ea777f097784492f880623067becac1b276f884Igor Murashkin    // Can't connect when there is already a producer connected
2527ea777f097784492f880623067becac1b276f884Igor Murashkin    IGraphicBufferProducer::QueueBufferOutput output;
2537ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->connect(TEST_TOKEN,
2547ea777f097784492f880623067becac1b276f884Igor Murashkin                                            TEST_API,
2557ea777f097784492f880623067becac1b276f884Igor Murashkin                                            TEST_CONTROLLED_BY_APP,
2567ea777f097784492f880623067becac1b276f884Igor Murashkin                                            &output));
2577ea777f097784492f880623067becac1b276f884Igor Murashkin
2587ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mConsumer->consumerDisconnect());
2597ea777f097784492f880623067becac1b276f884Igor Murashkin    // Can't connect when IGBP is abandoned
2607ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(NO_INIT, mProducer->connect(TEST_TOKEN,
2617ea777f097784492f880623067becac1b276f884Igor Murashkin                                          TEST_API,
2627ea777f097784492f880623067becac1b276f884Igor Murashkin                                          TEST_CONTROLLED_BY_APP,
2637ea777f097784492f880623067becac1b276f884Igor Murashkin                                          &output));
2647ea777f097784492f880623067becac1b276f884Igor Murashkin}
2657ea777f097784492f880623067becac1b276f884Igor Murashkin
2667ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Disconnect_Succeeds) {
2677ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
2687ea777f097784492f880623067becac1b276f884Igor Murashkin
2697ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mProducer->disconnect(TEST_API));
2707ea777f097784492f880623067becac1b276f884Igor Murashkin}
2717ea777f097784492f880623067becac1b276f884Igor Murashkin
2727ea777f097784492f880623067becac1b276f884Igor Murashkin
2737ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Disconnect_ReturnsError) {
2747ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
2757ea777f097784492f880623067becac1b276f884Igor Murashkin
2767ea777f097784492f880623067becac1b276f884Igor Murashkin    // Must disconnect with same API number
2777ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_EQ(BAD_VALUE, mProducer->disconnect(TEST_API_OTHER));
2787ea777f097784492f880623067becac1b276f884Igor Murashkin    // API must not be out of range
2797ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_EQ(BAD_VALUE, mProducer->disconnect(/*api*/0xDEADBEEF));
2807ea777f097784492f880623067becac1b276f884Igor Murashkin
2817ea777f097784492f880623067becac1b276f884Igor Murashkin    // TODO: somehow kill mProducer so that this returns DEAD_OBJECT
2827ea777f097784492f880623067becac1b276f884Igor Murashkin}
2837ea777f097784492f880623067becac1b276f884Igor Murashkin
2847ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Query_Succeeds) {
2857ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
2867ea777f097784492f880623067becac1b276f884Igor Murashkin
287f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    int32_t value = -1;
2887ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_WIDTH, &value));
289f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    EXPECT_EQ(DEFAULT_WIDTH, static_cast<uint32_t>(value));
2907ea777f097784492f880623067becac1b276f884Igor Murashkin
2917ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_HEIGHT, &value));
292f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza    EXPECT_EQ(DEFAULT_HEIGHT, static_cast<uint32_t>(value));
2937ea777f097784492f880623067becac1b276f884Igor Murashkin
2947ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_FORMAT, &value));
2957ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(DEFAULT_FORMAT, value);
2967ea777f097784492f880623067becac1b276f884Igor Murashkin
2977ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &value));
2987ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_LE(0, value);
2997ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_GE(BufferQueue::NUM_BUFFER_SLOTS, value);
3007ea777f097784492f880623067becac1b276f884Igor Murashkin
3017ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value));
3027ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_FALSE(value); // Can't run behind when we haven't touched the queue
3037ea777f097784492f880623067becac1b276f884Igor Murashkin
3047ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_OK(mProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &value));
3057ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(DEFAULT_CONSUMER_USAGE_BITS, value);
3067ea777f097784492f880623067becac1b276f884Igor Murashkin
3077ea777f097784492f880623067becac1b276f884Igor Murashkin}
3087ea777f097784492f880623067becac1b276f884Igor Murashkin
3097ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Query_ReturnsError) {
3107ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
3117ea777f097784492f880623067becac1b276f884Igor Murashkin
3127ea777f097784492f880623067becac1b276f884Igor Murashkin    // One past the end of the last 'query' enum value. Update this if we add more enums.
313d8ead0ccd4b700e43a78efe9b140d4a069dc343dMichael Lentine    const int NATIVE_WINDOW_QUERY_LAST_OFF_BY_ONE = NATIVE_WINDOW_BUFFER_AGE + 1;
3147ea777f097784492f880623067becac1b276f884Igor Murashkin
3157ea777f097784492f880623067becac1b276f884Igor Murashkin    int value;
3167ea777f097784492f880623067becac1b276f884Igor Murashkin    // What was out of range
3177ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(/*what*/-1, &value));
3187ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(/*what*/0xDEADBEEF, &value));
3197ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_QUERY_LAST_OFF_BY_ONE, &value));
3207ea777f097784492f880623067becac1b276f884Igor Murashkin
3217ea777f097784492f880623067becac1b276f884Igor Murashkin    // Some enums from window.h are 'invalid'
3227ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER, &value));
3237ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_CONCRETE_TYPE, &value));
3247ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_DEFAULT_WIDTH, &value));
3257ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_DEFAULT_HEIGHT, &value));
3267ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_TRANSFORM_HINT, &value));
3277ea777f097784492f880623067becac1b276f884Igor Murashkin    // TODO: Consider documented the above enums as unsupported or make a new enum for IGBP
3287ea777f097784492f880623067becac1b276f884Igor Murashkin
3297ea777f097784492f880623067becac1b276f884Igor Murashkin    // Value was NULL
3307ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->query(NATIVE_WINDOW_FORMAT, /*value*/NULL));
3317ea777f097784492f880623067becac1b276f884Igor Murashkin
3327ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mConsumer->consumerDisconnect());
3337ea777f097784492f880623067becac1b276f884Igor Murashkin
3347ea777f097784492f880623067becac1b276f884Igor Murashkin    // BQ was abandoned
3357ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(NO_INIT, mProducer->query(NATIVE_WINDOW_FORMAT, &value));
3367ea777f097784492f880623067becac1b276f884Igor Murashkin
3377ea777f097784492f880623067becac1b276f884Igor Murashkin    // TODO: other things in window.h that are supported by Surface::query
3387ea777f097784492f880623067becac1b276f884Igor Murashkin    // but not by BufferQueue::query
3397ea777f097784492f880623067becac1b276f884Igor Murashkin}
3407ea777f097784492f880623067becac1b276f884Igor Murashkin
3417ea777f097784492f880623067becac1b276f884Igor Murashkin// TODO: queue under more complicated situations not involving just a single buffer
3427ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Queue_Succeeds) {
3437ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
3447ea777f097784492f880623067becac1b276f884Igor Murashkin
3457ea777f097784492f880623067becac1b276f884Igor Murashkin    int dequeuedSlot = -1;
3467ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<Fence> dequeuedFence;
3477ea777f097784492f880623067becac1b276f884Igor Murashkin
348567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos
349583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
350583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos            (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
3517ea777f097784492f880623067becac1b276f884Igor Murashkin                                     DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
352583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                                     TEST_PRODUCER_USAGE_BITS)));
3537ea777f097784492f880623067becac1b276f884Igor Murashkin
3547ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_LE(0, dequeuedSlot);
3557ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, dequeuedSlot);
3567ea777f097784492f880623067becac1b276f884Igor Murashkin
3577ea777f097784492f880623067becac1b276f884Igor Murashkin    // Request the buffer (pre-requisite for queueing)
3587ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<GraphicBuffer> dequeuedBuffer;
3597ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mProducer->requestBuffer(dequeuedSlot, &dequeuedBuffer));
3607ea777f097784492f880623067becac1b276f884Igor Murashkin
3617ea777f097784492f880623067becac1b276f884Igor Murashkin    // A generic "valid" input
3627ea777f097784492f880623067becac1b276f884Igor Murashkin    IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
3637ea777f097784492f880623067becac1b276f884Igor Murashkin    IGraphicBufferProducer::QueueBufferOutput output;
3647ea777f097784492f880623067becac1b276f884Igor Murashkin
3657ea777f097784492f880623067becac1b276f884Igor Murashkin    // Queue the buffer back into the BQ
3667ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mProducer->queueBuffer(dequeuedSlot, input, &output));
3677ea777f097784492f880623067becac1b276f884Igor Murashkin
3687ea777f097784492f880623067becac1b276f884Igor Murashkin    {
3697ea777f097784492f880623067becac1b276f884Igor Murashkin        uint32_t width;
3707ea777f097784492f880623067becac1b276f884Igor Murashkin        uint32_t height;
3717ea777f097784492f880623067becac1b276f884Igor Murashkin        uint32_t transformHint;
3727ea777f097784492f880623067becac1b276f884Igor Murashkin        uint32_t numPendingBuffers;
3737ea777f097784492f880623067becac1b276f884Igor Murashkin
3747ea777f097784492f880623067becac1b276f884Igor Murashkin        output.deflate(&width, &height, &transformHint, &numPendingBuffers);
3757ea777f097784492f880623067becac1b276f884Igor Murashkin
3767ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(DEFAULT_WIDTH, width);
3777ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(DEFAULT_HEIGHT, height);
3787ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(DEFAULT_TRANSFORM_HINT, transformHint);
379f8cebe54576c8100769e2515d4f67bdd013a6927Dan Stoza        EXPECT_EQ(1u, numPendingBuffers); // since queueBuffer was called exactly once
3807ea777f097784492f880623067becac1b276f884Igor Murashkin    }
3817ea777f097784492f880623067becac1b276f884Igor Murashkin
3827ea777f097784492f880623067becac1b276f884Igor Murashkin    // Buffer was not in the dequeued state
3837ea777f097784492f880623067becac1b276f884Igor Murashkin    EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
3847ea777f097784492f880623067becac1b276f884Igor Murashkin}
3857ea777f097784492f880623067becac1b276f884Igor Murashkin
3867ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, Queue_ReturnsError) {
3877ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
3887ea777f097784492f880623067becac1b276f884Igor Murashkin
3897ea777f097784492f880623067becac1b276f884Igor Murashkin    // Invalid slot number
3907ea777f097784492f880623067becac1b276f884Igor Murashkin    {
3917ea777f097784492f880623067becac1b276f884Igor Murashkin        // A generic "valid" input
3927ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
3937ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
3947ea777f097784492f880623067becac1b276f884Igor Murashkin
3957ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/-1, input, &output));
3967ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/0xDEADBEEF, input, &output));
3977ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(BufferQueue::NUM_BUFFER_SLOTS,
3987ea777f097784492f880623067becac1b276f884Igor Murashkin                                                    input, &output));
3997ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4007ea777f097784492f880623067becac1b276f884Igor Murashkin
4017ea777f097784492f880623067becac1b276f884Igor Murashkin    // Slot was not in the dequeued state (all slots start out in Free state)
4027ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4037ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
4047ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4057ea777f097784492f880623067becac1b276f884Igor Murashkin
4067ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(/*slot*/0, input, &output));
4077ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4087ea777f097784492f880623067becac1b276f884Igor Murashkin
4097ea777f097784492f880623067becac1b276f884Igor Murashkin    // Put the slot into the "dequeued" state for the rest of the test
4107ea777f097784492f880623067becac1b276f884Igor Murashkin    int dequeuedSlot = -1;
4117ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<Fence> dequeuedFence;
4127ea777f097784492f880623067becac1b276f884Igor Murashkin
413583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
414583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos            (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
4157ea777f097784492f880623067becac1b276f884Igor Murashkin                                     DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
416583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                                     TEST_PRODUCER_USAGE_BITS)));
4177ea777f097784492f880623067becac1b276f884Igor Murashkin
4187ea777f097784492f880623067becac1b276f884Igor Murashkin    // Slot was enqueued without requesting a buffer
4197ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4207ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
4217ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4227ea777f097784492f880623067becac1b276f884Igor Murashkin
4237ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
4247ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4257ea777f097784492f880623067becac1b276f884Igor Murashkin
4267ea777f097784492f880623067becac1b276f884Igor Murashkin    // Request the buffer so that the rest of the tests don't fail on earlier checks.
4277ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<GraphicBuffer> dequeuedBuffer;
4287ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mProducer->requestBuffer(dequeuedSlot, &dequeuedBuffer));
4297ea777f097784492f880623067becac1b276f884Igor Murashkin
4307ea777f097784492f880623067becac1b276f884Igor Murashkin    // Fence was NULL
4317ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4327ea777f097784492f880623067becac1b276f884Igor Murashkin        sp<Fence> nullFence = NULL;
4337ea777f097784492f880623067becac1b276f884Igor Murashkin
4347ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input =
4357ea777f097784492f880623067becac1b276f884Igor Murashkin                QueueBufferInputBuilder().setFence(nullFence).build();
4367ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4377ea777f097784492f880623067becac1b276f884Igor Murashkin
4387ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
4397ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4407ea777f097784492f880623067becac1b276f884Igor Murashkin
4417ea777f097784492f880623067becac1b276f884Igor Murashkin    // Scaling mode was unknown
4427ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4437ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input =
4447ea777f097784492f880623067becac1b276f884Igor Murashkin                QueueBufferInputBuilder().setScalingMode(-1).build();
4457ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4467ea777f097784492f880623067becac1b276f884Igor Murashkin
4477ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
4487ea777f097784492f880623067becac1b276f884Igor Murashkin
4497ea777f097784492f880623067becac1b276f884Igor Murashkin        input = QueueBufferInputBuilder().setScalingMode(0xDEADBEEF).build();
4507ea777f097784492f880623067becac1b276f884Igor Murashkin
4517ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
4527ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4537ea777f097784492f880623067becac1b276f884Igor Murashkin
4547ea777f097784492f880623067becac1b276f884Igor Murashkin    // Crop rect is out of bounds of the buffer dimensions
4557ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4567ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input =
4577ea777f097784492f880623067becac1b276f884Igor Murashkin                QueueBufferInputBuilder().setCrop(Rect(DEFAULT_WIDTH + 1, DEFAULT_HEIGHT + 1))
4587ea777f097784492f880623067becac1b276f884Igor Murashkin                .build();
4597ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4607ea777f097784492f880623067becac1b276f884Igor Murashkin
4617ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(BAD_VALUE, mProducer->queueBuffer(dequeuedSlot, input, &output));
4627ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4637ea777f097784492f880623067becac1b276f884Igor Murashkin
4647ea777f097784492f880623067becac1b276f884Igor Murashkin    // Abandon the buffer queue so that the last test fails
4657ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_OK(mConsumer->consumerDisconnect());
4667ea777f097784492f880623067becac1b276f884Igor Murashkin
4677ea777f097784492f880623067becac1b276f884Igor Murashkin    // The buffer queue has been abandoned.
4687ea777f097784492f880623067becac1b276f884Igor Murashkin    {
4697ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
4707ea777f097784492f880623067becac1b276f884Igor Murashkin        IGraphicBufferProducer::QueueBufferOutput output;
4717ea777f097784492f880623067becac1b276f884Igor Murashkin
4727ea777f097784492f880623067becac1b276f884Igor Murashkin        EXPECT_EQ(NO_INIT, mProducer->queueBuffer(dequeuedSlot, input, &output));
4737ea777f097784492f880623067becac1b276f884Igor Murashkin    }
4747ea777f097784492f880623067becac1b276f884Igor Murashkin}
4757ea777f097784492f880623067becac1b276f884Igor Murashkin
4767ea777f097784492f880623067becac1b276f884Igor MurashkinTEST_F(IGraphicBufferProducerTest, CancelBuffer_DoesntCrash) {
4777ea777f097784492f880623067becac1b276f884Igor Murashkin    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
4787ea777f097784492f880623067becac1b276f884Igor Murashkin
4797ea777f097784492f880623067becac1b276f884Igor Murashkin    int dequeuedSlot = -1;
4807ea777f097784492f880623067becac1b276f884Igor Murashkin    sp<Fence> dequeuedFence;
4817ea777f097784492f880623067becac1b276f884Igor Murashkin
482583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
483583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos            (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
4847ea777f097784492f880623067becac1b276f884Igor Murashkin                                     DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
485583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                                     TEST_PRODUCER_USAGE_BITS)));
4867ea777f097784492f880623067becac1b276f884Igor Murashkin
4877ea777f097784492f880623067becac1b276f884Igor Murashkin    // No return code, but at least test that it doesn't blow up...
4887ea777f097784492f880623067becac1b276f884Igor Murashkin    // TODO: add a return code
4897ea777f097784492f880623067becac1b276f884Igor Murashkin    mProducer->cancelBuffer(dequeuedSlot, dequeuedFence);
4907ea777f097784492f880623067becac1b276f884Igor Murashkin}
4917ea777f097784492f880623067becac1b276f884Igor Murashkin
492fa455354557f6283ff3a7d76979e52fd251c155fPablo CeballosTEST_F(IGraphicBufferProducerTest, SetMaxDequeuedBufferCount_Succeeds) {
493583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
494fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    int minUndequeuedBuffers;
495fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
496fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            &minUndequeuedBuffers));
497fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
498fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    const int minBuffers = 1;
499fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    const int maxBuffers = BufferQueue::NUM_BUFFER_SLOTS - minUndequeuedBuffers;
500fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
501fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setAsyncMode(false)) << "async mode: " << false;
502fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setMaxDequeuedBufferCount(minBuffers))
503fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << "bufferCount: " << minBuffers;
504fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
505fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Should now be able to dequeue up to minBuffers times
50672daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    DequeueBufferResult result;
507fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    for (int i = 0; i < minBuffers; ++i) {
508fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
509583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        EXPECT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
510567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                (dequeueBuffer(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
511583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                              TEST_PRODUCER_USAGE_BITS, &result)))
512fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                << "iteration: " << i << ", slot: " << result.slot;
513fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    }
514fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
515fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setMaxDequeuedBufferCount(maxBuffers));
516fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
51772daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // queue the first buffer to enable max dequeued buffer count checking
51872daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
51972daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    IGraphicBufferProducer::QueueBufferOutput output;
52072daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    sp<GraphicBuffer> buffer;
52172daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    ASSERT_OK(mProducer->requestBuffer(result.slot, &buffer));
52272daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    ASSERT_OK(mProducer->queueBuffer(result.slot, input, &output));
52372daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos
52472daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos
525fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Should now be able to dequeue up to maxBuffers times
52672daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    int dequeuedSlot = -1;
52772daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    sp<Fence> dequeuedFence;
528fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    for (int i = 0; i < maxBuffers; ++i) {
529fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
530583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        EXPECT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
531583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
532fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                                         DEFAULT_WIDTH, DEFAULT_HEIGHT,
533fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                                         DEFAULT_FORMAT,
534583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                                         TEST_PRODUCER_USAGE_BITS)))
535fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                << "iteration: " << i << ", slot: " << dequeuedSlot;
536fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    }
53772daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos
53872daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // Cancel a buffer, so we can decrease the buffer count
53972daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    ASSERT_OK(mProducer->cancelBuffer(dequeuedSlot, dequeuedFence));
54072daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos
54172daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // Should now be able to decrease the max dequeued count by 1
54272daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    ASSERT_OK(mProducer->setMaxDequeuedBufferCount(maxBuffers-1));
543fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos}
544fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
545fa455354557f6283ff3a7d76979e52fd251c155fPablo CeballosTEST_F(IGraphicBufferProducerTest, SetMaxDequeuedBufferCount_Fails) {
546583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
547fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    int minUndequeuedBuffers;
548fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
549fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                               &minUndequeuedBuffers));
550fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
551fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    const int minBuffers = 1;
552fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    const int maxBuffers = BufferQueue::NUM_BUFFER_SLOTS - minUndequeuedBuffers;
553fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
554fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setAsyncMode(false)) << "async mode: " << false;
555fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Buffer count was out of range
556fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    EXPECT_EQ(BAD_VALUE, mProducer->setMaxDequeuedBufferCount(0))
557fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << "bufferCount: " << 0;
558fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    EXPECT_EQ(BAD_VALUE, mProducer->setMaxDequeuedBufferCount(maxBuffers + 1))
559fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << "bufferCount: " << maxBuffers + 1;
560fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
56172daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // Set max dequeue count to 2
56272daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    ASSERT_OK(mProducer->setMaxDequeuedBufferCount(2));
56372daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // Dequeue 2 buffers
56472daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    int dequeuedSlot = -1;
56572daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    sp<Fence> dequeuedFence;
56672daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    for (int i = 0; i < 2; i++) {
567583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
568583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
569fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                                         DEFAULT_WIDTH, DEFAULT_HEIGHT,
570fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                                         DEFAULT_FORMAT,
571583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                                         TEST_PRODUCER_USAGE_BITS)))
572fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos                << "slot: " << dequeuedSlot;
573fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    }
574fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
57572daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    // Client has too many buffers dequeued
57672daab652e3481566c01ce45c6afdb9fcec6f140Pablo Ceballos    EXPECT_EQ(BAD_VALUE, mProducer->setMaxDequeuedBufferCount(1))
577fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << "bufferCount: " << minBuffers;
578fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
579fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Abandon buffer queue
580fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mConsumer->consumerDisconnect());
581fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
582fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Fail because the buffer queue was abandoned
583fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    EXPECT_EQ(NO_INIT, mProducer->setMaxDequeuedBufferCount(minBuffers))
584fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << "bufferCount: " << minBuffers;
585fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
586fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos}
587fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
588fa455354557f6283ff3a7d76979e52fd251c155fPablo CeballosTEST_F(IGraphicBufferProducerTest, SetAsyncMode_Succeeds) {
589fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mConsumer->setMaxAcquiredBufferCount(1)) << "maxAcquire: " << 1;
590583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
591fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setAsyncMode(true)) << "async mode: " << true;
592fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mProducer->setMaxDequeuedBufferCount(1)) << "maxDequeue: " << 1;
593fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
594fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    int dequeuedSlot = -1;
595fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    sp<Fence> dequeuedFence;
596567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
597fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    IGraphicBufferProducer::QueueBufferOutput output;
598fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    sp<GraphicBuffer> dequeuedBuffer;
599fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
600fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Should now be able to queue/dequeue as many buffers as we want without
601fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // blocking
602fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    for (int i = 0; i < 5; ++i) {
603583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
604583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
605567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
606567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                TEST_PRODUCER_USAGE_BITS))) << "slot : " << dequeuedSlot;
607fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos        ASSERT_OK(mProducer->requestBuffer(dequeuedSlot, &dequeuedBuffer));
608fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos        ASSERT_OK(mProducer->queueBuffer(dequeuedSlot, input, &output));
609fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    }
610fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos}
611fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
612fa455354557f6283ff3a7d76979e52fd251c155fPablo CeballosTEST_F(IGraphicBufferProducerTest, SetAsyncMode_Fails) {
613583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
614fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Prerequisite to fail out a valid setBufferCount call
615fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    {
616fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos        int dequeuedSlot = -1;
617fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos        sp<Fence> dequeuedFence;
618fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
619583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
620583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos                (mProducer->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
621567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FORMAT,
622567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos                TEST_PRODUCER_USAGE_BITS))) << "slot: " << dequeuedSlot;
623fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    }
624fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
625fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Abandon buffer queue
626fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    ASSERT_OK(mConsumer->consumerDisconnect());
627fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
628fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // Fail because the buffer queue was abandoned
629fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    EXPECT_EQ(NO_INIT, mProducer->setAsyncMode(false)) << "asyncMode: "
630fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos            << false;
631fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos}
632fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
633583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
634583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_dequeueBuffer) {
635583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
636583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
637583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
638567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->dequeueBuffer(&slot, &fence, DEFAULT_WIDTH,
639567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos            DEFAULT_HEIGHT, DEFAULT_FORMAT, TEST_PRODUCER_USAGE_BITS));
640583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
641583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
642583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
643583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_detachNextBuffer) {
644583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
645583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
646583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
647583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->detachNextBuffer(&buffer, &fence));
648583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
649583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
650583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
651583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_requestBuffer) {
652583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_NO_FATAL_FAILURE(ConnectProducer());
653583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
654583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
655583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
656583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
657583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(OK, ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
658567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos            (mProducer->dequeueBuffer(&slot, &fence, DEFAULT_WIDTH,
659567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos            DEFAULT_HEIGHT, DEFAULT_FORMAT, TEST_PRODUCER_USAGE_BITS)));
660583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
661583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    EXPECT_LE(0, slot);
662583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, slot);
663583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
664583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->disconnect(TEST_API));
665583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
666583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
667583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
668583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->requestBuffer(slot, &buffer));
669583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
670583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
671583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
672583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
673583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_detachBuffer) {
674583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
675583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
676583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
677583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
678583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    setupDequeueRequestBuffer(&slot, &fence, &buffer);
679583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
680583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->disconnect(TEST_API));
681583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
682583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->detachBuffer(slot));
683583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
684583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
685583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
686583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_queueBuffer) {
687583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
688583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
689583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
690583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
691583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    setupDequeueRequestBuffer(&slot, &fence, &buffer);
692583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
693583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->disconnect(TEST_API));
694583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
695583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    // A generic "valid" input
696583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    IGraphicBufferProducer::QueueBufferInput input = CreateBufferInput();
697583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    IGraphicBufferProducer::QueueBufferOutput output;
698583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
699583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->queueBuffer(slot, input, &output));
700583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
701583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
702583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
703583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_cancelBuffer) {
704583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
705583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
706583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
707583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
708583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    setupDequeueRequestBuffer(&slot, &fence, &buffer);
709583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
710583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->disconnect(TEST_API));
711583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
712583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->cancelBuffer(slot, fence));
713583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
714583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
715583b1b32191992d6ada58b3c61c71932a71c0c4bPablo CeballosTEST_F(IGraphicBufferProducerTest,
716583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos        DisconnectedProducerReturnsError_attachBuffer) {
717583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    int slot = -1;
718583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<Fence> fence;
719583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    sp<GraphicBuffer> buffer;
720583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
721583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    setupDequeueRequestBuffer(&slot, &fence, &buffer);
722583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
723583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->detachBuffer(slot));
724583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
725583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_OK(mProducer->disconnect(TEST_API));
726583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
727583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    ASSERT_EQ(NO_INIT, mProducer->attachBuffer(&slot, buffer));
728583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos}
729583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos
7307ea777f097784492f880623067becac1b276f884Igor Murashkin} // namespace android
731