blob: 0f461e59c6e6e049c39fed7baaa6c2b04f674748 [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
Andy McFadden2adaf042012-12-18 09:49:45 -080044class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080045{
46public:
Andy McFadden2adaf042012-12-18 09:49:45 -080047 BpGraphicBufferProducer(const sp<IBinder>& impl)
48 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080049 {
50 }
51
Jamie Gennis7b305ff2011-07-19 12:08:33 -070052 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080053 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080054 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070056 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
57 if (result != NO_ERROR) {
58 return result;
59 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080060 bool nonNull = reply.readInt32();
61 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070062 *buf = new GraphicBuffer();
Lingyun Zhu2aff7022012-11-20 19:24:35 +080063 result = reply.read(**buf);
64 if(result != NO_ERROR) {
65 (*buf).clear();
66 return result;
67 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080068 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070069 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070070 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080071 }
72
73 virtual status_t setBufferCount(int bufferCount)
74 {
75 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080076 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080077 data.writeInt32(bufferCount);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070078 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
79 if (result != NO_ERROR) {
80 return result;
81 }
82 result = reply.readInt32();
83 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080084 }
85
Mathias Agopian7cdd7862013-07-18 22:10:56 -070086 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -070087 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080088 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080089 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian7cdd7862013-07-18 22:10:56 -070090 data.writeInt32(async);
Mathias Agopianc04f1532011-04-25 20:22:14 -070091 data.writeInt32(w);
92 data.writeInt32(h);
93 data.writeInt32(format);
94 data.writeInt32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070095 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
96 if (result != NO_ERROR) {
97 return result;
98 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080099 *buf = reply.readInt32();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700100 bool nonNull = reply.readInt32();
101 if (nonNull) {
Jesse Hall4c00cc12013-03-15 21:34:30 -0700102 *fence = new Fence();
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700103 reply.read(**fence);
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 Agopian365857d2013-09-11 19:35:45 -0700145 virtual status_t connect(const sp<IBinder>& token,
146 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());
Mathias Agopian365857d2013-09-11 19:35:45 -0700149 data.writeStrongBinder(token);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700150 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700151 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700152 status_t result = remote()->transact(CONNECT, data, &reply);
153 if (result != NO_ERROR) {
154 return result;
155 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700156 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700157 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700158 return result;
159 }
Mathias Agopian80727112011-05-02 19:51:12 -0700160
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700161 virtual status_t disconnect(int api) {
162 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800163 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700164 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700165 status_t result =remote()->transact(DISCONNECT, data, &reply);
166 if (result != NO_ERROR) {
167 return result;
168 }
169 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700170 return result;
171 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800172};
173
Andy McFadden466a1922013-01-08 11:25:51 -0800174IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175
176// ----------------------------------------------------------------------
177
Andy McFadden2adaf042012-12-18 09:49:45 -0800178status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800179 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
180{
181 switch(code) {
182 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800183 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700185 sp<GraphicBuffer> buffer;
186 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800187 reply->writeInt32(buffer != 0);
188 if (buffer != 0) {
189 reply->write(*buffer);
190 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700191 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800192 return NO_ERROR;
193 } break;
194 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800195 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800196 int bufferCount = data.readInt32();
197 int result = setBufferCount(bufferCount);
198 reply->writeInt32(result);
199 return NO_ERROR;
200 } break;
201 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800202 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700203 bool async = data.readInt32();
Mathias Agopianc04f1532011-04-25 20:22:14 -0700204 uint32_t w = data.readInt32();
205 uint32_t h = data.readInt32();
206 uint32_t format = data.readInt32();
207 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700209 sp<Fence> fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700210 int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800212 reply->writeInt32(fence != NULL);
213 if (fence != NULL) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700214 reply->write(*fence);
Jesse Hallf7857542012-06-14 15:26:33 -0700215 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800216 reply->writeInt32(result);
217 return NO_ERROR;
218 } break;
219 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800220 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800221 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700222 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700223 QueueBufferOutput* const output =
224 reinterpret_cast<QueueBufferOutput *>(
225 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700226 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227 reply->writeInt32(result);
228 return NO_ERROR;
229 } break;
230 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800231 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800233 sp<Fence> fence = new Fence();
234 data.read(*fence.get());
Jesse Hallc777b0b2012-06-28 12:52:05 -0700235 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 return NO_ERROR;
237 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700238 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800239 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700240 int value;
241 int what = data.readInt32();
242 int res = query(what, &value);
243 reply->writeInt32(value);
244 reply->writeInt32(res);
245 return NO_ERROR;
246 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700247 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800248 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian365857d2013-09-11 19:35:45 -0700249 sp<IBinder> token = data.readStrongBinder();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700250 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700251 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700252 QueueBufferOutput* const output =
253 reinterpret_cast<QueueBufferOutput *>(
254 reply->writeInplace(sizeof(QueueBufferOutput)));
Mathias Agopian365857d2013-09-11 19:35:45 -0700255 status_t res = connect(token, api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700256 reply->writeInt32(res);
257 return NO_ERROR;
258 } break;
259 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800260 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700261 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700262 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700263 reply->writeInt32(res);
264 return NO_ERROR;
265 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800266 }
267 return BBinder::onTransact(code, data, reply, flags);
268}
269
270// ----------------------------------------------------------------------------
271
Andy McFadden2adaf042012-12-18 09:49:45 -0800272IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700273 parcel.read(*this);
274}
275
Mathias Agopiane1424282013-07-29 21:24:40 -0700276size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700277 return sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700278 + sizeof(isAutoTimestamp)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700279 + sizeof(crop)
280 + sizeof(scalingMode)
281 + sizeof(transform)
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700282 + sizeof(async)
Jamie Gennis1df8c342012-12-20 14:05:45 -0800283 + fence->getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700284}
285
Mathias Agopiane1424282013-07-29 21:24:40 -0700286size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800287 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700288}
289
Mathias Agopiane1424282013-07-29 21:24:40 -0700290status_t IGraphicBufferProducer::QueueBufferInput::flatten(
291 void*& buffer, size_t& size, int*& fds, size_t& count) const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700292{
Mathias Agopiane1424282013-07-29 21:24:40 -0700293 if (size < getFlattenedSize()) {
294 return NO_MEMORY;
295 }
296 FlattenableUtils::write(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700297 FlattenableUtils::write(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700298 FlattenableUtils::write(buffer, size, crop);
299 FlattenableUtils::write(buffer, size, scalingMode);
300 FlattenableUtils::write(buffer, size, transform);
301 FlattenableUtils::write(buffer, size, async);
302 return fence->flatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700303}
304
Mathias Agopiane1424282013-07-29 21:24:40 -0700305status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
306 void const*& buffer, size_t& size, int const*& fds, size_t& count)
Jesse Hallc777b0b2012-06-28 12:52:05 -0700307{
Mathias Agopiane1424282013-07-29 21:24:40 -0700308 size_t minNeeded =
309 sizeof(timestamp)
Andy McFadden3c256212013-08-16 14:55:39 -0700310 + sizeof(isAutoTimestamp)
Mathias Agopiane1424282013-07-29 21:24:40 -0700311 + sizeof(crop)
312 + sizeof(scalingMode)
313 + sizeof(transform)
314 + sizeof(async);
315
316 if (size < minNeeded) {
317 return NO_MEMORY;
318 }
319
320 FlattenableUtils::read(buffer, size, timestamp);
Andy McFadden3c256212013-08-16 14:55:39 -0700321 FlattenableUtils::read(buffer, size, isAutoTimestamp);
Mathias Agopiane1424282013-07-29 21:24:40 -0700322 FlattenableUtils::read(buffer, size, crop);
323 FlattenableUtils::read(buffer, size, scalingMode);
324 FlattenableUtils::read(buffer, size, transform);
325 FlattenableUtils::read(buffer, size, async);
326
Jamie Gennis1df8c342012-12-20 14:05:45 -0800327 fence = new Fence();
Mathias Agopiane1424282013-07-29 21:24:40 -0700328 return fence->unflatten(buffer, size, fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700329}
330
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800331}; // namespace android