blob: 8b6f2023dc323d2fb6bdaa2a36eda2956cbcb436 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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#define LOG_TAG "SurfaceComposerClient"
18
Vishnu Nair9402bb02022-09-28 16:45:06 -070019#include <semaphore.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
Patrick Williams641f7f22022-06-22 19:25:35 +000023#include <android/gui/BnWindowInfosReportedListener.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080024#include <android/gui/DisplayState.h>
Huihong Luod3d8f8e2022-03-08 14:48:46 -080025#include <android/gui/ISurfaceComposerClient.h>
chaviw60c9d3e2021-06-04 12:52:17 -050026#include <android/gui/IWindowInfosListener.h>
Chavi Weingarten076acac2023-01-19 17:20:43 +000027#include <android/gui/TrustedPresentationThresholds.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080028#include <android/os/IInputConstants.h>
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070029#include <gui/FrameRateUtils.h>
Patrick Williams640b7292022-10-11 19:25:06 +000030#include <gui/TraceUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <utils/SortedVector.h>
34#include <utils/String8.h>
35#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070037#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070039#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Michael Wright28f24d02016-07-12 13:30:53 -070041#include <system/graphics.h>
42
Huihong Luo3bdef862022-03-03 11:57:19 -080043#include <gui/AidlStatusUtil.h>
Robert Carr673134e2017-01-09 19:48:38 -080044#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070045#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080046#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080047#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070048#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080049#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080050#include <gui/SurfaceComposerClient.h>
chaviw98318de2021-05-19 16:45:23 -050051#include <gui/WindowInfo.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010052#include <private/gui/ParcelUtils.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010053#include <ui/DisplayMode.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080054#include <ui/DisplayState.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010055#include <ui/DynamicDisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Chavi Weingartene0237bb2023-02-06 21:48:32 +000057#include <android-base/thread_annotations.h>
Chavi Weingartenc78f53c2023-04-14 18:50:53 +000058#include <gui/LayerStatePermissions.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080059#include <private/gui/ComposerService.h>
Huihong Luof5029222021-12-16 14:33:46 -080060#include <private/gui/ComposerServiceAIDL.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Marissa Wall73411622019-01-25 10:45:41 -080062// This server size should always be smaller than the server cache size
Patrick Williams271afa92023-08-09 18:30:22 -050063#define BUFFER_CACHE_MAX_SIZE 4096
Marissa Wall73411622019-01-25 10:45:41 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070066
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050067using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
chaviw98318de2021-05-19 16:45:23 -050068using gui::FocusRequest;
Huihong Luoecc1f902021-11-20 11:55:05 -080069using gui::IRegionSamplingListener;
Chavi Weingarten076acac2023-01-19 17:20:43 +000070using gui::TrustedPresentationThresholds;
chaviw98318de2021-05-19 16:45:23 -050071using gui::WindowInfo;
72using gui::WindowInfoHandle;
chaviw60c9d3e2021-06-04 12:52:17 -050073using gui::WindowInfosListener;
Huihong Luo3bdef862022-03-03 11:57:19 -080074using gui::aidl_utils::statusTFromBinderStatus;
Peiyong Lin9f034472018-03-28 15:29:00 -070075using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076// ---------------------------------------------------------------------------
77
Mathias Agopian7e27f052010-05-28 14:22:23 -070078ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
Huihong Luof5029222021-12-16 14:33:46 -080079ANDROID_SINGLETON_STATIC_INSTANCE(ComposerServiceAIDL);
Mathias Agopian7e27f052010-05-28 14:22:23 -070080
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080081namespace {
82// Initialize transaction id counter used to generate transaction ids
83std::atomic<uint32_t> idCounter = 0;
84int64_t generateId() {
85 return (((int64_t)getpid()) << 32) | ++idCounter;
86}
Pascal Muetschard29247e52022-11-08 11:33:10 +010087
88void emptyCallback(nsecs_t, const sp<Fence>&, const std::vector<SurfaceControlStats>&) {}
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080089} // namespace
90
Mathias Agopianb7e930d2010-06-01 15:12:58 -070091ComposerService::ComposerService()
92: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070093 Mutex::Autolock _l(mLock);
94 connectLocked();
95}
96
Brian Lindahl9b85a192021-05-10 09:56:33 +000097bool ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070098 const String16 name("SurfaceFlinger");
Brian Lindahl9b85a192021-05-10 09:56:33 +000099 mComposerService = waitForService<ISurfaceComposer>(name);
100 if (mComposerService == nullptr) {
101 return false; // fatal error or permission problem
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700102 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700103
104 // Create the death listener.
105 class DeathObserver : public IBinder::DeathRecipient {
106 ComposerService& mComposerService;
107 virtual void binderDied(const wp<IBinder>& who) {
108 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
109 who.unsafe_get());
110 mComposerService.composerServiceDied();
111 }
112 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700113 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700114 };
115
116 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800117 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Brian Lindahl9b85a192021-05-10 09:56:33 +0000118 return true;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119}
120
Andy McFadden6652b3e2012-09-06 18:45:56 -0700121/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
122 ComposerService& instance = ComposerService::getInstance();
123 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700124 if (instance.mComposerService == nullptr) {
Brian Lindahl9b85a192021-05-10 09:56:33 +0000125 if (ComposerService::getInstance().connectLocked()) {
126 ALOGD("ComposerService reconnected");
127 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700128 }
129 return instance.mComposerService;
130}
131
132void ComposerService::composerServiceDied()
133{
134 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700135 mComposerService = nullptr;
136 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700137}
138
Huihong Luof5029222021-12-16 14:33:46 -0800139ComposerServiceAIDL::ComposerServiceAIDL() : Singleton<ComposerServiceAIDL>() {
140 std::scoped_lock lock(mMutex);
141 connectLocked();
142}
143
144bool ComposerServiceAIDL::connectLocked() {
145 const String16 name("SurfaceFlingerAIDL");
146 mComposerService = waitForService<gui::ISurfaceComposer>(name);
147 if (mComposerService == nullptr) {
148 return false; // fatal error or permission problem
149 }
150
151 // Create the death listener.
152 class DeathObserver : public IBinder::DeathRecipient {
153 ComposerServiceAIDL& mComposerService;
154 virtual void binderDied(const wp<IBinder>& who) {
155 ALOGW("ComposerService aidl remote (surfaceflinger) died [%p]", who.unsafe_get());
156 mComposerService.composerServiceDied();
157 }
158
159 public:
160 explicit DeathObserver(ComposerServiceAIDL& mgr) : mComposerService(mgr) {}
161 };
162
163 mDeathObserver = new DeathObserver(*const_cast<ComposerServiceAIDL*>(this));
164 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
165 return true;
166}
167
168/*static*/ sp<gui::ISurfaceComposer> ComposerServiceAIDL::getComposerService() {
169 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
170 std::scoped_lock lock(instance.mMutex);
171 if (instance.mComposerService == nullptr) {
172 if (ComposerServiceAIDL::getInstance().connectLocked()) {
173 ALOGD("ComposerServiceAIDL reconnected");
Huihong Luo02186fb2022-02-23 14:21:54 -0800174 WindowInfosListenerReporter::getInstance()->reconnect(instance.mComposerService);
Huihong Luof5029222021-12-16 14:33:46 -0800175 }
176 }
177 return instance.mComposerService;
178}
179
180void ComposerServiceAIDL::composerServiceDied() {
181 std::scoped_lock lock(mMutex);
182 mComposerService = nullptr;
183 mDeathObserver = nullptr;
184}
185
Robert Carrfb4d58b2019-01-15 09:21:27 -0800186class DefaultComposerClient: public Singleton<DefaultComposerClient> {
187 Mutex mLock;
188 sp<SurfaceComposerClient> mClient;
189 friend class Singleton<ComposerService>;
190public:
191 static sp<SurfaceComposerClient> getComposerClient() {
192 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
193 Mutex::Autolock _l(dc.mLock);
194 if (dc.mClient == nullptr) {
195 dc.mClient = new SurfaceComposerClient;
196 }
197 return dc.mClient;
198 }
199};
200ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
201
202
203sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
204 return DefaultComposerClient::getComposerClient();
205}
206
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100207JankDataListener::~JankDataListener() {
208}
209
Mathias Agopian7e27f052010-05-28 14:22:23 -0700210// ---------------------------------------------------------------------------
211
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700212// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
213// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
214// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
215
Marissa Wallc837b5e2018-10-12 10:04:44 -0700216// 0 is an invalid callback id
217TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
218
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700219int64_t TransactionCompletedListener::getNextIdLocked() {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700220 return mCallbackIdCounter++;
221}
222
chaviw0b06a8d2021-08-06 11:49:08 -0500223sp<TransactionCompletedListener> TransactionCompletedListener::sInstance = nullptr;
Robert Carr2df7c452022-04-04 12:21:37 -0700224static std::mutex sListenerInstanceMutex;
chaviw0b06a8d2021-08-06 11:49:08 -0500225
226void TransactionCompletedListener::setInstance(const sp<TransactionCompletedListener>& listener) {
227 sInstance = listener;
228}
229
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700230sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
Robert Carr2df7c452022-04-04 12:21:37 -0700231 std::lock_guard<std::mutex> lock(sListenerInstanceMutex);
chaviw0b06a8d2021-08-06 11:49:08 -0500232 if (sInstance == nullptr) {
233 sInstance = new TransactionCompletedListener;
234 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700235 return sInstance;
236}
237
Marissa Wallc837b5e2018-10-12 10:04:44 -0700238sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
239 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
240}
241
242void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700243 if (mListening) {
244 return;
245 }
246 ProcessState::self()->startThreadPool();
247 mListening = true;
248}
249
Marissa Wall80d94ad2019-01-18 16:04:36 -0800250CallbackId TransactionCompletedListener::addCallbackFunction(
251 const TransactionCompletedCallback& callbackFunction,
252 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700253 surfaceControls,
254 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700255 std::lock_guard<std::mutex> lock(mMutex);
Pascal Muetschard29247e52022-11-08 11:33:10 +0100256 return addCallbackFunctionLocked(callbackFunction, surfaceControls, callbackType);
257}
258
259CallbackId TransactionCompletedListener::addCallbackFunctionLocked(
260 const TransactionCompletedCallback& callbackFunction,
261 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
262 surfaceControls,
263 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700264 startListeningLocked();
265
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700266 CallbackId callbackId(getNextIdLocked(), callbackType);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800267 mCallbacks[callbackId].callbackFunction = callbackFunction;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800268 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
269
270 for (const auto& surfaceControl : surfaceControls) {
271 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
Pascal Muetschardf54181b2022-12-13 14:53:25 +0100272
273 if (callbackType == CallbackId::Type::ON_COMPLETE &&
274 mJankListeners.count(surfaceControl->getLayerId()) != 0) {
275 callbackId.includeJankData = true;
276 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800277 }
278
Marissa Wallc837b5e2018-10-12 10:04:44 -0700279 return callbackId;
280}
281
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100282void TransactionCompletedListener::addJankListener(const sp<JankDataListener>& listener,
283 sp<SurfaceControl> surfaceControl) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200284 std::lock_guard<std::mutex> lock(mMutex);
chaviw29bc5362021-09-07 16:17:13 -0500285 mJankListeners.insert({surfaceControl->getLayerId(), listener});
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100286}
287
288void TransactionCompletedListener::removeJankListener(const sp<JankDataListener>& listener) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200289 std::lock_guard<std::mutex> lock(mMutex);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100290 for (auto it = mJankListeners.begin(); it != mJankListeners.end();) {
291 if (it->second == listener) {
292 it = mJankListeners.erase(it);
293 } else {
294 it++;
295 }
296 }
297}
298
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700299void TransactionCompletedListener::setReleaseBufferCallback(const ReleaseCallbackId& callbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800300 ReleaseBufferCallback listener) {
301 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700302 mReleaseBufferCallbacks[callbackId] = listener;
Vishnu Nair1506b182021-02-22 14:35:15 -0800303}
304
Jorim Jaggif51775d2021-01-14 23:44:15 +0100305void TransactionCompletedListener::addSurfaceStatsListener(void* context, void* cookie,
306 sp<SurfaceControl> surfaceControl, SurfaceStatsCallback listener) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200307 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500308 mSurfaceStatsListeners.insert(
309 {surfaceControl->getLayerId(), SurfaceStatsCallbackEntry(context, cookie, listener)});
Jorim Jaggif51775d2021-01-14 23:44:15 +0100310}
311
312void TransactionCompletedListener::removeSurfaceStatsListener(void* context, void* cookie) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200313 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100314 for (auto it = mSurfaceStatsListeners.begin(); it != mSurfaceStatsListeners.end();) {
315 auto [itContext, itCookie, itListener] = it->second;
316 if (itContext == context && itCookie == cookie) {
317 it = mSurfaceStatsListeners.erase(it);
318 } else {
319 it++;
320 }
321 }
322}
323
Marissa Wall80d94ad2019-01-18 16:04:36 -0800324void TransactionCompletedListener::addSurfaceControlToCallbacks(
Pascal Muetschard29247e52022-11-08 11:33:10 +0100325 SurfaceComposerClient::CallbackInfo& callbackInfo,
326 const sp<SurfaceControl>& surfaceControl) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800327 std::lock_guard<std::mutex> lock(mMutex);
328
Pascal Muetschard29247e52022-11-08 11:33:10 +0100329 bool includingJankData = false;
330 for (auto callbackId : callbackInfo.callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800331 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
332 std::forward_as_tuple(
333 surfaceControl->getHandle()),
334 std::forward_as_tuple(surfaceControl));
Pascal Muetschard29247e52022-11-08 11:33:10 +0100335 includingJankData = includingJankData || callbackId.includeJankData;
336 }
337
338 // If no registered callback is requesting jank data, but there is a jank listener registered
339 // on the new surface control, add a synthetic callback that requests the jank data.
340 if (!includingJankData && mJankListeners.count(surfaceControl->getLayerId()) != 0) {
341 CallbackId callbackId =
342 addCallbackFunctionLocked(&emptyCallback, callbackInfo.surfaceControls,
343 CallbackId::Type::ON_COMPLETE);
344 callbackInfo.callbackIds.emplace(callbackId);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800345 }
346}
347
Huihong Luoffee3bc2023-01-17 16:14:35 +0000348void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700349 std::unordered_map<CallbackId, CallbackTranslation, CallbackIdHash> callbacksMap;
chaviw29bc5362021-09-07 16:17:13 -0500350 std::multimap<int32_t, sp<JankDataListener>> jankListenersMap;
Valerie Haud3b90d22019-11-06 09:37:31 -0800351 {
352 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700353
Valerie Haud3b90d22019-11-06 09:37:31 -0800354 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
355 * callbackIds, except for when Transactions are merged together. This probably cannot be
356 * solved before this point because the Transactions could be merged together and applied in
357 * a different process.
358 *
359 * Fortunately, we get all the callbacks for this listener for the same frame together at
360 * the same time. This means if any Transactions were merged together, we will get their
361 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
362 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
363 * sp<SurfaceControl> that could possibly exist for the callbacks.
364 */
365 callbacksMap = mCallbacks;
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200366 jankListenersMap = mJankListeners;
Valerie Haud3b90d22019-11-06 09:37:31 -0800367 for (const auto& transactionStats : listenerStats.transactionStats) {
368 for (auto& callbackId : transactionStats.callbackIds) {
369 mCallbacks.erase(callbackId);
370 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800371 }
372 }
Marissa Walld600d572019-03-26 15:38:50 -0700373 for (const auto& transactionStats : listenerStats.transactionStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700374 // handle on commit callbacks
Marissa Walld600d572019-03-26 15:38:50 -0700375 for (auto callbackId : transactionStats.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700376 if (callbackId.type != CallbackId::Type::ON_COMMIT) {
377 continue;
378 }
379 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
380 if (!callbackFunction) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700381 continue;
382 }
383 std::vector<SurfaceControlStats> surfaceControlStats;
384 for (const auto& surfaceStats : transactionStats.surfaceStats) {
385 surfaceControlStats
386 .emplace_back(callbacksMap[callbackId]
387 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800388 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700389 transactionStats.presentFence,
390 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800391 surfaceStats.eventStats,
392 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700393 }
394
395 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
396 surfaceControlStats);
Patrick Williams30da3672023-07-25 16:52:01 -0500397
398 // More than one transaction may contain the same callback id. Erase the callback from
399 // the map to ensure that it is only called once. This can happen if transactions are
400 // parcelled out of process and applied in both processes.
401 callbacksMap.erase(callbackId);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700402 }
403
404 // handle on complete callbacks
405 for (auto callbackId : transactionStats.callbackIds) {
406 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
407 continue;
408 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800409 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-18 16:04:36 -0800410 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700411 ALOGE("cannot call null callback function, skipping");
412 continue;
413 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800414 std::vector<SurfaceControlStats> surfaceControlStats;
415 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800416 surfaceControlStats
417 .emplace_back(callbacksMap[callbackId]
418 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800419 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Valerie Hau871d6352020-01-29 08:44:02 -0800420 transactionStats.presentFence,
421 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800422 surfaceStats.eventStats,
423 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair71fcf912022-10-18 09:14:20 -0700424 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl] &&
425 surfaceStats.transformHint.has_value()) {
Valerie Hau84d87ff2020-01-08 17:23:21 -0800426 callbacksMap[callbackId]
427 .surfaceControls[surfaceStats.surfaceControl]
Vishnu Nair71fcf912022-10-18 09:14:20 -0700428 ->setTransformHint(*surfaceStats.transformHint);
Valerie Hau84d87ff2020-01-08 17:23:21 -0800429 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800430 // If there is buffer id set, we look up any pending client release buffer callbacks
431 // and call them. This is a performance optimization when we have a transaction
432 // callback and a release buffer callback happening at the same time to avoid an
433 // additional ipc call from the server.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700434 if (surfaceStats.previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
Vishnu Nair5faedf52021-03-22 11:32:58 -0700435 ReleaseBufferCallback callback;
436 {
437 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700438 callback = popReleaseBufferCallbackLocked(
439 surfaceStats.previousReleaseCallbackId);
Vishnu Nair5faedf52021-03-22 11:32:58 -0700440 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800441 if (callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700442 callback(surfaceStats.previousReleaseCallbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800443 surfaceStats.previousReleaseFence
444 ? surfaceStats.previousReleaseFence
Robert Carr97e7cc02021-06-07 10:45:40 -0700445 : Fence::NO_FENCE,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700446 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800447 }
448 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800449 }
450
451 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
452 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700453 }
Vishnu Nair6f4d2f52023-08-10 12:33:12 -0700454 }
chaviw29bc5362021-09-07 16:17:13 -0500455
Vishnu Nair6f4d2f52023-08-10 12:33:12 -0700456 for (const auto& transactionStats : listenerStats.transactionStats) {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100457 for (const auto& surfaceStats : transactionStats.surfaceStats) {
chaviw29bc5362021-09-07 16:17:13 -0500458 // The callbackMap contains the SurfaceControl object, which we need to look up the
459 // layerId. Since we don't know which callback contains the SurfaceControl, iterate
460 // through all until the SC is found.
461 int32_t layerId = -1;
462 for (auto callbackId : transactionStats.callbackIds) {
Pablo Gamito10026472021-11-22 22:16:45 +0100463 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
464 // We only want to run the stats callback for ON_COMPLETE
465 continue;
466 }
chaviw29bc5362021-09-07 16:17:13 -0500467 sp<SurfaceControl> sc =
468 callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl];
469 if (sc != nullptr) {
470 layerId = sc->getLayerId();
471 break;
472 }
473 }
474
Pablo Gamito10026472021-11-22 22:16:45 +0100475 if (layerId != -1) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200476 // Acquire surface stats listener lock such that we guarantee that after calling
477 // unregister, there won't be any further callback.
478 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500479 auto listenerRange = mSurfaceStatsListeners.equal_range(layerId);
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200480 for (auto it = listenerRange.first; it != listenerRange.second; it++) {
481 auto entry = it->second;
482 entry.callback(entry.context, transactionStats.latchTime,
483 transactionStats.presentFence, surfaceStats);
484 }
Jorim Jaggif51775d2021-01-14 23:44:15 +0100485 }
486
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100487 if (surfaceStats.jankData.empty()) continue;
chaviw29bc5362021-09-07 16:17:13 -0500488 auto jankRange = jankListenersMap.equal_range(layerId);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100489 for (auto it = jankRange.first; it != jankRange.second; it++) {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100490 it->second->onJankDataAvailable(surfaceStats.jankData);
491 }
492 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700493 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700494}
495
Huihong Luoffee3bc2023-01-17 16:14:35 +0000496void TransactionCompletedListener::onTransactionQueueStalled(const String8& reason) {
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000497 std::unordered_map<void*, std::function<void(const std::string&)>> callbackCopy;
498 {
499 std::scoped_lock<std::mutex> lock(mMutex);
500 callbackCopy = mQueueStallListeners;
501 }
502 for (auto const& it : callbackCopy) {
503 it.second(reason.c_str());
504 }
Robert Carr4c1b6462021-12-21 10:30:50 -0800505}
506
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000507void TransactionCompletedListener::addQueueStallListener(
508 std::function<void(const std::string&)> stallListener, void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800509 std::scoped_lock<std::mutex> lock(mMutex);
510 mQueueStallListeners[id] = stallListener;
511}
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000512
513void TransactionCompletedListener::removeQueueStallListener(void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800514 std::scoped_lock<std::mutex> lock(mMutex);
515 mQueueStallListeners.erase(id);
516}
517
Huihong Luoffee3bc2023-01-17 16:14:35 +0000518void TransactionCompletedListener::onReleaseBuffer(ReleaseCallbackId callbackId,
519 sp<Fence> releaseFence,
520 uint32_t currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800521 ReleaseBufferCallback callback;
522 {
523 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700524 callback = popReleaseBufferCallbackLocked(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800525 }
526 if (!callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700527 ALOGE("Could not call release buffer callback, buffer not found %s",
528 callbackId.to_string().c_str());
Huihong Luoffee3bc2023-01-17 16:14:35 +0000529 return;
Vishnu Nair1506b182021-02-22 14:35:15 -0800530 }
chaviw69058fb2021-09-27 09:37:30 -0500531 std::optional<uint32_t> optionalMaxAcquiredBufferCount =
Huihong Luoffee3bc2023-01-17 16:14:35 +0000532 currentMaxAcquiredBufferCount == UINT_MAX
chaviw69058fb2021-09-27 09:37:30 -0500533 ? std::nullopt
534 : std::make_optional<uint32_t>(currentMaxAcquiredBufferCount);
535 callback(callbackId, releaseFence, optionalMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800536}
537
538ReleaseBufferCallback TransactionCompletedListener::popReleaseBufferCallbackLocked(
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700539 const ReleaseCallbackId& callbackId) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800540 ReleaseBufferCallback callback;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700541 auto itr = mReleaseBufferCallbacks.find(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800542 if (itr == mReleaseBufferCallbacks.end()) {
543 return nullptr;
544 }
545 callback = itr->second;
546 mReleaseBufferCallbacks.erase(itr);
547 return callback;
548}
549
chaviw0acd33a2021-11-02 11:55:37 -0500550void TransactionCompletedListener::removeReleaseBufferCallback(
551 const ReleaseCallbackId& callbackId) {
552 {
553 std::scoped_lock<std::mutex> lock(mMutex);
554 popReleaseBufferCallbackLocked(callbackId);
555 }
556}
557
Chavi Weingarten076acac2023-01-19 17:20:43 +0000558SurfaceComposerClient::PresentationCallbackRAII::PresentationCallbackRAII(
559 TransactionCompletedListener* tcl, int id) {
560 mTcl = tcl;
561 mId = id;
562}
563
564SurfaceComposerClient::PresentationCallbackRAII::~PresentationCallbackRAII() {
565 mTcl->clearTrustedPresentationCallback(mId);
566}
567
568sp<SurfaceComposerClient::PresentationCallbackRAII>
569TransactionCompletedListener::addTrustedPresentationCallback(TrustedPresentationCallback tpc,
570 int id, void* context) {
571 std::scoped_lock<std::mutex> lock(mMutex);
572 mTrustedPresentationCallbacks[id] =
573 std::tuple<TrustedPresentationCallback, void*>(tpc, context);
574 return new SurfaceComposerClient::PresentationCallbackRAII(this, id);
575}
576
577void TransactionCompletedListener::clearTrustedPresentationCallback(int id) {
578 std::scoped_lock<std::mutex> lock(mMutex);
579 mTrustedPresentationCallbacks.erase(id);
580}
581
582void TransactionCompletedListener::onTrustedPresentationChanged(int id,
583 bool presentedWithinThresholds) {
584 TrustedPresentationCallback tpc;
585 void* context;
586 {
587 std::scoped_lock<std::mutex> lock(mMutex);
588 auto it = mTrustedPresentationCallbacks.find(id);
589 if (it == mTrustedPresentationCallbacks.end()) {
590 return;
591 }
592 std::tie(tpc, context) = it->second;
593 }
594 tpc(context, presentedWithinThresholds);
595}
596
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700597// ---------------------------------------------------------------------------
598
Robert Carre06ad2b2020-04-10 15:09:33 -0700599void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 14:58:34 -0700600
Robert Carre06ad2b2020-04-10 15:09:33 -0700601/**
602 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
603 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
604 * 1. Cost of sending the FD
605 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
606 * To ease this cost we implement the following scheme of caching buffers to integers,
607 * or said-otherwise, naming them with integers. This is the scheme known as slots in
608 * the legacy BufferQueue system.
609 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
610 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
611 * send the cached integer.
612 * 3. If there is a cache miss, we cache the new buffer and send the integer
613 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
614 * entry, and we use the integer for further communication.
615 * A few details about lifetime:
616 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
617 * which is per process Unique. The server side cache is larger than the client side
618 * cache so that the server will never evict entries before the client.
619 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
620 * transaction.
621 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
622 * to auto-evict destroyed buffers.
623 */
Marissa Wall73411622019-01-25 10:45:41 -0800624class BufferCache : public Singleton<BufferCache> {
625public:
626 BufferCache() : token(new BBinder()) {}
627
628 sp<IBinder> getToken() {
629 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
630 }
631
Marissa Wall78b72202019-03-15 14:58:34 -0700632 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800633 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800634
Marissa Wall78b72202019-03-15 14:58:34 -0700635 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800636 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700637 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800638 }
Marissa Wall78b72202019-03-15 14:58:34 -0700639 itr->second = getCounter();
640 *cacheId = buffer->getId();
641 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800642 }
643
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000644 uint64_t cache(const sp<GraphicBuffer>& buffer,
645 std::optional<client_cache_t>& outUncacheBuffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800646 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800647
Marissa Wall78b72202019-03-15 14:58:34 -0700648 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000649 outUncacheBuffer = findLeastRecentlyUsedBuffer();
650 mBuffers.erase(outUncacheBuffer->id);
Marissa Wall78b72202019-03-15 14:58:34 -0700651 }
Marissa Wall73411622019-01-25 10:45:41 -0800652
Robert Carre06ad2b2020-04-10 15:09:33 -0700653 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 14:58:34 -0700654
655 mBuffers[buffer->getId()] = getCounter();
656 return buffer->getId();
657 }
658
659 void uncache(uint64_t cacheId) {
660 std::lock_guard<std::mutex> lock(mMutex);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000661 if (mBuffers.erase(cacheId)) {
662 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
663 }
Marissa Wall73411622019-01-25 10:45:41 -0800664 }
665
666private:
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000667 client_cache_t findLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800668 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700669 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800670 auto minBuffer = itr;
671 itr++;
672
673 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700674 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800675 if (counter < minCounter) {
676 minCounter = counter;
677 minBuffer = itr;
678 }
679 itr++;
680 }
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000681
682 return {.token = getToken(), .id = minBuffer->first};
Marissa Wall73411622019-01-25 10:45:41 -0800683 }
684
685 uint64_t getCounter() REQUIRES(mMutex) {
686 static uint64_t counter = 0;
687 return counter++;
688 }
689
Marissa Wall73411622019-01-25 10:45:41 -0800690 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700691 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800692
693 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
694 sp<IBinder> token;
695};
696
697ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
698
Robert Carre06ad2b2020-04-10 15:09:33 -0700699void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 14:58:34 -0700700 // GraphicBuffer id's are used as the cache ids.
701 BufferCache::getInstance().uncache(graphicBufferId);
702}
703
Marissa Wall73411622019-01-25 10:45:41 -0800704// ---------------------------------------------------------------------------
705
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000706SurfaceComposerClient::Transaction::Transaction() {
707 mId = generateId();
708}
709
Marissa Wall17b4e452018-12-26 16:32:34 -0800710SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000711 : mId(other.mId),
Marissa Wall17b4e452018-12-26 16:32:34 -0800712 mTransactionNestCount(other.mTransactionNestCount),
713 mAnimation(other.mAnimation),
Ady Abraham8cbd3072021-03-15 16:39:06 -0700714 mEarlyWakeupStart(other.mEarlyWakeupStart),
715 mEarlyWakeupEnd(other.mEarlyWakeupEnd),
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000716 mMayContainBuffer(other.mMayContainBuffer),
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700717 mDesiredPresentTime(other.mDesiredPresentTime),
Ady Abrahamf0c56492020-12-17 18:04:15 -0800718 mIsAutoTimestamp(other.mIsAutoTimestamp),
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000719 mFrameTimelineInfo(other.mFrameTimelineInfo),
Patrick Williamse5991f52022-06-23 20:23:43 +0000720 mApplyToken(other.mApplyToken) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700721 mDisplayStates = other.mDisplayStates;
722 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800723 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700724 mListenerCallbacks = other.mListenerCallbacks;
725}
726
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000727void SurfaceComposerClient::Transaction::sanitize(int pid, int uid) {
728 uint32_t permissions = LayerStatePermissions::getTransactionPermissions(pid, uid);
Robert Carrde6d7b42022-01-07 18:23:06 -0800729 for (auto & [handle, composerState] : mComposerStates) {
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000730 composerState.state.sanitize(permissions);
Robert Carrde6d7b42022-01-07 18:23:06 -0800731 }
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000732 if (!mInputWindowCommands.empty() &&
733 (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) {
734 ALOGE("Only privileged callers are allowed to send input commands.");
735 mInputWindowCommands.clear();
736 }
Robert Carrde6d7b42022-01-07 18:23:06 -0800737}
738
Vishnu Nair621102e2019-06-12 14:16:57 -0700739std::unique_ptr<SurfaceComposerClient::Transaction>
740SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
741 auto transaction = std::make_unique<Transaction>();
742 if (transaction->readFromParcel(parcel) == NO_ERROR) {
743 return transaction;
744 }
745 return nullptr;
746}
747
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000748
Vishnu Nair621102e2019-06-12 14:16:57 -0700749status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
Pablo Gamito825df7f2022-06-23 14:45:44 +0000750 const uint64_t transactionId = parcel->readUint64();
Vishnu Nair621102e2019-06-12 14:16:57 -0700751 const uint32_t transactionNestCount = parcel->readUint32();
752 const bool animation = parcel->readBool();
Ady Abraham8cbd3072021-03-15 16:39:06 -0700753 const bool earlyWakeupStart = parcel->readBool();
754 const bool earlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 14:16:57 -0700755 const int64_t desiredPresentTime = parcel->readInt64();
Ady Abrahamf0c56492020-12-17 18:04:15 -0800756 const bool isAutoTimestamp = parcel->readBool();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000757 FrameTimelineInfo frameTimelineInfo;
Huihong Luo3bdef862022-03-03 11:57:19 -0800758 frameTimelineInfo.readFromParcel(parcel);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000759
Vishnu Nair277142c2021-01-05 18:35:29 -0800760 sp<IBinder> applyToken;
761 parcel->readNullableStrongBinder(&applyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700762 size_t count = static_cast<size_t>(parcel->readUint32());
763 if (count > parcel->dataSize()) {
764 return BAD_VALUE;
765 }
766 SortedVector<DisplayState> displayStates;
767 displayStates.setCapacity(count);
768 for (size_t i = 0; i < count; i++) {
769 DisplayState displayState;
770 if (displayState.read(*parcel) == BAD_VALUE) {
771 return BAD_VALUE;
772 }
773 displayStates.add(displayState);
774 }
775
776 count = static_cast<size_t>(parcel->readUint32());
777 if (count > parcel->dataSize()) {
778 return BAD_VALUE;
779 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700780 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
781 listenerCallbacks.reserve(count);
782 for (size_t i = 0; i < count; i++) {
783 sp<ITransactionCompletedListener> listener =
784 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
785 size_t numCallbackIds = parcel->readUint32();
786 if (numCallbackIds > parcel->dataSize()) {
787 return BAD_VALUE;
788 }
789 for (size_t j = 0; j < numCallbackIds; j++) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700790 CallbackId id;
791 parcel->readParcelable(&id);
792 listenerCallbacks[listener].callbackIds.insert(id);
Valerie Hau9dab9732019-08-20 09:29:25 -0700793 }
794 size_t numSurfaces = parcel->readUint32();
795 if (numSurfaces > parcel->dataSize()) {
796 return BAD_VALUE;
797 }
798 for (size_t j = 0; j < numSurfaces; j++) {
799 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000800 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 09:29:25 -0700801 listenerCallbacks[listener].surfaceControls.insert(surface);
802 }
803 }
804
805 count = static_cast<size_t>(parcel->readUint32());
806 if (count > parcel->dataSize()) {
807 return BAD_VALUE;
808 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700809 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 14:16:57 -0700810 composerStates.reserve(count);
811 for (size_t i = 0; i < count; i++) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000812 sp<IBinder> surfaceControlHandle;
813 SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700814
815 ComposerState composerState;
816 if (composerState.read(*parcel) == BAD_VALUE) {
817 return BAD_VALUE;
818 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700819 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 14:16:57 -0700820 }
821
822 InputWindowCommands inputWindowCommands;
823 inputWindowCommands.read(*parcel);
824
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000825 count = static_cast<size_t>(parcel->readUint32());
826 if (count > parcel->dataSize()) {
827 return BAD_VALUE;
828 }
829 std::vector<client_cache_t> uncacheBuffers(count);
830 for (size_t i = 0; i < count; i++) {
831 sp<IBinder> tmpBinder;
832 SAFE_PARCEL(parcel->readStrongBinder, &tmpBinder);
833 uncacheBuffers[i].token = tmpBinder;
834 SAFE_PARCEL(parcel->readUint64, &uncacheBuffers[i].id);
835 }
836
Pablo Gamito23780be2023-04-18 08:30:00 +0000837 count = static_cast<size_t>(parcel->readUint32());
838 if (count > parcel->dataSize()) {
839 return BAD_VALUE;
840 }
841 std::vector<uint64_t> mergedTransactionIds(count);
842 for (size_t i = 0; i < count; i++) {
843 SAFE_PARCEL(parcel->readUint64, &mergedTransactionIds[i]);
844 }
845
Vishnu Nair621102e2019-06-12 14:16:57 -0700846 // Parsing was successful. Update the object.
Pablo Gamito825df7f2022-06-23 14:45:44 +0000847 mId = transactionId;
Vishnu Nair621102e2019-06-12 14:16:57 -0700848 mTransactionNestCount = transactionNestCount;
849 mAnimation = animation;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700850 mEarlyWakeupStart = earlyWakeupStart;
851 mEarlyWakeupEnd = earlyWakeupEnd;
Vishnu Nair621102e2019-06-12 14:16:57 -0700852 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800853 mIsAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000854 mFrameTimelineInfo = frameTimelineInfo;
Vishnu Nair621102e2019-06-12 14:16:57 -0700855 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 09:29:25 -0700856 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 14:16:57 -0700857 mComposerStates = composerStates;
858 mInputWindowCommands = inputWindowCommands;
Vishnu Nair277142c2021-01-05 18:35:29 -0800859 mApplyToken = applyToken;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000860 mUncacheBuffers = std::move(uncacheBuffers);
Pablo Gamito23780be2023-04-18 08:30:00 +0000861 mMergedTransactionIds = std::move(mergedTransactionIds);
Vishnu Nair621102e2019-06-12 14:16:57 -0700862 return NO_ERROR;
863}
864
865status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700866 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
867 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
868 // but is unlikely to use them again as they are owned by the other process.
869 // You may be asking yourself, is this const cast safe? Const cast is safe up
870 // until the point where you try and write to an object that was originally const at which
871 // point we enter undefined behavior. In this case we are safe though, because there are
872 // two possibilities:
873 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
874 // 2. It was originall const! In this case not only was it useless, but it by definition
875 // contains no composer states and so cacheBuffers will not perform any writes.
876
877 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
878
Pablo Gamito825df7f2022-06-23 14:45:44 +0000879 parcel->writeUint64(mId);
Vishnu Nair621102e2019-06-12 14:16:57 -0700880 parcel->writeUint32(mTransactionNestCount);
881 parcel->writeBool(mAnimation);
Ady Abraham8cbd3072021-03-15 16:39:06 -0700882 parcel->writeBool(mEarlyWakeupStart);
883 parcel->writeBool(mEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 14:16:57 -0700884 parcel->writeInt64(mDesiredPresentTime);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800885 parcel->writeBool(mIsAutoTimestamp);
Huihong Luo3bdef862022-03-03 11:57:19 -0800886 mFrameTimelineInfo.writeToParcel(parcel);
Vishnu Nair277142c2021-01-05 18:35:29 -0800887 parcel->writeStrongBinder(mApplyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700888 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
889 for (auto const& displayState : mDisplayStates) {
890 displayState.write(*parcel);
891 }
892
Valerie Hau9dab9732019-08-20 09:29:25 -0700893 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
894 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
895 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
896 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
897 for (auto callbackId : callbackInfo.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700898 parcel->writeParcelable(callbackId);
Valerie Hau9dab9732019-08-20 09:29:25 -0700899 }
900 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
901 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45 +0000902 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 09:29:25 -0700903 }
904 }
905
Vishnu Nair621102e2019-06-12 14:16:57 -0700906 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000907 for (auto const& [handle, composerState] : mComposerStates) {
908 SAFE_PARCEL(parcel->writeStrongBinder, handle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700909 composerState.write(*parcel);
910 }
911
912 mInputWindowCommands.write(*parcel);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000913
914 SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mUncacheBuffers.size()));
915 for (const client_cache_t& uncacheBuffer : mUncacheBuffers) {
916 SAFE_PARCEL(parcel->writeStrongBinder, uncacheBuffer.token.promote());
917 SAFE_PARCEL(parcel->writeUint64, uncacheBuffer.id);
918 }
919
Pablo Gamito23780be2023-04-18 08:30:00 +0000920 SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mMergedTransactionIds.size()));
921 for (auto mergedTransactionId : mMergedTransactionIds) {
922 SAFE_PARCEL(parcel->writeUint64, mergedTransactionId);
923 }
924
Vishnu Nair621102e2019-06-12 14:16:57 -0700925 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -0700926}
927
chaviw69058fb2021-09-27 09:37:30 -0500928void SurfaceComposerClient::Transaction::releaseBufferIfOverwriting(const layer_state_t& state) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700929 if (!(state.what & layer_state_t::eBufferChanged) || !state.bufferData->hasBuffer()) {
chaviw69058fb2021-09-27 09:37:30 -0500930 return;
931 }
932
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800933 auto listener = state.bufferData->releaseBufferListener;
chaviw69058fb2021-09-27 09:37:30 -0500934 sp<Fence> fence =
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800935 state.bufferData->acquireFence ? state.bufferData->acquireFence : Fence::NO_FENCE;
936 if (state.bufferData->releaseBufferEndpoint ==
chaviw69058fb2021-09-27 09:37:30 -0500937 IInterface::asBinder(TransactionCompletedListener::getIInstance())) {
938 // if the callback is in process, run on a different thread to avoid any lock contigency
939 // issues in the client.
940 SurfaceComposerClient::getDefault()
chaviw8dd181f2022-01-05 18:36:46 -0600941 ->mReleaseCallbackThread
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800942 .addReleaseCallback(state.bufferData->generateReleaseCallbackId(), fence);
chaviw69058fb2021-09-27 09:37:30 -0500943 } else {
Huihong Luoffee3bc2023-01-17 16:14:35 +0000944 listener->onReleaseBuffer(state.bufferData->generateReleaseCallbackId(), fence, UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -0500945 }
946}
947
Robert Carr2c5f6d22017-09-26 12:30:35 -0700948SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Pablo Gamito23780be2023-04-18 08:30:00 +0000949 while (mMergedTransactionIds.size() + other.mMergedTransactionIds.size() >
950 MAX_MERGE_HISTORY_LENGTH - 1 &&
951 mMergedTransactionIds.size() > 0) {
952 mMergedTransactionIds.pop_back();
953 }
954 if (other.mMergedTransactionIds.size() == MAX_MERGE_HISTORY_LENGTH) {
955 mMergedTransactionIds.insert(mMergedTransactionIds.begin(),
956 other.mMergedTransactionIds.begin(),
957 other.mMergedTransactionIds.end() - 1);
958 } else if (other.mMergedTransactionIds.size() > 0u) {
959 mMergedTransactionIds.insert(mMergedTransactionIds.begin(),
960 other.mMergedTransactionIds.begin(),
961 other.mMergedTransactionIds.end());
962 }
963 mMergedTransactionIds.insert(mMergedTransactionIds.begin(), other.mId);
964
Pablo Gamitodbc31672020-09-01 18:28:58 +0000965 for (auto const& [handle, composerState] : other.mComposerStates) {
966 if (mComposerStates.count(handle) == 0) {
967 mComposerStates[handle] = composerState;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700968 } else {
chaviw69058fb2021-09-27 09:37:30 -0500969 if (composerState.state.what & layer_state_t::eBufferChanged) {
970 releaseBufferIfOverwriting(mComposerStates[handle].state);
971 }
Pablo Gamitodbc31672020-09-01 18:28:58 +0000972 mComposerStates[handle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700973 }
974 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700975
976 for (auto const& state : other.mDisplayStates) {
977 ssize_t index = mDisplayStates.indexOf(state);
978 if (index < 0) {
979 mDisplayStates.add(state);
980 } else {
981 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
982 }
983 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700984
Marissa Wallc837b5e2018-10-12 10:04:44 -0700985 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
986 auto& [callbackIds, surfaceControls] = callbackInfo;
987 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
988 callbackIds.begin()),
989 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -0700990
Valerie Hau236eba32020-01-03 16:53:39 -0800991 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
992 surfaceControls.end());
993
994 auto& currentProcessCallbackInfo =
995 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
996 currentProcessCallbackInfo.surfaceControls
997 .insert(std::make_move_iterator(surfaceControls.begin()),
998 std::make_move_iterator(surfaceControls.end()));
999
1000 // register all surface controls for all callbackIds for this listener that is merging
1001 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
1002 TransactionCompletedListener::getInstance()
Pascal Muetschard29247e52022-11-08 11:33:10 +01001003 ->addSurfaceControlToCallbacks(currentProcessCallbackInfo, surfaceControl);
Valerie Hau236eba32020-01-03 16:53:39 -08001004 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001005 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001006
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001007 for (const auto& cacheId : other.mUncacheBuffers) {
1008 mUncacheBuffers.push_back(cacheId);
1009 }
1010
chaviw273171b2018-12-26 11:46:30 -08001011 mInputWindowCommands.merge(other.mInputWindowCommands);
1012
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001013 mMayContainBuffer |= other.mMayContainBuffer;
Ady Abraham8cbd3072021-03-15 16:39:06 -07001014 mEarlyWakeupStart = mEarlyWakeupStart || other.mEarlyWakeupStart;
1015 mEarlyWakeupEnd = mEarlyWakeupEnd || other.mEarlyWakeupEnd;
Vishnu Nair277142c2021-01-05 18:35:29 -08001016 mApplyToken = other.mApplyToken;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001017
Huihong Luo3bdef862022-03-03 11:57:19 -08001018 mergeFrameTimelineInfo(mFrameTimelineInfo, other.mFrameTimelineInfo);
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001019
Vishnu Nairfef244e2019-06-17 18:07:51 -07001020 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -07001021 return *this;
1022}
1023
Vishnu Nairfef244e2019-06-17 18:07:51 -07001024void SurfaceComposerClient::Transaction::clear() {
1025 mComposerStates.clear();
1026 mDisplayStates.clear();
1027 mListenerCallbacks.clear();
1028 mInputWindowCommands.clear();
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001029 mUncacheBuffers.clear();
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001030 mMayContainBuffer = false;
Vishnu Nairfef244e2019-06-17 18:07:51 -07001031 mTransactionNestCount = 0;
1032 mAnimation = false;
Ady Abraham8cbd3072021-03-15 16:39:06 -07001033 mEarlyWakeupStart = false;
1034 mEarlyWakeupEnd = false;
Ady Abrahamf0c56492020-12-17 18:04:15 -08001035 mDesiredPresentTime = 0;
1036 mIsAutoTimestamp = true;
Ady Abraham88930f92023-06-07 10:32:08 -07001037 mFrameTimelineInfo = {};
Vishnu Nair277142c2021-01-05 18:35:29 -08001038 mApplyToken = nullptr;
Pablo Gamito23780be2023-04-18 08:30:00 +00001039 mMergedTransactionIds.clear();
Vishnu Nairfef244e2019-06-17 18:07:51 -07001040}
1041
Pablo Gamito3e4e0be2022-05-27 17:26:24 +00001042uint64_t SurfaceComposerClient::Transaction::getId() {
1043 return mId;
1044}
1045
Pablo Gamito23780be2023-04-18 08:30:00 +00001046std::vector<uint64_t> SurfaceComposerClient::Transaction::getMergedTransactionIds() {
1047 return mMergedTransactionIds;
1048}
1049
Marissa Wall78b72202019-03-15 14:58:34 -07001050void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
1051 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1052
Marissa Wall947d34e2019-03-29 14:03:53 -07001053 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -07001054 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -07001055 uncacheBuffer.id = cacheId;
Vishnu Nair40fff5c2022-11-04 02:46:28 +00001056 Vector<ComposerState> composerStates;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001057 status_t status = sf->setTransactionState(FrameTimelineInfo{}, composerStates, {},
1058 ISurfaceComposer::eOneWay,
1059 Transaction::getDefaultApplyToken(), {}, systemTime(),
Pablo Gamito23780be2023-04-18 08:30:00 +00001060 true, {uncacheBuffer}, false, {}, generateId(), {});
Patrick Williams640b7292022-10-11 19:25:06 +00001061 if (status != NO_ERROR) {
1062 ALOGE_AND_TRACE("SurfaceComposerClient::doUncacheBufferTransaction - %s",
1063 strerror(-status));
1064 }
Marissa Wall78b72202019-03-15 14:58:34 -07001065}
1066
1067void SurfaceComposerClient::Transaction::cacheBuffers() {
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001068 if (!mMayContainBuffer) {
Marissa Wall78b72202019-03-15 14:58:34 -07001069 return;
1070 }
1071
1072 size_t count = 0;
Vishnu Nairf03652d2019-07-16 17:56:56 -07001073 for (auto& [handle, cs] : mComposerStates) {
Pablo Gamitodbc31672020-09-01 18:28:58 +00001074 layer_state_t* s = &(mComposerStates[handle].state);
Marissa Wall78b72202019-03-15 14:58:34 -07001075 if (!(s->what & layer_state_t::eBufferChanged)) {
1076 continue;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001077 } else if (s->bufferData &&
1078 s->bufferData->flags.test(BufferData::BufferDataChange::cachedBufferChanged)) {
Robert Carr28037922020-04-08 10:57:07 -07001079 // If eBufferChanged and eCachedBufferChanged are both trued then that means
1080 // we already cached the buffer in a previous call to cacheBuffers, perhaps
1081 // from writeToParcel on a Transaction that was merged in to this one.
1082 continue;
Marissa Wall78b72202019-03-15 14:58:34 -07001083 }
1084
Marissa Wall00597242019-03-27 10:35:19 -07001085 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
1086 // time trying to cache them.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001087 if (!s->bufferData || !s->bufferData->buffer) {
Marissa Wall00597242019-03-27 10:35:19 -07001088 continue;
1089 }
1090
Marissa Wall78b72202019-03-15 14:58:34 -07001091 uint64_t cacheId = 0;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001092 status_t ret = BufferCache::getInstance().getCacheId(s->bufferData->buffer, &cacheId);
Marissa Wall78b72202019-03-15 14:58:34 -07001093 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -07001094 // Cache-hit. Strip the buffer and send only the id.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001095 s->bufferData->buffer = nullptr;
Marissa Wall78b72202019-03-15 14:58:34 -07001096 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -07001097 // Cache-miss. Include the buffer and send the new cacheId.
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001098 std::optional<client_cache_t> uncacheBuffer;
1099 cacheId = BufferCache::getInstance().cache(s->bufferData->buffer, uncacheBuffer);
1100 if (uncacheBuffer) {
1101 mUncacheBuffers.push_back(*uncacheBuffer);
1102 }
Marissa Wall78b72202019-03-15 14:58:34 -07001103 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001104 s->bufferData->flags |= BufferData::BufferDataChange::cachedBufferChanged;
1105 s->bufferData->cachedBuffer.token = BufferCache::getInstance().getToken();
1106 s->bufferData->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -07001107
1108 // If we have more buffers than the size of the cache, we should stop caching so we don't
1109 // evict other buffers in this transaction
1110 count++;
1111 if (count >= BUFFER_CACHE_MAX_SIZE) {
1112 break;
1113 }
1114 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001115}
1116
Vishnu Nair9402bb02022-09-28 16:45:06 -07001117class SyncCallback {
1118public:
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001119 static auto getCallback(std::shared_ptr<SyncCallback>& callbackContext) {
1120 return [callbackContext](void* /* unused context */, nsecs_t /* latchTime */,
1121 const sp<Fence>& /* presentFence */,
1122 const std::vector<SurfaceControlStats>& /* stats */) {
1123 if (!callbackContext) {
1124 ALOGE("failed to get callback context for SyncCallback");
1125 return;
1126 }
1127 LOG_ALWAYS_FATAL_IF(sem_post(&callbackContext->mSemaphore), "sem_post failed");
1128 };
Vishnu Nair9402bb02022-09-28 16:45:06 -07001129 }
1130 ~SyncCallback() {
1131 if (mInitialized) {
1132 LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
1133 }
1134 }
1135 void init() {
1136 LOG_ALWAYS_FATAL_IF(clock_gettime(CLOCK_MONOTONIC, &mTimeoutTimespec) == -1,
1137 "clock_gettime() fail! in SyncCallback::init");
1138 mTimeoutTimespec.tv_sec += 4;
1139 LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed");
1140 mInitialized = true;
1141 }
1142 void wait() {
1143 int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &mTimeoutTimespec);
1144 if (result && errno != ETIMEDOUT && errno != EINTR) {
1145 LOG_ALWAYS_FATAL("sem_clockwait failed(%d)", errno);
1146 } else if (errno == ETIMEDOUT) {
1147 ALOGW("Sync transaction timed out waiting for commit callback.");
1148 }
1149 }
1150 void* getContext() { return static_cast<void*>(this); }
1151
1152private:
1153 sem_t mSemaphore;
1154 bool mInitialized = false;
1155 timespec mTimeoutTimespec;
1156};
1157
Robert Carr79dc06a2022-02-22 15:28:59 -08001158status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001159 if (mStatus != NO_ERROR) {
1160 return mStatus;
1161 }
1162
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001163 std::shared_ptr<SyncCallback> syncCallback = std::make_shared<SyncCallback>();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001164 if (synchronous) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001165 syncCallback->init();
1166 addTransactionCommittedCallback(SyncCallback::getCallback(syncCallback),
1167 /*callbackContext=*/nullptr);
Vishnu Nair9402bb02022-09-28 16:45:06 -07001168 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001169
Valerie Hau9dab9732019-08-20 09:29:25 -07001170 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 14:03:19 -07001171 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001172 // For every listener with registered callbacks
1173 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
1174 auto& [callbackIds, surfaceControls] = callbackInfo;
1175 if (callbackIds.empty()) {
1176 continue;
1177 }
1178
Valerie Hau9dab9732019-08-20 09:29:25 -07001179 if (surfaceControls.empty()) {
1180 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
1181 } else {
1182 // If the listener has any SurfaceControls set on this Transaction update the surface
1183 // state
1184 for (const auto& surfaceControl : surfaceControls) {
1185 layer_state_t* s = getLayerState(surfaceControl);
1186 if (!s) {
1187 ALOGE("failed to get layer state");
1188 continue;
1189 }
1190 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
1191 s->what |= layer_state_t::eHasListenerCallbacksChanged;
1192 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001193 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001194 }
1195 }
Valerie Hau9dab9732019-08-20 09:29:25 -07001196
Marissa Wall78b72202019-03-15 14:58:34 -07001197 cacheBuffers();
1198
Robert Carr4cdc58f2017-08-23 14:22:20 -07001199 Vector<ComposerState> composerStates;
1200 Vector<DisplayState> displayStates;
1201 uint32_t flags = 0;
1202
Vishnu Nair9402bb02022-09-28 16:45:06 -07001203 for (auto const& kv : mComposerStates) {
chaviw8e3fe5d2018-02-22 10:55:42 -08001204 composerStates.add(kv.second);
1205 }
1206
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001207 displayStates = std::move(mDisplayStates);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001208
Robert Carr4cdc58f2017-08-23 14:22:20 -07001209 if (mAnimation) {
1210 flags |= ISurfaceComposer::eAnimation;
1211 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001212 if (oneWay) {
Vishnu Nair2709efe2022-09-28 17:17:08 -07001213 if (synchronous) {
1214 ALOGE("Transaction attempted to set synchronous and one way at the same time"
1215 " this is an invalid request. Synchronous will win for safety");
1216 } else {
1217 flags |= ISurfaceComposer::eOneWay;
1218 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001219 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001220
Ady Abraham8cbd3072021-03-15 16:39:06 -07001221 // If both mEarlyWakeupStart and mEarlyWakeupEnd are set
Ady Abrahambf1349c2020-06-12 14:26:18 -07001222 // it is equivalent for none
Ady Abraham8cbd3072021-03-15 16:39:06 -07001223 if (mEarlyWakeupStart && !mEarlyWakeupEnd) {
1224 flags |= ISurfaceComposer::eEarlyWakeupStart;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001225 }
Ady Abraham8cbd3072021-03-15 16:39:06 -07001226 if (mEarlyWakeupEnd && !mEarlyWakeupStart) {
1227 flags |= ISurfaceComposer::eEarlyWakeupEnd;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001228 }
1229
Patrick Williams84541d12023-09-29 11:00:23 -05001230 sp<IBinder> applyToken = mApplyToken ? mApplyToken : getDefaultApplyToken();
Vishnu Nair277142c2021-01-05 18:35:29 -08001231
Vishnu Nair9402bb02022-09-28 16:45:06 -07001232 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001233 sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
Ady Abrahamf0c56492020-12-17 18:04:15 -08001234 mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
Pablo Gamito23780be2023-04-18 08:30:00 +00001235 mUncacheBuffers, hasListenerCallbacks, listenerCallbacks, mId,
1236 mMergedTransactionIds);
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001237 mId = generateId();
1238
1239 // Clear the current states and flags
1240 clear();
1241
Vishnu Nair9402bb02022-09-28 16:45:06 -07001242 if (synchronous) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001243 syncCallback->wait();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001244 }
1245
Robert Carr4cdc58f2017-08-23 14:22:20 -07001246 mStatus = NO_ERROR;
1247 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001248}
1249
Vishnu Nair80da86b2022-08-11 16:32:58 -07001250sp<IBinder> SurfaceComposerClient::Transaction::sApplyToken = new BBinder();
1251
Patrick Williams84541d12023-09-29 11:00:23 -05001252std::mutex SurfaceComposerClient::Transaction::sApplyTokenMutex;
1253
Vishnu Nair80da86b2022-08-11 16:32:58 -07001254sp<IBinder> SurfaceComposerClient::Transaction::getDefaultApplyToken() {
Patrick Williams84541d12023-09-29 11:00:23 -05001255 std::scoped_lock lock{sApplyTokenMutex};
Vishnu Nair80da86b2022-08-11 16:32:58 -07001256 return sApplyToken;
1257}
1258
1259void SurfaceComposerClient::Transaction::setDefaultApplyToken(sp<IBinder> applyToken) {
Patrick Williams84541d12023-09-29 11:00:23 -05001260 std::scoped_lock lock{sApplyTokenMutex};
Vishnu Nair80da86b2022-08-11 16:32:58 -07001261 sApplyToken = applyToken;
1262}
Pascal Muetschardb39918f2023-01-27 11:36:11 +01001263
1264status_t SurfaceComposerClient::Transaction::sendSurfaceFlushJankDataTransaction(
1265 const sp<SurfaceControl>& sc) {
1266 Transaction t;
1267 layer_state_t* s = t.getLayerState(sc);
1268 if (!s) {
1269 return BAD_INDEX;
1270 }
1271
1272 s->what |= layer_state_t::eFlushJankData;
1273 t.registerSurfaceControlForCallback(sc);
1274 return t.apply(/*sync=*/false, /* oneWay=*/true);
1275}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276// ---------------------------------------------------------------------------
1277
Huihong Luo1768cb02022-10-11 11:10:34 -07001278sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure,
1279 float requestedRefereshRate) {
Huihong Luo07e72362022-02-14 14:26:04 -08001280 sp<IBinder> display = nullptr;
1281 binder::Status status =
1282 ComposerServiceAIDL::getComposerService()->createDisplay(std::string(
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +00001283 displayName.c_str()),
Huihong Luo1768cb02022-10-11 11:10:34 -07001284 secure, requestedRefereshRate,
1285 &display);
Huihong Luo07e72362022-02-14 14:26:04 -08001286 return status.isOk() ? display : nullptr;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001287}
1288
Robert Carr4cdc58f2017-08-23 14:22:20 -07001289void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Huihong Luo07e72362022-02-14 14:26:04 -08001290 ComposerServiceAIDL::getComposerService()->destroyDisplay(display);
Jesse Hall6c913be2013-08-08 12:15:49 -07001291}
1292
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001293std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
Huihong Luo07e72362022-02-14 14:26:04 -08001294 std::vector<int64_t> displayIds;
1295 std::vector<PhysicalDisplayId> physicalDisplayIds;
1296 binder::Status status =
1297 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayIds(&displayIds);
1298 if (status.isOk()) {
1299 physicalDisplayIds.reserve(displayIds.size());
1300 for (auto item : displayIds) {
1301 auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(item));
1302 physicalDisplayIds.push_back(*id);
1303 }
1304 }
1305 return physicalDisplayIds;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001306}
1307
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001308sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
Huihong Luo07e72362022-02-14 14:26:04 -08001309 sp<IBinder> display = nullptr;
1310 binder::Status status =
1311 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayToken(displayId.value,
1312 &display);
1313 return status.isOk() ? display : nullptr;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001314}
1315
Patrick Williams090ad062023-08-08 12:30:10 -05001316std::optional<gui::StalledTransactionInfo> SurfaceComposerClient::getStalledTransactionInfo(
1317 pid_t pid) {
1318 std::optional<gui::StalledTransactionInfo> result;
1319 ComposerServiceAIDL::getComposerService()->getStalledTransactionInfo(pid, &result);
1320 return result;
1321}
1322
Robert Carr4cdc58f2017-08-23 14:22:20 -07001323void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001324 mAnimation = true;
1325}
1326
Ady Abraham8cbd3072021-03-15 16:39:06 -07001327void SurfaceComposerClient::Transaction::setEarlyWakeupStart() {
1328 mEarlyWakeupStart = true;
Dan Stoza84d619e2018-03-28 17:07:36 -07001329}
1330
Ady Abraham8cbd3072021-03-15 16:39:06 -07001331void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() {
1332 mEarlyWakeupEnd = true;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001333}
1334
Pablo Gamitodbc31672020-09-01 18:28:58 +00001335layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
Robert Carr5b3b9142021-02-22 12:27:32 -08001336 auto handle = sc->getLayerStateHandle();
Pablo Gamitodbc31672020-09-01 18:28:58 +00001337
Vishnu Nairf03652d2019-07-16 17:56:56 -07001338 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001339 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -08001340 ComposerState s;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001341
Vishnu Nairf03652d2019-07-16 17:56:56 -07001342 s.state.surface = handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001343 s.state.layerId = sc->getLayerId();
1344
Vishnu Nairf03652d2019-07-16 17:56:56 -07001345 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -07001346 }
1347
Vishnu Nairf03652d2019-07-16 17:56:56 -07001348 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001349}
1350
Marissa Wallc837b5e2018-10-12 10:04:44 -07001351void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
1352 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08001353 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
1354 callbackInfo.surfaceControls.insert(sc);
1355
Pascal Muetschard29247e52022-11-08 11:33:10 +01001356 TransactionCompletedListener::getInstance()->addSurfaceControlToCallbacks(callbackInfo, sc);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001357}
1358
Robert Carr4cdc58f2017-08-23 14:22:20 -07001359SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
1360 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -08001361 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001362 if (!s) {
1363 mStatus = BAD_INDEX;
1364 return *this;
1365 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001366 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001367 s->x = x;
1368 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001369
1370 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001371 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001372}
1373
Robert Carr4cdc58f2017-08-23 14:22:20 -07001374SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
1375 const sp<SurfaceControl>& sc) {
1376 return setFlags(sc, 0, layer_state_t::eLayerHidden);
1377}
1378
1379SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
1380 const sp<SurfaceControl>& sc) {
1381 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
1382}
1383
Robert Carr4cdc58f2017-08-23 14:22:20 -07001384SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
1385 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001386 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001387 if (!s) {
1388 mStatus = BAD_INDEX;
1389 return *this;
1390 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001391 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001392 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001393 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001394
1395 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001396 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001397}
1398
Pablo Gamito11dcc222020-09-12 15:49:39 +00001399SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
1400 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001401 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -07001402 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001403 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -07001404 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001405 }
1406 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001407 s->what &= ~layer_state_t::eLayerChanged;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001408 s->relativeLayerSurfaceControl = relativeTo;
Robert Carrdb66e622017-04-10 16:55:57 -07001409 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001410
1411 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001412 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001413}
1414
Robert Carr4cdc58f2017-08-23 14:22:20 -07001415SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
1416 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -07001417 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -08001418 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001419 if (!s) {
1420 mStatus = BAD_INDEX;
1421 return *this;
1422 }
chaviwc5676c62020-09-18 15:01:04 -07001423 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001424 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot) ||
Leon Scroggins9a20f722021-12-28 14:43:12 +00001425 (mask & layer_state_t::eEnableBackpressure) ||
Vishnu Nairaa799bd2022-11-16 23:09:09 +00001426 (mask & layer_state_t::eIgnoreDestinationFrame) ||
ramindanib2158ee2023-02-13 20:29:59 -08001427 (mask & layer_state_t::eLayerIsDisplayDecoration) ||
1428 (mask & layer_state_t::eLayerIsRefreshRateIndicator)) {
Dan Stoza23116082015-06-18 14:58:39 -07001429 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -08001430 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001431 s->flags &= ~mask;
1432 s->flags |= (flags & mask);
1433 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001434
1435 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001436 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001437}
1438
Robert Carr4cdc58f2017-08-23 14:22:20 -07001439SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
1440 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -07001441 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -08001442 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001443 if (!s) {
1444 mStatus = BAD_INDEX;
1445 return *this;
1446 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001447 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001448 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001449
1450 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001451 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001452}
1453
Sally Qi421ffb02022-03-21 19:41:33 -07001454SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDimmingEnabled(
1455 const sp<SurfaceControl>& sc, bool dimmingEnabled) {
1456 layer_state_t* s = getLayerState(sc);
1457 if (!s) {
1458 mStatus = BAD_INDEX;
1459 return *this;
1460 }
1461 s->what |= layer_state_t::eDimmingEnabledChanged;
1462 s->dimmingEnabled = dimmingEnabled;
1463
1464 registerSurfaceControlForCallback(sc);
1465 return *this;
1466}
1467
Robert Carr4cdc58f2017-08-23 14:22:20 -07001468SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
1469 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -08001470 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001471 if (!s) {
1472 mStatus = BAD_INDEX;
1473 return *this;
1474 }
Ady Abrahamff930b42022-08-05 19:04:16 +00001475 if (alpha < 0.0f || alpha > 1.0f) {
Ady Abraham24d0b2a2022-08-11 16:27:34 -07001476 ALOGE("SurfaceComposerClient::Transaction::setAlpha: invalid alpha %f, clamping", alpha);
Ady Abrahamff930b42022-08-05 19:04:16 +00001477 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001478 s->what |= layer_state_t::eAlphaChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001479 s->color.a = std::clamp(alpha, 0.f, 1.f);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001480
1481 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001482 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001483}
1484
Robert Carr4cdc58f2017-08-23 14:22:20 -07001485SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001486 const sp<SurfaceControl>& sc, ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001487 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001488 if (!s) {
1489 mStatus = BAD_INDEX;
1490 return *this;
1491 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001492 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -07001493 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001494
1495 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001496 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -07001497}
1498
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001499SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -07001500 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001501 layer_state_t* s = getLayerState(sc);
1502 if (!s) {
1503 mStatus = BAD_INDEX;
1504 return *this;
1505 }
1506 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -07001507
1508 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001509
1510 registerSurfaceControlForCallback(sc);
1511 return *this;
1512}
1513
Robert Carr4cdc58f2017-08-23 14:22:20 -07001514SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
1515 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -08001516 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -08001517 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001518 if (!s) {
1519 mStatus = BAD_INDEX;
1520 return *this;
1521 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001522 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001523 layer_state_t::matrix22_t matrix;
1524 matrix.dsdx = dsdx;
1525 matrix.dtdx = dtdx;
1526 matrix.dsdy = dsdy;
1527 matrix.dtdy = dtdy;
1528 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001529
1530 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001531 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001532}
1533
chaviw25714502021-02-11 10:01:08 -08001534SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
Robert Carr4cdc58f2017-08-23 14:22:20 -07001535 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -08001536 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001537 if (!s) {
1538 mStatus = BAD_INDEX;
1539 return *this;
1540 }
chaviw25714502021-02-11 10:01:08 -08001541 s->what |= layer_state_t::eCropChanged;
1542 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001543
1544 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001545 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001546}
1547
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001548SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
1549 const sp<SurfaceControl>& sc, float cornerRadius) {
1550 layer_state_t* s = getLayerState(sc);
1551 if (!s) {
1552 mStatus = BAD_INDEX;
1553 return *this;
1554 }
1555 s->what |= layer_state_t::eCornerRadiusChanged;
1556 s->cornerRadius = cornerRadius;
1557 return *this;
1558}
1559
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001560SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1561 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1562 layer_state_t* s = getLayerState(sc);
1563 if (!s) {
1564 mStatus = BAD_INDEX;
1565 return *this;
1566 }
1567 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1568 s->backgroundBlurRadius = backgroundBlurRadius;
1569 return *this;
1570}
1571
Lucas Dupinc3800b82020-10-02 16:24:48 -07001572SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBlurRegions(
1573 const sp<SurfaceControl>& sc, const std::vector<BlurRegion>& blurRegions) {
1574 layer_state_t* s = getLayerState(sc);
1575 if (!s) {
1576 mStatus = BAD_INDEX;
1577 return *this;
1578 }
1579 s->what |= layer_state_t::eBlurRegionsChanged;
1580 s->blurRegions = blurRegions;
1581 return *this;
1582}
1583
Robert Carr4cdc58f2017-08-23 14:22:20 -07001584SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001585 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001586 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001587 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001588 mStatus = BAD_INDEX;
1589 return *this;
chaviw06178942017-07-27 10:25:59 -07001590 }
Robert Carr5b3b9142021-02-22 12:27:32 -08001591 if (SurfaceControl::isSameSurface(sc, newParent)) {
1592 return *this;
1593 }
chaviwf1961f72017-09-18 16:41:07 -07001594 s->what |= layer_state_t::eReparent;
Robert Carr5b3b9142021-02-22 12:27:32 -08001595 s->parentSurfaceControlForChild = newParent ? newParent->getParentingLayer() : nullptr;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001596
1597 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001598 return *this;
chaviw06178942017-07-27 10:25:59 -07001599}
1600
Robert Carr4cdc58f2017-08-23 14:22:20 -07001601SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1602 const sp<SurfaceControl>& sc,
1603 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001604 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001605 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001606 mStatus = BAD_INDEX;
1607 return *this;
1608 }
1609 s->what |= layer_state_t::eColorChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001610 s->color.rgb = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001611
1612 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001613 return *this;
1614}
1615
Valerie Haudd0b7572019-01-29 14:59:27 -08001616SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1617 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001618 layer_state_t* s = getLayerState(sc);
1619 if (!s) {
1620 mStatus = BAD_INDEX;
1621 return *this;
1622 }
1623
Valerie Haudd0b7572019-01-29 14:59:27 -08001624 s->what |= layer_state_t::eBackgroundColorChanged;
Vishnu Naird47bcee2023-02-24 18:08:51 +00001625 s->bgColor.rgb = color;
1626 s->bgColor.a = alpha;
Valerie Haudd0b7572019-01-29 14:59:27 -08001627 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001628
1629 registerSurfaceControlForCallback(sc);
1630 return *this;
1631}
1632
Marissa Wall61c58622018-07-18 10:12:20 -07001633SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1634 const sp<SurfaceControl>& sc, uint32_t transform) {
1635 layer_state_t* s = getLayerState(sc);
1636 if (!s) {
1637 mStatus = BAD_INDEX;
1638 return *this;
1639 }
Vishnu Nairbbceb462022-10-10 04:52:13 +00001640 s->what |= layer_state_t::eBufferTransformChanged;
1641 s->bufferTransform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001642
1643 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001644 return *this;
1645}
1646
1647SurfaceComposerClient::Transaction&
1648SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1649 bool transformToDisplayInverse) {
1650 layer_state_t* s = getLayerState(sc);
1651 if (!s) {
1652 mStatus = BAD_INDEX;
1653 return *this;
1654 }
1655 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1656 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001657
1658 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001659 return *this;
1660}
1661
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001662std::shared_ptr<BufferData> SurfaceComposerClient::Transaction::getAndClearBuffer(
chaviw0acd33a2021-11-02 11:55:37 -05001663 const sp<SurfaceControl>& sc) {
1664 layer_state_t* s = getLayerState(sc);
1665 if (!s) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001666 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001667 }
1668 if (!(s->what & layer_state_t::eBufferChanged)) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001669 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001670 }
1671
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001672 std::shared_ptr<BufferData> bufferData = std::move(s->bufferData);
chaviw0acd33a2021-11-02 11:55:37 -05001673
1674 TransactionCompletedListener::getInstance()->removeReleaseBufferCallback(
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001675 bufferData->generateReleaseCallbackId());
chaviw0acd33a2021-11-02 11:55:37 -05001676 s->what &= ~layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001677 s->bufferData = nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001678
chaviw0acd33a2021-11-02 11:55:37 -05001679 return bufferData;
1680}
1681
Robert Carr79dc06a2022-02-22 15:28:59 -08001682SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferHasBarrier(
1683 const sp<SurfaceControl>& sc, uint64_t barrierFrameNumber) {
1684 layer_state_t* s = getLayerState(sc);
1685 if (!s) {
1686 mStatus = BAD_INDEX;
1687 return *this;
1688 }
1689 s->bufferData->hasBarrier = true;
1690 s->bufferData->barrierFrameNumber = barrierFrameNumber;
1691 return *this;
1692}
1693
Marissa Wall61c58622018-07-18 10:12:20 -07001694SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
chaviwba4320c2021-09-15 15:20:53 -05001695 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer,
John Reck57f748c2022-03-17 17:23:49 -04001696 const std::optional<sp<Fence>>& fence, const std::optional<uint64_t>& optFrameNumber,
liulijuneb489f62022-10-17 22:02:14 +08001697 uint32_t producerId, ReleaseBufferCallback callback) {
Marissa Wall61c58622018-07-18 10:12:20 -07001698 layer_state_t* s = getLayerState(sc);
1699 if (!s) {
1700 mStatus = BAD_INDEX;
1701 return *this;
1702 }
chaviw69058fb2021-09-27 09:37:30 -05001703
1704 releaseBufferIfOverwriting(*s);
1705
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001706 std::shared_ptr<BufferData> bufferData = std::make_shared<BufferData>();
1707 bufferData->buffer = buffer;
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001708 if (buffer) {
1709 uint64_t frameNumber = sc->resolveFrameNumber(optFrameNumber);
1710 bufferData->frameNumber = frameNumber;
1711 bufferData->producerId = producerId;
1712 bufferData->flags |= BufferData::BufferDataChange::frameNumberChanged;
1713 if (fence) {
1714 bufferData->acquireFence = *fence;
1715 bufferData->flags |= BufferData::BufferDataChange::fenceChanged;
1716 }
1717 bufferData->releaseBufferEndpoint =
1718 IInterface::asBinder(TransactionCompletedListener::getIInstance());
1719 setReleaseBufferCallback(bufferData.get(), callback);
chaviwba4320c2021-09-15 15:20:53 -05001720 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001721
Ady Abrahamf0c56492020-12-17 18:04:15 -08001722 if (mIsAutoTimestamp) {
1723 mDesiredPresentTime = systemTime();
1724 }
chaviwba4320c2021-09-15 15:20:53 -05001725 s->what |= layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001726 s->bufferData = std::move(bufferData);
Marissa Wallebc2c052019-01-16 19:16:55 -08001727 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001728
chaviw1a227f02022-04-25 16:23:44 -05001729 // With the current infrastructure, a release callback will not be invoked if there's no
1730 // transaction callback in the case when a buffer is latched and not released early. This is
1731 // because the legacy implementation didn't have a release callback and sent releases in the
1732 // transaction callback. Because of this, we need to make sure to have a transaction callback
1733 // set up when a buffer is sent in a transaction to ensure the caller gets the release
1734 // callback, regardless if they set up a transaction callback.
1735 //
1736 // TODO (b/230380821): Remove when release callbacks are separated from transaction callbacks
1737 addTransactionCompletedCallback([](void*, nsecs_t, const sp<Fence>&,
1738 const std::vector<SurfaceControlStats>&) {},
1739 nullptr);
1740
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001741 mMayContainBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001742 return *this;
1743}
1744
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001745SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::unsetBuffer(
1746 const sp<SurfaceControl>& sc) {
1747 layer_state_t* s = getLayerState(sc);
1748 if (!s) {
1749 mStatus = BAD_INDEX;
1750 return *this;
1751 }
1752
1753 if (!(s->what & layer_state_t::eBufferChanged)) {
1754 return *this;
1755 }
1756
1757 releaseBufferIfOverwriting(*s);
1758
1759 s->what &= ~layer_state_t::eBufferChanged;
1760 s->bufferData = nullptr;
1761 return *this;
1762}
1763
chaviwba4320c2021-09-15 15:20:53 -05001764void SurfaceComposerClient::Transaction::setReleaseBufferCallback(BufferData* bufferData,
Vishnu Nair1506b182021-02-22 14:35:15 -08001765 ReleaseBufferCallback callback) {
1766 if (!callback) {
1767 return;
1768 }
1769
chaviwba4320c2021-09-15 15:20:53 -05001770 if (!bufferData->buffer) {
Vishnu Nair1506b182021-02-22 14:35:15 -08001771 ALOGW("Transaction::setReleaseBufferCallback"
1772 "ignored trying to set a callback on a null buffer.");
1773 return;
1774 }
1775
chaviwba4320c2021-09-15 15:20:53 -05001776 bufferData->releaseBufferListener = TransactionCompletedListener::getIInstance();
Vishnu Nair1506b182021-02-22 14:35:15 -08001777 auto listener = TransactionCompletedListener::getInstance();
chaviw8dd181f2022-01-05 18:36:46 -06001778 listener->setReleaseBufferCallback(bufferData->generateReleaseCallbackId(), callback);
Vishnu Nair1506b182021-02-22 14:35:15 -08001779}
1780
Marissa Wall61c58622018-07-18 10:12:20 -07001781SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1782 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1783 layer_state_t* s = getLayerState(sc);
1784 if (!s) {
1785 mStatus = BAD_INDEX;
1786 return *this;
1787 }
1788 s->what |= layer_state_t::eDataspaceChanged;
1789 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001790
1791 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001792 return *this;
1793}
1794
John Reck68796592023-01-25 13:47:12 -05001795SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setExtendedRangeBrightness(
1796 const sp<SurfaceControl>& sc, float currentBufferRatio, float desiredRatio) {
1797 layer_state_t* s = getLayerState(sc);
1798 if (!s) {
1799 mStatus = BAD_INDEX;
1800 return *this;
1801 }
1802 s->what |= layer_state_t::eExtendedRangeBrightnessChanged;
Sally Qi963049b2023-03-23 14:06:21 -07001803 s->currentHdrSdrRatio = currentBufferRatio;
1804 s->desiredHdrSdrRatio = desiredRatio;
John Reck68796592023-01-25 13:47:12 -05001805
1806 registerSurfaceControlForCallback(sc);
1807 return *this;
1808}
1809
Alec Mourif4af03e2023-02-11 00:25:24 +00001810SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCachingHint(
1811 const sp<SurfaceControl>& sc, gui::CachingHint cachingHint) {
1812 layer_state_t* s = getLayerState(sc);
1813 if (!s) {
1814 mStatus = BAD_INDEX;
1815 return *this;
1816 }
1817 s->what |= layer_state_t::eCachingHintChanged;
1818 s->cachingHint = cachingHint;
1819
1820 registerSurfaceControlForCallback(sc);
1821 return *this;
1822}
1823
Marissa Wall61c58622018-07-18 10:12:20 -07001824SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1825 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1826 layer_state_t* s = getLayerState(sc);
1827 if (!s) {
1828 mStatus = BAD_INDEX;
1829 return *this;
1830 }
1831 s->what |= layer_state_t::eHdrMetadataChanged;
1832 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001833
1834 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001835 return *this;
1836}
1837
1838SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1839 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1840 layer_state_t* s = getLayerState(sc);
1841 if (!s) {
1842 mStatus = BAD_INDEX;
1843 return *this;
1844 }
1845 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1846 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001847
1848 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001849 return *this;
1850}
1851
1852SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1853 const sp<SurfaceControl>& sc, int32_t api) {
1854 layer_state_t* s = getLayerState(sc);
1855 if (!s) {
1856 mStatus = BAD_INDEX;
1857 return *this;
1858 }
1859 s->what |= layer_state_t::eApiChanged;
1860 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001861
1862 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001863 return *this;
1864}
1865
1866SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1867 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1868 layer_state_t* s = getLayerState(sc);
1869 if (!s) {
1870 mStatus = BAD_INDEX;
1871 return *this;
1872 }
1873 s->what |= layer_state_t::eSidebandStreamChanged;
1874 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001875
1876 registerSurfaceControlForCallback(sc);
1877 return *this;
1878}
1879
Marissa Wall17b4e452018-12-26 16:32:34 -08001880SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1881 nsecs_t desiredPresentTime) {
1882 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -08001883 mIsAutoTimestamp = false;
Marissa Wall17b4e452018-12-26 16:32:34 -08001884 return *this;
1885}
1886
Peiyong Linc502cb72019-03-01 15:00:23 -08001887SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1888 const sp<SurfaceControl>& sc, const bool agnostic) {
1889 layer_state_t* s = getLayerState(sc);
1890 if (!s) {
1891 mStatus = BAD_INDEX;
1892 return *this;
1893 }
1894 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1895 s->colorSpaceAgnostic = agnostic;
1896
1897 registerSurfaceControlForCallback(sc);
1898 return *this;
1899}
1900
Marissa Wallc837b5e2018-10-12 10:04:44 -07001901SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001902SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1903 int32_t priority) {
1904 layer_state_t* s = getLayerState(sc);
1905 if (!s) {
1906 mStatus = BAD_INDEX;
1907 return *this;
1908 }
1909
1910 s->what |= layer_state_t::eFrameRateSelectionPriority;
1911 s->frameRateSelectionPriority = priority;
1912
1913 registerSurfaceControlForCallback(sc);
1914 return *this;
1915}
1916
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001917SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::addTransactionCallback(
1918 TransactionCompletedCallbackTakesContext callback, void* callbackContext,
1919 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001920 auto listener = TransactionCompletedListener::getInstance();
1921
Marissa Wall80d94ad2019-01-18 16:04:36 -08001922 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1923 std::placeholders::_2, std::placeholders::_3);
1924 const auto& surfaceControls =
1925 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001926
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001927 CallbackId callbackId =
1928 listener->addCallbackFunction(callbackWithContext, surfaceControls, callbackType);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001929
1930 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1931 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001932 return *this;
1933}
1934
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001935SurfaceComposerClient::Transaction&
1936SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
1937 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1938 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMPLETE);
1939}
1940
1941SurfaceComposerClient::Transaction&
1942SurfaceComposerClient::Transaction::addTransactionCommittedCallback(
1943 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1944 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMMIT);
1945}
1946
Valerie Hau871d6352020-01-29 08:44:02 -08001947SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1948 const sp<SurfaceControl>& sc) {
1949 layer_state_t* s = getLayerState(sc);
1950 if (!s) {
1951 mStatus = BAD_INDEX;
1952 return *this;
1953 }
1954
1955 s->what |= layer_state_t::eProducerDisconnect;
1956 return *this;
1957}
1958
Robert Carr2c358bf2018-08-08 15:58:15 -07001959SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
chaviw98318de2021-05-19 16:45:23 -05001960 const sp<SurfaceControl>& sc, const WindowInfo& info) {
Robert Carr2c358bf2018-08-08 15:58:15 -07001961 layer_state_t* s = getLayerState(sc);
1962 if (!s) {
1963 mStatus = BAD_INDEX;
1964 return *this;
1965 }
chaviw98318de2021-05-19 16:45:23 -05001966 s->windowInfoHandle = new WindowInfoHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001967 s->what |= layer_state_t::eInputInfoChanged;
1968 return *this;
1969}
chaviw273171b2018-12-26 11:46:30 -08001970
Vishnu Naire798b472020-07-23 13:52:21 -07001971SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Naire798b472020-07-23 13:52:21 -07001972 const FocusRequest& request) {
1973 mInputWindowCommands.focusRequests.push_back(request);
1974 return *this;
1975}
1976
Patrick Williamse5991f52022-06-23 20:23:43 +00001977SurfaceComposerClient::Transaction&
1978SurfaceComposerClient::Transaction::addWindowInfosReportedListener(
1979 sp<gui::IWindowInfosReportedListener> windowInfosReportedListener) {
1980 mInputWindowCommands.windowInfosReportedListeners.insert(windowInfosReportedListener);
chaviwa911b102019-02-14 10:18:33 -08001981 return *this;
1982}
1983
Peiyong Lind3788632018-09-18 16:01:31 -07001984SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1985 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1986 layer_state_t* s = getLayerState(sc);
1987 if (!s) {
1988 mStatus = BAD_INDEX;
1989 return *this;
1990 }
1991 s->what |= layer_state_t::eColorTransformChanged;
1992 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001993
1994 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001995 return *this;
1996}
1997
Robert Carrfb4d58b2019-01-15 09:21:27 -08001998SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1999 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
chaviw25714502021-02-11 10:01:08 -08002000 setCrop(sc, source);
Robert Carrfb4d58b2019-01-15 09:21:27 -08002001
2002 int x = dst.left;
2003 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07002004
2005 float sourceWidth = source.getWidth();
2006 float sourceHeight = source.getHeight();
2007
2008 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
2009 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08002010 float matrix[4] = {1, 0, 0, 1};
2011
2012 switch (transform) {
2013 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
2014 matrix[0] = -xScale; matrix[1] = 0;
2015 matrix[2] = 0; matrix[3] = yScale;
2016 x += source.getWidth();
2017 break;
2018 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
2019 matrix[0] = xScale; matrix[1] = 0;
2020 matrix[2] = 0; matrix[3] = -yScale;
2021 y += source.getHeight();
2022 break;
2023 case NATIVE_WINDOW_TRANSFORM_ROT_90:
2024 matrix[0] = 0; matrix[1] = -yScale;
2025 matrix[2] = xScale; matrix[3] = 0;
2026 x += source.getHeight();
2027 break;
2028 case NATIVE_WINDOW_TRANSFORM_ROT_180:
2029 matrix[0] = -xScale; matrix[1] = 0;
2030 matrix[2] = 0; matrix[3] = -yScale;
2031 x += source.getWidth();
2032 y += source.getHeight();
2033 break;
2034 case NATIVE_WINDOW_TRANSFORM_ROT_270:
2035 matrix[0] = 0; matrix[1] = yScale;
2036 matrix[2] = -xScale; matrix[3] = 0;
2037 y += source.getWidth();
2038 break;
2039 default:
2040 matrix[0] = xScale; matrix[1] = 0;
2041 matrix[2] = 0; matrix[3] = yScale;
2042 break;
2043 }
2044 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07002045 float offsetX = xScale * source.left;
2046 float offsetY = yScale * source.top;
2047 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08002048
2049 return *this;
2050}
2051
Vishnu Nairc97b8db2019-10-29 18:19:35 -07002052SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
2053 const sp<SurfaceControl>& sc, float shadowRadius) {
2054 layer_state_t* s = getLayerState(sc);
2055 if (!s) {
2056 mStatus = BAD_INDEX;
2057 return *this;
2058 }
2059 s->what |= layer_state_t::eShadowRadiusChanged;
2060 s->shadowRadius = shadowRadius;
2061 return *this;
2062}
2063
Steven Thomas3172e202020-01-06 19:25:30 -08002064SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Marin Shalamanov46084422020-10-13 12:33:42 +02002065 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility,
Marin Shalamanovc5986772021-03-16 16:09:49 +01002066 int8_t changeFrameRateStrategy) {
Steven Thomas3172e202020-01-06 19:25:30 -08002067 layer_state_t* s = getLayerState(sc);
2068 if (!s) {
2069 mStatus = BAD_INDEX;
2070 return *this;
2071 }
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002072 // Allow privileged values as well here, those will be ignored by SF if
2073 // the caller is not privileged
Marin Shalamanovc5986772021-03-16 16:09:49 +01002074 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
2075 "Transaction::setFrameRate",
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002076 /*privileged=*/true)) {
Steven Thomas62a4cf82020-01-31 12:04:03 -08002077 mStatus = BAD_VALUE;
2078 return *this;
2079 }
Steven Thomas3172e202020-01-06 19:25:30 -08002080 s->what |= layer_state_t::eFrameRateChanged;
2081 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002082 s->frameRateCompatibility = compatibility;
Marin Shalamanovc5986772021-03-16 16:09:49 +01002083 s->changeFrameRateStrategy = changeFrameRateStrategy;
Steven Thomas3172e202020-01-06 19:25:30 -08002084 return *this;
2085}
2086
Andy Labrada096227e2022-06-15 16:58:11 +00002087SurfaceComposerClient::Transaction&
2088SurfaceComposerClient::Transaction::setDefaultFrameRateCompatibility(const sp<SurfaceControl>& sc,
2089 int8_t compatibility) {
2090 layer_state_t* s = getLayerState(sc);
2091 if (!s) {
2092 mStatus = BAD_INDEX;
2093 return *this;
2094 }
2095 s->what |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
2096 s->defaultFrameRateCompatibility = compatibility;
2097 return *this;
2098}
2099
Rachel Leece6e0042023-06-27 11:22:54 -07002100SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRateCategory(
Rachel Lee67afbea2023-09-28 15:35:07 -07002101 const sp<SurfaceControl>& sc, int8_t category, bool smoothSwitchOnly) {
Rachel Leece6e0042023-06-27 11:22:54 -07002102 layer_state_t* s = getLayerState(sc);
2103 if (!s) {
2104 mStatus = BAD_INDEX;
2105 return *this;
2106 }
2107 s->what |= layer_state_t::eFrameRateCategoryChanged;
2108 s->frameRateCategory = category;
Rachel Lee67afbea2023-09-28 15:35:07 -07002109 s->frameRateCategorySmoothSwitchOnly = smoothSwitchOnly;
Rachel Leece6e0042023-06-27 11:22:54 -07002110 return *this;
2111}
2112
Rachel Lee58cc90d2023-09-05 18:50:20 -07002113SurfaceComposerClient::Transaction&
2114SurfaceComposerClient::Transaction::setFrameRateSelectionStrategy(const sp<SurfaceControl>& sc,
2115 int8_t strategy) {
2116 layer_state_t* s = getLayerState(sc);
2117 if (!s) {
2118 mStatus = BAD_INDEX;
2119 return *this;
2120 }
2121 s->what |= layer_state_t::eFrameRateSelectionStrategyChanged;
2122 s->frameRateSelectionStrategy = strategy;
2123 return *this;
2124}
2125
Vishnu Nair6213bd92020-05-08 17:42:25 -07002126SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
2127 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
2128 layer_state_t* s = getLayerState(sc);
2129 if (!s) {
2130 mStatus = BAD_INDEX;
2131 return *this;
2132 }
2133
2134 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
2135 ? ui::Transform::ROT_INVALID
2136 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
2137 s->what |= layer_state_t::eFixedTransformHintChanged;
2138 s->fixedTransformHint = transform;
2139 return *this;
2140}
2141
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10002142SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
2143 const FrameTimelineInfo& frameTimelineInfo) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002144 mergeFrameTimelineInfo(mFrameTimelineInfo, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -07002145 return *this;
2146}
2147
Vishnu Naircf26a0a2020-11-13 12:56:20 -08002148SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAutoRefresh(
2149 const sp<SurfaceControl>& sc, bool autoRefresh) {
2150 layer_state_t* s = getLayerState(sc);
2151 if (!s) {
2152 mStatus = BAD_INDEX;
2153 return *this;
2154 }
2155
2156 s->what |= layer_state_t::eAutoRefreshChanged;
2157 s->autoRefresh = autoRefresh;
2158 return *this;
2159}
2160
Winson Chunga30f7c92021-06-29 15:42:56 -07002161SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedOverlay(
2162 const sp<SurfaceControl>& sc, bool isTrustedOverlay) {
2163 layer_state_t* s = getLayerState(sc);
2164 if (!s) {
2165 mStatus = BAD_INDEX;
2166 return *this;
2167 }
2168
2169 s->what |= layer_state_t::eTrustedOverlayChanged;
2170 s->isTrustedOverlay = isTrustedOverlay;
2171 return *this;
2172}
2173
Vishnu Nair277142c2021-01-05 18:35:29 -08002174SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken(
2175 const sp<IBinder>& applyToken) {
2176 mApplyToken = applyToken;
2177 return *this;
2178}
2179
John Reckcdb4ed72021-02-04 13:39:33 -05002180SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setStretchEffect(
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002181 const sp<SurfaceControl>& sc, const StretchEffect& stretchEffect) {
John Reckcdb4ed72021-02-04 13:39:33 -05002182 layer_state_t* s = getLayerState(sc);
2183 if (!s) {
2184 mStatus = BAD_INDEX;
2185 return *this;
2186 }
2187
2188 s->what |= layer_state_t::eStretchChanged;
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002189 s->stretchEffect = stretchEffect;
John Reckcdb4ed72021-02-04 13:39:33 -05002190 return *this;
2191}
2192
chaviwf3f40fe2021-04-27 15:54:02 -05002193SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferCrop(
2194 const sp<SurfaceControl>& sc, const Rect& bufferCrop) {
2195 layer_state_t* s = getLayerState(sc);
2196 if (!s) {
2197 mStatus = BAD_INDEX;
2198 return *this;
2199 }
2200
2201 s->what |= layer_state_t::eBufferCropChanged;
2202 s->bufferCrop = bufferCrop;
2203
2204 registerSurfaceControlForCallback(sc);
2205 return *this;
2206}
2207
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07002208SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDestinationFrame(
2209 const sp<SurfaceControl>& sc, const Rect& destinationFrame) {
2210 layer_state_t* s = getLayerState(sc);
2211 if (!s) {
2212 mStatus = BAD_INDEX;
2213 return *this;
2214 }
2215
2216 s->what |= layer_state_t::eDestinationFrameChanged;
2217 s->destinationFrame = destinationFrame;
2218
2219 registerSurfaceControlForCallback(sc);
2220 return *this;
2221}
2222
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002223SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDropInputMode(
2224 const sp<SurfaceControl>& sc, gui::DropInputMode mode) {
2225 layer_state_t* s = getLayerState(sc);
2226 if (!s) {
2227 mStatus = BAD_INDEX;
2228 return *this;
2229 }
2230
2231 s->what |= layer_state_t::eDropInputModeChanged;
2232 s->dropInputMode = mode;
2233
2234 registerSurfaceControlForCallback(sc);
2235 return *this;
2236}
2237
Tianhao Yao67dd7122022-02-22 17:48:33 +00002238SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::enableBorder(
Tianhao Yao10cea3c2022-03-30 01:37:22 +00002239 const sp<SurfaceControl>& sc, bool shouldEnable, float width, const half4& color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00002240 layer_state_t* s = getLayerState(sc);
2241 if (!s) {
2242 mStatus = BAD_INDEX;
2243 return *this;
2244 }
2245
2246 s->what |= layer_state_t::eRenderBorderChanged;
2247 s->borderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00002248 s->borderWidth = width;
2249 s->borderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00002250
2251 registerSurfaceControlForCallback(sc);
2252 return *this;
2253}
2254
Mathias Agopian698c0872011-06-28 19:09:31 -07002255// ---------------------------------------------------------------------------
2256
chaviw763ef572018-02-22 16:04:57 -08002257DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002258 DisplayState s;
2259 s.token = token;
2260 ssize_t index = mDisplayStates.indexOf(s);
2261 if (index < 0) {
2262 // we don't have it, add an initialized layer_state to our list
2263 s.what = 0;
2264 index = mDisplayStates.add(s);
2265 }
Dan Stozad723bd72014-11-18 10:24:03 -08002266 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002267}
2268
Robert Carr4cdc58f2017-08-23 14:22:20 -07002269status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
2270 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07002271 if (bufferProducer.get() != nullptr) {
2272 // Make sure that composition can never be stalled by a virtual display
2273 // consumer that isn't processing buffers fast enough.
2274 status_t err = bufferProducer->setAsyncMode(true);
2275 if (err != NO_ERROR) {
2276 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
2277 "BufferQueue. This BufferQueue cannot be used for virtual "
2278 "display. (%d)", err);
2279 return err;
2280 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002281 }
chaviw763ef572018-02-22 16:04:57 -08002282 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08002283 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002284 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002285 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002286}
2287
Robert Carr4cdc58f2017-08-23 14:22:20 -07002288void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002289 ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08002290 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002291 s.layerStack = layerStack;
2292 s.what |= DisplayState::eLayerStackChanged;
2293}
2294
Evan Rosky2239b372021-05-20 13:43:47 -07002295void SurfaceComposerClient::Transaction::setDisplayFlags(const sp<IBinder>& token, uint32_t flags) {
2296 DisplayState& s(getDisplayState(token));
2297 s.flags = flags;
2298 s.what |= DisplayState::eFlagsChanged;
2299}
2300
Robert Carr4cdc58f2017-08-23 14:22:20 -07002301void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08002302 ui::Rotation orientation,
2303 const Rect& layerStackRect,
2304 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08002305 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002306 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002307 s.layerStackSpaceRect = layerStackRect;
2308 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002309 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002310}
2311
Robert Carr4cdc58f2017-08-23 14:22:20 -07002312void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08002313 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07002314 s.width = width;
2315 s.height = height;
2316 s.what |= DisplayState::eDisplaySizeChanged;
2317}
2318
Huihong Luo3bdef862022-03-03 11:57:19 -08002319// copied from FrameTimelineInfo::merge()
2320void SurfaceComposerClient::Transaction::mergeFrameTimelineInfo(FrameTimelineInfo& t,
2321 const FrameTimelineInfo& other) {
2322 // When merging vsync Ids we take the oldest valid one
2323 if (t.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID &&
2324 other.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
2325 if (other.vsyncId > t.vsyncId) {
Ady Abraham88930f92023-06-07 10:32:08 -07002326 t = other;
Huihong Luo3bdef862022-03-03 11:57:19 -08002327 }
2328 } else if (t.vsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) {
Ady Abraham88930f92023-06-07 10:32:08 -07002329 t = other;
Huihong Luo3bdef862022-03-03 11:57:19 -08002330 }
2331}
2332
Chavi Weingarten076acac2023-01-19 17:20:43 +00002333SurfaceComposerClient::Transaction&
2334SurfaceComposerClient::Transaction::setTrustedPresentationCallback(
2335 const sp<SurfaceControl>& sc, TrustedPresentationCallback cb,
2336 const TrustedPresentationThresholds& thresholds, void* context,
2337 sp<SurfaceComposerClient::PresentationCallbackRAII>& outCallbackRef) {
2338 auto listener = TransactionCompletedListener::getInstance();
2339 outCallbackRef = listener->addTrustedPresentationCallback(cb, sc->getLayerId(), context);
2340
2341 layer_state_t* s = getLayerState(sc);
2342 if (!s) {
2343 mStatus = BAD_INDEX;
2344 return *this;
2345 }
2346 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2347 s->trustedPresentationThresholds = thresholds;
2348 s->trustedPresentationListener.callbackInterface = TransactionCompletedListener::getIInstance();
2349 s->trustedPresentationListener.callbackId = sc->getLayerId();
2350
2351 return *this;
2352}
2353
2354SurfaceComposerClient::Transaction&
2355SurfaceComposerClient::Transaction::clearTrustedPresentationCallback(const sp<SurfaceControl>& sc) {
2356 auto listener = TransactionCompletedListener::getInstance();
2357 listener->clearTrustedPresentationCallback(sc->getLayerId());
2358
2359 layer_state_t* s = getLayerState(sc);
2360 if (!s) {
2361 mStatus = BAD_INDEX;
2362 return *this;
2363 }
2364 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2365 s->trustedPresentationThresholds = TrustedPresentationThresholds();
2366 s->trustedPresentationListener.callbackInterface = nullptr;
2367 s->trustedPresentationListener.callbackId = -1;
2368
2369 return *this;
2370}
2371
Mathias Agopiane57f2922012-08-09 16:29:12 -07002372// ---------------------------------------------------------------------------
2373
chaviw60c9d3e2021-06-04 12:52:17 -05002374SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002375
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002376SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
chaviw60c9d3e2021-06-04 12:52:17 -05002377 : mStatus(NO_ERROR), mClient(client) {}
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002378
Mathias Agopian698c0872011-06-28 19:09:31 -07002379void SurfaceComposerClient::onFirstRef() {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002380 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002381 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08002382 sp<ISurfaceComposerClient> conn;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002383 binder::Status status = sf->createConnection(&conn);
2384 if (status.isOk() && conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002385 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002386 mStatus = NO_ERROR;
2387 }
2388 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002389}
2390
Mathias Agopian698c0872011-06-28 19:09:31 -07002391SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07002392 dispose();
2393}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07002394
Mathias Agopian698c0872011-06-28 19:09:31 -07002395status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002396 return mStatus;
2397}
2398
Mathias Agopian698c0872011-06-28 19:09:31 -07002399sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002400 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002401}
2402
Mathias Agopiand4784a32010-05-27 19:41:15 -07002403status_t SurfaceComposerClient::linkToComposerDeath(
2404 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07002405 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002406 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2407 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002408}
2409
Mathias Agopian698c0872011-06-28 19:09:31 -07002410void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002411 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07002412 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002413 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07002414 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002415 client = mClient; // hold ref while lock is held
2416 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002417 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07002418 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419}
2420
Carlos Martinez Romero4609a4a2022-11-30 00:38:11 +00002421status_t SurfaceComposerClient::bootFinished() {
2422 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2423 binder::Status status = sf->bootFinished();
2424 return statusTFromBinderStatus(status);
2425}
2426
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002427sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002428 PixelFormat format, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002429 const sp<IBinder>& parentHandle,
Valerie Hau1acd6962019-10-28 16:35:48 -07002430 LayerMetadata metadata,
2431 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07002432 sp<SurfaceControl> s;
Vishnu Nair992496b2020-10-22 17:27:21 -07002433 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07002434 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07002435 return s;
2436}
2437
Patrick Williamsa361de62022-10-06 20:34:10 +00002438static std::string toString(const String16& string) {
2439 return std::string(String8(string).c_str());
2440}
2441
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002442status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
2443 PixelFormat format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002444 sp<SurfaceControl>* outSurface, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002445 const sp<IBinder>& parentHandle,
2446 LayerMetadata metadata,
Valerie Hau1acd6962019-10-28 16:35:48 -07002447 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002448 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07002449 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07002450
Mathias Agopian698c0872011-06-28 19:09:31 -07002451 if (mStatus == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002452 gui::CreateSurfaceResult result;
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +00002453 binder::Status status = mClient->createSurface(std::string(name.c_str()), flags,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002454 parentHandle, std::move(metadata), &result);
2455 err = statusTFromBinderStatus(status);
Valerie Hau1acd6962019-10-28 16:35:48 -07002456 if (outTransformHint) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002457 *outTransformHint = result.transformHint;
Valerie Hau1acd6962019-10-28 16:35:48 -07002458 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002459 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
2460 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002461 *outSurface = new SurfaceControl(this, result.handle, result.layerId,
2462 toString(result.layerName), w, h, format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002463 result.transformHint, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -07002464 }
2465 }
Robert Carr3b382ed2018-03-14 13:49:41 -07002466 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07002467}
2468
chaviwfe94a222019-08-21 13:52:59 -07002469sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
2470 if (mirrorFromSurface == nullptr) {
2471 return nullptr;
2472 }
2473
chaviwfe94a222019-08-21 13:52:59 -07002474 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Patrick Williamsa361de62022-10-06 20:34:10 +00002475 gui::CreateSurfaceResult result;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002476 const binder::Status status = mClient->mirrorSurface(mirrorFromHandle, &result);
2477 const status_t err = statusTFromBinderStatus(status);
chaviwfe94a222019-08-21 13:52:59 -07002478 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002479 return new SurfaceControl(this, result.handle, result.layerId, toString(result.layerName));
chaviwfe94a222019-08-21 13:52:59 -07002480 }
2481 return nullptr;
2482}
2483
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002484sp<SurfaceControl> SurfaceComposerClient::mirrorDisplay(DisplayId displayId) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002485 gui::CreateSurfaceResult result;
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002486 const binder::Status status = mClient->mirrorDisplay(displayId.value, &result);
2487 const status_t err = statusTFromBinderStatus(status);
2488 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002489 return new SurfaceControl(this, result.handle, result.layerId, toString(result.layerName));
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002490 }
2491 return nullptr;
2492}
2493
Svetoslavd85084b2014-03-20 10:28:31 -07002494status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
2495 if (mStatus != NO_ERROR) {
2496 return mStatus;
2497 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002498 const binder::Status status = mClient->clearLayerFrameStats(token);
2499 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002500}
2501
2502status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
2503 FrameStats* outStats) const {
2504 if (mStatus != NO_ERROR) {
2505 return mStatus;
2506 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002507 gui::FrameStats stats;
2508 const binder::Status status = mClient->getLayerFrameStats(token, &stats);
2509 if (status.isOk()) {
2510 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2511 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2512 for (const auto& t : stats.desiredPresentTimesNano) {
2513 outStats->desiredPresentTimesNano.add(t);
2514 }
2515 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2516 for (const auto& t : stats.actualPresentTimesNano) {
2517 outStats->actualPresentTimesNano.add(t);
2518 }
2519 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2520 for (const auto& t : stats.frameReadyTimesNano) {
2521 outStats->frameReadyTimesNano.add(t);
2522 }
2523 }
2524 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002525}
2526
Mathias Agopian698c0872011-06-28 19:09:31 -07002527// ----------------------------------------------------------------------------
2528
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002529status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
2530 ui::DisplayState* state) {
Huihong Luoaa7fc2e2022-02-15 10:43:00 -08002531 gui::DisplayState ds;
2532 binder::Status status =
2533 ComposerServiceAIDL::getComposerService()->getDisplayState(display, &ds);
2534 if (status.isOk()) {
2535 state->layerStack = ui::LayerStack::fromValue(ds.layerStack);
2536 state->orientation = static_cast<ui::Rotation>(ds.orientation);
2537 state->layerStackSpaceRect =
2538 ui::Size(ds.layerStackSpaceRect.width, ds.layerStackSpaceRect.height);
2539 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002540 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002541}
2542
Sally Qi6bb12822022-10-05 11:42:30 -07002543status_t SurfaceComposerClient::getStaticDisplayInfo(int64_t displayId,
Huihong Luoa79ddf42022-02-17 00:01:38 -08002544 ui::StaticDisplayInfo* outInfo) {
2545 using Tag = android::gui::DeviceProductInfo::ManufactureOrModelDate::Tag;
2546 gui::StaticDisplayInfo ginfo;
2547 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002548 ComposerServiceAIDL::getComposerService()->getStaticDisplayInfo(displayId, &ginfo);
Huihong Luoa79ddf42022-02-17 00:01:38 -08002549 if (status.isOk()) {
2550 // convert gui::StaticDisplayInfo to ui::StaticDisplayInfo
2551 outInfo->connectionType = static_cast<ui::DisplayConnectionType>(ginfo.connectionType);
2552 outInfo->density = ginfo.density;
2553 outInfo->secure = ginfo.secure;
2554 outInfo->installOrientation = static_cast<ui::Rotation>(ginfo.installOrientation);
2555
Snild Dolkow8d675642023-07-14 12:43:35 +02002556 if (const std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo) {
2557 DeviceProductInfo info;
2558 info.name = dpi->name;
2559 if (dpi->manufacturerPnpId.size() > 0) {
2560 // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
2561 constexpr int kMaxPnpIdSize = 4;
2562 size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
2563 std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
2564 }
2565 if (dpi->relativeAddress.size() > 0) {
2566 std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
2567 std::back_inserter(info.relativeAddress));
2568 }
2569 info.productId = dpi->productId;
Huihong Luoa79ddf42022-02-17 00:01:38 -08002570
Snild Dolkow8d675642023-07-14 12:43:35 +02002571 const gui::DeviceProductInfo::ManufactureOrModelDate& date =
2572 dpi->manufactureOrModelDate;
2573 if (date.getTag() == Tag::modelYear) {
2574 DeviceProductInfo::ModelYear modelYear;
2575 modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
2576 info.manufactureOrModelDate = modelYear;
2577 } else if (date.getTag() == Tag::manufactureYear) {
2578 DeviceProductInfo::ManufactureYear manufactureYear;
2579 manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
2580 info.manufactureOrModelDate = manufactureYear;
2581 } else if (date.getTag() == Tag::manufactureWeekAndYear) {
2582 DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
2583 weekAndYear.year =
2584 date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
2585 weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
2586 info.manufactureOrModelDate = weekAndYear;
2587 }
2588
2589 outInfo->deviceProductInfo = info;
2590 }
Huihong Luoa79ddf42022-02-17 00:01:38 -08002591 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002592 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002593}
2594
Sally Qi6bb12822022-10-05 11:42:30 -07002595void SurfaceComposerClient::getDynamicDisplayInfoInternal(gui::DynamicDisplayInfo& ginfo,
2596 ui::DynamicDisplayInfo*& outInfo) {
2597 // convert gui::DynamicDisplayInfo to ui::DynamicDisplayInfo
2598 outInfo->supportedDisplayModes.clear();
2599 outInfo->supportedDisplayModes.reserve(ginfo.supportedDisplayModes.size());
2600 for (const auto& mode : ginfo.supportedDisplayModes) {
2601 ui::DisplayMode outMode;
2602 outMode.id = mode.id;
2603 outMode.resolution.width = mode.resolution.width;
2604 outMode.resolution.height = mode.resolution.height;
2605 outMode.xDpi = mode.xDpi;
2606 outMode.yDpi = mode.yDpi;
Alec Mouri55e31032023-10-02 20:34:18 +00002607 outMode.peakRefreshRate = mode.peakRefreshRate;
Alec Mourid122a1c2023-09-30 01:36:33 +00002608 outMode.vsyncRate = mode.vsyncRate;
Sally Qi6bb12822022-10-05 11:42:30 -07002609 outMode.appVsyncOffset = mode.appVsyncOffset;
2610 outMode.sfVsyncOffset = mode.sfVsyncOffset;
2611 outMode.presentationDeadline = mode.presentationDeadline;
2612 outMode.group = mode.group;
2613 std::transform(mode.supportedHdrTypes.begin(), mode.supportedHdrTypes.end(),
2614 std::back_inserter(outMode.supportedHdrTypes),
2615 [](const int32_t& value) { return static_cast<ui::Hdr>(value); });
2616 outInfo->supportedDisplayModes.push_back(outMode);
2617 }
2618
2619 outInfo->activeDisplayModeId = ginfo.activeDisplayModeId;
2620 outInfo->renderFrameRate = ginfo.renderFrameRate;
2621
2622 outInfo->supportedColorModes.clear();
2623 outInfo->supportedColorModes.reserve(ginfo.supportedColorModes.size());
2624 for (const auto& cmode : ginfo.supportedColorModes) {
2625 outInfo->supportedColorModes.push_back(static_cast<ui::ColorMode>(cmode));
2626 }
2627
2628 outInfo->activeColorMode = static_cast<ui::ColorMode>(ginfo.activeColorMode);
2629
2630 std::vector<ui::Hdr> types;
2631 types.reserve(ginfo.hdrCapabilities.supportedHdrTypes.size());
2632 for (const auto& hdr : ginfo.hdrCapabilities.supportedHdrTypes) {
2633 types.push_back(static_cast<ui::Hdr>(hdr));
2634 }
2635 outInfo->hdrCapabilities = HdrCapabilities(types, ginfo.hdrCapabilities.maxLuminance,
2636 ginfo.hdrCapabilities.maxAverageLuminance,
2637 ginfo.hdrCapabilities.minLuminance);
2638
2639 outInfo->autoLowLatencyModeSupported = ginfo.autoLowLatencyModeSupported;
2640 outInfo->gameContentTypeSupported = ginfo.gameContentTypeSupported;
2641 outInfo->preferredBootDisplayMode = ginfo.preferredBootDisplayMode;
2642}
2643
2644status_t SurfaceComposerClient::getDynamicDisplayInfoFromId(int64_t displayId,
2645 ui::DynamicDisplayInfo* outInfo) {
Huihong Luo38603fd2022-02-21 14:32:54 -08002646 gui::DynamicDisplayInfo ginfo;
2647 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002648 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromId(displayId,
2649 &ginfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002650 if (status.isOk()) {
Sally Qi6bb12822022-10-05 11:42:30 -07002651 getDynamicDisplayInfoInternal(ginfo, outInfo);
2652 }
2653 return statusTFromBinderStatus(status);
2654}
Huihong Luo38603fd2022-02-21 14:32:54 -08002655
Sally Qi6bb12822022-10-05 11:42:30 -07002656status_t SurfaceComposerClient::getDynamicDisplayInfoFromToken(const sp<IBinder>& display,
2657 ui::DynamicDisplayInfo* outInfo) {
2658 gui::DynamicDisplayInfo ginfo;
2659 binder::Status status =
2660 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromToken(display,
2661 &ginfo);
2662 if (status.isOk()) {
2663 getDynamicDisplayInfoInternal(ginfo, outInfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002664 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002665 return statusTFromBinderStatus(status);
Dan Stoza7f7da322014-05-02 15:26:25 -07002666}
2667
Marin Shalamanova7fe3042021-01-29 21:02:08 +01002668status_t SurfaceComposerClient::getActiveDisplayMode(const sp<IBinder>& display,
2669 ui::DisplayMode* mode) {
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002670 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -07002671
2672 status_t result = getDynamicDisplayInfoFromToken(display, &info);
Dan Stoza7f7da322014-05-02 15:26:25 -07002673 if (result != NO_ERROR) {
2674 return result;
2675 }
2676
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002677 if (const auto activeMode = info.getActiveDisplayMode()) {
2678 *mode = *activeMode;
2679 return NO_ERROR;
Dan Stoza7f7da322014-05-02 15:26:25 -07002680 }
2681
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002682 ALOGE("Active display mode not found.");
2683 return NAME_NOT_FOUND;
Dan Stoza7f7da322014-05-02 15:26:25 -07002684}
2685
Ady Abraham285f8c12022-10-11 17:12:14 -07002686status_t SurfaceComposerClient::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
2687 const gui::DisplayModeSpecs& specs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002688 binder::Status status =
Ady Abraham285f8c12022-10-11 17:12:14 -07002689 ComposerServiceAIDL::getComposerService()->setDesiredDisplayModeSpecs(displayToken,
2690 specs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002691 return statusTFromBinderStatus(status);
Ana Krulec0782b882019-10-15 17:34:54 -07002692}
2693
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002694status_t SurfaceComposerClient::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002695 gui::DisplayModeSpecs* outSpecs) {
2696 if (!outSpecs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002697 return BAD_VALUE;
2698 }
Huihong Luo02186fb2022-02-23 14:21:54 -08002699 binder::Status status =
2700 ComposerServiceAIDL::getComposerService()->getDesiredDisplayModeSpecs(displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002701 outSpecs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002702 return statusTFromBinderStatus(status);
Ana Krulec234bb162019-11-10 22:55:55 +01002703}
2704
Daniel Solomon42d04562019-01-20 21:03:19 -08002705status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
2706 ui::DisplayPrimaries& outPrimaries) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002707 gui::DisplayPrimaries primaries;
2708 binder::Status status =
2709 ComposerServiceAIDL::getComposerService()->getDisplayNativePrimaries(display,
2710 &primaries);
2711 if (status.isOk()) {
2712 outPrimaries.red.X = primaries.red.X;
2713 outPrimaries.red.Y = primaries.red.Y;
2714 outPrimaries.red.Z = primaries.red.Z;
2715
2716 outPrimaries.green.X = primaries.green.X;
2717 outPrimaries.green.Y = primaries.green.Y;
2718 outPrimaries.green.Z = primaries.green.Z;
2719
2720 outPrimaries.blue.X = primaries.blue.X;
2721 outPrimaries.blue.Y = primaries.blue.Y;
2722 outPrimaries.blue.Z = primaries.blue.Z;
2723
2724 outPrimaries.white.X = primaries.white.X;
2725 outPrimaries.white.Y = primaries.white.Y;
2726 outPrimaries.white.Z = primaries.white.Z;
2727 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002728 return statusTFromBinderStatus(status);
Daniel Solomon42d04562019-01-20 21:03:19 -08002729}
2730
Michael Wright28f24d02016-07-12 13:30:53 -07002731status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07002732 ColorMode colorMode) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002733 binder::Status status = ComposerServiceAIDL::getComposerService()
2734 ->setActiveColorMode(display, static_cast<int>(colorMode));
Huihong Luo3bdef862022-03-03 11:57:19 -08002735 return statusTFromBinderStatus(status);
Michael Wright28f24d02016-07-12 13:30:53 -07002736}
2737
Kriti Dang7defaf32021-11-15 11:55:43 +01002738status_t SurfaceComposerClient::getBootDisplayModeSupport(bool* support) {
Huihong Luo37396db2022-02-15 10:43:00 -08002739 binder::Status status =
2740 ComposerServiceAIDL::getComposerService()->getBootDisplayModeSupport(support);
Huihong Luo3bdef862022-03-03 11:57:19 -08002741 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002742}
2743
Sally Qi0cbd08b2022-08-17 12:12:28 -07002744status_t SurfaceComposerClient::getOverlaySupport(gui::OverlayProperties* outProperties) {
2745 binder::Status status =
2746 ComposerServiceAIDL::getComposerService()->getOverlaySupport(outProperties);
2747 return statusTFromBinderStatus(status);
2748}
2749
Kriti Dang7defaf32021-11-15 11:55:43 +01002750status_t SurfaceComposerClient::setBootDisplayMode(const sp<IBinder>& display,
2751 ui::DisplayModeId displayModeId) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002752 binder::Status status = ComposerServiceAIDL::getComposerService()
2753 ->setBootDisplayMode(display, static_cast<int>(displayModeId));
Huihong Luo3bdef862022-03-03 11:57:19 -08002754 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002755}
2756
2757status_t SurfaceComposerClient::clearBootDisplayMode(const sp<IBinder>& display) {
Huihong Luo37396db2022-02-15 10:43:00 -08002758 binder::Status status =
2759 ComposerServiceAIDL::getComposerService()->clearBootDisplayMode(display);
Huihong Luo3bdef862022-03-03 11:57:19 -08002760 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002761}
2762
Kriti Dang674b9372022-11-18 10:58:44 +01002763status_t SurfaceComposerClient::getHdrConversionCapabilities(
2764 std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) {
2765 binder::Status status = ComposerServiceAIDL::getComposerService()->getHdrConversionCapabilities(
2766 hdrConversionCapabilities);
2767 return statusTFromBinderStatus(status);
2768}
2769
2770status_t SurfaceComposerClient::setHdrConversionStrategy(
Kriti Dangd432bb52023-02-09 18:21:04 +01002771 gui::HdrConversionStrategy hdrConversionStrategy, ui::Hdr* outPreferredHdrOutputType) {
2772 int hdrType;
2773 binder::Status status = ComposerServiceAIDL::getComposerService()
2774 ->setHdrConversionStrategy(hdrConversionStrategy, &hdrType);
2775 *outPreferredHdrOutputType = static_cast<ui::Hdr>(hdrType);
Kriti Dang674b9372022-11-18 10:58:44 +01002776 return statusTFromBinderStatus(status);
2777}
2778
Kriti Dangafd1fda2023-01-17 13:33:15 +01002779status_t SurfaceComposerClient::getHdrOutputConversionSupport(bool* isSupported) {
2780 binder::Status status =
2781 ComposerServiceAIDL::getComposerService()->getHdrOutputConversionSupport(isSupported);
2782 return statusTFromBinderStatus(status);
2783}
2784
Andy Yu8c2703d2023-11-03 11:22:46 -07002785status_t SurfaceComposerClient::setGameModeFrameRateOverride(uid_t uid, float frameRate) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002786 binder::Status status =
Andy Yu8c2703d2023-11-03 11:22:46 -07002787 ComposerServiceAIDL::getComposerService()->setGameModeFrameRateOverride(uid, frameRate);
2788 return statusTFromBinderStatus(status);
2789}
2790
2791status_t SurfaceComposerClient::setGameDefaultFrameRateOverride(uid_t uid, float frameRate) {
2792 binder::Status status =
2793 ComposerServiceAIDL::getComposerService()->setGameDefaultFrameRateOverride(uid,
2794 frameRate);
Huihong Luo3bdef862022-03-03 11:57:19 -08002795 return statusTFromBinderStatus(status);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08002796}
2797
Tony Huangf3621102023-09-04 17:14:22 +08002798status_t SurfaceComposerClient::updateSmallAreaDetection(std::vector<int32_t>& appIds,
Tony Huang9ac5e6e2023-08-24 09:01:44 +00002799 std::vector<float>& thresholds) {
2800 binder::Status status =
Tony Huangf3621102023-09-04 17:14:22 +08002801 ComposerServiceAIDL::getComposerService()->updateSmallAreaDetection(appIds, thresholds);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00002802 return statusTFromBinderStatus(status);
2803}
2804
Tony Huangf3621102023-09-04 17:14:22 +08002805status_t SurfaceComposerClient::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00002806 binder::Status status =
Tony Huangf3621102023-09-04 17:14:22 +08002807 ComposerServiceAIDL::getComposerService()->setSmallAreaDetectionThreshold(appId,
Tony Huang9ac5e6e2023-08-24 09:01:44 +00002808 threshold);
2809 return statusTFromBinderStatus(status);
2810}
2811
Galia Peycheva5492cb52019-10-30 14:13:16 +01002812void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002813 ComposerServiceAIDL::getComposerService()->setAutoLowLatencyMode(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002814}
2815
Galia Peycheva5492cb52019-10-30 14:13:16 +01002816void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002817 ComposerServiceAIDL::getComposerService()->setGameContentType(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002818}
2819
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002820void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
2821 int mode) {
Huihong Luo37396db2022-02-15 10:43:00 -08002822 ComposerServiceAIDL::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07002823}
2824
Peiyong Linc6780972018-10-28 15:24:08 -07002825status_t SurfaceComposerClient::getCompositionPreference(
2826 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
2827 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
Huihong Luo05539a12022-02-23 10:29:40 -08002828 gui::CompositionPreference pref;
2829 binder::Status status =
2830 ComposerServiceAIDL::getComposerService()->getCompositionPreference(&pref);
2831 if (status.isOk()) {
2832 *defaultDataspace = static_cast<ui::Dataspace>(pref.defaultDataspace);
2833 *defaultPixelFormat = static_cast<ui::PixelFormat>(pref.defaultPixelFormat);
2834 *wideColorGamutDataspace = static_cast<ui::Dataspace>(pref.wideColorGamutDataspace);
2835 *wideColorGamutPixelFormat = static_cast<ui::PixelFormat>(pref.wideColorGamutPixelFormat);
2836 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002837 return statusTFromBinderStatus(status);
Peiyong Lin0256f722018-08-31 15:45:10 -07002838}
2839
Peiyong Lin08d10512019-01-16 13:27:35 -08002840bool SurfaceComposerClient::getProtectedContentSupport() {
2841 bool supported = false;
Huihong Luo05539a12022-02-23 10:29:40 -08002842 ComposerServiceAIDL::getComposerService()->getProtectedContentSupport(&supported);
Peiyong Lin08d10512019-01-16 13:27:35 -08002843 return supported;
2844}
2845
Svetoslavd85084b2014-03-20 10:28:31 -07002846status_t SurfaceComposerClient::clearAnimationFrameStats() {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002847 binder::Status status = ComposerServiceAIDL::getComposerService()->clearAnimationFrameStats();
Huihong Luo3bdef862022-03-03 11:57:19 -08002848 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002849}
2850
2851status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002852 gui::FrameStats stats;
2853 binder::Status status =
2854 ComposerServiceAIDL::getComposerService()->getAnimationFrameStats(&stats);
2855 if (status.isOk()) {
2856 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2857 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2858 for (const auto& t : stats.desiredPresentTimesNano) {
2859 outStats->desiredPresentTimesNano.add(t);
2860 }
2861 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2862 for (const auto& t : stats.actualPresentTimesNano) {
2863 outStats->actualPresentTimesNano.add(t);
2864 }
2865 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2866 for (const auto& t : stats.frameReadyTimesNano) {
2867 outStats->frameReadyTimesNano.add(t);
2868 }
2869 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002870 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002871}
2872
Kriti Dang49ad4132021-01-08 11:49:56 +01002873status_t SurfaceComposerClient::overrideHdrTypes(const sp<IBinder>& display,
2874 const std::vector<ui::Hdr>& hdrTypes) {
Huihong Luo05539a12022-02-23 10:29:40 -08002875 std::vector<int32_t> hdrTypesVector;
2876 hdrTypesVector.reserve(hdrTypes.size());
2877 for (auto t : hdrTypes) {
2878 hdrTypesVector.push_back(static_cast<int32_t>(t));
2879 }
2880
2881 binder::Status status =
2882 ComposerServiceAIDL::getComposerService()->overrideHdrTypes(display, hdrTypesVector);
Huihong Luo3bdef862022-03-03 11:57:19 -08002883 return statusTFromBinderStatus(status);
Kriti Dang49ad4132021-01-08 11:49:56 +01002884}
2885
Tej Singhe2751772021-04-06 22:05:29 -07002886status_t SurfaceComposerClient::onPullAtom(const int32_t atomId, std::string* outData,
2887 bool* success) {
Huihong Luo05539a12022-02-23 10:29:40 -08002888 gui::PullAtomData pad;
2889 binder::Status status = ComposerServiceAIDL::getComposerService()->onPullAtom(atomId, &pad);
2890 if (status.isOk()) {
Huihong Luo30aa4372022-10-03 14:54:12 -07002891 outData->assign(pad.data.begin(), pad.data.end());
Huihong Luo05539a12022-02-23 10:29:40 -08002892 *success = pad.success;
2893 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002894 return statusTFromBinderStatus(status);
Tej Singhe2751772021-04-06 22:05:29 -07002895}
2896
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002897status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
2898 ui::PixelFormat* outFormat,
2899 ui::Dataspace* outDataspace,
2900 uint8_t* outComponentMask) {
Huihong Luo05539a12022-02-23 10:29:40 -08002901 if (!outFormat || !outDataspace || !outComponentMask) {
2902 return BAD_VALUE;
2903 }
2904
2905 gui::ContentSamplingAttributes attrs;
2906 binder::Status status = ComposerServiceAIDL::getComposerService()
2907 ->getDisplayedContentSamplingAttributes(display, &attrs);
2908 if (status.isOk()) {
2909 *outFormat = static_cast<ui::PixelFormat>(attrs.format);
2910 *outDataspace = static_cast<ui::Dataspace>(attrs.dataspace);
2911 *outComponentMask = static_cast<uint8_t>(attrs.componentMask);
2912 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002913 return statusTFromBinderStatus(status);
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002914}
2915
Kevin DuBois74e53772018-11-19 10:52:38 -08002916status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
2917 bool enable, uint8_t componentMask,
2918 uint64_t maxFrames) {
Huihong Luo05539a12022-02-23 10:29:40 -08002919 binder::Status status =
2920 ComposerServiceAIDL::getComposerService()
2921 ->setDisplayContentSamplingEnabled(display, enable,
2922 static_cast<int8_t>(componentMask),
2923 static_cast<int64_t>(maxFrames));
Huihong Luo3bdef862022-03-03 11:57:19 -08002924 return statusTFromBinderStatus(status);
Kevin DuBois74e53772018-11-19 10:52:38 -08002925}
2926
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002927status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
2928 uint64_t maxFrames, uint64_t timestamp,
2929 DisplayedFrameStats* outStats) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002930 if (!outStats) {
2931 return BAD_VALUE;
2932 }
2933
2934 gui::DisplayedFrameStats stats;
2935 binder::Status status =
2936 ComposerServiceAIDL::getComposerService()->getDisplayedContentSample(display, maxFrames,
2937 timestamp, &stats);
2938 if (status.isOk()) {
2939 // convert gui::DisplayedFrameStats to ui::DisplayedFrameStats
2940 outStats->numFrames = static_cast<uint64_t>(stats.numFrames);
2941 outStats->component_0_sample.reserve(stats.component_0_sample.size());
2942 for (const auto& s : stats.component_0_sample) {
2943 outStats->component_0_sample.push_back(static_cast<uint64_t>(s));
2944 }
2945 outStats->component_1_sample.reserve(stats.component_1_sample.size());
2946 for (const auto& s : stats.component_1_sample) {
2947 outStats->component_1_sample.push_back(static_cast<uint64_t>(s));
2948 }
2949 outStats->component_2_sample.reserve(stats.component_2_sample.size());
2950 for (const auto& s : stats.component_2_sample) {
2951 outStats->component_2_sample.push_back(static_cast<uint64_t>(s));
2952 }
2953 outStats->component_3_sample.reserve(stats.component_3_sample.size());
2954 for (const auto& s : stats.component_3_sample) {
2955 outStats->component_3_sample.push_back(static_cast<uint64_t>(s));
2956 }
2957 }
2958 return statusTFromBinderStatus(status);
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002959}
Marissa Wall35187b32019-01-08 10:08:52 -08002960
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002961status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
2962 bool* outIsWideColorDisplay) {
Huihong Luo37396db2022-02-15 10:43:00 -08002963 binder::Status status =
2964 ComposerServiceAIDL::getComposerService()->isWideColorDisplay(display,
2965 outIsWideColorDisplay);
Huihong Luo3bdef862022-03-03 11:57:19 -08002966 return statusTFromBinderStatus(status);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002967}
2968
Kevin DuBois00c66832019-02-18 16:21:31 -08002969status_t SurfaceComposerClient::addRegionSamplingListener(
2970 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
2971 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002972 gui::ARect rect;
2973 rect.left = samplingArea.left;
2974 rect.top = samplingArea.top;
2975 rect.right = samplingArea.right;
2976 rect.bottom = samplingArea.bottom;
2977 binder::Status status =
2978 ComposerServiceAIDL::getComposerService()->addRegionSamplingListener(rect,
2979 stopLayerHandle,
2980 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002981 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002982}
2983
2984status_t SurfaceComposerClient::removeRegionSamplingListener(
2985 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002986 binder::Status status =
2987 ComposerServiceAIDL::getComposerService()->removeRegionSamplingListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002988 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002989}
2990
Alec Mouria9a68a62021-03-04 19:14:50 -08002991status_t SurfaceComposerClient::addFpsListener(int32_t taskId,
Alec Mouriadebf5c2021-01-05 12:57:36 -08002992 const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002993 binder::Status status =
2994 ComposerServiceAIDL::getComposerService()->addFpsListener(taskId, listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002995 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08002996}
2997
2998status_t SurfaceComposerClient::removeFpsListener(const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002999 binder::Status status = ComposerServiceAIDL::getComposerService()->removeFpsListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003000 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08003001}
3002
Galia Peycheva8f04b302021-04-27 13:25:38 +02003003status_t SurfaceComposerClient::addTunnelModeEnabledListener(
3004 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003005 binder::Status status =
3006 ComposerServiceAIDL::getComposerService()->addTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003007 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02003008}
3009
3010status_t SurfaceComposerClient::removeTunnelModeEnabledListener(
3011 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003012 binder::Status status =
3013 ComposerServiceAIDL::getComposerService()->removeTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003014 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02003015}
3016
Dan Gittik57e63c52019-01-18 16:37:54 +00003017bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
3018 bool support = false;
Huihong Luo37396db2022-02-15 10:43:00 -08003019 binder::Status status =
3020 ComposerServiceAIDL::getComposerService()->getDisplayBrightnessSupport(displayToken,
3021 &support);
3022 return status.isOk() ? support : false;
Dan Gittik57e63c52019-01-18 16:37:54 +00003023}
3024
3025status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
John Reck22be6962021-03-10 12:59:54 -05003026 const gui::DisplayBrightness& brightness) {
Huihong Luo37396db2022-02-15 10:43:00 -08003027 binder::Status status =
3028 ComposerServiceAIDL::getComposerService()->setDisplayBrightness(displayToken,
3029 brightness);
Huihong Luo3bdef862022-03-03 11:57:19 -08003030 return statusTFromBinderStatus(status);
Dan Gittik57e63c52019-01-18 16:37:54 +00003031}
3032
John Reck88270902021-03-18 11:27:35 -04003033status_t SurfaceComposerClient::addHdrLayerInfoListener(
3034 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08003035 binder::Status status =
3036 ComposerServiceAIDL::getComposerService()->addHdrLayerInfoListener(displayToken,
3037 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003038 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04003039}
3040
3041status_t SurfaceComposerClient::removeHdrLayerInfoListener(
3042 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08003043 binder::Status status =
3044 ComposerServiceAIDL::getComposerService()->removeHdrLayerInfoListener(displayToken,
3045 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003046 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04003047}
3048
Lais Andrade3a6e47d2020-04-02 11:20:16 +01003049status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
Huihong Luo37396db2022-02-15 10:43:00 -08003050 binder::Status status = ComposerServiceAIDL::getComposerService()->notifyPowerBoost(boostId);
Huihong Luo3bdef862022-03-03 11:57:19 -08003051 return statusTFromBinderStatus(status);
Ady Abraham8532d012019-05-08 14:50:56 -07003052}
3053
Vishnu Nairb13bb952019-11-15 10:24:08 -08003054status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
3055 const half4& spotColor, float lightPosY,
3056 float lightPosZ, float lightRadius) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003057 gui::Color ambientColorG, spotColorG;
3058 ambientColorG.r = ambientColor.r;
3059 ambientColorG.g = ambientColor.g;
3060 ambientColorG.b = ambientColor.b;
3061 ambientColorG.a = ambientColor.a;
3062 spotColorG.r = spotColor.r;
3063 spotColorG.g = spotColor.g;
3064 spotColorG.b = spotColor.b;
3065 spotColorG.a = spotColor.a;
3066 binder::Status status =
3067 ComposerServiceAIDL::getComposerService()->setGlobalShadowSettings(ambientColorG,
3068 spotColorG,
3069 lightPosY, lightPosZ,
3070 lightRadius);
3071 return statusTFromBinderStatus(status);
Vishnu Nairb13bb952019-11-15 10:24:08 -08003072}
3073
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05003074std::optional<DisplayDecorationSupport> SurfaceComposerClient::getDisplayDecorationSupport(
3075 const sp<IBinder>& displayToken) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003076 std::optional<gui::DisplayDecorationSupport> gsupport;
3077 binder::Status status =
3078 ComposerServiceAIDL::getComposerService()->getDisplayDecorationSupport(displayToken,
3079 &gsupport);
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05003080 std::optional<DisplayDecorationSupport> support;
Leon Scroggins III9a11a2e2022-10-04 12:46:21 -04003081 if (status.isOk() && gsupport.has_value()) {
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04003082 support.emplace(DisplayDecorationSupport{
3083 .format =
3084 static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
3085 gsupport->format),
3086 .alphaInterpretation =
Huihong Luo3bdef862022-03-03 11:57:19 -08003087 static_cast<aidl::android::hardware::graphics::common::AlphaInterpretation>(
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04003088 gsupport->alphaInterpretation)
3089 });
Huihong Luo3bdef862022-03-03 11:57:19 -08003090 }
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -05003091 return support;
3092}
3093
Huihong Luo02186fb2022-02-23 14:21:54 -08003094int SurfaceComposerClient::getGpuContextPriority() {
3095 int priority;
3096 binder::Status status =
3097 ComposerServiceAIDL::getComposerService()->getGpuContextPriority(&priority);
3098 if (!status.isOk()) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003099 status_t err = statusTFromBinderStatus(status);
Huihong Luo02186fb2022-02-23 14:21:54 -08003100 ALOGE("getGpuContextPriority failed to read data: %s (%d)", strerror(-err), err);
3101 return 0;
3102 }
3103 return priority;
Ana Krulec31f2b3c2020-12-14 14:30:09 -08003104}
3105
chaviw60c9d3e2021-06-04 12:52:17 -05003106status_t SurfaceComposerClient::addWindowInfosListener(
chaviwa2948ad2022-03-08 11:07:01 -06003107 const sp<WindowInfosListener>& windowInfosListener,
3108 std::pair<std::vector<gui::WindowInfo>, std::vector<gui::DisplayInfo>>* outInitialInfo) {
chaviw60c9d3e2021-06-04 12:52:17 -05003109 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08003110 ->addWindowInfosListener(windowInfosListener, ComposerServiceAIDL::getComposerService(),
chaviwa2948ad2022-03-08 11:07:01 -06003111 outInitialInfo);
chaviw60c9d3e2021-06-04 12:52:17 -05003112}
3113
3114status_t SurfaceComposerClient::removeWindowInfosListener(
3115 const sp<WindowInfosListener>& windowInfosListener) {
3116 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08003117 ->removeWindowInfosListener(windowInfosListener,
3118 ComposerServiceAIDL::getComposerService());
chaviw60c9d3e2021-06-04 12:52:17 -05003119}
3120
Mathias Agopian698c0872011-06-28 19:09:31 -07003121// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003122
chaviw690db382020-07-27 16:46:46 -07003123status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003124 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003125 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003126 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003127
Huihong Luof5029222021-12-16 14:33:46 -08003128 binder::Status status = s->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003129 return statusTFromBinderStatus(status);
Robert Carr673134e2017-01-09 19:48:38 -08003130}
3131
John Reck7e4c4872023-11-14 18:31:03 -05003132status_t ScreenshotClient::captureDisplay(DisplayId displayId, const gui::CaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003133 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003134 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
chaviw93df2ea2019-04-30 16:45:12 -07003135 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003136
John Reck7e4c4872023-11-14 18:31:03 -05003137 binder::Status status = s->captureDisplayById(displayId.value, captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003138 return statusTFromBinderStatus(status);
chaviw93df2ea2019-04-30 16:45:12 -07003139}
3140
chaviw26c52482020-07-28 16:25:52 -07003141status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003142 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003143 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003144 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003145
Huihong Luof5029222021-12-16 14:33:46 -08003146 binder::Status status = s->captureLayers(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003147 return statusTFromBinderStatus(status);
chaviwa76b2712017-09-20 12:02:26 -07003148}
Dominik Laskowski718f9602019-11-09 20:01:35 -08003149
chaviw69058fb2021-09-27 09:37:30 -05003150// ---------------------------------------------------------------------------------
3151
3152void ReleaseCallbackThread::addReleaseCallback(const ReleaseCallbackId callbackId,
3153 sp<Fence> releaseFence) {
3154 std::scoped_lock<std::mutex> lock(mMutex);
3155 if (!mStarted) {
3156 mThread = std::thread(&ReleaseCallbackThread::threadMain, this);
3157 mStarted = true;
3158 }
3159
3160 mCallbackInfos.emplace(callbackId, std::move(releaseFence));
3161 mReleaseCallbackPending.notify_one();
3162}
3163
3164void ReleaseCallbackThread::threadMain() {
3165 const auto listener = TransactionCompletedListener::getInstance();
3166 std::queue<std::tuple<const ReleaseCallbackId, const sp<Fence>>> callbackInfos;
3167 while (true) {
3168 {
3169 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003170 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003171 callbackInfos = std::move(mCallbackInfos);
3172 mCallbackInfos = {};
3173 }
3174
3175 while (!callbackInfos.empty()) {
3176 auto [callbackId, releaseFence] = callbackInfos.front();
Huihong Luoffee3bc2023-01-17 16:14:35 +00003177 listener->onReleaseBuffer(callbackId, std::move(releaseFence), UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -05003178 callbackInfos.pop();
3179 }
3180
3181 {
3182 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003183 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003184 if (mCallbackInfos.size() == 0) {
3185 mReleaseCallbackPending.wait(lock);
3186 }
3187 }
3188 }
3189}
3190
Dominik Laskowski718f9602019-11-09 20:01:35 -08003191} // namespace android