• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
BpGraphicBufferAlloc(const sp<IBinder> & impl)40     BpGraphicBufferAlloc(const sp<IBinder>& impl)
41         : BpInterface<IGraphicBufferAlloc>(impl)
42     {
43     }
44 
45     virtual ~BpGraphicBufferAlloc();
46 
createGraphicBuffer(uint32_t width,uint32_t height,PixelFormat format,uint32_t usage,std::string requestorName,status_t * error)47     virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t width,
48             uint32_t height, PixelFormat format, uint32_t usage,
49             std::string requestorName, status_t* error) {
50         Parcel data, reply;
51         data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor());
52         data.writeUint32(width);
53         data.writeUint32(height);
54         data.writeInt32(static_cast<int32_t>(format));
55         data.writeUint32(usage);
56         if (requestorName.empty()) {
57             requestorName += "[PID ";
58             requestorName += std::to_string(getpid());
59             requestorName += ']';
60         }
61         data.writeUtf8AsUtf16(requestorName);
62         remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply);
63         sp<GraphicBuffer> graphicBuffer;
64         status_t result = reply.readInt32();
65         if (result == NO_ERROR) {
66             graphicBuffer = new GraphicBuffer();
67             result = reply.read(*graphicBuffer);
68             if (result != NO_ERROR) {
69                 graphicBuffer.clear();
70             }
71             // reply.readStrongBinder();
72             // here we don't even have to read the BufferReference from
73             // the parcel, it'll die with the parcel.
74         }
75         *error = result;
76         return graphicBuffer;
77     }
78 };
79 
80 // Out-of-line virtual method definition to trigger vtable emission in this
81 // translation unit (see clang warning -Wweak-vtables)
~BpGraphicBufferAlloc()82 BpGraphicBufferAlloc::~BpGraphicBufferAlloc() {}
83 
84 IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc");
85 
86 // ----------------------------------------------------------------------
87 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)88 status_t BnGraphicBufferAlloc::onTransact(
89     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
90 {
91     // codes that don't require permission check
92 
93     // BufferReference just keeps a strong reference to a GraphicBuffer until it
94     // is destroyed (that is, until no local or remote process have a reference
95     // to it).
96     class BufferReference : public BBinder {
97         sp<GraphicBuffer> mBuffer;
98     public:
99         BufferReference(const sp<GraphicBuffer>& buffer) : mBuffer(buffer) {}
100     };
101 
102 
103     switch (code) {
104         case CREATE_GRAPHIC_BUFFER: {
105             CHECK_INTERFACE(IGraphicBufferAlloc, data, reply);
106             uint32_t width = data.readUint32();
107             uint32_t height = data.readUint32();
108             PixelFormat format = static_cast<PixelFormat>(data.readInt32());
109             uint32_t usage = data.readUint32();
110             status_t error = NO_ERROR;
111             std::string requestorName;
112             data.readUtf8FromUtf16(&requestorName);
113             sp<GraphicBuffer> result = createGraphicBuffer(width, height,
114                     format, usage, requestorName, &error);
115             reply->writeInt32(error);
116             if (result != 0) {
117                 reply->write(*result);
118                 // We add a BufferReference to this parcel to make sure the
119                 // buffer stays alive until the GraphicBuffer object on
120                 // the other side has been created.
121                 // This is needed so that the buffer handle can be
122                 // registered before the buffer is destroyed on implementations
123                 // that do not use file-descriptors to track their buffers.
124                 reply->writeStrongBinder( new BufferReference(result) );
125             }
126             return NO_ERROR;
127         }
128         default:
129             return BBinder::onTransact(code, data, reply, flags);
130     }
131 }
132 
133 }; // namespace android
134