xref: /aosp_15_r20/frameworks/native/include/gui/ConsumerBase.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef ANDROID_GUI_CONSUMERBASE_H
18*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_GUI_CONSUMERBASE_H
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <com_android_graphics_libgui_flags.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <gui/BufferQueueDefs.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <gui/IConsumerListener.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <gui/IGraphicBufferConsumer.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <gui/IGraphicBufferProducer.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <gui/OccupancyTracker.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <ui/PixelFormat.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <utils/String8.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <utils/Vector.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <utils/threads.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker namespace android {
32*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker class String8;
35*38e8c45fSAndroid Build Coastguard Worker class GraphicBuffer;
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker // ConsumerBase is a base class for BufferQueue consumer end-points. It
38*38e8c45fSAndroid Build Coastguard Worker // handles common tasks like management of the connection to the BufferQueue
39*38e8c45fSAndroid Build Coastguard Worker // and the buffer pool.
40*38e8c45fSAndroid Build Coastguard Worker class ConsumerBase : public virtual RefBase,
41*38e8c45fSAndroid Build Coastguard Worker         protected ConsumerListener {
42*38e8c45fSAndroid Build Coastguard Worker public:
43*38e8c45fSAndroid Build Coastguard Worker     struct FrameAvailableListener : public virtual RefBase {
44*38e8c45fSAndroid Build Coastguard Worker         // See IConsumerListener::onFrame{Available,Replaced}
45*38e8c45fSAndroid Build Coastguard Worker         virtual void onFrameAvailable(const BufferItem& item) = 0;
onFrameReplacedFrameAvailableListener46*38e8c45fSAndroid Build Coastguard Worker         virtual void onFrameReplaced(const BufferItem& /* item */) {}
onFrameDequeuedFrameAvailableListener47*38e8c45fSAndroid Build Coastguard Worker         virtual void onFrameDequeued(const uint64_t){};
onFrameCancelledFrameAvailableListener48*38e8c45fSAndroid Build Coastguard Worker         virtual void onFrameCancelled(const uint64_t){};
onFrameDetachedFrameAvailableListener49*38e8c45fSAndroid Build Coastguard Worker         virtual void onFrameDetached(const uint64_t){};
50*38e8c45fSAndroid Build Coastguard Worker     };
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker     ~ConsumerBase() override;
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker     // abandon frees all the buffers and puts the ConsumerBase into the
55*38e8c45fSAndroid Build Coastguard Worker     // 'abandoned' state.  Once put in this state the ConsumerBase can never
56*38e8c45fSAndroid Build Coastguard Worker     // leave it.  When in the 'abandoned' state, all methods of the
57*38e8c45fSAndroid Build Coastguard Worker     // IGraphicBufferProducer interface will fail with the NO_INIT error.
58*38e8c45fSAndroid Build Coastguard Worker     //
59*38e8c45fSAndroid Build Coastguard Worker     // Note that while calling this method causes all the buffers to be freed
60*38e8c45fSAndroid Build Coastguard Worker     // from the perspective of the the ConsumerBase, if there are additional
61*38e8c45fSAndroid Build Coastguard Worker     // references on the buffers (e.g. if a buffer is referenced by a client
62*38e8c45fSAndroid Build Coastguard Worker     // or by OpenGL ES as a texture) then those buffer will remain allocated.
63*38e8c45fSAndroid Build Coastguard Worker     void abandon();
64*38e8c45fSAndroid Build Coastguard Worker 
65*38e8c45fSAndroid Build Coastguard Worker     // Returns true if the ConsumerBase is in the 'abandoned' state
66*38e8c45fSAndroid Build Coastguard Worker     bool isAbandoned();
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker     // set the name of the ConsumerBase that will be used to identify it in
69*38e8c45fSAndroid Build Coastguard Worker     // log messages.
70*38e8c45fSAndroid Build Coastguard Worker     void setName(const String8& name);
71*38e8c45fSAndroid Build Coastguard Worker 
72*38e8c45fSAndroid Build Coastguard Worker     // dumpState writes the current state to a string. Child classes should add
73*38e8c45fSAndroid Build Coastguard Worker     // their state to the dump by overriding the dumpLocked method, which is
74*38e8c45fSAndroid Build Coastguard Worker     // called by these methods after locking the mutex.
75*38e8c45fSAndroid Build Coastguard Worker     void dumpState(String8& result) const;
76*38e8c45fSAndroid Build Coastguard Worker     void dumpState(String8& result, const char* prefix) const;
77*38e8c45fSAndroid Build Coastguard Worker 
78*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
79*38e8c45fSAndroid Build Coastguard Worker     // Returns a Surface that can be used as the producer for this consumer.
80*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> getSurface() const;
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker     // DEPRECATED, DO NOT USE. Returns the underlying IGraphicBufferConsumer
83*38e8c45fSAndroid Build Coastguard Worker     // that backs this ConsumerBase.
84*38e8c45fSAndroid Build Coastguard Worker     sp<IGraphicBufferConsumer> getIGraphicBufferConsumer() const
85*38e8c45fSAndroid Build Coastguard Worker             __attribute((deprecated("DO NOT USE: Temporary hack for refactoring")));
86*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
87*38e8c45fSAndroid Build Coastguard Worker 
88*38e8c45fSAndroid Build Coastguard Worker     // setFrameAvailableListener sets the listener object that will be notified
89*38e8c45fSAndroid Build Coastguard Worker     // when a new frame becomes available.
90*38e8c45fSAndroid Build Coastguard Worker     void setFrameAvailableListener(const wp<FrameAvailableListener>& listener);
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::detachBuffer
93*38e8c45fSAndroid Build Coastguard Worker     status_t detachBuffer(int slot) __attribute((
94*38e8c45fSAndroid Build Coastguard Worker             deprecated("Please use the GraphicBuffer variant--slots are deprecated.")));
95*38e8c45fSAndroid Build Coastguard Worker 
96*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
97*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::detachBuffer
98*38e8c45fSAndroid Build Coastguard Worker     status_t detachBuffer(const sp<GraphicBuffer>& buffer);
99*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
100*38e8c45fSAndroid Build Coastguard Worker 
101*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setDefaultBufferSize
102*38e8c45fSAndroid Build Coastguard Worker     status_t setDefaultBufferSize(uint32_t width, uint32_t height);
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setDefaultBufferFormat
105*38e8c45fSAndroid Build Coastguard Worker     status_t setDefaultBufferFormat(PixelFormat defaultFormat);
106*38e8c45fSAndroid Build Coastguard Worker 
107*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setDefaultBufferDataSpace
108*38e8c45fSAndroid Build Coastguard Worker     status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
109*38e8c45fSAndroid Build Coastguard Worker 
110*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setConsumerUsageBits
111*38e8c45fSAndroid Build Coastguard Worker     status_t setConsumerUsageBits(uint64_t usage);
112*38e8c45fSAndroid Build Coastguard Worker 
113*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setTransformHint
114*38e8c45fSAndroid Build Coastguard Worker     status_t setTransformHint(uint32_t hint);
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
117*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setMaxBufferCount
118*38e8c45fSAndroid Build Coastguard Worker     status_t setMaxBufferCount(int bufferCount);
119*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
120*38e8c45fSAndroid Build Coastguard Worker 
121*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::setMaxAcquiredBufferCount
122*38e8c45fSAndroid Build Coastguard Worker     status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
123*38e8c45fSAndroid Build Coastguard Worker 
124*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
125*38e8c45fSAndroid Build Coastguard Worker     status_t setConsumerIsProtected(bool isProtected);
126*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
127*38e8c45fSAndroid Build Coastguard Worker 
128*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::getSidebandStream
129*38e8c45fSAndroid Build Coastguard Worker     sp<NativeHandle> getSidebandStream() const;
130*38e8c45fSAndroid Build Coastguard Worker 
131*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::getOccupancyHistory
132*38e8c45fSAndroid Build Coastguard Worker     status_t getOccupancyHistory(bool forceFlush,
133*38e8c45fSAndroid Build Coastguard Worker             std::vector<OccupancyTracker::Segment>* outHistory);
134*38e8c45fSAndroid Build Coastguard Worker 
135*38e8c45fSAndroid Build Coastguard Worker     // See IGraphicBufferConsumer::discardFreeBuffers
136*38e8c45fSAndroid Build Coastguard Worker     status_t discardFreeBuffers();
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker private:
139*38e8c45fSAndroid Build Coastguard Worker     ConsumerBase(const ConsumerBase&);
140*38e8c45fSAndroid Build Coastguard Worker     void operator=(const ConsumerBase&);
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker     void initialize(bool controlledByApp);
143*38e8c45fSAndroid Build Coastguard Worker 
144*38e8c45fSAndroid Build Coastguard Worker protected:
145*38e8c45fSAndroid Build Coastguard Worker     // ConsumerBase constructs a new ConsumerBase object to consume image
146*38e8c45fSAndroid Build Coastguard Worker     // buffers from the given IGraphicBufferConsumer.
147*38e8c45fSAndroid Build Coastguard Worker     // The controlledByApp flag indicates that this consumer is under the application's
148*38e8c45fSAndroid Build Coastguard Worker     // control.
149*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
150*38e8c45fSAndroid Build Coastguard Worker     explicit ConsumerBase(bool controlledByApp = false, bool consumerIsSurfaceFlinger = false);
151*38e8c45fSAndroid Build Coastguard Worker     explicit ConsumerBase(const sp<IGraphicBufferProducer>& producer,
152*38e8c45fSAndroid Build Coastguard Worker                           const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false);
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker     explicit ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false)
155*38e8c45fSAndroid Build Coastguard Worker             __attribute((deprecated("ConsumerBase should own its own producer, and constructing it "
156*38e8c45fSAndroid Build Coastguard Worker                                     "without one is fragile! This method is going away soon.")));
157*38e8c45fSAndroid Build Coastguard Worker #else
158*38e8c45fSAndroid Build Coastguard Worker     explicit ConsumerBase(const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp = false);
159*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
160*38e8c45fSAndroid Build Coastguard Worker 
161*38e8c45fSAndroid Build Coastguard Worker     // onLastStrongRef gets called by RefBase just before the dtor of the most
162*38e8c45fSAndroid Build Coastguard Worker     // derived class.  It is used to clean up the buffers so that ConsumerBase
163*38e8c45fSAndroid Build Coastguard Worker     // can coordinate the clean-up by calling into virtual methods implemented
164*38e8c45fSAndroid Build Coastguard Worker     // by the derived classes.  This would not be possible from the
165*38e8c45fSAndroid Build Coastguard Worker     // ConsuemrBase dtor because by the time that gets called the derived
166*38e8c45fSAndroid Build Coastguard Worker     // classes have already been destructed.
167*38e8c45fSAndroid Build Coastguard Worker     //
168*38e8c45fSAndroid Build Coastguard Worker     // This methods should not need to be overridden by derived classes, but
169*38e8c45fSAndroid Build Coastguard Worker     // if they are overridden the ConsumerBase implementation must be called
170*38e8c45fSAndroid Build Coastguard Worker     // from the derived class.
171*38e8c45fSAndroid Build Coastguard Worker     virtual void onLastStrongRef(const void* id);
172*38e8c45fSAndroid Build Coastguard Worker 
173*38e8c45fSAndroid Build Coastguard Worker     // Implementation of the IConsumerListener interface.  These
174*38e8c45fSAndroid Build Coastguard Worker     // calls are used to notify the ConsumerBase of asynchronous events in the
175*38e8c45fSAndroid Build Coastguard Worker     // BufferQueue.  The onFrameAvailable, onFrameReplaced, and
176*38e8c45fSAndroid Build Coastguard Worker     // onBuffersReleased methods should not need to be overridden by derived
177*38e8c45fSAndroid Build Coastguard Worker     // classes, but if they are overridden the ConsumerBase implementation must
178*38e8c45fSAndroid Build Coastguard Worker     // be called from the derived class. The ConsumerBase version of
179*38e8c45fSAndroid Build Coastguard Worker     // onSidebandStreamChanged does nothing and can be overriden by derived
180*38e8c45fSAndroid Build Coastguard Worker     // classes if they want the notification.
181*38e8c45fSAndroid Build Coastguard Worker     virtual void onFrameAvailable(const BufferItem& item) override;
182*38e8c45fSAndroid Build Coastguard Worker     virtual void onFrameReplaced(const BufferItem& item) override;
183*38e8c45fSAndroid Build Coastguard Worker     virtual void onFrameDequeued(const uint64_t bufferId) override;
184*38e8c45fSAndroid Build Coastguard Worker     virtual void onFrameCancelled(const uint64_t bufferId) override;
185*38e8c45fSAndroid Build Coastguard Worker     virtual void onFrameDetached(const uint64_t bufferId) override;
186*38e8c45fSAndroid Build Coastguard Worker     virtual void onBuffersReleased() override;
187*38e8c45fSAndroid Build Coastguard Worker     virtual void onSidebandStreamChanged() override;
188*38e8c45fSAndroid Build Coastguard Worker 
189*38e8c45fSAndroid Build Coastguard Worker     virtual int getSlotForBufferLocked(const sp<GraphicBuffer>& buffer);
190*38e8c45fSAndroid Build Coastguard Worker 
191*38e8c45fSAndroid Build Coastguard Worker     virtual status_t detachBufferLocked(int slotIndex);
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker     // freeBufferLocked frees up the given buffer slot.  If the slot has been
194*38e8c45fSAndroid Build Coastguard Worker     // initialized this will release the reference to the GraphicBuffer in that
195*38e8c45fSAndroid Build Coastguard Worker     // slot.  Otherwise it has no effect.
196*38e8c45fSAndroid Build Coastguard Worker     //
197*38e8c45fSAndroid Build Coastguard Worker     // Derived classes should override this method to clean up any state they
198*38e8c45fSAndroid Build Coastguard Worker     // keep per slot.  If it is overridden, the derived class's implementation
199*38e8c45fSAndroid Build Coastguard Worker     // must call ConsumerBase::freeBufferLocked.
200*38e8c45fSAndroid Build Coastguard Worker     //
201*38e8c45fSAndroid Build Coastguard Worker     // This method must be called with mMutex locked.
202*38e8c45fSAndroid Build Coastguard Worker     virtual void freeBufferLocked(int slotIndex);
203*38e8c45fSAndroid Build Coastguard Worker 
204*38e8c45fSAndroid Build Coastguard Worker     // abandonLocked puts the BufferQueue into the abandoned state, causing
205*38e8c45fSAndroid Build Coastguard Worker     // all future operations on it to fail. This method rather than the public
206*38e8c45fSAndroid Build Coastguard Worker     // abandon method should be overridden by child classes to add abandon-
207*38e8c45fSAndroid Build Coastguard Worker     // time behavior.
208*38e8c45fSAndroid Build Coastguard Worker     //
209*38e8c45fSAndroid Build Coastguard Worker     // Derived classes should override this method to clean up any object
210*38e8c45fSAndroid Build Coastguard Worker     // state they keep (as opposed to per-slot state).  If it is overridden,
211*38e8c45fSAndroid Build Coastguard Worker     // the derived class's implementation must call ConsumerBase::abandonLocked.
212*38e8c45fSAndroid Build Coastguard Worker     //
213*38e8c45fSAndroid Build Coastguard Worker     // This method must be called with mMutex locked.
214*38e8c45fSAndroid Build Coastguard Worker     virtual void abandonLocked();
215*38e8c45fSAndroid Build Coastguard Worker 
216*38e8c45fSAndroid Build Coastguard Worker     // dumpLocked dumps the current state of the ConsumerBase object to the
217*38e8c45fSAndroid Build Coastguard Worker     // result string.  Each line is prefixed with the string pointed to by the
218*38e8c45fSAndroid Build Coastguard Worker     // prefix argument.  The buffer argument points to a buffer that may be
219*38e8c45fSAndroid Build Coastguard Worker     // used for intermediate formatting data, and the size of that buffer is
220*38e8c45fSAndroid Build Coastguard Worker     // indicated by the size argument.
221*38e8c45fSAndroid Build Coastguard Worker     //
222*38e8c45fSAndroid Build Coastguard Worker     // Derived classes should override this method to dump their internal
223*38e8c45fSAndroid Build Coastguard Worker     // state.  If this method is overridden the derived class's implementation
224*38e8c45fSAndroid Build Coastguard Worker     // should call ConsumerBase::dumpLocked.
225*38e8c45fSAndroid Build Coastguard Worker     //
226*38e8c45fSAndroid Build Coastguard Worker     // This method must be called with mMutex locked.
227*38e8c45fSAndroid Build Coastguard Worker     virtual void dumpLocked(String8& result, const char* prefix) const;
228*38e8c45fSAndroid Build Coastguard Worker 
229*38e8c45fSAndroid Build Coastguard Worker     // acquireBufferLocked fetches the next buffer from the BufferQueue and
230*38e8c45fSAndroid Build Coastguard Worker     // updates the buffer slot for the buffer returned.
231*38e8c45fSAndroid Build Coastguard Worker     //
232*38e8c45fSAndroid Build Coastguard Worker     // Derived classes should override this method to perform any
233*38e8c45fSAndroid Build Coastguard Worker     // initialization that must take place the first time a buffer is assigned
234*38e8c45fSAndroid Build Coastguard Worker     // to a slot.  If it is overridden the derived class's implementation must
235*38e8c45fSAndroid Build Coastguard Worker     // call ConsumerBase::acquireBufferLocked.
236*38e8c45fSAndroid Build Coastguard Worker     virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen,
237*38e8c45fSAndroid Build Coastguard Worker             uint64_t maxFrameNumber = 0);
238*38e8c45fSAndroid Build Coastguard Worker 
239*38e8c45fSAndroid Build Coastguard Worker     // releaseBufferLocked relinquishes control over a buffer, returning that
240*38e8c45fSAndroid Build Coastguard Worker     // control to the BufferQueue.
241*38e8c45fSAndroid Build Coastguard Worker     //
242*38e8c45fSAndroid Build Coastguard Worker     // Derived classes should override this method to perform any cleanup that
243*38e8c45fSAndroid Build Coastguard Worker     // must take place when a buffer is released back to the BufferQueue.  If
244*38e8c45fSAndroid Build Coastguard Worker     // it is overridden the derived class's implementation must call
245*38e8c45fSAndroid Build Coastguard Worker     // ConsumerBase::releaseBufferLocked.
246*38e8c45fSAndroid Build Coastguard Worker     virtual status_t releaseBufferLocked(int slot,
247*38e8c45fSAndroid Build Coastguard Worker             const sp<GraphicBuffer> graphicBuffer,
248*38e8c45fSAndroid Build Coastguard Worker             EGLDisplay display = EGL_NO_DISPLAY, EGLSyncKHR eglFence = EGL_NO_SYNC_KHR);
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker     // returns true iff the slot still has the graphicBuffer in it.
251*38e8c45fSAndroid Build Coastguard Worker     bool stillTracking(int slot, const sp<GraphicBuffer> graphicBuffer);
252*38e8c45fSAndroid Build Coastguard Worker 
253*38e8c45fSAndroid Build Coastguard Worker     // addReleaseFence* adds the sync points associated with a fence to the set
254*38e8c45fSAndroid Build Coastguard Worker     // of sync points that must be reached before the buffer in the given slot
255*38e8c45fSAndroid Build Coastguard Worker     // may be used after the slot has been released.  This should be called by
256*38e8c45fSAndroid Build Coastguard Worker     // derived classes each time some asynchronous work is kicked off that
257*38e8c45fSAndroid Build Coastguard Worker     // references the buffer.
258*38e8c45fSAndroid Build Coastguard Worker     status_t addReleaseFence(int slot,
259*38e8c45fSAndroid Build Coastguard Worker             const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
260*38e8c45fSAndroid Build Coastguard Worker     status_t addReleaseFenceLocked(int slot,
261*38e8c45fSAndroid Build Coastguard Worker             const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence);
262*38e8c45fSAndroid Build Coastguard Worker 
263*38e8c45fSAndroid Build Coastguard Worker     // Slot contains the information and object references that
264*38e8c45fSAndroid Build Coastguard Worker     // ConsumerBase maintains about a BufferQueue buffer slot.
265*38e8c45fSAndroid Build Coastguard Worker     struct Slot {
266*38e8c45fSAndroid Build Coastguard Worker         // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
267*38e8c45fSAndroid Build Coastguard Worker         // no Gralloc buffer is in the slot.
268*38e8c45fSAndroid Build Coastguard Worker         sp<GraphicBuffer> mGraphicBuffer;
269*38e8c45fSAndroid Build Coastguard Worker 
270*38e8c45fSAndroid Build Coastguard Worker         // mFence is a fence which will signal when the buffer associated with
271*38e8c45fSAndroid Build Coastguard Worker         // this buffer slot is no longer being used by the consumer and can be
272*38e8c45fSAndroid Build Coastguard Worker         // overwritten. The buffer can be dequeued before the fence signals;
273*38e8c45fSAndroid Build Coastguard Worker         // the producer is responsible for delaying writes until it signals.
274*38e8c45fSAndroid Build Coastguard Worker         sp<Fence> mFence;
275*38e8c45fSAndroid Build Coastguard Worker 
276*38e8c45fSAndroid Build Coastguard Worker         // the frame number of the last acquired frame for this slot
277*38e8c45fSAndroid Build Coastguard Worker         uint64_t mFrameNumber;
278*38e8c45fSAndroid Build Coastguard Worker     };
279*38e8c45fSAndroid Build Coastguard Worker 
280*38e8c45fSAndroid Build Coastguard Worker     // mSlots stores the buffers that have been allocated by the BufferQueue
281*38e8c45fSAndroid Build Coastguard Worker     // for each buffer slot.  It is initialized to null pointers, and gets
282*38e8c45fSAndroid Build Coastguard Worker     // filled in with the result of BufferQueue::acquire when the
283*38e8c45fSAndroid Build Coastguard Worker     // client dequeues a buffer from a
284*38e8c45fSAndroid Build Coastguard Worker     // slot that has not yet been used. The buffer allocated to a slot will also
285*38e8c45fSAndroid Build Coastguard Worker     // be replaced if the requested buffer usage or geometry differs from that
286*38e8c45fSAndroid Build Coastguard Worker     // of the buffer allocated to a slot.
287*38e8c45fSAndroid Build Coastguard Worker     Slot mSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
288*38e8c45fSAndroid Build Coastguard Worker 
289*38e8c45fSAndroid Build Coastguard Worker     // mAbandoned indicates that the BufferQueue will no longer be used to
290*38e8c45fSAndroid Build Coastguard Worker     // consume images buffers pushed to it using the IGraphicBufferProducer
291*38e8c45fSAndroid Build Coastguard Worker     // interface. It is initialized to false, and set to true in the abandon
292*38e8c45fSAndroid Build Coastguard Worker     // method.  A BufferQueue that has been abandoned will return the NO_INIT
293*38e8c45fSAndroid Build Coastguard Worker     // error from all IConsumerBase methods capable of returning an error.
294*38e8c45fSAndroid Build Coastguard Worker     bool mAbandoned;
295*38e8c45fSAndroid Build Coastguard Worker 
296*38e8c45fSAndroid Build Coastguard Worker     // mName is a string used to identify the ConsumerBase in log messages.
297*38e8c45fSAndroid Build Coastguard Worker     // It can be set by the setName method.
298*38e8c45fSAndroid Build Coastguard Worker     String8 mName;
299*38e8c45fSAndroid Build Coastguard Worker 
300*38e8c45fSAndroid Build Coastguard Worker     // mFrameAvailableListener is the listener object that will be called when a
301*38e8c45fSAndroid Build Coastguard Worker     // new frame becomes available. If it is not NULL it will be called from
302*38e8c45fSAndroid Build Coastguard Worker     // queueBuffer. The listener object is protected by mFrameAvailableMutex
303*38e8c45fSAndroid Build Coastguard Worker     // (not mMutex).
304*38e8c45fSAndroid Build Coastguard Worker     Mutex mFrameAvailableMutex;
305*38e8c45fSAndroid Build Coastguard Worker     wp<FrameAvailableListener> mFrameAvailableListener;
306*38e8c45fSAndroid Build Coastguard Worker 
307*38e8c45fSAndroid Build Coastguard Worker     // The ConsumerBase has-a BufferQueue and is responsible for creating these
308*38e8c45fSAndroid Build Coastguard Worker     // objects if not supplied.
309*38e8c45fSAndroid Build Coastguard Worker     sp<IGraphicBufferConsumer> mConsumer;
310*38e8c45fSAndroid Build Coastguard Worker 
311*38e8c45fSAndroid Build Coastguard Worker #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
312*38e8c45fSAndroid Build Coastguard Worker     // This Surface wraps the IGraphicBufferConsumer created for this
313*38e8c45fSAndroid Build Coastguard Worker     // ConsumerBase.
314*38e8c45fSAndroid Build Coastguard Worker     sp<Surface> mSurface;
315*38e8c45fSAndroid Build Coastguard Worker #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
316*38e8c45fSAndroid Build Coastguard Worker 
317*38e8c45fSAndroid Build Coastguard Worker     // The final release fence of the most recent buffer released by
318*38e8c45fSAndroid Build Coastguard Worker     // releaseBufferLocked.
319*38e8c45fSAndroid Build Coastguard Worker     sp<Fence> mPrevFinalReleaseFence;
320*38e8c45fSAndroid Build Coastguard Worker 
321*38e8c45fSAndroid Build Coastguard Worker     // mMutex is the mutex used to prevent concurrent access to the member
322*38e8c45fSAndroid Build Coastguard Worker     // variables of ConsumerBase objects. It must be locked whenever the
323*38e8c45fSAndroid Build Coastguard Worker     // member variables are accessed or when any of the *Locked methods are
324*38e8c45fSAndroid Build Coastguard Worker     // called.
325*38e8c45fSAndroid Build Coastguard Worker     //
326*38e8c45fSAndroid Build Coastguard Worker     // This mutex is intended to be locked by derived classes.
327*38e8c45fSAndroid Build Coastguard Worker     mutable Mutex mMutex;
328*38e8c45fSAndroid Build Coastguard Worker };
329*38e8c45fSAndroid Build Coastguard Worker 
330*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------
331*38e8c45fSAndroid Build Coastguard Worker }; // namespace android
332*38e8c45fSAndroid Build Coastguard Worker 
333*38e8c45fSAndroid Build Coastguard Worker #endif // ANDROID_GUI_CONSUMERBASE_H
334