1 /*
2  * Copyright (C) 2007 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 <android/gui/IDisplayEventConnection.h>
21 #include <android/gui/IRegionSamplingListener.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/Parcel.h>
25 #include <gui/IGraphicBufferProducer.h>
26 #include <gui/ISurfaceComposer.h>
27 #include <gui/LayerState.h>
28 #include <gui/SchedulingPolicy.h>
29 #include <private/gui/ParcelUtils.h>
30 #include <stdint.h>
31 #include <sys/types.h>
32 #include <system/graphics.h>
33 #include <ui/DisplayMode.h>
34 #include <ui/DisplayStatInfo.h>
35 #include <ui/DisplayState.h>
36 #include <ui/DynamicDisplayInfo.h>
37 #include <ui/HdrCapabilities.h>
38 #include <utils/Log.h>
39 
40 // ---------------------------------------------------------------------------
41 
42 using namespace aidl::android::hardware::graphics;
43 
44 namespace android {
45 
46 using gui::DisplayCaptureArgs;
47 using gui::IDisplayEventConnection;
48 using gui::IRegionSamplingListener;
49 using gui::IWindowInfosListener;
50 using gui::LayerCaptureArgs;
51 using ui::ColorMode;
52 
53 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
54 {
55 public:
BpSurfaceComposer(const sp<IBinder> & impl)56     explicit BpSurfaceComposer(const sp<IBinder>& impl)
57         : BpInterface<ISurfaceComposer>(impl)
58     {
59     }
60 
61     virtual ~BpSurfaceComposer();
62 
setTransactionState(const FrameTimelineInfo & frameTimelineInfo,Vector<ComposerState> & state,const Vector<DisplayState> & displays,uint32_t flags,const sp<IBinder> & applyToken,InputWindowCommands commands,int64_t desiredPresentTime,bool isAutoTimestamp,const std::vector<client_cache_t> & uncacheBuffers,bool hasListenerCallbacks,const std::vector<ListenerCallbacks> & listenerCallbacks,uint64_t transactionId,const std::vector<uint64_t> & mergedTransactionIds)63     status_t setTransactionState(
64             const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& state,
65             const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
66             InputWindowCommands commands, int64_t desiredPresentTime, bool isAutoTimestamp,
67             const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks,
68             const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId,
69             const std::vector<uint64_t>& mergedTransactionIds) override {
70         Parcel data, reply;
71         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
72 
73         frameTimelineInfo.writeToParcel(&data);
74 
75         SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(state.size()));
76         for (const auto& s : state) {
77             SAFE_PARCEL(s.write, data);
78         }
79 
80         SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(displays.size()));
81         for (const auto& d : displays) {
82             SAFE_PARCEL(d.write, data);
83         }
84 
85         SAFE_PARCEL(data.writeUint32, flags);
86         SAFE_PARCEL(data.writeStrongBinder, applyToken);
87         SAFE_PARCEL(commands.write, data);
88         SAFE_PARCEL(data.writeInt64, desiredPresentTime);
89         SAFE_PARCEL(data.writeBool, isAutoTimestamp);
90         SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(uncacheBuffers.size()));
91         for (const client_cache_t& uncacheBuffer : uncacheBuffers) {
92             SAFE_PARCEL(data.writeStrongBinder, uncacheBuffer.token.promote());
93             SAFE_PARCEL(data.writeUint64, uncacheBuffer.id);
94         }
95         SAFE_PARCEL(data.writeBool, hasListenerCallbacks);
96 
97         SAFE_PARCEL(data.writeVectorSize, listenerCallbacks);
98         for (const auto& [listener, callbackIds] : listenerCallbacks) {
99             SAFE_PARCEL(data.writeStrongBinder, listener);
100             SAFE_PARCEL(data.writeParcelableVector, callbackIds);
101         }
102 
103         SAFE_PARCEL(data.writeUint64, transactionId);
104 
105         SAFE_PARCEL(data.writeUint32, static_cast<uint32_t>(mergedTransactionIds.size()));
106         for (auto mergedTransactionId : mergedTransactionIds) {
107             SAFE_PARCEL(data.writeUint64, mergedTransactionId);
108         }
109 
110         if (flags & ISurfaceComposer::eOneWay) {
111             return remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE,
112                     data, &reply, IBinder::FLAG_ONEWAY);
113         } else {
114             return remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE,
115                     data, &reply);
116         }
117     }
118 };
119 
120 // Out-of-line virtual method definition to trigger vtable emission in this
121 // translation unit (see clang warning -Wweak-vtables)
~BpSurfaceComposer()122 BpSurfaceComposer::~BpSurfaceComposer() {}
123 
124 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
125 
126 // ----------------------------------------------------------------------
127 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)128 status_t BnSurfaceComposer::onTransact(
129     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
130 {
131     switch (code) {
132         case SET_TRANSACTION_STATE: {
133             CHECK_INTERFACE(ISurfaceComposer, data, reply);
134 
135             FrameTimelineInfo frameTimelineInfo;
136             frameTimelineInfo.readFromParcel(&data);
137 
138             uint32_t count = 0;
139             SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize());
140             Vector<ComposerState> state;
141             state.setCapacity(count);
142             for (size_t i = 0; i < count; i++) {
143                 ComposerState s;
144                 SAFE_PARCEL(s.read, data);
145                 state.add(s);
146             }
147 
148             SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize());
149             DisplayState d;
150             Vector<DisplayState> displays;
151             displays.setCapacity(count);
152             for (size_t i = 0; i < count; i++) {
153                 SAFE_PARCEL(d.read, data);
154                 displays.add(d);
155             }
156 
157             uint32_t stateFlags = 0;
158             SAFE_PARCEL(data.readUint32, &stateFlags);
159             sp<IBinder> applyToken;
160             SAFE_PARCEL(data.readStrongBinder, &applyToken);
161             InputWindowCommands inputWindowCommands;
162             SAFE_PARCEL(inputWindowCommands.read, data);
163 
164             int64_t desiredPresentTime = 0;
165             bool isAutoTimestamp = true;
166             SAFE_PARCEL(data.readInt64, &desiredPresentTime);
167             SAFE_PARCEL(data.readBool, &isAutoTimestamp);
168 
169             SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize());
170             std::vector<client_cache_t> uncacheBuffers(count);
171             sp<IBinder> tmpBinder;
172             for (size_t i = 0; i < count; i++) {
173                 SAFE_PARCEL(data.readNullableStrongBinder, &tmpBinder);
174                 uncacheBuffers[i].token = tmpBinder;
175                 SAFE_PARCEL(data.readUint64, &uncacheBuffers[i].id);
176             }
177 
178             bool hasListenerCallbacks = false;
179             SAFE_PARCEL(data.readBool, &hasListenerCallbacks);
180 
181             std::vector<ListenerCallbacks> listenerCallbacks;
182             int32_t listenersSize = 0;
183             SAFE_PARCEL_READ_SIZE(data.readInt32, &listenersSize, data.dataSize());
184             for (int32_t i = 0; i < listenersSize; i++) {
185                 SAFE_PARCEL(data.readStrongBinder, &tmpBinder);
186                 std::vector<CallbackId> callbackIds;
187                 SAFE_PARCEL(data.readParcelableVector, &callbackIds);
188                 listenerCallbacks.emplace_back(tmpBinder, callbackIds);
189             }
190 
191             uint64_t transactionId = -1;
192             SAFE_PARCEL(data.readUint64, &transactionId);
193 
194             SAFE_PARCEL_READ_SIZE(data.readUint32, &count, data.dataSize());
195             std::vector<uint64_t> mergedTransactions(count);
196             for (size_t i = 0; i < count; i++) {
197                 SAFE_PARCEL(data.readUint64, &mergedTransactions[i]);
198             }
199 
200             return setTransactionState(frameTimelineInfo, state, displays, stateFlags, applyToken,
201                                        std::move(inputWindowCommands), desiredPresentTime,
202                                        isAutoTimestamp, uncacheBuffers, hasListenerCallbacks,
203                                        listenerCallbacks, transactionId, mergedTransactions);
204         }
205         case GET_SCHEDULING_POLICY: {
206             gui::SchedulingPolicy policy;
207             const auto status = gui::getSchedulingPolicy(&policy);
208             if (!status.isOk()) {
209                 return status.exceptionCode();
210             }
211 
212             SAFE_PARCEL(reply->writeInt32, policy.policy);
213             SAFE_PARCEL(reply->writeInt32, policy.priority);
214             return NO_ERROR;
215         }
216 
217         default: {
218             return BBinder::onTransact(code, data, reply, flags);
219         }
220     }
221 }
222 
223 } // namespace android
224