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
26namespace android {
27
28class BufferQueue {
29public:
30    // BufferQueue will keep track of at most this value of buffers.
31    // Attempts at runtime to increase the number of buffers past this will fail.
32    enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
33    // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
34    enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
35    // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code!
36    enum {
37        NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE,
38        PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER,
39    };
40
41    // When in async mode we reserve two slots in order to guarantee that the
42    // producer and consumer can run asynchronously.
43    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
44
45    // for backward source compatibility
46    typedef ::android::ConsumerListener ConsumerListener;
47
48    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
49    // reference to the actual consumer object.  It forwards all calls to that
50    // consumer object so long as it exists.
51    //
52    // This class exists to avoid having a circular reference between the
53    // BufferQueue object and the consumer object.  The reason this can't be a weak
54    // reference in the BufferQueue class is because we're planning to expose the
55    // consumer side of a BufferQueue as a binder interface, which doesn't support
56    // weak references.
57    class ProxyConsumerListener : public BnConsumerListener {
58    public:
59        explicit ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
60        virtual ~ProxyConsumerListener();
61        void onDisconnect() override;
62        void onFrameAvailable(const BufferItem& item) override;
63        void onFrameReplaced(const BufferItem& item) override;
64        void onBuffersReleased() override;
65        void onSidebandStreamChanged() override;
66        void addAndGetFrameTimestamps(
67                const NewFrameEventsEntry* newTimestamps,
68                FrameEventHistoryDelta* outDelta) 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            bool consumerIsSurfaceFlinger = false);
81
82    BufferQueue() = delete; // Create through createBufferQueue
83};
84
85// ----------------------------------------------------------------------------
86}; // namespace android
87
88#endif // ANDROID_GUI_BUFFERQUEUE_H
89