| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 5471631 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 20 | #include <gui/BufferItem.h> | 
| Dan Stoza | e0d5862 | 2014-04-22 14:12:55 -0700 | [diff] [blame] | 21 | #include <gui/BufferQueueDefs.h> | 
 | 22 | #include <gui/IGraphicBufferConsumer.h> | 
 | 23 | #include <gui/IGraphicBufferProducer.h> | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 24 | #include <gui/IConsumerListener.h> | 
 | 25 |  | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 26 | namespace android { | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 27 |  | 
| Dan Stoza | e0d5862 | 2014-04-22 14:12:55 -0700 | [diff] [blame] | 28 | class BufferQueue { | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 29 | public: | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 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. | 
| Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 32 |     enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS }; | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 33 |     // Used as a placeholder slot# when the value isn't pointing to an existing buffer. | 
| Dan Stoza | 5471631 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 34 |     enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT }; | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 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 |     }; | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 40 |  | 
| Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 45 |     // for backward source compatibility | 
 | 46 |     typedef ::android::ConsumerListener ConsumerListener; | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 47 |  | 
| Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 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. | 
| Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 57 |     class ProxyConsumerListener : public BnConsumerListener { | 
| Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 58 |     public: | 
| Chih-Hung Hsieh | 65d4787 | 2016-09-01 11:39:25 -0700 | [diff] [blame] | 59 |         explicit ProxyConsumerListener(const wp<ConsumerListener>& consumerListener); | 
| Yi Kong | 64865fd | 2017-05-04 16:40:43 -0700 | [diff] [blame] | 60 |         ~ProxyConsumerListener() override; | 
| Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 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( | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 67 |                 const NewFrameEventsEntry* newTimestamps, | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 68 |                 FrameEventHistoryDelta* outDelta) override; | 
| Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 69 |     private: | 
| Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 70 |         // mConsumerListener is a weak reference to the IConsumerListener.  This is | 
| Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 71 |         // the raison d'etre of ProxyConsumerListener. | 
| Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 72 |         wp<ConsumerListener> mConsumerListener; | 
| Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 73 |     }; | 
 | 74 |  | 
| Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 75 |     // BufferQueue manages a pool of gralloc memory slots to be used by | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 76 |     // producers and consumers. allocator is used to allocate all the | 
 | 77 |     // needed gralloc buffers. | 
| Dan Stoza | f522af7 | 2014-03-12 10:17:20 -0700 | [diff] [blame] | 78 |     static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer, | 
| Dan Stoza | 70982a5 | 2016-01-11 23:40:44 +0000 | [diff] [blame] | 79 |             sp<IGraphicBufferConsumer>* outConsumer, | 
| Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 80 |             bool consumerIsSurfaceFlinger = false); | 
| Dan Stoza | f522af7 | 2014-03-12 10:17:20 -0700 | [diff] [blame] | 81 |  | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 82 |     BufferQueue() = delete; // Create through createBufferQueue | 
| Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 83 | }; | 
 | 84 |  | 
 | 85 | // ---------------------------------------------------------------------------- | 
 | 86 | }; // namespace android | 
 | 87 |  | 
 | 88 | #endif // ANDROID_GUI_BUFFERQUEUE_H |