blob: c66694d94a0f3a7d34e0b6cd86fd755d0dea4dbd [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,
Pablo Ceballosff95aab2016-01-13 17:09:58 -080055 SET_AUTO_REFRESH,
Dan Stoza127fc632015-06-30 13:43:32 -070056 SET_DEQUEUE_TIMEOUT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057};
58
Andy McFadden2adaf042012-12-18 09:49:45 -080059class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080060{
61public:
Andy McFadden2adaf042012-12-18 09:49:45 -080062 BpGraphicBufferProducer(const sp<IBinder>& impl)
63 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080064 {
65 }
66
Dan Stoza3be1c6b2014-11-18 10:24:03 -080067 virtual ~BpGraphicBufferProducer();
68
Jamie Gennis7b305ff2011-07-19 12:08:33 -070069 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080070 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080071 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080072 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070073 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
74 if (result != NO_ERROR) {
75 return result;
76 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080077 bool nonNull = reply.readInt32();
78 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070079 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080080 result = reply.read(**buf);
81 if(result != NO_ERROR) {
82 (*buf).clear();
83 return result;
84 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070086 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070087 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080088 }
89
Pablo Ceballosfa455352015-08-12 17:47:47 -070090 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
91 Parcel data, reply;
92 data.writeInterfaceToken(
93 IGraphicBufferProducer::getInterfaceDescriptor());
94 data.writeInt32(maxDequeuedBuffers);
95 status_t result = remote()->transact(SET_MAX_DEQUEUED_BUFFER_COUNT,
96 data, &reply);
97 if (result != NO_ERROR) {
98 return result;
99 }
100 result = reply.readInt32();
101 return result;
102 }
103
104 virtual status_t setAsyncMode(bool async) {
105 Parcel data, reply;
106 data.writeInterfaceToken(
107 IGraphicBufferProducer::getInterfaceDescriptor());
108 data.writeInt32(async);
109 status_t result = remote()->transact(SET_ASYNC_MODE,
110 data, &reply);
111 if (result != NO_ERROR) {
112 return result;
113 }
114 result = reply.readInt32();
115 return result;
116 }
117
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700118 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, uint32_t width,
119 uint32_t height, PixelFormat format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800120 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800121 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800122 data.writeUint32(width);
123 data.writeUint32(height);
124 data.writeInt32(static_cast<int32_t>(format));
125 data.writeUint32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700126 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
127 if (result != NO_ERROR) {
128 return result;
129 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700131 bool nonNull = reply.readInt32();
132 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700133 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700134 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700135 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700136 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800137 return result;
138 }
139
Dan Stoza9f3053d2014-03-06 15:14:33 -0800140 virtual status_t detachBuffer(int slot) {
141 Parcel data, reply;
142 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
143 data.writeInt32(slot);
144 status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
145 if (result != NO_ERROR) {
146 return result;
147 }
148 result = reply.readInt32();
149 return result;
150 }
151
Dan Stozad9822a32014-03-28 15:25:31 -0700152 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
153 sp<Fence>* outFence) {
154 if (outBuffer == NULL) {
155 ALOGE("detachNextBuffer: outBuffer must not be NULL");
156 return BAD_VALUE;
157 } else if (outFence == NULL) {
158 ALOGE("detachNextBuffer: outFence must not be NULL");
159 return BAD_VALUE;
160 }
161 Parcel data, reply;
162 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
163 status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
164 if (result != NO_ERROR) {
165 return result;
166 }
167 result = reply.readInt32();
168 if (result == NO_ERROR) {
169 bool nonNull = reply.readInt32();
170 if (nonNull) {
171 *outBuffer = new GraphicBuffer;
172 reply.read(**outBuffer);
173 }
174 nonNull = reply.readInt32();
175 if (nonNull) {
176 *outFence = new Fence;
177 reply.read(**outFence);
178 }
179 }
180 return result;
181 }
182
Dan Stoza9f3053d2014-03-06 15:14:33 -0800183 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
184 Parcel data, reply;
185 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
186 data.write(*buffer.get());
187 status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
188 if (result != NO_ERROR) {
189 return result;
190 }
191 *slot = reply.readInt32();
192 result = reply.readInt32();
193 return result;
194 }
195
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700196 virtual status_t queueBuffer(int buf,
197 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800198 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800199 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800200 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700201 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700202 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
203 if (result != NO_ERROR) {
204 return result;
205 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700206 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700207 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 return result;
209 }
210
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700211 virtual status_t cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800213 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800215 data.write(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700216 status_t result = remote()->transact(CANCEL_BUFFER, data, &reply);
217 if (result != NO_ERROR) {
218 return result;
219 }
220 result = reply.readInt32();
221 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800222 }
223
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700224 virtual int query(int what, int* value) {
225 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800226 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700227 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700228 status_t result = remote()->transact(QUERY, data, &reply);
229 if (result != NO_ERROR) {
230 return result;
231 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700232 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700233 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700234 return result;
235 }
236
Dan Stozaf0eaf252014-03-21 13:05:51 -0700237 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700238 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700239 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800240 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stozaf0eaf252014-03-21 13:05:51 -0700241 if (listener != NULL) {
242 data.writeInt32(1);
Marco Nelissen097ca272014-11-14 08:01:01 -0800243 data.writeStrongBinder(IInterface::asBinder(listener));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700244 } else {
245 data.writeInt32(0);
246 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700247 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700248 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700249 status_t result = remote()->transact(CONNECT, data, &reply);
250 if (result != NO_ERROR) {
251 return result;
252 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700253 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700254 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700255 return result;
256 }
Mathias Agopian80727112011-05-02 19:51:12 -0700257
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700258 virtual status_t disconnect(int api) {
259 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800260 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700261 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700262 status_t result =remote()->transact(DISCONNECT, data, &reply);
263 if (result != NO_ERROR) {
264 return result;
265 }
266 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700267 return result;
268 }
Jesse Hall399184a2014-03-03 15:42:54 -0800269
270 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
271 Parcel data, reply;
272 status_t result;
273 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
274 if (stream.get()) {
275 data.writeInt32(true);
276 data.writeNativeHandle(stream->handle());
277 } else {
278 data.writeInt32(false);
279 }
280 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
281 result = reply.readInt32();
282 }
283 return result;
284 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700285
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700286 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800287 PixelFormat format, uint32_t usage) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700288 Parcel data, reply;
289 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800290 data.writeUint32(width);
291 data.writeUint32(height);
Dan Stoza29a3e902014-06-20 13:13:57 -0700292 data.writeInt32(static_cast<int32_t>(format));
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800293 data.writeUint32(usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700294 status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply);
295 if (result != NO_ERROR) {
296 ALOGE("allocateBuffers failed to transact: %d", result);
297 }
298 }
Dan Stoza9de72932015-04-16 17:28:43 -0700299
300 virtual status_t allowAllocation(bool allow) {
301 Parcel data, reply;
302 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
303 data.writeInt32(static_cast<int32_t>(allow));
304 status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
305 if (result != NO_ERROR) {
306 return result;
307 }
308 result = reply.readInt32();
309 return result;
310 }
Dan Stoza812ed062015-06-02 15:45:22 -0700311
312 virtual status_t setGenerationNumber(uint32_t generationNumber) {
313 Parcel data, reply;
314 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
315 data.writeUint32(generationNumber);
316 status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
317 if (result == NO_ERROR) {
318 result = reply.readInt32();
319 }
320 return result;
321 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700322
323 virtual String8 getConsumerName() const {
324 Parcel data, reply;
325 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
326 status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
327 if (result != NO_ERROR) {
328 ALOGE("getConsumerName failed to transact: %d", result);
329 return String8("TransactFailed");
330 }
331 return reply.readString8();
332 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700333
334 virtual uint64_t getNextFrameNumber() const {
335 Parcel data, reply;
336 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
337 status_t result = remote()->transact(GET_NEXT_FRAME_NUMBER, data, &reply);
338 if (result != NO_ERROR) {
339 ALOGE("getNextFrameNumber failed to transact: %d", result);
340 return 0;
341 }
342 uint64_t frameNumber = reply.readUint64();
343 return frameNumber;
344 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700345
346 virtual status_t setSingleBufferMode(bool singleBufferMode) {
347 Parcel data, reply;
348 data.writeInterfaceToken(
349 IGraphicBufferProducer::getInterfaceDescriptor());
350 data.writeInt32(singleBufferMode);
351 status_t result = remote()->transact(SET_SINGLE_BUFFER_MODE, data,
352 &reply);
353 if (result == NO_ERROR) {
354 result = reply.readInt32();
355 }
356 return result;
357 }
Dan Stoza127fc632015-06-30 13:43:32 -0700358
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800359 virtual status_t setAutoRefresh(bool autoRefresh) {
360 Parcel data, reply;
361 data.writeInterfaceToken(
362 IGraphicBufferProducer::getInterfaceDescriptor());
363 data.writeInt32(autoRefresh);
364 status_t result = remote()->transact(SET_AUTO_REFRESH, data, &reply);
365 if (result == NO_ERROR) {
366 result = reply.readInt32();
367 }
368 return result;
369 }
370
Dan Stoza127fc632015-06-30 13:43:32 -0700371 virtual status_t setDequeueTimeout(nsecs_t timeout) {
372 Parcel data, reply;
373 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
374 data.writeInt64(timeout);
375 status_t result = remote()->transact(SET_DEQUEUE_TIMEOUT, data, &reply);
376 if (result != NO_ERROR) {
377 ALOGE("setDequeueTimeout failed to transact: %d", result);
378 return result;
379 }
380 return reply.readInt32();
381 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800382};
383
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800384// Out-of-line virtual method definition to trigger vtable emission in this
385// translation unit (see clang warning -Wweak-vtables)
386BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
387
Andy McFadden466a1922013-01-08 11:25:51 -0800388IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800389
390// ----------------------------------------------------------------------
391
Andy McFadden2adaf042012-12-18 09:49:45 -0800392status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
394{
395 switch(code) {
396 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800397 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800398 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700399 sp<GraphicBuffer> buffer;
400 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800401 reply->writeInt32(buffer != 0);
402 if (buffer != 0) {
403 reply->write(*buffer);
404 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700405 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800406 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800407 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700408 case SET_MAX_DEQUEUED_BUFFER_COUNT: {
409 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
410 int maxDequeuedBuffers = data.readInt32();
411 int result = setMaxDequeuedBufferCount(maxDequeuedBuffers);
412 reply->writeInt32(result);
413 return NO_ERROR;
414 }
415 case SET_ASYNC_MODE: {
416 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
417 bool async = data.readInt32();
418 int result = setAsyncMode(async);
419 reply->writeInt32(result);
420 return NO_ERROR;
421 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800422 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800423 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800424 uint32_t width = data.readUint32();
425 uint32_t height = data.readUint32();
426 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
427 uint32_t usage = data.readUint32();
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700428 int buf = 0;
Jesse Hallf7857542012-06-14 15:26:33 -0700429 sp<Fence> fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700430 int result = dequeueBuffer(&buf, &fence, width, height, format,
431 usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800432 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800433 reply->writeInt32(fence != NULL);
434 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700435 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700436 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800437 reply->writeInt32(result);
438 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800439 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800440 case DETACH_BUFFER: {
441 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
442 int slot = data.readInt32();
443 int result = detachBuffer(slot);
444 reply->writeInt32(result);
445 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800446 }
Dan Stozad9822a32014-03-28 15:25:31 -0700447 case DETACH_NEXT_BUFFER: {
448 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
449 sp<GraphicBuffer> buffer;
450 sp<Fence> fence;
451 int32_t result = detachNextBuffer(&buffer, &fence);
452 reply->writeInt32(result);
453 if (result == NO_ERROR) {
454 reply->writeInt32(buffer != NULL);
455 if (buffer != NULL) {
456 reply->write(*buffer);
457 }
458 reply->writeInt32(fence != NULL);
459 if (fence != NULL) {
460 reply->write(*fence);
461 }
462 }
463 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800464 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800465 case ATTACH_BUFFER: {
466 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
467 sp<GraphicBuffer> buffer = new GraphicBuffer();
468 data.read(*buffer.get());
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700469 int slot = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800470 int result = attachBuffer(&slot, buffer);
471 reply->writeInt32(slot);
472 reply->writeInt32(result);
473 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800474 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800475 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800476 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800477 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700478 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700479 QueueBufferOutput* const output =
480 reinterpret_cast<QueueBufferOutput *>(
481 reply->writeInplace(sizeof(QueueBufferOutput)));
Robert Shihd06421f2016-01-11 15:02:12 -0800482 memset(output, 0, sizeof(QueueBufferOutput));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700483 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800484 reply->writeInt32(result);
485 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800486 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800487 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800488 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800490 sp<Fence> fence = new Fence();
491 data.read(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700492 status_t result = cancelBuffer(buf, fence);
493 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800495 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700496 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700498 int value = 0;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700499 int what = data.readInt32();
500 int res = query(what, &value);
501 reply->writeInt32(value);
502 reply->writeInt32(res);
503 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800504 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700505 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700507 sp<IProducerListener> listener;
508 if (data.readInt32() == 1) {
509 listener = IProducerListener::asInterface(data.readStrongBinder());
510 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700511 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700512 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700513 QueueBufferOutput* const output =
514 reinterpret_cast<QueueBufferOutput *>(
515 reply->writeInplace(sizeof(QueueBufferOutput)));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700516 status_t res = connect(listener, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700517 reply->writeInt32(res);
518 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800519 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700520 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800521 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700522 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700523 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700524 reply->writeInt32(res);
525 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800526 }
Jesse Hall399184a2014-03-03 15:42:54 -0800527 case SET_SIDEBAND_STREAM: {
528 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
529 sp<NativeHandle> stream;
530 if (data.readInt32()) {
Wonsik Kim0ec54e12014-03-21 10:46:24 +0900531 stream = NativeHandle::create(data.readNativeHandle(), true);
Jesse Hall399184a2014-03-03 15:42:54 -0800532 }
533 status_t result = setSidebandStream(stream);
534 reply->writeInt32(result);
535 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800536 }
Dan Stoza9de72932015-04-16 17:28:43 -0700537 case ALLOCATE_BUFFERS: {
Dan Stoza29a3e902014-06-20 13:13:57 -0700538 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800539 uint32_t width = data.readUint32();
540 uint32_t height = data.readUint32();
541 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
542 uint32_t usage = data.readUint32();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700543 allocateBuffers(width, height, format, usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700544 return NO_ERROR;
Dan Stoza9de72932015-04-16 17:28:43 -0700545 }
546 case ALLOW_ALLOCATION: {
547 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
548 bool allow = static_cast<bool>(data.readInt32());
549 status_t result = allowAllocation(allow);
550 reply->writeInt32(result);
551 return NO_ERROR;
552 }
Dan Stoza812ed062015-06-02 15:45:22 -0700553 case SET_GENERATION_NUMBER: {
554 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
555 uint32_t generationNumber = data.readUint32();
556 status_t result = setGenerationNumber(generationNumber);
557 reply->writeInt32(result);
558 return NO_ERROR;
559 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700560 case GET_CONSUMER_NAME: {
561 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
562 reply->writeString8(getConsumerName());
563 return NO_ERROR;
564 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700565 case GET_NEXT_FRAME_NUMBER: {
566 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
567 uint64_t frameNumber = getNextFrameNumber();
568 reply->writeUint64(frameNumber);
569 return NO_ERROR;
570 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700571 case SET_SINGLE_BUFFER_MODE: {
572 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
573 bool singleBufferMode = data.readInt32();
574 status_t result = setSingleBufferMode(singleBufferMode);
575 reply->writeInt32(result);
576 return NO_ERROR;
577 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800578 case SET_AUTO_REFRESH: {
579 CHECK_INTERFACE(IGraphicBuffer, data, reply);
580 bool autoRefresh = data.readInt32();
581 status_t result = setAutoRefresh(autoRefresh);
582 reply->writeInt32(result);
583 return NO_ERROR;
584 }
Dan Stoza127fc632015-06-30 13:43:32 -0700585 case SET_DEQUEUE_TIMEOUT: {
586 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
587 nsecs_t timeout = data.readInt64();
588 status_t result = setDequeueTimeout(timeout);
589 reply->writeInt32(result);
590 return NO_ERROR;
591 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800592 }
593 return BBinder::onTransact(code, data, reply, flags);
594}
595
596// ----------------------------------------------------------------------------
597
Andy McFadden2adaf042012-12-18 09:49:45 -0800598IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700599 parcel.read(*this);
600}
601
Mathias Agopiane1424282013-07-29 21:24:40 -0700602size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700603 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700604 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800605 + sizeof(dataSpace)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700606 + sizeof(crop)
607 + sizeof(scalingMode)
608 + sizeof(transform)
Ruben Brunk1681d952014-06-27 15:51:55 -0700609 + sizeof(stickyTransform)
Dan Stoza5065a552015-03-17 16:23:42 -0700610 + fence->getFlattenedSize()
611 + surfaceDamage.getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700612}
613
Mathias Agopiane1424282013-07-29 21:24:40 -0700614size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800615 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700616}
617
Mathias Agopiane1424282013-07-29 21:24:40 -0700618status_t IGraphicBufferProducer::QueueBufferInput::flatten(
619 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700620{
Mathias Agopiane1424282013-07-29 21:24:40 -0700621 if (size < getFlattenedSize()) {
622 return NO_MEMORY;
623 }
624 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700625 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800626 FlattenableUtils::write(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700627 FlattenableUtils::write(buffer, size, crop);
628 FlattenableUtils::write(buffer, size, scalingMode);
629 FlattenableUtils::write(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700630 FlattenableUtils::write(buffer, size, stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700631 status_t result = fence->flatten(buffer, size, fds, count);
632 if (result != NO_ERROR) {
633 return result;
634 }
635 return surfaceDamage.flatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700636}
637
Mathias Agopiane1424282013-07-29 21:24:40 -0700638status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
639 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700640{
Mathias Agopiane1424282013-07-29 21:24:40 -0700641 size_t minNeeded =
642 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700643 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800644 + sizeof(dataSpace)
Mathias Agopiane1424282013-07-29 21:24:40 -0700645 + sizeof(crop)
646 + sizeof(scalingMode)
647 + sizeof(transform)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700648 + sizeof(stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700649
650 if (size < minNeeded) {
651 return NO_MEMORY;
652 }
653
654 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700655 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800656 FlattenableUtils::read(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700657 FlattenableUtils::read(buffer, size, crop);
658 FlattenableUtils::read(buffer, size, scalingMode);
659 FlattenableUtils::read(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700660 FlattenableUtils::read(buffer, size, stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700661
Jamie Gennis1df8c342012-12-20 14:05:45 -0800662 fence = new Fence();
Dan Stoza5065a552015-03-17 16:23:42 -0700663 status_t result = fence->unflatten(buffer, size, fds, count);
664 if (result != NO_ERROR) {
665 return result;
666 }
667 return surfaceDamage.unflatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700668}
669
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800670}; // namespace android