IGraphicBufferProducer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #include <stdint.h>
  17. #include <sys/types.h>
  18. #include <utils/Errors.h>
  19. #include <utils/NativeHandle.h>
  20. #include <utils/RefBase.h>
  21. #include <utils/Timers.h>
  22. #include <utils/Vector.h>
  23. #include <binder/Parcel.h>
  24. #include <binder/IInterface.h>
  25. #include <gui/BufferQueueDefs.h>
  26. #include <gui/IGraphicBufferProducer.h>
  27. #include <gui/IProducerListener.h>
  28. namespace android {
  29. // ----------------------------------------------------------------------------
  30. enum {
  31. REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
  32. SET_BUFFER_COUNT,
  33. DEQUEUE_BUFFER,
  34. DETACH_BUFFER,
  35. DETACH_NEXT_BUFFER,
  36. ATTACH_BUFFER,
  37. QUEUE_BUFFER,
  38. CANCEL_BUFFER,
  39. QUERY,
  40. CONNECT,
  41. DISCONNECT,
  42. SET_SIDEBAND_STREAM,
  43. ALLOCATE_BUFFERS,
  44. ALLOW_ALLOCATION,
  45. SET_GENERATION_NUMBER,
  46. GET_CONSUMER_NAME,
  47. };
  48. class BpGraphicBufferProducer : public BpInterface<IGraphicBufferProducer>
  49. {
  50. public:
  51. BpGraphicBufferProducer(const sp<IBinder>& impl)
  52. : BpInterface<IGraphicBufferProducer>(impl)
  53. {
  54. }
  55. virtual ~BpGraphicBufferProducer();
  56. virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
  57. Parcel data, reply;
  58. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  59. data.writeInt32(bufferIdx);
  60. status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
  61. if (result != NO_ERROR) {
  62. return result;
  63. }
  64. bool nonNull = reply.readInt32();
  65. if (nonNull) {
  66. *buf = new GraphicBuffer();
  67. result = reply.read(**buf);
  68. if(result != NO_ERROR) {
  69. (*buf).clear();
  70. return result;
  71. }
  72. }
  73. result = reply.readInt32();
  74. return result;
  75. }
  76. virtual status_t setBufferCount(int bufferCount)
  77. {
  78. Parcel data, reply;
  79. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  80. data.writeInt32(bufferCount);
  81. status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
  82. if (result != NO_ERROR) {
  83. return result;
  84. }
  85. result = reply.readInt32();
  86. return result;
  87. }
  88. virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
  89. uint32_t width, uint32_t height, PixelFormat format,
  90. uint32_t usage) {
  91. Parcel data, reply;
  92. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  93. data.writeInt32(static_cast<int32_t>(async));
  94. data.writeUint32(width);
  95. data.writeUint32(height);
  96. data.writeInt32(static_cast<int32_t>(format));
  97. data.writeUint32(usage);
  98. status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
  99. if (result != NO_ERROR) {
  100. return result;
  101. }
  102. *buf = reply.readInt32();
  103. bool nonNull = reply.readInt32();
  104. if (nonNull) {
  105. *fence = new Fence();
  106. reply.read(**fence);
  107. }
  108. result = reply.readInt32();
  109. return result;
  110. }
  111. virtual status_t detachBuffer(int slot) {
  112. Parcel data, reply;
  113. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  114. data.writeInt32(slot);
  115. status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
  116. if (result != NO_ERROR) {
  117. return result;
  118. }
  119. result = reply.readInt32();
  120. return result;
  121. }
  122. virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
  123. sp<Fence>* outFence) {
  124. if (outBuffer == NULL) {
  125. ALOGE("detachNextBuffer: outBuffer must not be NULL");
  126. return BAD_VALUE;
  127. } else if (outFence == NULL) {
  128. ALOGE("detachNextBuffer: outFence must not be NULL");
  129. return BAD_VALUE;
  130. }
  131. Parcel data, reply;
  132. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  133. status_t result = remote()->transact(DETACH_NEXT_BUFFER, data, &reply);
  134. if (result != NO_ERROR) {
  135. return result;
  136. }
  137. result = reply.readInt32();
  138. if (result == NO_ERROR) {
  139. bool nonNull = reply.readInt32();
  140. if (nonNull) {
  141. *outBuffer = new GraphicBuffer;
  142. reply.read(**outBuffer);
  143. }
  144. nonNull = reply.readInt32();
  145. if (nonNull) {
  146. *outFence = new Fence;
  147. reply.read(**outFence);
  148. }
  149. }
  150. return result;
  151. }
  152. virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
  153. Parcel data, reply;
  154. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  155. data.write(*buffer.get());
  156. status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
  157. if (result != NO_ERROR) {
  158. return result;
  159. }
  160. *slot = reply.readInt32();
  161. result = reply.readInt32();
  162. if (result == NO_ERROR &&
  163. (*slot < 0 || *slot >= BufferQueueDefs::NUM_BUFFER_SLOTS)) {
  164. ALOGE("attachBuffer returned invalid slot %d", *slot);
  165. android_errorWriteLog(0x534e4554, "37478824");
  166. return UNKNOWN_ERROR;
  167. }
  168. return result;
  169. }
  170. virtual status_t queueBuffer(int buf,
  171. const QueueBufferInput& input, QueueBufferOutput* output) {
  172. Parcel data, reply;
  173. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  174. data.writeInt32(buf);
  175. data.write(input);
  176. status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
  177. if (result != NO_ERROR) {
  178. return result;
  179. }
  180. memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
  181. result = reply.readInt32();
  182. return result;
  183. }
  184. virtual void cancelBuffer(int buf, const sp<Fence>& fence) {
  185. Parcel data, reply;
  186. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  187. data.writeInt32(buf);
  188. data.write(*fence.get());
  189. remote()->transact(CANCEL_BUFFER, data, &reply);
  190. }
  191. virtual int query(int what, int* value) {
  192. Parcel data, reply;
  193. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  194. data.writeInt32(what);
  195. status_t result = remote()->transact(QUERY, data, &reply);
  196. if (result != NO_ERROR) {
  197. return result;
  198. }
  199. value[0] = reply.readInt32();
  200. result = reply.readInt32();
  201. return result;
  202. }
  203. virtual status_t connect(const sp<IProducerListener>& listener,
  204. int api, bool producerControlledByApp, QueueBufferOutput* output) {
  205. Parcel data, reply;
  206. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  207. if (listener != NULL) {
  208. data.writeInt32(1);
  209. data.writeStrongBinder(IInterface::asBinder(listener));
  210. } else {
  211. data.writeInt32(0);
  212. }
  213. data.writeInt32(api);
  214. data.writeInt32(producerControlledByApp);
  215. status_t result = remote()->transact(CONNECT, data, &reply);
  216. if (result != NO_ERROR) {
  217. return result;
  218. }
  219. memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
  220. result = reply.readInt32();
  221. return result;
  222. }
  223. virtual status_t disconnect(int api) {
  224. Parcel data, reply;
  225. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  226. data.writeInt32(api);
  227. status_t result =remote()->transact(DISCONNECT, data, &reply);
  228. if (result != NO_ERROR) {
  229. return result;
  230. }
  231. result = reply.readInt32();
  232. return result;
  233. }
  234. virtual status_t setSidebandStream(const sp<NativeHandle>& stream) {
  235. Parcel data, reply;
  236. status_t result;
  237. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  238. if (stream.get()) {
  239. data.writeInt32(true);
  240. data.writeNativeHandle(stream->handle());
  241. } else {
  242. data.writeInt32(false);
  243. }
  244. if ((result = remote()->transact(SET_SIDEBAND_STREAM, data, &reply)) == NO_ERROR) {
  245. result = reply.readInt32();
  246. }
  247. return result;
  248. }
  249. virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
  250. PixelFormat format, uint32_t usage) {
  251. Parcel data, reply;
  252. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  253. data.writeInt32(static_cast<int32_t>(async));
  254. data.writeUint32(width);
  255. data.writeUint32(height);
  256. data.writeInt32(static_cast<int32_t>(format));
  257. data.writeUint32(usage);
  258. status_t result = remote()->transact(ALLOCATE_BUFFERS, data, &reply);
  259. if (result != NO_ERROR) {
  260. ALOGE("allocateBuffers failed to transact: %d", result);
  261. }
  262. }
  263. virtual status_t allowAllocation(bool allow) {
  264. Parcel data, reply;
  265. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  266. data.writeInt32(static_cast<int32_t>(allow));
  267. status_t result = remote()->transact(ALLOW_ALLOCATION, data, &reply);
  268. if (result != NO_ERROR) {
  269. return result;
  270. }
  271. result = reply.readInt32();
  272. return result;
  273. }
  274. virtual status_t setGenerationNumber(uint32_t generationNumber) {
  275. Parcel data, reply;
  276. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  277. data.writeUint32(generationNumber);
  278. status_t result = remote()->transact(SET_GENERATION_NUMBER, data, &reply);
  279. if (result == NO_ERROR) {
  280. result = reply.readInt32();
  281. }
  282. return result;
  283. }
  284. virtual String8 getConsumerName() const {
  285. Parcel data, reply;
  286. data.writeInterfaceToken(IGraphicBufferProducer::getInterfaceDescriptor());
  287. status_t result = remote()->transact(GET_CONSUMER_NAME, data, &reply);
  288. if (result != NO_ERROR) {
  289. ALOGE("getConsumerName failed to transact: %d", result);
  290. return String8("TransactFailed");
  291. }
  292. return reply.readString8();
  293. }
  294. };
  295. // Out-of-line virtual method definition to trigger vtable emission in this
  296. // translation unit (see clang warning -Wweak-vtables)
  297. BpGraphicBufferProducer::~BpGraphicBufferProducer() {}
  298. IMPLEMENT_META_INTERFACE(GraphicBufferProducer, "android.gui.IGraphicBufferProducer");
  299. // ----------------------------------------------------------------------
  300. status_t BnGraphicBufferProducer::onTransact(
  301. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  302. {
  303. switch(code) {
  304. case REQUEST_BUFFER: {
  305. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  306. int bufferIdx = data.readInt32();
  307. sp<GraphicBuffer> buffer;
  308. int result = requestBuffer(bufferIdx, &buffer);
  309. reply->writeInt32(buffer != 0);
  310. if (buffer != 0) {
  311. reply->write(*buffer);
  312. }
  313. reply->writeInt32(result);
  314. return NO_ERROR;
  315. }
  316. case SET_BUFFER_COUNT: {
  317. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  318. int bufferCount = data.readInt32();
  319. int result = setBufferCount(bufferCount);
  320. reply->writeInt32(result);
  321. return NO_ERROR;
  322. }
  323. case DEQUEUE_BUFFER: {
  324. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  325. bool async = static_cast<bool>(data.readInt32());
  326. uint32_t width = data.readUint32();
  327. uint32_t height = data.readUint32();
  328. PixelFormat format = static_cast<PixelFormat>(data.readInt32());
  329. uint32_t usage = data.readUint32();
  330. int buf = 0;
  331. sp<Fence> fence;
  332. int result = dequeueBuffer(&buf, &fence, async, width, height,
  333. format, usage);
  334. reply->writeInt32(buf);
  335. reply->writeInt32(fence != NULL);
  336. if (fence != NULL) {
  337. reply->write(*fence);
  338. }
  339. reply->writeInt32(result);
  340. return NO_ERROR;
  341. }
  342. case DETACH_BUFFER: {
  343. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  344. int slot = data.readInt32();
  345. int result = detachBuffer(slot);
  346. reply->writeInt32(result);
  347. return NO_ERROR;
  348. }
  349. case DETACH_NEXT_BUFFER: {
  350. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  351. sp<GraphicBuffer> buffer;
  352. sp<Fence> fence;
  353. int32_t result = detachNextBuffer(&buffer, &fence);
  354. reply->writeInt32(result);
  355. if (result == NO_ERROR) {
  356. reply->writeInt32(buffer != NULL);
  357. if (buffer != NULL) {
  358. reply->write(*buffer);
  359. }
  360. reply->writeInt32(fence != NULL);
  361. if (fence != NULL) {
  362. reply->write(*fence);
  363. }
  364. }
  365. return NO_ERROR;
  366. }
  367. case ATTACH_BUFFER: {
  368. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  369. sp<GraphicBuffer> buffer = new GraphicBuffer();
  370. data.read(*buffer.get());
  371. int slot = 0;
  372. int result = attachBuffer(&slot, buffer);
  373. reply->writeInt32(slot);
  374. reply->writeInt32(result);
  375. return NO_ERROR;
  376. }
  377. case QUEUE_BUFFER: {
  378. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  379. int buf = data.readInt32();
  380. QueueBufferInput input(data);
  381. QueueBufferOutput* const output =
  382. reinterpret_cast<QueueBufferOutput *>(
  383. reply->writeInplace(sizeof(QueueBufferOutput)));
  384. memset(output, 0, sizeof(QueueBufferOutput));
  385. status_t result = queueBuffer(buf, input, output);
  386. reply->writeInt32(result);
  387. return NO_ERROR;
  388. }
  389. case CANCEL_BUFFER: {
  390. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  391. int buf = data.readInt32();
  392. sp<Fence> fence = new Fence();
  393. data.read(*fence.get());
  394. cancelBuffer(buf, fence);
  395. return NO_ERROR;
  396. }
  397. case QUERY: {
  398. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  399. int value = 0;
  400. int what = data.readInt32();
  401. int res = query(what, &value);
  402. reply->writeInt32(value);
  403. reply->writeInt32(res);
  404. return NO_ERROR;
  405. }
  406. case CONNECT: {
  407. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  408. sp<IProducerListener> listener;
  409. if (data.readInt32() == 1) {
  410. listener = IProducerListener::asInterface(data.readStrongBinder());
  411. }
  412. int api = data.readInt32();
  413. bool producerControlledByApp = data.readInt32();
  414. QueueBufferOutput* const output =
  415. reinterpret_cast<QueueBufferOutput *>(
  416. reply->writeInplace(sizeof(QueueBufferOutput)));
  417. memset(output, 0, sizeof(QueueBufferOutput));
  418. status_t res = connect(listener, api, producerControlledByApp, output);
  419. reply->writeInt32(res);
  420. return NO_ERROR;
  421. }
  422. case DISCONNECT: {
  423. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  424. int api = data.readInt32();
  425. status_t res = disconnect(api);
  426. reply->writeInt32(res);
  427. return NO_ERROR;
  428. }
  429. case SET_SIDEBAND_STREAM: {
  430. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  431. sp<NativeHandle> stream;
  432. if (data.readInt32()) {
  433. stream = NativeHandle::create(data.readNativeHandle(), true);
  434. }
  435. status_t result = setSidebandStream(stream);
  436. reply->writeInt32(result);
  437. return NO_ERROR;
  438. }
  439. case ALLOCATE_BUFFERS: {
  440. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  441. bool async = static_cast<bool>(data.readInt32());
  442. uint32_t width = data.readUint32();
  443. uint32_t height = data.readUint32();
  444. PixelFormat format = static_cast<PixelFormat>(data.readInt32());
  445. uint32_t usage = data.readUint32();
  446. allocateBuffers(async, width, height, format, usage);
  447. return NO_ERROR;
  448. }
  449. case ALLOW_ALLOCATION: {
  450. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  451. bool allow = static_cast<bool>(data.readInt32());
  452. status_t result = allowAllocation(allow);
  453. reply->writeInt32(result);
  454. return NO_ERROR;
  455. }
  456. case SET_GENERATION_NUMBER: {
  457. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  458. uint32_t generationNumber = data.readUint32();
  459. status_t result = setGenerationNumber(generationNumber);
  460. reply->writeInt32(result);
  461. return NO_ERROR;
  462. }
  463. case GET_CONSUMER_NAME: {
  464. CHECK_INTERFACE(IGraphicBufferProducer, data, reply);
  465. reply->writeString8(getConsumerName());
  466. return NO_ERROR;
  467. }
  468. }
  469. return BBinder::onTransact(code, data, reply, flags);
  470. }
  471. // ----------------------------------------------------------------------------
  472. IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
  473. parcel.read(*this);
  474. }
  475. size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
  476. return sizeof(timestamp)
  477. + sizeof(isAutoTimestamp)
  478. + sizeof(dataSpace)
  479. + sizeof(crop)
  480. + sizeof(scalingMode)
  481. + sizeof(transform)
  482. + sizeof(stickyTransform)
  483. + sizeof(async)
  484. + fence->getFlattenedSize()
  485. + surfaceDamage.getFlattenedSize();
  486. }
  487. size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
  488. return fence->getFdCount();
  489. }
  490. status_t IGraphicBufferProducer::QueueBufferInput::flatten(
  491. void*& buffer, size_t& size, int*& fds, size_t& count) const
  492. {
  493. if (size < getFlattenedSize()) {
  494. return NO_MEMORY;
  495. }
  496. FlattenableUtils::write(buffer, size, timestamp);
  497. FlattenableUtils::write(buffer, size, isAutoTimestamp);
  498. FlattenableUtils::write(buffer, size, dataSpace);
  499. FlattenableUtils::write(buffer, size, crop);
  500. FlattenableUtils::write(buffer, size, scalingMode);
  501. FlattenableUtils::write(buffer, size, transform);
  502. FlattenableUtils::write(buffer, size, stickyTransform);
  503. FlattenableUtils::write(buffer, size, async);
  504. status_t result = fence->flatten(buffer, size, fds, count);
  505. if (result != NO_ERROR) {
  506. return result;
  507. }
  508. return surfaceDamage.flatten(buffer, size);
  509. }
  510. status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
  511. void const*& buffer, size_t& size, int const*& fds, size_t& count)
  512. {
  513. size_t minNeeded =
  514. sizeof(timestamp)
  515. + sizeof(isAutoTimestamp)
  516. + sizeof(dataSpace)
  517. + sizeof(crop)
  518. + sizeof(scalingMode)
  519. + sizeof(transform)
  520. + sizeof(stickyTransform)
  521. + sizeof(async);
  522. if (size < minNeeded) {
  523. return NO_MEMORY;
  524. }
  525. FlattenableUtils::read(buffer, size, timestamp);
  526. FlattenableUtils::read(buffer, size, isAutoTimestamp);
  527. FlattenableUtils::read(buffer, size, dataSpace);
  528. FlattenableUtils::read(buffer, size, crop);
  529. FlattenableUtils::read(buffer, size, scalingMode);
  530. FlattenableUtils::read(buffer, size, transform);
  531. FlattenableUtils::read(buffer, size, stickyTransform);
  532. FlattenableUtils::read(buffer, size, async);
  533. fence = new Fence();
  534. status_t result = fence->unflatten(buffer, size, fds, count);
  535. if (result != NO_ERROR) {
  536. return result;
  537. }
  538. return surfaceDamage.unflatten(buffer, size);
  539. }
  540. }; // namespace android