1 /*
2  * Copyright (C) 2013 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 #include <gui/IGraphicBufferConsumer.h>
18 
19 #include <gui/BufferItem.h>
20 #include <gui/IConsumerListener.h>
21 
22 #include <binder/Parcel.h>
23 
24 #include <ui/Fence.h>
25 #include <ui/GraphicBuffer.h>
26 
27 #include <utils/NativeHandle.h>
28 #include <utils/String8.h>
29 
30 namespace android {
31 
32 namespace { // Anonymous namespace
33 
34 enum class Tag : uint32_t {
35     ACQUIRE_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
36     DETACH_BUFFER,
37     ATTACH_BUFFER,
38     RELEASE_BUFFER,
39     CONSUMER_CONNECT,
40     CONSUMER_DISCONNECT,
41     GET_RELEASED_BUFFERS,
42     SET_DEFAULT_BUFFER_SIZE,
43     SET_MAX_BUFFER_COUNT,
44     SET_MAX_ACQUIRED_BUFFER_COUNT,
45     SET_CONSUMER_NAME,
46     SET_DEFAULT_BUFFER_FORMAT,
47     SET_DEFAULT_BUFFER_DATA_SPACE,
48     SET_CONSUMER_USAGE_BITS,
49     SET_CONSUMER_IS_PROTECTED,
50     SET_TRANSFORM_HINT,
51     GET_SIDEBAND_STREAM,
52     GET_OCCUPANCY_HISTORY,
53     DISCARD_FREE_BUFFERS,
54     DUMP_STATE,
55     LAST = DUMP_STATE,
56 };
57 
58 } // Anonymous namespace
59 
60 class BpGraphicBufferConsumer : public SafeBpInterface<IGraphicBufferConsumer> {
61 public:
BpGraphicBufferConsumer(const sp<IBinder> & impl)62     explicit BpGraphicBufferConsumer(const sp<IBinder>& impl)
63           : SafeBpInterface<IGraphicBufferConsumer>(impl, "BpGraphicBufferConsumer") {}
64 
65     ~BpGraphicBufferConsumer() override;
66 
acquireBuffer(BufferItem * buffer,nsecs_t presentWhen,uint64_t maxFrameNumber)67     status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
68                            uint64_t maxFrameNumber) override {
69         using Signature = decltype(&IGraphicBufferConsumer::acquireBuffer);
70         return callRemote<Signature>(Tag::ACQUIRE_BUFFER, buffer, presentWhen, maxFrameNumber);
71     }
72 
detachBuffer(int slot)73     status_t detachBuffer(int slot) override {
74         using Signature = decltype(&IGraphicBufferConsumer::detachBuffer);
75         return callRemote<Signature>(Tag::DETACH_BUFFER, slot);
76     }
77 
attachBuffer(int * slot,const sp<GraphicBuffer> & buffer)78     status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer) override {
79         using Signature = decltype(&IGraphicBufferConsumer::attachBuffer);
80         return callRemote<Signature>(Tag::ATTACH_BUFFER, slot, buffer);
81     }
82 
releaseBuffer(int buf,uint64_t frameNumber,EGLDisplay display,EGLSyncKHR fence,const sp<Fence> & releaseFence)83     status_t releaseBuffer(int buf, uint64_t frameNumber,
84                            EGLDisplay display __attribute__((unused)),
85                            EGLSyncKHR fence __attribute__((unused)),
86                            const sp<Fence>& releaseFence) override {
87         return callRemote<ReleaseBuffer>(Tag::RELEASE_BUFFER, buf, frameNumber, releaseFence);
88     }
89 
consumerConnect(const sp<IConsumerListener> & consumer,bool controlledByApp)90     status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) override {
91         using Signature = decltype(&IGraphicBufferConsumer::consumerConnect);
92         return callRemote<Signature>(Tag::CONSUMER_CONNECT, consumer, controlledByApp);
93     }
94 
consumerDisconnect()95     status_t consumerDisconnect() override {
96         return callRemote<decltype(&IGraphicBufferConsumer::consumerDisconnect)>(
97                 Tag::CONSUMER_DISCONNECT);
98     }
99 
getReleasedBuffers(uint64_t * slotMask)100     status_t getReleasedBuffers(uint64_t* slotMask) override {
101         using Signature = decltype(&IGraphicBufferConsumer::getReleasedBuffers);
102         return callRemote<Signature>(Tag::GET_RELEASED_BUFFERS, slotMask);
103     }
104 
setDefaultBufferSize(uint32_t width,uint32_t height)105     status_t setDefaultBufferSize(uint32_t width, uint32_t height) override {
106         using Signature = decltype(&IGraphicBufferConsumer::setDefaultBufferSize);
107         return callRemote<Signature>(Tag::SET_DEFAULT_BUFFER_SIZE, width, height);
108     }
109 
setMaxBufferCount(int bufferCount)110     status_t setMaxBufferCount(int bufferCount) override {
111         using Signature = decltype(&IGraphicBufferConsumer::setMaxBufferCount);
112         return callRemote<Signature>(Tag::SET_MAX_BUFFER_COUNT, bufferCount);
113     }
114 
setMaxAcquiredBufferCount(int maxAcquiredBuffers)115     status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) override {
116         using Signature = decltype(&IGraphicBufferConsumer::setMaxAcquiredBufferCount);
117         return callRemote<Signature>(Tag::SET_MAX_ACQUIRED_BUFFER_COUNT, maxAcquiredBuffers);
118     }
119 
setConsumerName(const String8 & name)120     status_t setConsumerName(const String8& name) override {
121         using Signature = decltype(&IGraphicBufferConsumer::setConsumerName);
122         return callRemote<Signature>(Tag::SET_CONSUMER_NAME, name);
123     }
124 
setDefaultBufferFormat(PixelFormat defaultFormat)125     status_t setDefaultBufferFormat(PixelFormat defaultFormat) override {
126         using Signature = decltype(&IGraphicBufferConsumer::setDefaultBufferFormat);
127         return callRemote<Signature>(Tag::SET_DEFAULT_BUFFER_FORMAT, defaultFormat);
128     }
129 
setDefaultBufferDataSpace(android_dataspace defaultDataSpace)130     status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace) override {
131         using Signature = decltype(&IGraphicBufferConsumer::setDefaultBufferDataSpace);
132         return callRemote<Signature>(Tag::SET_DEFAULT_BUFFER_DATA_SPACE, defaultDataSpace);
133     }
134 
setConsumerUsageBits(uint64_t usage)135     status_t setConsumerUsageBits(uint64_t usage) override {
136         using Signature = decltype(&IGraphicBufferConsumer::setConsumerUsageBits);
137         return callRemote<Signature>(Tag::SET_CONSUMER_USAGE_BITS, usage);
138     }
139 
setConsumerIsProtected(bool isProtected)140     status_t setConsumerIsProtected(bool isProtected) override {
141         using Signature = decltype(&IGraphicBufferConsumer::setConsumerIsProtected);
142         return callRemote<Signature>(Tag::SET_CONSUMER_IS_PROTECTED, isProtected);
143     }
144 
setTransformHint(uint32_t hint)145     status_t setTransformHint(uint32_t hint) override {
146         using Signature = decltype(&IGraphicBufferConsumer::setTransformHint);
147         return callRemote<Signature>(Tag::SET_TRANSFORM_HINT, hint);
148     }
149 
getSidebandStream(sp<NativeHandle> * outStream) const150     status_t getSidebandStream(sp<NativeHandle>* outStream) const override {
151         using Signature = decltype(&IGraphicBufferConsumer::getSidebandStream);
152         return callRemote<Signature>(Tag::GET_SIDEBAND_STREAM, outStream);
153     }
154 
getOccupancyHistory(bool forceFlush,std::vector<OccupancyTracker::Segment> * outHistory)155     status_t getOccupancyHistory(bool forceFlush,
156                                  std::vector<OccupancyTracker::Segment>* outHistory) override {
157         using Signature = decltype(&IGraphicBufferConsumer::getOccupancyHistory);
158         return callRemote<Signature>(Tag::GET_OCCUPANCY_HISTORY, forceFlush, outHistory);
159     }
160 
discardFreeBuffers()161     status_t discardFreeBuffers() override {
162         return callRemote<decltype(&IGraphicBufferConsumer::discardFreeBuffers)>(
163                 Tag::DISCARD_FREE_BUFFERS);
164     }
165 
dumpState(const String8 & prefix,String8 * outResult) const166     status_t dumpState(const String8& prefix, String8* outResult) const override {
167         using Signature = status_t (IGraphicBufferConsumer::*)(const String8&, String8*) const;
168         return callRemote<Signature>(Tag::DUMP_STATE, prefix, outResult);
169     }
170 };
171 
172 // Out-of-line virtual method definition to trigger vtable emission in this translation unit
173 // (see clang warning -Wweak-vtables)
174 BpGraphicBufferConsumer::~BpGraphicBufferConsumer() = default;
175 
176 IMPLEMENT_META_INTERFACE(GraphicBufferConsumer, "android.gui.IGraphicBufferConsumer");
177 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)178 status_t BnGraphicBufferConsumer::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
179                                              uint32_t flags) {
180     if (code < IBinder::FIRST_CALL_TRANSACTION || code > static_cast<uint32_t>(Tag::LAST)) {
181         return BBinder::onTransact(code, data, reply, flags);
182     }
183     auto tag = static_cast<Tag>(code);
184     switch (tag) {
185         case Tag::ACQUIRE_BUFFER:
186             return callLocal(data, reply, &IGraphicBufferConsumer::acquireBuffer);
187         case Tag::DETACH_BUFFER:
188             return callLocal(data, reply, &IGraphicBufferConsumer::detachBuffer);
189         case Tag::ATTACH_BUFFER:
190             return callLocal(data, reply, &IGraphicBufferConsumer::attachBuffer);
191         case Tag::RELEASE_BUFFER:
192             return callLocal(data, reply, &IGraphicBufferConsumer::releaseHelper);
193         case Tag::CONSUMER_CONNECT:
194             return callLocal(data, reply, &IGraphicBufferConsumer::consumerConnect);
195         case Tag::CONSUMER_DISCONNECT:
196             return callLocal(data, reply, &IGraphicBufferConsumer::consumerDisconnect);
197         case Tag::GET_RELEASED_BUFFERS:
198             return callLocal(data, reply, &IGraphicBufferConsumer::getReleasedBuffers);
199         case Tag::SET_DEFAULT_BUFFER_SIZE:
200             return callLocal(data, reply, &IGraphicBufferConsumer::setDefaultBufferSize);
201         case Tag::SET_MAX_BUFFER_COUNT:
202             return callLocal(data, reply, &IGraphicBufferConsumer::setMaxBufferCount);
203         case Tag::SET_MAX_ACQUIRED_BUFFER_COUNT:
204             return callLocal(data, reply, &IGraphicBufferConsumer::setMaxAcquiredBufferCount);
205         case Tag::SET_CONSUMER_NAME:
206             return callLocal(data, reply, &IGraphicBufferConsumer::setConsumerName);
207         case Tag::SET_DEFAULT_BUFFER_FORMAT:
208             return callLocal(data, reply, &IGraphicBufferConsumer::setDefaultBufferFormat);
209         case Tag::SET_DEFAULT_BUFFER_DATA_SPACE:
210             return callLocal(data, reply, &IGraphicBufferConsumer::setDefaultBufferDataSpace);
211         case Tag::SET_CONSUMER_USAGE_BITS:
212             return callLocal(data, reply, &IGraphicBufferConsumer::setConsumerUsageBits);
213         case Tag::SET_CONSUMER_IS_PROTECTED:
214             return callLocal(data, reply, &IGraphicBufferConsumer::setConsumerIsProtected);
215         case Tag::SET_TRANSFORM_HINT:
216             return callLocal(data, reply, &IGraphicBufferConsumer::setTransformHint);
217         case Tag::GET_SIDEBAND_STREAM:
218             return callLocal(data, reply, &IGraphicBufferConsumer::getSidebandStream);
219         case Tag::GET_OCCUPANCY_HISTORY:
220             return callLocal(data, reply, &IGraphicBufferConsumer::getOccupancyHistory);
221         case Tag::DISCARD_FREE_BUFFERS:
222             return callLocal(data, reply, &IGraphicBufferConsumer::discardFreeBuffers);
223         case Tag::DUMP_STATE: {
224             using Signature = status_t (IGraphicBufferConsumer::*)(const String8&, String8*) const;
225             return callLocal<Signature>(data, reply, &IGraphicBufferConsumer::dumpState);
226         }
227     }
228 }
229 
230 } // namespace android
231