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_BUFFERITEMCONSUMER_H 18 #define ANDROID_GUI_BUFFERITEMCONSUMER_H 19 20 #include <com_android_graphics_libgui_flags.h> 21 #include <gui/BufferQueue.h> 22 #include <gui/ConsumerBase.h> 23 24 #define ANDROID_GRAPHICS_BUFFERITEMCONSUMER_JNI_ID "mBufferItemConsumer" 25 26 namespace android { 27 28 class GraphicBuffer; 29 class String8; 30 31 /** 32 * BufferItemConsumer is a BufferQueue consumer endpoint that allows clients 33 * access to the whole BufferItem entry from BufferQueue. Multiple buffers may 34 * be acquired at once, to be used concurrently by the client. This consumer can 35 * operate either in synchronous or asynchronous mode. 36 */ 37 class BufferItemConsumer: public ConsumerBase 38 { 39 public: 40 typedef ConsumerBase::FrameAvailableListener FrameAvailableListener; 41 42 struct BufferFreedListener : public virtual RefBase { 43 virtual void onBufferFreed(const wp<GraphicBuffer>& graphicBuffer) = 0; 44 }; 45 46 enum { DEFAULT_MAX_BUFFERS = -1 }; 47 enum { INVALID_BUFFER_SLOT = BufferQueue::INVALID_BUFFER_SLOT }; 48 enum { NO_BUFFER_AVAILABLE = BufferQueue::NO_BUFFER_AVAILABLE }; 49 50 static std::tuple<sp<BufferItemConsumer>, sp<Surface>> create( 51 uint64_t consumerUsage, int bufferCount = DEFAULT_MAX_BUFFERS, 52 bool controlledByApp = false, bool isConsumerSurfaceFlinger = false); 53 54 static sp<BufferItemConsumer> create(const sp<IGraphicBufferConsumer>& consumer, 55 uint64_t consumerUsage, 56 int bufferCount = DEFAULT_MAX_BUFFERS, 57 bool controlledByApp = false) 58 __attribute((deprecated("Prefer ctors that create their own surface and consumer."))); 59 60 // Create a new buffer item consumer. The consumerUsage parameter determines 61 // the consumer usage flags passed to the graphics allocator. The 62 // bufferCount parameter specifies how many buffers can be locked for user 63 // access at the same time. 64 // controlledByApp tells whether this consumer is controlled by the 65 // application. 66 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) 67 BufferItemConsumer(uint64_t consumerUsage, int bufferCount = DEFAULT_MAX_BUFFERS, 68 bool controlledByApp = false, bool isConsumerSurfaceFlinger = false); 69 BufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, 70 int bufferCount = DEFAULT_MAX_BUFFERS, bool controlledByApp = false) 71 __attribute((deprecated("Prefer ctors that create their own surface and consumer."))); 72 #else 73 BufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, 74 uint64_t consumerUsage, int bufferCount = DEFAULT_MAX_BUFFERS, 75 bool controlledByApp = false); 76 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) 77 78 ~BufferItemConsumer() override; 79 80 // setBufferFreedListener sets the listener object that will be notified 81 // when an old buffer is being freed. 82 void setBufferFreedListener(const wp<BufferFreedListener>& listener); 83 84 // Gets the next graphics buffer from the producer, filling out the 85 // passed-in BufferItem structure. Returns NO_BUFFER_AVAILABLE if the queue 86 // of buffers is empty, and INVALID_OPERATION if the maximum number of 87 // buffers is already acquired. 88 // 89 // Only a fixed number of buffers can be acquired at a time, determined by 90 // the construction-time bufferCount parameter. If INVALID_OPERATION is 91 // returned by acquireBuffer, then old buffers must be returned to the 92 // queue by calling releaseBuffer before more buffers can be acquired. 93 // 94 // If waitForFence is true, and the acquired BufferItem has a valid fence object, 95 // acquireBuffer will wait on the fence with no timeout before returning. 96 status_t acquireBuffer(BufferItem* item, nsecs_t presentWhen, 97 bool waitForFence = true); 98 99 // Transfer ownership of a buffer to the BufferQueue. On NO_ERROR, the buffer 100 // is considered as if it were acquired. Buffer must not be null. 101 // 102 // Returns 103 // - BAD_VALUE if buffer is null 104 // - INVALID_OPERATION if too many buffers have already been acquired 105 status_t attachBuffer(const sp<GraphicBuffer>& buffer); 106 107 // Returns an acquired buffer to the queue, allowing it to be reused. Since 108 // only a fixed number of buffers may be acquired at a time, old buffers 109 // must be released by calling releaseBuffer to ensure new buffers can be 110 // acquired by acquireBuffer. Once a BufferItem is released, the caller must 111 // not access any members of the BufferItem, and should immediately remove 112 // all of its references to the BufferItem itself. 113 status_t releaseBuffer(const BufferItem &item, 114 const sp<Fence>& releaseFence = Fence::NO_FENCE); 115 116 status_t releaseBuffer(const sp<GraphicBuffer>& buffer, 117 const sp<Fence>& releaseFence = Fence::NO_FENCE); 118 119 protected: 120 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) 121 // This should only be used by BLASTBufferQueue: 122 BufferItemConsumer(const sp<IGraphicBufferProducer>& producer, 123 const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, 124 int bufferCount = DEFAULT_MAX_BUFFERS, bool controlledByApp = false); 125 #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) 126 127 private: 128 void initialize(uint64_t consumerUsage, int bufferCount); 129 130 status_t releaseBufferSlotLocked(int slotIndex, const sp<GraphicBuffer>& buffer, 131 const sp<Fence>& releaseFence); 132 133 void freeBufferLocked(int slotIndex) override; 134 135 // mBufferFreedListener is the listener object that will be called when 136 // an old buffer is being freed. If it is not NULL it will be called from 137 // freeBufferLocked. 138 wp<BufferFreedListener> mBufferFreedListener; 139 }; 140 141 } // namespace android 142 143 #endif // ANDROID_GUI_CPUCONSUMER_H 144