Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [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_BUFFERITEMCONSUMER_H |
| 18 | #define ANDROID_GUI_BUFFERITEMCONSUMER_H |
| 19 | |
| 20 | #include <gui/ConsumerBase.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 21 | #include <gui/BufferQueue.h> |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 22 | |
| 23 | #define ANDROID_GRAPHICS_BUFFERITEMCONSUMER_JNI_ID "mBufferItemConsumer" |
| 24 | |
| 25 | namespace android { |
| 26 | |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 27 | class String8; |
Mathias Agopian | 8f938a5 | 2013-07-12 22:06:26 -0700 | [diff] [blame] | 28 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 29 | /** |
| 30 | * BufferItemConsumer is a BufferQueue consumer endpoint that allows clients |
| 31 | * access to the whole BufferItem entry from BufferQueue. Multiple buffers may |
| 32 | * be acquired at once, to be used concurrently by the client. This consumer can |
| 33 | * operate either in synchronous or asynchronous mode. |
| 34 | */ |
| 35 | class BufferItemConsumer: public ConsumerBase |
| 36 | { |
| 37 | public: |
| 38 | typedef ConsumerBase::FrameAvailableListener FrameAvailableListener; |
| 39 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 40 | struct BufferFreedListener : public virtual RefBase { |
| 41 | virtual void onBufferFreed(const wp<GraphicBuffer>& graphicBuffer) = 0; |
| 42 | }; |
| 43 | |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 44 | enum { DEFAULT_MAX_BUFFERS = -1 }; |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 45 | enum { INVALID_BUFFER_SLOT = BufferQueue::INVALID_BUFFER_SLOT }; |
| 46 | enum { NO_BUFFER_AVAILABLE = BufferQueue::NO_BUFFER_AVAILABLE }; |
| 47 | |
| 48 | // Create a new buffer item consumer. The consumerUsage parameter determines |
| 49 | // the consumer usage flags passed to the graphics allocator. The |
| 50 | // bufferCount parameter specifies how many buffers can be locked for user |
| 51 | // access at the same time. |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 52 | // controlledByApp tells whether this consumer is controlled by the |
| 53 | // application. |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 54 | BufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, |
| 55 | uint32_t consumerUsage, int bufferCount = DEFAULT_MAX_BUFFERS, |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 56 | bool controlledByApp = false); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 57 | |
| 58 | virtual ~BufferItemConsumer(); |
| 59 | |
| 60 | // set the name of the BufferItemConsumer that will be used to identify it in |
| 61 | // log messages. |
| 62 | void setName(const String8& name); |
| 63 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 64 | // setBufferFreedListener sets the listener object that will be notified |
| 65 | // when an old buffer is being freed. |
| 66 | void setBufferFreedListener(const wp<BufferFreedListener>& listener); |
| 67 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 68 | // Gets the next graphics buffer from the producer, filling out the |
| 69 | // passed-in BufferItem structure. Returns NO_BUFFER_AVAILABLE if the queue |
| 70 | // of buffers is empty, and INVALID_OPERATION if the maximum number of |
| 71 | // buffers is already acquired. |
| 72 | // |
| 73 | // Only a fixed number of buffers can be acquired at a time, determined by |
| 74 | // the construction-time bufferCount parameter. If INVALID_OPERATION is |
| 75 | // returned by acquireBuffer, then old buffers must be returned to the |
| 76 | // queue by calling releaseBuffer before more buffers can be acquired. |
| 77 | // |
| 78 | // If waitForFence is true, and the acquired BufferItem has a valid fence object, |
| 79 | // acquireBuffer will wait on the fence with no timeout before returning. |
Dan Stoza | 5471631 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 80 | status_t acquireBuffer(BufferItem* item, nsecs_t presentWhen, |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 81 | bool waitForFence = true); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 82 | |
| 83 | // Returns an acquired buffer to the queue, allowing it to be reused. Since |
| 84 | // only a fixed number of buffers may be acquired at a time, old buffers |
| 85 | // must be released by calling releaseBuffer to ensure new buffers can be |
| 86 | // acquired by acquireBuffer. Once a BufferItem is released, the caller must |
| 87 | // not access any members of the BufferItem, and should immediately remove |
| 88 | // all of its references to the BufferItem itself. |
| 89 | status_t releaseBuffer(const BufferItem &item, |
| 90 | const sp<Fence>& releaseFence = Fence::NO_FENCE); |
| 91 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 92 | private: |
| 93 | void freeBufferLocked(int slotIndex) override; |
| 94 | |
| 95 | // mBufferFreedListener is the listener object that will be called when |
| 96 | // an old buffer is being freed. If it is not NULL it will be called from |
| 97 | // freeBufferLocked. |
| 98 | wp<BufferFreedListener> mBufferFreedListener; |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace android |
| 102 | |
| 103 | #endif // ANDROID_GUI_CPUCONSUMER_H |