IGraphicBufferConsumer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (C) 2013 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 <binder/Parcel.h>
  21. #include <binder/IInterface.h>
  22. #include <gui/BufferItem.h>
  23. #include <gui/IConsumerListener.h>
  24. #include <gui/IGraphicBufferConsumer.h>
  25. #include <ui/GraphicBuffer.h>
  26. #include <ui/Fence.h>
  27. #include <system/window.h>
  28. namespace android {
  29. enum {
  30. ACQUIRE_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
  31. DETACH_BUFFER,
  32. ATTACH_BUFFER,
  33. RELEASE_BUFFER,
  34. CONSUMER_CONNECT,
  35. CONSUMER_DISCONNECT,
  36. GET_RELEASED_BUFFERS,
  37. SET_DEFAULT_BUFFER_SIZE,
  38. SET_DEFAULT_MAX_BUFFER_COUNT,
  39. DISABLE_ASYNC_BUFFER,
  40. SET_MAX_ACQUIRED_BUFFER_COUNT,
  41. SET_CONSUMER_NAME,
  42. SET_DEFAULT_BUFFER_FORMAT,
  43. SET_DEFAULT_BUFFER_DATA_SPACE,
  44. SET_CONSUMER_USAGE_BITS,
  45. SET_TRANSFORM_HINT,
  46. GET_SIDEBAND_STREAM,
  47. DUMP,
  48. };
  49. class BpGraphicBufferConsumer : public BpInterface<IGraphicBufferConsumer>
  50. {
  51. public:
  52. BpGraphicBufferConsumer(const sp<IBinder>& impl)
  53. : BpInterface<IGraphicBufferConsumer>(impl)
  54. {
  55. }
  56. virtual ~BpGraphicBufferConsumer();
  57. virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen,
  58. uint64_t maxFrameNumber) {
  59. Parcel data, reply;
  60. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  61. data.writeInt64(presentWhen);
  62. data.writeUint64(maxFrameNumber);
  63. status_t result = remote()->transact(ACQUIRE_BUFFER, data, &reply);
  64. if (result != NO_ERROR) {
  65. return result;
  66. }
  67. result = reply.read(*buffer);
  68. if (result != NO_ERROR) {
  69. return result;
  70. }
  71. return reply.readInt32();
  72. }
  73. virtual status_t detachBuffer(int slot) {
  74. Parcel data, reply;
  75. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  76. data.writeInt32(slot);
  77. status_t result = remote()->transact(DETACH_BUFFER, data, &reply);
  78. if (result != NO_ERROR) {
  79. return result;
  80. }
  81. result = reply.readInt32();
  82. return result;
  83. }
  84. virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) {
  85. Parcel data, reply;
  86. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  87. data.write(*buffer.get());
  88. status_t result = remote()->transact(ATTACH_BUFFER, data, &reply);
  89. if (result != NO_ERROR) {
  90. return result;
  91. }
  92. *slot = reply.readInt32();
  93. result = reply.readInt32();
  94. return result;
  95. }
  96. virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
  97. EGLDisplay display __attribute__((unused)), EGLSyncKHR fence __attribute__((unused)),
  98. const sp<Fence>& releaseFence) {
  99. Parcel data, reply;
  100. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  101. data.writeInt32(buf);
  102. data.writeInt64(static_cast<int64_t>(frameNumber));
  103. data.write(*releaseFence);
  104. status_t result = remote()->transact(RELEASE_BUFFER, data, &reply);
  105. if (result != NO_ERROR) {
  106. return result;
  107. }
  108. return reply.readInt32();
  109. }
  110. virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) {
  111. Parcel data, reply;
  112. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  113. data.writeStrongBinder(IInterface::asBinder(consumer));
  114. data.writeInt32(controlledByApp);
  115. status_t result = remote()->transact(CONSUMER_CONNECT, data, &reply);
  116. if (result != NO_ERROR) {
  117. return result;
  118. }
  119. return reply.readInt32();
  120. }
  121. virtual status_t consumerDisconnect() {
  122. Parcel data, reply;
  123. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  124. status_t result = remote()->transact(CONSUMER_DISCONNECT, data, &reply);
  125. if (result != NO_ERROR) {
  126. return result;
  127. }
  128. return reply.readInt32();
  129. }
  130. virtual status_t getReleasedBuffers(uint64_t* slotMask) {
  131. Parcel data, reply;
  132. if (slotMask == NULL) {
  133. ALOGE("getReleasedBuffers: slotMask must not be NULL");
  134. return BAD_VALUE;
  135. }
  136. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  137. status_t result = remote()->transact(GET_RELEASED_BUFFERS, data, &reply);
  138. if (result != NO_ERROR) {
  139. return result;
  140. }
  141. *slotMask = static_cast<uint64_t>(reply.readInt64());
  142. return reply.readInt32();
  143. }
  144. virtual status_t setDefaultBufferSize(uint32_t width, uint32_t height) {
  145. Parcel data, reply;
  146. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  147. data.writeUint32(width);
  148. data.writeUint32(height);
  149. status_t result = remote()->transact(SET_DEFAULT_BUFFER_SIZE, data, &reply);
  150. if (result != NO_ERROR) {
  151. return result;
  152. }
  153. return reply.readInt32();
  154. }
  155. virtual status_t setDefaultMaxBufferCount(int bufferCount) {
  156. Parcel data, reply;
  157. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  158. data.writeInt32(bufferCount);
  159. status_t result = remote()->transact(SET_DEFAULT_MAX_BUFFER_COUNT, data, &reply);
  160. if (result != NO_ERROR) {
  161. return result;
  162. }
  163. return reply.readInt32();
  164. }
  165. virtual status_t disableAsyncBuffer() {
  166. Parcel data, reply;
  167. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  168. status_t result = remote()->transact(DISABLE_ASYNC_BUFFER, data, &reply);
  169. if (result != NO_ERROR) {
  170. return result;
  171. }
  172. return reply.readInt32();
  173. }
  174. virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
  175. Parcel data, reply;
  176. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  177. data.writeInt32(maxAcquiredBuffers);
  178. status_t result = remote()->transact(SET_MAX_ACQUIRED_BUFFER_COUNT, data, &reply);
  179. if (result != NO_ERROR) {
  180. return result;
  181. }
  182. return reply.readInt32();
  183. }
  184. virtual void setConsumerName(const String8& name) {
  185. Parcel data, reply;
  186. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  187. data.writeString8(name);
  188. remote()->transact(SET_CONSUMER_NAME, data, &reply);
  189. }
  190. virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) {
  191. Parcel data, reply;
  192. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  193. data.writeInt32(static_cast<int32_t>(defaultFormat));
  194. status_t result = remote()->transact(SET_DEFAULT_BUFFER_FORMAT, data, &reply);
  195. if (result != NO_ERROR) {
  196. return result;
  197. }
  198. return reply.readInt32();
  199. }
  200. virtual status_t setDefaultBufferDataSpace(
  201. android_dataspace defaultDataSpace) {
  202. Parcel data, reply;
  203. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  204. data.writeInt32(static_cast<int32_t>(defaultDataSpace));
  205. status_t result = remote()->transact(SET_DEFAULT_BUFFER_DATA_SPACE,
  206. data, &reply);
  207. if (result != NO_ERROR) {
  208. return result;
  209. }
  210. return reply.readInt32();
  211. }
  212. virtual status_t setConsumerUsageBits(uint32_t usage) {
  213. Parcel data, reply;
  214. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  215. data.writeUint32(usage);
  216. status_t result = remote()->transact(SET_CONSUMER_USAGE_BITS, data, &reply);
  217. if (result != NO_ERROR) {
  218. return result;
  219. }
  220. return reply.readInt32();
  221. }
  222. virtual status_t setTransformHint(uint32_t hint) {
  223. Parcel data, reply;
  224. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  225. data.writeUint32(hint);
  226. status_t result = remote()->transact(SET_TRANSFORM_HINT, data, &reply);
  227. if (result != NO_ERROR) {
  228. return result;
  229. }
  230. return reply.readInt32();
  231. }
  232. virtual sp<NativeHandle> getSidebandStream() const {
  233. Parcel data, reply;
  234. status_t err;
  235. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  236. if ((err = remote()->transact(GET_SIDEBAND_STREAM, data, &reply)) != NO_ERROR) {
  237. return NULL;
  238. }
  239. sp<NativeHandle> stream;
  240. if (reply.readInt32()) {
  241. stream = NativeHandle::create(reply.readNativeHandle(), true);
  242. }
  243. return stream;
  244. }
  245. virtual void dump(String8& result, const char* prefix) const {
  246. Parcel data, reply;
  247. data.writeInterfaceToken(IGraphicBufferConsumer::getInterfaceDescriptor());
  248. data.writeString8(result);
  249. data.writeString8(String8(prefix ? prefix : ""));
  250. remote()->transact(DUMP, data, &reply);
  251. reply.readString8();
  252. }
  253. };
  254. // Out-of-line virtual method definition to trigger vtable emission in this
  255. // translation unit (see clang warning -Wweak-vtables)
  256. BpGraphicBufferConsumer::~BpGraphicBufferConsumer() {}
  257. IMPLEMENT_META_INTERFACE(GraphicBufferConsumer, "android.gui.IGraphicBufferConsumer");
  258. // ----------------------------------------------------------------------
  259. status_t BnGraphicBufferConsumer::onTransact(
  260. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  261. {
  262. switch(code) {
  263. case ACQUIRE_BUFFER: {
  264. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  265. BufferItem item;
  266. int64_t presentWhen = data.readInt64();
  267. uint64_t maxFrameNumber = data.readUint64();
  268. status_t result = acquireBuffer(&item, presentWhen, maxFrameNumber);
  269. status_t err = reply->write(item);
  270. if (err) return err;
  271. reply->writeInt32(result);
  272. return NO_ERROR;
  273. }
  274. case DETACH_BUFFER: {
  275. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  276. int slot = data.readInt32();
  277. int result = detachBuffer(slot);
  278. reply->writeInt32(result);
  279. return NO_ERROR;
  280. }
  281. case ATTACH_BUFFER: {
  282. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  283. sp<GraphicBuffer> buffer = new GraphicBuffer();
  284. data.read(*buffer.get());
  285. int slot = -1;
  286. int result = attachBuffer(&slot, buffer);
  287. reply->writeInt32(slot);
  288. reply->writeInt32(result);
  289. return NO_ERROR;
  290. }
  291. case RELEASE_BUFFER: {
  292. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  293. int buf = data.readInt32();
  294. uint64_t frameNumber = static_cast<uint64_t>(data.readInt64());
  295. sp<Fence> releaseFence = new Fence();
  296. status_t err = data.read(*releaseFence);
  297. if (err) return err;
  298. status_t result = releaseBuffer(buf, frameNumber,
  299. EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, releaseFence);
  300. reply->writeInt32(result);
  301. return NO_ERROR;
  302. }
  303. case CONSUMER_CONNECT: {
  304. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  305. sp<IConsumerListener> consumer = IConsumerListener::asInterface( data.readStrongBinder() );
  306. bool controlledByApp = data.readInt32();
  307. status_t result = consumerConnect(consumer, controlledByApp);
  308. reply->writeInt32(result);
  309. return NO_ERROR;
  310. }
  311. case CONSUMER_DISCONNECT: {
  312. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  313. status_t result = consumerDisconnect();
  314. reply->writeInt32(result);
  315. return NO_ERROR;
  316. }
  317. case GET_RELEASED_BUFFERS: {
  318. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  319. uint64_t slotMask = 0;
  320. status_t result = getReleasedBuffers(&slotMask);
  321. reply->writeInt64(static_cast<int64_t>(slotMask));
  322. reply->writeInt32(result);
  323. return NO_ERROR;
  324. }
  325. case SET_DEFAULT_BUFFER_SIZE: {
  326. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  327. uint32_t width = data.readUint32();
  328. uint32_t height = data.readUint32();
  329. status_t result = setDefaultBufferSize(width, height);
  330. reply->writeInt32(result);
  331. return NO_ERROR;
  332. }
  333. case SET_DEFAULT_MAX_BUFFER_COUNT: {
  334. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  335. int bufferCount = data.readInt32();
  336. status_t result = setDefaultMaxBufferCount(bufferCount);
  337. reply->writeInt32(result);
  338. return NO_ERROR;
  339. }
  340. case DISABLE_ASYNC_BUFFER: {
  341. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  342. status_t result = disableAsyncBuffer();
  343. reply->writeInt32(result);
  344. return NO_ERROR;
  345. }
  346. case SET_MAX_ACQUIRED_BUFFER_COUNT: {
  347. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  348. int maxAcquiredBuffers = data.readInt32();
  349. status_t result = setMaxAcquiredBufferCount(maxAcquiredBuffers);
  350. reply->writeInt32(result);
  351. return NO_ERROR;
  352. }
  353. case SET_CONSUMER_NAME: {
  354. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  355. setConsumerName( data.readString8() );
  356. return NO_ERROR;
  357. }
  358. case SET_DEFAULT_BUFFER_FORMAT: {
  359. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  360. PixelFormat defaultFormat = static_cast<PixelFormat>(data.readInt32());
  361. status_t result = setDefaultBufferFormat(defaultFormat);
  362. reply->writeInt32(result);
  363. return NO_ERROR;
  364. }
  365. case SET_DEFAULT_BUFFER_DATA_SPACE: {
  366. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  367. android_dataspace defaultDataSpace =
  368. static_cast<android_dataspace>(data.readInt32());
  369. status_t result = setDefaultBufferDataSpace(defaultDataSpace);
  370. reply->writeInt32(result);
  371. return NO_ERROR;
  372. }
  373. case SET_CONSUMER_USAGE_BITS: {
  374. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  375. uint32_t usage = data.readUint32();
  376. status_t result = setConsumerUsageBits(usage);
  377. reply->writeInt32(result);
  378. return NO_ERROR;
  379. }
  380. case SET_TRANSFORM_HINT: {
  381. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  382. uint32_t hint = data.readUint32();
  383. status_t result = setTransformHint(hint);
  384. reply->writeInt32(result);
  385. return NO_ERROR;
  386. }
  387. case GET_SIDEBAND_STREAM: {
  388. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  389. sp<NativeHandle> stream = getSidebandStream();
  390. reply->writeInt32(static_cast<int32_t>(stream != NULL));
  391. if (stream != NULL) {
  392. reply->writeNativeHandle(stream->handle());
  393. }
  394. return NO_ERROR;
  395. }
  396. case DUMP: {
  397. CHECK_INTERFACE(IGraphicBufferConsumer, data, reply);
  398. String8 result = data.readString8();
  399. String8 prefix = data.readString8();
  400. static_cast<IGraphicBufferConsumer*>(this)->dump(result, prefix);
  401. reply->writeString8(result);
  402. return NO_ERROR;
  403. }
  404. }
  405. return BBinder::onTransact(code, data, reply, flags);
  406. }
  407. }; // namespace android