blob: c36fcadf9fff9f6a297960b0bdea50c02ff540c1 [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,
Pablo Ceballos3559fbf2016-03-17 15:50:23 -070054 SET_SHARED_BUFFER_MODE,
Pablo Ceballosff95aab2016-01-13 17:09:58 -080055 SET_AUTO_REFRESH,
Dan Stoza127fc632015-06-30 13:43:32 -070056 SET_DEQUEUE_TIMEOUT,
Dan Stoza50101d02016-04-07 16:53:23 -070057 GET_LAST_QUEUED_BUFFER,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080058};
59
Andy McFadden2adaf042012-12-18 09:49:45 -080060class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061{
62public:
Andy McFadden2adaf042012-12-18 09:49:45 -080063 BpGraphicBufferProducer(const sp<IBinder>& impl)
64 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080065 {
66 }
67
Dan Stoza3be1c6b2014-11-18 10:24:03 -080068 virtual ~BpGraphicBufferProducer();
69
Jamie Gennis7b305ff2011-07-19 12:08:33 -070070 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080071 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080072 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070074 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
75 if (result != NO_ERROR) {
76 return result;
77 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080078 bool nonNull = reply.readInt32();
79 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070080 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080081 result = reply.read(**buf);
82 if(result != NO_ERROR) {
83 (*buf).clear();
84 return result;
85 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080086 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070087 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070088 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080089 }
90
Pablo Ceballosfa455352015-08-12 17:47:47 -070091 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
92 Parcel data, reply;
93 data.writeInterfaceToken(
94 IGraphicBufferProducer::getInterfaceDescriptor());
95 data.writeInt32(maxDequeuedBuffers);
96 status_t result = remote()->transact(SET_MAX_DEQUEUED_BUFFER_COUNT,
97 data, &reply);
98 if (result != NO_ERROR) {
99 return result;
100 }
101 result = reply.readInt32();
102 return result;
103 }
104
105 virtual status_t setAsyncMode(bool async) {
106 Parcel data, reply;
107 data.writeInterfaceToken(
108 IGraphicBufferProducer::getInterfaceDescriptor());
109 data.writeInt32(async);
110 status_t result = remote()->transact(SET_ASYNC_MODE,
111 data, &reply);
112 if (result != NO_ERROR) {
113 return result;
114 }
115 result = reply.readInt32();
116 return result;
117 }
118
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700119 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, uint32_t width,
120 uint32_t height, PixelFormat format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800121 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800122 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800123 data.writeUint32(width);
124 data.writeUint32(height);
125 data.writeInt32(static_cast<int32_t>(format));
126 data.writeUint32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700127 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
128 if (result != NO_ERROR) {
129 return result;
130 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700132 bool nonNull = reply.readInt32();
133 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700134 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700135 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700136 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700137 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800138 return result;
139 }
140
Dan Stoza9f3053d2014-03-06 15:14:33 -0800141 virtual status_t detachBuffer(int slot) {
142 Parcel data, reply;
143 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
144 data.writeInt32(slot);
145 status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
146 if (result != NO_ERROR) {
147 return result;
148 }
149 result = reply.readInt32();
150 return result;
151 }
152
Dan Stozad9822a32014-03-28 15:25:31 -0700153 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
154 sp<Fence>* outFence) {
155 if (outBuffer == NULL) {
156 ALOGE("detachNextBuffer: outBuffer must not be NULL");
157 return BAD_VALUE;
158 } else if (outFence == NULL) {
159 ALOGE("detachNextBuffer: outFence must not be NULL");
160 return BAD_VALUE;
161 }
162 Parcel data, reply;
163 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
164 status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
165 if (result != NO_ERROR) {
166 return result;
167 }
168 result = reply.readInt32();
169 if (result == NO_ERROR) {
170 bool nonNull = reply.readInt32();
171 if (nonNull) {
172 *outBuffer = new GraphicBuffer;
173 reply.read(**outBuffer);
174 }
175 nonNull = reply.readInt32();
176 if (nonNull) {
177 *outFence = new Fence;
178 reply.read(**outFence);
179 }
180 }
181 return result;
182 }
183
Dan Stoza9f3053d2014-03-06 15:14:33 -0800184 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
185 Parcel data, reply;
186 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
187 data.write(*buffer.get());
188 status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
189 if (result != NO_ERROR) {
190 return result;
191 }
192 *slot = reply.readInt32();
193 result = reply.readInt32();
194 return result;
195 }
196
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700197 virtual status_t queueBuffer(int buf,
198 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800199 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800200 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700202 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700203 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
204 if (result != NO_ERROR) {
205 return result;
206 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700207 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700208 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800209 return result;
210 }
211
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700212 virtual status_t cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800214 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800216 data.write(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700217 status_t result = remote()->transact(CANCEL_BUFFER, data, &reply);
218 if (result != NO_ERROR) {
219 return result;
220 }
221 result = reply.readInt32();
222 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800223 }
224
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700225 virtual int query(int what, int* value) {
226 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800227 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700228 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700229 status_t result = remote()->transact(QUERY, data, &reply);
230 if (result != NO_ERROR) {
231 return result;
232 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700233 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700234 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700235 return result;
236 }
237
Dan Stozaf0eaf252014-03-21 13:05:51 -0700238 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700239 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700240 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800241 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stozaf0eaf252014-03-21 13:05:51 -0700242 if (listener != NULL) {
243 data.writeInt32(1);
Marco Nelissen097ca272014-11-14 08:01:01 -0800244 data.writeStrongBinder(IInterface::asBinder(listener));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700245 } else {
246 data.writeInt32(0);
247 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700248 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700249 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700250 status_t result = remote()->transact(CONNECT, data, &reply);
251 if (result != NO_ERROR) {
252 return result;
253 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700254 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700255 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700256 return result;
257 }
Mathias Agopian80727112011-05-02 19:51:12 -0700258
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700259 virtual status_t disconnect(int api) {
260 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800261 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700262 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700263 status_t result =remote()->transact(DISCONNECT, data, &reply);
264 if (result != NO_ERROR) {
265 return result;
266 }
267 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700268 return result;
269 }
Jesse Hall399184a2014-03-03 15:42:54 -0800270
271 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
272 Parcel data, reply;
273 status_t result;
274 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
275 if (stream.get()) {
276 data.writeInt32(true);
277 data.writeNativeHandle(stream->handle());
278 } else {
279 data.writeInt32(false);
280 }
281 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
282 result = reply.readInt32();
283 }
284 return result;
285 }
Dan Stoza29a3e902014-06-20 13:13:57 -0700286
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700287 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800288 PixelFormat format, uint32_t usage) {
Dan Stoza29a3e902014-06-20 13:13:57 -0700289 Parcel data, reply;
290 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800291 data.writeUint32(width);
292 data.writeUint32(height);
Dan Stoza29a3e902014-06-20 13:13:57 -0700293 data.writeInt32(static_cast<int32_t>(format));
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800294 data.writeUint32(usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700295 status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply);
296 if (result != NO_ERROR) {
297 ALOGE("allocateBuffers failed to transact: %d", result);
298 }
299 }
Dan Stoza9de72932015-04-16 17:28:43 -0700300
301 virtual status_t allowAllocation(bool allow) {
302 Parcel data, reply;
303 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
304 data.writeInt32(static_cast<int32_t>(allow));
305 status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
306 if (result != NO_ERROR) {
307 return result;
308 }
309 result = reply.readInt32();
310 return result;
311 }
Dan Stoza812ed062015-06-02 15:45:22 -0700312
313 virtual status_t setGenerationNumber(uint32_t generationNumber) {
314 Parcel data, reply;
315 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
316 data.writeUint32(generationNumber);
317 status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
318 if (result == NO_ERROR) {
319 result = reply.readInt32();
320 }
321 return result;
322 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700323
324 virtual String8 getConsumerName() const {
325 Parcel data, reply;
326 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
327 status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
328 if (result != NO_ERROR) {
329 ALOGE("getConsumerName failed to transact: %d", result);
330 return String8("TransactFailed");
331 }
332 return reply.readString8();
333 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700334
335 virtual uint64_t getNextFrameNumber() const {
336 Parcel data, reply;
337 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
338 status_t result = remote()->transact(GET_NEXT_FRAME_NUMBER, data, &reply);
339 if (result != NO_ERROR) {
340 ALOGE("getNextFrameNumber failed to transact: %d", result);
341 return 0;
342 }
343 uint64_t frameNumber = reply.readUint64();
344 return frameNumber;
345 }
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700346
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700347 virtual status_t setSharedBufferMode(bool sharedBufferMode) {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700348 Parcel data, reply;
349 data.writeInterfaceToken(
350 IGraphicBufferProducer::getInterfaceDescriptor());
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700351 data.writeInt32(sharedBufferMode);
352 status_t result = remote()->transact(SET_SHARED_BUFFER_MODE, data,
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700353 &reply);
354 if (result == NO_ERROR) {
355 result = reply.readInt32();
356 }
357 return result;
358 }
Dan Stoza127fc632015-06-30 13:43:32 -0700359
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800360 virtual status_t setAutoRefresh(bool autoRefresh) {
361 Parcel data, reply;
362 data.writeInterfaceToken(
363 IGraphicBufferProducer::getInterfaceDescriptor());
364 data.writeInt32(autoRefresh);
365 status_t result = remote()->transact(SET_AUTO_REFRESH, data, &reply);
366 if (result == NO_ERROR) {
367 result = reply.readInt32();
368 }
369 return result;
370 }
371
Dan Stoza127fc632015-06-30 13:43:32 -0700372 virtual status_t setDequeueTimeout(nsecs_t timeout) {
373 Parcel data, reply;
374 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
375 data.writeInt64(timeout);
376 status_t result = remote()->transact(SET_DEQUEUE_TIMEOUT, data, &reply);
377 if (result != NO_ERROR) {
378 ALOGE("setDequeueTimeout failed to transact: %d", result);
379 return result;
380 }
381 return reply.readInt32();
382 }
Dan Stoza50101d02016-04-07 16:53:23 -0700383
384 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
385 sp<Fence>* outFence) override {
386 Parcel data, reply;
387 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
388 status_t result = remote()->transact(GET_LAST_QUEUED_BUFFER, data,
389 &reply);
390 if (result != NO_ERROR) {
391 ALOGE("getLastQueuedBuffer failed to transact: %d", result);
392 return result;
393 }
394 result = reply.readInt32();
395 if (result != NO_ERROR) {
396 return result;
397 }
398 sp<GraphicBuffer> buffer(new GraphicBuffer);
399 result = reply.read(*buffer);
400 if (result != NO_ERROR) {
401 ALOGE("getLastQueuedBuffer failed to read buffer: %d", result);
402 return result;
403 }
404 sp<Fence> fence(new Fence);
405 result = reply.read(*fence);
406 if (result != NO_ERROR) {
407 ALOGE("getLastQueuedBuffer failed to read fence: %d", result);
408 return result;
409 }
410 *outBuffer = buffer;
411 *outFence = fence;
412 return result;
413 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800414};
415
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800416// Out-of-line virtual method definition to trigger vtable emission in this
417// translation unit (see clang warning -Wweak-vtables)
418BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
419
Andy McFadden466a1922013-01-08 11:25:51 -0800420IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421
422// ----------------------------------------------------------------------
423
Andy McFadden2adaf042012-12-18 09:49:45 -0800424status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800425 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
426{
427 switch(code) {
428 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800429 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800430 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700431 sp<GraphicBuffer> buffer;
432 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800433 reply->writeInt32(buffer != 0);
434 if (buffer != 0) {
435 reply->write(*buffer);
436 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700437 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800438 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800439 }
Pablo Ceballosfa455352015-08-12 17:47:47 -0700440 case SET_MAX_DEQUEUED_BUFFER_COUNT: {
441 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
442 int maxDequeuedBuffers = data.readInt32();
443 int result = setMaxDequeuedBufferCount(maxDequeuedBuffers);
444 reply->writeInt32(result);
445 return NO_ERROR;
446 }
447 case SET_ASYNC_MODE: {
448 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
449 bool async = data.readInt32();
450 int result = setAsyncMode(async);
451 reply->writeInt32(result);
452 return NO_ERROR;
453 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800454 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800455 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800456 uint32_t width = data.readUint32();
457 uint32_t height = data.readUint32();
458 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
459 uint32_t usage = data.readUint32();
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700460 int buf = 0;
Jesse Hallf7857542012-06-14 15:26:33 -0700461 sp<Fence> fence;
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700462 int result = dequeueBuffer(&buf, &fence, width, height, format,
463 usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800464 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800465 reply->writeInt32(fence != NULL);
466 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700467 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700468 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800469 reply->writeInt32(result);
470 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800471 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800472 case DETACH_BUFFER: {
473 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
474 int slot = data.readInt32();
475 int result = detachBuffer(slot);
476 reply->writeInt32(result);
477 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800478 }
Dan Stozad9822a32014-03-28 15:25:31 -0700479 case DETACH_NEXT_BUFFER: {
480 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
481 sp<GraphicBuffer> buffer;
482 sp<Fence> fence;
483 int32_t result = detachNextBuffer(&buffer, &fence);
484 reply->writeInt32(result);
485 if (result == NO_ERROR) {
486 reply->writeInt32(buffer != NULL);
487 if (buffer != NULL) {
488 reply->write(*buffer);
489 }
490 reply->writeInt32(fence != NULL);
491 if (fence != NULL) {
492 reply->write(*fence);
493 }
494 }
495 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800496 }
Dan Stoza9f3053d2014-03-06 15:14:33 -0800497 case ATTACH_BUFFER: {
498 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
499 sp<GraphicBuffer> buffer = new GraphicBuffer();
500 data.read(*buffer.get());
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700501 int slot = 0;
Dan Stoza9f3053d2014-03-06 15:14:33 -0800502 int result = attachBuffer(&slot, buffer);
503 reply->writeInt32(slot);
504 reply->writeInt32(result);
505 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800506 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800508 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800509 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700510 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700511 QueueBufferOutput* const output =
512 reinterpret_cast<QueueBufferOutput *>(
513 reply->writeInplace(sizeof(QueueBufferOutput)));
Robert Shihd06421f2016-01-11 15:02:12 -0800514 memset(output, 0, sizeof(QueueBufferOutput));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700515 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800516 reply->writeInt32(result);
517 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800518 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800519 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800520 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800521 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800522 sp<Fence> fence = new Fence();
523 data.read(*fence.get());
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700524 status_t result = cancelBuffer(buf, fence);
525 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800526 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800527 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700528 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800529 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Naveen Leekha12ba0f52015-09-21 17:28:04 -0700530 int value = 0;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700531 int what = data.readInt32();
532 int res = query(what, &value);
533 reply->writeInt32(value);
534 reply->writeInt32(res);
535 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800536 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700537 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800538 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stozaf0eaf252014-03-21 13:05:51 -0700539 sp<IProducerListener> listener;
540 if (data.readInt32() == 1) {
541 listener = IProducerListener::asInterface(data.readStrongBinder());
542 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700543 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700544 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700545 QueueBufferOutput* const output =
546 reinterpret_cast<QueueBufferOutput *>(
547 reply->writeInplace(sizeof(QueueBufferOutput)));
Pablo Ceballos93c617f2016-03-15 18:10:49 -0700548 memset(output, 0, sizeof(QueueBufferOutput));
Dan Stozaf0eaf252014-03-21 13:05:51 -0700549 status_t res = connect(listener, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700550 reply->writeInt32(res);
551 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800552 }
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700553 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800554 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700555 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700556 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700557 reply->writeInt32(res);
558 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800559 }
Jesse Hall399184a2014-03-03 15:42:54 -0800560 case SET_SIDEBAND_STREAM: {
561 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
562 sp<NativeHandle> stream;
563 if (data.readInt32()) {
Wonsik Kim0ec54e12014-03-21 10:46:24 +0900564 stream = NativeHandle::create(data.readNativeHandle(), true);
Jesse Hall399184a2014-03-03 15:42:54 -0800565 }
566 status_t result = setSidebandStream(stream);
567 reply->writeInt32(result);
568 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800569 }
Dan Stoza9de72932015-04-16 17:28:43 -0700570 case ALLOCATE_BUFFERS: {
Dan Stoza29a3e902014-06-20 13:13:57 -0700571 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800572 uint32_t width = data.readUint32();
573 uint32_t height = data.readUint32();
574 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
575 uint32_t usage = data.readUint32();
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700576 allocateBuffers(width, height, format, usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700577 return NO_ERROR;
Dan Stoza9de72932015-04-16 17:28:43 -0700578 }
579 case ALLOW_ALLOCATION: {
580 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
581 bool allow = static_cast<bool>(data.readInt32());
582 status_t result = allowAllocation(allow);
583 reply->writeInt32(result);
584 return NO_ERROR;
585 }
Dan Stoza812ed062015-06-02 15:45:22 -0700586 case SET_GENERATION_NUMBER: {
587 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
588 uint32_t generationNumber = data.readUint32();
589 status_t result = setGenerationNumber(generationNumber);
590 reply->writeInt32(result);
591 return NO_ERROR;
592 }
Dan Stozac6f30bd2015-06-08 09:32:50 -0700593 case GET_CONSUMER_NAME: {
594 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
595 reply->writeString8(getConsumerName());
596 return NO_ERROR;
597 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700598 case GET_NEXT_FRAME_NUMBER: {
599 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
600 uint64_t frameNumber = getNextFrameNumber();
601 reply->writeUint64(frameNumber);
602 return NO_ERROR;
603 }
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700604 case SET_SHARED_BUFFER_MODE: {
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700605 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700606 bool sharedBufferMode = data.readInt32();
607 status_t result = setSharedBufferMode(sharedBufferMode);
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700608 reply->writeInt32(result);
609 return NO_ERROR;
610 }
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800611 case SET_AUTO_REFRESH: {
612 CHECK_INTERFACE(IGraphicBuffer, data, reply);
613 bool autoRefresh = data.readInt32();
614 status_t result = setAutoRefresh(autoRefresh);
615 reply->writeInt32(result);
616 return NO_ERROR;
617 }
Dan Stoza127fc632015-06-30 13:43:32 -0700618 case SET_DEQUEUE_TIMEOUT: {
619 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
620 nsecs_t timeout = data.readInt64();
621 status_t result = setDequeueTimeout(timeout);
622 reply->writeInt32(result);
623 return NO_ERROR;
624 }
Dan Stoza50101d02016-04-07 16:53:23 -0700625 case GET_LAST_QUEUED_BUFFER: {
626 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
627 sp<GraphicBuffer> buffer(nullptr);
628 sp<Fence> fence(Fence::NO_FENCE);
629 status_t result = getLastQueuedBuffer(&buffer, &fence);
630 reply->writeInt32(result);
631 if (result != NO_ERROR) {
632 return result;
633 }
634 result = reply->write(*buffer);
635 if (result != NO_ERROR) {
636 ALOGE("getLastQueuedBuffer failed to write buffer: %d", result);
637 return result;
638 }
639 result = reply->write(*fence);
640 if (result != NO_ERROR) {
641 ALOGE("getLastQueuedBuffer failed to write fence: %d", result);
642 return result;
643 }
644 return NO_ERROR;
645 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800646 }
647 return BBinder::onTransact(code, data, reply, flags);
648}
649
650// ----------------------------------------------------------------------------
651
Andy McFadden2adaf042012-12-18 09:49:45 -0800652IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700653 parcel.read(*this);
654}
655
Mathias Agopiane1424282013-07-29 21:24:40 -0700656size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700657 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700658 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800659 + sizeof(dataSpace)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700660 + sizeof(crop)
661 + sizeof(scalingMode)
662 + sizeof(transform)
Ruben Brunk1681d952014-06-27 15:51:55 -0700663 + sizeof(stickyTransform)
Dan Stoza5065a552015-03-17 16:23:42 -0700664 + fence->getFlattenedSize()
665 + surfaceDamage.getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700666}
667
Mathias Agopiane1424282013-07-29 21:24:40 -0700668size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800669 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700670}
671
Mathias Agopiane1424282013-07-29 21:24:40 -0700672status_t IGraphicBufferProducer::QueueBufferInput::flatten(
673 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700674{
Mathias Agopiane1424282013-07-29 21:24:40 -0700675 if (size < getFlattenedSize()) {
676 return NO_MEMORY;
677 }
678 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700679 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800680 FlattenableUtils::write(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700681 FlattenableUtils::write(buffer, size, crop);
682 FlattenableUtils::write(buffer, size, scalingMode);
683 FlattenableUtils::write(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700684 FlattenableUtils::write(buffer, size, stickyTransform);
Dan Stoza5065a552015-03-17 16:23:42 -0700685 status_t result = fence->flatten(buffer, size, fds, count);
686 if (result != NO_ERROR) {
687 return result;
688 }
689 return surfaceDamage.flatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700690}
691
Mathias Agopiane1424282013-07-29 21:24:40 -0700692status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
693 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700694{
Mathias Agopiane1424282013-07-29 21:24:40 -0700695 size_t minNeeded =
696 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700697 + sizeof(isAutoTimestamp)
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800698 + sizeof(dataSpace)
Mathias Agopiane1424282013-07-29 21:24:40 -0700699 + sizeof(crop)
700 + sizeof(scalingMode)
701 + sizeof(transform)
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700702 + sizeof(stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700703
704 if (size < minNeeded) {
705 return NO_MEMORY;
706 }
707
708 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700709 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800710 FlattenableUtils::read(buffer, size, dataSpace);
Mathias Agopiane1424282013-07-29 21:24:40 -0700711 FlattenableUtils::read(buffer, size, crop);
712 FlattenableUtils::read(buffer, size, scalingMode);
713 FlattenableUtils::read(buffer, size, transform);
Ruben Brunk1681d952014-06-27 15:51:55 -0700714 FlattenableUtils::read(buffer, size, stickyTransform);
Mathias Agopiane1424282013-07-29 21:24:40 -0700715
Jamie Gennis1df8c342012-12-20 14:05:45 -0800716 fence = new Fence();
Dan Stoza5065a552015-03-17 16:23:42 -0700717 status_t result = fence->unflatten(buffer, size, fds, count);
718 if (result != NO_ERROR) {
719 return result;
720 }
721 return surfaceDamage.unflatten(buffer, size);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700722}
723
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800724}; // namespace android