IGraphicBufferAlloc.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2011 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. // tag as surfaceflinger
  17. #define LOG_TAG "SurfaceFlinger"
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <binder/Parcel.h>
  21. #include <ui/GraphicBuffer.h>
  22. #include <gui/IGraphicBufferAlloc.h>
  23. // ---------------------------------------------------------------------------
  24. namespace android {
  25. enum {
  26. CREATE_GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
  27. };
  28. class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc>
  29. {
  30. public:
  31. BpGraphicBufferAlloc(const sp<IBinder>& impl)
  32. : BpInterface<IGraphicBufferAlloc>(impl)
  33. {
  34. }
  35. virtual ~BpGraphicBufferAlloc();
  36. virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t width,
  37. uint32_t height, PixelFormat format, uint32_t usage,
  38. status_t* error) {
  39. Parcel data, reply;
  40. data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor());
  41. data.writeUint32(width);
  42. data.writeUint32(height);
  43. data.writeInt32(static_cast<int32_t>(format));
  44. data.writeUint32(usage);
  45. remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply);
  46. sp<GraphicBuffer> graphicBuffer;
  47. status_t result = reply.readInt32();
  48. if (result == NO_ERROR) {
  49. graphicBuffer = new GraphicBuffer();
  50. result = reply.read(*graphicBuffer);
  51. if (result != NO_ERROR) {
  52. graphicBuffer.clear();
  53. }
  54. // reply.readStrongBinder();
  55. // here we don't even have to read the BufferReference from
  56. // the parcel, it'll die with the parcel.
  57. }
  58. *error = result;
  59. return graphicBuffer;
  60. }
  61. };
  62. // Out-of-line virtual method definition to trigger vtable emission in this
  63. // translation unit (see clang warning -Wweak-vtables)
  64. BpGraphicBufferAlloc::~BpGraphicBufferAlloc() {}
  65. IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc");
  66. // ----------------------------------------------------------------------
  67. status_t BnGraphicBufferAlloc::onTransact(
  68. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  69. {
  70. // codes that don't require permission check
  71. // BufferReference just keeps a strong reference to a GraphicBuffer until it
  72. // is destroyed (that is, until no local or remote process have a reference
  73. // to it).
  74. class BufferReference : public BBinder {
  75. sp<GraphicBuffer> mBuffer;
  76. public:
  77. BufferReference(const sp<GraphicBuffer>& buffer) : mBuffer(buffer) {}
  78. };
  79. switch (code) {
  80. case CREATE_GRAPHIC_BUFFER: {
  81. CHECK_INTERFACE(IGraphicBufferAlloc, data, reply);
  82. uint32_t width = data.readUint32();
  83. uint32_t height = data.readUint32();
  84. PixelFormat format = static_cast<PixelFormat>(data.readInt32());
  85. uint32_t usage = data.readUint32();
  86. status_t error;
  87. sp<GraphicBuffer> result =
  88. createGraphicBuffer(width, height, format, usage, &error);
  89. reply->writeInt32(error);
  90. if (result != 0) {
  91. reply->write(*result);
  92. // We add a BufferReference to this parcel to make sure the
  93. // buffer stays alive until the GraphicBuffer object on
  94. // the other side has been created.
  95. // This is needed so that the buffer handle can be
  96. // registered before the buffer is destroyed on implementations
  97. // that do not use file-descriptors to track their buffers.
  98. reply->writeStrongBinder( new BufferReference(result) );
  99. }
  100. return NO_ERROR;
  101. }
  102. default:
  103. return BBinder::onTransact(code, data, reply, flags);
  104. }
  105. }
  106. }; // namespace android