Chia-I Wu | 527747d | 2017-03-13 20:38:48 +0000 | [diff] [blame] | 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 | |
| 17 | // tag as surfaceflinger |
| 18 | #define LOG_TAG "SurfaceFlinger" |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <binder/Parcel.h> |
| 24 | |
| 25 | #include <ui/GraphicBuffer.h> |
| 26 | |
| 27 | #include <gui/IGraphicBufferAlloc.h> |
| 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | enum { |
| 34 | CREATE_GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION, |
| 35 | }; |
| 36 | |
| 37 | class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc> |
| 38 | { |
| 39 | public: |
| 40 | explicit BpGraphicBufferAlloc(const sp<IBinder>& impl) |
| 41 | : BpInterface<IGraphicBufferAlloc>(impl) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | virtual ~BpGraphicBufferAlloc(); |
| 46 | |
| 47 | virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t width, |
| 48 | uint32_t height, PixelFormat format, uint32_t layerCount, |
| 49 | uint64_t producerUsage, uint64_t consumerUsage, |
| 50 | std::string requestorName, status_t* error) { |
| 51 | Parcel data, reply; |
| 52 | data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor()); |
| 53 | data.writeUint32(width); |
| 54 | data.writeUint32(height); |
| 55 | data.writeInt32(static_cast<int32_t>(format)); |
| 56 | data.writeUint32(layerCount); |
| 57 | data.writeUint64(producerUsage); |
| 58 | data.writeUint64(consumerUsage); |
| 59 | if (requestorName.empty()) { |
| 60 | requestorName += "[PID "; |
| 61 | requestorName += std::to_string(getpid()); |
| 62 | requestorName += ']'; |
| 63 | } |
| 64 | data.writeUtf8AsUtf16(requestorName); |
| 65 | remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply); |
| 66 | sp<GraphicBuffer> graphicBuffer; |
| 67 | status_t result = reply.readInt32(); |
| 68 | if (result == NO_ERROR) { |
| 69 | graphicBuffer = new GraphicBuffer(); |
| 70 | result = reply.read(*graphicBuffer); |
| 71 | if (result != NO_ERROR) { |
| 72 | graphicBuffer.clear(); |
| 73 | } |
| 74 | // reply.readStrongBinder(); |
| 75 | // here we don't even have to read the BufferReference from |
| 76 | // the parcel, it'll die with the parcel. |
| 77 | } |
| 78 | *error = result; |
| 79 | return graphicBuffer; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | // Out-of-line virtual method definition to trigger vtable emission in this |
| 84 | // translation unit (see clang warning -Wweak-vtables) |
| 85 | BpGraphicBufferAlloc::~BpGraphicBufferAlloc() {} |
| 86 | |
| 87 | IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc"); |
| 88 | |
| 89 | // ---------------------------------------------------------------------- |
| 90 | |
| 91 | status_t BnGraphicBufferAlloc::onTransact( |
| 92 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 93 | { |
| 94 | // codes that don't require permission check |
| 95 | |
| 96 | // BufferReference just keeps a strong reference to a GraphicBuffer until it |
| 97 | // is destroyed (that is, until no local or remote process have a reference |
| 98 | // to it). |
| 99 | class BufferReference : public BBinder { |
| 100 | sp<GraphicBuffer> mBuffer; |
| 101 | public: |
| 102 | explicit BufferReference(const sp<GraphicBuffer>& buffer) : mBuffer(buffer) {} |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | switch (code) { |
| 107 | case CREATE_GRAPHIC_BUFFER: { |
| 108 | CHECK_INTERFACE(IGraphicBufferAlloc, data, reply); |
| 109 | uint32_t width = data.readUint32(); |
| 110 | uint32_t height = data.readUint32(); |
| 111 | PixelFormat format = static_cast<PixelFormat>(data.readInt32()); |
| 112 | uint32_t layerCount = data.readUint32(); |
| 113 | uint64_t producerUsage = data.readUint64(); |
| 114 | uint64_t consumerUsage = data.readUint64(); |
| 115 | status_t error = NO_ERROR; |
| 116 | std::string requestorName; |
| 117 | data.readUtf8FromUtf16(&requestorName); |
| 118 | sp<GraphicBuffer> result = createGraphicBuffer(width, height, |
| 119 | format, layerCount, producerUsage, consumerUsage, |
| 120 | requestorName, &error); |
| 121 | reply->writeInt32(error); |
| 122 | if (result != 0) { |
| 123 | reply->write(*result); |
| 124 | // We add a BufferReference to this parcel to make sure the |
| 125 | // buffer stays alive until the GraphicBuffer object on |
| 126 | // the other side has been created. |
| 127 | // This is needed so that the buffer handle can be |
| 128 | // registered before the buffer is destroyed on implementations |
| 129 | // that do not use file-descriptors to track their buffers. |
| 130 | reply->writeStrongBinder( new BufferReference(result) ); |
| 131 | } |
| 132 | return NO_ERROR; |
| 133 | } |
| 134 | default: |
| 135 | return BBinder::onTransact(code, data, reply, flags); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | }; // namespace android |