blob: 7c2648250907c0bfc2ebbb459ff79a6272f0d7c7 [file] [log] [blame]
Jamie Gennis1a4d8832012-08-02 20:11:05 -07001/*
2 * Copyright (C) 2010 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_CONSUMERBASE_H
18#define ANDROID_GUI_CONSUMERBASE_H
19
Mathias Agopiana9347642017-02-13 16:42:28 -080020#include <gui/BufferQueueDefs.h>
21#include <gui/IConsumerListener.h>
22#include <gui/IGraphicBufferConsumer.h>
23#include <gui/OccupancyTracker.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070024
Mathias Agopiana9347642017-02-13 16:42:28 -080025#include <ui/PixelFormat.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070026
27#include <utils/String8.h>
28#include <utils/Vector.h>
29#include <utils/threads.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080030
Jamie Gennis1a4d8832012-08-02 20:11:05 -070031
32namespace android {
33// ----------------------------------------------------------------------------
34
35class String8;
Mathias Agopiana9347642017-02-13 16:42:28 -080036class GraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070037
38// ConsumerBase is a base class for BufferQueue consumer end-points. It
39// handles common tasks like management of the connection to the BufferQueue
40// and the buffer pool.
41class ConsumerBase : public virtual RefBase,
Mathias Agopiana4e19522013-07-31 20:09:53 -070042 protected ConsumerListener {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043public:
44 struct FrameAvailableListener : public virtual RefBase {
Dan Stozadc13c5b2015-05-11 15:33:01 -070045 // See IConsumerListener::onFrame{Available,Replaced}
Dan Stoza8dc55392014-11-04 11:37:46 -080046 virtual void onFrameAvailable(const BufferItem& item) = 0;
Dan Stozadc13c5b2015-05-11 15:33:01 -070047 virtual void onFrameReplaced(const BufferItem& /* item */) {}
Jamie Gennis1a4d8832012-08-02 20:11:05 -070048 };
49
Yi Kongca038512017-05-02 16:55:24 -070050 ~ConsumerBase() override;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070051
52 // abandon frees all the buffers and puts the ConsumerBase into the
53 // 'abandoned' state. Once put in this state the ConsumerBase can never
54 // leave it. When in the 'abandoned' state, all methods of the
Andy McFadden2adaf042012-12-18 09:49:45 -080055 // IGraphicBufferProducer interface will fail with the NO_INIT error.
Jamie Gennis1a4d8832012-08-02 20:11:05 -070056 //
57 // Note that while calling this method causes all the buffers to be freed
58 // from the perspective of the the ConsumerBase, if there are additional
59 // references on the buffers (e.g. if a buffer is referenced by a client
60 // or by OpenGL ES as a texture) then those buffer will remain allocated.
61 void abandon();
62
John Recke4783052015-05-14 15:55:11 -070063 // Returns true if the ConsumerBase is in the 'abandoned' state
64 bool isAbandoned();
65
Jamie Gennis1a4d8832012-08-02 20:11:05 -070066 // set the name of the ConsumerBase that will be used to identify it in
67 // log messages.
68 void setName(const String8& name);
69
Colin Cross3d1d2802016-09-26 18:10:16 -070070 // dumpState writes the current state to a string. Child classes should add
Jesse Hall7adb0f82013-03-06 16:13:49 -080071 // their state to the dump by overriding the dumpLocked method, which is
72 // called by these methods after locking the mutex.
Colin Cross3d1d2802016-09-26 18:10:16 -070073 void dumpState(String8& result) const;
74 void dumpState(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070075
76 // setFrameAvailableListener sets the listener object that will be notified
77 // when a new frame becomes available.
Igor Murashkina4a31492012-10-29 13:36:11 -070078 void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070079
Dan Stoza634f5ee2015-04-03 14:22:05 -070080 // See IGraphicBufferConsumer::detachBuffer
81 status_t detachBuffer(int slot);
82
Michael Lentine847f11e2015-05-18 13:41:23 -070083 // See IGraphicBufferConsumer::setDefaultBufferSize
84 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
85
86 // See IGraphicBufferConsumer::setDefaultBufferFormat
87 status_t setDefaultBufferFormat(PixelFormat defaultFormat);
88
89 // See IGraphicBufferConsumer::setDefaultBufferDataSpace
90 status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
91
Chia-I Wua81bc492017-11-27 10:16:00 -080092 // See IGraphicBufferConsumer::setConsumerUsageBits
93 status_t setConsumerUsageBits(uint64_t usage);
94
95 // See IGraphicBufferConsumer::setTransformHint
96 status_t setTransformHint(uint32_t hint);
97
98 // See IGraphicBufferConsumer::setMaxAcquiredBufferCount
99 status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
100
101 // See IGraphicBufferConsumer::getSidebandStream
102 sp<NativeHandle> getSidebandStream() const;
103
Dan Stozae77c7662016-05-13 11:37:28 -0700104 // See IGraphicBufferConsumer::getOccupancyHistory
105 status_t getOccupancyHistory(bool forceFlush,
106 std::vector<OccupancyTracker::Segment>* outHistory);
107
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700108 // See IGraphicBufferConsumer::discardFreeBuffers
109 status_t discardFreeBuffers();
110
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111private:
112 ConsumerBase(const ConsumerBase&);
113 void operator=(const ConsumerBase&);
114
115protected:
Jamie Gennis9fea3422012-08-07 18:03:04 -0700116 // ConsumerBase constructs a new ConsumerBase object to consume image
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700117 // buffers from the given IGraphicBufferConsumer.
Mathias Agopian595264f2013-07-16 22:56:09 -0700118 // The controlledByApp flag indicates that this consumer is under the application's
119 // control.
Chih-Hung Hsieh65d47872016-09-01 11:39:25 -0700120 explicit ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700121
Jamie Gennisad669b02013-04-05 16:41:27 -0700122 // onLastStrongRef gets called by RefBase just before the dtor of the most
123 // derived class. It is used to clean up the buffers so that ConsumerBase
124 // can coordinate the clean-up by calling into virtual methods implemented
125 // by the derived classes. This would not be possible from the
126 // ConsuemrBase dtor because by the time that gets called the derived
127 // classes have already been destructed.
128 //
129 // This methods should not need to be overridden by derived classes, but
130 // if they are overridden the ConsumerBase implementation must be called
131 // from the derived class.
132 virtual void onLastStrongRef(const void* id);
133
Pablo Ceballose07e3e52016-03-15 15:07:54 -0700134 // Implementation of the IConsumerListener interface. These
135 // calls are used to notify the ConsumerBase of asynchronous events in the
136 // BufferQueue. The onFrameAvailable, onFrameReplaced, and
137 // onBuffersReleased methods should not need to be overridden by derived
138 // classes, but if they are overridden the ConsumerBase implementation must
139 // be called from the derived class. The ConsumerBase version of
140 // onSidebandStreamChanged does nothing and can be overriden by derived
141 // classes if they want the notification.
142 virtual void onFrameAvailable(const BufferItem& item) override;
143 virtual void onFrameReplaced(const BufferItem& item) override;
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700144 virtual void onBufferAllocated(const BufferItem& item) override;
Pablo Ceballose07e3e52016-03-15 15:07:54 -0700145 virtual void onBuffersReleased() override;
146 virtual void onSidebandStreamChanged() override;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700147
148 // freeBufferLocked frees up the given buffer slot. If the slot has been
149 // initialized this will release the reference to the GraphicBuffer in that
Jamie Gennis9fea3422012-08-07 18:03:04 -0700150 // slot. Otherwise it has no effect.
151 //
152 // Derived classes should override this method to clean up any state they
153 // keep per slot. If it is overridden, the derived class's implementation
154 // must call ConsumerBase::freeBufferLocked.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700155 //
156 // This method must be called with mMutex locked.
157 virtual void freeBufferLocked(int slotIndex);
158
159 // abandonLocked puts the BufferQueue into the abandoned state, causing
160 // all future operations on it to fail. This method rather than the public
161 // abandon method should be overridden by child classes to add abandon-
162 // time behavior.
163 //
Jamie Gennis9fea3422012-08-07 18:03:04 -0700164 // Derived classes should override this method to clean up any object
165 // state they keep (as opposed to per-slot state). If it is overridden,
166 // the derived class's implementation must call ConsumerBase::abandonLocked.
167 //
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700168 // This method must be called with mMutex locked.
169 virtual void abandonLocked();
170
Jamie Gennis9fea3422012-08-07 18:03:04 -0700171 // dumpLocked dumps the current state of the ConsumerBase object to the
172 // result string. Each line is prefixed with the string pointed to by the
173 // prefix argument. The buffer argument points to a buffer that may be
174 // used for intermediate formatting data, and the size of that buffer is
175 // indicated by the size argument.
176 //
177 // Derived classes should override this method to dump their internal
178 // state. If this method is overridden the derived class's implementation
179 // should call ConsumerBase::dumpLocked.
180 //
181 // This method must be called with mMutex locked.
Mathias Agopian74d211a2013-04-22 16:55:35 +0200182 virtual void dumpLocked(String8& result, const char* prefix) const;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700183
184 // acquireBufferLocked fetches the next buffer from the BufferQueue and
185 // updates the buffer slot for the buffer returned.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700186 //
187 // Derived classes should override this method to perform any
188 // initialization that must take place the first time a buffer is assigned
189 // to a slot. If it is overridden the derived class's implementation must
190 // call ConsumerBase::acquireBufferLocked.
Dan Stozaa4650a52015-05-12 12:56:16 -0700191 virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen,
192 uint64_t maxFrameNumber = 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700193
194 // releaseBufferLocked relinquishes control over a buffer, returning that
195 // control to the BufferQueue.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700196 //
197 // Derived classes should override this method to perform any cleanup that
198 // must take place when a buffer is released back to the BufferQueue. If
199 // it is overridden the derived class's implementation must call
Brian Anderson3546a3f2016-07-14 11:51:14 -0700200 // ConsumerBase::releaseBufferLocked.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700201 virtual status_t releaseBufferLocked(int slot,
202 const sp<GraphicBuffer> graphicBuffer,
Chia-I Wu1e24cce2017-11-10 10:36:01 -0800203 EGLDisplay display = EGL_NO_DISPLAY, EGLSyncKHR eglFence = EGL_NO_SYNC_KHR);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700204
205 // returns true iff the slot still has the graphicBuffer in it.
206 bool stillTracking(int slot, const sp<GraphicBuffer> graphicBuffer);
Jamie Gennisb2725412012-09-05 20:09:05 -0700207
Jesse Hall9504eb92012-10-05 14:34:21 -0700208 // addReleaseFence* adds the sync points associated with a fence to the set
Jamie Gennisb2725412012-09-05 20:09:05 -0700209 // of sync points that must be reached before the buffer in the given slot
210 // may be used after the slot has been released. This should be called by
211 // derived classes each time some asynchronous work is kicked off that
212 // references the buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700213 status_t addReleaseFence(int slot,
214 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
215 status_t addReleaseFenceLocked(int slot,
216 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700217
218 // Slot contains the information and object references that
219 // ConsumerBase maintains about a BufferQueue buffer slot.
220 struct Slot {
221 // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
222 // no Gralloc buffer is in the slot.
223 sp<GraphicBuffer> mGraphicBuffer;
224
225 // mFence is a fence which will signal when the buffer associated with
226 // this buffer slot is no longer being used by the consumer and can be
227 // overwritten. The buffer can be dequeued before the fence signals;
228 // the producer is responsible for delaying writes until it signals.
229 sp<Fence> mFence;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700230
231 // the frame number of the last acquired frame for this slot
232 uint64_t mFrameNumber;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700233 };
234
235 // mSlots stores the buffers that have been allocated by the BufferQueue
236 // for each buffer slot. It is initialized to null pointers, and gets
237 // filled in with the result of BufferQueue::acquire when the
238 // client dequeues a buffer from a
239 // slot that has not yet been used. The buffer allocated to a slot will also
240 // be replaced if the requested buffer usage or geometry differs from that
241 // of the buffer allocated to a slot.
Mathias Agopiana9347642017-02-13 16:42:28 -0800242 Slot mSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700243
244 // mAbandoned indicates that the BufferQueue will no longer be used to
Andy McFadden2adaf042012-12-18 09:49:45 -0800245 // consume images buffers pushed to it using the IGraphicBufferProducer
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700246 // interface. It is initialized to false, and set to true in the abandon
247 // method. A BufferQueue that has been abandoned will return the NO_INIT
248 // error from all IConsumerBase methods capable of returning an error.
249 bool mAbandoned;
250
251 // mName is a string used to identify the ConsumerBase in log messages.
252 // It can be set by the setName method.
253 String8 mName;
254
255 // mFrameAvailableListener is the listener object that will be called when a
256 // new frame becomes available. If it is not NULL it will be called from
Dan Stoza95971c82017-06-26 14:27:18 -0700257 // queueBuffer. The listener object is protected by mFrameAvailableMutex
258 // (not mMutex).
259 Mutex mFrameAvailableMutex;
Igor Murashkina4a31492012-10-29 13:36:11 -0700260 wp<FrameAvailableListener> mFrameAvailableListener;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700261
262 // The ConsumerBase has-a BufferQueue and is responsible for creating this object
263 // if none is supplied
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700264 sp<IGraphicBufferConsumer> mConsumer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700265
Brian Anderson3546a3f2016-07-14 11:51:14 -0700266 // The final release fence of the most recent buffer released by
267 // releaseBufferLocked.
268 sp<Fence> mPrevFinalReleaseFence;
269
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700270 // mMutex is the mutex used to prevent concurrent access to the member
271 // variables of ConsumerBase objects. It must be locked whenever the
Jamie Gennis9fea3422012-08-07 18:03:04 -0700272 // member variables are accessed or when any of the *Locked methods are
273 // called.
274 //
275 // This mutex is intended to be locked by derived classes.
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700276 mutable Mutex mMutex;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700277};
278
279// ----------------------------------------------------------------------------
280}; // namespace android
281
282#endif // ANDROID_GUI_CONSUMERBASE_H