1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_GUI_BUFFERQUEUE_H
18#define ANDROID_GUI_BUFFERQUEUE_H
19
20#include <gui/BufferItem.h>
21#include <gui/BufferQueueDefs.h>
22#include <gui/IGraphicBufferConsumer.h>
23#include <gui/IGraphicBufferProducer.h>
24#include <gui/IConsumerListener.h>
25
26// These are only required to keep other parts of the framework with incomplete
27// dependencies building successfully
28#include <gui/IGraphicBufferAlloc.h>
29
30namespace android {
31
32class BufferQueue {
33public:
34    // BufferQueue will keep track of at most this value of buffers.
35    // Attempts at runtime to increase the number of buffers past this will fail.
36    enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
37    // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
38    enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
39    // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code!
40    enum {
41        NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE,
42        PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER,
43    };
44
45    // When in async mode we reserve two slots in order to guarantee that the
46    // producer and consumer can run asynchronously.
47    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
48
49    // for backward source compatibility
50    typedef ::android::ConsumerListener ConsumerListener;
51
52    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
53    // reference to the actual consumer object.  It forwards all calls to that
54    // consumer object so long as it exists.
55    //
56    // This class exists to avoid having a circular reference between the
57    // BufferQueue object and the consumer object.  The reason this can't be a weak
58    // reference in the BufferQueue class is because we're planning to expose the
59    // consumer side of a BufferQueue as a binder interface, which doesn't support
60    // weak references.
61    class ProxyConsumerListener : public BnConsumerListener {
62    public:
63        ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
64        virtual ~ProxyConsumerListener();
65        virtual void onFrameAvailable(const BufferItem& item) override;
66        virtual void onFrameReplaced(const BufferItem& item) override;
67        virtual void onBuffersReleased() override;
68        virtual void onSidebandStreamChanged() override;
69    private:
70        // mConsumerListener is a weak reference to the IConsumerListener.  This is
71        // the raison d'etre of ProxyConsumerListener.
72        wp<ConsumerListener> mConsumerListener;
73    };
74
75    // BufferQueue manages a pool of gralloc memory slots to be used by
76    // producers and consumers. allocator is used to allocate all the
77    // needed gralloc buffers.
78    static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
79            sp<IGraphicBufferConsumer>* outConsumer,
80            const sp<IGraphicBufferAlloc>& allocator = NULL);
81
82private:
83    BufferQueue(); // Create through createBufferQueue
84};
85
86// ----------------------------------------------------------------------------
87}; // namespace android
88
89#endif // ANDROID_GUI_BUFFERQUEUE_H
90