16b091c53000c843211c218ce40287a7edca9bc63Daniel Lam/*
26b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Copyright (C) 2012 The Android Open Source Project
36b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
46b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Licensed under the Apache License, Version 2.0 (the "License");
56b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * you may not use this file except in compliance with the License.
66b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * You may obtain a copy of the License at
76b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
86b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *      http://www.apache.org/licenses/LICENSE-2.0
96b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Unless required by applicable law or agreed to in writing, software
116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * distributed under the License is distributed on an "AS IS" BASIS,
126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * See the License for the specific language governing permissions and
146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * limitations under the License.
156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam */
166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#ifndef ANDROID_GUI_BUFFERQUEUE_H
186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define ANDROID_GUI_BUFFERQUEUE_H
196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
2054716317b5752010134edc64421a3f492bd608abDan Stoza#include <gui/BufferItem.h>
21e0d5862ad85f7bf4feb1eee9444bf3a5b49c5842Dan Stoza#include <gui/BufferQueueDefs.h>
22e0d5862ad85f7bf4feb1eee9444bf3a5b49c5842Dan Stoza#include <gui/IGraphicBufferConsumer.h>
23e0d5862ad85f7bf4feb1eee9444bf3a5b49c5842Dan Stoza#include <gui/IGraphicBufferProducer.h>
24a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian#include <gui/IConsumerListener.h>
256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
263e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza// These are only required to keep other parts of the framework with incomplete
273e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza// dependencies building successfully
283e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza#include <gui/IGraphicBufferAlloc.h>
296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
306b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
32e0d5862ad85f7bf4feb1eee9444bf3a5b49c5842Dan Stozaclass BufferQueue {
336b091c53000c843211c218ce40287a7edca9bc63Daniel Lampublic:
347d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    // BufferQueue will keep track of at most this value of buffers.
357d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    // Attempts at runtime to increase the number of buffers past this will fail.
36febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza    enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
377d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
3854716317b5752010134edc64421a3f492bd608abDan Stoza    enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
397d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code!
407d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    enum {
417d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin        NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE,
427d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin        PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER,
437d2d160cdc3cba9f4454f38433c94b68376cb843Igor Murashkin    };
446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
45c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // When in async mode we reserve two slots in order to guarantee that the
46c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // producer and consumer can run asynchronously.
47c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
48c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis
49a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    // for backward source compatibility
50a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    typedef ::android::ConsumerListener ConsumerListener;
51fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
52fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
53fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference to the actual consumer object.  It forwards all calls to that
54fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer object so long as it exists.
55fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
56fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // This class exists to avoid having a circular reference between the
57fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue object and the consumer object.  The reason this can't be a weak
58fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference in the BufferQueue class is because we're planning to expose the
59fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer side of a BufferQueue as a binder interface, which doesn't support
60fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // weak references.
61a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    class ProxyConsumerListener : public BnConsumerListener {
62fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    public:
63a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
64fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual ~ProxyConsumerListener();
65dc13c5b85b099050c73297a19f1ef89308f7620bDan Stoza        virtual void onFrameAvailable(const BufferItem& item) override;
66dc13c5b85b099050c73297a19f1ef89308f7620bDan Stoza        virtual void onFrameReplaced(const BufferItem& item) override;
67dc13c5b85b099050c73297a19f1ef89308f7620bDan Stoza        virtual void onBuffersReleased() override;
68dc13c5b85b099050c73297a19f1ef89308f7620bDan Stoza        virtual void onSidebandStreamChanged() override;
69ce796e78a57018f186b062199c75d94545318acaPablo Ceballos        virtual bool getFrameTimestamps(uint64_t frameNumber,
70ce796e78a57018f186b062199c75d94545318acaPablo Ceballos                FrameTimestamps* outTimestamps) const override;
71fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    private:
72a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        // mConsumerListener is a weak reference to the IConsumerListener.  This is
73fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // the raison d'etre of ProxyConsumerListener.
74a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        wp<ConsumerListener> mConsumerListener;
756b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    };
766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
7772f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // BufferQueue manages a pool of gralloc memory slots to be used by
78595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // producers and consumers. allocator is used to allocate all the
79595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // needed gralloc buffers.
80f522af7eb6048c2efae77d7b94960bc49c003e0eDan Stoza    static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
8170982a5f95f68295244e5f6cc037c193713a5259Dan Stoza            sp<IGraphicBufferConsumer>* outConsumer,
8270982a5f95f68295244e5f6cc037c193713a5259Dan Stoza            const sp<IGraphicBufferAlloc>& allocator = NULL);
83f522af7eb6048c2efae77d7b94960bc49c003e0eDan Stoza
84eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamprivate:
85e0d5862ad85f7bf4feb1eee9444bf3a5b49c5842Dan Stoza    BufferQueue(); // Create through createBufferQueue
866b091c53000c843211c218ce40287a7edca9bc63Daniel Lam};
876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// ----------------------------------------------------------------------------
896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
916b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#endif // ANDROID_GUI_BUFFERQUEUE_H
92