1 /*
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 
17 #ifndef NATIVEWINDOW_IGONKGRAPHICBUFFERCONSUMER_LL_H
18 #define NATIVEWINDOW_IGONKGRAPHICBUFFERCONSUMER_LL_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <utils/Errors.h>
24 #include <utils/RefBase.h>
25 #include <utils/Timers.h>
26 
27 #include <binder/IInterface.h>
28 #include <ui/Rect.h>
29 
30 #include "mozilla/RefPtr.h"
31 
32 class ANativeWindowBuffer;
33 
34 namespace mozilla {
35 namespace layers {
36 class TextureClient;
37 }
38 }
39 
40 namespace android {
41 // ----------------------------------------------------------------------------
42 
43 class Fence;
44 class GraphicBuffer;
45 class IConsumerListener;
46 class NativeHandle;
47 
48 class IGonkGraphicBufferConsumer : public IInterface {
49 public:
50 
51     // public facing structure for BufferSlot
52     class BufferItem : public Flattenable<BufferItem> {
53         friend class Flattenable<BufferItem>;
54         size_t getPodSize() const;
55         size_t getFlattenedSize() const;
56         size_t getFdCount() const;
57         status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
58         status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
59 
60     public:
61         // The default value of mBuf, used to indicate this doesn't correspond to a slot.
62         enum { INVALID_BUFFER_SLOT = -1 };
63         BufferItem();
64 
65         // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
66         // if the buffer in this slot has been acquired in the past (see
67         // BufferSlot.mAcquireCalled).
68         sp<GraphicBuffer> mGraphicBuffer;
69 
70         // mFence is a fence that will signal when the buffer is idle.
71         sp<Fence> mFence;
72 
73         // mCrop is the current crop rectangle for this buffer slot.
74         Rect mCrop;
75 
76         // mTransform is the current transform flags for this buffer slot.
77         // refer to NATIVE_WINDOW_TRANSFORM_* in <window.h>
78         uint32_t mTransform;
79 
80         // mScalingMode is the current scaling mode for this buffer slot.
81         // refer to NATIVE_WINDOW_SCALING_* in <window.h>
82         uint32_t mScalingMode;
83 
84         // mTimestamp is the current timestamp for this buffer slot. This gets
85         // to set by queueBuffer each time this slot is queued. This value
86         // is guaranteed to be monotonically increasing for each newly
87         // acquired buffer.
88         int64_t mTimestamp;
89 
90         // mIsAutoTimestamp indicates whether mTimestamp was generated
91         // automatically when the buffer was queued.
92         bool mIsAutoTimestamp;
93 
94         // mFrameNumber is the number of the queued frame for this slot.
95         uint64_t mFrameNumber;
96 
97         // mBuf is the slot index of this buffer (default INVALID_BUFFER_SLOT).
98         int mBuf;
99 
100         // mIsDroppable whether this buffer was queued with the
101         // property that it can be replaced by a new buffer for the purpose of
102         // making sure dequeueBuffer() won't block.
103         // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer
104         // was queued.
105         bool mIsDroppable;
106 
107         // Indicates whether this buffer has been seen by a consumer yet
108         bool mAcquireCalled;
109 
110         // Indicates this buffer must be transformed by the inverse transform of the screen
111         // it is displayed onto. This is applied after mTransform.
112         bool mTransformToDisplayInverse;
113     };
114 
115     enum {
116         // Returned by releaseBuffer, after which the consumer must
117         // free any references to the just-released buffer that it might have.
118         STALE_BUFFER_SLOT = 1,
119         // Returned by dequeueBuffer if there are no pending buffers available.
120         NO_BUFFER_AVAILABLE,
121         // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
122         PRESENT_LATER,
123     };
124 
125     // acquireBuffer attempts to acquire ownership of the next pending buffer in
126     // the BufferQueue.  If no buffer is pending then it returns
127     // NO_BUFFER_AVAILABLE.  If a buffer is successfully acquired, the
128     // information about the buffer is returned in BufferItem.
129     //
130     // If the buffer returned had previously been
131     // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
132     // NULL and it is assumed that the consumer still holds a reference to the
133     // buffer.
134     //
135     // If presentWhen is non-zero, it indicates the time when the buffer will
136     // be displayed on screen.  If the buffer's timestamp is farther in the
137     // future, the buffer won't be acquired, and PRESENT_LATER will be
138     // returned.  The presentation time is in nanoseconds, and the time base
139     // is CLOCK_MONOTONIC.
140     //
141     // Return of NO_ERROR means the operation completed as normal.
142     //
143     // Return of a positive value means the operation could not be completed
144     //    at this time, but the user should try again later:
145     // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
146     // * PRESENT_LATER - the buffer's timestamp is farther in the future
147     //
148     // Return of a negative value means an error has occurred:
149     // * INVALID_OPERATION - too many buffers have been acquired
150     virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
151 
152     // detachBuffer attempts to remove all ownership of the buffer in the given
153     // slot from the buffer queue. If this call succeeds, the slot will be
154     // freed, and there will be no way to obtain the buffer from this interface.
155     // The freed slot will remain unallocated until either it is selected to
156     // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
157     // to the slot. The buffer must have already been acquired.
158     //
159     // Return of a value other than NO_ERROR means an error has occurred:
160     // * BAD_VALUE - the given slot number is invalid, either because it is
161     //               out of the range [0, NUM_BUFFER_SLOTS) or because the slot
162     //               it refers to is not currently acquired.
163     virtual status_t detachBuffer(int slot) = 0;
164 
165     // attachBuffer attempts to transfer ownership of a buffer to the buffer
166     // queue. If this call succeeds, it will be as if this buffer was acquired
167     // from the returned slot number. As such, this call will fail if attaching
168     // this buffer would cause too many buffers to be simultaneously acquired.
169     //
170     // If the buffer is successfully attached, its frameNumber is initialized
171     // to 0. This must be passed into the releaseBuffer call or else the buffer
172     // will be deallocated as stale.
173     //
174     // Return of a value other than NO_ERROR means an error has occurred:
175     // * BAD_VALUE - outSlot or buffer were NULL
176     // * INVALID_OPERATION - cannot attach the buffer because it would cause too
177     //                       many buffers to be acquired.
178     // * NO_MEMORY - no free slots available
179     virtual status_t attachBuffer(int *outSlot,
180             const sp<GraphicBuffer>& buffer) = 0;
181 
182     // releaseBuffer releases a buffer slot from the consumer back to the
183     // BufferQueue.  This may be done while the buffer's contents are still
184     // being accessed.  The fence will signal when the buffer is no longer
185     // in use. frameNumber is used to indentify the exact buffer returned.
186     //
187     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
188     // any references to the just-released buffer that it might have, as if it
189     // had received a onBuffersReleased() call with a mask set for the released
190     // buffer.
191     //
192     // Note that the dependencies on EGL will be removed once we switch to using
193     // the Android HW Sync HAL.
194     //
195     // Return of NO_ERROR means the operation completed as normal.
196     //
197     // Return of a positive value means the operation could not be completed
198     //    at this time, but the user should try again later:
199     // * STALE_BUFFER_SLOT - see above (second paragraph)
200     //
201     // Return of a negative value means an error has occurred:
202     // * BAD_VALUE - one of the following could've happened:
203     //               * the buffer slot was invalid
204     //               * the fence was NULL
205     //               * the buffer slot specified is not in the acquired state
206     virtual status_t releaseBuffer(int buf, uint64_t frameNumber, const sp<Fence>& releaseFence) = 0;
207 
208     // consumerConnect connects a consumer to the BufferQueue.  Only one
209     // consumer may be connected, and when that consumer disconnects the
210     // BufferQueue is placed into the "abandoned" state, causing most
211     // interactions with the BufferQueue by the producer to fail.
212     // controlledByApp indicates whether the consumer is controlled by
213     // the application.
214     //
215     // consumer may not be NULL.
216     //
217     // Return of a value other than NO_ERROR means an error has occurred:
218     // * NO_INIT - the buffer queue has been abandoned
219     // * BAD_VALUE - a NULL consumer was provided
220     virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
221 
222     // consumerDisconnect disconnects a consumer from the BufferQueue. All
223     // buffers will be freed and the BufferQueue is placed in the "abandoned"
224     // state, causing most interactions with the BufferQueue by the producer to
225     // fail.
226     //
227     // Return of a value other than NO_ERROR means an error has occurred:
228     // * BAD_VALUE - no consumer is currently connected
229     virtual status_t consumerDisconnect() = 0;
230 
231     // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
232     // Each bit index with a 1 corresponds to a released buffer slot with that
233     // index value.  In particular, a released buffer is one that has
234     // been released by the BufferQueue but have not yet been released by the consumer.
235     //
236     // This should be called from the onBuffersReleased() callback.
237     //
238     // Return of a value other than NO_ERROR means an error has occurred:
239     // * NO_INIT - the buffer queue has been abandoned.
240     virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
241 
242     // setDefaultBufferSize is used to set the size of buffers returned by
243     // dequeueBuffer when a width and height of zero is requested.  Default
244     // is 1x1.
245     //
246     // Return of a value other than NO_ERROR means an error has occurred:
247     // * BAD_VALUE - either w or h was zero
248     virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
249 
250     // setDefaultMaxBufferCount sets the default value for the maximum buffer
251     // count (the initial default is 2). If the producer has requested a
252     // buffer count using setBufferCount, the default buffer count will only
253     // take effect if the producer sets the count back to zero.
254     //
255     // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
256     //
257     // Return of a value other than NO_ERROR means an error has occurred:
258     // * BAD_VALUE - bufferCount was out of range (see above).
259     virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
260 
261     // disableAsyncBuffer disables the extra buffer used in async mode
262     // (when both producer and consumer have set their "isControlledByApp"
263     // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
264     //
265     // This can only be called before consumerConnect().
266     //
267     // Return of a value other than NO_ERROR means an error has occurred:
268     // * INVALID_OPERATION - attempting to call this after consumerConnect.
269     virtual status_t disableAsyncBuffer() = 0;
270 
271     // setMaxAcquiredBufferCount sets the maximum number of buffers that can
272     // be acquired by the consumer at one time (default 1).  This call will
273     // fail if a producer is connected to the BufferQueue.
274     //
275     // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
276     //
277     // Return of a value other than NO_ERROR means an error has occurred:
278     // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
279     // * INVALID_OPERATION - attempting to call this after a producer connected.
280     virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
281 
282     // setConsumerName sets the name used in logging
283     virtual void setConsumerName(const String8& name) = 0;
284 
285     // setDefaultBufferFormat allows the BufferQueue to create
286     // GraphicBuffers of a defaultFormat if no format is specified
287     // in dequeueBuffer.  Formats are enumerated in graphics.h; the
288     // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
289     //
290     // Return of a value other than NO_ERROR means an unknown error has occurred.
291     virtual status_t setDefaultBufferFormat(uint32_t defaultFormat) = 0;
292 
293     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
294     // These are merged with the bits passed to dequeueBuffer.  The values are
295     // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
296     //
297     // Return of a value other than NO_ERROR means an unknown error has occurred.
298     virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
299 
300     // setTransformHint bakes in rotation to buffers so overlays can be used.
301     // The values are enumerated in window.h, e.g.
302     // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
303     //
304     // Return of a value other than NO_ERROR means an unknown error has occurred.
305     virtual status_t setTransformHint(uint32_t hint) = 0;
306 
307     // Retrieve the sideband buffer stream, if any.
308     virtual sp<NativeHandle> getSidebandStream() const = 0;
309 
310     // dump state into a string
311     virtual void dumpToString(String8& result, const char* prefix) const = 0;
312 
313     // Added by mozilla
314     virtual already_AddRefed<mozilla::layers::TextureClient>
315         getTextureClientFromBuffer(ANativeWindowBuffer* buffer) = 0;
316 
317     virtual int getSlotFromTextureClientLocked(mozilla::layers::TextureClient* client) const = 0;
318 
319 public:
320     DECLARE_META_INTERFACE(GonkGraphicBufferConsumer);
321 };
322 
323 // ----------------------------------------------------------------------------
324 
325 class BnGonkGraphicBufferConsumer : public BnInterface<IGonkGraphicBufferConsumer>
326 {
327 public:
328     virtual status_t    onTransact( uint32_t code,
329                                     const Parcel& data,
330                                     Parcel* reply,
331                                     uint32_t flags = 0);
332 };
333 
334 // ----------------------------------------------------------------------------
335 }; // namespace android
336 
337 #endif // ANDROID_GUI_IGONKGRAPHICBUFFERCONSUMER_H
338