blob: 0cca58dc2645f7a65bae4d8a090fe989fee85cbb [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>
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080031
32namespace android {
33// ----------------------------------------------------------------------------
34
35enum {
36 REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080037 DEQUEUE_BUFFER,
Dan Stoza9f3053d2014-03-06 15:14:33 -080038 DETACH_BUFFER,
Dan Stozad9822a32014-03-28 15:25:31 -070039 DETACH_NEXT_BUFFER,
Dan Stoza9f3053d2014-03-06 15:14:33 -080040 ATTACH_BUFFER,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041 QUEUE_BUFFER,
42 CANCEL_BUFFER,
Mathias Agopianeafabcd2011-04-20 14:20:59 -070043 QUERY,
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070044 CONNECT,
45 DISCONNECT,
Jesse Hall399184a2014-03-03 15:42:54 -080046 SET_SIDEBAND_STREAM,
Dan Stoza29a3e902014-06-20 13:13:57 -070047 ALLOCATE_BUFFERS,
Dan Stoza9de72932015-04-16 17:28:43 -070048 ALLOW_ALLOCATION,
Dan Stoza812ed062015-06-02 15:45:22 -070049 SET_GENERATION_NUMBER,
Dan Stozac6f30bd2015-06-08 09:32:50 -070050 GET_CONSUMER_NAME,
Pablo Ceballosfa455352015-08-12 17:47:47 -070051 SET_MAX_DEQUEUED_BUFFER_COUNT,
Dan Stoza7dde5992015-05-22 09:51:44 -070052 SET_ASYNC_MODE,
Pablo Ceballosccdfd602015-10-07 15:05:45 -070053 GET_NEXT_FRAME_NUMBER,
Dan Stoza127fc632015-06-30 13:43:32 -070054 SET_SINGLE_BUFFER_MODE,
55 SET_DEQUEUE_TIMEOUT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080056};
57
Andy McFadden2adaf042012-12-18 09:49:45 -080058class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080059{
60public:
Andy McFadden2adaf042012-12-18 09:49:45 -080061 BpGraphicBufferProducer(const sp<IBinder>& impl)
62 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080063 {
64 }
65
Dan Stoza3be1c6b2014-11-18 10:24:03 -080066 virtual ~BpGraphicBufferProducer();
67
Jamie Gennis7b305ff2011-07-19 12:08:33 -070068 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080070 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080071 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070072 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
73 if (result != NO_ERROR) {
74 return result;
75 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080076 bool nonNull = reply.readInt32();
77 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070078 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080079 result = reply.read(**buf);
80 if(result != NO_ERROR) {
81 (*buf).clear();
82 return result;
83 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080084 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070085 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070086 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080087 }
88
Pablo Ceballosfa455352015-08-12 17:47:47 -070089 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
90 Parcel data, reply;
91 data.writeInterfaceToken(
92 IGraphicBufferProducer::getInterfaceDescriptor());
93 data.writeInt32(maxDequeuedBuffers);
94 status_t result = remote()->transact(SET_MAX_DEQUEUED_BUFFER_COUNT,
95 data, &reply);
96 if (result != NO_ERROR) {
97 return result;
98 }
99 result = reply.readInt32();
100 return result;
101 }
102
103 virtual status_t setAsyncMode(bool async) {
104 Parcel data, reply;
105 data.writeInterfaceToken(
106 IGraphicBufferProducer::getInterfaceDescriptor());
107 data.writeInt32(async);
108 status_t result = remote()->transact(SET_ASYNC_MODE,
109 data, &reply);
110 if (result != NO_ERROR) {
111 return result;
112 }
113 result = reply.readInt32();
114 return result;
115 }
116
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700117 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, uint32_t width,
118 uint32_t height, PixelFormat format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800119 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800120 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800121 data.writeUint32(width);
122 data.writeUint32(height);
123 data.writeInt32(static_cast<int32_t>(format));
124 data.writeUint32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700125 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
126 if (result != NO_ERROR) {
127 return result;
128 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700130 bool nonNull = reply.readInt32();
131 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700132 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700133 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700134 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700135 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800136 return result;
137 }
138
Dan Stoza9f3053d2014-03-06 15:14:33 -0800139 virtual status_t detachBuffer(int slot) {
140 Parcel data, reply;
141 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
142 data.writeInt32(slot);
143 status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
144 if (result != NO_ERROR) {
145 return result;
146 }
147 result = reply.readInt32();
148 return result;
149 }
150
Dan Stozad9822a32014-03-28 15:25:31 -0700151 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
152 sp<Fence>* outFence) {
153 if (outBuffer == NULL) {
154 ALOGE("detachNextBuffer: outBuffer must not be NULL");
155 return BAD_VALUE;
156 } else if (outFence == NULL) {
157 ALOGE("detachNextBuffer: outFence must not be NULL");
158 return BAD_VALUE;
159 }
160 Parcel data, reply;
161 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
162 status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
163 if (result != NO_ERROR) {
164 return result;
165 }
166 result = reply.readInt32();
167 if (result == NO_ERROR) {
168 bool nonNull = reply.readInt32();
169 if (nonNull) {
170 *outBuffer = new GraphicBuffer;
171 reply.read(**outBuffer);
172 }
173 nonNull = reply.readInt32();
174 if (nonNull) {
175 *outFence = new Fence;
176 reply.read(**outFence);
177 }
178 }
179 return result;
180 }
181
Dan Stoza9f3053d2014-03-06 15:14:33 -0800182 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
183 Parcel data, reply;
184 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
185 data.write(*buffer.get());
186 status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
187 if (result != NO_ERROR) {
188 return result;
189 }
190 *slot = reply.readInt32();
191 result = reply.readInt32();
192 return result;
193 }
194
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700195 virtual status_t queueBuffer(int buf,
196 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800198 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800199 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700200 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700201 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
202 if (result != NO_ERROR) {
203 return result;
204 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700205 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700206 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 return result;
208 }
209
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700210 virtual status_t cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800212 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800214 data.write(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700215 status_t result = remote()->transact(CANCEL_BUFFER, data, &reply);
216 if (result != NO_ERROR) {
217 return result;
218 }
219 result = reply.readInt32();
220 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800221 }
222
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700223 virtual int query(int what, int* value) {
224 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800225 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700226 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700227 status_t result = remote()->transact(QUERY, data, &reply);
228 if (result != NO_ERROR) {
229 return result;
230 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700231 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700232 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700233 return result;
234 }
235
Dan Stozaf0eaf252014-03-21 13:05:51 -0700236 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700237 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700238 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800239 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stozaf0eaf252014-03-21 13:05:51 -0700240 if (listener != NULL) {
241 data.writeInt32(1);
Marco Nelissen097ca272014-11-14 08:01:01 -0800242 data.writeStrongBinder(IInterface::asBinder(listener));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700243 } else {
244 data.writeInt32(0);
245 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700246 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700247 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700248 status_t result = remote()->transact(CONNECT, data, &reply);
249 if (result != NO_ERROR) {
250 return result;
251 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700252 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700253 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700254 return result;
255 }
Mathias Agopian80727112011-05-02 19:51:12 -0700256
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700257 virtual status_t disconnect(int api) {
258 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800259 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700260 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700261 status_t result =remote()->transact(DISCONNECT, data, &reply);
262 if (result != NO_ERROR) {
263 return result;
264 }
265 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700266 return result;
267 }
Jesse Hall399184a2014-03-03 15:42:54 -0800268
269 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
270 Parcel data, reply;
271 status_t result;
272 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
273 if (stream.get()) {
274 data.writeInt32(true);
275 data.writeNativeHandle(stream->handle());
276 } else {
277 data.writeInt32(false);
278 }
279 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
280 result = reply.readInt32();
281 }
282 return result;
283 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700284
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700285 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800286 PixelFormat format, uint32_t usage) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700287 Parcel data, reply;
288 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800289 data.writeUint32(width);
290 data.writeUint32(height);
Dan Stoza29a3e902014-06-20 13:13:57 -0700291 data.writeInt32(static_cast<int32_t>(format));
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800292 data.writeUint32(usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700293 status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply);
294 if (result != NO_ERROR) {
295 ALOGE("allocateBuffers failed to transact: %d", result);
296 }
297 }
Dan Stoza9de72932015-04-16 17:28:43 -0700298
299 virtual status_t allowAllocation(bool allow) {
300 Parcel data, reply;
301 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
302 data.writeInt32(static_cast<int32_t>(allow));
303 status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
304 if (result != NO_ERROR) {
305 return result;
306 }
307 result = reply.readInt32();
308 return result;
309 }
Dan Stoza812ed062015-06-02 15:45:22 -0700310
311 virtual status_t setGenerationNumber(uint32_t generationNumber) {
312 Parcel data, reply;
313 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
314 data.writeUint32(generationNumber);
315 status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
316 if (result == NO_ERROR) {
317 result = reply.readInt32();
318 }
319 return result;
320 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700321
322 virtual String8 getConsumerName() const {
323 Parcel data, reply;
324 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
325 status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
326 if (result != NO_ERROR) {
327 ALOGE("getConsumerName failed to transact: %d", result);
328 return String8("TransactFailed");
329 }
330 return reply.readString8();
331 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700332
333 virtual uint64_t getNextFrameNumber() const {
334 Parcel data, reply;
335 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
336 status_t result = remote()->transact(GET_NEXT_FRAME_NUMBER, data, &reply);
337 if (result != NO_ERROR) {
338 ALOGE("getNextFrameNumber failed to transact: %d", result);
339 return 0;
340 }
341 uint64_t frameNumber = reply.readUint64();
342 return frameNumber;
343 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700344
345 virtual status_t setSingleBufferMode(bool singleBufferMode) {
346 Parcel data, reply;
347 data.writeInterfaceToken(
348 IGraphicBufferProducer::getInterfaceDescriptor());
349 data.writeInt32(singleBufferMode);
350 status_t result = remote()->transact(SET_SINGLE_BUFFER_MODE, data,
351 &reply);
352 if (result == NO_ERROR) {
353 result = reply.readInt32();
354 }
355 return result;
356 }
Dan Stoza127fc632015-06-30 13:43:32 -0700357
358 virtual status_t setDequeueTimeout(nsecs_t timeout) {
359 Parcel data, reply;
360 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
361 data.writeInt64(timeout);
362 status_t result = remote()->transact(SET_DEQUEUE_TIMEOUT, data, &reply);
363 if (result != NO_ERROR) {
364 ALOGE("setDequeueTimeout failed to transact: %d", result);
365 return result;
366 }
367 return reply.readInt32();
368 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800369};
370
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800371// Out-of-line virtual method definition to trigger vtable emission in this
372// translation unit (see clang warning -Wweak-vtables)
373BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
374
Andy McFadden466a1922013-01-08 11:25:51 -0800375IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800376
377// ----------------------------------------------------------------------
378
Andy McFadden2adaf042012-12-18 09:49:45 -0800379status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800380 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
381{
382 switch(code) {
383 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800384 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800385 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700386 sp<GraphicBuffer> buffer;
387 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800388 reply->writeInt32(buffer != 0);
389 if (buffer != 0) {
390 reply->write(*buffer);
391 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700392 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800394 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700395 case SET_MAX_DEQUEUED_BUFFER_COUNT: {
396 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
397 int maxDequeuedBuffers = data.readInt32();
398 int result = setMaxDequeuedBufferCount(maxDequeuedBuffers);
399 reply->writeInt32(result);
400 return NO_ERROR;
401 }
402 case SET_ASYNC_MODE: {
403 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
404 bool async = data.readInt32();
405 int result = setAsyncMode(async);
406 reply->writeInt32(result);
407 return NO_ERROR;
408 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800409 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800410 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800411 uint32_t width = data.readUint32();
412 uint32_t height = data.readUint32();
413 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
414 uint32_t usage = data.readUint32();
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700415 int buf = 0;
Jesse Hallf7857542012-06-14 15:26:33 -0700416 sp<Fence> fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700417 int result = dequeueBuffer(&buf, &fence, width, height, format,
418 usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800419 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800420 reply->writeInt32(fence != NULL);
421 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700422 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700423 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800424 reply->writeInt32(result);
425 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800426 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800427 case DETACH_BUFFER: {
428 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
429 int slot = data.readInt32();
430 int result = detachBuffer(slot);
431 reply->writeInt32(result);
432 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800433 }
Dan Stozad9822a32014-03-28 15:25:31 -0700434 case DETACH_NEXT_BUFFER: {
435 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
436 sp<GraphicBuffer> buffer;
437 sp<Fence> fence;
438 int32_t result = detachNextBuffer(&buffer, &fence);
439 reply->writeInt32(result);
440 if (result == NO_ERROR) {
441 reply->writeInt32(buffer != NULL);
442 if (buffer != NULL) {
443 reply->write(*buffer);
444 }
445 reply->writeInt32(fence != NULL);
446 if (fence != NULL) {
447 reply->write(*fence);
448 }
449 }
450 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800451 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800452 case ATTACH_BUFFER: {
453 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
454 sp<GraphicBuffer> buffer = new GraphicBuffer();
455 data.read(*buffer.get());
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700456 int slot = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800457 int result = attachBuffer(&slot, buffer);
458 reply->writeInt32(slot);
459 reply->writeInt32(result);
460 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800461 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800462 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800463 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800464 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700465 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700466 QueueBufferOutput* const output =
467 reinterpret_cast<QueueBufferOutput *>(
468 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700469 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800470 reply->writeInt32(result);
471 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800472 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800473 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800474 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800475 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800476 sp<Fence> fence = new Fence();
477 data.read(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700478 status_t result = cancelBuffer(buf, fence);
479 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800480 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800481 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700482 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800483 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700484 int value = 0;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700485 int what = data.readInt32();
486 int res = query(what, &value);
487 reply->writeInt32(value);
488 reply->writeInt32(res);
489 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800490 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700491 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800492 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700493 sp<IProducerListener> listener;
494 if (data.readInt32() == 1) {
495 listener = IProducerListener::asInterface(data.readStrongBinder());
496 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700497 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700498 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700499 QueueBufferOutput* const output =
500 reinterpret_cast<QueueBufferOutput *>(
501 reply->writeInplace(sizeof(QueueBufferOutput)));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700502 status_t res = connect(listener, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700503 reply->writeInt32(res);
504 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800505 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700506 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800507 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700508 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700509 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700510 reply->writeInt32(res);
511 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800512 }
Jesse Hall399184a2014-03-03 15:42:54 -0800513 case SET_SIDEBAND_STREAM: {
514 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
515 sp<NativeHandle> stream;
516 if (data.readInt32()) {
Wonsik Kim0ec54e12014-03-21 10:46:24 +0900517 stream = NativeHandle::create(data.readNativeHandle(), true);
Jesse Hall399184a2014-03-03 15:42:54 -0800518 }
519 status_t result = setSidebandStream(stream);
520 reply->writeInt32(result);
521 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800522 }
Dan Stoza9de72932015-04-16 17:28:43 -0700523 case ALLOCATE_BUFFERS: {
Dan Stoza29a3e902014-06-20 13:13:57 -0700524 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800525 uint32_t width = data.readUint32();
526 uint32_t height = data.readUint32();
527 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
528 uint32_t usage = data.readUint32();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700529 allocateBuffers(width, height, format, usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700530 return NO_ERROR;
Dan Stoza9de72932015-04-16 17:28:43 -0700531 }
532 case ALLOW_ALLOCATION: {
533 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
534 bool allow = static_cast<bool>(data.readInt32());
535 status_t result = allowAllocation(allow);
536 reply->writeInt32(result);
537 return NO_ERROR;
538 }
Dan Stoza812ed062015-06-02 15:45:22 -0700539 case SET_GENERATION_NUMBER: {
540 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
541 uint32_t generationNumber = data.readUint32();
542 status_t result = setGenerationNumber(generationNumber);
543 reply->writeInt32(result);
544 return NO_ERROR;
545 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700546 case GET_CONSUMER_NAME: {
547 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
548 reply->writeString8(getConsumerName());
549 return NO_ERROR;
550 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700551 case GET_NEXT_FRAME_NUMBER: {
552 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
553 uint64_t frameNumber = getNextFrameNumber();
554 reply->writeUint64(frameNumber);
555 return NO_ERROR;
556 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700557 case SET_SINGLE_BUFFER_MODE: {
558 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
559 bool singleBufferMode = data.readInt32();
560 status_t result = setSingleBufferMode(singleBufferMode);
561 reply->writeInt32(result);
562 return NO_ERROR;
563 }
Dan Stoza127fc632015-06-30 13:43:32 -0700564 case SET_DEQUEUE_TIMEOUT: {
565 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
566 nsecs_t timeout = data.readInt64();
567 status_t result = setDequeueTimeout(timeout);
568 reply->writeInt32(result);
569 return NO_ERROR;
570 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800571 }
572 return BBinder::onTransact(code, data, reply, flags);
573}
574
575// ----------------------------------------------------------------------------
576
Andy McFadden2adaf042012-12-18 09:49:45 -0800577IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700578 parcel.read(*this);
579}
580
Mathias Agopiane1424282013-07-29 21:24:40 -0700581size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700582 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700583 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800584 + sizeof(dataSpace)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700585 + sizeof(crop)
586 + sizeof(scalingMode)
587 + sizeof(transform)
Ruben Brunk1681d952014-06-27 15:51:55 -0700588 + sizeof(stickyTransform)
Dan Stoza5065a552015-03-17 16:23:42 -0700589 + fence->getFlattenedSize()
590 + surfaceDamage.getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700591}
592
Mathias Agopiane1424282013-07-29 21:24:40 -0700593size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800594 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700595}
596
Mathias Agopiane1424282013-07-29 21:24:40 -0700597status_t IGraphicBufferProducer::QueueBufferInput::flatten(
598 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700599{
Mathias Agopiane1424282013-07-29 21:24:40 -0700600 if (size < getFlattenedSize()) {
601 return NO_MEMORY;
602 }
603 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700604 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800605 FlattenableUtils::write(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700606 FlattenableUtils::write(buffer, size, crop);
607 FlattenableUtils::write(buffer, size, scalingMode);
608 FlattenableUtils::write(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700609 FlattenableUtils::write(buffer, size, stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700610 status_t result = fence->flatten(buffer, size, fds, count);
611 if (result != NO_ERROR) {
612 return result;
613 }
614 return surfaceDamage.flatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700615}
616
Mathias Agopiane1424282013-07-29 21:24:40 -0700617status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
618 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700619{
Mathias Agopiane1424282013-07-29 21:24:40 -0700620 size_t minNeeded =
621 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700622 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800623 + sizeof(dataSpace)
Mathias Agopiane1424282013-07-29 21:24:40 -0700624 + sizeof(crop)
625 + sizeof(scalingMode)
626 + sizeof(transform)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700627 + sizeof(stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700628
629 if (size < minNeeded) {
630 return NO_MEMORY;
631 }
632
633 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700634 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800635 FlattenableUtils::read(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700636 FlattenableUtils::read(buffer, size, crop);
637 FlattenableUtils::read(buffer, size, scalingMode);
638 FlattenableUtils::read(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700639 FlattenableUtils::read(buffer, size, stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700640
Jamie Gennis1df8c342012-12-20 14:05:45 -0800641 fence = new Fence();
Dan Stoza5065a552015-03-17 16:23:42 -0700642 status_t result = fence->unflatten(buffer, size, fds, count);
643 if (result != NO_ERROR) {
644 return result;
645 }
646 return surfaceDamage.unflatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700647}
648
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800649}; // namespace android