blob: a3d3b74c6b430d7d1df68a14ed77ee158f396edf [file] [log] [blame]
Jamie Gennis9a78c902011-01-12 18:30:40 -08001/*
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 Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/IGraphicBufferAlloc.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080028
29// ---------------------------------------------------------------------------
30
31namespace android {
32
33enum {
34 CREATE_GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
Jamie Gennis9a78c902011-01-12 18:30:40 -080035};
36
37class BpGraphicBufferAlloc : public BpInterface<IGraphicBufferAlloc>
38{
39public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070040 explicit BpGraphicBufferAlloc(const sp<IBinder>& impl)
Jamie Gennis9a78c902011-01-12 18:30:40 -080041 : BpInterface<IGraphicBufferAlloc>(impl)
42 {
43 }
44
Dan Stoza3be1c6b2014-11-18 10:24:03 -080045 virtual ~BpGraphicBufferAlloc();
46
47 virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t width,
Craig Donner6ebc46a2016-10-21 15:23:44 -070048 uint32_t height, PixelFormat format, uint32_t layerCount,
49 uint32_t usage, std::string requestorName, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -080050 Parcel data, reply;
Mathias Agopian4cb18882011-04-08 19:10:43 -070051 data.writeInterfaceToken(IGraphicBufferAlloc::getInterfaceDescriptor());
Dan Stoza3be1c6b2014-11-18 10:24:03 -080052 data.writeUint32(width);
53 data.writeUint32(height);
54 data.writeInt32(static_cast<int32_t>(format));
Craig Donner6ebc46a2016-10-21 15:23:44 -070055 data.writeUint32(layerCount);
Dan Stoza3be1c6b2014-11-18 10:24:03 -080056 data.writeUint32(usage);
Dan Stoza024e9312016-08-24 12:17:29 -070057 if (requestorName.empty()) {
58 requestorName += "[PID ";
59 requestorName += std::to_string(getpid());
60 requestorName += ']';
61 }
62 data.writeUtf8AsUtf16(requestorName);
Jamie Gennis9a78c902011-01-12 18:30:40 -080063 remote()->transact(CREATE_GRAPHIC_BUFFER, data, &reply);
64 sp<GraphicBuffer> graphicBuffer;
Mathias Agopiand9e8c642011-07-01 14:53:49 -070065 status_t result = reply.readInt32();
66 if (result == NO_ERROR) {
Jamie Gennis9a78c902011-01-12 18:30:40 -080067 graphicBuffer = new GraphicBuffer();
Jamie Gennisd69097f2012-08-30 13:28:23 -070068 result = reply.read(*graphicBuffer);
Dan Stoza3d6022a2015-06-01 13:59:15 -070069 if (result != NO_ERROR) {
70 graphicBuffer.clear();
71 }
Mathias Agopian4cb18882011-04-08 19:10:43 -070072 // reply.readStrongBinder();
73 // here we don't even have to read the BufferReference from
74 // the parcel, it'll die with the parcel.
Jamie Gennis9a78c902011-01-12 18:30:40 -080075 }
Mathias Agopiand9e8c642011-07-01 14:53:49 -070076 *error = result;
Jamie Gennis9a78c902011-01-12 18:30:40 -080077 return graphicBuffer;
78 }
Jamie Gennis9a78c902011-01-12 18:30:40 -080079};
80
Dan Stoza3be1c6b2014-11-18 10:24:03 -080081// Out-of-line virtual method definition to trigger vtable emission in this
82// translation unit (see clang warning -Wweak-vtables)
83BpGraphicBufferAlloc::~BpGraphicBufferAlloc() {}
84
Jamie Gennis9a78c902011-01-12 18:30:40 -080085IMPLEMENT_META_INTERFACE(GraphicBufferAlloc, "android.ui.IGraphicBufferAlloc");
86
87// ----------------------------------------------------------------------
88
89status_t BnGraphicBufferAlloc::onTransact(
90 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
91{
92 // codes that don't require permission check
93
Dan Stoza3be1c6b2014-11-18 10:24:03 -080094 // BufferReference just keeps a strong reference to a GraphicBuffer until it
95 // is destroyed (that is, until no local or remote process have a reference
96 // to it).
Mathias Agopian4cb18882011-04-08 19:10:43 -070097 class BufferReference : public BBinder {
Dan Stoza3be1c6b2014-11-18 10:24:03 -080098 sp<GraphicBuffer> mBuffer;
Mathias Agopian4cb18882011-04-08 19:10:43 -070099 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700100 explicit BufferReference(const sp<GraphicBuffer>& buffer) : mBuffer(buffer) {}
Mathias Agopian4cb18882011-04-08 19:10:43 -0700101 };
102
103
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800104 switch (code) {
Jamie Gennis9a78c902011-01-12 18:30:40 -0800105 case CREATE_GRAPHIC_BUFFER: {
106 CHECK_INTERFACE(IGraphicBufferAlloc, data, reply);
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800107 uint32_t width = data.readUint32();
108 uint32_t height = data.readUint32();
109 PixelFormat format = static_cast<PixelFormat>(data.readInt32());
Craig Donner6ebc46a2016-10-21 15:23:44 -0700110 uint32_t layerCount = data.readUint32();
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800111 uint32_t usage = data.readUint32();
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700112 status_t error = NO_ERROR;
Dan Stoza024e9312016-08-24 12:17:29 -0700113 std::string requestorName;
114 data.readUtf8FromUtf16(&requestorName);
115 sp<GraphicBuffer> result = createGraphicBuffer(width, height,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700116 format, layerCount, usage, requestorName, &error);
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700117 reply->writeInt32(error);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800118 if (result != 0) {
119 reply->write(*result);
Mathias Agopian4cb18882011-04-08 19:10:43 -0700120 // We add a BufferReference to this parcel to make sure the
121 // buffer stays alive until the GraphicBuffer object on
122 // the other side has been created.
123 // This is needed so that the buffer handle can be
124 // registered before the buffer is destroyed on implementations
125 // that do not use file-descriptors to track their buffers.
126 reply->writeStrongBinder( new BufferReference(result) );
Jamie Gennis9a78c902011-01-12 18:30:40 -0800127 }
128 return NO_ERROR;
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800129 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800130 default:
131 return BBinder::onTransact(code, data, reply, flags);
132 }
133}
134
135}; // namespace android