blob: 704fa50c8d6b525ee6f9b3a6a1d9758c46fb3c19 [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();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080064 result = reply.read(**buf);
65 if(result != NO_ERROR) {
66 (*buf).clear();
67 return result;
68 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070070 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070071 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080072 }
73
74 virtual status_t setBufferCount(int bufferCount)
75 {
76 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080077 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080078 data.writeInt32(bufferCount);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070079 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
80 if (result != NO_ERROR) {
81 return result;
82 }
83 result = reply.readInt32();
84 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085 }
86
Mathias Agopian7cdd7862013-07-18 22:10:56 -070087 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -070088 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080089 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080090 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian7cdd7862013-07-18 22:10:56 -070091 data.writeInt32(async);
Mathias Agopianc04f1532011-04-25 20:22:14 -070092 data.writeInt32(w);
93 data.writeInt32(h);
94 data.writeInt32(format);
95 data.writeInt32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070096 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
97 if (result != NO_ERROR) {
98 return result;
99 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700101 bool nonNull = reply.readInt32();
102 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700103 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700104 reply.read(**fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700105 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700106 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 return result;
108 }
109
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700110 virtual status_t queueBuffer(int buf,
111 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800113 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800114 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700115 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700116 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
117 if (result != NO_ERROR) {
118 return result;
119 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700120 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700121 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800122 return result;
123 }
124
Jesse Hall4c00cc12013-03-15 21:34:30 -0700125 virtual void cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800127 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800129 data.write(*fence.get());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130 remote()->transact(CANCEL_BUFFER, data, &reply);
131 }
132
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700133 virtual int query(int what, int* value) {
134 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800135 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700136 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700137 status_t result = remote()->transact(QUERY, data, &reply);
138 if (result != NO_ERROR) {
139 return result;
140 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700141 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700142 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700143 return result;
144 }
145
Mathias Agopian595264f2013-07-16 22:56:09 -0700146 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700147 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800148 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700149 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700150 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700151 status_t result = remote()->transact(CONNECT, data, &reply);
152 if (result != NO_ERROR) {
153 return result;
154 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700155 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700156 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700157 return result;
158 }
Mathias Agopian80727112011-05-02 19:51:12 -0700159
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700160 virtual status_t disconnect(int api) {
161 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800162 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700163 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700164 status_t result =remote()->transact(DISCONNECT, data, &reply);
165 if (result != NO_ERROR) {
166 return result;
167 }
168 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700169 return result;
170 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800171};
172
Andy McFadden466a1922013-01-08 11:25:51 -0800173IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800174
175// ----------------------------------------------------------------------
176
Andy McFadden2adaf042012-12-18 09:49:45 -0800177status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
179{
180 switch(code) {
181 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800182 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800183 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700184 sp<GraphicBuffer> buffer;
185 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800186 reply->writeInt32(buffer != 0);
187 if (buffer != 0) {
188 reply->write(*buffer);
189 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700190 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800191 return NO_ERROR;
192 } break;
193 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800194 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 int bufferCount = data.readInt32();
196 int result = setBufferCount(bufferCount);
197 reply->writeInt32(result);
198 return NO_ERROR;
199 } break;
200 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800201 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700202 bool async = data.readInt32();
Mathias Agopianc04f1532011-04-25 20:22:14 -0700203 uint32_t w = data.readInt32();
204 uint32_t h = data.readInt32();
205 uint32_t format = data.readInt32();
206 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700208 sp<Fence> fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700209 int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800211 reply->writeInt32(fence != NULL);
212 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700213 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700214 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 reply->writeInt32(result);
216 return NO_ERROR;
217 } break;
218 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800219 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800220 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700221 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700222 QueueBufferOutput* const output =
223 reinterpret_cast<QueueBufferOutput *>(
224 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700225 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800226 reply->writeInt32(result);
227 return NO_ERROR;
228 } break;
229 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800230 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800231 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800232 sp<Fence> fence = new Fence();
233 data.read(*fence.get());
Jesse Hallc777b0b2012-06-28 12:52:05 -0700234 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800235 return NO_ERROR;
236 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700237 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800238 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700239 int value;
240 int what = data.readInt32();
241 int res = query(what, &value);
242 reply->writeInt32(value);
243 reply->writeInt32(res);
244 return NO_ERROR;
245 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700246 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800247 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700248 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700249 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700250 QueueBufferOutput* const output =
251 reinterpret_cast<QueueBufferOutput *>(
252 reply->writeInplace(sizeof(QueueBufferOutput)));
Mathias Agopian595264f2013-07-16 22:56:09 -0700253 status_t res = connect(api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700254 reply->writeInt32(res);
255 return NO_ERROR;
256 } break;
257 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800258 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700259 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700260 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700261 reply->writeInt32(res);
262 return NO_ERROR;
263 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800264 }
265 return BBinder::onTransact(code, data, reply, flags);
266}
267
268// ----------------------------------------------------------------------------
269
Andy McFadden2adaf042012-12-18 09:49:45 -0800270IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700271 parcel.read(*this);
272}
273
Mathias Agopiane1424282013-07-29 21:24:40 -0700274size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700275 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700276 + sizeof(isAutoTimestamp)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700277 + sizeof(crop)
278 + sizeof(scalingMode)
279 + sizeof(transform)
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700280 + sizeof(async)
Jamie Gennis1df8c342012-12-20 14:05:45 -0800281 + fence->getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700282}
283
Mathias Agopiane1424282013-07-29 21:24:40 -0700284size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800285 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700286}
287
Mathias Agopiane1424282013-07-29 21:24:40 -0700288status_t IGraphicBufferProducer::QueueBufferInput::flatten(
289 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700290{
Mathias Agopiane1424282013-07-29 21:24:40 -0700291 if (size < getFlattenedSize()) {
292 return NO_MEMORY;
293 }
294 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700295 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700296 FlattenableUtils::write(buffer, size, crop);
297 FlattenableUtils::write(buffer, size, scalingMode);
298 FlattenableUtils::write(buffer, size, transform);
299 FlattenableUtils::write(buffer, size, async);
300 return fence->flatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700301}
302
Mathias Agopiane1424282013-07-29 21:24:40 -0700303status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
304 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700305{
Mathias Agopiane1424282013-07-29 21:24:40 -0700306 size_t minNeeded =
307 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700308 + sizeof(isAutoTimestamp)
Mathias Agopiane1424282013-07-29 21:24:40 -0700309 + sizeof(crop)
310 + sizeof(scalingMode)
311 + sizeof(transform)
312 + sizeof(async);
313
314 if (size < minNeeded) {
315 return NO_MEMORY;
316 }
317
318 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700319 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700320 FlattenableUtils::read(buffer, size, crop);
321 FlattenableUtils::read(buffer, size, scalingMode);
322 FlattenableUtils::read(buffer, size, transform);
323 FlattenableUtils::read(buffer, size, async);
324
Jamie Gennis1df8c342012-12-20 14:05:45 -0800325 fence = new Fence();
Mathias Agopiane1424282013-07-29 21:24:40 -0700326 return fence->unflatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700327}
328
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800329}; // namespace android