| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [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 |  | 
| Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 27 | #include <gui/IGraphicBufferAlloc.h> | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | // --------------------------------------------------------------------------- | 
|  | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 |  | 
|  | 33 | enum { | 
|  | 34 | CREATE_GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION, | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 35 | }; | 
|  | 36 |  | 
|  | 37 | class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc> | 
|  | 38 | { | 
|  | 39 | public: | 
|  | 40 | BpGraphicBufferAlloc(const sp<IBinder>& impl) | 
|  | 41 | : BpInterface<IGraphicBufferAlloc>(impl) | 
|  | 42 | { | 
|  | 43 | } | 
|  | 44 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 45 | virtual ~BpGraphicBufferAlloc(); | 
|  | 46 |  | 
|  | 47 | virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t width, | 
|  | 48 | uint32_t height, PixelFormat format, uint32_t usage, | 
|  | 49 | status_t* error) { | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 50 | Parcel data, reply; | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 51 | data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor()); | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 52 | data.writeUint32(width); | 
|  | 53 | data.writeUint32(height); | 
|  | 54 | data.writeInt32(static_cast<int32_t>(format)); | 
|  | 55 | data.writeUint32(usage); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 56 | remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply); | 
|  | 57 | sp<GraphicBuffer> graphicBuffer; | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 58 | status_t result = reply.readInt32(); | 
|  | 59 | if (result == NO_ERROR) { | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 60 | graphicBuffer = new GraphicBuffer(); | 
| Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 61 | result = reply.read(*graphicBuffer); | 
| Dan Stoza | 6a04e5f | 2015-06-01 13:59:15 -0700 | [diff] [blame] | 62 | if (result != NO_ERROR) { | 
|  | 63 | graphicBuffer.clear(); | 
|  | 64 | } | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 65 | // reply.readStrongBinder(); | 
|  | 66 | // here we don't even have to read the BufferReference from | 
|  | 67 | // the parcel, it'll die with the parcel. | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 68 | } | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 69 | *error = result; | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 70 | return graphicBuffer; | 
|  | 71 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 72 | }; | 
|  | 73 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 74 | // Out-of-line virtual method definition to trigger vtable emission in this | 
|  | 75 | // translation unit (see clang warning -Wweak-vtables) | 
|  | 76 | BpGraphicBufferAlloc::~BpGraphicBufferAlloc() {} | 
|  | 77 |  | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 78 | IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc"); | 
|  | 79 |  | 
|  | 80 | // ---------------------------------------------------------------------- | 
|  | 81 |  | 
|  | 82 | status_t BnGraphicBufferAlloc::onTransact( | 
|  | 83 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 84 | { | 
|  | 85 | // codes that don't require permission check | 
|  | 86 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 87 | // BufferReference just keeps a strong reference to a GraphicBuffer until it | 
|  | 88 | // is destroyed (that is, until no local or remote process have a reference | 
|  | 89 | // to it). | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 90 | class BufferReference : public BBinder { | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 91 | sp<GraphicBuffer> mBuffer; | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 92 | public: | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 93 | BufferReference(const sp<GraphicBuffer>& buffer) : mBuffer(buffer) {} | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 94 | }; | 
|  | 95 |  | 
|  | 96 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 97 | switch (code) { | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 98 | case CREATE_GRAPHIC_BUFFER: { | 
|  | 99 | CHECK_INTERFACE(IGraphicBufferAlloc, data, reply); | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 100 | uint32_t width = data.readUint32(); | 
|  | 101 | uint32_t height = data.readUint32(); | 
|  | 102 | PixelFormat format = static_cast<PixelFormat>(data.readInt32()); | 
|  | 103 | uint32_t usage = data.readUint32(); | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 104 | status_t error; | 
|  | 105 | sp<GraphicBuffer> result = | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 106 | createGraphicBuffer(width, height, format, usage, &error); | 
| Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 107 | reply->writeInt32(error); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 108 | if (result != 0) { | 
|  | 109 | reply->write(*result); | 
| Mathias Agopian | 4cb1888 | 2011-04-08 19:10:43 -0700 | [diff] [blame] | 110 | // We add a BufferReference to this parcel to make sure the | 
|  | 111 | // buffer stays alive until the GraphicBuffer object on | 
|  | 112 | // the other side has been created. | 
|  | 113 | // This is needed so that the buffer handle can be | 
|  | 114 | // registered before the buffer is destroyed on implementations | 
|  | 115 | // that do not use file-descriptors to track their buffers. | 
|  | 116 | reply->writeStrongBinder( new BufferReference(result) ); | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 117 | } | 
|  | 118 | return NO_ERROR; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 119 | } | 
| Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 120 | default: | 
|  | 121 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | }; // namespace android |