blob: bd62d85afba759b49a0ecc246bc18c71eb620395 [file] [log] [blame]
Daniel Lam6b091c52012-01-22 15:26:27 -08001/*
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
Dan Stoza54716312015-03-13 14:40:34 -070020#include <gui/BufferItem.h>
Dan Stozae0d58622014-04-22 14:12:55 -070021#include <gui/BufferQueueDefs.h>
22#include <gui/IGraphicBufferConsumer.h>
23#include <gui/IGraphicBufferProducer.h>
Dan Stoza3e96f192014-03-03 10:16:19 -080024#include <gui/IConsumerListener.h>
25
Daniel Lam6b091c52012-01-22 15:26:27 -080026namespace android {
Daniel Lam6b091c52012-01-22 15:26:27 -080027
Dan Stozae0d58622014-04-22 14:12:55 -070028class BufferQueue {
Daniel Lam6b091c52012-01-22 15:26:27 -080029public:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080030 // 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.
Dan Stozafebd4f42014-04-09 16:14:51 -070032 enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
Igor Murashkin7d2d1602013-11-12 18:02:20 -080033 // Used as a placeholder slot# when the value isn't pointing to an existing buffer.
Dan Stoza54716312015-03-13 14:40:34 -070034 enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
Igor Murashkin7d2d1602013-11-12 18:02:20 -080035 // 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 };
Daniel Lam6b091c52012-01-22 15:26:27 -080040
Jamie Gennisc68f2ec2012-08-30 18:36:22 -070041 // 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
Mathias Agopiana4e19522013-07-31 20:09:53 -070045 // for backward source compatibility
46 typedef ::android::ConsumerListener ConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -080047
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070048 // 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.
Mathias Agopiana4e19522013-07-31 20:09:53 -070057 class ProxyConsumerListener : public BnConsumerListener {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070058 public:
Chih-Hung Hsieh65d47872016-09-01 11:39:25 -070059 explicit ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070060 virtual ~ProxyConsumerListener();
Brian Anderson5ea5e592016-12-01 16:54:33 -080061 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(
Brian Andersond6927fb2016-07-23 23:37:30 -070067 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -070068 FrameEventHistoryDelta* outDelta) override;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070069 private:
Mathias Agopiana4e19522013-07-31 20:09:53 -070070 // mConsumerListener is a weak reference to the IConsumerListener. This is
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070071 // the raison d'etre of ProxyConsumerListener.
Mathias Agopiana4e19522013-07-31 20:09:53 -070072 wp<ConsumerListener> mConsumerListener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070073 };
74
Jamie Gennis72f096f2012-08-27 18:48:37 -070075 // BufferQueue manages a pool of gralloc memory slots to be used by
Mathias Agopian595264f2013-07-16 22:56:09 -070076 // producers and consumers. allocator is used to allocate all the
77 // needed gralloc buffers.
Dan Stozaf522af72014-03-12 10:17:20 -070078 static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
Dan Stoza70982a52016-01-11 23:40:44 +000079 sp<IGraphicBufferConsumer>* outConsumer,
Irvel468051e2016-06-13 16:44:44 -070080 bool consumerIsSurfaceFlinger = false);
Dan Stozaf522af72014-03-12 10:17:20 -070081
Mathias Agopian78491c92017-03-15 14:30:52 -070082 BufferQueue() = delete; // Create through createBufferQueue
Daniel Lam6b091c52012-01-22 15:26:27 -080083};
84
85// ----------------------------------------------------------------------------
86}; // namespace android
87
88#endif // ANDROID_GUI_BUFFERQUEUE_H