blob: 9f65fc3f2b0b7d00fd8b9bc31f9b45ca047951ca [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
Jesse Hall4c00cc12013-03-15 21:34:30 -070083 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence,
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 Agopianc04f1532011-04-25 20:22:14 -070087 data.writeInt32(w);
88 data.writeInt32(h);
89 data.writeInt32(format);
90 data.writeInt32(usage);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070091 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
92 if (result != NO_ERROR) {
93 return result;
94 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080095 *buf = reply.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -080096 bool fenceWasWritten = reply.readInt32();
97 if (fenceWasWritten) {
98 // If the fence was written by the callee, then overwrite the
99 // caller's fence here. If it wasn't written then don't touch the
100 // caller's fence.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700101 *fence = new Fence();
102 reply.read(*(fence->get()));
Jesse Hallf7857542012-06-14 15:26:33 -0700103 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700104 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800105 return result;
106 }
107
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700108 virtual status_t queueBuffer(int buf,
109 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800111 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700113 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700114 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
115 if (result != NO_ERROR) {
116 return result;
117 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700118 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700119 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800120 return result;
121 }
122
Jesse Hall4c00cc12013-03-15 21:34:30 -0700123 virtual void cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800124 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800125 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126 data.writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800127 data.write(*fence.get());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128 remote()->transact(CANCEL_BUFFER, data, &reply);
129 }
130
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700131 virtual int query(int what, int* value) {
132 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800133 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700134 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700135 status_t result = remote()->transact(QUERY, data, &reply);
136 if (result != NO_ERROR) {
137 return result;
138 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700139 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700140 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700141 return result;
142 }
143
Mathias Agopian595264f2013-07-16 22:56:09 -0700144 virtual status_t connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700145 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800146 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700147 data.writeInt32(api);
Mathias Agopian595264f2013-07-16 22:56:09 -0700148 data.writeInt32(producerControlledByApp);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700149 status_t result = remote()->transact(CONNECT, data, &reply);
150 if (result != NO_ERROR) {
151 return result;
152 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700153 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700154 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700155 return result;
156 }
Mathias Agopian80727112011-05-02 19:51:12 -0700157
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700158 virtual status_t disconnect(int api) {
159 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800160 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700161 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700162 status_t result =remote()->transact(DISCONNECT, data, &reply);
163 if (result != NO_ERROR) {
164 return result;
165 }
166 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700167 return result;
168 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800169};
170
Andy McFadden466a1922013-01-08 11:25:51 -0800171IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800172
173// ----------------------------------------------------------------------
174
Andy McFadden2adaf042012-12-18 09:49:45 -0800175status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
177{
178 switch(code) {
179 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800180 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800181 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700182 sp<GraphicBuffer> buffer;
183 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184 reply->writeInt32(buffer != 0);
185 if (buffer != 0) {
186 reply->write(*buffer);
187 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700188 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 return NO_ERROR;
190 } break;
191 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800192 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800193 int bufferCount = data.readInt32();
194 int result = setBufferCount(bufferCount);
195 reply->writeInt32(result);
196 return NO_ERROR;
197 } break;
198 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800199 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700200 uint32_t w = data.readInt32();
201 uint32_t h = data.readInt32();
202 uint32_t format = data.readInt32();
203 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800204 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700205 sp<Fence> fence;
Jesse Hall4c00cc12013-03-15 21:34:30 -0700206 int result = dequeueBuffer(&buf, &fence, w, h, format, usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 reply->writeInt32(buf);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800208 reply->writeInt32(fence != NULL);
209 if (fence != NULL) {
Jesse Hallf7857542012-06-14 15:26:33 -0700210 reply->write(*fence.get());
211 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 reply->writeInt32(result);
213 return NO_ERROR;
214 } break;
215 case QUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800216 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800217 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700218 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700219 QueueBufferOutput* const output =
220 reinterpret_cast<QueueBufferOutput *>(
221 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700222 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800223 reply->writeInt32(result);
224 return NO_ERROR;
225 } break;
226 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800227 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800228 int buf = data.readInt32();
Jamie Gennis1df8c342012-12-20 14:05:45 -0800229 sp<Fence> fence = new Fence();
230 data.read(*fence.get());
Jesse Hallc777b0b2012-06-28 12:52:05 -0700231 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 return NO_ERROR;
233 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700234 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800235 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700236 int value;
237 int what = data.readInt32();
238 int res = query(what, &value);
239 reply->writeInt32(value);
240 reply->writeInt32(res);
241 return NO_ERROR;
242 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700243 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800244 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700245 int api = data.readInt32();
Mathias Agopian595264f2013-07-16 22:56:09 -0700246 bool producerControlledByApp = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700247 QueueBufferOutput* const output =
248 reinterpret_cast<QueueBufferOutput *>(
249 reply->writeInplace(sizeof(QueueBufferOutput)));
Mathias Agopian595264f2013-07-16 22:56:09 -0700250 status_t res = connect(api, producerControlledByApp, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700251 reply->writeInt32(res);
252 return NO_ERROR;
253 } break;
254 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800255 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700256 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700257 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700258 reply->writeInt32(res);
259 return NO_ERROR;
260 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800261 }
262 return BBinder::onTransact(code, data, reply, flags);
263}
264
265// ----------------------------------------------------------------------------
266
Andy McFadden2adaf042012-12-18 09:49:45 -0800267IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700268 parcel.read(*this);
269}
270
Andy McFadden2adaf042012-12-18 09:49:45 -0800271size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700272{
273 return sizeof(timestamp)
274 + sizeof(crop)
275 + sizeof(scalingMode)
276 + sizeof(transform)
Jamie Gennis1df8c342012-12-20 14:05:45 -0800277 + fence->getFlattenedSize();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700278}
279
Andy McFadden2adaf042012-12-18 09:49:45 -0800280size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700281{
Jamie Gennis1df8c342012-12-20 14:05:45 -0800282 return fence->getFdCount();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700283}
284
Andy McFadden2adaf042012-12-18 09:49:45 -0800285status_t IGraphicBufferProducer::QueueBufferInput::flatten(void* buffer, size_t size,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700286 int fds[], size_t count) const
287{
288 status_t err = NO_ERROR;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700289 char* p = (char*)buffer;
290 memcpy(p, &timestamp, sizeof(timestamp)); p += sizeof(timestamp);
291 memcpy(p, &crop, sizeof(crop)); p += sizeof(crop);
292 memcpy(p, &scalingMode, sizeof(scalingMode)); p += sizeof(scalingMode);
293 memcpy(p, &transform, sizeof(transform)); p += sizeof(transform);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800294 err = fence->flatten(p, size - (p - (char*)buffer), fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700295 return err;
296}
297
Andy McFadden2adaf042012-12-18 09:49:45 -0800298status_t IGraphicBufferProducer::QueueBufferInput::unflatten(void const* buffer,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700299 size_t size, int fds[], size_t count)
300{
301 status_t err = NO_ERROR;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700302 const char* p = (const char*)buffer;
303 memcpy(&timestamp, p, sizeof(timestamp)); p += sizeof(timestamp);
304 memcpy(&crop, p, sizeof(crop)); p += sizeof(crop);
305 memcpy(&scalingMode, p, sizeof(scalingMode)); p += sizeof(scalingMode);
306 memcpy(&transform, p, sizeof(transform)); p += sizeof(transform);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800307 fence = new Fence();
308 err = fence->unflatten(p, size - (p - (const char*)buffer), fds, count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700309 return err;
310}
311
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800312}; // namespace android