IGraphicBufferProducer.cpp revision fd34b65f717b84fa06d8c37f41b070f41d0ad3a3
1/* 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/NativeHandle.h> 22#include <utils/RefBase.h> 23#include <utils/Timers.h> 24#include <utils/Vector.h> 25 26#include <binder/Parcel.h> 27#include <binder/IInterface.h> 28 29#include <gui/IGraphicBufferProducer.h> 30#include <gui/IProducerListener.h> 31 32namespace android { 33// ---------------------------------------------------------------------------- 34 35enum { 36 REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION, 37 SET_BUFFER_COUNT, 38 DEQUEUE_BUFFER, 39 DETACH_BUFFER, 40 ATTACH_BUFFER, 41 QUEUE_BUFFER, 42 CANCEL_BUFFER, 43 QUERY, 44 CONNECT, 45 DISCONNECT, 46 SET_SIDEBAND_STREAM, 47}; 48 49class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer> 50{ 51public: 52 BpGraphicBufferProducer(const sp<IBinder>& impl) 53 : BpInterface<IGraphicBufferProducer>(impl) 54 { 55 } 56 57 virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) { 58 Parcel data, reply; 59 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 60 data.writeInt32(bufferIdx); 61 status_t result =remote()->transact(REQUEST_BUFFER, data, &reply); 62 if (result != NO_ERROR) { 63 return result; 64 } 65 bool nonNull = reply.readInt32(); 66 if (nonNull) { 67 *buf = new GraphicBuffer(); 68 result = reply.read(**buf); 69 if(result != NO_ERROR) { 70 (*buf).clear(); 71 return result; 72 } 73 } 74 result = reply.readInt32(); 75 return result; 76 } 77 78 virtual status_t setBufferCount(int bufferCount) 79 { 80 Parcel data, reply; 81 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 82 data.writeInt32(bufferCount); 83 status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply); 84 if (result != NO_ERROR) { 85 return result; 86 } 87 result = reply.readInt32(); 88 return result; 89 } 90 91 virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async, 92 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { 93 Parcel data, reply; 94 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 95 data.writeInt32(async); 96 data.writeInt32(w); 97 data.writeInt32(h); 98 data.writeInt32(format); 99 data.writeInt32(usage); 100 status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply); 101 if (result != NO_ERROR) { 102 return result; 103 } 104 *buf = reply.readInt32(); 105 bool nonNull = reply.readInt32(); 106 if (nonNull) { 107 *fence = new Fence(); 108 reply.read(**fence); 109 } 110 result = reply.readInt32(); 111 return result; 112 } 113 114 virtual status_t detachBuffer(int slot) { 115 Parcel data, reply; 116 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 117 data.writeInt32(slot); 118 status_t result = remote()->transact(DETACH_BUFFER, data, &reply); 119 if (result != NO_ERROR) { 120 return result; 121 } 122 result = reply.readInt32(); 123 return result; 124 } 125 126 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) { 127 Parcel data, reply; 128 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 129 data.write(*buffer.get()); 130 status_t result = remote()->transact(ATTACH_BUFFER, data, &reply); 131 if (result != NO_ERROR) { 132 return result; 133 } 134 *slot = reply.readInt32(); 135 result = reply.readInt32(); 136 return result; 137 } 138 139 virtual status_t queueBuffer(int buf, 140 const QueueBufferInput& input, QueueBufferOutput* output) { 141 Parcel data, reply; 142 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 143 data.writeInt32(buf); 144 data.write(input); 145 status_t result = remote()->transact(QUEUE_BUFFER, data, &reply); 146 if (result != NO_ERROR) { 147 return result; 148 } 149 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output)); 150 result = reply.readInt32(); 151 return result; 152 } 153 154 virtual void cancelBuffer(int buf, const sp<Fence>& fence) { 155 Parcel data, reply; 156 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 157 data.writeInt32(buf); 158 data.write(*fence.get()); 159 remote()->transact(CANCEL_BUFFER, data, &reply); 160 } 161 162 virtual int query(int what, int* value) { 163 Parcel data, reply; 164 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 165 data.writeInt32(what); 166 status_t result = remote()->transact(QUERY, data, &reply); 167 if (result != NO_ERROR) { 168 return result; 169 } 170 value[0] = reply.readInt32(); 171 result = reply.readInt32(); 172 return result; 173 } 174 175 virtual status_t connect(const sp<IProducerListener>& listener, 176 int api, bool producerControlledByApp, QueueBufferOutput* output) { 177 Parcel data, reply; 178 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 179 if (listener != NULL) { 180 data.writeInt32(1); 181 data.writeStrongBinder(listener->asBinder()); 182 } else { 183 data.writeInt32(0); 184 } 185 data.writeInt32(api); 186 data.writeInt32(producerControlledByApp); 187 status_t result = remote()->transact(CONNECT, data, &reply); 188 if (result != NO_ERROR) { 189 return result; 190 } 191 memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output)); 192 result = reply.readInt32(); 193 return result; 194 } 195 196 virtual status_t disconnect(int api) { 197 Parcel data, reply; 198 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 199 data.writeInt32(api); 200 status_t result =remote()->transact(DISCONNECT, data, &reply); 201 if (result != NO_ERROR) { 202 return result; 203 } 204 result = reply.readInt32(); 205 return result; 206 } 207 208 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) { 209 Parcel data, reply; 210 status_t result; 211 data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor()); 212 if (stream.get()) { 213 data.writeInt32(true); 214 data.writeNativeHandle(stream->handle()); 215 } else { 216 data.writeInt32(false); 217 } 218 if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) { 219 result = reply.readInt32(); 220 } 221 return result; 222 } 223}; 224 225IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer"); 226 227// ---------------------------------------------------------------------- 228 229status_t BnGraphicBufferProducer::onTransact( 230 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 231{ 232 switch(code) { 233 case REQUEST_BUFFER: { 234 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 235 int bufferIdx = data.readInt32(); 236 sp<GraphicBuffer> buffer; 237 int result = requestBuffer(bufferIdx, &buffer); 238 reply->writeInt32(buffer != 0); 239 if (buffer != 0) { 240 reply->write(*buffer); 241 } 242 reply->writeInt32(result); 243 return NO_ERROR; 244 } break; 245 case SET_BUFFER_COUNT: { 246 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 247 int bufferCount = data.readInt32(); 248 int result = setBufferCount(bufferCount); 249 reply->writeInt32(result); 250 return NO_ERROR; 251 } break; 252 case DEQUEUE_BUFFER: { 253 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 254 bool async = data.readInt32(); 255 uint32_t w = data.readInt32(); 256 uint32_t h = data.readInt32(); 257 uint32_t format = data.readInt32(); 258 uint32_t usage = data.readInt32(); 259 int buf; 260 sp<Fence> fence; 261 int result = dequeueBuffer(&buf, &fence, async, w, h, format, usage); 262 reply->writeInt32(buf); 263 reply->writeInt32(fence != NULL); 264 if (fence != NULL) { 265 reply->write(*fence); 266 } 267 reply->writeInt32(result); 268 return NO_ERROR; 269 } break; 270 case DETACH_BUFFER: { 271 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 272 int slot = data.readInt32(); 273 int result = detachBuffer(slot); 274 reply->writeInt32(result); 275 return NO_ERROR; 276 } break; 277 case ATTACH_BUFFER: { 278 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 279 sp<GraphicBuffer> buffer = new GraphicBuffer(); 280 data.read(*buffer.get()); 281 int slot; 282 int result = attachBuffer(&slot, buffer); 283 reply->writeInt32(slot); 284 reply->writeInt32(result); 285 return NO_ERROR; 286 } break; 287 case QUEUE_BUFFER: { 288 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 289 int buf = data.readInt32(); 290 QueueBufferInput input(data); 291 QueueBufferOutput* const output = 292 reinterpret_cast<QueueBufferOutput *>( 293 reply->writeInplace(sizeof(QueueBufferOutput))); 294 status_t result = queueBuffer(buf, input, output); 295 reply->writeInt32(result); 296 return NO_ERROR; 297 } break; 298 case CANCEL_BUFFER: { 299 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 300 int buf = data.readInt32(); 301 sp<Fence> fence = new Fence(); 302 data.read(*fence.get()); 303 cancelBuffer(buf, fence); 304 return NO_ERROR; 305 } break; 306 case QUERY: { 307 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 308 int value; 309 int what = data.readInt32(); 310 int res = query(what, &value); 311 reply->writeInt32(value); 312 reply->writeInt32(res); 313 return NO_ERROR; 314 } break; 315 case CONNECT: { 316 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 317 sp<IProducerListener> listener; 318 if (data.readInt32() == 1) { 319 listener = IProducerListener::asInterface(data.readStrongBinder()); 320 } 321 int api = data.readInt32(); 322 bool producerControlledByApp = data.readInt32(); 323 QueueBufferOutput* const output = 324 reinterpret_cast<QueueBufferOutput *>( 325 reply->writeInplace(sizeof(QueueBufferOutput))); 326 status_t res = connect(listener, api, producerControlledByApp, output); 327 reply->writeInt32(res); 328 return NO_ERROR; 329 } break; 330 case DISCONNECT: { 331 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 332 int api = data.readInt32(); 333 status_t res = disconnect(api); 334 reply->writeInt32(res); 335 return NO_ERROR; 336 } break; 337 case SET_SIDEBAND_STREAM: { 338 CHECK_INTERFACE(IGraphicBufferProducer, data, reply); 339 sp<NativeHandle> stream; 340 if (data.readInt32()) { 341 stream = NativeHandle::create(data.readNativeHandle(), true); 342 } 343 status_t result = setSidebandStream(stream); 344 reply->writeInt32(result); 345 return NO_ERROR; 346 } break; 347 } 348 return BBinder::onTransact(code, data, reply, flags); 349} 350 351// ---------------------------------------------------------------------------- 352 353IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) { 354 parcel.read(*this); 355} 356 357size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const { 358 return sizeof(timestamp) 359 + sizeof(isAutoTimestamp) 360 + sizeof(crop) 361 + sizeof(scalingMode) 362 + sizeof(transform) 363 + sizeof(async) 364 + fence->getFlattenedSize(); 365} 366 367size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const { 368 return fence->getFdCount(); 369} 370 371status_t IGraphicBufferProducer::QueueBufferInput::flatten( 372 void*& buffer, size_t& size, int*& fds, size_t& count) const 373{ 374 if (size < getFlattenedSize()) { 375 return NO_MEMORY; 376 } 377 FlattenableUtils::write(buffer, size, timestamp); 378 FlattenableUtils::write(buffer, size, isAutoTimestamp); 379 FlattenableUtils::write(buffer, size, crop); 380 FlattenableUtils::write(buffer, size, scalingMode); 381 FlattenableUtils::write(buffer, size, transform); 382 FlattenableUtils::write(buffer, size, async); 383 return fence->flatten(buffer, size, fds, count); 384} 385 386status_t IGraphicBufferProducer::QueueBufferInput::unflatten( 387 void const*& buffer, size_t& size, int const*& fds, size_t& count) 388{ 389 size_t minNeeded = 390 sizeof(timestamp) 391 + sizeof(isAutoTimestamp) 392 + sizeof(crop) 393 + sizeof(scalingMode) 394 + sizeof(transform) 395 + sizeof(async); 396 397 if (size < minNeeded) { 398 return NO_MEMORY; 399 } 400 401 FlattenableUtils::read(buffer, size, timestamp); 402 FlattenableUtils::read(buffer, size, isAutoTimestamp); 403 FlattenableUtils::read(buffer, size, crop); 404 FlattenableUtils::read(buffer, size, scalingMode); 405 FlattenableUtils::read(buffer, size, transform); 406 FlattenableUtils::read(buffer, size, async); 407 408 fence = new Fence(); 409 return fence->unflatten(buffer, size, fds, count); 410} 411 412}; // namespace android 413