blob: 033c7273c6cf8e021c2f8294df7b116c1b870236 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
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
Andy McFadden2adaf042012-12-18 09:49:45 -080017#ifndef ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
18#define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
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
Jesse Hallf7857542012-06-14 15:26:33 -070028#include <ui/Fence.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029#include <ui/GraphicBuffer.h>
30#include <ui/Rect.h>
31
32namespace android {
33// ----------------------------------------------------------------------------
34
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035class Surface;
Mathias Agopianeafabcd2011-04-20 14:20:59 -070036
Andy McFadden0273adb2012-12-12 17:10:08 -080037/*
Andy McFadden466a1922013-01-08 11:25:51 -080038 * This class defines the Binder IPC interface for the producer side of
39 * a queue of graphics buffers. It's used to send graphics data from one
40 * component to another. For example, a class that decodes video for
41 * playback might use this to provide frames. This is typically done
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042 * indirectly, through Surface.
Andy McFadden0273adb2012-12-12 17:10:08 -080043 *
Andy McFadden466a1922013-01-08 11:25:51 -080044 * The underlying mechanism is a BufferQueue, which implements
45 * BnGraphicBufferProducer. In normal operation, the producer calls
46 * dequeueBuffer() to get an empty buffer, fills it with data, then
47 * calls queueBuffer() to make it available to the consumer.
Andy McFadden0273adb2012-12-12 17:10:08 -080048 *
Andy McFadden2adaf042012-12-18 09:49:45 -080049 * This class was previously called ISurfaceTexture.
Andy McFadden0273adb2012-12-12 17:10:08 -080050 */
Andy McFadden2adaf042012-12-18 09:49:45 -080051class IGraphicBufferProducer : public IInterface
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080052{
53public:
Andy McFadden2adaf042012-12-18 09:49:45 -080054 DECLARE_META_INTERFACE(GraphicBufferProducer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055
Mathias Agopian80727112011-05-02 19:51:12 -070056 enum {
57 BUFFER_NEEDS_REALLOCATION = 0x1,
58 RELEASE_ALL_BUFFERS = 0x2,
59 };
Mathias Agopiana5c75c02011-03-31 19:10:24 -070060
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061 // requestBuffer requests a new buffer for the given index. The server (i.e.
Andy McFadden2adaf042012-12-18 09:49:45 -080062 // the IGraphicBufferProducer implementation) assigns the newly created
63 // buffer to the given slot index, and the client is expected to mirror the
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080064 // slot->buffer mapping so that it's not necessary to transfer a
65 // GraphicBuffer for every dequeue operation.
Jamie Gennis7b305ff2011-07-19 12:08:33 -070066 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080067
68 // setBufferCount sets the number of buffer slots available. Calling this
69 // will also cause all buffer slots to be emptied. The caller should empty
70 // its mirrored copy of the buffer slots when calling this method.
71 virtual status_t setBufferCount(int bufferCount) = 0;
72
73 // dequeueBuffer requests a new buffer slot for the client to use. Ownership
74 // of the slot is transfered to the client, meaning that the server will not
75 // use the contents of the buffer associated with that slot. The slot index
76 // returned may or may not contain a buffer. If the slot is empty the client
77 // should call requestBuffer to assign a new buffer to that slot. The client
78 // is expected to either call cancelBuffer on the dequeued slot or to fill
79 // in the contents of its associated buffer contents and call queueBuffer.
Mathias Agopiana5c75c02011-03-31 19:10:24 -070080 // If dequeueBuffer return BUFFER_NEEDS_REALLOCATION, the client is
81 // expected to call requestBuffer immediately.
Jesse Hallf7857542012-06-14 15:26:33 -070082 //
83 // The fence parameter will be updated to hold the fence associated with
84 // the buffer. The contents of the buffer must not be overwritten until the
85 // fence signals. If the fence is NULL, the buffer may be written
86 // immediately.
Mathias Agopian7cdd7862013-07-18 22:10:56 -070087 //
88 // The async parameter sets whether we're in asynchrnous mode for this
89 // deququeBuffer() call.
90 virtual status_t dequeueBuffer(int *slot, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -070091 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080092
93 // queueBuffer indicates that the client has finished filling in the
94 // contents of the buffer associated with slot and transfers ownership of
95 // that slot back to the server. It is not valid to call queueBuffer on a
96 // slot that is not owned by the client or one for which a buffer associated
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080097 // via requestBuffer. In addition, a timestamp must be provided by the
98 // client for this buffer. The timestamp is measured in nanoseconds, and
99 // must be monotonically increasing. Its other properties (zero point, etc)
100 // are client-dependent, and should be documented by the client.
Mathias Agopian97c602c2011-07-19 15:24:46 -0700101 //
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700102 // The async parameter sets whether we're queuing a buffer in asynchronous mode.
103 //
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700104 // outWidth, outHeight and outTransform are filled with the default width
105 // and height of the window and current transform applied to buffers,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700106 // respectively.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700107
Mathias Agopiane1424282013-07-29 21:24:40 -0700108 struct QueueBufferInput : public Flattenable<QueueBufferInput> {
109 friend class Flattenable<QueueBufferInput>;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700110 inline QueueBufferInput(const Parcel& parcel);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700111 inline QueueBufferInput(int64_t timestamp,
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700112 const Rect& crop, int scalingMode, uint32_t transform, bool async,
113 const sp<Fence>& fence)
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700114 : timestamp(timestamp), crop(crop), scalingMode(scalingMode),
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700115 transform(transform), async(async), fence(fence) { }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700116 inline void deflate(int64_t* outTimestamp, Rect* outCrop,
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700117 int* outScalingMode, uint32_t* outTransform, bool* outAsync,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700118 sp<Fence>* outFence) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700119 *outTimestamp = timestamp;
120 *outCrop = crop;
121 *outScalingMode = scalingMode;
122 *outTransform = transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700123 *outAsync = bool(async);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700124 *outFence = fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700125 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700126
Mathias Agopiane1424282013-07-29 21:24:40 -0700127 // Flattenable protocol
128 size_t getFlattenedSize() const;
129 size_t getFdCount() const;
130 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
131 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700132
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700133 private:
134 int64_t timestamp;
135 Rect crop;
136 int scalingMode;
137 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700138 int async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700139 sp<Fence> fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700140 };
141
142 // QueueBufferOutput must be a POD structure
143 struct QueueBufferOutput {
144 inline QueueBufferOutput() { }
145 inline void deflate(uint32_t* outWidth,
Mathias Agopian2488b202012-04-20 17:19:28 -0700146 uint32_t* outHeight,
147 uint32_t* outTransformHint,
148 uint32_t* outNumPendingBuffers) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700149 *outWidth = width;
150 *outHeight = height;
151 *outTransformHint = transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700152 *outNumPendingBuffers = numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700153 }
154 inline void inflate(uint32_t inWidth, uint32_t inHeight,
Mathias Agopian2488b202012-04-20 17:19:28 -0700155 uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700156 width = inWidth;
157 height = inHeight;
158 transformHint = inTransformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700159 numPendingBuffers = inNumPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700160 }
161 private:
162 uint32_t width;
163 uint32_t height;
164 uint32_t transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700165 uint32_t numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700166 };
167
168 virtual status_t queueBuffer(int slot,
169 const QueueBufferInput& input, QueueBufferOutput* output) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800170
171 // cancelBuffer indicates that the client does not wish to fill in the
172 // buffer associated with slot and transfers ownership of the slot back to
173 // the server.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700174 virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700176 // query retrieves some information for this surface
177 // 'what' tokens allowed are that of android_natives.h
178 virtual int query(int what, int* value) = 0;
Mathias Agopian80727112011-05-02 19:51:12 -0700179
Andy McFadden2adaf042012-12-18 09:49:45 -0800180 // connect attempts to connect a client API to the IGraphicBufferProducer.
181 // This must be called before any other IGraphicBufferProducer methods are
182 // called except for getAllocator.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700183 //
184 // This method will fail if the connect was previously called on the
Andy McFadden2adaf042012-12-18 09:49:45 -0800185 // IGraphicBufferProducer and no corresponding disconnect call was made.
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700186 //
187 // outWidth, outHeight and outTransform are filled with the default width
188 // and height of the window and current transform applied to buffers,
189 // respectively.
Mathias Agopian595264f2013-07-16 22:56:09 -0700190 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700191
Andy McFadden2adaf042012-12-18 09:49:45 -0800192 // disconnect attempts to disconnect a client API from the
193 // IGraphicBufferProducer. Calling this method will cause any subsequent
194 // calls to other IGraphicBufferProducer methods to fail except for
195 // getAllocator and connect. Successfully calling connect after this will
196 // allow the other methods to succeed again.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700197 //
Andy McFadden2adaf042012-12-18 09:49:45 -0800198 // This method will fail if the the IGraphicBufferProducer is not currently
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700199 // connected to the specified client API.
200 virtual status_t disconnect(int api) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201};
202
203// ----------------------------------------------------------------------------
204
Andy McFadden2adaf042012-12-18 09:49:45 -0800205class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206{
207public:
208 virtual status_t onTransact( uint32_t code,
209 const Parcel& data,
210 Parcel* reply,
211 uint32_t flags = 0);
212};
213
214// ----------------------------------------------------------------------------
215}; // namespace android
216
Andy McFadden2adaf042012-12-18 09:49:45 -0800217#endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H