blob: 640a9f2136f81e1ccb32b8ef7b120837b6443530 [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,
53 GET_NEXT_FRAME_NUMBER
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080054};
55
Andy McFadden2adaf042012-12-18 09:49:45 -080056class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057{
58public:
Andy McFadden2adaf042012-12-18 09:49:45 -080059 BpGraphicBufferProducer(const sp<IBinder>& impl)
60 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061 {
62 }
63
Dan Stoza3be1c6b2014-11-18 10:24:03 -080064 virtual ~BpGraphicBufferProducer();
65
Jamie Gennis7b305ff2011-07-19 12:08:33 -070066 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080067 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080068 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070070 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
71 if (result != NO_ERROR) {
72 return result;
73 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080074 bool nonNull = reply.readInt32();
75 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070076 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080077 result = reply.read(**buf);
78 if(result != NO_ERROR) {
79 (*buf).clear();
80 return result;
81 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080082 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070083 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070084 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085 }
86
Pablo Ceballosfa455352015-08-12 17:47:47 -070087 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
88 Parcel data, reply;
89 data.writeInterfaceToken(
90 IGraphicBufferProducer::getInterfaceDescriptor());
91 data.writeInt32(maxDequeuedBuffers);
92 status_t result = remote()->transact(SET_MAX_DEQUEUED_BUFFER_COUNT,
93 data, &reply);
94 if (result != NO_ERROR) {
95 return result;
96 }
97 result = reply.readInt32();
98 return result;
99 }
100
101 virtual status_t setAsyncMode(bool async) {
102 Parcel data, reply;
103 data.writeInterfaceToken(
104 IGraphicBufferProducer::getInterfaceDescriptor());
105 data.writeInt32(async);
106 status_t result = remote()->transact(SET_ASYNC_MODE,
107 data, &reply);
108 if (result != NO_ERROR) {
109 return result;
110 }
111 result = reply.readInt32();
112 return result;
113 }
114
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700115 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, uint32_t width,
116 uint32_t height, PixelFormat format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800117 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800118 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800119 data.writeUint32(width);
120 data.writeUint32(height);
121 data.writeInt32(static_cast<int32_t>(format));
122 data.writeUint32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700123 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
124 if (result != NO_ERROR) {
125 return result;
126 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800127 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700128 bool nonNull = reply.readInt32();
129 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700130 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700131 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700132 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700133 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134 return result;
135 }
136
Dan Stoza9f3053d2014-03-06 15:14:33 -0800137 virtual status_t detachBuffer(int slot) {
138 Parcel data, reply;
139 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
140 data.writeInt32(slot);
141 status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
142 if (result != NO_ERROR) {
143 return result;
144 }
145 result = reply.readInt32();
146 return result;
147 }
148
Dan Stozad9822a32014-03-28 15:25:31 -0700149 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
150 sp<Fence>* outFence) {
151 if (outBuffer == NULL) {
152 ALOGE("detachNextBuffer: outBuffer must not be NULL");
153 return BAD_VALUE;
154 } else if (outFence == NULL) {
155 ALOGE("detachNextBuffer: outFence must not be NULL");
156 return BAD_VALUE;
157 }
158 Parcel data, reply;
159 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
160 status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
161 if (result != NO_ERROR) {
162 return result;
163 }
164 result = reply.readInt32();
165 if (result == NO_ERROR) {
166 bool nonNull = reply.readInt32();
167 if (nonNull) {
168 *outBuffer = new GraphicBuffer;
169 reply.read(**outBuffer);
170 }
171 nonNull = reply.readInt32();
172 if (nonNull) {
173 *outFence = new Fence;
174 reply.read(**outFence);
175 }
176 }
177 return result;
178 }
179
Dan Stoza9f3053d2014-03-06 15:14:33 -0800180 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
181 Parcel data, reply;
182 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
183 data.write(*buffer.get());
184 status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
185 if (result != NO_ERROR) {
186 return result;
187 }
188 *slot = reply.readInt32();
189 result = reply.readInt32();
190 return result;
191 }
192
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700193 virtual status_t queueBuffer(int buf,
194 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800196 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700198 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700199 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
200 if (result != NO_ERROR) {
201 return result;
202 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700203 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700204 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800205 return result;
206 }
207
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700208 virtual status_t cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800209 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800210 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800212 data.write(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700213 status_t result = remote()->transact(CANCEL_BUFFER, data, &reply);
214 if (result != NO_ERROR) {
215 return result;
216 }
217 result = reply.readInt32();
218 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 }
220
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700221 virtual int query(int what, int* value) {
222 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800223 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700224 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700225 status_t result = remote()->transact(QUERY, data, &reply);
226 if (result != NO_ERROR) {
227 return result;
228 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700229 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700230 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700231 return result;
232 }
233
Dan Stozaf0eaf252014-03-21 13:05:51 -0700234 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700235 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700236 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800237 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stozaf0eaf252014-03-21 13:05:51 -0700238 if (listener != NULL) {
239 data.writeInt32(1);
Marco Nelissen097ca272014-11-14 08:01:01 -0800240 data.writeStrongBinder(IInterface::asBinder(listener));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700241 } else {
242 data.writeInt32(0);
243 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700244 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700245 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700246 status_t result = remote()->transact(CONNECT, data, &reply);
247 if (result != NO_ERROR) {
248 return result;
249 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700250 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700251 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700252 return result;
253 }
Mathias Agopian80727112011-05-02 19:51:12 -0700254
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700255 virtual status_t disconnect(int api) {
256 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800257 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700258 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700259 status_t result =remote()->transact(DISCONNECT, data, &reply);
260 if (result != NO_ERROR) {
261 return result;
262 }
263 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700264 return result;
265 }
Jesse Hall399184a2014-03-03 15:42:54 -0800266
267 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
268 Parcel data, reply;
269 status_t result;
270 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
271 if (stream.get()) {
272 data.writeInt32(true);
273 data.writeNativeHandle(stream->handle());
274 } else {
275 data.writeInt32(false);
276 }
277 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
278 result = reply.readInt32();
279 }
280 return result;
281 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700282
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700283 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800284 PixelFormat format, uint32_t usage) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700285 Parcel data, reply;
286 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800287 data.writeUint32(width);
288 data.writeUint32(height);
Dan Stoza29a3e902014-06-20 13:13:57 -0700289 data.writeInt32(static_cast<int32_t>(format));
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800290 data.writeUint32(usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700291 status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply);
292 if (result != NO_ERROR) {
293 ALOGE("allocateBuffers failed to transact: %d", result);
294 }
295 }
Dan Stoza9de72932015-04-16 17:28:43 -0700296
297 virtual status_t allowAllocation(bool allow) {
298 Parcel data, reply;
299 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
300 data.writeInt32(static_cast<int32_t>(allow));
301 status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
302 if (result != NO_ERROR) {
303 return result;
304 }
305 result = reply.readInt32();
306 return result;
307 }
Dan Stoza812ed062015-06-02 15:45:22 -0700308
309 virtual status_t setGenerationNumber(uint32_t generationNumber) {
310 Parcel data, reply;
311 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
312 data.writeUint32(generationNumber);
313 status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
314 if (result == NO_ERROR) {
315 result = reply.readInt32();
316 }
317 return result;
318 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700319
320 virtual String8 getConsumerName() const {
321 Parcel data, reply;
322 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
323 status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
324 if (result != NO_ERROR) {
325 ALOGE("getConsumerName failed to transact: %d", result);
326 return String8("TransactFailed");
327 }
328 return reply.readString8();
329 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700330
331 virtual uint64_t getNextFrameNumber() const {
332 Parcel data, reply;
333 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
334 status_t result = remote()->transact(GET_NEXT_FRAME_NUMBER, data, &reply);
335 if (result != NO_ERROR) {
336 ALOGE("getNextFrameNumber failed to transact: %d", result);
337 return 0;
338 }
339 uint64_t frameNumber = reply.readUint64();
340 return frameNumber;
341 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800342};
343
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800344// Out-of-line virtual method definition to trigger vtable emission in this
345// translation unit (see clang warning -Wweak-vtables)
346BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
347
Andy McFadden466a1922013-01-08 11:25:51 -0800348IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800349
350// ----------------------------------------------------------------------
351
Andy McFadden2adaf042012-12-18 09:49:45 -0800352status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800353 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
354{
355 switch(code) {
356 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800357 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800358 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700359 sp<GraphicBuffer> buffer;
360 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800361 reply->writeInt32(buffer != 0);
362 if (buffer != 0) {
363 reply->write(*buffer);
364 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700365 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800366 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800367 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700368 case SET_MAX_DEQUEUED_BUFFER_COUNT: {
369 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
370 int maxDequeuedBuffers = data.readInt32();
371 int result = setMaxDequeuedBufferCount(maxDequeuedBuffers);
372 reply->writeInt32(result);
373 return NO_ERROR;
374 }
375 case SET_ASYNC_MODE: {
376 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
377 bool async = data.readInt32();
378 int result = setAsyncMode(async);
379 reply->writeInt32(result);
380 return NO_ERROR;
381 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800382 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800383 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800384 uint32_t width = data.readUint32();
385 uint32_t height = data.readUint32();
386 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
387 uint32_t usage = data.readUint32();
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700388 int buf = 0;
Jesse Hallf7857542012-06-14 15:26:33 -0700389 sp<Fence> fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700390 int result = dequeueBuffer(&buf, &fence, width, height, format,
391 usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800392 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800393 reply->writeInt32(fence != NULL);
394 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700395 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700396 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800397 reply->writeInt32(result);
398 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800399 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800400 case DETACH_BUFFER: {
401 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
402 int slot = data.readInt32();
403 int result = detachBuffer(slot);
404 reply->writeInt32(result);
405 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800406 }
Dan Stozad9822a32014-03-28 15:25:31 -0700407 case DETACH_NEXT_BUFFER: {
408 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
409 sp<GraphicBuffer> buffer;
410 sp<Fence> fence;
411 int32_t result = detachNextBuffer(&buffer, &fence);
412 reply->writeInt32(result);
413 if (result == NO_ERROR) {
414 reply->writeInt32(buffer != NULL);
415 if (buffer != NULL) {
416 reply->write(*buffer);
417 }
418 reply->writeInt32(fence != NULL);
419 if (fence != NULL) {
420 reply->write(*fence);
421 }
422 }
423 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800424 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800425 case ATTACH_BUFFER: {
426 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
427 sp<GraphicBuffer> buffer = new GraphicBuffer();
428 data.read(*buffer.get());
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700429 int slot = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800430 int result = attachBuffer(&slot, buffer);
431 reply->writeInt32(slot);
432 reply->writeInt32(result);
433 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800434 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800435 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800436 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800437 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700438 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700439 QueueBufferOutput* const output =
440 reinterpret_cast<QueueBufferOutput *>(
441 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700442 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800443 reply->writeInt32(result);
444 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800445 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800446 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800447 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800448 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800449 sp<Fence> fence = new Fence();
450 data.read(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700451 status_t result = cancelBuffer(buf, fence);
452 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800453 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800454 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700455 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800456 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700457 int value = 0;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700458 int what = data.readInt32();
459 int res = query(what, &value);
460 reply->writeInt32(value);
461 reply->writeInt32(res);
462 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800463 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700464 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800465 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700466 sp<IProducerListener> listener;
467 if (data.readInt32() == 1) {
468 listener = IProducerListener::asInterface(data.readStrongBinder());
469 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700470 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700471 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700472 QueueBufferOutput* const output =
473 reinterpret_cast<QueueBufferOutput *>(
474 reply->writeInplace(sizeof(QueueBufferOutput)));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700475 status_t res = connect(listener, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700476 reply->writeInt32(res);
477 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800478 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700479 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800480 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700481 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700482 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700483 reply->writeInt32(res);
484 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800485 }
Jesse Hall399184a2014-03-03 15:42:54 -0800486 case SET_SIDEBAND_STREAM: {
487 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
488 sp<NativeHandle> stream;
489 if (data.readInt32()) {
Wonsik Kim0ec54e12014-03-21 10:46:24 +0900490 stream = NativeHandle::create(data.readNativeHandle(), true);
Jesse Hall399184a2014-03-03 15:42:54 -0800491 }
492 status_t result = setSidebandStream(stream);
493 reply->writeInt32(result);
494 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800495 }
Dan Stoza9de72932015-04-16 17:28:43 -0700496 case ALLOCATE_BUFFERS: {
Dan Stoza29a3e902014-06-20 13:13:57 -0700497 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800498 uint32_t width = data.readUint32();
499 uint32_t height = data.readUint32();
500 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
501 uint32_t usage = data.readUint32();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700502 allocateBuffers(width, height, format, usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700503 return NO_ERROR;
Dan Stoza9de72932015-04-16 17:28:43 -0700504 }
505 case ALLOW_ALLOCATION: {
506 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
507 bool allow = static_cast<bool>(data.readInt32());
508 status_t result = allowAllocation(allow);
509 reply->writeInt32(result);
510 return NO_ERROR;
511 }
Dan Stoza812ed062015-06-02 15:45:22 -0700512 case SET_GENERATION_NUMBER: {
513 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
514 uint32_t generationNumber = data.readUint32();
515 status_t result = setGenerationNumber(generationNumber);
516 reply->writeInt32(result);
517 return NO_ERROR;
518 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700519 case GET_CONSUMER_NAME: {
520 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
521 reply->writeString8(getConsumerName());
522 return NO_ERROR;
523 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700524 case GET_NEXT_FRAME_NUMBER: {
525 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
526 uint64_t frameNumber = getNextFrameNumber();
527 reply->writeUint64(frameNumber);
528 return NO_ERROR;
529 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800530 }
531 return BBinder::onTransact(code, data, reply, flags);
532}
533
534// ----------------------------------------------------------------------------
535
Andy McFadden2adaf042012-12-18 09:49:45 -0800536IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700537 parcel.read(*this);
538}
539
Mathias Agopiane1424282013-07-29 21:24:40 -0700540size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700541 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700542 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800543 + sizeof(dataSpace)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700544 + sizeof(crop)
545 + sizeof(scalingMode)
546 + sizeof(transform)
Ruben Brunk1681d952014-06-27 15:51:55 -0700547 + sizeof(stickyTransform)
Dan Stoza5065a552015-03-17 16:23:42 -0700548 + fence->getFlattenedSize()
549 + surfaceDamage.getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700550}
551
Mathias Agopiane1424282013-07-29 21:24:40 -0700552size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800553 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700554}
555
Mathias Agopiane1424282013-07-29 21:24:40 -0700556status_t IGraphicBufferProducer::QueueBufferInput::flatten(
557 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700558{
Mathias Agopiane1424282013-07-29 21:24:40 -0700559 if (size < getFlattenedSize()) {
560 return NO_MEMORY;
561 }
562 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700563 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800564 FlattenableUtils::write(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700565 FlattenableUtils::write(buffer, size, crop);
566 FlattenableUtils::write(buffer, size, scalingMode);
567 FlattenableUtils::write(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700568 FlattenableUtils::write(buffer, size, stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700569 status_t result = fence->flatten(buffer, size, fds, count);
570 if (result != NO_ERROR) {
571 return result;
572 }
573 return surfaceDamage.flatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700574}
575
Mathias Agopiane1424282013-07-29 21:24:40 -0700576status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
577 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700578{
Mathias Agopiane1424282013-07-29 21:24:40 -0700579 size_t minNeeded =
580 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700581 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800582 + sizeof(dataSpace)
Mathias Agopiane1424282013-07-29 21:24:40 -0700583 + sizeof(crop)
584 + sizeof(scalingMode)
585 + sizeof(transform)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700586 + sizeof(stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700587
588 if (size < minNeeded) {
589 return NO_MEMORY;
590 }
591
592 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700593 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800594 FlattenableUtils::read(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700595 FlattenableUtils::read(buffer, size, crop);
596 FlattenableUtils::read(buffer, size, scalingMode);
597 FlattenableUtils::read(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700598 FlattenableUtils::read(buffer, size, stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700599
Jamie Gennis1df8c342012-12-20 14:05:45 -0800600 fence = new Fence();
Dan Stoza5065a552015-03-17 16:23:42 -0700601 status_t result = fence->unflatten(buffer, size, fds, count);
602 if (result != NO_ERROR) {
603 return result;
604 }
605 return surfaceDamage.unflatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700606}
607
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800608}; // namespace android