blob: efbe878627d070e9e94669252966879fb9eba9bd [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#include <stdint.h>
18#include <sys/types.h>
19
20#include <utils/Errors.h>
Jesse Hall399184a2014-03-03 15:42:54 -080021#include <utils/NativeHandle.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080022#include <utils/RefBase.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080023#include <utils/Timers.h>
Jesse Hall399184a2014-03-03 15:42:54 -080024#include <utils/Vector.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080025
26#include <binder/Parcel.h>
27#include <binder/IInterface.h>
28
Andy McFadden2adaf042012-12-18 09:49:45 -080029#include <gui/IGraphicBufferProducer.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080030
31namespace android {
32// ----------------------------------------------------------------------------
33
34enum {
35 REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
36 SET_BUFFER_COUNT,
37 DEQUEUE_BUFFER,
38 QUEUE_BUFFER,
39 CANCEL_BUFFER,
Mathias Agopianeafabcd2011-04-20 14:20:59 -070040 QUERY,
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070041 CONNECT,
42 DISCONNECT,
Jesse Hall399184a2014-03-03 15:42:54 -080043 SET_SIDEBAND_STREAM,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044};
45
Andy McFadden2adaf042012-12-18 09:49:45 -080046class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080047{
48public:
Andy McFadden2adaf042012-12-18 09:49:45 -080049 BpGraphicBufferProducer(const sp<IBinder>& impl)
50 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080051 {
52 }
53
Jamie Gennis7b305ff2011-07-19 12:08:33 -070054 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080056 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070058 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
59 if (result != NO_ERROR) {
60 return result;
61 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080062 bool nonNull = reply.readInt32();
63 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070064 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080065 result = reply.read(**buf);
66 if(result != NO_ERROR) {
67 (*buf).clear();
68 return result;
69 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080070 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070071 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070072 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073 }
74
75 virtual status_t setBufferCount(int bufferCount)
76 {
77 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080078 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080079 data.writeInt32(bufferCount);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070080 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
81 if (result != NO_ERROR) {
82 return result;
83 }
84 result = reply.readInt32();
85 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080086 }
87
Mathias Agopian7cdd7862013-07-18 22:10:56 -070088 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -070089 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080090 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080091 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian7cdd7862013-07-18 22:10:56 -070092 data.writeInt32(async);
Mathias Agopianc04f1532011-04-25 20:22:14 -070093 data.writeInt32(w);
94 data.writeInt32(h);
95 data.writeInt32(format);
96 data.writeInt32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070097 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
98 if (result != NO_ERROR) {
99 return result;
100 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800101 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700102 bool nonNull = reply.readInt32();
103 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700104 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700105 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700106 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700107 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108 return result;
109 }
110
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700111 virtual status_t queueBuffer(int buf,
112 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800113 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800114 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800115 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700116 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700117 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
118 if (result != NO_ERROR) {
119 return result;
120 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700121 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700122 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123 return result;
124 }
125
Jesse Hall4c00cc12013-03-15 21:34:30 -0700126 virtual void cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800127 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800128 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800130 data.write(*fence.get());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131 remote()->transact(CANCEL_BUFFER, data, &reply);
132 }
133
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700134 virtual int query(int what, int* value) {
135 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800136 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700137 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700138 status_t result = remote()->transact(QUERY, data, &reply);
139 if (result != NO_ERROR) {
140 return result;
141 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700142 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700143 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700144 return result;
145 }
146
Mathias Agopian365857d2013-09-11 19:35:45 -0700147 virtual status_t connect(const sp<IBinder>& token,
148 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700149 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800150 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian365857d2013-09-11 19:35:45 -0700151 data.writeStrongBinder(token);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700152 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700153 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700154 status_t result = remote()->transact(CONNECT, data, &reply);
155 if (result != NO_ERROR) {
156 return result;
157 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700158 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700159 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700160 return result;
161 }
Mathias Agopian80727112011-05-02 19:51:12 -0700162
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700163 virtual status_t disconnect(int api) {
164 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800165 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700166 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700167 status_t result =remote()->transact(DISCONNECT, data, &reply);
168 if (result != NO_ERROR) {
169 return result;
170 }
171 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700172 return result;
173 }
Jesse Hall399184a2014-03-03 15:42:54 -0800174
175 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
176 Parcel data, reply;
177 status_t result;
178 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
179 if (stream.get()) {
180 data.writeInt32(true);
181 data.writeNativeHandle(stream->handle());
182 } else {
183 data.writeInt32(false);
184 }
185 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
186 result = reply.readInt32();
187 }
188 return result;
189 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800190};
191
Andy McFadden466a1922013-01-08 11:25:51 -0800192IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800193
194// ----------------------------------------------------------------------
195
Andy McFadden2adaf042012-12-18 09:49:45 -0800196status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
198{
199 switch(code) {
200 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800201 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800202 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700203 sp<GraphicBuffer> buffer;
204 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800205 reply->writeInt32(buffer != 0);
206 if (buffer != 0) {
207 reply->write(*buffer);
208 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700209 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 return NO_ERROR;
211 } break;
212 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800213 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 int bufferCount = data.readInt32();
215 int result = setBufferCount(bufferCount);
216 reply->writeInt32(result);
217 return NO_ERROR;
218 } break;
219 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800220 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700221 bool async = data.readInt32();
Mathias Agopianc04f1532011-04-25 20:22:14 -0700222 uint32_t w = data.readInt32();
223 uint32_t h = data.readInt32();
224 uint32_t format = data.readInt32();
225 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800226 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700227 sp<Fence> fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700228 int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800229 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800230 reply->writeInt32(fence != NULL);
231 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700232 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700233 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 reply->writeInt32(result);
235 return NO_ERROR;
236 } break;
237 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800238 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800239 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700240 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700241 QueueBufferOutput* const output =
242 reinterpret_cast<QueueBufferOutput *>(
243 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700244 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800245 reply->writeInt32(result);
246 return NO_ERROR;
247 } break;
248 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800249 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800250 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800251 sp<Fence> fence = new Fence();
252 data.read(*fence.get());
Jesse Hallc777b0b2012-06-28 12:52:05 -0700253 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800254 return NO_ERROR;
255 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700256 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800257 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700258 int value;
259 int what = data.readInt32();
260 int res = query(what, &value);
261 reply->writeInt32(value);
262 reply->writeInt32(res);
263 return NO_ERROR;
264 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700265 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800266 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian365857d2013-09-11 19:35:45 -0700267 sp<IBinder> token = data.readStrongBinder();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700268 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700269 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700270 QueueBufferOutput* const output =
271 reinterpret_cast<QueueBufferOutput *>(
272 reply->writeInplace(sizeof(QueueBufferOutput)));
Mathias Agopian365857d2013-09-11 19:35:45 -0700273 status_t res = connect(token, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700274 reply->writeInt32(res);
275 return NO_ERROR;
276 } break;
277 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800278 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700279 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700280 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700281 reply->writeInt32(res);
282 return NO_ERROR;
283 } break;
Jesse Hall399184a2014-03-03 15:42:54 -0800284 case SET_SIDEBAND_STREAM: {
285 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
286 sp<NativeHandle> stream;
287 if (data.readInt32()) {
288 stream = NativeHandle::create(data.readNativeHandle());
289 }
290 status_t result = setSidebandStream(stream);
291 reply->writeInt32(result);
292 return NO_ERROR;
293 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800294 }
295 return BBinder::onTransact(code, data, reply, flags);
296}
297
298// ----------------------------------------------------------------------------
299
Andy McFadden2adaf042012-12-18 09:49:45 -0800300IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700301 parcel.read(*this);
302}
303
Mathias Agopiane1424282013-07-29 21:24:40 -0700304size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700305 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700306 + sizeof(isAutoTimestamp)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700307 + sizeof(crop)
308 + sizeof(scalingMode)
309 + sizeof(transform)
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700310 + sizeof(async)
Jamie Gennis1df8c342012-12-20 14:05:45 -0800311 + fence->getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700312}
313
Mathias Agopiane1424282013-07-29 21:24:40 -0700314size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800315 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700316}
317
Mathias Agopiane1424282013-07-29 21:24:40 -0700318status_t IGraphicBufferProducer::QueueBufferInput::flatten(
319 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700320{
Mathias Agopiane1424282013-07-29 21:24:40 -0700321 if (size < getFlattenedSize()) {
322 return NO_MEMORY;
323 }
324 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700325 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700326 FlattenableUtils::write(buffer, size, crop);
327 FlattenableUtils::write(buffer, size, scalingMode);
328 FlattenableUtils::write(buffer, size, transform);
329 FlattenableUtils::write(buffer, size, async);
330 return fence->flatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700331}
332
Mathias Agopiane1424282013-07-29 21:24:40 -0700333status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
334 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700335{
Mathias Agopiane1424282013-07-29 21:24:40 -0700336 size_t minNeeded =
337 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700338 + sizeof(isAutoTimestamp)
Mathias Agopiane1424282013-07-29 21:24:40 -0700339 + sizeof(crop)
340 + sizeof(scalingMode)
341 + sizeof(transform)
342 + sizeof(async);
343
344 if (size < minNeeded) {
345 return NO_MEMORY;
346 }
347
348 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700349 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700350 FlattenableUtils::read(buffer, size, crop);
351 FlattenableUtils::read(buffer, size, scalingMode);
352 FlattenableUtils::read(buffer, size, transform);
353 FlattenableUtils::read(buffer, size, async);
354
Jamie Gennis1df8c342012-12-20 14:05:45 -0800355 fence = new Fence();
Mathias Agopiane1424282013-07-29 21:24:40 -0700356 return fence->unflatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700357}
358
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800359}; // namespace android