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 23 #include <gui/IConsumerListener.h> 24 #include <gui/IGraphicBufferConsumer.h> 25 #include <gui/IGraphicBufferProducer.h> 26 27 #include <com_android_graphics_libgui_flags.h> 28 29 namespace android { 30 31 class BufferQueue { 32 public: 33 // BufferQueue will keep track of at most this value of buffers. 34 // Attempts at runtime to increase the number of buffers past this will fail. 35 enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS }; 36 // Used as a placeholder slot# when the value isn't pointing to an existing buffer. 37 enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT }; 38 // Alias to <IGraphicBufferConsumer.h> -- please scope from there in future code! 39 enum { 40 NO_BUFFER_AVAILABLE = IGraphicBufferConsumer::NO_BUFFER_AVAILABLE, 41 PRESENT_LATER = IGraphicBufferConsumer::PRESENT_LATER, 42 }; 43 44 // When in async mode we reserve two slots in order to guarantee that the 45 // producer and consumer can run asynchronously. 46 enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 }; 47 48 // for backward source compatibility 49 typedef ::android::ConsumerListener ConsumerListener; 50 51 // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak 52 // reference to the actual consumer object. It forwards all calls to that 53 // consumer object so long as it exists. 54 // 55 // This class exists to avoid having a circular reference between the 56 // BufferQueue object and the consumer object. The reason this can't be a weak 57 // reference in the BufferQueue class is because we're planning to expose the 58 // consumer side of a BufferQueue as a binder interface, which doesn't support 59 // weak references. 60 class ProxyConsumerListener : public BnConsumerListener { 61 public: 62 explicit ProxyConsumerListener(const wp<ConsumerListener>& consumerListener); 63 ~ProxyConsumerListener() override; 64 void onDisconnect() override; 65 void onFrameAvailable(const BufferItem& item) override; 66 void onFrameReplaced(const BufferItem& item) override; 67 void onBuffersReleased() override; 68 void onSidebandStreamChanged() override; 69 void onFrameDequeued(const uint64_t bufferId) override; 70 void onFrameCancelled(const uint64_t bufferId) override; 71 void onFrameDetached(const uint64_t bufferID) override; 72 void addAndGetFrameTimestamps( 73 const NewFrameEventsEntry* newTimestamps, 74 FrameEventHistoryDelta* outDelta) override; 75 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE) 76 void onSetFrameRate(float frameRate, int8_t compatibility, 77 int8_t changeFrameRateStrategy) override; 78 #endif 79 private: 80 // mConsumerListener is a weak reference to the IConsumerListener. This is 81 // the raison d'etre of ProxyConsumerListener. 82 wp<ConsumerListener> mConsumerListener; 83 }; 84 85 // BufferQueue manages a pool of gralloc memory slots to be used by 86 // producers and consumers. allocator is used to allocate all the 87 // needed gralloc buffers. 88 static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer, 89 sp<IGraphicBufferConsumer>* outConsumer, 90 bool consumerIsSurfaceFlinger = false); 91 92 BufferQueue() = delete; // Create through createBufferQueue 93 }; 94 95 // ---------------------------------------------------------------------------- 96 }; // namespace android 97 98 #endif // ANDROID_GUI_BUFFERQUEUE_H 99