blob: 48b28708f77865282c67f6fe2e45308658b19d5c [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>
21#include <utils/RefBase.h>
22#include <utils/Vector.h>
23#include <utils/Timers.h>
24
25#include <binder/Parcel.h>
26#include <binder/IInterface.h>
27
Andy McFadden2adaf042012-12-18 09:49:45 -080028#include <gui/IGraphicBufferProducer.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
30namespace android {
31// ----------------------------------------------------------------------------
32
33enum {
34 REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
35 SET_BUFFER_COUNT,
36 DEQUEUE_BUFFER,
37 QUEUE_BUFFER,
38 CANCEL_BUFFER,
Mathias Agopianeafabcd2011-04-20 14:20:59 -070039 QUERY,
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070040 CONNECT,
41 DISCONNECT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080042};
43
44
Andy McFadden2adaf042012-12-18 09:49:45 -080045class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080046{
47public:
Andy McFadden2adaf042012-12-18 09:49:45 -080048 BpGraphicBufferProducer(const sp<IBinder>& impl)
49 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080050 {
51 }
52
Jamie Gennis7b305ff2011-07-19 12:08:33 -070053 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080054 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080055 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080056 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070057 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
58 if (result != NO_ERROR) {
59 return result;
60 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061 bool nonNull = reply.readInt32();
62 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070063 *buf = new GraphicBuffer();
64 reply.read(**buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080065 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070066 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070067 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080068 }
69
70 virtual status_t setBufferCount(int bufferCount)
71 {
72 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080073 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080074 data.writeInt32(bufferCount);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070075 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
76 if (result != NO_ERROR) {
77 return result;
78 }
79 result = reply.readInt32();
80 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080081 }
82
Mathias Agopian7cdd7862013-07-18 22:10:56 -070083 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -070084 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080086 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian7cdd7862013-07-18 22:10:56 -070087 data.writeInt32(async);
Mathias Agopianc04f1532011-04-25 20:22:14 -070088 data.writeInt32(w);
89 data.writeInt32(h);
90 data.writeInt32(format);
91 data.writeInt32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070092 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
93 if (result != NO_ERROR) {
94 return result;
95 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080096 *buf = reply.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -080097 bool fenceWasWritten = reply.readInt32();
98 if (fenceWasWritten) {
99 // If the fence was written by the callee, then overwrite the
100 // caller's fence here. If it wasn't written then don't touch the
101 // caller's fence.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700102 *fence = new Fence();
103 reply.read(*(fence->get()));
Jesse Hallf7857542012-06-14 15:26:33 -0700104 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700105 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800106 return result;
107 }
108
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700109 virtual status_t queueBuffer(int buf,
110 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800111 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800112 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800113 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700114 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700115 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
116 if (result != NO_ERROR) {
117 return result;
118 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700119 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700120 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800121 return result;
122 }
123
Jesse Hall4c00cc12013-03-15 21:34:30 -0700124 virtual void cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800125 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800126 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800127 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800128 data.write(*fence.get());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 remote()->transact(CANCEL_BUFFER, data, &reply);
130 }
131
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700132 virtual int query(int what, int* value) {
133 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800134 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700135 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700136 status_t result = remote()->transact(QUERY, data, &reply);
137 if (result != NO_ERROR) {
138 return result;
139 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700140 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700141 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700142 return result;
143 }
144
Mathias Agopian595264f2013-07-16 22:56:09 -0700145 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700146 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800147 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700148 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700149 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700150 status_t result = remote()->transact(CONNECT, data, &reply);
151 if (result != NO_ERROR) {
152 return result;
153 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700154 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700155 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700156 return result;
157 }
Mathias Agopian80727112011-05-02 19:51:12 -0700158
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700159 virtual status_t disconnect(int api) {
160 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800161 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700162 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700163 status_t result =remote()->transact(DISCONNECT, data, &reply);
164 if (result != NO_ERROR) {
165 return result;
166 }
167 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700168 return result;
169 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800170};
171
Andy McFadden466a1922013-01-08 11:25:51 -0800172IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173
174// ----------------------------------------------------------------------
175
Andy McFadden2adaf042012-12-18 09:49:45 -0800176status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800177 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
178{
179 switch(code) {
180 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800181 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800182 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700183 sp<GraphicBuffer> buffer;
184 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800185 reply->writeInt32(buffer != 0);
186 if (buffer != 0) {
187 reply->write(*buffer);
188 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700189 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800190 return NO_ERROR;
191 } break;
192 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800193 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 int bufferCount = data.readInt32();
195 int result = setBufferCount(bufferCount);
196 reply->writeInt32(result);
197 return NO_ERROR;
198 } break;
199 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800200 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700201 bool async = data.readInt32();
Mathias Agopianc04f1532011-04-25 20:22:14 -0700202 uint32_t w = data.readInt32();
203 uint32_t h = data.readInt32();
204 uint32_t format = data.readInt32();
205 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700207 sp<Fence> fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700208 int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800209 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800210 reply->writeInt32(fence != NULL);
211 if (fence != NULL) {
Jesse Hallf7857542012-06-14 15:26:33 -0700212 reply->write(*fence.get());
213 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 reply->writeInt32(result);
215 return NO_ERROR;
216 } break;
217 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800218 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700220 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700221 QueueBufferOutput* const output =
222 reinterpret_cast<QueueBufferOutput *>(
223 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700224 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800225 reply->writeInt32(result);
226 return NO_ERROR;
227 } break;
228 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800229 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800230 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800231 sp<Fence> fence = new Fence();
232 data.read(*fence.get());
Jesse Hallc777b0b2012-06-28 12:52:05 -0700233 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 return NO_ERROR;
235 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700236 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800237 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700238 int value;
239 int what = data.readInt32();
240 int res = query(what, &value);
241 reply->writeInt32(value);
242 reply->writeInt32(res);
243 return NO_ERROR;
244 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700245 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800246 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700247 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700248 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700249 QueueBufferOutput* const output =
250 reinterpret_cast<QueueBufferOutput *>(
251 reply->writeInplace(sizeof(QueueBufferOutput)));
Mathias Agopian595264f2013-07-16 22:56:09 -0700252 status_t res = connect(api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700253 reply->writeInt32(res);
254 return NO_ERROR;
255 } break;
256 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800257 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700258 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700259 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700260 reply->writeInt32(res);
261 return NO_ERROR;
262 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800263 }
264 return BBinder::onTransact(code, data, reply, flags);
265}
266
267// ----------------------------------------------------------------------------
268
Andy McFadden2adaf042012-12-18 09:49:45 -0800269IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700270 parcel.read(*this);
271}
272
Mathias Agopiane1424282013-07-29 21:24:40 -0700273size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700274 return sizeof(timestamp)
275 + sizeof(crop)
276 + sizeof(scalingMode)
277 + sizeof(transform)
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700278 + sizeof(async)
Jamie Gennis1df8c342012-12-20 14:05:45 -0800279 + fence->getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700280}
281
Mathias Agopiane1424282013-07-29 21:24:40 -0700282size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800283 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700284}
285
Mathias Agopiane1424282013-07-29 21:24:40 -0700286status_t IGraphicBufferProducer::QueueBufferInput::flatten(
287 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700288{
Mathias Agopiane1424282013-07-29 21:24:40 -0700289 if (size < getFlattenedSize()) {
290 return NO_MEMORY;
291 }
292 FlattenableUtils::write(buffer, size, timestamp);
293 FlattenableUtils::write(buffer, size, crop);
294 FlattenableUtils::write(buffer, size, scalingMode);
295 FlattenableUtils::write(buffer, size, transform);
296 FlattenableUtils::write(buffer, size, async);
297 return fence->flatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700298}
299
Mathias Agopiane1424282013-07-29 21:24:40 -0700300status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
301 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700302{
Mathias Agopiane1424282013-07-29 21:24:40 -0700303 size_t minNeeded =
304 sizeof(timestamp)
305 + sizeof(crop)
306 + sizeof(scalingMode)
307 + sizeof(transform)
308 + sizeof(async);
309
310 if (size < minNeeded) {
311 return NO_MEMORY;
312 }
313
314 FlattenableUtils::read(buffer, size, timestamp);
315 FlattenableUtils::read(buffer, size, crop);
316 FlattenableUtils::read(buffer, size, scalingMode);
317 FlattenableUtils::read(buffer, size, transform);
318 FlattenableUtils::read(buffer, size, async);
319
Jamie Gennis1df8c342012-12-20 14:05:45 -0800320 fence = new Fence();
Mathias Agopiane1424282013-07-29 21:24:40 -0700321 return fence->unflatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700322}
323
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800324}; // namespace android