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