blob: 58345a028db5732f7ab45179e16740facec19053 [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
17#ifndef ANDROID_GUI_ISURFACETEXTURE_H
18#define ANDROID_GUI_ISURFACETEXTURE_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
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 Agopianeafabcd2011-04-20 14:20:59 -070035class SurfaceTextureClient;
36
Andy McFadden0273adb2012-12-12 17:10:08 -080037/*
38 * This class defines an interface that is implemented by classes that
39 * produce buffers of graphics data. For example, a class that decodes
40 * video for playback might use this to provide frames. This is
41 * typically done indirectly, through SurfaceTextureClient.
42 *
43 * The underlying mechanism is a BufferQueue. In normal operation, the
44 * producer calls dequeueBuffer() to get an empty buffer, fills it with
45 * data, then calls queueBuffer() to make it available to the consumer.
46 *
47 * The BnSurfaceTexture and BpSurfaceTexture classes provide the Binder
48 * IPC implementation.
49 *
50 * TODO: rename to IGraphicBufferProducer (IBufferProducer?
51 * IBufferQueueProducer?)
52 */
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080053class ISurfaceTexture : public IInterface
54{
55public:
56 DECLARE_META_INTERFACE(SurfaceTexture);
57
Mathias Agopian80727112011-05-02 19:51:12 -070058 enum {
59 BUFFER_NEEDS_REALLOCATION = 0x1,
60 RELEASE_ALL_BUFFERS = 0x2,
61 };
Mathias Agopiana5c75c02011-03-31 19:10:24 -070062
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080063 // requestBuffer requests a new buffer for the given index. The server (i.e.
64 // the ISurfaceTexture implementation) assigns the newly created buffer to
65 // the given slot index, and the client is expected to mirror the
66 // slot->buffer mapping so that it's not necessary to transfer a
67 // GraphicBuffer for every dequeue operation.
Jamie Gennis7b305ff2011-07-19 12:08:33 -070068 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069
70 // setBufferCount sets the number of buffer slots available. Calling this
71 // will also cause all buffer slots to be emptied. The caller should empty
72 // its mirrored copy of the buffer slots when calling this method.
73 virtual status_t setBufferCount(int bufferCount) = 0;
74
75 // dequeueBuffer requests a new buffer slot for the client to use. Ownership
76 // of the slot is transfered to the client, meaning that the server will not
77 // use the contents of the buffer associated with that slot. The slot index
78 // returned may or may not contain a buffer. If the slot is empty the client
79 // should call requestBuffer to assign a new buffer to that slot. The client
80 // is expected to either call cancelBuffer on the dequeued slot or to fill
81 // in the contents of its associated buffer contents and call queueBuffer.
Mathias Agopiana5c75c02011-03-31 19:10:24 -070082 // If dequeueBuffer return BUFFER_NEEDS_REALLOCATION, the client is
83 // expected to call requestBuffer immediately.
Jesse Hallf7857542012-06-14 15:26:33 -070084 //
85 // The fence parameter will be updated to hold the fence associated with
86 // the buffer. The contents of the buffer must not be overwritten until the
87 // fence signals. If the fence is NULL, the buffer may be written
88 // immediately.
89 virtual status_t dequeueBuffer(int *slot, sp<Fence>& fence,
90 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080091
92 // queueBuffer indicates that the client has finished filling in the
93 // contents of the buffer associated with slot and transfers ownership of
94 // that slot back to the server. It is not valid to call queueBuffer on a
95 // slot that is not owned by the client or one for which a buffer associated
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080096 // via requestBuffer. In addition, a timestamp must be provided by the
97 // client for this buffer. The timestamp is measured in nanoseconds, and
98 // must be monotonically increasing. Its other properties (zero point, etc)
99 // are client-dependent, and should be documented by the client.
Mathias Agopian97c602c2011-07-19 15:24:46 -0700100 //
Mathias Agopianc10d9d92011-07-20 16:46:11 -0700101 // outWidth, outHeight and outTransform are filled with the default width
102 // and height of the window and current transform applied to buffers,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700103 // respectively.
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700104
Jesse Hallc777b0b2012-06-28 12:52:05 -0700105 struct QueueBufferInput : public Flattenable {
106 inline QueueBufferInput(const Parcel& parcel);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700107 inline QueueBufferInput(int64_t timestamp,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700108 const Rect& crop, int scalingMode, uint32_t transform,
109 sp<Fence> fence)
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700110 : timestamp(timestamp), crop(crop), scalingMode(scalingMode),
Jesse Hallc777b0b2012-06-28 12:52:05 -0700111 transform(transform), fence(fence) { }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700112 inline void deflate(int64_t* outTimestamp, Rect* outCrop,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700113 int* outScalingMode, uint32_t* outTransform,
114 sp<Fence>* outFence) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700115 *outTimestamp = timestamp;
116 *outCrop = crop;
117 *outScalingMode = scalingMode;
118 *outTransform = transform;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700119 *outFence = fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700120 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700121
122 // Flattenable interface
123 virtual size_t getFlattenedSize() const;
124 virtual size_t getFdCount() const;
125 virtual status_t flatten(void* buffer, size_t size,
126 int fds[], size_t count) const;
127 virtual status_t unflatten(void const* buffer, size_t size,
128 int fds[], size_t count);
129
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700130 private:
131 int64_t timestamp;
132 Rect crop;
133 int scalingMode;
134 uint32_t transform;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700135 sp<Fence> fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700136 };
137
138 // QueueBufferOutput must be a POD structure
139 struct QueueBufferOutput {
140 inline QueueBufferOutput() { }
141 inline void deflate(uint32_t* outWidth,
Mathias Agopian2488b202012-04-20 17:19:28 -0700142 uint32_t* outHeight,
143 uint32_t* outTransformHint,
144 uint32_t* outNumPendingBuffers) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700145 *outWidth = width;
146 *outHeight = height;
147 *outTransformHint = transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700148 *outNumPendingBuffers = numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700149 }
150 inline void inflate(uint32_t inWidth, uint32_t inHeight,
Mathias Agopian2488b202012-04-20 17:19:28 -0700151 uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700152 width = inWidth;
153 height = inHeight;
154 transformHint = inTransformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700155 numPendingBuffers = inNumPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700156 }
157 private:
158 uint32_t width;
159 uint32_t height;
160 uint32_t transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700161 uint32_t numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700162 };
163
164 virtual status_t queueBuffer(int slot,
165 const QueueBufferInput& input, QueueBufferOutput* output) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166
167 // cancelBuffer indicates that the client does not wish to fill in the
168 // buffer associated with slot and transfers ownership of the slot back to
169 // the server.
Jesse Hallc777b0b2012-06-28 12:52:05 -0700170 virtual void cancelBuffer(int slot, sp<Fence> fence) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800171
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700172 // query retrieves some information for this surface
173 // 'what' tokens allowed are that of android_natives.h
174 virtual int query(int what, int* value) = 0;
Mathias Agopian80727112011-05-02 19:51:12 -0700175
176 // setSynchronousMode set whether dequeueBuffer is synchronous or
177 // asynchronous. In synchronous mode, dequeueBuffer blocks until
178 // a buffer is available, the currently bound buffer can be dequeued and
179 // queued buffers will be retired in order.
180 // The default mode is asynchronous.
181 virtual status_t setSynchronousMode(bool enabled) = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700182
183 // connect attempts to connect a client API to the SurfaceTexture. This
184 // must be called before any other ISurfaceTexture methods are called except
185 // for getAllocator.
186 //
187 // This method will fail if the connect was previously called on the
188 // SurfaceTexture and no corresponding disconnect call was made.
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700189 //
190 // outWidth, outHeight and outTransform are filled with the default width
191 // and height of the window and current transform applied to buffers,
192 // respectively.
Mathias Agopian24202f52012-04-23 14:28:58 -0700193 virtual status_t connect(int api, QueueBufferOutput* output) = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700194
195 // disconnect attempts to disconnect a client API from the SurfaceTexture.
196 // Calling this method will cause any subsequent calls to other
197 // ISurfaceTexture methods to fail except for getAllocator and connect.
198 // Successfully calling connect after this will allow the other methods to
199 // succeed again.
200 //
201 // This method will fail if the the SurfaceTexture is not currently
202 // connected to the specified client API.
203 virtual status_t disconnect(int api) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800204};
205
206// ----------------------------------------------------------------------------
207
208class BnSurfaceTexture : public BnInterface<ISurfaceTexture>
209{
210public:
211 virtual status_t onTransact( uint32_t code,
212 const Parcel& data,
213 Parcel* reply,
214 uint32_t flags = 0);
215};
216
217// ----------------------------------------------------------------------------
218}; // namespace android
219
220#endif // ANDROID_GUI_ISURFACETEXTURE_H