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 | |
| 27 | #include <surfaceflinger/IGraphicBufferAlloc.h> |
| 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | enum { |
| 34 | CREATE_GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION, |
| 35 | FREE_ALL_GRAPHIC_BUFFERS_EXCEPT, |
| 36 | }; |
| 37 | |
| 38 | class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc> |
| 39 | { |
| 40 | public: |
| 41 | BpGraphicBufferAlloc(const sp<IBinder>& impl) |
| 42 | : BpInterface<IGraphicBufferAlloc>(impl) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h, |
| 47 | PixelFormat format, uint32_t usage) { |
| 48 | Parcel data, reply; |
| 49 | data.writeInterfaceToken( |
| 50 | IGraphicBufferAlloc::getInterfaceDescriptor()); |
| 51 | data.writeInt32(w); |
| 52 | data.writeInt32(h); |
| 53 | data.writeInt32(format); |
| 54 | data.writeInt32(usage); |
| 55 | remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply); |
| 56 | sp<GraphicBuffer> graphicBuffer; |
| 57 | bool nonNull = (bool)reply.readInt32(); |
| 58 | if (nonNull) { |
| 59 | graphicBuffer = new GraphicBuffer(); |
| 60 | reply.read(*graphicBuffer); |
| 61 | } |
| 62 | return graphicBuffer; |
| 63 | } |
| 64 | |
| 65 | virtual void freeAllGraphicBuffersExcept(int bufIdx) { |
| 66 | Parcel data, reply; |
| 67 | data.writeInterfaceToken( |
| 68 | IGraphicBufferAlloc::getInterfaceDescriptor()); |
| 69 | data.writeInt32(bufIdx); |
| 70 | remote()->transact(FREE_ALL_GRAPHIC_BUFFERS_EXCEPT, data, &reply); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc"); |
| 75 | |
| 76 | // ---------------------------------------------------------------------- |
| 77 | |
| 78 | status_t BnGraphicBufferAlloc::onTransact( |
| 79 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 80 | { |
| 81 | // codes that don't require permission check |
| 82 | |
| 83 | switch(code) { |
| 84 | case CREATE_GRAPHIC_BUFFER: { |
| 85 | CHECK_INTERFACE(IGraphicBufferAlloc, data, reply); |
| 86 | uint32_t w = data.readInt32(); |
| 87 | uint32_t h = data.readInt32(); |
| 88 | PixelFormat format = data.readInt32(); |
| 89 | uint32_t usage = data.readInt32(); |
| 90 | sp<GraphicBuffer> result(createGraphicBuffer(w, h, format, usage)); |
| 91 | reply->writeInt32(result != 0); |
| 92 | if (result != 0) { |
| 93 | reply->write(*result); |
| 94 | } |
| 95 | return NO_ERROR; |
| 96 | } break; |
| 97 | case FREE_ALL_GRAPHIC_BUFFERS_EXCEPT: { |
| 98 | CHECK_INTERFACE(IGraphicBufferAlloc, data, reply); |
| 99 | int bufIdx = data.readInt32(); |
| 100 | freeAllGraphicBuffersExcept(bufIdx); |
| 101 | return NO_ERROR; |
| 102 | } break; |
| 103 | default: |
| 104 | return BBinder::onTransact(code, data, reply, flags); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | }; // namespace android |