blob: c949817036b02835b861ed8790170bf9f246929f [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,
Mathias Agopian80727112011-05-02 19:51:12 -070040 SET_SYNCHRONOUS_MODE,
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070041 CONNECT,
42 DISCONNECT,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043};
44
45
Andy McFadden2adaf042012-12-18 09:49:45 -080046class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080047{
48public:
Andy McFadden2adaf042012-12-18 09:49:45 -080049 BpGraphicBufferProducer(const sp<IBinder>& impl)
50 : BpInterface<IGraphicBufferProducer>(impl)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080051 {
52 }
53
Jamie Gennis7b305ff2011-07-19 12:08:33 -070054 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080056 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057 data.writeInt32(bufferIdx);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070058 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
59 if (result != NO_ERROR) {
60 return result;
61 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080062 bool nonNull = reply.readInt32();
63 if (nonNull) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -070064 *buf = new GraphicBuffer();
65 reply.read(**buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080066 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -070067 result = reply.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -070068 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069 }
70
71 virtual status_t setBufferCount(int bufferCount)
72 {
73 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080074 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080075 data.writeInt32(bufferCount);
Jamie Gennis8a29ff22011-10-14 15:03:17 -070076 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
77 if (result != NO_ERROR) {
78 return result;
79 }
80 result = reply.readInt32();
81 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080082 }
83
Jesse Hallf7857542012-06-14 15:26:33 -070084 virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence,
85 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080086 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -080087 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
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();
Jesse Hallf7857542012-06-14 15:26:33 -070097 fence.clear();
98 bool hasFence = reply.readInt32();
99 if (hasFence) {
100 fence = new Fence();
101 reply.read(*fence.get());
102 }
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700103 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104 return result;
105 }
106
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700107 virtual status_t queueBuffer(int buf,
108 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800109 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800110 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800111 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700112 data.write(input);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700113 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
114 if (result != NO_ERROR) {
115 return result;
116 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700117 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700118 result = reply.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800119 return result;
120 }
121
Jesse Hallc777b0b2012-06-28 12:52:05 -0700122 virtual void cancelBuffer(int buf, sp<Fence> fence) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123 Parcel data, reply;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700124 bool hasFence = fence.get() && fence->isValid();
Andy McFadden2adaf042012-12-18 09:49:45 -0800125 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126 data.writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700127 data.writeInt32(hasFence);
128 if (hasFence) {
129 data.write(*fence.get());
130 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131 remote()->transact(CANCEL_BUFFER, data, &reply);
132 }
133
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700134 virtual int query(int what, int* value) {
135 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800136 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700137 data.writeInt32(what);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700138 status_t result = remote()->transact(QUERY, data, &reply);
139 if (result != NO_ERROR) {
140 return result;
141 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700142 value[0] = reply.readInt32();
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700143 result = reply.readInt32();
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700144 return result;
145 }
146
Mathias Agopian80727112011-05-02 19:51:12 -0700147 virtual status_t setSynchronousMode(bool enabled) {
148 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800149 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Mathias Agopian80727112011-05-02 19:51:12 -0700150 data.writeInt32(enabled);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700151 status_t result = remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply);
152 if (result != NO_ERROR) {
153 return result;
154 }
155 result = reply.readInt32();
Mathias Agopian80727112011-05-02 19:51:12 -0700156 return result;
157 }
158
Mathias Agopian24202f52012-04-23 14:28:58 -0700159 virtual status_t connect(int api, QueueBufferOutput* output) {
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700160 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(CONNECT, data, &reply);
164 if (result != NO_ERROR) {
165 return result;
166 }
Mathias Agopian24202f52012-04-23 14:28:58 -0700167 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700168 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700169 return result;
170 }
Mathias Agopian80727112011-05-02 19:51:12 -0700171
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700172 virtual status_t disconnect(int api) {
173 Parcel data, reply;
Andy McFadden2adaf042012-12-18 09:49:45 -0800174 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700175 data.writeInt32(api);
Jamie Gennis8a29ff22011-10-14 15:03:17 -0700176 status_t result =remote()->transact(DISCONNECT, data, &reply);
177 if (result != NO_ERROR) {
178 return result;
179 }
180 result = reply.readInt32();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700181 return result;
182 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800183};
184
Andy McFadden466a1922013-01-08 11:25:51 -0800185IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800186
187// ----------------------------------------------------------------------
188
Andy McFadden2adaf042012-12-18 09:49:45 -0800189status_t BnGraphicBufferProducer::onTransact(
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800190 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
191{
192 switch(code) {
193 case REQUEST_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800194 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 int bufferIdx = data.readInt32();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700196 sp<GraphicBuffer> buffer;
197 int result = requestBuffer(bufferIdx, &buffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800198 reply->writeInt32(buffer != 0);
199 if (buffer != 0) {
200 reply->write(*buffer);
201 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700202 reply->writeInt32(result);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800203 return NO_ERROR;
204 } break;
205 case SET_BUFFER_COUNT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800206 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 int bufferCount = data.readInt32();
208 int result = setBufferCount(bufferCount);
209 reply->writeInt32(result);
210 return NO_ERROR;
211 } break;
212 case DEQUEUE_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800213 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700214 uint32_t w = data.readInt32();
215 uint32_t h = data.readInt32();
216 uint32_t format = data.readInt32();
217 uint32_t usage = data.readInt32();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800218 int buf;
Jesse Hallf7857542012-06-14 15:26:33 -0700219 sp<Fence> fence;
220 int result = dequeueBuffer(&buf, fence, w, h, format, usage);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700221 bool hasFence = fence.get() && fence->isValid();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800222 reply->writeInt32(buf);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700223 reply->writeInt32(hasFence);
224 if (hasFence) {
Jesse Hallf7857542012-06-14 15:26:33 -0700225 reply->write(*fence.get());
226 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227 reply->writeInt32(result);
228 return NO_ERROR;
229 } break;
230 case QUEUE_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();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700233 QueueBufferInput input(data);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700234 QueueBufferOutput* const output =
235 reinterpret_cast<QueueBufferOutput *>(
236 reply->writeInplace(sizeof(QueueBufferOutput)));
Jesse Hallc777b0b2012-06-28 12:52:05 -0700237 status_t result = queueBuffer(buf, input, output);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800238 reply->writeInt32(result);
239 return NO_ERROR;
240 } break;
241 case CANCEL_BUFFER: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800242 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 int buf = data.readInt32();
Jesse Hallc777b0b2012-06-28 12:52:05 -0700244 sp<Fence> fence;
245 bool hasFence = data.readInt32();
246 if (hasFence) {
247 fence = new Fence();
248 data.read(*fence.get());
249 }
250 cancelBuffer(buf, fence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800251 return NO_ERROR;
252 } break;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700253 case QUERY: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800254 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700255 int value;
256 int what = data.readInt32();
257 int res = query(what, &value);
258 reply->writeInt32(value);
259 reply->writeInt32(res);
260 return NO_ERROR;
261 } break;
Mathias Agopian80727112011-05-02 19:51:12 -0700262 case SET_SYNCHRONOUS_MODE: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800263 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Mathias Agopian80727112011-05-02 19:51:12 -0700264 bool enabled = data.readInt32();
265 status_t res = setSynchronousMode(enabled);
266 reply->writeInt32(res);
267 return NO_ERROR;
268 } break;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700269 case CONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800270 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700271 int api = data.readInt32();
Mathias Agopian24202f52012-04-23 14:28:58 -0700272 QueueBufferOutput* const output =
273 reinterpret_cast<QueueBufferOutput *>(
274 reply->writeInplace(sizeof(QueueBufferOutput)));
275 status_t res = connect(api, output);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700276 reply->writeInt32(res);
277 return NO_ERROR;
278 } break;
279 case DISCONNECT: {
Andy McFadden2adaf042012-12-18 09:49:45 -0800280 CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700281 int api = data.readInt32();
Mathias Agopian27730042011-07-14 20:20:58 -0700282 status_t res = disconnect(api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700283 reply->writeInt32(res);
284 return NO_ERROR;
285 } break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800286 }
287 return BBinder::onTransact(code, data, reply, flags);
288}
289
290// ----------------------------------------------------------------------------
291
Jesse Hallc777b0b2012-06-28 12:52:05 -0700292static bool isValid(const sp<Fence>& fence) {
293 return fence.get() && fence->isValid();
294}
295
Andy McFadden2adaf042012-12-18 09:49:45 -0800296IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
Jesse Hallc777b0b2012-06-28 12:52:05 -0700297 parcel.read(*this);
298}
299
Andy McFadden2adaf042012-12-18 09:49:45 -0800300size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700301{
302 return sizeof(timestamp)
303 + sizeof(crop)
304 + sizeof(scalingMode)
305 + sizeof(transform)
306 + sizeof(bool)
307 + (isValid(fence) ? fence->getFlattenedSize() : 0);
308}
309
Andy McFadden2adaf042012-12-18 09:49:45 -0800310size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const
Jesse Hallc777b0b2012-06-28 12:52:05 -0700311{
312 return isValid(fence) ? fence->getFdCount() : 0;
313}
314
Andy McFadden2adaf042012-12-18 09:49:45 -0800315status_t IGraphicBufferProducer::QueueBufferInput::flatten(void* buffer, size_t size,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700316 int fds[], size_t count) const
317{
318 status_t err = NO_ERROR;
319 bool haveFence = isValid(fence);
320 char* p = (char*)buffer;
321 memcpy(p, &timestamp, sizeof(timestamp)); p += sizeof(timestamp);
322 memcpy(p, &crop, sizeof(crop)); p += sizeof(crop);
323 memcpy(p, &scalingMode, sizeof(scalingMode)); p += sizeof(scalingMode);
324 memcpy(p, &transform, sizeof(transform)); p += sizeof(transform);
325 memcpy(p, &haveFence, sizeof(haveFence)); p += sizeof(haveFence);
326 if (haveFence) {
327 err = fence->flatten(p, size - (p - (char*)buffer), fds, count);
328 }
329 return err;
330}
331
Andy McFadden2adaf042012-12-18 09:49:45 -0800332status_t IGraphicBufferProducer::QueueBufferInput::unflatten(void const* buffer,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700333 size_t size, int fds[], size_t count)
334{
335 status_t err = NO_ERROR;
336 bool haveFence;
337 const char* p = (const char*)buffer;
338 memcpy(&timestamp, p, sizeof(timestamp)); p += sizeof(timestamp);
339 memcpy(&crop, p, sizeof(crop)); p += sizeof(crop);
340 memcpy(&scalingMode, p, sizeof(scalingMode)); p += sizeof(scalingMode);
341 memcpy(&transform, p, sizeof(transform)); p += sizeof(transform);
342 memcpy(&haveFence, p, sizeof(haveFence)); p += sizeof(haveFence);
343 if (haveFence) {
344 fence = new Fence();
345 err = fence->unflatten(p, size - (p - (const char*)buffer), fds, count);
346 }
347 return err;
348}
349
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800350}; // namespace android