blob: 90076f122fcabb48d70f912f1fa6a42bba6878f0 [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
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Patrick Williams641f7f22022-06-22 19:25:35 +000022#include <android/gui/BnWindowInfosReportedListener.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080023#include <android/gui/DisplayState.h>
Huihong Luod3d8f8e2022-03-08 14:48:46 -080024#include <android/gui/ISurfaceComposerClient.h>
chaviw60c9d3e2021-06-04 12:52:17 -050025#include <android/gui/IWindowInfosListener.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080026#include <android/os/IInputConstants.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <utils/SortedVector.h>
30#include <utils/String8.h>
31#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070033#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070034#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070035#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080036
Michael Wright28f24d02016-07-12 13:30:53 -070037#include <system/graphics.h>
38
Huihong Luo3bdef862022-03-03 11:57:19 -080039#include <gui/AidlStatusUtil.h>
Robert Carr673134e2017-01-09 19:48:38 -080040#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070041#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070044#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080045#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080046#include <gui/SurfaceComposerClient.h>
chaviw98318de2021-05-19 16:45:23 -050047#include <gui/WindowInfo.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010048#include <private/gui/ParcelUtils.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010049#include <ui/DisplayMode.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080050#include <ui/DisplayState.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010051#include <ui/DynamicDisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian41f673c2011-11-17 17:48:35 -080053#include <private/gui/ComposerService.h>
Huihong Luof5029222021-12-16 14:33:46 -080054#include <private/gui/ComposerServiceAIDL.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Marissa Wall73411622019-01-25 10:45:41 -080056// This server size should always be smaller than the server cache size
57#define BUFFER_CACHE_MAX_SIZE 64
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070060
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050061using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
chaviw98318de2021-05-19 16:45:23 -050062using gui::FocusRequest;
Huihong Luoecc1f902021-11-20 11:55:05 -080063using gui::IRegionSamplingListener;
chaviw98318de2021-05-19 16:45:23 -050064using gui::WindowInfo;
65using gui::WindowInfoHandle;
chaviw60c9d3e2021-06-04 12:52:17 -050066using gui::WindowInfosListener;
Huihong Luo3bdef862022-03-03 11:57:19 -080067using gui::aidl_utils::statusTFromBinderStatus;
Peiyong Lin9f034472018-03-28 15:29:00 -070068using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069// ---------------------------------------------------------------------------
70
Mathias Agopian7e27f052010-05-28 14:22:23 -070071ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
Huihong Luof5029222021-12-16 14:33:46 -080072ANDROID_SINGLETON_STATIC_INSTANCE(ComposerServiceAIDL);
Mathias Agopian7e27f052010-05-28 14:22:23 -070073
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080074namespace {
75// Initialize transaction id counter used to generate transaction ids
76std::atomic<uint32_t> idCounter = 0;
77int64_t generateId() {
78 return (((int64_t)getpid()) << 32) | ++idCounter;
79}
80} // namespace
81
Mathias Agopianb7e930d2010-06-01 15:12:58 -070082ComposerService::ComposerService()
83: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070084 Mutex::Autolock _l(mLock);
85 connectLocked();
86}
87
Brian Lindahl9b85a192021-05-10 09:56:33 +000088bool ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070089 const String16 name("SurfaceFlinger");
Brian Lindahl9b85a192021-05-10 09:56:33 +000090 mComposerService = waitForService<ISurfaceComposer>(name);
91 if (mComposerService == nullptr) {
92 return false; // fatal error or permission problem
Mathias Agopianb7e930d2010-06-01 15:12:58 -070093 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070094
95 // Create the death listener.
96 class DeathObserver : public IBinder::DeathRecipient {
97 ComposerService& mComposerService;
98 virtual void binderDied(const wp<IBinder>& who) {
99 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
100 who.unsafe_get());
101 mComposerService.composerServiceDied();
102 }
103 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700104 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700105 };
106
107 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800108 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Brian Lindahl9b85a192021-05-10 09:56:33 +0000109 return true;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700110}
111
Andy McFadden6652b3e2012-09-06 18:45:56 -0700112/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
113 ComposerService& instance = ComposerService::getInstance();
114 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700115 if (instance.mComposerService == nullptr) {
Brian Lindahl9b85a192021-05-10 09:56:33 +0000116 if (ComposerService::getInstance().connectLocked()) {
117 ALOGD("ComposerService reconnected");
118 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700119 }
120 return instance.mComposerService;
121}
122
123void ComposerService::composerServiceDied()
124{
125 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700126 mComposerService = nullptr;
127 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700128}
129
Huihong Luof5029222021-12-16 14:33:46 -0800130ComposerServiceAIDL::ComposerServiceAIDL() : Singleton<ComposerServiceAIDL>() {
131 std::scoped_lock lock(mMutex);
132 connectLocked();
133}
134
135bool ComposerServiceAIDL::connectLocked() {
136 const String16 name("SurfaceFlingerAIDL");
137 mComposerService = waitForService<gui::ISurfaceComposer>(name);
138 if (mComposerService == nullptr) {
139 return false; // fatal error or permission problem
140 }
141
142 // Create the death listener.
143 class DeathObserver : public IBinder::DeathRecipient {
144 ComposerServiceAIDL& mComposerService;
145 virtual void binderDied(const wp<IBinder>& who) {
146 ALOGW("ComposerService aidl remote (surfaceflinger) died [%p]", who.unsafe_get());
147 mComposerService.composerServiceDied();
148 }
149
150 public:
151 explicit DeathObserver(ComposerServiceAIDL& mgr) : mComposerService(mgr) {}
152 };
153
154 mDeathObserver = new DeathObserver(*const_cast<ComposerServiceAIDL*>(this));
155 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
156 return true;
157}
158
159/*static*/ sp<gui::ISurfaceComposer> ComposerServiceAIDL::getComposerService() {
160 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
161 std::scoped_lock lock(instance.mMutex);
162 if (instance.mComposerService == nullptr) {
163 if (ComposerServiceAIDL::getInstance().connectLocked()) {
164 ALOGD("ComposerServiceAIDL reconnected");
Huihong Luo02186fb2022-02-23 14:21:54 -0800165 WindowInfosListenerReporter::getInstance()->reconnect(instance.mComposerService);
Huihong Luof5029222021-12-16 14:33:46 -0800166 }
167 }
168 return instance.mComposerService;
169}
170
171void ComposerServiceAIDL::composerServiceDied() {
172 std::scoped_lock lock(mMutex);
173 mComposerService = nullptr;
174 mDeathObserver = nullptr;
175}
176
Robert Carrfb4d58b2019-01-15 09:21:27 -0800177class DefaultComposerClient: public Singleton<DefaultComposerClient> {
178 Mutex mLock;
179 sp<SurfaceComposerClient> mClient;
180 friend class Singleton<ComposerService>;
181public:
182 static sp<SurfaceComposerClient> getComposerClient() {
183 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
184 Mutex::Autolock _l(dc.mLock);
185 if (dc.mClient == nullptr) {
186 dc.mClient = new SurfaceComposerClient;
187 }
188 return dc.mClient;
189 }
190};
191ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
192
193
194sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
195 return DefaultComposerClient::getComposerClient();
196}
197
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100198JankDataListener::~JankDataListener() {
199}
200
Mathias Agopian7e27f052010-05-28 14:22:23 -0700201// ---------------------------------------------------------------------------
202
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700203// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
204// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
205// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
206
Marissa Wallc837b5e2018-10-12 10:04:44 -0700207// 0 is an invalid callback id
208TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
209
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700210int64_t TransactionCompletedListener::getNextIdLocked() {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700211 return mCallbackIdCounter++;
212}
213
chaviw0b06a8d2021-08-06 11:49:08 -0500214sp<TransactionCompletedListener> TransactionCompletedListener::sInstance = nullptr;
Robert Carr2df7c452022-04-04 12:21:37 -0700215static std::mutex sListenerInstanceMutex;
chaviw0b06a8d2021-08-06 11:49:08 -0500216
217void TransactionCompletedListener::setInstance(const sp<TransactionCompletedListener>& listener) {
218 sInstance = listener;
219}
220
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700221sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
Robert Carr2df7c452022-04-04 12:21:37 -0700222 std::lock_guard<std::mutex> lock(sListenerInstanceMutex);
chaviw0b06a8d2021-08-06 11:49:08 -0500223 if (sInstance == nullptr) {
224 sInstance = new TransactionCompletedListener;
225 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700226 return sInstance;
227}
228
Marissa Wallc837b5e2018-10-12 10:04:44 -0700229sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
230 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
231}
232
233void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700234 if (mListening) {
235 return;
236 }
237 ProcessState::self()->startThreadPool();
238 mListening = true;
239}
240
Marissa Wall80d94ad2019-01-18 16:04:36 -0800241CallbackId TransactionCompletedListener::addCallbackFunction(
242 const TransactionCompletedCallback& callbackFunction,
243 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700244 surfaceControls,
245 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700246 std::lock_guard<std::mutex> lock(mMutex);
247 startListeningLocked();
248
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700249 CallbackId callbackId(getNextIdLocked(), callbackType);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800250 mCallbacks[callbackId].callbackFunction = callbackFunction;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800251 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
252
253 for (const auto& surfaceControl : surfaceControls) {
254 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
255 }
256
Marissa Wallc837b5e2018-10-12 10:04:44 -0700257 return callbackId;
258}
259
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100260void TransactionCompletedListener::addJankListener(const sp<JankDataListener>& listener,
261 sp<SurfaceControl> surfaceControl) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200262 std::lock_guard<std::mutex> lock(mMutex);
chaviw29bc5362021-09-07 16:17:13 -0500263 mJankListeners.insert({surfaceControl->getLayerId(), listener});
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100264}
265
266void TransactionCompletedListener::removeJankListener(const sp<JankDataListener>& listener) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200267 std::lock_guard<std::mutex> lock(mMutex);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100268 for (auto it = mJankListeners.begin(); it != mJankListeners.end();) {
269 if (it->second == listener) {
270 it = mJankListeners.erase(it);
271 } else {
272 it++;
273 }
274 }
275}
276
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700277void TransactionCompletedListener::setReleaseBufferCallback(const ReleaseCallbackId& callbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800278 ReleaseBufferCallback listener) {
279 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700280 mReleaseBufferCallbacks[callbackId] = listener;
Vishnu Nair1506b182021-02-22 14:35:15 -0800281}
282
Jorim Jaggif51775d2021-01-14 23:44:15 +0100283void TransactionCompletedListener::addSurfaceStatsListener(void* context, void* cookie,
284 sp<SurfaceControl> surfaceControl, SurfaceStatsCallback listener) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200285 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500286 mSurfaceStatsListeners.insert(
287 {surfaceControl->getLayerId(), SurfaceStatsCallbackEntry(context, cookie, listener)});
Jorim Jaggif51775d2021-01-14 23:44:15 +0100288}
289
290void TransactionCompletedListener::removeSurfaceStatsListener(void* context, void* cookie) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200291 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100292 for (auto it = mSurfaceStatsListeners.begin(); it != mSurfaceStatsListeners.end();) {
293 auto [itContext, itCookie, itListener] = it->second;
294 if (itContext == context && itCookie == cookie) {
295 it = mSurfaceStatsListeners.erase(it);
296 } else {
297 it++;
298 }
299 }
300}
301
Marissa Wall80d94ad2019-01-18 16:04:36 -0800302void TransactionCompletedListener::addSurfaceControlToCallbacks(
303 const sp<SurfaceControl>& surfaceControl,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700304 const std::unordered_set<CallbackId, CallbackIdHash>& callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800305 std::lock_guard<std::mutex> lock(mMutex);
306
307 for (auto callbackId : callbackIds) {
308 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
309 std::forward_as_tuple(
310 surfaceControl->getHandle()),
311 std::forward_as_tuple(surfaceControl));
312 }
313}
314
Marissa Walle2ffb422018-10-12 11:33:52 -0700315void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700316 std::unordered_map<CallbackId, CallbackTranslation, CallbackIdHash> callbacksMap;
chaviw29bc5362021-09-07 16:17:13 -0500317 std::multimap<int32_t, sp<JankDataListener>> jankListenersMap;
Valerie Haud3b90d22019-11-06 09:37:31 -0800318 {
319 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700320
Valerie Haud3b90d22019-11-06 09:37:31 -0800321 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
322 * callbackIds, except for when Transactions are merged together. This probably cannot be
323 * solved before this point because the Transactions could be merged together and applied in
324 * a different process.
325 *
326 * Fortunately, we get all the callbacks for this listener for the same frame together at
327 * the same time. This means if any Transactions were merged together, we will get their
328 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
329 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
330 * sp<SurfaceControl> that could possibly exist for the callbacks.
331 */
332 callbacksMap = mCallbacks;
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200333 jankListenersMap = mJankListeners;
Valerie Haud3b90d22019-11-06 09:37:31 -0800334 for (const auto& transactionStats : listenerStats.transactionStats) {
335 for (auto& callbackId : transactionStats.callbackIds) {
336 mCallbacks.erase(callbackId);
337 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800338 }
339 }
Marissa Walld600d572019-03-26 15:38:50 -0700340 for (const auto& transactionStats : listenerStats.transactionStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700341 // handle on commit callbacks
Marissa Walld600d572019-03-26 15:38:50 -0700342 for (auto callbackId : transactionStats.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700343 if (callbackId.type != CallbackId::Type::ON_COMMIT) {
344 continue;
345 }
346 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
347 if (!callbackFunction) {
348 ALOGE("cannot call null callback function, skipping");
349 continue;
350 }
351 std::vector<SurfaceControlStats> surfaceControlStats;
352 for (const auto& surfaceStats : transactionStats.surfaceStats) {
353 surfaceControlStats
354 .emplace_back(callbacksMap[callbackId]
355 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800356 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700357 transactionStats.presentFence,
358 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
359 surfaceStats.eventStats);
360 }
361
362 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
363 surfaceControlStats);
364 }
365
366 // handle on complete callbacks
367 for (auto callbackId : transactionStats.callbackIds) {
368 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
369 continue;
370 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800371 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-18 16:04:36 -0800372 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700373 ALOGE("cannot call null callback function, skipping");
374 continue;
375 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800376 std::vector<SurfaceControlStats> surfaceControlStats;
377 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800378 surfaceControlStats
379 .emplace_back(callbacksMap[callbackId]
380 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800381 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Valerie Hau871d6352020-01-29 08:44:02 -0800382 transactionStats.presentFence,
383 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
384 surfaceStats.eventStats);
Valerie Hau84d87ff2020-01-08 17:23:21 -0800385 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl]) {
386 callbacksMap[callbackId]
387 .surfaceControls[surfaceStats.surfaceControl]
388 ->setTransformHint(surfaceStats.transformHint);
389 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800390 // If there is buffer id set, we look up any pending client release buffer callbacks
391 // and call them. This is a performance optimization when we have a transaction
392 // callback and a release buffer callback happening at the same time to avoid an
393 // additional ipc call from the server.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700394 if (surfaceStats.previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
Vishnu Nair5faedf52021-03-22 11:32:58 -0700395 ReleaseBufferCallback callback;
396 {
397 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700398 callback = popReleaseBufferCallbackLocked(
399 surfaceStats.previousReleaseCallbackId);
Vishnu Nair5faedf52021-03-22 11:32:58 -0700400 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800401 if (callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700402 callback(surfaceStats.previousReleaseCallbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800403 surfaceStats.previousReleaseFence
404 ? surfaceStats.previousReleaseFence
Robert Carr97e7cc02021-06-07 10:45:40 -0700405 : Fence::NO_FENCE,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700406 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800407 }
408 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800409 }
410
411 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
412 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700413 }
chaviw29bc5362021-09-07 16:17:13 -0500414
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100415 for (const auto& surfaceStats : transactionStats.surfaceStats) {
chaviw29bc5362021-09-07 16:17:13 -0500416 // The callbackMap contains the SurfaceControl object, which we need to look up the
417 // layerId. Since we don't know which callback contains the SurfaceControl, iterate
418 // through all until the SC is found.
419 int32_t layerId = -1;
420 for (auto callbackId : transactionStats.callbackIds) {
Pablo Gamito10026472021-11-22 22:16:45 +0100421 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
422 // We only want to run the stats callback for ON_COMPLETE
423 continue;
424 }
chaviw29bc5362021-09-07 16:17:13 -0500425 sp<SurfaceControl> sc =
426 callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl];
427 if (sc != nullptr) {
428 layerId = sc->getLayerId();
429 break;
430 }
431 }
432
Pablo Gamito10026472021-11-22 22:16:45 +0100433 if (layerId != -1) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200434 // Acquire surface stats listener lock such that we guarantee that after calling
435 // unregister, there won't be any further callback.
436 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500437 auto listenerRange = mSurfaceStatsListeners.equal_range(layerId);
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200438 for (auto it = listenerRange.first; it != listenerRange.second; it++) {
439 auto entry = it->second;
440 entry.callback(entry.context, transactionStats.latchTime,
441 transactionStats.presentFence, surfaceStats);
442 }
Jorim Jaggif51775d2021-01-14 23:44:15 +0100443 }
444
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100445 if (surfaceStats.jankData.empty()) continue;
chaviw29bc5362021-09-07 16:17:13 -0500446 auto jankRange = jankListenersMap.equal_range(layerId);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100447 for (auto it = jankRange.first; it != jankRange.second; it++) {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100448 it->second->onJankDataAvailable(surfaceStats.jankData);
449 }
450 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700451 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700452}
453
Robert Carr4c1b6462021-12-21 10:30:50 -0800454void TransactionCompletedListener::onTransactionQueueStalled() {
455 std::unordered_map<void*, std::function<void()>> callbackCopy;
456 {
457 std::scoped_lock<std::mutex> lock(mMutex);
458 callbackCopy = mQueueStallListeners;
459 }
460 for (auto const& it : callbackCopy) {
461 it.second();
462 }
463}
464
465void TransactionCompletedListener::addQueueStallListener(std::function<void()> stallListener,
466 void* id) {
467 std::scoped_lock<std::mutex> lock(mMutex);
468 mQueueStallListeners[id] = stallListener;
469}
470void TransactionCompletedListener::removeQueueStallListener(void *id) {
471 std::scoped_lock<std::mutex> lock(mMutex);
472 mQueueStallListeners.erase(id);
473}
474
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700475void TransactionCompletedListener::onReleaseBuffer(ReleaseCallbackId callbackId,
chaviw69058fb2021-09-27 09:37:30 -0500476 sp<Fence> releaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700477 uint32_t currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800478 ReleaseBufferCallback callback;
479 {
480 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700481 callback = popReleaseBufferCallbackLocked(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800482 }
483 if (!callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700484 ALOGE("Could not call release buffer callback, buffer not found %s",
485 callbackId.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800486 return;
487 }
chaviw69058fb2021-09-27 09:37:30 -0500488 std::optional<uint32_t> optionalMaxAcquiredBufferCount =
489 currentMaxAcquiredBufferCount == UINT_MAX
490 ? std::nullopt
491 : std::make_optional<uint32_t>(currentMaxAcquiredBufferCount);
492 callback(callbackId, releaseFence, optionalMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800493}
494
495ReleaseBufferCallback TransactionCompletedListener::popReleaseBufferCallbackLocked(
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700496 const ReleaseCallbackId& callbackId) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800497 ReleaseBufferCallback callback;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700498 auto itr = mReleaseBufferCallbacks.find(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800499 if (itr == mReleaseBufferCallbacks.end()) {
500 return nullptr;
501 }
502 callback = itr->second;
503 mReleaseBufferCallbacks.erase(itr);
504 return callback;
505}
506
chaviw0acd33a2021-11-02 11:55:37 -0500507void TransactionCompletedListener::removeReleaseBufferCallback(
508 const ReleaseCallbackId& callbackId) {
509 {
510 std::scoped_lock<std::mutex> lock(mMutex);
511 popReleaseBufferCallbackLocked(callbackId);
512 }
513}
514
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700515// ---------------------------------------------------------------------------
516
Robert Carre06ad2b2020-04-10 15:09:33 -0700517void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 14:58:34 -0700518
Robert Carre06ad2b2020-04-10 15:09:33 -0700519/**
520 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
521 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
522 * 1. Cost of sending the FD
523 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
524 * To ease this cost we implement the following scheme of caching buffers to integers,
525 * or said-otherwise, naming them with integers. This is the scheme known as slots in
526 * the legacy BufferQueue system.
527 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
528 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
529 * send the cached integer.
530 * 3. If there is a cache miss, we cache the new buffer and send the integer
531 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
532 * entry, and we use the integer for further communication.
533 * A few details about lifetime:
534 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
535 * which is per process Unique. The server side cache is larger than the client side
536 * cache so that the server will never evict entries before the client.
537 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
538 * transaction.
539 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
540 * to auto-evict destroyed buffers.
541 */
Marissa Wall73411622019-01-25 10:45:41 -0800542class BufferCache : public Singleton<BufferCache> {
543public:
544 BufferCache() : token(new BBinder()) {}
545
546 sp<IBinder> getToken() {
547 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
548 }
549
Marissa Wall78b72202019-03-15 14:58:34 -0700550 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800551 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800552
Marissa Wall78b72202019-03-15 14:58:34 -0700553 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800554 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700555 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800556 }
Marissa Wall78b72202019-03-15 14:58:34 -0700557 itr->second = getCounter();
558 *cacheId = buffer->getId();
559 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800560 }
561
Marissa Wall78b72202019-03-15 14:58:34 -0700562 uint64_t cache(const sp<GraphicBuffer>& buffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800563 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800564
Marissa Wall78b72202019-03-15 14:58:34 -0700565 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
566 evictLeastRecentlyUsedBuffer();
567 }
Marissa Wall73411622019-01-25 10:45:41 -0800568
Robert Carre06ad2b2020-04-10 15:09:33 -0700569 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 14:58:34 -0700570
571 mBuffers[buffer->getId()] = getCounter();
572 return buffer->getId();
573 }
574
575 void uncache(uint64_t cacheId) {
576 std::lock_guard<std::mutex> lock(mMutex);
577 uncacheLocked(cacheId);
578 }
579
580 void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) {
581 mBuffers.erase(cacheId);
582 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
Marissa Wall73411622019-01-25 10:45:41 -0800583 }
584
585private:
Marissa Wall78b72202019-03-15 14:58:34 -0700586 void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800587 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700588 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800589 auto minBuffer = itr;
590 itr++;
591
592 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700593 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800594 if (counter < minCounter) {
595 minCounter = counter;
596 minBuffer = itr;
597 }
598 itr++;
599 }
Marissa Wall78b72202019-03-15 14:58:34 -0700600 uncacheLocked(minBuffer->first);
Marissa Wall73411622019-01-25 10:45:41 -0800601 }
602
603 uint64_t getCounter() REQUIRES(mMutex) {
604 static uint64_t counter = 0;
605 return counter++;
606 }
607
Marissa Wall73411622019-01-25 10:45:41 -0800608 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700609 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800610
611 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
612 sp<IBinder> token;
613};
614
615ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
616
Robert Carre06ad2b2020-04-10 15:09:33 -0700617void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 14:58:34 -0700618 // GraphicBuffer id's are used as the cache ids.
619 BufferCache::getInstance().uncache(graphicBufferId);
620}
621
Marissa Wall73411622019-01-25 10:45:41 -0800622// ---------------------------------------------------------------------------
623
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000624SurfaceComposerClient::Transaction::Transaction() {
625 mId = generateId();
626}
627
Marissa Wall17b4e452018-12-26 16:32:34 -0800628SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000629 : mId(other.mId),
Marissa Wall17b4e452018-12-26 16:32:34 -0800630 mTransactionNestCount(other.mTransactionNestCount),
631 mAnimation(other.mAnimation),
Ady Abraham8cbd3072021-03-15 16:39:06 -0700632 mEarlyWakeupStart(other.mEarlyWakeupStart),
633 mEarlyWakeupEnd(other.mEarlyWakeupEnd),
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000634 mMayContainBuffer(other.mMayContainBuffer),
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700635 mDesiredPresentTime(other.mDesiredPresentTime),
Ady Abrahamf0c56492020-12-17 18:04:15 -0800636 mIsAutoTimestamp(other.mIsAutoTimestamp),
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000637 mFrameTimelineInfo(other.mFrameTimelineInfo),
Patrick Williamse5991f52022-06-23 20:23:43 +0000638 mApplyToken(other.mApplyToken) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700639 mDisplayStates = other.mDisplayStates;
640 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800641 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700642 mListenerCallbacks = other.mListenerCallbacks;
643}
644
Robert Carrde6d7b42022-01-07 18:23:06 -0800645void SurfaceComposerClient::Transaction::sanitize() {
646 for (auto & [handle, composerState] : mComposerStates) {
647 composerState.state.sanitize(0 /* permissionMask */);
648 }
649 mInputWindowCommands.clear();
650}
651
Vishnu Nair621102e2019-06-12 14:16:57 -0700652std::unique_ptr<SurfaceComposerClient::Transaction>
653SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
654 auto transaction = std::make_unique<Transaction>();
655 if (transaction->readFromParcel(parcel) == NO_ERROR) {
656 return transaction;
657 }
658 return nullptr;
659}
660
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000661
Vishnu Nair621102e2019-06-12 14:16:57 -0700662status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
Pablo Gamito825df7f2022-06-23 14:45:44 +0000663 const uint64_t transactionId = parcel->readUint64();
Vishnu Nair621102e2019-06-12 14:16:57 -0700664 const uint32_t transactionNestCount = parcel->readUint32();
665 const bool animation = parcel->readBool();
Ady Abraham8cbd3072021-03-15 16:39:06 -0700666 const bool earlyWakeupStart = parcel->readBool();
667 const bool earlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 14:16:57 -0700668 const int64_t desiredPresentTime = parcel->readInt64();
Ady Abrahamf0c56492020-12-17 18:04:15 -0800669 const bool isAutoTimestamp = parcel->readBool();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000670 FrameTimelineInfo frameTimelineInfo;
Huihong Luo3bdef862022-03-03 11:57:19 -0800671 frameTimelineInfo.readFromParcel(parcel);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000672
Vishnu Nair277142c2021-01-05 18:35:29 -0800673 sp<IBinder> applyToken;
674 parcel->readNullableStrongBinder(&applyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700675 size_t count = static_cast<size_t>(parcel->readUint32());
676 if (count > parcel->dataSize()) {
677 return BAD_VALUE;
678 }
679 SortedVector<DisplayState> displayStates;
680 displayStates.setCapacity(count);
681 for (size_t i = 0; i < count; i++) {
682 DisplayState displayState;
683 if (displayState.read(*parcel) == BAD_VALUE) {
684 return BAD_VALUE;
685 }
686 displayStates.add(displayState);
687 }
688
689 count = static_cast<size_t>(parcel->readUint32());
690 if (count > parcel->dataSize()) {
691 return BAD_VALUE;
692 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700693 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
694 listenerCallbacks.reserve(count);
695 for (size_t i = 0; i < count; i++) {
696 sp<ITransactionCompletedListener> listener =
697 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
698 size_t numCallbackIds = parcel->readUint32();
699 if (numCallbackIds > parcel->dataSize()) {
700 return BAD_VALUE;
701 }
702 for (size_t j = 0; j < numCallbackIds; j++) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700703 CallbackId id;
704 parcel->readParcelable(&id);
705 listenerCallbacks[listener].callbackIds.insert(id);
Valerie Hau9dab9732019-08-20 09:29:25 -0700706 }
707 size_t numSurfaces = parcel->readUint32();
708 if (numSurfaces > parcel->dataSize()) {
709 return BAD_VALUE;
710 }
711 for (size_t j = 0; j < numSurfaces; j++) {
712 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000713 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 09:29:25 -0700714 listenerCallbacks[listener].surfaceControls.insert(surface);
715 }
716 }
717
718 count = static_cast<size_t>(parcel->readUint32());
719 if (count > parcel->dataSize()) {
720 return BAD_VALUE;
721 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700722 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 14:16:57 -0700723 composerStates.reserve(count);
724 for (size_t i = 0; i < count; i++) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000725 sp<IBinder> surfaceControlHandle;
726 SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700727
728 ComposerState composerState;
729 if (composerState.read(*parcel) == BAD_VALUE) {
730 return BAD_VALUE;
731 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700732 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 14:16:57 -0700733 }
734
735 InputWindowCommands inputWindowCommands;
736 inputWindowCommands.read(*parcel);
737
738 // Parsing was successful. Update the object.
Pablo Gamito825df7f2022-06-23 14:45:44 +0000739 mId = transactionId;
Vishnu Nair621102e2019-06-12 14:16:57 -0700740 mTransactionNestCount = transactionNestCount;
741 mAnimation = animation;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700742 mEarlyWakeupStart = earlyWakeupStart;
743 mEarlyWakeupEnd = earlyWakeupEnd;
Vishnu Nair621102e2019-06-12 14:16:57 -0700744 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800745 mIsAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000746 mFrameTimelineInfo = frameTimelineInfo;
Vishnu Nair621102e2019-06-12 14:16:57 -0700747 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 09:29:25 -0700748 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 14:16:57 -0700749 mComposerStates = composerStates;
750 mInputWindowCommands = inputWindowCommands;
Vishnu Nair277142c2021-01-05 18:35:29 -0800751 mApplyToken = applyToken;
Vishnu Nair621102e2019-06-12 14:16:57 -0700752 return NO_ERROR;
753}
754
755status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700756 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
757 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
758 // but is unlikely to use them again as they are owned by the other process.
759 // You may be asking yourself, is this const cast safe? Const cast is safe up
760 // until the point where you try and write to an object that was originally const at which
761 // point we enter undefined behavior. In this case we are safe though, because there are
762 // two possibilities:
763 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
764 // 2. It was originall const! In this case not only was it useless, but it by definition
765 // contains no composer states and so cacheBuffers will not perform any writes.
766
767 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
768
Pablo Gamito825df7f2022-06-23 14:45:44 +0000769 parcel->writeUint64(mId);
Vishnu Nair621102e2019-06-12 14:16:57 -0700770 parcel->writeUint32(mTransactionNestCount);
771 parcel->writeBool(mAnimation);
Ady Abraham8cbd3072021-03-15 16:39:06 -0700772 parcel->writeBool(mEarlyWakeupStart);
773 parcel->writeBool(mEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 14:16:57 -0700774 parcel->writeInt64(mDesiredPresentTime);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800775 parcel->writeBool(mIsAutoTimestamp);
Huihong Luo3bdef862022-03-03 11:57:19 -0800776 mFrameTimelineInfo.writeToParcel(parcel);
Vishnu Nair277142c2021-01-05 18:35:29 -0800777 parcel->writeStrongBinder(mApplyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700778 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
779 for (auto const& displayState : mDisplayStates) {
780 displayState.write(*parcel);
781 }
782
Valerie Hau9dab9732019-08-20 09:29:25 -0700783 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
784 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
785 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
786 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
787 for (auto callbackId : callbackInfo.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700788 parcel->writeParcelable(callbackId);
Valerie Hau9dab9732019-08-20 09:29:25 -0700789 }
790 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
791 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45 +0000792 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 09:29:25 -0700793 }
794 }
795
Vishnu Nair621102e2019-06-12 14:16:57 -0700796 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000797 for (auto const& [handle, composerState] : mComposerStates) {
798 SAFE_PARCEL(parcel->writeStrongBinder, handle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700799 composerState.write(*parcel);
800 }
801
802 mInputWindowCommands.write(*parcel);
803 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -0700804}
805
chaviw69058fb2021-09-27 09:37:30 -0500806void SurfaceComposerClient::Transaction::releaseBufferIfOverwriting(const layer_state_t& state) {
807 if (!(state.what & layer_state_t::eBufferChanged)) {
808 return;
809 }
810
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800811 auto listener = state.bufferData->releaseBufferListener;
chaviw69058fb2021-09-27 09:37:30 -0500812 sp<Fence> fence =
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800813 state.bufferData->acquireFence ? state.bufferData->acquireFence : Fence::NO_FENCE;
814 if (state.bufferData->releaseBufferEndpoint ==
chaviw69058fb2021-09-27 09:37:30 -0500815 IInterface::asBinder(TransactionCompletedListener::getIInstance())) {
816 // if the callback is in process, run on a different thread to avoid any lock contigency
817 // issues in the client.
818 SurfaceComposerClient::getDefault()
chaviw8dd181f2022-01-05 18:36:46 -0600819 ->mReleaseCallbackThread
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800820 .addReleaseCallback(state.bufferData->generateReleaseCallbackId(), fence);
chaviw69058fb2021-09-27 09:37:30 -0500821 } else {
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800822 listener->onReleaseBuffer(state.bufferData->generateReleaseCallbackId(), fence, UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -0500823 }
824}
825
Robert Carr2c5f6d22017-09-26 12:30:35 -0700826SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000827 for (auto const& [handle, composerState] : other.mComposerStates) {
828 if (mComposerStates.count(handle) == 0) {
829 mComposerStates[handle] = composerState;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700830 } else {
chaviw69058fb2021-09-27 09:37:30 -0500831 if (composerState.state.what & layer_state_t::eBufferChanged) {
832 releaseBufferIfOverwriting(mComposerStates[handle].state);
833 }
Pablo Gamitodbc31672020-09-01 18:28:58 +0000834 mComposerStates[handle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700835 }
836 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700837
838 for (auto const& state : other.mDisplayStates) {
839 ssize_t index = mDisplayStates.indexOf(state);
840 if (index < 0) {
841 mDisplayStates.add(state);
842 } else {
843 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
844 }
845 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700846
Marissa Wallc837b5e2018-10-12 10:04:44 -0700847 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
848 auto& [callbackIds, surfaceControls] = callbackInfo;
849 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
850 callbackIds.begin()),
851 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -0700852
Valerie Hau236eba32020-01-03 16:53:39 -0800853 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
854 surfaceControls.end());
855
856 auto& currentProcessCallbackInfo =
857 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
858 currentProcessCallbackInfo.surfaceControls
859 .insert(std::make_move_iterator(surfaceControls.begin()),
860 std::make_move_iterator(surfaceControls.end()));
861
862 // register all surface controls for all callbackIds for this listener that is merging
863 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
864 TransactionCompletedListener::getInstance()
865 ->addSurfaceControlToCallbacks(surfaceControl,
866 currentProcessCallbackInfo.callbackIds);
867 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700868 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700869
chaviw273171b2018-12-26 11:46:30 -0800870 mInputWindowCommands.merge(other.mInputWindowCommands);
871
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000872 mMayContainBuffer |= other.mMayContainBuffer;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700873 mEarlyWakeupStart = mEarlyWakeupStart || other.mEarlyWakeupStart;
874 mEarlyWakeupEnd = mEarlyWakeupEnd || other.mEarlyWakeupEnd;
Vishnu Nair277142c2021-01-05 18:35:29 -0800875 mApplyToken = other.mApplyToken;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700876
Huihong Luo3bdef862022-03-03 11:57:19 -0800877 mergeFrameTimelineInfo(mFrameTimelineInfo, other.mFrameTimelineInfo);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700878
Vishnu Nairfef244e2019-06-17 18:07:51 -0700879 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -0700880 return *this;
881}
882
Vishnu Nairfef244e2019-06-17 18:07:51 -0700883void SurfaceComposerClient::Transaction::clear() {
884 mComposerStates.clear();
885 mDisplayStates.clear();
886 mListenerCallbacks.clear();
887 mInputWindowCommands.clear();
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000888 mMayContainBuffer = false;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700889 mTransactionNestCount = 0;
890 mAnimation = false;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700891 mEarlyWakeupStart = false;
892 mEarlyWakeupEnd = false;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800893 mDesiredPresentTime = 0;
894 mIsAutoTimestamp = true;
Huihong Luo3bdef862022-03-03 11:57:19 -0800895 clearFrameTimelineInfo(mFrameTimelineInfo);
Vishnu Nair277142c2021-01-05 18:35:29 -0800896 mApplyToken = nullptr;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700897}
898
Pablo Gamito3e4e0be2022-05-27 17:26:24 +0000899uint64_t SurfaceComposerClient::Transaction::getId() {
900 return mId;
901}
902
Marissa Wall78b72202019-03-15 14:58:34 -0700903void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
904 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
905
Marissa Wall947d34e2019-03-29 14:03:53 -0700906 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -0700907 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700908 uncacheBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700909
Vishnu Nairf7204112022-09-06 19:11:26 +0000910 sf->setTransactionState(FrameTimelineInfo{}, {}, {}, 0, Transaction::getDefaultApplyToken(), {},
911 systemTime(), true, uncacheBuffer, false, {}, generateId());
Marissa Wall78b72202019-03-15 14:58:34 -0700912}
913
914void SurfaceComposerClient::Transaction::cacheBuffers() {
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000915 if (!mMayContainBuffer) {
Marissa Wall78b72202019-03-15 14:58:34 -0700916 return;
917 }
918
919 size_t count = 0;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700920 for (auto& [handle, cs] : mComposerStates) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000921 layer_state_t* s = &(mComposerStates[handle].state);
Marissa Wall78b72202019-03-15 14:58:34 -0700922 if (!(s->what & layer_state_t::eBufferChanged)) {
923 continue;
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800924 } else if (s->bufferData &&
925 s->bufferData->flags.test(BufferData::BufferDataChange::cachedBufferChanged)) {
Robert Carr28037922020-04-08 10:57:07 -0700926 // If eBufferChanged and eCachedBufferChanged are both trued then that means
927 // we already cached the buffer in a previous call to cacheBuffers, perhaps
928 // from writeToParcel on a Transaction that was merged in to this one.
929 continue;
Marissa Wall78b72202019-03-15 14:58:34 -0700930 }
931
Marissa Wall00597242019-03-27 10:35:19 -0700932 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
933 // time trying to cache them.
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800934 if (!s->bufferData || !s->bufferData->buffer) {
Marissa Wall00597242019-03-27 10:35:19 -0700935 continue;
936 }
937
Marissa Wall78b72202019-03-15 14:58:34 -0700938 uint64_t cacheId = 0;
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800939 status_t ret = BufferCache::getInstance().getCacheId(s->bufferData->buffer, &cacheId);
Marissa Wall78b72202019-03-15 14:58:34 -0700940 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -0700941 // Cache-hit. Strip the buffer and send only the id.
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800942 s->bufferData->buffer = nullptr;
Marissa Wall78b72202019-03-15 14:58:34 -0700943 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -0700944 // Cache-miss. Include the buffer and send the new cacheId.
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800945 cacheId = BufferCache::getInstance().cache(s->bufferData->buffer);
Marissa Wall78b72202019-03-15 14:58:34 -0700946 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800947 s->bufferData->flags |= BufferData::BufferDataChange::cachedBufferChanged;
948 s->bufferData->cachedBuffer.token = BufferCache::getInstance().getToken();
949 s->bufferData->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700950
951 // If we have more buffers than the size of the cache, we should stop caching so we don't
952 // evict other buffers in this transaction
953 count++;
954 if (count >= BUFFER_CACHE_MAX_SIZE) {
955 break;
956 }
957 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800958}
959
Robert Carr79dc06a2022-02-22 15:28:59 -0800960status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700961 if (mStatus != NO_ERROR) {
962 return mStatus;
963 }
964
965 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
966
Valerie Hau9dab9732019-08-20 09:29:25 -0700967 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 14:03:19 -0700968 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700969 // For every listener with registered callbacks
970 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
971 auto& [callbackIds, surfaceControls] = callbackInfo;
972 if (callbackIds.empty()) {
973 continue;
974 }
975
Valerie Hau9dab9732019-08-20 09:29:25 -0700976 if (surfaceControls.empty()) {
977 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
978 } else {
979 // If the listener has any SurfaceControls set on this Transaction update the surface
980 // state
981 for (const auto& surfaceControl : surfaceControls) {
982 layer_state_t* s = getLayerState(surfaceControl);
983 if (!s) {
984 ALOGE("failed to get layer state");
985 continue;
986 }
987 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
988 s->what |= layer_state_t::eHasListenerCallbacksChanged;
989 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700990 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700991 }
992 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700993
Marissa Wall78b72202019-03-15 14:58:34 -0700994 cacheBuffers();
995
Robert Carr4cdc58f2017-08-23 14:22:20 -0700996 Vector<ComposerState> composerStates;
997 Vector<DisplayState> displayStates;
998 uint32_t flags = 0;
999
chaviw8e3fe5d2018-02-22 10:55:42 -08001000 for (auto const& kv : mComposerStates){
1001 composerStates.add(kv.second);
1002 }
1003
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001004 displayStates = std::move(mDisplayStates);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001005
Vishnu Nair2709efe2022-09-28 17:17:08 -07001006 if (synchronous) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001007 flags |= ISurfaceComposer::eSynchronous;
1008 }
1009 if (mAnimation) {
1010 flags |= ISurfaceComposer::eAnimation;
1011 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001012 if (oneWay) {
Vishnu Nair2709efe2022-09-28 17:17:08 -07001013 if (synchronous) {
1014 ALOGE("Transaction attempted to set synchronous and one way at the same time"
1015 " this is an invalid request. Synchronous will win for safety");
1016 } else {
1017 flags |= ISurfaceComposer::eOneWay;
1018 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001019 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001020
Ady Abraham8cbd3072021-03-15 16:39:06 -07001021 // If both mEarlyWakeupStart and mEarlyWakeupEnd are set
Ady Abrahambf1349c2020-06-12 14:26:18 -07001022 // it is equivalent for none
Ady Abraham8cbd3072021-03-15 16:39:06 -07001023 if (mEarlyWakeupStart && !mEarlyWakeupEnd) {
1024 flags |= ISurfaceComposer::eEarlyWakeupStart;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001025 }
Ady Abraham8cbd3072021-03-15 16:39:06 -07001026 if (mEarlyWakeupEnd && !mEarlyWakeupStart) {
1027 flags |= ISurfaceComposer::eEarlyWakeupEnd;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001028 }
1029
Vishnu Nair80da86b2022-08-11 16:32:58 -07001030 sp<IBinder> applyToken = mApplyToken ? mApplyToken : sApplyToken;
Vishnu Nair277142c2021-01-05 18:35:29 -08001031
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001032 sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
Ady Abrahamf0c56492020-12-17 18:04:15 -08001033 mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
Marissa Wall3dad52d2019-03-22 14:03:19 -07001034 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001035 hasListenerCallbacks, listenerCallbacks, mId);
1036 mId = generateId();
1037
1038 // Clear the current states and flags
1039 clear();
1040
Robert Carr4cdc58f2017-08-23 14:22:20 -07001041 mStatus = NO_ERROR;
1042 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001043}
1044
Vishnu Nair80da86b2022-08-11 16:32:58 -07001045sp<IBinder> SurfaceComposerClient::Transaction::sApplyToken = new BBinder();
1046
1047sp<IBinder> SurfaceComposerClient::Transaction::getDefaultApplyToken() {
1048 return sApplyToken;
1049}
1050
1051void SurfaceComposerClient::Transaction::setDefaultApplyToken(sp<IBinder> applyToken) {
1052 sApplyToken = applyToken;
1053}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054// ---------------------------------------------------------------------------
1055
Robert Carr4cdc58f2017-08-23 14:22:20 -07001056sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Huihong Luo07e72362022-02-14 14:26:04 -08001057 sp<IBinder> display = nullptr;
1058 binder::Status status =
1059 ComposerServiceAIDL::getComposerService()->createDisplay(std::string(
1060 displayName.string()),
1061 secure, &display);
1062 return status.isOk() ? display : nullptr;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001063}
1064
Robert Carr4cdc58f2017-08-23 14:22:20 -07001065void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Huihong Luo07e72362022-02-14 14:26:04 -08001066 ComposerServiceAIDL::getComposerService()->destroyDisplay(display);
Jesse Hall6c913be2013-08-08 12:15:49 -07001067}
1068
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001069std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
Huihong Luo07e72362022-02-14 14:26:04 -08001070 std::vector<int64_t> displayIds;
1071 std::vector<PhysicalDisplayId> physicalDisplayIds;
1072 binder::Status status =
1073 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayIds(&displayIds);
1074 if (status.isOk()) {
1075 physicalDisplayIds.reserve(displayIds.size());
1076 for (auto item : displayIds) {
1077 auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(item));
1078 physicalDisplayIds.push_back(*id);
1079 }
1080 }
1081 return physicalDisplayIds;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001082}
1083
1084std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
Huihong Luo07e72362022-02-14 14:26:04 -08001085 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
1086 return instance.getInternalDisplayId();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001087}
1088
1089sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
Huihong Luo07e72362022-02-14 14:26:04 -08001090 sp<IBinder> display = nullptr;
1091 binder::Status status =
1092 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayToken(displayId.value,
1093 &display);
1094 return status.isOk() ? display : nullptr;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001095}
1096
1097sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
Huihong Luo07e72362022-02-14 14:26:04 -08001098 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
1099 return instance.getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001100}
1101
Robert Carr4cdc58f2017-08-23 14:22:20 -07001102void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001103 mAnimation = true;
1104}
1105
Ady Abraham8cbd3072021-03-15 16:39:06 -07001106void SurfaceComposerClient::Transaction::setEarlyWakeupStart() {
1107 mEarlyWakeupStart = true;
Dan Stoza84d619e2018-03-28 17:07:36 -07001108}
1109
Ady Abraham8cbd3072021-03-15 16:39:06 -07001110void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() {
1111 mEarlyWakeupEnd = true;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001112}
1113
Pablo Gamitodbc31672020-09-01 18:28:58 +00001114layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
Robert Carr5b3b9142021-02-22 12:27:32 -08001115 auto handle = sc->getLayerStateHandle();
Pablo Gamitodbc31672020-09-01 18:28:58 +00001116
Vishnu Nairf03652d2019-07-16 17:56:56 -07001117 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001118 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -08001119 ComposerState s;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001120
Vishnu Nairf03652d2019-07-16 17:56:56 -07001121 s.state.surface = handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001122 s.state.layerId = sc->getLayerId();
1123
Vishnu Nairf03652d2019-07-16 17:56:56 -07001124 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -07001125 }
1126
Vishnu Nairf03652d2019-07-16 17:56:56 -07001127 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001128}
1129
Marissa Wallc837b5e2018-10-12 10:04:44 -07001130void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
1131 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08001132 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
1133 callbackInfo.surfaceControls.insert(sc);
1134
1135 TransactionCompletedListener::getInstance()
1136 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001137}
1138
Robert Carr4cdc58f2017-08-23 14:22:20 -07001139SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
1140 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -08001141 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001142 if (!s) {
1143 mStatus = BAD_INDEX;
1144 return *this;
1145 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001146 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001147 s->x = x;
1148 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001149
1150 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001151 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001152}
1153
Robert Carr4cdc58f2017-08-23 14:22:20 -07001154SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
1155 const sp<SurfaceControl>& sc) {
1156 return setFlags(sc, 0, layer_state_t::eLayerHidden);
1157}
1158
1159SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
1160 const sp<SurfaceControl>& sc) {
1161 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
1162}
1163
Robert Carr4cdc58f2017-08-23 14:22:20 -07001164SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
1165 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001166 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001167 if (!s) {
1168 mStatus = BAD_INDEX;
1169 return *this;
1170 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001171 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001172 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001173 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001174
1175 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001176 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001177}
1178
Pablo Gamito11dcc222020-09-12 15:49:39 +00001179SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
1180 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001181 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -07001182 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001183 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -07001184 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001185 }
1186 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001187 s->what &= ~layer_state_t::eLayerChanged;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001188 s->relativeLayerSurfaceControl = relativeTo;
Robert Carrdb66e622017-04-10 16:55:57 -07001189 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001190
1191 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001192 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001193}
1194
Robert Carr4cdc58f2017-08-23 14:22:20 -07001195SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
1196 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -07001197 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -08001198 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001199 if (!s) {
1200 mStatus = BAD_INDEX;
1201 return *this;
1202 }
chaviwc5676c62020-09-18 15:01:04 -07001203 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001204 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot) ||
Leon Scroggins9a20f722021-12-28 14:43:12 +00001205 (mask & layer_state_t::eEnableBackpressure) ||
1206 (mask & layer_state_t::eLayerIsDisplayDecoration)) {
Dan Stoza23116082015-06-18 14:58:39 -07001207 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -08001208 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001209 s->flags &= ~mask;
1210 s->flags |= (flags & mask);
1211 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001212
1213 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001214 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001215}
1216
Robert Carr4cdc58f2017-08-23 14:22:20 -07001217SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
1218 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -07001219 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -08001220 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001221 if (!s) {
1222 mStatus = BAD_INDEX;
1223 return *this;
1224 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001225 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001226 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001227
1228 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001229 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001230}
1231
Sally Qi421ffb02022-03-21 19:41:33 -07001232SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDimmingEnabled(
1233 const sp<SurfaceControl>& sc, bool dimmingEnabled) {
1234 layer_state_t* s = getLayerState(sc);
1235 if (!s) {
1236 mStatus = BAD_INDEX;
1237 return *this;
1238 }
1239 s->what |= layer_state_t::eDimmingEnabledChanged;
1240 s->dimmingEnabled = dimmingEnabled;
1241
1242 registerSurfaceControlForCallback(sc);
1243 return *this;
1244}
1245
Robert Carr4cdc58f2017-08-23 14:22:20 -07001246SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
1247 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -08001248 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001249 if (!s) {
1250 mStatus = BAD_INDEX;
1251 return *this;
1252 }
Ady Abrahamff930b42022-08-05 19:04:16 +00001253 if (alpha < 0.0f || alpha > 1.0f) {
Ady Abraham24d0b2a2022-08-11 16:27:34 -07001254 ALOGE("SurfaceComposerClient::Transaction::setAlpha: invalid alpha %f, clamping", alpha);
Ady Abrahamff930b42022-08-05 19:04:16 +00001255 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001256 s->what |= layer_state_t::eAlphaChanged;
Ady Abraham24d0b2a2022-08-11 16:27:34 -07001257 s->alpha = std::clamp(alpha, 0.f, 1.f);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001258
1259 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001260 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001261}
1262
Robert Carr4cdc58f2017-08-23 14:22:20 -07001263SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001264 const sp<SurfaceControl>& sc, ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001265 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001266 if (!s) {
1267 mStatus = BAD_INDEX;
1268 return *this;
1269 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001270 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -07001271 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001272
1273 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001274 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -07001275}
1276
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001277SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -07001278 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001279 layer_state_t* s = getLayerState(sc);
1280 if (!s) {
1281 mStatus = BAD_INDEX;
1282 return *this;
1283 }
1284 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -07001285
1286 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001287
1288 registerSurfaceControlForCallback(sc);
1289 return *this;
1290}
1291
Robert Carr4cdc58f2017-08-23 14:22:20 -07001292SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
1293 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -08001294 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -08001295 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001296 if (!s) {
1297 mStatus = BAD_INDEX;
1298 return *this;
1299 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001300 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001301 layer_state_t::matrix22_t matrix;
1302 matrix.dsdx = dsdx;
1303 matrix.dtdx = dtdx;
1304 matrix.dsdy = dsdy;
1305 matrix.dtdy = dtdy;
1306 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001307
1308 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001309 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001310}
1311
chaviw25714502021-02-11 10:01:08 -08001312SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
Robert Carr4cdc58f2017-08-23 14:22:20 -07001313 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -08001314 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001315 if (!s) {
1316 mStatus = BAD_INDEX;
1317 return *this;
1318 }
chaviw25714502021-02-11 10:01:08 -08001319 s->what |= layer_state_t::eCropChanged;
1320 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001321
1322 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001323 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001324}
1325
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001326SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
1327 const sp<SurfaceControl>& sc, float cornerRadius) {
1328 layer_state_t* s = getLayerState(sc);
1329 if (!s) {
1330 mStatus = BAD_INDEX;
1331 return *this;
1332 }
1333 s->what |= layer_state_t::eCornerRadiusChanged;
1334 s->cornerRadius = cornerRadius;
1335 return *this;
1336}
1337
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001338SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1339 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1340 layer_state_t* s = getLayerState(sc);
1341 if (!s) {
1342 mStatus = BAD_INDEX;
1343 return *this;
1344 }
1345 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1346 s->backgroundBlurRadius = backgroundBlurRadius;
1347 return *this;
1348}
1349
Lucas Dupinc3800b82020-10-02 16:24:48 -07001350SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBlurRegions(
1351 const sp<SurfaceControl>& sc, const std::vector<BlurRegion>& blurRegions) {
1352 layer_state_t* s = getLayerState(sc);
1353 if (!s) {
1354 mStatus = BAD_INDEX;
1355 return *this;
1356 }
1357 s->what |= layer_state_t::eBlurRegionsChanged;
1358 s->blurRegions = blurRegions;
1359 return *this;
1360}
1361
Robert Carr4cdc58f2017-08-23 14:22:20 -07001362SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001363 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001364 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001365 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001366 mStatus = BAD_INDEX;
1367 return *this;
chaviw06178942017-07-27 10:25:59 -07001368 }
Robert Carr5b3b9142021-02-22 12:27:32 -08001369 if (SurfaceControl::isSameSurface(sc, newParent)) {
1370 return *this;
1371 }
chaviwf1961f72017-09-18 16:41:07 -07001372 s->what |= layer_state_t::eReparent;
Robert Carr5b3b9142021-02-22 12:27:32 -08001373 s->parentSurfaceControlForChild = newParent ? newParent->getParentingLayer() : nullptr;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001374
1375 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001376 return *this;
chaviw06178942017-07-27 10:25:59 -07001377}
1378
Robert Carr4cdc58f2017-08-23 14:22:20 -07001379SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1380 const sp<SurfaceControl>& sc,
1381 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001382 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001383 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001384 mStatus = BAD_INDEX;
1385 return *this;
1386 }
1387 s->what |= layer_state_t::eColorChanged;
1388 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001389
1390 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001391 return *this;
1392}
1393
Valerie Haudd0b7572019-01-29 14:59:27 -08001394SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1395 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001396 layer_state_t* s = getLayerState(sc);
1397 if (!s) {
1398 mStatus = BAD_INDEX;
1399 return *this;
1400 }
1401
Valerie Haudd0b7572019-01-29 14:59:27 -08001402 s->what |= layer_state_t::eBackgroundColorChanged;
1403 s->color = color;
1404 s->bgColorAlpha = alpha;
1405 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001406
1407 registerSurfaceControlForCallback(sc);
1408 return *this;
1409}
1410
Marissa Wall61c58622018-07-18 10:12:20 -07001411SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1412 const sp<SurfaceControl>& sc, uint32_t transform) {
1413 layer_state_t* s = getLayerState(sc);
1414 if (!s) {
1415 mStatus = BAD_INDEX;
1416 return *this;
1417 }
1418 s->what |= layer_state_t::eTransformChanged;
1419 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001420
1421 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001422 return *this;
1423}
1424
1425SurfaceComposerClient::Transaction&
1426SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1427 bool transformToDisplayInverse) {
1428 layer_state_t* s = getLayerState(sc);
1429 if (!s) {
1430 mStatus = BAD_INDEX;
1431 return *this;
1432 }
1433 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1434 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001435
1436 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001437 return *this;
1438}
1439
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001440std::shared_ptr<BufferData> SurfaceComposerClient::Transaction::getAndClearBuffer(
chaviw0acd33a2021-11-02 11:55:37 -05001441 const sp<SurfaceControl>& sc) {
1442 layer_state_t* s = getLayerState(sc);
1443 if (!s) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001444 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001445 }
1446 if (!(s->what & layer_state_t::eBufferChanged)) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001447 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001448 }
1449
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001450 std::shared_ptr<BufferData> bufferData = std::move(s->bufferData);
chaviw0acd33a2021-11-02 11:55:37 -05001451
1452 TransactionCompletedListener::getInstance()->removeReleaseBufferCallback(
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001453 bufferData->generateReleaseCallbackId());
chaviw0acd33a2021-11-02 11:55:37 -05001454 s->what &= ~layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001455 s->bufferData = nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001456
chaviw0acd33a2021-11-02 11:55:37 -05001457 return bufferData;
1458}
1459
Robert Carr79dc06a2022-02-22 15:28:59 -08001460SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferHasBarrier(
1461 const sp<SurfaceControl>& sc, uint64_t barrierFrameNumber) {
1462 layer_state_t* s = getLayerState(sc);
1463 if (!s) {
1464 mStatus = BAD_INDEX;
1465 return *this;
1466 }
1467 s->bufferData->hasBarrier = true;
1468 s->bufferData->barrierFrameNumber = barrierFrameNumber;
1469 return *this;
1470}
1471
Marissa Wall61c58622018-07-18 10:12:20 -07001472SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
chaviwba4320c2021-09-15 15:20:53 -05001473 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer,
John Reck57f748c2022-03-17 17:23:49 -04001474 const std::optional<sp<Fence>>& fence, const std::optional<uint64_t>& optFrameNumber,
chaviw8dd181f2022-01-05 18:36:46 -06001475 ReleaseBufferCallback callback) {
Marissa Wall61c58622018-07-18 10:12:20 -07001476 layer_state_t* s = getLayerState(sc);
1477 if (!s) {
1478 mStatus = BAD_INDEX;
1479 return *this;
1480 }
chaviw69058fb2021-09-27 09:37:30 -05001481
1482 releaseBufferIfOverwriting(*s);
1483
chaviwd1bda4b2022-05-19 17:44:46 -05001484 if (buffer == nullptr) {
1485 s->what &= ~layer_state_t::eBufferChanged;
1486 s->bufferData = nullptr;
chaviwd1bda4b2022-05-19 17:44:46 -05001487 return *this;
1488 }
1489
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001490 std::shared_ptr<BufferData> bufferData = std::make_shared<BufferData>();
1491 bufferData->buffer = buffer;
John Reck57f748c2022-03-17 17:23:49 -04001492 uint64_t frameNumber = sc->resolveFrameNumber(optFrameNumber);
1493 bufferData->frameNumber = frameNumber;
1494 bufferData->flags |= BufferData::BufferDataChange::frameNumberChanged;
chaviwba4320c2021-09-15 15:20:53 -05001495 if (fence) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001496 bufferData->acquireFence = *fence;
1497 bufferData->flags |= BufferData::BufferDataChange::fenceChanged;
chaviwba4320c2021-09-15 15:20:53 -05001498 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001499 bufferData->releaseBufferEndpoint =
chaviwba4320c2021-09-15 15:20:53 -05001500 IInterface::asBinder(TransactionCompletedListener::getIInstance());
Ady Abrahamf0c56492020-12-17 18:04:15 -08001501 if (mIsAutoTimestamp) {
1502 mDesiredPresentTime = systemTime();
1503 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001504 setReleaseBufferCallback(bufferData.get(), callback);
chaviwba4320c2021-09-15 15:20:53 -05001505 s->what |= layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001506 s->bufferData = std::move(bufferData);
Marissa Wallebc2c052019-01-16 19:16:55 -08001507 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001508
chaviw1a227f02022-04-25 16:23:44 -05001509 // With the current infrastructure, a release callback will not be invoked if there's no
1510 // transaction callback in the case when a buffer is latched and not released early. This is
1511 // because the legacy implementation didn't have a release callback and sent releases in the
1512 // transaction callback. Because of this, we need to make sure to have a transaction callback
1513 // set up when a buffer is sent in a transaction to ensure the caller gets the release
1514 // callback, regardless if they set up a transaction callback.
1515 //
1516 // TODO (b/230380821): Remove when release callbacks are separated from transaction callbacks
1517 addTransactionCompletedCallback([](void*, nsecs_t, const sp<Fence>&,
1518 const std::vector<SurfaceControlStats>&) {},
1519 nullptr);
1520
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001521 mMayContainBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001522 return *this;
1523}
1524
chaviwba4320c2021-09-15 15:20:53 -05001525void SurfaceComposerClient::Transaction::setReleaseBufferCallback(BufferData* bufferData,
Vishnu Nair1506b182021-02-22 14:35:15 -08001526 ReleaseBufferCallback callback) {
1527 if (!callback) {
1528 return;
1529 }
1530
chaviwba4320c2021-09-15 15:20:53 -05001531 if (!bufferData->buffer) {
Vishnu Nair1506b182021-02-22 14:35:15 -08001532 ALOGW("Transaction::setReleaseBufferCallback"
1533 "ignored trying to set a callback on a null buffer.");
1534 return;
1535 }
1536
chaviwba4320c2021-09-15 15:20:53 -05001537 bufferData->releaseBufferListener = TransactionCompletedListener::getIInstance();
Vishnu Nair1506b182021-02-22 14:35:15 -08001538 auto listener = TransactionCompletedListener::getInstance();
chaviw8dd181f2022-01-05 18:36:46 -06001539 listener->setReleaseBufferCallback(bufferData->generateReleaseCallbackId(), callback);
Vishnu Nair1506b182021-02-22 14:35:15 -08001540}
1541
Marissa Wall61c58622018-07-18 10:12:20 -07001542SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1543 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1544 layer_state_t* s = getLayerState(sc);
1545 if (!s) {
1546 mStatus = BAD_INDEX;
1547 return *this;
1548 }
1549 s->what |= layer_state_t::eDataspaceChanged;
1550 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001551
1552 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001553 return *this;
1554}
1555
1556SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1557 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1558 layer_state_t* s = getLayerState(sc);
1559 if (!s) {
1560 mStatus = BAD_INDEX;
1561 return *this;
1562 }
1563 s->what |= layer_state_t::eHdrMetadataChanged;
1564 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001565
1566 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001567 return *this;
1568}
1569
1570SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1571 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1572 layer_state_t* s = getLayerState(sc);
1573 if (!s) {
1574 mStatus = BAD_INDEX;
1575 return *this;
1576 }
1577 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1578 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001579
1580 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001581 return *this;
1582}
1583
1584SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1585 const sp<SurfaceControl>& sc, int32_t api) {
1586 layer_state_t* s = getLayerState(sc);
1587 if (!s) {
1588 mStatus = BAD_INDEX;
1589 return *this;
1590 }
1591 s->what |= layer_state_t::eApiChanged;
1592 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001593
1594 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001595 return *this;
1596}
1597
1598SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1599 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1600 layer_state_t* s = getLayerState(sc);
1601 if (!s) {
1602 mStatus = BAD_INDEX;
1603 return *this;
1604 }
1605 s->what |= layer_state_t::eSidebandStreamChanged;
1606 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001607
1608 registerSurfaceControlForCallback(sc);
1609 return *this;
1610}
1611
Marissa Wall17b4e452018-12-26 16:32:34 -08001612SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1613 nsecs_t desiredPresentTime) {
1614 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -08001615 mIsAutoTimestamp = false;
Marissa Wall17b4e452018-12-26 16:32:34 -08001616 return *this;
1617}
1618
Peiyong Linc502cb72019-03-01 15:00:23 -08001619SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1620 const sp<SurfaceControl>& sc, const bool agnostic) {
1621 layer_state_t* s = getLayerState(sc);
1622 if (!s) {
1623 mStatus = BAD_INDEX;
1624 return *this;
1625 }
1626 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1627 s->colorSpaceAgnostic = agnostic;
1628
1629 registerSurfaceControlForCallback(sc);
1630 return *this;
1631}
1632
Marissa Wallc837b5e2018-10-12 10:04:44 -07001633SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001634SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1635 int32_t priority) {
1636 layer_state_t* s = getLayerState(sc);
1637 if (!s) {
1638 mStatus = BAD_INDEX;
1639 return *this;
1640 }
1641
1642 s->what |= layer_state_t::eFrameRateSelectionPriority;
1643 s->frameRateSelectionPriority = priority;
1644
1645 registerSurfaceControlForCallback(sc);
1646 return *this;
1647}
1648
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001649SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::addTransactionCallback(
1650 TransactionCompletedCallbackTakesContext callback, void* callbackContext,
1651 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001652 auto listener = TransactionCompletedListener::getInstance();
1653
Marissa Wall80d94ad2019-01-18 16:04:36 -08001654 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1655 std::placeholders::_2, std::placeholders::_3);
1656 const auto& surfaceControls =
1657 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001658
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001659 CallbackId callbackId =
1660 listener->addCallbackFunction(callbackWithContext, surfaceControls, callbackType);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001661
1662 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1663 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001664 return *this;
1665}
1666
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001667SurfaceComposerClient::Transaction&
1668SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
1669 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1670 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMPLETE);
1671}
1672
1673SurfaceComposerClient::Transaction&
1674SurfaceComposerClient::Transaction::addTransactionCommittedCallback(
1675 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1676 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMMIT);
1677}
1678
Valerie Hau871d6352020-01-29 08:44:02 -08001679SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1680 const sp<SurfaceControl>& sc) {
1681 layer_state_t* s = getLayerState(sc);
1682 if (!s) {
1683 mStatus = BAD_INDEX;
1684 return *this;
1685 }
1686
1687 s->what |= layer_state_t::eProducerDisconnect;
1688 return *this;
1689}
1690
Robert Carr2c358bf2018-08-08 15:58:15 -07001691SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
chaviw98318de2021-05-19 16:45:23 -05001692 const sp<SurfaceControl>& sc, const WindowInfo& info) {
Robert Carr2c358bf2018-08-08 15:58:15 -07001693 layer_state_t* s = getLayerState(sc);
1694 if (!s) {
1695 mStatus = BAD_INDEX;
1696 return *this;
1697 }
chaviw98318de2021-05-19 16:45:23 -05001698 s->windowInfoHandle = new WindowInfoHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001699 s->what |= layer_state_t::eInputInfoChanged;
1700 return *this;
1701}
chaviw273171b2018-12-26 11:46:30 -08001702
Vishnu Naire798b472020-07-23 13:52:21 -07001703SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Naire798b472020-07-23 13:52:21 -07001704 const FocusRequest& request) {
1705 mInputWindowCommands.focusRequests.push_back(request);
1706 return *this;
1707}
1708
Patrick Williamse5991f52022-06-23 20:23:43 +00001709SurfaceComposerClient::Transaction&
1710SurfaceComposerClient::Transaction::addWindowInfosReportedListener(
1711 sp<gui::IWindowInfosReportedListener> windowInfosReportedListener) {
1712 mInputWindowCommands.windowInfosReportedListeners.insert(windowInfosReportedListener);
chaviwa911b102019-02-14 10:18:33 -08001713 return *this;
1714}
1715
Peiyong Lind3788632018-09-18 16:01:31 -07001716SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1717 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1718 layer_state_t* s = getLayerState(sc);
1719 if (!s) {
1720 mStatus = BAD_INDEX;
1721 return *this;
1722 }
1723 s->what |= layer_state_t::eColorTransformChanged;
1724 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001725
1726 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001727 return *this;
1728}
1729
Robert Carrfb4d58b2019-01-15 09:21:27 -08001730SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1731 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
chaviw25714502021-02-11 10:01:08 -08001732 setCrop(sc, source);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001733
1734 int x = dst.left;
1735 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001736
1737 float sourceWidth = source.getWidth();
1738 float sourceHeight = source.getHeight();
1739
1740 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1741 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001742 float matrix[4] = {1, 0, 0, 1};
1743
1744 switch (transform) {
1745 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1746 matrix[0] = -xScale; matrix[1] = 0;
1747 matrix[2] = 0; matrix[3] = yScale;
1748 x += source.getWidth();
1749 break;
1750 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1751 matrix[0] = xScale; matrix[1] = 0;
1752 matrix[2] = 0; matrix[3] = -yScale;
1753 y += source.getHeight();
1754 break;
1755 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1756 matrix[0] = 0; matrix[1] = -yScale;
1757 matrix[2] = xScale; matrix[3] = 0;
1758 x += source.getHeight();
1759 break;
1760 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1761 matrix[0] = -xScale; matrix[1] = 0;
1762 matrix[2] = 0; matrix[3] = -yScale;
1763 x += source.getWidth();
1764 y += source.getHeight();
1765 break;
1766 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1767 matrix[0] = 0; matrix[1] = yScale;
1768 matrix[2] = -xScale; matrix[3] = 0;
1769 y += source.getWidth();
1770 break;
1771 default:
1772 matrix[0] = xScale; matrix[1] = 0;
1773 matrix[2] = 0; matrix[3] = yScale;
1774 break;
1775 }
1776 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07001777 float offsetX = xScale * source.left;
1778 float offsetY = yScale * source.top;
1779 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001780
1781 return *this;
1782}
1783
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001784SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1785 const sp<SurfaceControl>& sc, float shadowRadius) {
1786 layer_state_t* s = getLayerState(sc);
1787 if (!s) {
1788 mStatus = BAD_INDEX;
1789 return *this;
1790 }
1791 s->what |= layer_state_t::eShadowRadiusChanged;
1792 s->shadowRadius = shadowRadius;
1793 return *this;
1794}
1795
Steven Thomas3172e202020-01-06 19:25:30 -08001796SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Marin Shalamanov46084422020-10-13 12:33:42 +02001797 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility,
Marin Shalamanovc5986772021-03-16 16:09:49 +01001798 int8_t changeFrameRateStrategy) {
Steven Thomas3172e202020-01-06 19:25:30 -08001799 layer_state_t* s = getLayerState(sc);
1800 if (!s) {
1801 mStatus = BAD_INDEX;
1802 return *this;
1803 }
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001804 // Allow privileged values as well here, those will be ignored by SF if
1805 // the caller is not privileged
Marin Shalamanovc5986772021-03-16 16:09:49 +01001806 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1807 "Transaction::setFrameRate",
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001808 /*privileged=*/true)) {
Steven Thomas62a4cf82020-01-31 12:04:03 -08001809 mStatus = BAD_VALUE;
1810 return *this;
1811 }
Steven Thomas3172e202020-01-06 19:25:30 -08001812 s->what |= layer_state_t::eFrameRateChanged;
1813 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08001814 s->frameRateCompatibility = compatibility;
Marin Shalamanovc5986772021-03-16 16:09:49 +01001815 s->changeFrameRateStrategy = changeFrameRateStrategy;
Steven Thomas3172e202020-01-06 19:25:30 -08001816 return *this;
1817}
1818
Andy Labrada096227e2022-06-15 16:58:11 +00001819SurfaceComposerClient::Transaction&
1820SurfaceComposerClient::Transaction::setDefaultFrameRateCompatibility(const sp<SurfaceControl>& sc,
1821 int8_t compatibility) {
1822 layer_state_t* s = getLayerState(sc);
1823 if (!s) {
1824 mStatus = BAD_INDEX;
1825 return *this;
1826 }
1827 s->what |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
1828 s->defaultFrameRateCompatibility = compatibility;
1829 return *this;
1830}
1831
Vishnu Nair6213bd92020-05-08 17:42:25 -07001832SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
1833 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
1834 layer_state_t* s = getLayerState(sc);
1835 if (!s) {
1836 mStatus = BAD_INDEX;
1837 return *this;
1838 }
1839
1840 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
1841 ? ui::Transform::ROT_INVALID
1842 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
1843 s->what |= layer_state_t::eFixedTransformHintChanged;
1844 s->fixedTransformHint = transform;
1845 return *this;
1846}
1847
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001848SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
1849 const FrameTimelineInfo& frameTimelineInfo) {
Huihong Luo3bdef862022-03-03 11:57:19 -08001850 mergeFrameTimelineInfo(mFrameTimelineInfo, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -07001851 return *this;
1852}
1853
Vishnu Naircf26a0a2020-11-13 12:56:20 -08001854SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAutoRefresh(
1855 const sp<SurfaceControl>& sc, bool autoRefresh) {
1856 layer_state_t* s = getLayerState(sc);
1857 if (!s) {
1858 mStatus = BAD_INDEX;
1859 return *this;
1860 }
1861
1862 s->what |= layer_state_t::eAutoRefreshChanged;
1863 s->autoRefresh = autoRefresh;
1864 return *this;
1865}
1866
Winson Chunga30f7c92021-06-29 15:42:56 -07001867SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedOverlay(
1868 const sp<SurfaceControl>& sc, bool isTrustedOverlay) {
1869 layer_state_t* s = getLayerState(sc);
1870 if (!s) {
1871 mStatus = BAD_INDEX;
1872 return *this;
1873 }
1874
1875 s->what |= layer_state_t::eTrustedOverlayChanged;
1876 s->isTrustedOverlay = isTrustedOverlay;
1877 return *this;
1878}
1879
Vishnu Nair277142c2021-01-05 18:35:29 -08001880SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken(
1881 const sp<IBinder>& applyToken) {
1882 mApplyToken = applyToken;
1883 return *this;
1884}
1885
John Reckcdb4ed72021-02-04 13:39:33 -05001886SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setStretchEffect(
Nader Jawad2dfc98b2021-04-08 20:35:39 -07001887 const sp<SurfaceControl>& sc, const StretchEffect& stretchEffect) {
John Reckcdb4ed72021-02-04 13:39:33 -05001888 layer_state_t* s = getLayerState(sc);
1889 if (!s) {
1890 mStatus = BAD_INDEX;
1891 return *this;
1892 }
1893
1894 s->what |= layer_state_t::eStretchChanged;
Nader Jawad2dfc98b2021-04-08 20:35:39 -07001895 s->stretchEffect = stretchEffect;
John Reckcdb4ed72021-02-04 13:39:33 -05001896 return *this;
1897}
1898
chaviwf3f40fe2021-04-27 15:54:02 -05001899SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferCrop(
1900 const sp<SurfaceControl>& sc, const Rect& bufferCrop) {
1901 layer_state_t* s = getLayerState(sc);
1902 if (!s) {
1903 mStatus = BAD_INDEX;
1904 return *this;
1905 }
1906
1907 s->what |= layer_state_t::eBufferCropChanged;
1908 s->bufferCrop = bufferCrop;
1909
1910 registerSurfaceControlForCallback(sc);
1911 return *this;
1912}
1913
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001914SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDestinationFrame(
1915 const sp<SurfaceControl>& sc, const Rect& destinationFrame) {
1916 layer_state_t* s = getLayerState(sc);
1917 if (!s) {
1918 mStatus = BAD_INDEX;
1919 return *this;
1920 }
1921
1922 s->what |= layer_state_t::eDestinationFrameChanged;
1923 s->destinationFrame = destinationFrame;
1924
1925 registerSurfaceControlForCallback(sc);
1926 return *this;
1927}
1928
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07001929SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDropInputMode(
1930 const sp<SurfaceControl>& sc, gui::DropInputMode mode) {
1931 layer_state_t* s = getLayerState(sc);
1932 if (!s) {
1933 mStatus = BAD_INDEX;
1934 return *this;
1935 }
1936
1937 s->what |= layer_state_t::eDropInputModeChanged;
1938 s->dropInputMode = mode;
1939
1940 registerSurfaceControlForCallback(sc);
1941 return *this;
1942}
1943
Tianhao Yao67dd7122022-02-22 17:48:33 +00001944SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::enableBorder(
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001945 const sp<SurfaceControl>& sc, bool shouldEnable, float width, const half4& color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00001946 layer_state_t* s = getLayerState(sc);
1947 if (!s) {
1948 mStatus = BAD_INDEX;
1949 return *this;
1950 }
1951
1952 s->what |= layer_state_t::eRenderBorderChanged;
1953 s->borderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00001954 s->borderWidth = width;
1955 s->borderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00001956
1957 registerSurfaceControlForCallback(sc);
1958 return *this;
1959}
1960
Mathias Agopian698c0872011-06-28 19:09:31 -07001961// ---------------------------------------------------------------------------
1962
chaviw763ef572018-02-22 16:04:57 -08001963DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001964 DisplayState s;
1965 s.token = token;
1966 ssize_t index = mDisplayStates.indexOf(s);
1967 if (index < 0) {
1968 // we don't have it, add an initialized layer_state to our list
1969 s.what = 0;
1970 index = mDisplayStates.add(s);
1971 }
Dan Stozad723bd72014-11-18 10:24:03 -08001972 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001973}
1974
Robert Carr4cdc58f2017-08-23 14:22:20 -07001975status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1976 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001977 if (bufferProducer.get() != nullptr) {
1978 // Make sure that composition can never be stalled by a virtual display
1979 // consumer that isn't processing buffers fast enough.
1980 status_t err = bufferProducer->setAsyncMode(true);
1981 if (err != NO_ERROR) {
1982 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1983 "BufferQueue. This BufferQueue cannot be used for virtual "
1984 "display. (%d)", err);
1985 return err;
1986 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001987 }
chaviw763ef572018-02-22 16:04:57 -08001988 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001989 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001990 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001991 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001992}
1993
Robert Carr4cdc58f2017-08-23 14:22:20 -07001994void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001995 ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001996 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001997 s.layerStack = layerStack;
1998 s.what |= DisplayState::eLayerStackChanged;
1999}
2000
Evan Rosky2239b372021-05-20 13:43:47 -07002001void SurfaceComposerClient::Transaction::setDisplayFlags(const sp<IBinder>& token, uint32_t flags) {
2002 DisplayState& s(getDisplayState(token));
2003 s.flags = flags;
2004 s.what |= DisplayState::eFlagsChanged;
2005}
2006
Robert Carr4cdc58f2017-08-23 14:22:20 -07002007void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08002008 ui::Rotation orientation,
2009 const Rect& layerStackRect,
2010 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08002011 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002012 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002013 s.layerStackSpaceRect = layerStackRect;
2014 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002015 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002016}
2017
Robert Carr4cdc58f2017-08-23 14:22:20 -07002018void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08002019 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07002020 s.width = width;
2021 s.height = height;
2022 s.what |= DisplayState::eDisplaySizeChanged;
2023}
2024
Huihong Luo3bdef862022-03-03 11:57:19 -08002025// copied from FrameTimelineInfo::merge()
2026void SurfaceComposerClient::Transaction::mergeFrameTimelineInfo(FrameTimelineInfo& t,
2027 const FrameTimelineInfo& other) {
2028 // When merging vsync Ids we take the oldest valid one
2029 if (t.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID &&
2030 other.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
2031 if (other.vsyncId > t.vsyncId) {
2032 t.vsyncId = other.vsyncId;
2033 t.inputEventId = other.inputEventId;
2034 t.startTimeNanos = other.startTimeNanos;
2035 }
2036 } else if (t.vsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) {
2037 t.vsyncId = other.vsyncId;
2038 t.inputEventId = other.inputEventId;
2039 t.startTimeNanos = other.startTimeNanos;
2040 }
2041}
2042
2043// copied from FrameTimelineInfo::clear()
2044void SurfaceComposerClient::Transaction::clearFrameTimelineInfo(FrameTimelineInfo& t) {
2045 t.vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID;
2046 t.inputEventId = os::IInputConstants::INVALID_INPUT_EVENT_ID;
2047 t.startTimeNanos = 0;
2048}
2049
Mathias Agopiane57f2922012-08-09 16:29:12 -07002050// ---------------------------------------------------------------------------
2051
chaviw60c9d3e2021-06-04 12:52:17 -05002052SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002053
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002054SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
chaviw60c9d3e2021-06-04 12:52:17 -05002055 : mStatus(NO_ERROR), mClient(client) {}
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002056
Mathias Agopian698c0872011-06-28 19:09:31 -07002057void SurfaceComposerClient::onFirstRef() {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002058 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002059 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08002060 sp<ISurfaceComposerClient> conn;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002061 binder::Status status = sf->createConnection(&conn);
2062 if (status.isOk() && conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002063 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002064 mStatus = NO_ERROR;
2065 }
2066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067}
2068
Mathias Agopian698c0872011-06-28 19:09:31 -07002069SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07002070 dispose();
2071}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07002072
Mathias Agopian698c0872011-06-28 19:09:31 -07002073status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074 return mStatus;
2075}
2076
Mathias Agopian698c0872011-06-28 19:09:31 -07002077sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002078 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079}
2080
Mathias Agopiand4784a32010-05-27 19:41:15 -07002081status_t SurfaceComposerClient::linkToComposerDeath(
2082 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07002083 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002084 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2085 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086}
2087
Mathias Agopian698c0872011-06-28 19:09:31 -07002088void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002089 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07002090 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002091 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07002092 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002093 client = mClient; // hold ref while lock is held
2094 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07002096 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097}
2098
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002099sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002100 PixelFormat format, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002101 const sp<IBinder>& parentHandle,
Valerie Hau1acd6962019-10-28 16:35:48 -07002102 LayerMetadata metadata,
2103 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07002104 sp<SurfaceControl> s;
Vishnu Nair992496b2020-10-22 17:27:21 -07002105 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07002106 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07002107 return s;
2108}
2109
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002110status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
2111 PixelFormat format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002112 sp<SurfaceControl>* outSurface, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002113 const sp<IBinder>& parentHandle,
2114 LayerMetadata metadata,
Valerie Hau1acd6962019-10-28 16:35:48 -07002115 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002116 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07002117 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07002118
Mathias Agopian698c0872011-06-28 19:09:31 -07002119 if (mStatus == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002120 gui::CreateSurfaceResult result;
2121 binder::Status status = mClient->createSurface(std::string(name.string()), flags,
2122 parentHandle, std::move(metadata), &result);
2123 err = statusTFromBinderStatus(status);
Valerie Hau1acd6962019-10-28 16:35:48 -07002124 if (outTransformHint) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002125 *outTransformHint = result.transformHint;
Valerie Hau1acd6962019-10-28 16:35:48 -07002126 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002127 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
2128 if (err == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002129 *outSurface = new SurfaceControl(this, result.handle, result.layerId, w, h, format,
2130 result.transformHint, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -07002131 }
2132 }
Robert Carr3b382ed2018-03-14 13:49:41 -07002133 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07002134}
2135
chaviwfe94a222019-08-21 13:52:59 -07002136sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
2137 if (mirrorFromSurface == nullptr) {
2138 return nullptr;
2139 }
2140
chaviwfe94a222019-08-21 13:52:59 -07002141 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002142 gui::MirrorSurfaceResult result;
2143 const binder::Status status = mClient->mirrorSurface(mirrorFromHandle, &result);
2144 const status_t err = statusTFromBinderStatus(status);
chaviwfe94a222019-08-21 13:52:59 -07002145 if (err == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002146 return new SurfaceControl(this, result.handle, result.layerId);
chaviwfe94a222019-08-21 13:52:59 -07002147 }
2148 return nullptr;
2149}
2150
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002151sp<SurfaceControl> SurfaceComposerClient::mirrorDisplay(DisplayId displayId) {
2152 gui::MirrorSurfaceResult result;
2153 const binder::Status status = mClient->mirrorDisplay(displayId.value, &result);
2154 const status_t err = statusTFromBinderStatus(status);
2155 if (err == NO_ERROR) {
2156 return new SurfaceControl(this, result.handle, result.layerId);
2157 }
2158 return nullptr;
2159}
2160
Svetoslavd85084b2014-03-20 10:28:31 -07002161status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
2162 if (mStatus != NO_ERROR) {
2163 return mStatus;
2164 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002165 const binder::Status status = mClient->clearLayerFrameStats(token);
2166 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002167}
2168
2169status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
2170 FrameStats* outStats) const {
2171 if (mStatus != NO_ERROR) {
2172 return mStatus;
2173 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002174 gui::FrameStats stats;
2175 const binder::Status status = mClient->getLayerFrameStats(token, &stats);
2176 if (status.isOk()) {
2177 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2178 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2179 for (const auto& t : stats.desiredPresentTimesNano) {
2180 outStats->desiredPresentTimesNano.add(t);
2181 }
2182 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2183 for (const auto& t : stats.actualPresentTimesNano) {
2184 outStats->actualPresentTimesNano.add(t);
2185 }
2186 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2187 for (const auto& t : stats.frameReadyTimesNano) {
2188 outStats->frameReadyTimesNano.add(t);
2189 }
2190 }
2191 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002192}
2193
Mathias Agopian698c0872011-06-28 19:09:31 -07002194// ----------------------------------------------------------------------------
2195
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07002196status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Huihong Luo05539a12022-02-23 10:29:40 -08002197 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2198 binder::Status status = sf->enableVSyncInjections(enable);
Huihong Luo3bdef862022-03-03 11:57:19 -08002199 return statusTFromBinderStatus(status);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07002200}
2201
2202status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Huihong Luo05539a12022-02-23 10:29:40 -08002203 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2204 binder::Status status = sf->injectVSync(when);
Huihong Luo3bdef862022-03-03 11:57:19 -08002205 return statusTFromBinderStatus(status);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07002206}
2207
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002208status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
2209 ui::DisplayState* state) {
Huihong Luoaa7fc2e2022-02-15 10:43:00 -08002210 gui::DisplayState ds;
2211 binder::Status status =
2212 ComposerServiceAIDL::getComposerService()->getDisplayState(display, &ds);
2213 if (status.isOk()) {
2214 state->layerStack = ui::LayerStack::fromValue(ds.layerStack);
2215 state->orientation = static_cast<ui::Rotation>(ds.orientation);
2216 state->layerStackSpaceRect =
2217 ui::Size(ds.layerStackSpaceRect.width, ds.layerStackSpaceRect.height);
2218 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002219 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002220}
2221
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002222status_t SurfaceComposerClient::getStaticDisplayInfo(const sp<IBinder>& display,
Huihong Luoa79ddf42022-02-17 00:01:38 -08002223 ui::StaticDisplayInfo* outInfo) {
2224 using Tag = android::gui::DeviceProductInfo::ManufactureOrModelDate::Tag;
2225 gui::StaticDisplayInfo ginfo;
2226 binder::Status status =
2227 ComposerServiceAIDL::getComposerService()->getStaticDisplayInfo(display, &ginfo);
2228 if (status.isOk()) {
2229 // convert gui::StaticDisplayInfo to ui::StaticDisplayInfo
2230 outInfo->connectionType = static_cast<ui::DisplayConnectionType>(ginfo.connectionType);
2231 outInfo->density = ginfo.density;
2232 outInfo->secure = ginfo.secure;
2233 outInfo->installOrientation = static_cast<ui::Rotation>(ginfo.installOrientation);
2234
2235 DeviceProductInfo info;
2236 std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo;
2237 gui::DeviceProductInfo::ManufactureOrModelDate& date = dpi->manufactureOrModelDate;
2238 info.name = dpi->name;
2239 if (dpi->manufacturerPnpId.size() > 0) {
2240 // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
2241 constexpr int kMaxPnpIdSize = 4;
2242 size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
2243 std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
2244 }
Kriti Dang1de958d2022-05-30 15:26:58 +02002245 if (dpi->relativeAddress.size() > 0) {
2246 std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
2247 std::back_inserter(info.relativeAddress));
2248 }
Huihong Luoa79ddf42022-02-17 00:01:38 -08002249 info.productId = dpi->productId;
2250 if (date.getTag() == Tag::modelYear) {
2251 DeviceProductInfo::ModelYear modelYear;
2252 modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
2253 info.manufactureOrModelDate = modelYear;
2254 } else if (date.getTag() == Tag::manufactureYear) {
2255 DeviceProductInfo::ManufactureYear manufactureYear;
2256 manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
2257 info.manufactureOrModelDate = manufactureYear;
2258 } else if (date.getTag() == Tag::manufactureWeekAndYear) {
2259 DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
2260 weekAndYear.year =
2261 date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
2262 weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
2263 info.manufactureOrModelDate = weekAndYear;
2264 }
2265
2266 outInfo->deviceProductInfo = info;
2267 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002268 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002269}
2270
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002271status_t SurfaceComposerClient::getDynamicDisplayInfo(const sp<IBinder>& display,
Huihong Luo38603fd2022-02-21 14:32:54 -08002272 ui::DynamicDisplayInfo* outInfo) {
2273 gui::DynamicDisplayInfo ginfo;
2274 binder::Status status =
2275 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfo(display, &ginfo);
2276 if (status.isOk()) {
2277 // convert gui::DynamicDisplayInfo to ui::DynamicDisplayInfo
2278 outInfo->supportedDisplayModes.clear();
2279 outInfo->supportedDisplayModes.reserve(ginfo.supportedDisplayModes.size());
2280 for (const auto& mode : ginfo.supportedDisplayModes) {
2281 ui::DisplayMode outMode;
2282 outMode.id = mode.id;
2283 outMode.resolution.width = mode.resolution.width;
2284 outMode.resolution.height = mode.resolution.height;
2285 outMode.xDpi = mode.xDpi;
2286 outMode.yDpi = mode.yDpi;
2287 outMode.refreshRate = mode.refreshRate;
2288 outMode.appVsyncOffset = mode.appVsyncOffset;
2289 outMode.sfVsyncOffset = mode.sfVsyncOffset;
2290 outMode.presentationDeadline = mode.presentationDeadline;
2291 outMode.group = mode.group;
2292 outInfo->supportedDisplayModes.push_back(outMode);
2293 }
2294
2295 outInfo->activeDisplayModeId = ginfo.activeDisplayModeId;
2296
2297 outInfo->supportedColorModes.clear();
2298 outInfo->supportedColorModes.reserve(ginfo.supportedColorModes.size());
2299 for (const auto& cmode : ginfo.supportedColorModes) {
2300 outInfo->supportedColorModes.push_back(static_cast<ui::ColorMode>(cmode));
2301 }
2302
2303 outInfo->activeColorMode = static_cast<ui::ColorMode>(ginfo.activeColorMode);
2304
2305 std::vector<ui::Hdr> types;
2306 types.reserve(ginfo.hdrCapabilities.supportedHdrTypes.size());
2307 for (const auto& hdr : ginfo.hdrCapabilities.supportedHdrTypes) {
2308 types.push_back(static_cast<ui::Hdr>(hdr));
2309 }
2310 outInfo->hdrCapabilities = HdrCapabilities(types, ginfo.hdrCapabilities.maxLuminance,
2311 ginfo.hdrCapabilities.maxAverageLuminance,
2312 ginfo.hdrCapabilities.minLuminance);
2313
2314 outInfo->autoLowLatencyModeSupported = ginfo.autoLowLatencyModeSupported;
2315 outInfo->gameContentTypeSupported = ginfo.gameContentTypeSupported;
2316 outInfo->preferredBootDisplayMode = ginfo.preferredBootDisplayMode;
2317 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002318 return statusTFromBinderStatus(status);
Dan Stoza7f7da322014-05-02 15:26:25 -07002319}
2320
Marin Shalamanova7fe3042021-01-29 21:02:08 +01002321status_t SurfaceComposerClient::getActiveDisplayMode(const sp<IBinder>& display,
2322 ui::DisplayMode* mode) {
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002323 ui::DynamicDisplayInfo info;
2324 status_t result = getDynamicDisplayInfo(display, &info);
Dan Stoza7f7da322014-05-02 15:26:25 -07002325 if (result != NO_ERROR) {
2326 return result;
2327 }
2328
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002329 if (const auto activeMode = info.getActiveDisplayMode()) {
2330 *mode = *activeMode;
2331 return NO_ERROR;
Dan Stoza7f7da322014-05-02 15:26:25 -07002332 }
2333
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002334 ALOGE("Active display mode not found.");
2335 return NAME_NOT_FOUND;
Dan Stoza7f7da322014-05-02 15:26:25 -07002336}
2337
Marin Shalamanova7fe3042021-01-29 21:02:08 +01002338status_t SurfaceComposerClient::setDesiredDisplayModeSpecs(
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002339 const sp<IBinder>& displayToken, ui::DisplayModeId defaultMode, bool allowGroupSwitching,
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +02002340 float primaryRefreshRateMin, float primaryRefreshRateMax, float appRequestRefreshRateMin,
2341 float appRequestRefreshRateMax) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002342 binder::Status status =
2343 ComposerServiceAIDL::getComposerService()
2344 ->setDesiredDisplayModeSpecs(displayToken, defaultMode, allowGroupSwitching,
2345 primaryRefreshRateMin, primaryRefreshRateMax,
2346 appRequestRefreshRateMin,
2347 appRequestRefreshRateMax);
Huihong Luo3bdef862022-03-03 11:57:19 -08002348 return statusTFromBinderStatus(status);
Ana Krulec0782b882019-10-15 17:34:54 -07002349}
2350
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002351status_t SurfaceComposerClient::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
2352 ui::DisplayModeId* outDefaultMode,
2353 bool* outAllowGroupSwitching,
2354 float* outPrimaryRefreshRateMin,
2355 float* outPrimaryRefreshRateMax,
2356 float* outAppRequestRefreshRateMin,
2357 float* outAppRequestRefreshRateMax) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002358 if (!outDefaultMode || !outAllowGroupSwitching || !outPrimaryRefreshRateMin ||
2359 !outPrimaryRefreshRateMax || !outAppRequestRefreshRateMin || !outAppRequestRefreshRateMax) {
2360 return BAD_VALUE;
2361 }
2362 gui::DisplayModeSpecs specs;
2363 binder::Status status =
2364 ComposerServiceAIDL::getComposerService()->getDesiredDisplayModeSpecs(displayToken,
2365 &specs);
2366 if (status.isOk()) {
2367 *outDefaultMode = specs.defaultMode;
2368 *outAllowGroupSwitching = specs.allowGroupSwitching;
2369 *outPrimaryRefreshRateMin = specs.primaryRefreshRateMin;
2370 *outPrimaryRefreshRateMax = specs.primaryRefreshRateMax;
2371 *outAppRequestRefreshRateMin = specs.appRequestRefreshRateMin;
2372 *outAppRequestRefreshRateMax = specs.appRequestRefreshRateMax;
2373 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002374 return statusTFromBinderStatus(status);
Ana Krulec234bb162019-11-10 22:55:55 +01002375}
2376
Daniel Solomon42d04562019-01-20 21:03:19 -08002377status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
2378 ui::DisplayPrimaries& outPrimaries) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002379 gui::DisplayPrimaries primaries;
2380 binder::Status status =
2381 ComposerServiceAIDL::getComposerService()->getDisplayNativePrimaries(display,
2382 &primaries);
2383 if (status.isOk()) {
2384 outPrimaries.red.X = primaries.red.X;
2385 outPrimaries.red.Y = primaries.red.Y;
2386 outPrimaries.red.Z = primaries.red.Z;
2387
2388 outPrimaries.green.X = primaries.green.X;
2389 outPrimaries.green.Y = primaries.green.Y;
2390 outPrimaries.green.Z = primaries.green.Z;
2391
2392 outPrimaries.blue.X = primaries.blue.X;
2393 outPrimaries.blue.Y = primaries.blue.Y;
2394 outPrimaries.blue.Z = primaries.blue.Z;
2395
2396 outPrimaries.white.X = primaries.white.X;
2397 outPrimaries.white.Y = primaries.white.Y;
2398 outPrimaries.white.Z = primaries.white.Z;
2399 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002400 return statusTFromBinderStatus(status);
Daniel Solomon42d04562019-01-20 21:03:19 -08002401}
2402
Michael Wright28f24d02016-07-12 13:30:53 -07002403status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07002404 ColorMode colorMode) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002405 binder::Status status = ComposerServiceAIDL::getComposerService()
2406 ->setActiveColorMode(display, static_cast<int>(colorMode));
Huihong Luo3bdef862022-03-03 11:57:19 -08002407 return statusTFromBinderStatus(status);
Michael Wright28f24d02016-07-12 13:30:53 -07002408}
2409
Kriti Dang7defaf32021-11-15 11:55:43 +01002410status_t SurfaceComposerClient::getBootDisplayModeSupport(bool* support) {
Huihong Luo37396db2022-02-15 10:43:00 -08002411 binder::Status status =
2412 ComposerServiceAIDL::getComposerService()->getBootDisplayModeSupport(support);
Huihong Luo3bdef862022-03-03 11:57:19 -08002413 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002414}
2415
2416status_t SurfaceComposerClient::setBootDisplayMode(const sp<IBinder>& display,
2417 ui::DisplayModeId displayModeId) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002418 binder::Status status = ComposerServiceAIDL::getComposerService()
2419 ->setBootDisplayMode(display, static_cast<int>(displayModeId));
Huihong Luo3bdef862022-03-03 11:57:19 -08002420 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002421}
2422
2423status_t SurfaceComposerClient::clearBootDisplayMode(const sp<IBinder>& display) {
Huihong Luo37396db2022-02-15 10:43:00 -08002424 binder::Status status =
2425 ComposerServiceAIDL::getComposerService()->clearBootDisplayMode(display);
Huihong Luo3bdef862022-03-03 11:57:19 -08002426 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002427}
2428
Andy Yu2ae6b6b2021-11-18 14:51:06 -08002429status_t SurfaceComposerClient::setOverrideFrameRate(uid_t uid, float frameRate) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002430 binder::Status status =
2431 ComposerServiceAIDL::getComposerService()->setOverrideFrameRate(uid, frameRate);
2432 return statusTFromBinderStatus(status);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08002433}
2434
Galia Peycheva5492cb52019-10-30 14:13:16 +01002435void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002436 ComposerServiceAIDL::getComposerService()->setAutoLowLatencyMode(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002437}
2438
Galia Peycheva5492cb52019-10-30 14:13:16 +01002439void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002440 ComposerServiceAIDL::getComposerService()->setGameContentType(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002441}
2442
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002443void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
2444 int mode) {
Huihong Luo37396db2022-02-15 10:43:00 -08002445 ComposerServiceAIDL::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07002446}
2447
Peiyong Linc6780972018-10-28 15:24:08 -07002448status_t SurfaceComposerClient::getCompositionPreference(
2449 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
2450 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
Huihong Luo05539a12022-02-23 10:29:40 -08002451 gui::CompositionPreference pref;
2452 binder::Status status =
2453 ComposerServiceAIDL::getComposerService()->getCompositionPreference(&pref);
2454 if (status.isOk()) {
2455 *defaultDataspace = static_cast<ui::Dataspace>(pref.defaultDataspace);
2456 *defaultPixelFormat = static_cast<ui::PixelFormat>(pref.defaultPixelFormat);
2457 *wideColorGamutDataspace = static_cast<ui::Dataspace>(pref.wideColorGamutDataspace);
2458 *wideColorGamutPixelFormat = static_cast<ui::PixelFormat>(pref.wideColorGamutPixelFormat);
2459 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002460 return statusTFromBinderStatus(status);
Peiyong Lin0256f722018-08-31 15:45:10 -07002461}
2462
Peiyong Lin08d10512019-01-16 13:27:35 -08002463bool SurfaceComposerClient::getProtectedContentSupport() {
2464 bool supported = false;
Huihong Luo05539a12022-02-23 10:29:40 -08002465 ComposerServiceAIDL::getComposerService()->getProtectedContentSupport(&supported);
Peiyong Lin08d10512019-01-16 13:27:35 -08002466 return supported;
2467}
2468
Svetoslavd85084b2014-03-20 10:28:31 -07002469status_t SurfaceComposerClient::clearAnimationFrameStats() {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002470 binder::Status status = ComposerServiceAIDL::getComposerService()->clearAnimationFrameStats();
Huihong Luo3bdef862022-03-03 11:57:19 -08002471 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002472}
2473
2474status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002475 gui::FrameStats stats;
2476 binder::Status status =
2477 ComposerServiceAIDL::getComposerService()->getAnimationFrameStats(&stats);
2478 if (status.isOk()) {
2479 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2480 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2481 for (const auto& t : stats.desiredPresentTimesNano) {
2482 outStats->desiredPresentTimesNano.add(t);
2483 }
2484 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2485 for (const auto& t : stats.actualPresentTimesNano) {
2486 outStats->actualPresentTimesNano.add(t);
2487 }
2488 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2489 for (const auto& t : stats.frameReadyTimesNano) {
2490 outStats->frameReadyTimesNano.add(t);
2491 }
2492 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002493 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002494}
2495
Kriti Dang49ad4132021-01-08 11:49:56 +01002496status_t SurfaceComposerClient::overrideHdrTypes(const sp<IBinder>& display,
2497 const std::vector<ui::Hdr>& hdrTypes) {
Huihong Luo05539a12022-02-23 10:29:40 -08002498 std::vector<int32_t> hdrTypesVector;
2499 hdrTypesVector.reserve(hdrTypes.size());
2500 for (auto t : hdrTypes) {
2501 hdrTypesVector.push_back(static_cast<int32_t>(t));
2502 }
2503
2504 binder::Status status =
2505 ComposerServiceAIDL::getComposerService()->overrideHdrTypes(display, hdrTypesVector);
Huihong Luo3bdef862022-03-03 11:57:19 -08002506 return statusTFromBinderStatus(status);
Kriti Dang49ad4132021-01-08 11:49:56 +01002507}
2508
Tej Singhe2751772021-04-06 22:05:29 -07002509status_t SurfaceComposerClient::onPullAtom(const int32_t atomId, std::string* outData,
2510 bool* success) {
Huihong Luo05539a12022-02-23 10:29:40 -08002511 gui::PullAtomData pad;
2512 binder::Status status = ComposerServiceAIDL::getComposerService()->onPullAtom(atomId, &pad);
2513 if (status.isOk()) {
2514 outData->assign((const char*)pad.data.data(), pad.data.size());
2515 *success = pad.success;
2516 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002517 return statusTFromBinderStatus(status);
Tej Singhe2751772021-04-06 22:05:29 -07002518}
2519
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002520status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
2521 ui::PixelFormat* outFormat,
2522 ui::Dataspace* outDataspace,
2523 uint8_t* outComponentMask) {
Huihong Luo05539a12022-02-23 10:29:40 -08002524 if (!outFormat || !outDataspace || !outComponentMask) {
2525 return BAD_VALUE;
2526 }
2527
2528 gui::ContentSamplingAttributes attrs;
2529 binder::Status status = ComposerServiceAIDL::getComposerService()
2530 ->getDisplayedContentSamplingAttributes(display, &attrs);
2531 if (status.isOk()) {
2532 *outFormat = static_cast<ui::PixelFormat>(attrs.format);
2533 *outDataspace = static_cast<ui::Dataspace>(attrs.dataspace);
2534 *outComponentMask = static_cast<uint8_t>(attrs.componentMask);
2535 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002536 return statusTFromBinderStatus(status);
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002537}
2538
Kevin DuBois74e53772018-11-19 10:52:38 -08002539status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
2540 bool enable, uint8_t componentMask,
2541 uint64_t maxFrames) {
Huihong Luo05539a12022-02-23 10:29:40 -08002542 binder::Status status =
2543 ComposerServiceAIDL::getComposerService()
2544 ->setDisplayContentSamplingEnabled(display, enable,
2545 static_cast<int8_t>(componentMask),
2546 static_cast<int64_t>(maxFrames));
Huihong Luo3bdef862022-03-03 11:57:19 -08002547 return statusTFromBinderStatus(status);
Kevin DuBois74e53772018-11-19 10:52:38 -08002548}
2549
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002550status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
2551 uint64_t maxFrames, uint64_t timestamp,
2552 DisplayedFrameStats* outStats) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002553 if (!outStats) {
2554 return BAD_VALUE;
2555 }
2556
2557 gui::DisplayedFrameStats stats;
2558 binder::Status status =
2559 ComposerServiceAIDL::getComposerService()->getDisplayedContentSample(display, maxFrames,
2560 timestamp, &stats);
2561 if (status.isOk()) {
2562 // convert gui::DisplayedFrameStats to ui::DisplayedFrameStats
2563 outStats->numFrames = static_cast<uint64_t>(stats.numFrames);
2564 outStats->component_0_sample.reserve(stats.component_0_sample.size());
2565 for (const auto& s : stats.component_0_sample) {
2566 outStats->component_0_sample.push_back(static_cast<uint64_t>(s));
2567 }
2568 outStats->component_1_sample.reserve(stats.component_1_sample.size());
2569 for (const auto& s : stats.component_1_sample) {
2570 outStats->component_1_sample.push_back(static_cast<uint64_t>(s));
2571 }
2572 outStats->component_2_sample.reserve(stats.component_2_sample.size());
2573 for (const auto& s : stats.component_2_sample) {
2574 outStats->component_2_sample.push_back(static_cast<uint64_t>(s));
2575 }
2576 outStats->component_3_sample.reserve(stats.component_3_sample.size());
2577 for (const auto& s : stats.component_3_sample) {
2578 outStats->component_3_sample.push_back(static_cast<uint64_t>(s));
2579 }
2580 }
2581 return statusTFromBinderStatus(status);
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002582}
Marissa Wall35187b32019-01-08 10:08:52 -08002583
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002584status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
2585 bool* outIsWideColorDisplay) {
Huihong Luo37396db2022-02-15 10:43:00 -08002586 binder::Status status =
2587 ComposerServiceAIDL::getComposerService()->isWideColorDisplay(display,
2588 outIsWideColorDisplay);
Huihong Luo3bdef862022-03-03 11:57:19 -08002589 return statusTFromBinderStatus(status);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002590}
2591
Kevin DuBois00c66832019-02-18 16:21:31 -08002592status_t SurfaceComposerClient::addRegionSamplingListener(
2593 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
2594 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002595 gui::ARect rect;
2596 rect.left = samplingArea.left;
2597 rect.top = samplingArea.top;
2598 rect.right = samplingArea.right;
2599 rect.bottom = samplingArea.bottom;
2600 binder::Status status =
2601 ComposerServiceAIDL::getComposerService()->addRegionSamplingListener(rect,
2602 stopLayerHandle,
2603 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002604 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002605}
2606
2607status_t SurfaceComposerClient::removeRegionSamplingListener(
2608 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002609 binder::Status status =
2610 ComposerServiceAIDL::getComposerService()->removeRegionSamplingListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002611 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002612}
2613
Alec Mouria9a68a62021-03-04 19:14:50 -08002614status_t SurfaceComposerClient::addFpsListener(int32_t taskId,
Alec Mouriadebf5c2021-01-05 12:57:36 -08002615 const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002616 binder::Status status =
2617 ComposerServiceAIDL::getComposerService()->addFpsListener(taskId, listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002618 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08002619}
2620
2621status_t SurfaceComposerClient::removeFpsListener(const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002622 binder::Status status = ComposerServiceAIDL::getComposerService()->removeFpsListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002623 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08002624}
2625
Galia Peycheva8f04b302021-04-27 13:25:38 +02002626status_t SurfaceComposerClient::addTunnelModeEnabledListener(
2627 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002628 binder::Status status =
2629 ComposerServiceAIDL::getComposerService()->addTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002630 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02002631}
2632
2633status_t SurfaceComposerClient::removeTunnelModeEnabledListener(
2634 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002635 binder::Status status =
2636 ComposerServiceAIDL::getComposerService()->removeTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002637 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02002638}
2639
Dan Gittik57e63c52019-01-18 16:37:54 +00002640bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
2641 bool support = false;
Huihong Luo37396db2022-02-15 10:43:00 -08002642 binder::Status status =
2643 ComposerServiceAIDL::getComposerService()->getDisplayBrightnessSupport(displayToken,
2644 &support);
2645 return status.isOk() ? support : false;
Dan Gittik57e63c52019-01-18 16:37:54 +00002646}
2647
2648status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
John Reck22be6962021-03-10 12:59:54 -05002649 const gui::DisplayBrightness& brightness) {
Huihong Luo37396db2022-02-15 10:43:00 -08002650 binder::Status status =
2651 ComposerServiceAIDL::getComposerService()->setDisplayBrightness(displayToken,
2652 brightness);
Huihong Luo3bdef862022-03-03 11:57:19 -08002653 return statusTFromBinderStatus(status);
Dan Gittik57e63c52019-01-18 16:37:54 +00002654}
2655
John Reck88270902021-03-18 11:27:35 -04002656status_t SurfaceComposerClient::addHdrLayerInfoListener(
2657 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08002658 binder::Status status =
2659 ComposerServiceAIDL::getComposerService()->addHdrLayerInfoListener(displayToken,
2660 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002661 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04002662}
2663
2664status_t SurfaceComposerClient::removeHdrLayerInfoListener(
2665 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08002666 binder::Status status =
2667 ComposerServiceAIDL::getComposerService()->removeHdrLayerInfoListener(displayToken,
2668 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002669 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04002670}
2671
Lais Andrade3a6e47d2020-04-02 11:20:16 +01002672status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
Huihong Luo37396db2022-02-15 10:43:00 -08002673 binder::Status status = ComposerServiceAIDL::getComposerService()->notifyPowerBoost(boostId);
Huihong Luo3bdef862022-03-03 11:57:19 -08002674 return statusTFromBinderStatus(status);
Ady Abraham8532d012019-05-08 14:50:56 -07002675}
2676
Vishnu Nairb13bb952019-11-15 10:24:08 -08002677status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
2678 const half4& spotColor, float lightPosY,
2679 float lightPosZ, float lightRadius) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002680 gui::Color ambientColorG, spotColorG;
2681 ambientColorG.r = ambientColor.r;
2682 ambientColorG.g = ambientColor.g;
2683 ambientColorG.b = ambientColor.b;
2684 ambientColorG.a = ambientColor.a;
2685 spotColorG.r = spotColor.r;
2686 spotColorG.g = spotColor.g;
2687 spotColorG.b = spotColor.b;
2688 spotColorG.a = spotColor.a;
2689 binder::Status status =
2690 ComposerServiceAIDL::getComposerService()->setGlobalShadowSettings(ambientColorG,
2691 spotColorG,
2692 lightPosY, lightPosZ,
2693 lightRadius);
2694 return statusTFromBinderStatus(status);
Vishnu Nairb13bb952019-11-15 10:24:08 -08002695}
2696
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05002697std::optional<DisplayDecorationSupport> SurfaceComposerClient::getDisplayDecorationSupport(
2698 const sp<IBinder>& displayToken) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002699 std::optional<gui::DisplayDecorationSupport> gsupport;
2700 binder::Status status =
2701 ComposerServiceAIDL::getComposerService()->getDisplayDecorationSupport(displayToken,
2702 &gsupport);
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05002703 std::optional<DisplayDecorationSupport> support;
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04002704 // TODO (b/241277093): Remove `false && ` once b/241278870 is fixed.
2705 if (false && status.isOk() && gsupport.has_value()) {
2706 support.emplace(DisplayDecorationSupport{
2707 .format =
2708 static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
2709 gsupport->format),
2710 .alphaInterpretation =
Huihong Luo3bdef862022-03-03 11:57:19 -08002711 static_cast<aidl::android::hardware::graphics::common::AlphaInterpretation>(
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04002712 gsupport->alphaInterpretation)
2713 });
Huihong Luo3bdef862022-03-03 11:57:19 -08002714 }
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -05002715 return support;
2716}
2717
Huihong Luo02186fb2022-02-23 14:21:54 -08002718int SurfaceComposerClient::getGpuContextPriority() {
2719 int priority;
2720 binder::Status status =
2721 ComposerServiceAIDL::getComposerService()->getGpuContextPriority(&priority);
2722 if (!status.isOk()) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002723 status_t err = statusTFromBinderStatus(status);
Huihong Luo02186fb2022-02-23 14:21:54 -08002724 ALOGE("getGpuContextPriority failed to read data: %s (%d)", strerror(-err), err);
2725 return 0;
2726 }
2727 return priority;
Ana Krulec31f2b3c2020-12-14 14:30:09 -08002728}
2729
chaviw60c9d3e2021-06-04 12:52:17 -05002730status_t SurfaceComposerClient::addWindowInfosListener(
chaviwa2948ad2022-03-08 11:07:01 -06002731 const sp<WindowInfosListener>& windowInfosListener,
2732 std::pair<std::vector<gui::WindowInfo>, std::vector<gui::DisplayInfo>>* outInitialInfo) {
chaviw60c9d3e2021-06-04 12:52:17 -05002733 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08002734 ->addWindowInfosListener(windowInfosListener, ComposerServiceAIDL::getComposerService(),
chaviwa2948ad2022-03-08 11:07:01 -06002735 outInitialInfo);
chaviw60c9d3e2021-06-04 12:52:17 -05002736}
2737
2738status_t SurfaceComposerClient::removeWindowInfosListener(
2739 const sp<WindowInfosListener>& windowInfosListener) {
2740 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08002741 ->removeWindowInfosListener(windowInfosListener,
2742 ComposerServiceAIDL::getComposerService());
chaviw60c9d3e2021-06-04 12:52:17 -05002743}
2744
Mathias Agopian698c0872011-06-28 19:09:31 -07002745// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002746
chaviw690db382020-07-27 16:46:46 -07002747status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07002748 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08002749 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002750 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07002751
Huihong Luof5029222021-12-16 14:33:46 -08002752 binder::Status status = s->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002753 return statusTFromBinderStatus(status);
Robert Carr673134e2017-01-09 19:48:38 -08002754}
2755
Dominik Laskowskif1833852021-03-23 15:06:50 -07002756status_t ScreenshotClient::captureDisplay(DisplayId displayId,
chaviwe7b9f272020-08-18 16:08:59 -07002757 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08002758 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
chaviw93df2ea2019-04-30 16:45:12 -07002759 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07002760
Huihong Luof5029222021-12-16 14:33:46 -08002761 binder::Status status = s->captureDisplayById(displayId.value, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002762 return statusTFromBinderStatus(status);
chaviw93df2ea2019-04-30 16:45:12 -07002763}
2764
chaviw26c52482020-07-28 16:25:52 -07002765status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07002766 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08002767 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002768 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07002769
Huihong Luof5029222021-12-16 14:33:46 -08002770 binder::Status status = s->captureLayers(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002771 return statusTFromBinderStatus(status);
chaviwa76b2712017-09-20 12:02:26 -07002772}
Dominik Laskowski718f9602019-11-09 20:01:35 -08002773
chaviw69058fb2021-09-27 09:37:30 -05002774// ---------------------------------------------------------------------------------
2775
2776void ReleaseCallbackThread::addReleaseCallback(const ReleaseCallbackId callbackId,
2777 sp<Fence> releaseFence) {
2778 std::scoped_lock<std::mutex> lock(mMutex);
2779 if (!mStarted) {
2780 mThread = std::thread(&ReleaseCallbackThread::threadMain, this);
2781 mStarted = true;
2782 }
2783
2784 mCallbackInfos.emplace(callbackId, std::move(releaseFence));
2785 mReleaseCallbackPending.notify_one();
2786}
2787
2788void ReleaseCallbackThread::threadMain() {
2789 const auto listener = TransactionCompletedListener::getInstance();
2790 std::queue<std::tuple<const ReleaseCallbackId, const sp<Fence>>> callbackInfos;
2791 while (true) {
2792 {
2793 std::unique_lock<std::mutex> lock(mMutex);
2794 callbackInfos = std::move(mCallbackInfos);
2795 mCallbackInfos = {};
2796 }
2797
2798 while (!callbackInfos.empty()) {
2799 auto [callbackId, releaseFence] = callbackInfos.front();
2800 listener->onReleaseBuffer(callbackId, std::move(releaseFence), UINT_MAX);
2801 callbackInfos.pop();
2802 }
2803
2804 {
2805 std::unique_lock<std::mutex> lock(mMutex);
2806 if (mCallbackInfos.size() == 0) {
2807 mReleaseCallbackPending.wait(lock);
2808 }
2809 }
2810 }
2811}
2812
Dominik Laskowski718f9602019-11-09 20:01:35 -08002813} // namespace android