blob: bd838bc95abd1aa0aed4f3823e122ff50ad119c8 [file] [log] [blame]
Mathias Agopiana4e19522013-07-31 20:09:53 -07001/*
2 * Copyright (C) 2013 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
Dan Stoza1d0359c2017-04-05 16:23:19 -070017#pragma once
Mathias Agopiana9347642017-02-13 16:42:28 -080018
Dan Stozae77c7662016-05-13 11:37:28 -070019#include <gui/OccupancyTracker.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070020
Dan Stoza1d0359c2017-04-05 16:23:19 -070021#include <binder/IInterface.h>
22
Dan Stoza99b18b42014-03-28 15:34:33 -070023#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
Dan Stoza1d0359c2017-04-05 16:23:19 -070026#include <ui/PixelFormat.h>
27
28#include <utils/Errors.h>
29
Mathias Agopiana4e19522013-07-31 20:09:53 -070030namespace android {
Mathias Agopiana4e19522013-07-31 20:09:53 -070031
Dan Stozade7100a2015-03-11 16:38:47 -070032class BufferItem;
Mathias Agopiana4e19522013-07-31 20:09:53 -070033class Fence;
Jesse Hall399184a2014-03-03 15:42:54 -080034class GraphicBuffer;
35class IConsumerListener;
36class NativeHandle;
Mathias Agopiana4e19522013-07-31 20:09:53 -070037
38class IGraphicBufferConsumer : public IInterface {
Mathias Agopiana4e19522013-07-31 20:09:53 -070039public:
Dan Stoza1d0359c2017-04-05 16:23:19 -070040 DECLARE_META_INTERFACE(GraphicBufferConsumer)
41
Igor Murashkin7d2d1602013-11-12 18:02:20 -080042 enum {
Dan Stoza1d0359c2017-04-05 16:23:19 -070043 // Returned by releaseBuffer, after which the consumer must free any references to the
44 // just-released buffer that it might have.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080045 STALE_BUFFER_SLOT = 1,
46 // Returned by dequeueBuffer if there are no pending buffers available.
47 NO_BUFFER_AVAILABLE,
48 // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
49 PRESENT_LATER,
50 };
Mathias Agopiana4e19522013-07-31 20:09:53 -070051
Dan Stoza1d0359c2017-04-05 16:23:19 -070052 // acquireBuffer attempts to acquire ownership of the next pending buffer in the BufferQueue.
53 // If no buffer is pending then it returns NO_BUFFER_AVAILABLE. If a buffer is successfully
54 // acquired, the information about the buffer is returned in BufferItem.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080055 //
Dan Stoza1d0359c2017-04-05 16:23:19 -070056 // If the buffer returned had previously been acquired then the BufferItem::mGraphicBuffer field
57 // of buffer is set to NULL and it is assumed that the consumer still holds a reference to the
Mathias Agopiana4e19522013-07-31 20:09:53 -070058 // buffer.
59 //
Dan Stoza1d0359c2017-04-05 16:23:19 -070060 // If presentWhen is non-zero, it indicates the time when the buffer will be displayed on
61 // screen. If the buffer's timestamp is farther in the future, the buffer won't be acquired, and
62 // PRESENT_LATER will be returned. The presentation time is in nanoseconds, and the time base
Mathias Agopiana4e19522013-07-31 20:09:53 -070063 // is CLOCK_MONOTONIC.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080064 //
Dan Stoza1d0359c2017-04-05 16:23:19 -070065 // If maxFrameNumber is non-zero, it indicates that acquireBuffer should only return a buffer
66 // with a frame number less than or equal to maxFrameNumber. If no such frame is available
67 // (such as when a buffer has been replaced but the consumer has not received the
68 // onFrameReplaced callback), then PRESENT_LATER will be returned.
Dan Stozaa4650a52015-05-12 12:56:16 -070069 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -080070 // Return of NO_ERROR means the operation completed as normal.
71 //
Dan Stoza1d0359c2017-04-05 16:23:19 -070072 // Return of a positive value means the operation could not be completed at this time, but the
73 // user should try again later:
Igor Murashkin7d2d1602013-11-12 18:02:20 -080074 // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
75 // * PRESENT_LATER - the buffer's timestamp is farther in the future
76 //
77 // Return of a negative value means an error has occurred:
78 // * INVALID_OPERATION - too many buffers have been acquired
Dan Stozaa4650a52015-05-12 12:56:16 -070079 virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
Dan Stoza1d0359c2017-04-05 16:23:19 -070080 uint64_t maxFrameNumber = 0) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -070081
Dan Stoza1d0359c2017-04-05 16:23:19 -070082 // detachBuffer attempts to remove all ownership of the buffer in the given slot from the buffer
83 // queue. If this call succeeds, the slot will be freed, and there will be no way to obtain the
84 // buffer from this interface. The freed slot will remain unallocated until either it is
85 // selected to hold a freshly allocated buffer in dequeueBuffer or a buffer is attached to the
86 // slot. The buffer must have already been acquired.
Dan Stoza9f3053d2014-03-06 15:14:33 -080087 //
88 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -070089 // * BAD_VALUE - the given slot number is invalid, either because it is out of the range
90 // [0, NUM_BUFFER_SLOTS) or because the slot it refers to is not
91 // currently acquired.
Dan Stoza9f3053d2014-03-06 15:14:33 -080092 virtual status_t detachBuffer(int slot) = 0;
93
Dan Stoza1d0359c2017-04-05 16:23:19 -070094 // attachBuffer attempts to transfer ownership of a buffer to the BufferQueue. If this call
95 // succeeds, it will be as if this buffer was acquired from the returned slot number. As such,
96 // this call will fail if attaching this buffer would cause too many buffers to be
97 // simultaneously acquired.
Dan Stoza9f3053d2014-03-06 15:14:33 -080098 //
Dan Stoza1d0359c2017-04-05 16:23:19 -070099 // If the buffer is successfully attached, its frameNumber is initialized to 0. This must be
100 // passed into the releaseBuffer call or else the buffer will be deallocated as stale.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800101 //
102 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700103 // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of the buffer did not
104 // match the BufferQueue.
105 // * INVALID_OPERATION - cannot attach the buffer because it would cause too many buffers
106 // to be acquired.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800107 // * NO_MEMORY - no free slots available
Dan Stoza1d0359c2017-04-05 16:23:19 -0700108 virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer) = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800109
Dan Stoza1d0359c2017-04-05 16:23:19 -0700110 // releaseBuffer releases a buffer slot from the consumer back to the BufferQueue. This may be
111 // done while the buffer's contents are still being accessed. The fence will signal when the
112 // buffer is no longer in use. frameNumber is used to identify the exact buffer returned.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700113 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700114 // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free any references to the
115 // just-released buffer that it might have, as if it had received a onBuffersReleased() call
116 // with a mask set for the released buffer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700117 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700118 // Note that the dependencies on EGL will be removed once we switch to using the Android HW
119 // Sync HAL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800120 //
121 // Return of NO_ERROR means the operation completed as normal.
122 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700123 // Return of a positive value means the operation could not be completed at this time, but the
124 // user should try again later:
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800125 // * STALE_BUFFER_SLOT - see above (second paragraph)
126 //
127 // Return of a negative value means an error has occurred:
128 // * BAD_VALUE - one of the following could've happened:
129 // * the buffer slot was invalid
130 // * the fence was NULL
131 // * the buffer slot specified is not in the acquired state
Dan Stoza1d0359c2017-04-05 16:23:19 -0700132 virtual status_t releaseBuffer(int buf, uint64_t frameNumber, EGLDisplay display,
133 EGLSyncKHR fence, const sp<Fence>& releaseFence) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700134
Dan Stoza1d0359c2017-04-05 16:23:19 -0700135 // consumerConnect connects a consumer to the BufferQueue. Only one consumer may be connected,
136 // and when that consumer disconnects the BufferQueue is placed into the "abandoned" state,
137 // causing most interactions with the BufferQueue by the producer to fail. controlledByApp
138 // indicates whether the consumer is controlled by the application.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700139 //
140 // consumer may not be NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800141 //
142 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700143 // * NO_INIT - the BufferQueue has been abandoned
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800144 // * BAD_VALUE - a NULL consumer was provided
Dan Stoza1d0359c2017-04-05 16:23:19 -0700145 virtual status_t consumerConnect(const sp<IConsumerListener>& consumer,
146 bool controlledByApp) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700147
Dan Stoza1d0359c2017-04-05 16:23:19 -0700148 // consumerDisconnect disconnects a consumer from the BufferQueue. All buffers will be freed and
149 // the BufferQueue is placed in the "abandoned" state, causing most interactions with the
150 // BufferQueue by the producer to fail.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800151 //
152 // Return of a value other than NO_ERROR means an error has occurred:
153 // * BAD_VALUE - no consumer is currently connected
Mathias Agopiana4e19522013-07-31 20:09:53 -0700154 virtual status_t consumerDisconnect() = 0;
155
Dan Stoza1d0359c2017-04-05 16:23:19 -0700156 // getReleasedBuffers sets the value pointed to by slotMask to a bit set. Each bit index with a
157 // 1 corresponds to a released buffer slot with that index value. In particular, a released
158 // buffer is one that has been released by the BufferQueue but has not yet been released by
159 // the consumer.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700160 //
161 // This should be called from the onBuffersReleased() callback.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800162 //
163 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700164 // * NO_INIT - the BufferQueue has been abandoned.
Dan Stozafebd4f42014-04-09 16:14:51 -0700165 virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700166
Dan Stoza1d0359c2017-04-05 16:23:19 -0700167 // setDefaultBufferSize is used to set the size of buffers returned by dequeueBuffer when a
168 // width and height of zero is requested. Default is 1x1.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800169 //
170 // Return of a value other than NO_ERROR means an error has occurred:
171 // * BAD_VALUE - either w or h was zero
Mathias Agopiana4e19522013-07-31 20:09:53 -0700172 virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
173
Dan Stoza1d0359c2017-04-05 16:23:19 -0700174 // setMaxBufferCount sets the maximum value for the number of buffers used in the BufferQueue
175 // (the initial default is NUM_BUFFER_SLOTS). If a call to setMaxAcquiredBufferCount (by the
176 // consumer), or a call to setAsyncMode or setMaxDequeuedBufferCount (by the producer), would
177 // cause this value to be exceeded then that call will fail. This call will fail if a producer
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700178 // is connected to the BufferQueue.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700179 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700180 // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count cannot be less than
181 // maxAcquiredBufferCount.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800182 //
183 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800184 // * BAD_VALUE - one of the below conditions occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700185 // * bufferCount was out of range (see above).
186 // * failure to adjust the number of available slots.
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700187 // * INVALID_OPERATION - attempting to call this after a producer connected.
188 virtual status_t setMaxBufferCount(int bufferCount) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700189
Dan Stoza1d0359c2017-04-05 16:23:19 -0700190 // setMaxAcquiredBufferCount sets the maximum number of buffers that can be acquired by the
191 // consumer at one time (default 1). If this method succeeds, any new buffer slots will be both
192 // unallocated and owned by the BufferQueue object (i.e. they are not owned by the producer or
193 // consumer). Calling this may also cause some buffer slots to be emptied.
Pablo Ceballos72daab62015-12-07 16:38:43 -0800194 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700195 // This function should not be called with a value of maxAcquiredBuffers that is less than the
196 // number of currently acquired buffer slots. Doing so will result in a BAD_VALUE error.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800197 //
Dan Stoza1d0359c2017-04-05 16:23:19 -0700198 // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS. It also cannot
199 // cause the maxBufferCount value to be exceeded.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800200 //
201 // Return of a value other than NO_ERROR means an error has occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700202 // * NO_INIT - the BufferQueue has been abandoned
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800203 // * BAD_VALUE - one of the below conditions occurred:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700204 // * maxAcquiredBuffers was out of range (see above).
205 // * failure to adjust the number of available slots.
206 // * client would have more than the requested number of acquired buffers after
207 // this call
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800208 // * INVALID_OPERATION - attempting to call this after a producer connected.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700209 virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
210
211 // setConsumerName sets the name used in logging
212 virtual void setConsumerName(const String8& name) = 0;
213
Dan Stoza1d0359c2017-04-05 16:23:19 -0700214 // setDefaultBufferFormat allows the BufferQueue to create GraphicBuffers of a defaultFormat if
215 // no format is specified in dequeueBuffer. The initial default is PIXEL_FORMAT_RGBA_8888.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800216 //
217 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stozad723bd72014-11-18 10:24:03 -0800218 virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700219
Dan Stoza1d0359c2017-04-05 16:23:19 -0700220 // setDefaultBufferDataSpace is a request to the producer to provide buffers of the indicated
221 // dataSpace. The producer may ignore this request. The initial default is
222 // HAL_DATASPACE_UNKNOWN.
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800223 //
224 // Return of a value other than NO_ERROR means an unknown error has occurred.
Dan Stoza1d0359c2017-04-05 16:23:19 -0700225 virtual status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace) = 0;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800226
Dan Stoza1d0359c2017-04-05 16:23:19 -0700227 // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer. These are merged
228 // with the bits passed to dequeueBuffer. The values are enumerated in gralloc.h,
229 // e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800230 //
231 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700232 virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
233
Dan Stoza1d0359c2017-04-05 16:23:19 -0700234 // setTransformHint bakes in rotation to buffers so overlays can be used. The values are
235 // enumerated in window.h, e.g. NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0
236 // (no transform).
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800237 //
238 // Return of a value other than NO_ERROR means an unknown error has occurred.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700239 virtual status_t setTransformHint(uint32_t hint) = 0;
240
Jesse Hall399184a2014-03-03 15:42:54 -0800241 // Retrieve the sideband buffer stream, if any.
242 virtual sp<NativeHandle> getSidebandStream() const = 0;
243
Dan Stoza1d0359c2017-04-05 16:23:19 -0700244 // Retrieves any stored segments of the occupancy history of this BufferQueue and clears them.
245 // Optionally closes out the pending segment if forceFlush is true.
Dan Stozae77c7662016-05-13 11:37:28 -0700246 virtual status_t getOccupancyHistory(bool forceFlush,
Dan Stoza1d0359c2017-04-05 16:23:19 -0700247 std::vector<OccupancyTracker::Segment>* outHistory) = 0;
Dan Stozae77c7662016-05-13 11:37:28 -0700248
Dan Stoza1d0359c2017-04-05 16:23:19 -0700249 // discardFreeBuffers releases all currently-free buffers held by the BufferQueue, in order to
250 // reduce the memory consumption of the BufferQueue to the minimum possible without
251 // discarding data.
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700252 virtual status_t discardFreeBuffers() = 0;
253
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700254 // dump state into a string
Colin Cross3d1d2802016-09-26 18:10:16 -0700255 virtual void dumpState(String8& result, const char* prefix) const = 0;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700256};
257
Dan Stoza1d0359c2017-04-05 16:23:19 -0700258class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer> {
Mathias Agopiana4e19522013-07-31 20:09:53 -0700259public:
Dan Stoza1d0359c2017-04-05 16:23:19 -0700260 status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
261 uint32_t flags = 0) override;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700262};
263
Dan Stoza1d0359c2017-04-05 16:23:19 -0700264} // namespace android