1 /*
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_IGRAPHICBUFFERPRODUCER_H
18 #define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <utils/Errors.h>
24 #include <utils/RefBase.h>
25 
26 #include <binder/IInterface.h>
27 
28 #include <ui/Fence.h>
29 #include <ui/GraphicBuffer.h>
30 #include <ui/Rect.h>
31 #include <ui/Region.h>
32 
33 #include <gui/FrameTimestamps.h>
34 #include <gui/HdrMetadata.h>
35 
36 #include <hidl/HybridInterface.h>
37 #include <android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer.h>
38 
39 namespace android {
40 // ----------------------------------------------------------------------------
41 
42 class IProducerListener;
43 class NativeHandle;
44 class Surface;
45 typedef ::android::hardware::graphics::bufferqueue::V1_0::IGraphicBufferProducer
46         HGraphicBufferProducer;
47 
48 /*
49  * This class defines the Binder IPC interface for the producer side of
50  * a queue of graphics buffers.  It's used to send graphics data from one
51  * component to another.  For example, a class that decodes video for
52  * playback might use this to provide frames.  This is typically done
53  * indirectly, through Surface.
54  *
55  * The underlying mechanism is a BufferQueue, which implements
56  * BnGraphicBufferProducer.  In normal operation, the producer calls
57  * dequeueBuffer() to get an empty buffer, fills it with data, then
58  * calls queueBuffer() to make it available to the consumer.
59  *
60  * This class was previously called ISurfaceTexture.
61  */
62 class IGraphicBufferProducer : public IInterface
63 {
64 public:
65     DECLARE_HYBRID_META_INTERFACE(GraphicBufferProducer, HGraphicBufferProducer)
66 
67     enum {
68         // A flag returned by dequeueBuffer when the client needs to call
69         // requestBuffer immediately thereafter.
70         BUFFER_NEEDS_REALLOCATION = 0x1,
71         // A flag returned by dequeueBuffer when all mirrored slots should be
72         // released by the client. This flag should always be processed first.
73         RELEASE_ALL_BUFFERS       = 0x2,
74     };
75 
76     enum {
77         // A parcelable magic indicates using Binder BufferQueue as transport
78         // backend.
79         USE_BUFFER_QUEUE = 0x62717565, // 'bque'
80         // A parcelable magic indicates using BufferHub as transport backend.
81         USE_BUFFER_HUB = 0x62687562, // 'bhub'
82     };
83 
84     // requestBuffer requests a new buffer for the given index. The server (i.e.
85     // the IGraphicBufferProducer implementation) assigns the newly created
86     // buffer to the given slot index, and the client is expected to mirror the
87     // slot->buffer mapping so that it's not necessary to transfer a
88     // GraphicBuffer for every dequeue operation.
89     //
90     // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
91     //
92     // Return of a value other than NO_ERROR means an error has occurred:
93     // * NO_INIT - the buffer queue has been abandoned or the producer is not
94     //             connected.
95     // * BAD_VALUE - one of the two conditions occurred:
96     //              * slot was out of range (see above)
97     //              * buffer specified by the slot is not dequeued
98     virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
99 
100     // setMaxDequeuedBufferCount sets the maximum number of buffers that can be
101     // dequeued by the producer at one time. If this method succeeds, any new
102     // buffer slots will be both unallocated and owned by the BufferQueue object
103     // (i.e. they are not owned by the producer or consumer). Calling this may
104     // also cause some buffer slots to be emptied. If the caller is caching the
105     // contents of the buffer slots, it should empty that cache after calling
106     // this method.
107     //
108     // This function should not be called with a value of maxDequeuedBuffers
109     // that is less than the number of currently dequeued buffer slots. Doing so
110     // will result in a BAD_VALUE error.
111     //
112     // The buffer count should be at least 1 (inclusive), but at most
113     // (NUM_BUFFER_SLOTS - the minimum undequeued buffer count) (exclusive). The
114     // minimum undequeued buffer count can be obtained by calling
115     // query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS).
116     //
117     // Return of a value other than NO_ERROR means an error has occurred:
118     // * NO_INIT - the buffer queue has been abandoned.
119     // * BAD_VALUE - one of the below conditions occurred:
120     //     * bufferCount was out of range (see above).
121     //     * client would have more than the requested number of dequeued
122     //       buffers after this call.
123     //     * this call would cause the maxBufferCount value to be exceeded.
124     //     * failure to adjust the number of available slots.
125     virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) = 0;
126 
127     // Set the async flag if the producer intends to asynchronously queue
128     // buffers without blocking. Typically this is used for triple-buffering
129     // and/or when the swap interval is set to zero.
130     //
131     // Enabling async mode will internally allocate an additional buffer to
132     // allow for the asynchronous behavior. If it is not enabled queue/dequeue
133     // calls may block.
134     //
135     // Return of a value other than NO_ERROR means an error has occurred:
136     // * NO_INIT - the buffer queue has been abandoned.
137     // * BAD_VALUE - one of the following has occurred:
138     //             * this call would cause the maxBufferCount value to be
139     //               exceeded
140     //             * failure to adjust the number of available slots.
141     virtual status_t setAsyncMode(bool async) = 0;
142 
143     // dequeueBuffer requests a new buffer slot for the client to use. Ownership
144     // of the slot is transfered to the client, meaning that the server will not
145     // use the contents of the buffer associated with that slot.
146     //
147     // The slot index returned may or may not contain a buffer (client-side).
148     // If the slot is empty the client should call requestBuffer to assign a new
149     // buffer to that slot.
150     //
151     // Once the client is done filling this buffer, it is expected to transfer
152     // buffer ownership back to the server with either cancelBuffer on
153     // the dequeued slot or to fill in the contents of its associated buffer
154     // contents and call queueBuffer.
155     //
156     // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is
157     // expected to call requestBuffer immediately.
158     //
159     // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is
160     // expected to release all of the mirrored slot->buffer mappings.
161     //
162     // The fence parameter will be updated to hold the fence associated with
163     // the buffer. The contents of the buffer must not be overwritten until the
164     // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written
165     // immediately.
166     //
167     // The width and height parameters must be no greater than the minimum of
168     // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
169     // An error due to invalid dimensions might not be reported until
170     // updateTexImage() is called.  If width and height are both zero, the
171     // default values specified by setDefaultBufferSize() are used instead.
172     //
173     // If the format is 0, the default format will be used.
174     //
175     // The usage argument specifies gralloc buffer usage flags.  The values
176     // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER.  These
177     // will be merged with the usage flags specified by
178     // IGraphicBufferConsumer::setConsumerUsageBits.
179     //
180     // This call will block until a buffer is available to be dequeued. If
181     // both the producer and consumer are controlled by the app, then this call
182     // can never block and will return WOULD_BLOCK if no buffer is available.
183     //
184     // A non-negative value with flags set (see above) will be returned upon
185     // success.
186     //
187     // Return of a negative means an error has occurred:
188     // * NO_INIT - the buffer queue has been abandoned or the producer is not
189     //             connected.
190     // * BAD_VALUE - both in async mode and buffer count was less than the
191     //               max numbers of buffers that can be allocated at once.
192     // * INVALID_OPERATION - cannot attach the buffer because it would cause
193     //                       too many buffers to be dequeued, either because
194     //                       the producer already has a single buffer dequeued
195     //                       and did not set a buffer count, or because a
196     //                       buffer count was set and this call would cause
197     //                       it to be exceeded.
198     // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled
199     //                 since both the producer/consumer are controlled by app
200     // * NO_MEMORY - out of memory, cannot allocate the graphics buffer.
201     // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while
202     //               waiting for a buffer to become available.
203     //
204     // All other negative values are an unknown error returned downstream
205     // from the graphics allocator (typically errno).
206     virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w, uint32_t h,
207                                    PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
208                                    FrameEventHistoryDelta* outTimestamps) = 0;
209 
210     // detachBuffer attempts to remove all ownership of the buffer in the given
211     // slot from the buffer queue. If this call succeeds, the slot will be
212     // freed, and there will be no way to obtain the buffer from this interface.
213     // The freed slot will remain unallocated until either it is selected to
214     // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
215     // to the slot. The buffer must have already been dequeued, and the caller
216     // must already possesses the sp<GraphicBuffer> (i.e., must have called
217     // requestBuffer).
218     //
219     // Return of a value other than NO_ERROR means an error has occurred:
220     // * NO_INIT - the buffer queue has been abandoned or the producer is not
221     //             connected.
222     // * BAD_VALUE - the given slot number is invalid, either because it is
223     //               out of the range [0, NUM_BUFFER_SLOTS), or because the slot
224     //               it refers to is not currently dequeued and requested.
225     virtual status_t detachBuffer(int slot) = 0;
226 
227     // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer,
228     // and detachBuffer in sequence, except for two things:
229     //
230     // 1) It is unnecessary to know the dimensions, format, or usage of the
231     //    next buffer.
232     // 2) It will not block, since if it cannot find an appropriate buffer to
233     //    return, it will return an error instead.
234     //
235     // Only slots that are free but still contain a GraphicBuffer will be
236     // considered, and the oldest of those will be returned. outBuffer is
237     // equivalent to outBuffer from the requestBuffer call, and outFence is
238     // equivalent to fence from the dequeueBuffer call.
239     //
240     // Return of a value other than NO_ERROR means an error has occurred:
241     // * NO_INIT - the buffer queue has been abandoned or the producer is not
242     //             connected.
243     // * BAD_VALUE - either outBuffer or outFence were NULL.
244     // * NO_MEMORY - no slots were found that were both free and contained a
245     //               GraphicBuffer.
246     virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
247             sp<Fence>* outFence) = 0;
248 
249     // attachBuffer attempts to transfer ownership of a buffer to the buffer
250     // queue. If this call succeeds, it will be as if this buffer was dequeued
251     // from the returned slot number. As such, this call will fail if attaching
252     // this buffer would cause too many buffers to be simultaneously dequeued.
253     //
254     // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is
255     // expected to release all of the mirrored slot->buffer mappings.
256     //
257     // A non-negative value with flags set (see above) will be returned upon
258     // success.
259     //
260     // Return of a negative value means an error has occurred:
261     // * NO_INIT - the buffer queue has been abandoned or the producer is not
262     //             connected.
263     // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of
264     //               async mode and buffer count override, or the generation
265     //               number of the buffer did not match the buffer queue.
266     // * INVALID_OPERATION - cannot attach the buffer because it would cause
267     //                       too many buffers to be dequeued, either because
268     //                       the producer already has a single buffer dequeued
269     //                       and did not set a buffer count, or because a
270     //                       buffer count was set and this call would cause
271     //                       it to be exceeded.
272     // * WOULD_BLOCK - no buffer slot is currently available, and blocking is
273     //                 disabled since both the producer/consumer are
274     //                 controlled by the app.
275     // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while
276     //               waiting for a slot to become available.
277     virtual status_t attachBuffer(int* outSlot,
278             const sp<GraphicBuffer>& buffer) = 0;
279 
280     // queueBuffer indicates that the client has finished filling in the
281     // contents of the buffer associated with slot and transfers ownership of
282     // that slot back to the server.
283     //
284     // It is not valid to call queueBuffer on a slot that is not owned
285     // by the client or one for which a buffer associated via requestBuffer
286     // (an attempt to do so will fail with a return value of BAD_VALUE).
287     //
288     // In addition, the input must be described by the client (as documented
289     // below). Any other properties (zero point, etc)
290     // are client-dependent, and should be documented by the client.
291     //
292     // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
293     //
294     // Upon success, the output will be filled with meaningful values
295     // (refer to the documentation below).
296     //
297     // Return of a value other than NO_ERROR means an error has occurred:
298     // * NO_INIT - the buffer queue has been abandoned or the producer is not
299     //             connected.
300     // * BAD_VALUE - one of the below conditions occurred:
301     //              * fence was NULL
302     //              * scaling mode was unknown
303     //              * both in async mode and buffer count was less than the
304     //                max numbers of buffers that can be allocated at once
305     //              * slot index was out of range (see above).
306     //              * the slot was not in the dequeued state
307     //              * the slot was enqueued without requesting a buffer
308     //              * crop rect is out of bounds of the buffer dimensions
309 
310     struct QueueBufferInput : public Flattenable<QueueBufferInput> {
311         friend class Flattenable<QueueBufferInput>;
312         explicit inline QueueBufferInput(const Parcel& parcel);
313 
314         // timestamp - a monotonically increasing value in nanoseconds
315         // isAutoTimestamp - if the timestamp was synthesized at queue time
316         // dataSpace - description of the contents, interpretation depends on format
317         // crop - a crop rectangle that's used as a hint to the consumer
318         // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h>
319         // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h>
320         // fence - a fence that the consumer must wait on before reading the buffer,
321         //         set this to Fence::NO_FENCE if the buffer is ready immediately
322         // sticky - the sticky transform set in Surface (only used by the LEGACY
323         //          camera mode).
324         // getFrameTimestamps - whether or not the latest frame timestamps
325         //                      should be retrieved from the consumer.
326         inline QueueBufferInput(int64_t _timestamp, bool _isAutoTimestamp,
327                 android_dataspace _dataSpace, const Rect& _crop,
328                 int _scalingMode, uint32_t _transform, const sp<Fence>& _fence,
329                 uint32_t _sticky = 0, bool _getFrameTimestamps = false)
timestampQueueBufferInput330                 : timestamp(_timestamp), isAutoTimestamp(_isAutoTimestamp),
331                   dataSpace(_dataSpace), crop(_crop), scalingMode(_scalingMode),
332                   transform(_transform), stickyTransform(_sticky), fence(_fence),
333                   surfaceDamage(), getFrameTimestamps(_getFrameTimestamps) { }
334 
335         inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
336                 android_dataspace* outDataSpace,
337                 Rect* outCrop, int* outScalingMode,
338                 uint32_t* outTransform, sp<Fence>* outFence,
339                 uint32_t* outStickyTransform = nullptr,
340                 bool* outGetFrameTimestamps = nullptr) const {
341             *outTimestamp = timestamp;
342             *outIsAutoTimestamp = bool(isAutoTimestamp);
343             *outDataSpace = dataSpace;
344             *outCrop = crop;
345             *outScalingMode = scalingMode;
346             *outTransform = transform;
347             *outFence = fence;
348             if (outStickyTransform != NULL) {
349                 *outStickyTransform = stickyTransform;
350             }
351             if (outGetFrameTimestamps) {
352                 *outGetFrameTimestamps = getFrameTimestamps;
353             }
354         }
355 
356         // Flattenable protocol
357         static constexpr size_t minFlattenedSize();
358         size_t getFlattenedSize() const;
359         size_t getFdCount() const;
360         status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
361         status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
362 
getSurfaceDamageQueueBufferInput363         const Region& getSurfaceDamage() const { return surfaceDamage; }
setSurfaceDamageQueueBufferInput364         void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; }
365 
getHdrMetadataQueueBufferInput366         const HdrMetadata& getHdrMetadata() const { return hdrMetadata; }
setHdrMetadataQueueBufferInput367         void setHdrMetadata(const HdrMetadata& metadata) { hdrMetadata = metadata; }
368 
369     private:
370         int64_t timestamp{0};
371         int isAutoTimestamp{0};
372         android_dataspace dataSpace{HAL_DATASPACE_UNKNOWN};
373         Rect crop;
374         int scalingMode{0};
375         uint32_t transform{0};
376         uint32_t stickyTransform{0};
377         sp<Fence> fence;
378         Region surfaceDamage;
379         bool getFrameTimestamps{false};
380         HdrMetadata hdrMetadata;
381     };
382 
383     struct QueueBufferOutput : public Flattenable<QueueBufferOutput> {
384         QueueBufferOutput() = default;
385 
386         // Moveable.
387         QueueBufferOutput(QueueBufferOutput&& src) = default;
388         QueueBufferOutput& operator=(QueueBufferOutput&& src) = default;
389         // Not copyable.
390         QueueBufferOutput(const QueueBufferOutput& src) = delete;
391         QueueBufferOutput& operator=(const QueueBufferOutput& src) = delete;
392 
393         // Flattenable protocol
394         static constexpr size_t minFlattenedSize();
395         size_t getFlattenedSize() const;
396         size_t getFdCount() const;
397         status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
398         status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
399 
400         uint32_t width{0};
401         uint32_t height{0};
402         uint32_t transformHint{0};
403         uint32_t numPendingBuffers{0};
404         uint64_t nextFrameNumber{0};
405         FrameEventHistoryDelta frameTimestamps;
406         bool bufferReplaced{false};
407     };
408 
409     virtual status_t queueBuffer(int slot, const QueueBufferInput& input,
410             QueueBufferOutput* output) = 0;
411 
412     // cancelBuffer indicates that the client does not wish to fill in the
413     // buffer associated with slot and transfers ownership of the slot back to
414     // the server.
415     //
416     // The buffer is not queued for use by the consumer.
417     //
418     // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
419     //
420     // The buffer will not be overwritten until the fence signals.  The fence
421     // will usually be the one obtained from dequeueBuffer.
422     //
423     // Return of a value other than NO_ERROR means an error has occurred:
424     // * NO_INIT - the buffer queue has been abandoned or the producer is not
425     //             connected.
426     // * BAD_VALUE - one of the below conditions occurred:
427     //              * fence was NULL
428     //              * slot index was out of range (see above).
429     //              * the slot was not in the dequeued state
430     virtual status_t cancelBuffer(int slot, const sp<Fence>& fence) = 0;
431 
432     // query retrieves some information for this surface
433     // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h>
434     //
435     // Return of a value other than NO_ERROR means an error has occurred:
436     // * NO_INIT - the buffer queue has been abandoned.
437     // * BAD_VALUE - what was out of range
438     virtual int query(int what, int* value) = 0;
439 
440     // connect attempts to connect a client API to the IGraphicBufferProducer.
441     // This must be called before any other IGraphicBufferProducer methods are
442     // called except for getAllocator. A consumer must be already connected.
443     //
444     // This method will fail if the connect was previously called on the
445     // IGraphicBufferProducer and no corresponding disconnect call was made.
446     //
447     // The listener is an optional binder callback object that can be used if
448     // the producer wants to be notified when the consumer releases a buffer
449     // back to the BufferQueue. It is also used to detect the death of the
450     // producer. If only the latter functionality is desired, there is a
451     // DummyProducerListener class in IProducerListener.h that can be used.
452     //
453     // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
454     //
455     // The producerControlledByApp should be set to true if the producer is hosted
456     // by an untrusted process (typically app_process-forked processes). If both
457     // the producer and the consumer are app-controlled then all buffer queues
458     // will operate in async mode regardless of the async flag.
459     //
460     // Upon success, the output will be filled with meaningful data
461     // (refer to QueueBufferOutput documentation above).
462     //
463     // Return of a value other than NO_ERROR means an error has occurred:
464     // * NO_INIT - one of the following occurred:
465     //             * the buffer queue was abandoned
466     //             * no consumer has yet connected
467     // * BAD_VALUE - one of the following has occurred:
468     //             * the producer is already connected
469     //             * api was out of range (see above).
470     //             * output was NULL.
471     //             * Failure to adjust the number of available slots. This can
472     //               happen because of trying to allocate/deallocate the async
473     //               buffer in response to the value of producerControlledByApp.
474     // * DEAD_OBJECT - the token is hosted by an already-dead process
475     //
476     // Additional negative errors may be returned by the internals, they
477     // should be treated as opaque fatal unrecoverable errors.
478     virtual status_t connect(const sp<IProducerListener>& listener,
479             int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
480 
481     enum class DisconnectMode {
482         // Disconnect only the specified API.
483         Api,
484         // Disconnect any API originally connected from the process calling disconnect.
485         AllLocal
486     };
487 
488     // disconnect attempts to disconnect a client API from the
489     // IGraphicBufferProducer.  Calling this method will cause any subsequent
490     // calls to other IGraphicBufferProducer methods to fail except for
491     // getAllocator and connect.  Successfully calling connect after this will
492     // allow the other methods to succeed again.
493     //
494     // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
495     //
496     // Alternatively if mode is AllLocal, then the API value is ignored, and any API
497     // connected from the same PID calling disconnect will be disconnected.
498     //
499     // Disconnecting from an abandoned IGraphicBufferProducer is legal and
500     // is considered a no-op.
501     //
502     // Return of a value other than NO_ERROR means an error has occurred:
503     // * NO_INIT - the producer is not connected
504     // * BAD_VALUE - one of the following has occurred:
505     //             * the api specified does not match the one that was connected
506     //             * api was out of range (see above).
507     // * DEAD_OBJECT - the token is hosted by an already-dead process
508     virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api) = 0;
509 
510     // Attaches a sideband buffer stream to the IGraphicBufferProducer.
511     //
512     // A sideband stream is a device-specific mechanism for passing buffers
513     // from the producer to the consumer without using dequeueBuffer/
514     // queueBuffer. If a sideband stream is present, the consumer can choose
515     // whether to acquire buffers from the sideband stream or from the queued
516     // buffers.
517     //
518     // Passing NULL or a different stream handle will detach the previous
519     // handle if any.
520     virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0;
521 
522     // Allocates buffers based on the given dimensions/format.
523     //
524     // This function will allocate up to the maximum number of buffers
525     // permitted by the current BufferQueue configuration. It will use the
526     // given format, dimensions, and usage bits, which are interpreted in the
527     // same way as for dequeueBuffer, and the async flag must be set the same
528     // way as for dequeueBuffer to ensure that the correct number of buffers are
529     // allocated. This is most useful to avoid an allocation delay during
530     // dequeueBuffer. If there are already the maximum number of buffers
531     // allocated, this function has no effect.
532     virtual void allocateBuffers(uint32_t width, uint32_t height,
533             PixelFormat format, uint64_t usage) = 0;
534 
535     // Sets whether dequeueBuffer is allowed to allocate new buffers.
536     //
537     // Normally dequeueBuffer does not discriminate between free slots which
538     // already have an allocated buffer and those which do not, and will
539     // allocate a new buffer if the slot doesn't have a buffer or if the slot's
540     // buffer doesn't match the requested size, format, or usage. This method
541     // allows the producer to restrict the eligible slots to those which already
542     // have an allocated buffer of the correct size, format, and usage. If no
543     // eligible slot is available, dequeueBuffer will block or return an error
544     // as usual.
545     virtual status_t allowAllocation(bool allow) = 0;
546 
547     // Sets the current generation number of the BufferQueue.
548     //
549     // This generation number will be inserted into any buffers allocated by the
550     // BufferQueue, and any attempts to attach a buffer with a different
551     // generation number will fail. Buffers already in the queue are not
552     // affected and will retain their current generation number. The generation
553     // number defaults to 0.
554     virtual status_t setGenerationNumber(uint32_t generationNumber) = 0;
555 
556     // Returns the name of the connected consumer.
557     virtual String8 getConsumerName() const = 0;
558 
559     // Used to enable/disable shared buffer mode.
560     //
561     // When shared buffer mode is enabled the first buffer that is queued or
562     // dequeued will be cached and returned to all subsequent calls to
563     // dequeueBuffer and acquireBuffer. This allows the producer and consumer to
564     // simultaneously access the same buffer.
565     virtual status_t setSharedBufferMode(bool sharedBufferMode) = 0;
566 
567     // Used to enable/disable auto-refresh.
568     //
569     // Auto refresh has no effect outside of shared buffer mode. In shared
570     // buffer mode, when enabled, it indicates to the consumer that it should
571     // attempt to acquire buffers even if it is not aware of any being
572     // available.
573     virtual status_t setAutoRefresh(bool autoRefresh) = 0;
574 
575     // Sets how long dequeueBuffer will wait for a buffer to become available
576     // before returning an error (TIMED_OUT).
577     //
578     // This timeout also affects the attachBuffer call, which will block if
579     // there is not a free slot available into which the attached buffer can be
580     // placed.
581     //
582     // By default, the BufferQueue will wait forever, which is indicated by a
583     // timeout of -1. If set (to a value other than -1), this will disable
584     // non-blocking mode and its corresponding spare buffer (which is used to
585     // ensure a buffer is always available).
586     //
587     // Return of a value other than NO_ERROR means an error has occurred:
588     // * BAD_VALUE - Failure to adjust the number of available slots. This can
589     //               happen because of trying to allocate/deallocate the async
590     //               buffer.
591     virtual status_t setDequeueTimeout(nsecs_t timeout) = 0;
592 
593     // Returns the last queued buffer along with a fence which must signal
594     // before the contents of the buffer are read. If there are no buffers in
595     // the queue, outBuffer will be populated with nullptr and outFence will be
596     // populated with Fence::NO_FENCE
597     //
598     // outTransformMatrix is not modified if outBuffer is null.
599     //
600     // Returns NO_ERROR or the status of the Binder transaction
601     virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
602             sp<Fence>* outFence, float outTransformMatrix[16]) = 0;
603 
604     // Gets the frame events that haven't already been retrieved.
getFrameTimestamps(FrameEventHistoryDelta *)605     virtual void getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) {}
606 
607     // Returns a unique id for this BufferQueue
608     virtual status_t getUniqueId(uint64_t* outId) const = 0;
609 
610     // Returns the consumer usage flags for this BufferQueue. This returns the
611     // full 64-bit usage flags, rather than the truncated 32-bit usage flags
612     // returned by querying the now deprecated
613     // NATIVE_WINDOW_CONSUMER_USAGE_BITS attribute.
614     virtual status_t getConsumerUsage(uint64_t* outUsage) const = 0;
615 
616     // Static method exports any IGraphicBufferProducer object to a parcel. It
617     // handles null producer as well.
618     static status_t exportToParcel(const sp<IGraphicBufferProducer>& producer,
619                                    Parcel* parcel);
620 
621     // Factory method that creates a new IBGP instance from the parcel.
622     static sp<IGraphicBufferProducer> createFromParcel(const Parcel* parcel);
623 
624 protected:
625     // Exports the current producer as a binder parcelable object. Note that the
626     // producer must be disconnected to be exportable. After successful export,
627     // the producer queue can no longer be connected again. Returns NO_ERROR
628     // when the export is successful and writes an implementation defined
629     // parcelable object into the parcel. For traditional Android BufferQueue,
630     // it writes a strong binder object; for BufferHub, it writes a
631     // ProducerQueueParcelable object.
632     virtual status_t exportToParcel(Parcel* parcel);
633 };
634 
635 // ----------------------------------------------------------------------------
636 
637 class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
638 {
639 public:
640     virtual status_t    onTransact( uint32_t code,
641                                     const Parcel& data,
642                                     Parcel* reply,
643                                     uint32_t flags = 0);
644 };
645 
646 // ----------------------------------------------------------------------------
647 }; // namespace android
648 
649 #endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
650