blob: 001d475823e5b357018edc6526483122be9ca600 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
Vishnu Nair9402bb02022-09-28 16:45:06 -070019#include <semaphore.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
Patrick Williams641f7f22022-06-22 19:25:35 +000023#include <android/gui/BnWindowInfosReportedListener.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080024#include <android/gui/DisplayState.h>
Huihong Luod3d8f8e2022-03-08 14:48:46 -080025#include <android/gui/ISurfaceComposerClient.h>
chaviw60c9d3e2021-06-04 12:52:17 -050026#include <android/gui/IWindowInfosListener.h>
Chavi Weingarten076acac2023-01-19 17:20:43 +000027#include <android/gui/TrustedPresentationThresholds.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080028#include <android/os/IInputConstants.h>
Patrick Williams640b7292022-10-11 19:25:06 +000029#include <gui/TraceUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <utils/SortedVector.h>
33#include <utils/String8.h>
34#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070036#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070038#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080039
Michael Wright28f24d02016-07-12 13:30:53 -070040#include <system/graphics.h>
41
Huihong Luo3bdef862022-03-03 11:57:19 -080042#include <gui/AidlStatusUtil.h>
Robert Carr673134e2017-01-09 19:48:38 -080043#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070044#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080046#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070047#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080048#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080049#include <gui/SurfaceComposerClient.h>
chaviw98318de2021-05-19 16:45:23 -050050#include <gui/WindowInfo.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010051#include <private/gui/ParcelUtils.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010052#include <ui/DisplayMode.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080053#include <ui/DisplayState.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010054#include <ui/DynamicDisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Chavi Weingartene0237bb2023-02-06 21:48:32 +000056#include <android-base/thread_annotations.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080057#include <private/gui/ComposerService.h>
Huihong Luof5029222021-12-16 14:33:46 -080058#include <private/gui/ComposerServiceAIDL.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Marissa Wall73411622019-01-25 10:45:41 -080060// This server size should always be smaller than the server cache size
61#define BUFFER_CACHE_MAX_SIZE 64
62
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070064
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050065using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
chaviw98318de2021-05-19 16:45:23 -050066using gui::FocusRequest;
Huihong Luoecc1f902021-11-20 11:55:05 -080067using gui::IRegionSamplingListener;
Chavi Weingarten076acac2023-01-19 17:20:43 +000068using gui::TrustedPresentationThresholds;
chaviw98318de2021-05-19 16:45:23 -050069using gui::WindowInfo;
70using gui::WindowInfoHandle;
chaviw60c9d3e2021-06-04 12:52:17 -050071using gui::WindowInfosListener;
Huihong Luo3bdef862022-03-03 11:57:19 -080072using gui::aidl_utils::statusTFromBinderStatus;
Peiyong Lin9f034472018-03-28 15:29:00 -070073using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian7e27f052010-05-28 14:22:23 -070076ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
Huihong Luof5029222021-12-16 14:33:46 -080077ANDROID_SINGLETON_STATIC_INSTANCE(ComposerServiceAIDL);
Mathias Agopian7e27f052010-05-28 14:22:23 -070078
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080079namespace {
80// Initialize transaction id counter used to generate transaction ids
81std::atomic<uint32_t> idCounter = 0;
82int64_t generateId() {
83 return (((int64_t)getpid()) << 32) | ++idCounter;
84}
Pascal Muetschard29247e52022-11-08 11:33:10 +010085
86void emptyCallback(nsecs_t, const sp<Fence>&, const std::vector<SurfaceControlStats>&) {}
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080087} // namespace
88
Mathias Agopianb7e930d2010-06-01 15:12:58 -070089ComposerService::ComposerService()
90: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070091 Mutex::Autolock _l(mLock);
92 connectLocked();
93}
94
Brian Lindahl9b85a192021-05-10 09:56:33 +000095bool ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070096 const String16 name("SurfaceFlinger");
Brian Lindahl9b85a192021-05-10 09:56:33 +000097 mComposerService = waitForService<ISurfaceComposer>(name);
98 if (mComposerService == nullptr) {
99 return false; // fatal error or permission problem
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700100 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700101
102 // Create the death listener.
103 class DeathObserver : public IBinder::DeathRecipient {
104 ComposerService& mComposerService;
105 virtual void binderDied(const wp<IBinder>& who) {
106 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
107 who.unsafe_get());
108 mComposerService.composerServiceDied();
109 }
110 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700111 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700112 };
113
114 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800115 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Brian Lindahl9b85a192021-05-10 09:56:33 +0000116 return true;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117}
118
Andy McFadden6652b3e2012-09-06 18:45:56 -0700119/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
120 ComposerService& instance = ComposerService::getInstance();
121 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700122 if (instance.mComposerService == nullptr) {
Brian Lindahl9b85a192021-05-10 09:56:33 +0000123 if (ComposerService::getInstance().connectLocked()) {
124 ALOGD("ComposerService reconnected");
125 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700126 }
127 return instance.mComposerService;
128}
129
130void ComposerService::composerServiceDied()
131{
132 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700133 mComposerService = nullptr;
134 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700135}
136
Huihong Luof5029222021-12-16 14:33:46 -0800137ComposerServiceAIDL::ComposerServiceAIDL() : Singleton<ComposerServiceAIDL>() {
138 std::scoped_lock lock(mMutex);
139 connectLocked();
140}
141
142bool ComposerServiceAIDL::connectLocked() {
143 const String16 name("SurfaceFlingerAIDL");
144 mComposerService = waitForService<gui::ISurfaceComposer>(name);
145 if (mComposerService == nullptr) {
146 return false; // fatal error or permission problem
147 }
148
149 // Create the death listener.
150 class DeathObserver : public IBinder::DeathRecipient {
151 ComposerServiceAIDL& mComposerService;
152 virtual void binderDied(const wp<IBinder>& who) {
153 ALOGW("ComposerService aidl remote (surfaceflinger) died [%p]", who.unsafe_get());
154 mComposerService.composerServiceDied();
155 }
156
157 public:
158 explicit DeathObserver(ComposerServiceAIDL& mgr) : mComposerService(mgr) {}
159 };
160
161 mDeathObserver = new DeathObserver(*const_cast<ComposerServiceAIDL*>(this));
162 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
163 return true;
164}
165
166/*static*/ sp<gui::ISurfaceComposer> ComposerServiceAIDL::getComposerService() {
167 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
168 std::scoped_lock lock(instance.mMutex);
169 if (instance.mComposerService == nullptr) {
170 if (ComposerServiceAIDL::getInstance().connectLocked()) {
171 ALOGD("ComposerServiceAIDL reconnected");
Huihong Luo02186fb2022-02-23 14:21:54 -0800172 WindowInfosListenerReporter::getInstance()->reconnect(instance.mComposerService);
Huihong Luof5029222021-12-16 14:33:46 -0800173 }
174 }
175 return instance.mComposerService;
176}
177
178void ComposerServiceAIDL::composerServiceDied() {
179 std::scoped_lock lock(mMutex);
180 mComposerService = nullptr;
181 mDeathObserver = nullptr;
182}
183
Robert Carrfb4d58b2019-01-15 09:21:27 -0800184class DefaultComposerClient: public Singleton<DefaultComposerClient> {
185 Mutex mLock;
186 sp<SurfaceComposerClient> mClient;
187 friend class Singleton<ComposerService>;
188public:
189 static sp<SurfaceComposerClient> getComposerClient() {
190 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
191 Mutex::Autolock _l(dc.mLock);
192 if (dc.mClient == nullptr) {
193 dc.mClient = new SurfaceComposerClient;
194 }
195 return dc.mClient;
196 }
197};
198ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
199
200
201sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
202 return DefaultComposerClient::getComposerClient();
203}
204
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100205JankDataListener::~JankDataListener() {
206}
207
Mathias Agopian7e27f052010-05-28 14:22:23 -0700208// ---------------------------------------------------------------------------
209
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700210// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
211// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
212// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
213
Marissa Wallc837b5e2018-10-12 10:04:44 -0700214// 0 is an invalid callback id
215TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
216
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700217int64_t TransactionCompletedListener::getNextIdLocked() {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700218 return mCallbackIdCounter++;
219}
220
chaviw0b06a8d2021-08-06 11:49:08 -0500221sp<TransactionCompletedListener> TransactionCompletedListener::sInstance = nullptr;
Robert Carr2df7c452022-04-04 12:21:37 -0700222static std::mutex sListenerInstanceMutex;
chaviw0b06a8d2021-08-06 11:49:08 -0500223
224void TransactionCompletedListener::setInstance(const sp<TransactionCompletedListener>& listener) {
225 sInstance = listener;
226}
227
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700228sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
Robert Carr2df7c452022-04-04 12:21:37 -0700229 std::lock_guard<std::mutex> lock(sListenerInstanceMutex);
chaviw0b06a8d2021-08-06 11:49:08 -0500230 if (sInstance == nullptr) {
231 sInstance = new TransactionCompletedListener;
232 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700233 return sInstance;
234}
235
Marissa Wallc837b5e2018-10-12 10:04:44 -0700236sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
237 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
238}
239
240void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700241 if (mListening) {
242 return;
243 }
244 ProcessState::self()->startThreadPool();
245 mListening = true;
246}
247
Marissa Wall80d94ad2019-01-18 16:04:36 -0800248CallbackId TransactionCompletedListener::addCallbackFunction(
249 const TransactionCompletedCallback& callbackFunction,
250 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700251 surfaceControls,
252 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700253 std::lock_guard<std::mutex> lock(mMutex);
Pascal Muetschard29247e52022-11-08 11:33:10 +0100254 return addCallbackFunctionLocked(callbackFunction, surfaceControls, callbackType);
255}
256
257CallbackId TransactionCompletedListener::addCallbackFunctionLocked(
258 const TransactionCompletedCallback& callbackFunction,
259 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
260 surfaceControls,
261 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700262 startListeningLocked();
263
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700264 CallbackId callbackId(getNextIdLocked(), callbackType);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800265 mCallbacks[callbackId].callbackFunction = callbackFunction;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800266 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
267
268 for (const auto& surfaceControl : surfaceControls) {
269 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
Pascal Muetschardf54181b2022-12-13 14:53:25 +0100270
271 if (callbackType == CallbackId::Type::ON_COMPLETE &&
272 mJankListeners.count(surfaceControl->getLayerId()) != 0) {
273 callbackId.includeJankData = true;
274 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800275 }
276
Marissa Wallc837b5e2018-10-12 10:04:44 -0700277 return callbackId;
278}
279
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100280void TransactionCompletedListener::addJankListener(const sp<JankDataListener>& listener,
281 sp<SurfaceControl> surfaceControl) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200282 std::lock_guard<std::mutex> lock(mMutex);
chaviw29bc5362021-09-07 16:17:13 -0500283 mJankListeners.insert({surfaceControl->getLayerId(), listener});
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100284}
285
286void TransactionCompletedListener::removeJankListener(const sp<JankDataListener>& listener) {
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200287 std::lock_guard<std::mutex> lock(mMutex);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100288 for (auto it = mJankListeners.begin(); it != mJankListeners.end();) {
289 if (it->second == listener) {
290 it = mJankListeners.erase(it);
291 } else {
292 it++;
293 }
294 }
295}
296
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700297void TransactionCompletedListener::setReleaseBufferCallback(const ReleaseCallbackId& callbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800298 ReleaseBufferCallback listener) {
299 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700300 mReleaseBufferCallbacks[callbackId] = listener;
Vishnu Nair1506b182021-02-22 14:35:15 -0800301}
302
Jorim Jaggif51775d2021-01-14 23:44:15 +0100303void TransactionCompletedListener::addSurfaceStatsListener(void* context, void* cookie,
304 sp<SurfaceControl> surfaceControl, SurfaceStatsCallback listener) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200305 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500306 mSurfaceStatsListeners.insert(
307 {surfaceControl->getLayerId(), SurfaceStatsCallbackEntry(context, cookie, listener)});
Jorim Jaggif51775d2021-01-14 23:44:15 +0100308}
309
310void TransactionCompletedListener::removeSurfaceStatsListener(void* context, void* cookie) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200311 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100312 for (auto it = mSurfaceStatsListeners.begin(); it != mSurfaceStatsListeners.end();) {
313 auto [itContext, itCookie, itListener] = it->second;
314 if (itContext == context && itCookie == cookie) {
315 it = mSurfaceStatsListeners.erase(it);
316 } else {
317 it++;
318 }
319 }
320}
321
Marissa Wall80d94ad2019-01-18 16:04:36 -0800322void TransactionCompletedListener::addSurfaceControlToCallbacks(
Pascal Muetschard29247e52022-11-08 11:33:10 +0100323 SurfaceComposerClient::CallbackInfo& callbackInfo,
324 const sp<SurfaceControl>& surfaceControl) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800325 std::lock_guard<std::mutex> lock(mMutex);
326
Pascal Muetschard29247e52022-11-08 11:33:10 +0100327 bool includingJankData = false;
328 for (auto callbackId : callbackInfo.callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800329 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
330 std::forward_as_tuple(
331 surfaceControl->getHandle()),
332 std::forward_as_tuple(surfaceControl));
Pascal Muetschard29247e52022-11-08 11:33:10 +0100333 includingJankData = includingJankData || callbackId.includeJankData;
334 }
335
336 // If no registered callback is requesting jank data, but there is a jank listener registered
337 // on the new surface control, add a synthetic callback that requests the jank data.
338 if (!includingJankData && mJankListeners.count(surfaceControl->getLayerId()) != 0) {
339 CallbackId callbackId =
340 addCallbackFunctionLocked(&emptyCallback, callbackInfo.surfaceControls,
341 CallbackId::Type::ON_COMPLETE);
342 callbackInfo.callbackIds.emplace(callbackId);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800343 }
344}
345
Huihong Luoffee3bc2023-01-17 16:14:35 +0000346void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700347 std::unordered_map<CallbackId, CallbackTranslation, CallbackIdHash> callbacksMap;
chaviw29bc5362021-09-07 16:17:13 -0500348 std::multimap<int32_t, sp<JankDataListener>> jankListenersMap;
Valerie Haud3b90d22019-11-06 09:37:31 -0800349 {
350 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700351
Valerie Haud3b90d22019-11-06 09:37:31 -0800352 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
353 * callbackIds, except for when Transactions are merged together. This probably cannot be
354 * solved before this point because the Transactions could be merged together and applied in
355 * a different process.
356 *
357 * Fortunately, we get all the callbacks for this listener for the same frame together at
358 * the same time. This means if any Transactions were merged together, we will get their
359 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
360 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
361 * sp<SurfaceControl> that could possibly exist for the callbacks.
362 */
363 callbacksMap = mCallbacks;
Jorim Jaggi39f4b1c2021-06-23 13:39:41 +0200364 jankListenersMap = mJankListeners;
Valerie Haud3b90d22019-11-06 09:37:31 -0800365 for (const auto& transactionStats : listenerStats.transactionStats) {
366 for (auto& callbackId : transactionStats.callbackIds) {
367 mCallbacks.erase(callbackId);
368 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800369 }
370 }
Marissa Walld600d572019-03-26 15:38:50 -0700371 for (const auto& transactionStats : listenerStats.transactionStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700372 // handle on commit callbacks
Marissa Walld600d572019-03-26 15:38:50 -0700373 for (auto callbackId : transactionStats.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700374 if (callbackId.type != CallbackId::Type::ON_COMMIT) {
375 continue;
376 }
377 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
378 if (!callbackFunction) {
379 ALOGE("cannot call null callback function, skipping");
380 continue;
381 }
382 std::vector<SurfaceControlStats> surfaceControlStats;
383 for (const auto& surfaceStats : transactionStats.surfaceStats) {
384 surfaceControlStats
385 .emplace_back(callbacksMap[callbackId]
386 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800387 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700388 transactionStats.presentFence,
389 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800390 surfaceStats.eventStats,
391 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700392 }
393
394 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
395 surfaceControlStats);
396 }
397
398 // handle on complete callbacks
399 for (auto callbackId : transactionStats.callbackIds) {
400 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
401 continue;
402 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800403 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-18 16:04:36 -0800404 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700405 ALOGE("cannot call null callback function, skipping");
406 continue;
407 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800408 std::vector<SurfaceControlStats> surfaceControlStats;
409 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800410 surfaceControlStats
411 .emplace_back(callbacksMap[callbackId]
412 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800413 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Valerie Hau871d6352020-01-29 08:44:02 -0800414 transactionStats.presentFence,
415 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800416 surfaceStats.eventStats,
417 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair71fcf912022-10-18 09:14:20 -0700418 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl] &&
419 surfaceStats.transformHint.has_value()) {
Valerie Hau84d87ff2020-01-08 17:23:21 -0800420 callbacksMap[callbackId]
421 .surfaceControls[surfaceStats.surfaceControl]
Vishnu Nair71fcf912022-10-18 09:14:20 -0700422 ->setTransformHint(*surfaceStats.transformHint);
Valerie Hau84d87ff2020-01-08 17:23:21 -0800423 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800424 // If there is buffer id set, we look up any pending client release buffer callbacks
425 // and call them. This is a performance optimization when we have a transaction
426 // callback and a release buffer callback happening at the same time to avoid an
427 // additional ipc call from the server.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700428 if (surfaceStats.previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
Vishnu Nair5faedf52021-03-22 11:32:58 -0700429 ReleaseBufferCallback callback;
430 {
431 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700432 callback = popReleaseBufferCallbackLocked(
433 surfaceStats.previousReleaseCallbackId);
Vishnu Nair5faedf52021-03-22 11:32:58 -0700434 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800435 if (callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700436 callback(surfaceStats.previousReleaseCallbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800437 surfaceStats.previousReleaseFence
438 ? surfaceStats.previousReleaseFence
Robert Carr97e7cc02021-06-07 10:45:40 -0700439 : Fence::NO_FENCE,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700440 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800441 }
442 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800443 }
444
445 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
446 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700447 }
chaviw29bc5362021-09-07 16:17:13 -0500448
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100449 for (const auto& surfaceStats : transactionStats.surfaceStats) {
chaviw29bc5362021-09-07 16:17:13 -0500450 // The callbackMap contains the SurfaceControl object, which we need to look up the
451 // layerId. Since we don't know which callback contains the SurfaceControl, iterate
452 // through all until the SC is found.
453 int32_t layerId = -1;
454 for (auto callbackId : transactionStats.callbackIds) {
Pablo Gamito10026472021-11-22 22:16:45 +0100455 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
456 // We only want to run the stats callback for ON_COMPLETE
457 continue;
458 }
chaviw29bc5362021-09-07 16:17:13 -0500459 sp<SurfaceControl> sc =
460 callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl];
461 if (sc != nullptr) {
462 layerId = sc->getLayerId();
463 break;
464 }
465 }
466
Pablo Gamito10026472021-11-22 22:16:45 +0100467 if (layerId != -1) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200468 // Acquire surface stats listener lock such that we guarantee that after calling
469 // unregister, there won't be any further callback.
470 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500471 auto listenerRange = mSurfaceStatsListeners.equal_range(layerId);
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200472 for (auto it = listenerRange.first; it != listenerRange.second; it++) {
473 auto entry = it->second;
474 entry.callback(entry.context, transactionStats.latchTime,
475 transactionStats.presentFence, surfaceStats);
476 }
Jorim Jaggif51775d2021-01-14 23:44:15 +0100477 }
478
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100479 if (surfaceStats.jankData.empty()) continue;
chaviw29bc5362021-09-07 16:17:13 -0500480 auto jankRange = jankListenersMap.equal_range(layerId);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100481 for (auto it = jankRange.first; it != jankRange.second; it++) {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100482 it->second->onJankDataAvailable(surfaceStats.jankData);
483 }
484 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700485 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700486}
487
Huihong Luoffee3bc2023-01-17 16:14:35 +0000488void TransactionCompletedListener::onTransactionQueueStalled(const String8& reason) {
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000489 std::unordered_map<void*, std::function<void(const std::string&)>> callbackCopy;
490 {
491 std::scoped_lock<std::mutex> lock(mMutex);
492 callbackCopy = mQueueStallListeners;
493 }
494 for (auto const& it : callbackCopy) {
495 it.second(reason.c_str());
496 }
Robert Carr4c1b6462021-12-21 10:30:50 -0800497}
498
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000499void TransactionCompletedListener::addQueueStallListener(
500 std::function<void(const std::string&)> stallListener, void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800501 std::scoped_lock<std::mutex> lock(mMutex);
502 mQueueStallListeners[id] = stallListener;
503}
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000504
505void TransactionCompletedListener::removeQueueStallListener(void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800506 std::scoped_lock<std::mutex> lock(mMutex);
507 mQueueStallListeners.erase(id);
508}
509
Huihong Luoffee3bc2023-01-17 16:14:35 +0000510void TransactionCompletedListener::onReleaseBuffer(ReleaseCallbackId callbackId,
511 sp<Fence> releaseFence,
512 uint32_t currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800513 ReleaseBufferCallback callback;
514 {
515 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700516 callback = popReleaseBufferCallbackLocked(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800517 }
518 if (!callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700519 ALOGE("Could not call release buffer callback, buffer not found %s",
520 callbackId.to_string().c_str());
Huihong Luoffee3bc2023-01-17 16:14:35 +0000521 return;
Vishnu Nair1506b182021-02-22 14:35:15 -0800522 }
chaviw69058fb2021-09-27 09:37:30 -0500523 std::optional<uint32_t> optionalMaxAcquiredBufferCount =
Huihong Luoffee3bc2023-01-17 16:14:35 +0000524 currentMaxAcquiredBufferCount == UINT_MAX
chaviw69058fb2021-09-27 09:37:30 -0500525 ? std::nullopt
526 : std::make_optional<uint32_t>(currentMaxAcquiredBufferCount);
527 callback(callbackId, releaseFence, optionalMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800528}
529
530ReleaseBufferCallback TransactionCompletedListener::popReleaseBufferCallbackLocked(
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700531 const ReleaseCallbackId& callbackId) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800532 ReleaseBufferCallback callback;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700533 auto itr = mReleaseBufferCallbacks.find(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800534 if (itr == mReleaseBufferCallbacks.end()) {
535 return nullptr;
536 }
537 callback = itr->second;
538 mReleaseBufferCallbacks.erase(itr);
539 return callback;
540}
541
chaviw0acd33a2021-11-02 11:55:37 -0500542void TransactionCompletedListener::removeReleaseBufferCallback(
543 const ReleaseCallbackId& callbackId) {
544 {
545 std::scoped_lock<std::mutex> lock(mMutex);
546 popReleaseBufferCallbackLocked(callbackId);
547 }
548}
549
Chavi Weingarten076acac2023-01-19 17:20:43 +0000550SurfaceComposerClient::PresentationCallbackRAII::PresentationCallbackRAII(
551 TransactionCompletedListener* tcl, int id) {
552 mTcl = tcl;
553 mId = id;
554}
555
556SurfaceComposerClient::PresentationCallbackRAII::~PresentationCallbackRAII() {
557 mTcl->clearTrustedPresentationCallback(mId);
558}
559
560sp<SurfaceComposerClient::PresentationCallbackRAII>
561TransactionCompletedListener::addTrustedPresentationCallback(TrustedPresentationCallback tpc,
562 int id, void* context) {
563 std::scoped_lock<std::mutex> lock(mMutex);
564 mTrustedPresentationCallbacks[id] =
565 std::tuple<TrustedPresentationCallback, void*>(tpc, context);
566 return new SurfaceComposerClient::PresentationCallbackRAII(this, id);
567}
568
569void TransactionCompletedListener::clearTrustedPresentationCallback(int id) {
570 std::scoped_lock<std::mutex> lock(mMutex);
571 mTrustedPresentationCallbacks.erase(id);
572}
573
574void TransactionCompletedListener::onTrustedPresentationChanged(int id,
575 bool presentedWithinThresholds) {
576 TrustedPresentationCallback tpc;
577 void* context;
578 {
579 std::scoped_lock<std::mutex> lock(mMutex);
580 auto it = mTrustedPresentationCallbacks.find(id);
581 if (it == mTrustedPresentationCallbacks.end()) {
582 return;
583 }
584 std::tie(tpc, context) = it->second;
585 }
586 tpc(context, presentedWithinThresholds);
587}
588
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700589// ---------------------------------------------------------------------------
590
Robert Carre06ad2b2020-04-10 15:09:33 -0700591void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 14:58:34 -0700592
Robert Carre06ad2b2020-04-10 15:09:33 -0700593/**
594 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
595 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
596 * 1. Cost of sending the FD
597 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
598 * To ease this cost we implement the following scheme of caching buffers to integers,
599 * or said-otherwise, naming them with integers. This is the scheme known as slots in
600 * the legacy BufferQueue system.
601 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
602 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
603 * send the cached integer.
604 * 3. If there is a cache miss, we cache the new buffer and send the integer
605 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
606 * entry, and we use the integer for further communication.
607 * A few details about lifetime:
608 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
609 * which is per process Unique. The server side cache is larger than the client side
610 * cache so that the server will never evict entries before the client.
611 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
612 * transaction.
613 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
614 * to auto-evict destroyed buffers.
615 */
Marissa Wall73411622019-01-25 10:45:41 -0800616class BufferCache : public Singleton<BufferCache> {
617public:
618 BufferCache() : token(new BBinder()) {}
619
620 sp<IBinder> getToken() {
621 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
622 }
623
Marissa Wall78b72202019-03-15 14:58:34 -0700624 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800625 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800626
Marissa Wall78b72202019-03-15 14:58:34 -0700627 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800628 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700629 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800630 }
Marissa Wall78b72202019-03-15 14:58:34 -0700631 itr->second = getCounter();
632 *cacheId = buffer->getId();
633 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800634 }
635
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000636 uint64_t cache(const sp<GraphicBuffer>& buffer,
637 std::optional<client_cache_t>& outUncacheBuffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800638 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800639
Marissa Wall78b72202019-03-15 14:58:34 -0700640 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000641 outUncacheBuffer = findLeastRecentlyUsedBuffer();
642 mBuffers.erase(outUncacheBuffer->id);
Marissa Wall78b72202019-03-15 14:58:34 -0700643 }
Marissa Wall73411622019-01-25 10:45:41 -0800644
Robert Carre06ad2b2020-04-10 15:09:33 -0700645 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 14:58:34 -0700646
647 mBuffers[buffer->getId()] = getCounter();
648 return buffer->getId();
649 }
650
651 void uncache(uint64_t cacheId) {
652 std::lock_guard<std::mutex> lock(mMutex);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000653 if (mBuffers.erase(cacheId)) {
654 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
655 }
Marissa Wall73411622019-01-25 10:45:41 -0800656 }
657
658private:
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000659 client_cache_t findLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800660 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700661 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800662 auto minBuffer = itr;
663 itr++;
664
665 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700666 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800667 if (counter < minCounter) {
668 minCounter = counter;
669 minBuffer = itr;
670 }
671 itr++;
672 }
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000673
674 return {.token = getToken(), .id = minBuffer->first};
Marissa Wall73411622019-01-25 10:45:41 -0800675 }
676
677 uint64_t getCounter() REQUIRES(mMutex) {
678 static uint64_t counter = 0;
679 return counter++;
680 }
681
Marissa Wall73411622019-01-25 10:45:41 -0800682 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700683 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800684
685 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
686 sp<IBinder> token;
687};
688
689ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
690
Robert Carre06ad2b2020-04-10 15:09:33 -0700691void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 14:58:34 -0700692 // GraphicBuffer id's are used as the cache ids.
693 BufferCache::getInstance().uncache(graphicBufferId);
694}
695
Marissa Wall73411622019-01-25 10:45:41 -0800696// ---------------------------------------------------------------------------
697
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000698SurfaceComposerClient::Transaction::Transaction() {
699 mId = generateId();
700}
701
Marissa Wall17b4e452018-12-26 16:32:34 -0800702SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000703 : mId(other.mId),
Marissa Wall17b4e452018-12-26 16:32:34 -0800704 mTransactionNestCount(other.mTransactionNestCount),
705 mAnimation(other.mAnimation),
Ady Abraham8cbd3072021-03-15 16:39:06 -0700706 mEarlyWakeupStart(other.mEarlyWakeupStart),
707 mEarlyWakeupEnd(other.mEarlyWakeupEnd),
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000708 mMayContainBuffer(other.mMayContainBuffer),
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700709 mDesiredPresentTime(other.mDesiredPresentTime),
Ady Abrahamf0c56492020-12-17 18:04:15 -0800710 mIsAutoTimestamp(other.mIsAutoTimestamp),
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000711 mFrameTimelineInfo(other.mFrameTimelineInfo),
Patrick Williamse5991f52022-06-23 20:23:43 +0000712 mApplyToken(other.mApplyToken) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700713 mDisplayStates = other.mDisplayStates;
714 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800715 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700716 mListenerCallbacks = other.mListenerCallbacks;
717}
718
Robert Carrde6d7b42022-01-07 18:23:06 -0800719void SurfaceComposerClient::Transaction::sanitize() {
720 for (auto & [handle, composerState] : mComposerStates) {
721 composerState.state.sanitize(0 /* permissionMask */);
722 }
723 mInputWindowCommands.clear();
724}
725
Vishnu Nair621102e2019-06-12 14:16:57 -0700726std::unique_ptr<SurfaceComposerClient::Transaction>
727SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
728 auto transaction = std::make_unique<Transaction>();
729 if (transaction->readFromParcel(parcel) == NO_ERROR) {
730 return transaction;
731 }
732 return nullptr;
733}
734
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000735
Vishnu Nair621102e2019-06-12 14:16:57 -0700736status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
Pablo Gamito825df7f2022-06-23 14:45:44 +0000737 const uint64_t transactionId = parcel->readUint64();
Vishnu Nair621102e2019-06-12 14:16:57 -0700738 const uint32_t transactionNestCount = parcel->readUint32();
739 const bool animation = parcel->readBool();
Ady Abraham8cbd3072021-03-15 16:39:06 -0700740 const bool earlyWakeupStart = parcel->readBool();
741 const bool earlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 14:16:57 -0700742 const int64_t desiredPresentTime = parcel->readInt64();
Ady Abrahamf0c56492020-12-17 18:04:15 -0800743 const bool isAutoTimestamp = parcel->readBool();
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000744 FrameTimelineInfo frameTimelineInfo;
Huihong Luo3bdef862022-03-03 11:57:19 -0800745 frameTimelineInfo.readFromParcel(parcel);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000746
Vishnu Nair277142c2021-01-05 18:35:29 -0800747 sp<IBinder> applyToken;
748 parcel->readNullableStrongBinder(&applyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700749 size_t count = static_cast<size_t>(parcel->readUint32());
750 if (count > parcel->dataSize()) {
751 return BAD_VALUE;
752 }
753 SortedVector<DisplayState> displayStates;
754 displayStates.setCapacity(count);
755 for (size_t i = 0; i < count; i++) {
756 DisplayState displayState;
757 if (displayState.read(*parcel) == BAD_VALUE) {
758 return BAD_VALUE;
759 }
760 displayStates.add(displayState);
761 }
762
763 count = static_cast<size_t>(parcel->readUint32());
764 if (count > parcel->dataSize()) {
765 return BAD_VALUE;
766 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700767 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
768 listenerCallbacks.reserve(count);
769 for (size_t i = 0; i < count; i++) {
770 sp<ITransactionCompletedListener> listener =
771 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
772 size_t numCallbackIds = parcel->readUint32();
773 if (numCallbackIds > parcel->dataSize()) {
774 return BAD_VALUE;
775 }
776 for (size_t j = 0; j < numCallbackIds; j++) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700777 CallbackId id;
778 parcel->readParcelable(&id);
779 listenerCallbacks[listener].callbackIds.insert(id);
Valerie Hau9dab9732019-08-20 09:29:25 -0700780 }
781 size_t numSurfaces = parcel->readUint32();
782 if (numSurfaces > parcel->dataSize()) {
783 return BAD_VALUE;
784 }
785 for (size_t j = 0; j < numSurfaces; j++) {
786 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000787 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 09:29:25 -0700788 listenerCallbacks[listener].surfaceControls.insert(surface);
789 }
790 }
791
792 count = static_cast<size_t>(parcel->readUint32());
793 if (count > parcel->dataSize()) {
794 return BAD_VALUE;
795 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700796 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 14:16:57 -0700797 composerStates.reserve(count);
798 for (size_t i = 0; i < count; i++) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000799 sp<IBinder> surfaceControlHandle;
800 SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700801
802 ComposerState composerState;
803 if (composerState.read(*parcel) == BAD_VALUE) {
804 return BAD_VALUE;
805 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700806 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 14:16:57 -0700807 }
808
809 InputWindowCommands inputWindowCommands;
810 inputWindowCommands.read(*parcel);
811
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000812 count = static_cast<size_t>(parcel->readUint32());
813 if (count > parcel->dataSize()) {
814 return BAD_VALUE;
815 }
816 std::vector<client_cache_t> uncacheBuffers(count);
817 for (size_t i = 0; i < count; i++) {
818 sp<IBinder> tmpBinder;
819 SAFE_PARCEL(parcel->readStrongBinder, &tmpBinder);
820 uncacheBuffers[i].token = tmpBinder;
821 SAFE_PARCEL(parcel->readUint64, &uncacheBuffers[i].id);
822 }
823
Vishnu Nair621102e2019-06-12 14:16:57 -0700824 // Parsing was successful. Update the object.
Pablo Gamito825df7f2022-06-23 14:45:44 +0000825 mId = transactionId;
Vishnu Nair621102e2019-06-12 14:16:57 -0700826 mTransactionNestCount = transactionNestCount;
827 mAnimation = animation;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700828 mEarlyWakeupStart = earlyWakeupStart;
829 mEarlyWakeupEnd = earlyWakeupEnd;
Vishnu Nair621102e2019-06-12 14:16:57 -0700830 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800831 mIsAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000832 mFrameTimelineInfo = frameTimelineInfo;
Vishnu Nair621102e2019-06-12 14:16:57 -0700833 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 09:29:25 -0700834 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 14:16:57 -0700835 mComposerStates = composerStates;
836 mInputWindowCommands = inputWindowCommands;
Vishnu Nair277142c2021-01-05 18:35:29 -0800837 mApplyToken = applyToken;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000838 mUncacheBuffers = std::move(uncacheBuffers);
Vishnu Nair621102e2019-06-12 14:16:57 -0700839 return NO_ERROR;
840}
841
842status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700843 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
844 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
845 // but is unlikely to use them again as they are owned by the other process.
846 // You may be asking yourself, is this const cast safe? Const cast is safe up
847 // until the point where you try and write to an object that was originally const at which
848 // point we enter undefined behavior. In this case we are safe though, because there are
849 // two possibilities:
850 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
851 // 2. It was originall const! In this case not only was it useless, but it by definition
852 // contains no composer states and so cacheBuffers will not perform any writes.
853
854 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
855
Pablo Gamito825df7f2022-06-23 14:45:44 +0000856 parcel->writeUint64(mId);
Vishnu Nair621102e2019-06-12 14:16:57 -0700857 parcel->writeUint32(mTransactionNestCount);
858 parcel->writeBool(mAnimation);
Ady Abraham8cbd3072021-03-15 16:39:06 -0700859 parcel->writeBool(mEarlyWakeupStart);
860 parcel->writeBool(mEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 14:16:57 -0700861 parcel->writeInt64(mDesiredPresentTime);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800862 parcel->writeBool(mIsAutoTimestamp);
Huihong Luo3bdef862022-03-03 11:57:19 -0800863 mFrameTimelineInfo.writeToParcel(parcel);
Vishnu Nair277142c2021-01-05 18:35:29 -0800864 parcel->writeStrongBinder(mApplyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700865 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
866 for (auto const& displayState : mDisplayStates) {
867 displayState.write(*parcel);
868 }
869
Valerie Hau9dab9732019-08-20 09:29:25 -0700870 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
871 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
872 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
873 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
874 for (auto callbackId : callbackInfo.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700875 parcel->writeParcelable(callbackId);
Valerie Hau9dab9732019-08-20 09:29:25 -0700876 }
877 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
878 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45 +0000879 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 09:29:25 -0700880 }
881 }
882
Vishnu Nair621102e2019-06-12 14:16:57 -0700883 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000884 for (auto const& [handle, composerState] : mComposerStates) {
885 SAFE_PARCEL(parcel->writeStrongBinder, handle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700886 composerState.write(*parcel);
887 }
888
889 mInputWindowCommands.write(*parcel);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000890
891 SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mUncacheBuffers.size()));
892 for (const client_cache_t& uncacheBuffer : mUncacheBuffers) {
893 SAFE_PARCEL(parcel->writeStrongBinder, uncacheBuffer.token.promote());
894 SAFE_PARCEL(parcel->writeUint64, uncacheBuffer.id);
895 }
896
Vishnu Nair621102e2019-06-12 14:16:57 -0700897 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -0700898}
899
chaviw69058fb2021-09-27 09:37:30 -0500900void SurfaceComposerClient::Transaction::releaseBufferIfOverwriting(const layer_state_t& state) {
901 if (!(state.what & layer_state_t::eBufferChanged)) {
902 return;
903 }
904
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800905 auto listener = state.bufferData->releaseBufferListener;
chaviw69058fb2021-09-27 09:37:30 -0500906 sp<Fence> fence =
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800907 state.bufferData->acquireFence ? state.bufferData->acquireFence : Fence::NO_FENCE;
908 if (state.bufferData->releaseBufferEndpoint ==
chaviw69058fb2021-09-27 09:37:30 -0500909 IInterface::asBinder(TransactionCompletedListener::getIInstance())) {
910 // if the callback is in process, run on a different thread to avoid any lock contigency
911 // issues in the client.
912 SurfaceComposerClient::getDefault()
chaviw8dd181f2022-01-05 18:36:46 -0600913 ->mReleaseCallbackThread
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800914 .addReleaseCallback(state.bufferData->generateReleaseCallbackId(), fence);
chaviw69058fb2021-09-27 09:37:30 -0500915 } else {
Huihong Luoffee3bc2023-01-17 16:14:35 +0000916 listener->onReleaseBuffer(state.bufferData->generateReleaseCallbackId(), fence, UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -0500917 }
918}
919
Robert Carr2c5f6d22017-09-26 12:30:35 -0700920SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000921 for (auto const& [handle, composerState] : other.mComposerStates) {
922 if (mComposerStates.count(handle) == 0) {
923 mComposerStates[handle] = composerState;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700924 } else {
chaviw69058fb2021-09-27 09:37:30 -0500925 if (composerState.state.what & layer_state_t::eBufferChanged) {
926 releaseBufferIfOverwriting(mComposerStates[handle].state);
927 }
Pablo Gamitodbc31672020-09-01 18:28:58 +0000928 mComposerStates[handle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700929 }
930 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700931
932 for (auto const& state : other.mDisplayStates) {
933 ssize_t index = mDisplayStates.indexOf(state);
934 if (index < 0) {
935 mDisplayStates.add(state);
936 } else {
937 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
938 }
939 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700940
Marissa Wallc837b5e2018-10-12 10:04:44 -0700941 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
942 auto& [callbackIds, surfaceControls] = callbackInfo;
943 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
944 callbackIds.begin()),
945 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -0700946
Valerie Hau236eba32020-01-03 16:53:39 -0800947 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
948 surfaceControls.end());
949
950 auto& currentProcessCallbackInfo =
951 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
952 currentProcessCallbackInfo.surfaceControls
953 .insert(std::make_move_iterator(surfaceControls.begin()),
954 std::make_move_iterator(surfaceControls.end()));
955
956 // register all surface controls for all callbackIds for this listener that is merging
957 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
958 TransactionCompletedListener::getInstance()
Pascal Muetschard29247e52022-11-08 11:33:10 +0100959 ->addSurfaceControlToCallbacks(currentProcessCallbackInfo, surfaceControl);
Valerie Hau236eba32020-01-03 16:53:39 -0800960 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700961 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700962
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000963 for (const auto& cacheId : other.mUncacheBuffers) {
964 mUncacheBuffers.push_back(cacheId);
965 }
966
chaviw273171b2018-12-26 11:46:30 -0800967 mInputWindowCommands.merge(other.mInputWindowCommands);
968
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000969 mMayContainBuffer |= other.mMayContainBuffer;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700970 mEarlyWakeupStart = mEarlyWakeupStart || other.mEarlyWakeupStart;
971 mEarlyWakeupEnd = mEarlyWakeupEnd || other.mEarlyWakeupEnd;
Vishnu Nair277142c2021-01-05 18:35:29 -0800972 mApplyToken = other.mApplyToken;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700973
Huihong Luo3bdef862022-03-03 11:57:19 -0800974 mergeFrameTimelineInfo(mFrameTimelineInfo, other.mFrameTimelineInfo);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700975
Vishnu Nairfef244e2019-06-17 18:07:51 -0700976 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -0700977 return *this;
978}
979
Vishnu Nairfef244e2019-06-17 18:07:51 -0700980void SurfaceComposerClient::Transaction::clear() {
981 mComposerStates.clear();
982 mDisplayStates.clear();
983 mListenerCallbacks.clear();
984 mInputWindowCommands.clear();
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000985 mUncacheBuffers.clear();
Chavi Weingarten1ac2a922022-09-15 20:34:48 +0000986 mMayContainBuffer = false;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700987 mTransactionNestCount = 0;
988 mAnimation = false;
Ady Abraham8cbd3072021-03-15 16:39:06 -0700989 mEarlyWakeupStart = false;
990 mEarlyWakeupEnd = false;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800991 mDesiredPresentTime = 0;
992 mIsAutoTimestamp = true;
Huihong Luo3bdef862022-03-03 11:57:19 -0800993 clearFrameTimelineInfo(mFrameTimelineInfo);
Vishnu Nair277142c2021-01-05 18:35:29 -0800994 mApplyToken = nullptr;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700995}
996
Pablo Gamito3e4e0be2022-05-27 17:26:24 +0000997uint64_t SurfaceComposerClient::Transaction::getId() {
998 return mId;
999}
1000
Marissa Wall78b72202019-03-15 14:58:34 -07001001void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
1002 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1003
Marissa Wall947d34e2019-03-29 14:03:53 -07001004 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -07001005 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -07001006 uncacheBuffer.id = cacheId;
Vishnu Nair40fff5c2022-11-04 02:46:28 +00001007 Vector<ComposerState> composerStates;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001008 status_t status = sf->setTransactionState(FrameTimelineInfo{}, composerStates, {},
1009 ISurfaceComposer::eOneWay,
1010 Transaction::getDefaultApplyToken(), {}, systemTime(),
1011 true, {uncacheBuffer}, false, {}, generateId());
Patrick Williams640b7292022-10-11 19:25:06 +00001012 if (status != NO_ERROR) {
1013 ALOGE_AND_TRACE("SurfaceComposerClient::doUncacheBufferTransaction - %s",
1014 strerror(-status));
1015 }
Marissa Wall78b72202019-03-15 14:58:34 -07001016}
1017
1018void SurfaceComposerClient::Transaction::cacheBuffers() {
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001019 if (!mMayContainBuffer) {
Marissa Wall78b72202019-03-15 14:58:34 -07001020 return;
1021 }
1022
1023 size_t count = 0;
Vishnu Nairf03652d2019-07-16 17:56:56 -07001024 for (auto& [handle, cs] : mComposerStates) {
Pablo Gamitodbc31672020-09-01 18:28:58 +00001025 layer_state_t* s = &(mComposerStates[handle].state);
Marissa Wall78b72202019-03-15 14:58:34 -07001026 if (!(s->what & layer_state_t::eBufferChanged)) {
1027 continue;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001028 } else if (s->bufferData &&
1029 s->bufferData->flags.test(BufferData::BufferDataChange::cachedBufferChanged)) {
Robert Carr28037922020-04-08 10:57:07 -07001030 // If eBufferChanged and eCachedBufferChanged are both trued then that means
1031 // we already cached the buffer in a previous call to cacheBuffers, perhaps
1032 // from writeToParcel on a Transaction that was merged in to this one.
1033 continue;
Marissa Wall78b72202019-03-15 14:58:34 -07001034 }
1035
Marissa Wall00597242019-03-27 10:35:19 -07001036 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
1037 // time trying to cache them.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001038 if (!s->bufferData || !s->bufferData->buffer) {
Marissa Wall00597242019-03-27 10:35:19 -07001039 continue;
1040 }
1041
Marissa Wall78b72202019-03-15 14:58:34 -07001042 uint64_t cacheId = 0;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001043 status_t ret = BufferCache::getInstance().getCacheId(s->bufferData->buffer, &cacheId);
Marissa Wall78b72202019-03-15 14:58:34 -07001044 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -07001045 // Cache-hit. Strip the buffer and send only the id.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001046 s->bufferData->buffer = nullptr;
Marissa Wall78b72202019-03-15 14:58:34 -07001047 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -07001048 // Cache-miss. Include the buffer and send the new cacheId.
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001049 std::optional<client_cache_t> uncacheBuffer;
1050 cacheId = BufferCache::getInstance().cache(s->bufferData->buffer, uncacheBuffer);
1051 if (uncacheBuffer) {
1052 mUncacheBuffers.push_back(*uncacheBuffer);
1053 }
Marissa Wall78b72202019-03-15 14:58:34 -07001054 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001055 s->bufferData->flags |= BufferData::BufferDataChange::cachedBufferChanged;
1056 s->bufferData->cachedBuffer.token = BufferCache::getInstance().getToken();
1057 s->bufferData->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -07001058
1059 // If we have more buffers than the size of the cache, we should stop caching so we don't
1060 // evict other buffers in this transaction
1061 count++;
1062 if (count >= BUFFER_CACHE_MAX_SIZE) {
1063 break;
1064 }
1065 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001066}
1067
Vishnu Nair9402bb02022-09-28 16:45:06 -07001068class SyncCallback {
1069public:
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001070 static auto getCallback(std::shared_ptr<SyncCallback>& callbackContext) {
1071 return [callbackContext](void* /* unused context */, nsecs_t /* latchTime */,
1072 const sp<Fence>& /* presentFence */,
1073 const std::vector<SurfaceControlStats>& /* stats */) {
1074 if (!callbackContext) {
1075 ALOGE("failed to get callback context for SyncCallback");
1076 return;
1077 }
1078 LOG_ALWAYS_FATAL_IF(sem_post(&callbackContext->mSemaphore), "sem_post failed");
1079 };
Vishnu Nair9402bb02022-09-28 16:45:06 -07001080 }
1081 ~SyncCallback() {
1082 if (mInitialized) {
1083 LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
1084 }
1085 }
1086 void init() {
1087 LOG_ALWAYS_FATAL_IF(clock_gettime(CLOCK_MONOTONIC, &mTimeoutTimespec) == -1,
1088 "clock_gettime() fail! in SyncCallback::init");
1089 mTimeoutTimespec.tv_sec += 4;
1090 LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed");
1091 mInitialized = true;
1092 }
1093 void wait() {
1094 int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &mTimeoutTimespec);
1095 if (result && errno != ETIMEDOUT && errno != EINTR) {
1096 LOG_ALWAYS_FATAL("sem_clockwait failed(%d)", errno);
1097 } else if (errno == ETIMEDOUT) {
1098 ALOGW("Sync transaction timed out waiting for commit callback.");
1099 }
1100 }
1101 void* getContext() { return static_cast<void*>(this); }
1102
1103private:
1104 sem_t mSemaphore;
1105 bool mInitialized = false;
1106 timespec mTimeoutTimespec;
1107};
1108
Robert Carr79dc06a2022-02-22 15:28:59 -08001109status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001110 if (mStatus != NO_ERROR) {
1111 return mStatus;
1112 }
1113
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001114 std::shared_ptr<SyncCallback> syncCallback = std::make_shared<SyncCallback>();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001115 if (synchronous) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001116 syncCallback->init();
1117 addTransactionCommittedCallback(SyncCallback::getCallback(syncCallback),
1118 /*callbackContext=*/nullptr);
Vishnu Nair9402bb02022-09-28 16:45:06 -07001119 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001120
Valerie Hau9dab9732019-08-20 09:29:25 -07001121 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 14:03:19 -07001122 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001123 // For every listener with registered callbacks
1124 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
1125 auto& [callbackIds, surfaceControls] = callbackInfo;
1126 if (callbackIds.empty()) {
1127 continue;
1128 }
1129
Valerie Hau9dab9732019-08-20 09:29:25 -07001130 if (surfaceControls.empty()) {
1131 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
1132 } else {
1133 // If the listener has any SurfaceControls set on this Transaction update the surface
1134 // state
1135 for (const auto& surfaceControl : surfaceControls) {
1136 layer_state_t* s = getLayerState(surfaceControl);
1137 if (!s) {
1138 ALOGE("failed to get layer state");
1139 continue;
1140 }
1141 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
1142 s->what |= layer_state_t::eHasListenerCallbacksChanged;
1143 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001144 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001145 }
1146 }
Valerie Hau9dab9732019-08-20 09:29:25 -07001147
Marissa Wall78b72202019-03-15 14:58:34 -07001148 cacheBuffers();
1149
Robert Carr4cdc58f2017-08-23 14:22:20 -07001150 Vector<ComposerState> composerStates;
1151 Vector<DisplayState> displayStates;
1152 uint32_t flags = 0;
1153
Vishnu Nair9402bb02022-09-28 16:45:06 -07001154 for (auto const& kv : mComposerStates) {
chaviw8e3fe5d2018-02-22 10:55:42 -08001155 composerStates.add(kv.second);
1156 }
1157
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001158 displayStates = std::move(mDisplayStates);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001159
Robert Carr4cdc58f2017-08-23 14:22:20 -07001160 if (mAnimation) {
1161 flags |= ISurfaceComposer::eAnimation;
1162 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001163 if (oneWay) {
Vishnu Nair2709efe2022-09-28 17:17:08 -07001164 if (synchronous) {
1165 ALOGE("Transaction attempted to set synchronous and one way at the same time"
1166 " this is an invalid request. Synchronous will win for safety");
1167 } else {
1168 flags |= ISurfaceComposer::eOneWay;
1169 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001170 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001171
Ady Abraham8cbd3072021-03-15 16:39:06 -07001172 // If both mEarlyWakeupStart and mEarlyWakeupEnd are set
Ady Abrahambf1349c2020-06-12 14:26:18 -07001173 // it is equivalent for none
Ady Abraham8cbd3072021-03-15 16:39:06 -07001174 if (mEarlyWakeupStart && !mEarlyWakeupEnd) {
1175 flags |= ISurfaceComposer::eEarlyWakeupStart;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001176 }
Ady Abraham8cbd3072021-03-15 16:39:06 -07001177 if (mEarlyWakeupEnd && !mEarlyWakeupStart) {
1178 flags |= ISurfaceComposer::eEarlyWakeupEnd;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001179 }
1180
Vishnu Nair80da86b2022-08-11 16:32:58 -07001181 sp<IBinder> applyToken = mApplyToken ? mApplyToken : sApplyToken;
Vishnu Nair277142c2021-01-05 18:35:29 -08001182
Vishnu Nair9402bb02022-09-28 16:45:06 -07001183 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001184 sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
Ady Abrahamf0c56492020-12-17 18:04:15 -08001185 mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001186 mUncacheBuffers, hasListenerCallbacks, listenerCallbacks, mId);
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001187 mId = generateId();
1188
1189 // Clear the current states and flags
1190 clear();
1191
Vishnu Nair9402bb02022-09-28 16:45:06 -07001192 if (synchronous) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001193 syncCallback->wait();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001194 }
1195
Robert Carr4cdc58f2017-08-23 14:22:20 -07001196 mStatus = NO_ERROR;
1197 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001198}
1199
Vishnu Nair80da86b2022-08-11 16:32:58 -07001200sp<IBinder> SurfaceComposerClient::Transaction::sApplyToken = new BBinder();
1201
1202sp<IBinder> SurfaceComposerClient::Transaction::getDefaultApplyToken() {
1203 return sApplyToken;
1204}
1205
1206void SurfaceComposerClient::Transaction::setDefaultApplyToken(sp<IBinder> applyToken) {
1207 sApplyToken = applyToken;
1208}
Pascal Muetschardb39918f2023-01-27 11:36:11 +01001209
1210status_t SurfaceComposerClient::Transaction::sendSurfaceFlushJankDataTransaction(
1211 const sp<SurfaceControl>& sc) {
1212 Transaction t;
1213 layer_state_t* s = t.getLayerState(sc);
1214 if (!s) {
1215 return BAD_INDEX;
1216 }
1217
1218 s->what |= layer_state_t::eFlushJankData;
1219 t.registerSurfaceControlForCallback(sc);
1220 return t.apply(/*sync=*/false, /* oneWay=*/true);
1221}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222// ---------------------------------------------------------------------------
1223
Huihong Luo1768cb02022-10-11 11:10:34 -07001224sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure,
1225 float requestedRefereshRate) {
Huihong Luo07e72362022-02-14 14:26:04 -08001226 sp<IBinder> display = nullptr;
1227 binder::Status status =
1228 ComposerServiceAIDL::getComposerService()->createDisplay(std::string(
1229 displayName.string()),
Huihong Luo1768cb02022-10-11 11:10:34 -07001230 secure, requestedRefereshRate,
1231 &display);
Huihong Luo07e72362022-02-14 14:26:04 -08001232 return status.isOk() ? display : nullptr;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001233}
1234
Robert Carr4cdc58f2017-08-23 14:22:20 -07001235void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Huihong Luo07e72362022-02-14 14:26:04 -08001236 ComposerServiceAIDL::getComposerService()->destroyDisplay(display);
Jesse Hall6c913be2013-08-08 12:15:49 -07001237}
1238
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001239std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
Huihong Luo07e72362022-02-14 14:26:04 -08001240 std::vector<int64_t> displayIds;
1241 std::vector<PhysicalDisplayId> physicalDisplayIds;
1242 binder::Status status =
1243 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayIds(&displayIds);
1244 if (status.isOk()) {
1245 physicalDisplayIds.reserve(displayIds.size());
1246 for (auto item : displayIds) {
1247 auto id = DisplayId::fromValue<PhysicalDisplayId>(static_cast<uint64_t>(item));
1248 physicalDisplayIds.push_back(*id);
1249 }
1250 }
1251 return physicalDisplayIds;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001252}
1253
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001254sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
Huihong Luo07e72362022-02-14 14:26:04 -08001255 sp<IBinder> display = nullptr;
1256 binder::Status status =
1257 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayToken(displayId.value,
1258 &display);
1259 return status.isOk() ? display : nullptr;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001260}
1261
Robert Carr4cdc58f2017-08-23 14:22:20 -07001262void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001263 mAnimation = true;
1264}
1265
Ady Abraham8cbd3072021-03-15 16:39:06 -07001266void SurfaceComposerClient::Transaction::setEarlyWakeupStart() {
1267 mEarlyWakeupStart = true;
Dan Stoza84d619e2018-03-28 17:07:36 -07001268}
1269
Ady Abraham8cbd3072021-03-15 16:39:06 -07001270void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() {
1271 mEarlyWakeupEnd = true;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001272}
1273
Pablo Gamitodbc31672020-09-01 18:28:58 +00001274layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
Robert Carr5b3b9142021-02-22 12:27:32 -08001275 auto handle = sc->getLayerStateHandle();
Pablo Gamitodbc31672020-09-01 18:28:58 +00001276
Vishnu Nairf03652d2019-07-16 17:56:56 -07001277 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001278 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -08001279 ComposerState s;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001280
Vishnu Nairf03652d2019-07-16 17:56:56 -07001281 s.state.surface = handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +00001282 s.state.layerId = sc->getLayerId();
1283
Vishnu Nairf03652d2019-07-16 17:56:56 -07001284 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -07001285 }
1286
Vishnu Nairf03652d2019-07-16 17:56:56 -07001287 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001288}
1289
Marissa Wallc837b5e2018-10-12 10:04:44 -07001290void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
1291 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08001292 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
1293 callbackInfo.surfaceControls.insert(sc);
1294
Pascal Muetschard29247e52022-11-08 11:33:10 +01001295 TransactionCompletedListener::getInstance()->addSurfaceControlToCallbacks(callbackInfo, sc);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001296}
1297
Robert Carr4cdc58f2017-08-23 14:22:20 -07001298SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
1299 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -08001300 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001301 if (!s) {
1302 mStatus = BAD_INDEX;
1303 return *this;
1304 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001305 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001306 s->x = x;
1307 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001308
1309 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001310 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001311}
1312
Robert Carr4cdc58f2017-08-23 14:22:20 -07001313SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
1314 const sp<SurfaceControl>& sc) {
1315 return setFlags(sc, 0, layer_state_t::eLayerHidden);
1316}
1317
1318SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
1319 const sp<SurfaceControl>& sc) {
1320 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
1321}
1322
Robert Carr4cdc58f2017-08-23 14:22:20 -07001323SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
1324 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001325 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001326 if (!s) {
1327 mStatus = BAD_INDEX;
1328 return *this;
1329 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001330 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001331 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001332 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001333
1334 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001335 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001336}
1337
Pablo Gamito11dcc222020-09-12 15:49:39 +00001338SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
1339 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001340 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -07001341 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001342 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -07001343 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001344 }
1345 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001346 s->what &= ~layer_state_t::eLayerChanged;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001347 s->relativeLayerSurfaceControl = relativeTo;
Robert Carrdb66e622017-04-10 16:55:57 -07001348 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001349
1350 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001351 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001352}
1353
Robert Carr4cdc58f2017-08-23 14:22:20 -07001354SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
1355 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -07001356 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -08001357 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001358 if (!s) {
1359 mStatus = BAD_INDEX;
1360 return *this;
1361 }
chaviwc5676c62020-09-18 15:01:04 -07001362 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001363 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot) ||
Leon Scroggins9a20f722021-12-28 14:43:12 +00001364 (mask & layer_state_t::eEnableBackpressure) ||
Vishnu Nairaa799bd2022-11-16 23:09:09 +00001365 (mask & layer_state_t::eIgnoreDestinationFrame) ||
Leon Scroggins9a20f722021-12-28 14:43:12 +00001366 (mask & layer_state_t::eLayerIsDisplayDecoration)) {
Dan Stoza23116082015-06-18 14:58:39 -07001367 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -08001368 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001369 s->flags &= ~mask;
1370 s->flags |= (flags & mask);
1371 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001372
1373 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001374 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001375}
1376
Robert Carr4cdc58f2017-08-23 14:22:20 -07001377SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
1378 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -07001379 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -08001380 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001381 if (!s) {
1382 mStatus = BAD_INDEX;
1383 return *this;
1384 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001385 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001386 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001387
1388 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001389 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001390}
1391
Sally Qi421ffb02022-03-21 19:41:33 -07001392SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDimmingEnabled(
1393 const sp<SurfaceControl>& sc, bool dimmingEnabled) {
1394 layer_state_t* s = getLayerState(sc);
1395 if (!s) {
1396 mStatus = BAD_INDEX;
1397 return *this;
1398 }
1399 s->what |= layer_state_t::eDimmingEnabledChanged;
1400 s->dimmingEnabled = dimmingEnabled;
1401
1402 registerSurfaceControlForCallback(sc);
1403 return *this;
1404}
1405
Robert Carr4cdc58f2017-08-23 14:22:20 -07001406SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
1407 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -08001408 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001409 if (!s) {
1410 mStatus = BAD_INDEX;
1411 return *this;
1412 }
Ady Abrahamff930b42022-08-05 19:04:16 +00001413 if (alpha < 0.0f || alpha > 1.0f) {
Ady Abraham24d0b2a2022-08-11 16:27:34 -07001414 ALOGE("SurfaceComposerClient::Transaction::setAlpha: invalid alpha %f, clamping", alpha);
Ady Abrahamff930b42022-08-05 19:04:16 +00001415 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001416 s->what |= layer_state_t::eAlphaChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001417 s->color.a = std::clamp(alpha, 0.f, 1.f);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001418
1419 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001420 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001421}
1422
Robert Carr4cdc58f2017-08-23 14:22:20 -07001423SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001424 const sp<SurfaceControl>& sc, ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001425 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001426 if (!s) {
1427 mStatus = BAD_INDEX;
1428 return *this;
1429 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001430 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -07001431 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001432
1433 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001434 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -07001435}
1436
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001437SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -07001438 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001439 layer_state_t* s = getLayerState(sc);
1440 if (!s) {
1441 mStatus = BAD_INDEX;
1442 return *this;
1443 }
1444 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -07001445
1446 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001447
1448 registerSurfaceControlForCallback(sc);
1449 return *this;
1450}
1451
Robert Carr4cdc58f2017-08-23 14:22:20 -07001452SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
1453 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -08001454 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -08001455 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001456 if (!s) {
1457 mStatus = BAD_INDEX;
1458 return *this;
1459 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001460 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001461 layer_state_t::matrix22_t matrix;
1462 matrix.dsdx = dsdx;
1463 matrix.dtdx = dtdx;
1464 matrix.dsdy = dsdy;
1465 matrix.dtdy = dtdy;
1466 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001467
1468 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001469 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001470}
1471
chaviw25714502021-02-11 10:01:08 -08001472SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
Robert Carr4cdc58f2017-08-23 14:22:20 -07001473 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -08001474 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001475 if (!s) {
1476 mStatus = BAD_INDEX;
1477 return *this;
1478 }
chaviw25714502021-02-11 10:01:08 -08001479 s->what |= layer_state_t::eCropChanged;
1480 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001481
1482 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001483 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001484}
1485
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001486SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
1487 const sp<SurfaceControl>& sc, float cornerRadius) {
1488 layer_state_t* s = getLayerState(sc);
1489 if (!s) {
1490 mStatus = BAD_INDEX;
1491 return *this;
1492 }
1493 s->what |= layer_state_t::eCornerRadiusChanged;
1494 s->cornerRadius = cornerRadius;
1495 return *this;
1496}
1497
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001498SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1499 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1500 layer_state_t* s = getLayerState(sc);
1501 if (!s) {
1502 mStatus = BAD_INDEX;
1503 return *this;
1504 }
1505 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1506 s->backgroundBlurRadius = backgroundBlurRadius;
1507 return *this;
1508}
1509
Lucas Dupinc3800b82020-10-02 16:24:48 -07001510SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBlurRegions(
1511 const sp<SurfaceControl>& sc, const std::vector<BlurRegion>& blurRegions) {
1512 layer_state_t* s = getLayerState(sc);
1513 if (!s) {
1514 mStatus = BAD_INDEX;
1515 return *this;
1516 }
1517 s->what |= layer_state_t::eBlurRegionsChanged;
1518 s->blurRegions = blurRegions;
1519 return *this;
1520}
1521
Robert Carr4cdc58f2017-08-23 14:22:20 -07001522SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001523 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001524 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001525 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001526 mStatus = BAD_INDEX;
1527 return *this;
chaviw06178942017-07-27 10:25:59 -07001528 }
Robert Carr5b3b9142021-02-22 12:27:32 -08001529 if (SurfaceControl::isSameSurface(sc, newParent)) {
1530 return *this;
1531 }
chaviwf1961f72017-09-18 16:41:07 -07001532 s->what |= layer_state_t::eReparent;
Robert Carr5b3b9142021-02-22 12:27:32 -08001533 s->parentSurfaceControlForChild = newParent ? newParent->getParentingLayer() : nullptr;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001534
1535 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001536 return *this;
chaviw06178942017-07-27 10:25:59 -07001537}
1538
Robert Carr4cdc58f2017-08-23 14:22:20 -07001539SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1540 const sp<SurfaceControl>& sc,
1541 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001542 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001543 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001544 mStatus = BAD_INDEX;
1545 return *this;
1546 }
1547 s->what |= layer_state_t::eColorChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001548 s->color.rgb = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001549
1550 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001551 return *this;
1552}
1553
Valerie Haudd0b7572019-01-29 14:59:27 -08001554SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1555 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001556 layer_state_t* s = getLayerState(sc);
1557 if (!s) {
1558 mStatus = BAD_INDEX;
1559 return *this;
1560 }
1561
Valerie Haudd0b7572019-01-29 14:59:27 -08001562 s->what |= layer_state_t::eBackgroundColorChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001563 s->color.rgb = color;
Valerie Haudd0b7572019-01-29 14:59:27 -08001564 s->bgColorAlpha = alpha;
1565 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001566
1567 registerSurfaceControlForCallback(sc);
1568 return *this;
1569}
1570
Marissa Wall61c58622018-07-18 10:12:20 -07001571SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1572 const sp<SurfaceControl>& sc, uint32_t transform) {
1573 layer_state_t* s = getLayerState(sc);
1574 if (!s) {
1575 mStatus = BAD_INDEX;
1576 return *this;
1577 }
Vishnu Nairbbceb462022-10-10 04:52:13 +00001578 s->what |= layer_state_t::eBufferTransformChanged;
1579 s->bufferTransform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001580
1581 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001582 return *this;
1583}
1584
1585SurfaceComposerClient::Transaction&
1586SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1587 bool transformToDisplayInverse) {
1588 layer_state_t* s = getLayerState(sc);
1589 if (!s) {
1590 mStatus = BAD_INDEX;
1591 return *this;
1592 }
1593 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1594 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001595
1596 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001597 return *this;
1598}
1599
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001600std::shared_ptr<BufferData> SurfaceComposerClient::Transaction::getAndClearBuffer(
chaviw0acd33a2021-11-02 11:55:37 -05001601 const sp<SurfaceControl>& sc) {
1602 layer_state_t* s = getLayerState(sc);
1603 if (!s) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001604 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001605 }
1606 if (!(s->what & layer_state_t::eBufferChanged)) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001607 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001608 }
1609
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001610 std::shared_ptr<BufferData> bufferData = std::move(s->bufferData);
chaviw0acd33a2021-11-02 11:55:37 -05001611
1612 TransactionCompletedListener::getInstance()->removeReleaseBufferCallback(
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001613 bufferData->generateReleaseCallbackId());
chaviw0acd33a2021-11-02 11:55:37 -05001614 s->what &= ~layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001615 s->bufferData = nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001616
chaviw0acd33a2021-11-02 11:55:37 -05001617 return bufferData;
1618}
1619
Robert Carr79dc06a2022-02-22 15:28:59 -08001620SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferHasBarrier(
1621 const sp<SurfaceControl>& sc, uint64_t barrierFrameNumber) {
1622 layer_state_t* s = getLayerState(sc);
1623 if (!s) {
1624 mStatus = BAD_INDEX;
1625 return *this;
1626 }
1627 s->bufferData->hasBarrier = true;
1628 s->bufferData->barrierFrameNumber = barrierFrameNumber;
1629 return *this;
1630}
1631
Marissa Wall61c58622018-07-18 10:12:20 -07001632SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
chaviwba4320c2021-09-15 15:20:53 -05001633 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer,
John Reck57f748c2022-03-17 17:23:49 -04001634 const std::optional<sp<Fence>>& fence, const std::optional<uint64_t>& optFrameNumber,
liulijuneb489f62022-10-17 22:02:14 +08001635 uint32_t producerId, ReleaseBufferCallback callback) {
Marissa Wall61c58622018-07-18 10:12:20 -07001636 layer_state_t* s = getLayerState(sc);
1637 if (!s) {
1638 mStatus = BAD_INDEX;
1639 return *this;
1640 }
chaviw69058fb2021-09-27 09:37:30 -05001641
1642 releaseBufferIfOverwriting(*s);
1643
chaviwd1bda4b2022-05-19 17:44:46 -05001644 if (buffer == nullptr) {
1645 s->what &= ~layer_state_t::eBufferChanged;
1646 s->bufferData = nullptr;
chaviwd1bda4b2022-05-19 17:44:46 -05001647 return *this;
1648 }
1649
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001650 std::shared_ptr<BufferData> bufferData = std::make_shared<BufferData>();
1651 bufferData->buffer = buffer;
John Reck57f748c2022-03-17 17:23:49 -04001652 uint64_t frameNumber = sc->resolveFrameNumber(optFrameNumber);
1653 bufferData->frameNumber = frameNumber;
liulijuneb489f62022-10-17 22:02:14 +08001654 bufferData->producerId = producerId;
John Reck57f748c2022-03-17 17:23:49 -04001655 bufferData->flags |= BufferData::BufferDataChange::frameNumberChanged;
chaviwba4320c2021-09-15 15:20:53 -05001656 if (fence) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001657 bufferData->acquireFence = *fence;
1658 bufferData->flags |= BufferData::BufferDataChange::fenceChanged;
chaviwba4320c2021-09-15 15:20:53 -05001659 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001660 bufferData->releaseBufferEndpoint =
chaviwba4320c2021-09-15 15:20:53 -05001661 IInterface::asBinder(TransactionCompletedListener::getIInstance());
Ady Abrahamf0c56492020-12-17 18:04:15 -08001662 if (mIsAutoTimestamp) {
1663 mDesiredPresentTime = systemTime();
1664 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001665 setReleaseBufferCallback(bufferData.get(), callback);
chaviwba4320c2021-09-15 15:20:53 -05001666 s->what |= layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001667 s->bufferData = std::move(bufferData);
Marissa Wallebc2c052019-01-16 19:16:55 -08001668 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001669
chaviw1a227f02022-04-25 16:23:44 -05001670 // With the current infrastructure, a release callback will not be invoked if there's no
1671 // transaction callback in the case when a buffer is latched and not released early. This is
1672 // because the legacy implementation didn't have a release callback and sent releases in the
1673 // transaction callback. Because of this, we need to make sure to have a transaction callback
1674 // set up when a buffer is sent in a transaction to ensure the caller gets the release
1675 // callback, regardless if they set up a transaction callback.
1676 //
1677 // TODO (b/230380821): Remove when release callbacks are separated from transaction callbacks
1678 addTransactionCompletedCallback([](void*, nsecs_t, const sp<Fence>&,
1679 const std::vector<SurfaceControlStats>&) {},
1680 nullptr);
1681
Chavi Weingarten1ac2a922022-09-15 20:34:48 +00001682 mMayContainBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001683 return *this;
1684}
1685
chaviwba4320c2021-09-15 15:20:53 -05001686void SurfaceComposerClient::Transaction::setReleaseBufferCallback(BufferData* bufferData,
Vishnu Nair1506b182021-02-22 14:35:15 -08001687 ReleaseBufferCallback callback) {
1688 if (!callback) {
1689 return;
1690 }
1691
chaviwba4320c2021-09-15 15:20:53 -05001692 if (!bufferData->buffer) {
Vishnu Nair1506b182021-02-22 14:35:15 -08001693 ALOGW("Transaction::setReleaseBufferCallback"
1694 "ignored trying to set a callback on a null buffer.");
1695 return;
1696 }
1697
chaviwba4320c2021-09-15 15:20:53 -05001698 bufferData->releaseBufferListener = TransactionCompletedListener::getIInstance();
Vishnu Nair1506b182021-02-22 14:35:15 -08001699 auto listener = TransactionCompletedListener::getInstance();
chaviw8dd181f2022-01-05 18:36:46 -06001700 listener->setReleaseBufferCallback(bufferData->generateReleaseCallbackId(), callback);
Vishnu Nair1506b182021-02-22 14:35:15 -08001701}
1702
Marissa Wall61c58622018-07-18 10:12:20 -07001703SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1704 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1705 layer_state_t* s = getLayerState(sc);
1706 if (!s) {
1707 mStatus = BAD_INDEX;
1708 return *this;
1709 }
1710 s->what |= layer_state_t::eDataspaceChanged;
1711 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001712
1713 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001714 return *this;
1715}
1716
John Reck68796592023-01-25 13:47:12 -05001717SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setExtendedRangeBrightness(
1718 const sp<SurfaceControl>& sc, float currentBufferRatio, float desiredRatio) {
1719 layer_state_t* s = getLayerState(sc);
1720 if (!s) {
1721 mStatus = BAD_INDEX;
1722 return *this;
1723 }
1724 s->what |= layer_state_t::eExtendedRangeBrightnessChanged;
1725 s->currentSdrHdrRatio = currentBufferRatio;
1726 s->desiredSdrHdrRatio = desiredRatio;
1727
1728 registerSurfaceControlForCallback(sc);
1729 return *this;
1730}
1731
Alec Mourif4af03e2023-02-11 00:25:24 +00001732SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCachingHint(
1733 const sp<SurfaceControl>& sc, gui::CachingHint cachingHint) {
1734 layer_state_t* s = getLayerState(sc);
1735 if (!s) {
1736 mStatus = BAD_INDEX;
1737 return *this;
1738 }
1739 s->what |= layer_state_t::eCachingHintChanged;
1740 s->cachingHint = cachingHint;
1741
1742 registerSurfaceControlForCallback(sc);
1743 return *this;
1744}
1745
Marissa Wall61c58622018-07-18 10:12:20 -07001746SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1747 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1748 layer_state_t* s = getLayerState(sc);
1749 if (!s) {
1750 mStatus = BAD_INDEX;
1751 return *this;
1752 }
1753 s->what |= layer_state_t::eHdrMetadataChanged;
1754 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001755
1756 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001757 return *this;
1758}
1759
1760SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1761 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1762 layer_state_t* s = getLayerState(sc);
1763 if (!s) {
1764 mStatus = BAD_INDEX;
1765 return *this;
1766 }
1767 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1768 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001769
1770 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001771 return *this;
1772}
1773
1774SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1775 const sp<SurfaceControl>& sc, int32_t api) {
1776 layer_state_t* s = getLayerState(sc);
1777 if (!s) {
1778 mStatus = BAD_INDEX;
1779 return *this;
1780 }
1781 s->what |= layer_state_t::eApiChanged;
1782 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001783
1784 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001785 return *this;
1786}
1787
1788SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1789 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1790 layer_state_t* s = getLayerState(sc);
1791 if (!s) {
1792 mStatus = BAD_INDEX;
1793 return *this;
1794 }
1795 s->what |= layer_state_t::eSidebandStreamChanged;
1796 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001797
1798 registerSurfaceControlForCallback(sc);
1799 return *this;
1800}
1801
Marissa Wall17b4e452018-12-26 16:32:34 -08001802SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1803 nsecs_t desiredPresentTime) {
1804 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -08001805 mIsAutoTimestamp = false;
Marissa Wall17b4e452018-12-26 16:32:34 -08001806 return *this;
1807}
1808
Peiyong Linc502cb72019-03-01 15:00:23 -08001809SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1810 const sp<SurfaceControl>& sc, const bool agnostic) {
1811 layer_state_t* s = getLayerState(sc);
1812 if (!s) {
1813 mStatus = BAD_INDEX;
1814 return *this;
1815 }
1816 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1817 s->colorSpaceAgnostic = agnostic;
1818
1819 registerSurfaceControlForCallback(sc);
1820 return *this;
1821}
1822
Marissa Wallc837b5e2018-10-12 10:04:44 -07001823SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001824SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1825 int32_t priority) {
1826 layer_state_t* s = getLayerState(sc);
1827 if (!s) {
1828 mStatus = BAD_INDEX;
1829 return *this;
1830 }
1831
1832 s->what |= layer_state_t::eFrameRateSelectionPriority;
1833 s->frameRateSelectionPriority = priority;
1834
1835 registerSurfaceControlForCallback(sc);
1836 return *this;
1837}
1838
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001839SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::addTransactionCallback(
1840 TransactionCompletedCallbackTakesContext callback, void* callbackContext,
1841 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001842 auto listener = TransactionCompletedListener::getInstance();
1843
Marissa Wall80d94ad2019-01-18 16:04:36 -08001844 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1845 std::placeholders::_2, std::placeholders::_3);
1846 const auto& surfaceControls =
1847 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001848
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001849 CallbackId callbackId =
1850 listener->addCallbackFunction(callbackWithContext, surfaceControls, callbackType);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001851
1852 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1853 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001854 return *this;
1855}
1856
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001857SurfaceComposerClient::Transaction&
1858SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
1859 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1860 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMPLETE);
1861}
1862
1863SurfaceComposerClient::Transaction&
1864SurfaceComposerClient::Transaction::addTransactionCommittedCallback(
1865 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1866 return addTransactionCallback(callback, callbackContext, CallbackId::Type::ON_COMMIT);
1867}
1868
Valerie Hau871d6352020-01-29 08:44:02 -08001869SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1870 const sp<SurfaceControl>& sc) {
1871 layer_state_t* s = getLayerState(sc);
1872 if (!s) {
1873 mStatus = BAD_INDEX;
1874 return *this;
1875 }
1876
1877 s->what |= layer_state_t::eProducerDisconnect;
1878 return *this;
1879}
1880
Robert Carr2c358bf2018-08-08 15:58:15 -07001881SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
chaviw98318de2021-05-19 16:45:23 -05001882 const sp<SurfaceControl>& sc, const WindowInfo& info) {
Robert Carr2c358bf2018-08-08 15:58:15 -07001883 layer_state_t* s = getLayerState(sc);
1884 if (!s) {
1885 mStatus = BAD_INDEX;
1886 return *this;
1887 }
chaviw98318de2021-05-19 16:45:23 -05001888 s->windowInfoHandle = new WindowInfoHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001889 s->what |= layer_state_t::eInputInfoChanged;
1890 return *this;
1891}
chaviw273171b2018-12-26 11:46:30 -08001892
Vishnu Naire798b472020-07-23 13:52:21 -07001893SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Naire798b472020-07-23 13:52:21 -07001894 const FocusRequest& request) {
1895 mInputWindowCommands.focusRequests.push_back(request);
1896 return *this;
1897}
1898
Patrick Williamse5991f52022-06-23 20:23:43 +00001899SurfaceComposerClient::Transaction&
1900SurfaceComposerClient::Transaction::addWindowInfosReportedListener(
1901 sp<gui::IWindowInfosReportedListener> windowInfosReportedListener) {
1902 mInputWindowCommands.windowInfosReportedListeners.insert(windowInfosReportedListener);
chaviwa911b102019-02-14 10:18:33 -08001903 return *this;
1904}
1905
Peiyong Lind3788632018-09-18 16:01:31 -07001906SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1907 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1908 layer_state_t* s = getLayerState(sc);
1909 if (!s) {
1910 mStatus = BAD_INDEX;
1911 return *this;
1912 }
1913 s->what |= layer_state_t::eColorTransformChanged;
1914 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001915
1916 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001917 return *this;
1918}
1919
Robert Carrfb4d58b2019-01-15 09:21:27 -08001920SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1921 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
chaviw25714502021-02-11 10:01:08 -08001922 setCrop(sc, source);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001923
1924 int x = dst.left;
1925 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001926
1927 float sourceWidth = source.getWidth();
1928 float sourceHeight = source.getHeight();
1929
1930 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1931 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001932 float matrix[4] = {1, 0, 0, 1};
1933
1934 switch (transform) {
1935 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1936 matrix[0] = -xScale; matrix[1] = 0;
1937 matrix[2] = 0; matrix[3] = yScale;
1938 x += source.getWidth();
1939 break;
1940 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1941 matrix[0] = xScale; matrix[1] = 0;
1942 matrix[2] = 0; matrix[3] = -yScale;
1943 y += source.getHeight();
1944 break;
1945 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1946 matrix[0] = 0; matrix[1] = -yScale;
1947 matrix[2] = xScale; matrix[3] = 0;
1948 x += source.getHeight();
1949 break;
1950 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1951 matrix[0] = -xScale; matrix[1] = 0;
1952 matrix[2] = 0; matrix[3] = -yScale;
1953 x += source.getWidth();
1954 y += source.getHeight();
1955 break;
1956 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1957 matrix[0] = 0; matrix[1] = yScale;
1958 matrix[2] = -xScale; matrix[3] = 0;
1959 y += source.getWidth();
1960 break;
1961 default:
1962 matrix[0] = xScale; matrix[1] = 0;
1963 matrix[2] = 0; matrix[3] = yScale;
1964 break;
1965 }
1966 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07001967 float offsetX = xScale * source.left;
1968 float offsetY = yScale * source.top;
1969 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001970
1971 return *this;
1972}
1973
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001974SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1975 const sp<SurfaceControl>& sc, float shadowRadius) {
1976 layer_state_t* s = getLayerState(sc);
1977 if (!s) {
1978 mStatus = BAD_INDEX;
1979 return *this;
1980 }
1981 s->what |= layer_state_t::eShadowRadiusChanged;
1982 s->shadowRadius = shadowRadius;
1983 return *this;
1984}
1985
Steven Thomas3172e202020-01-06 19:25:30 -08001986SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Marin Shalamanov46084422020-10-13 12:33:42 +02001987 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility,
Marin Shalamanovc5986772021-03-16 16:09:49 +01001988 int8_t changeFrameRateStrategy) {
Steven Thomas3172e202020-01-06 19:25:30 -08001989 layer_state_t* s = getLayerState(sc);
1990 if (!s) {
1991 mStatus = BAD_INDEX;
1992 return *this;
1993 }
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001994 // Allow privileged values as well here, those will be ignored by SF if
1995 // the caller is not privileged
Marin Shalamanovc5986772021-03-16 16:09:49 +01001996 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1997 "Transaction::setFrameRate",
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001998 /*privileged=*/true)) {
Steven Thomas62a4cf82020-01-31 12:04:03 -08001999 mStatus = BAD_VALUE;
2000 return *this;
2001 }
Steven Thomas3172e202020-01-06 19:25:30 -08002002 s->what |= layer_state_t::eFrameRateChanged;
2003 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002004 s->frameRateCompatibility = compatibility;
Marin Shalamanovc5986772021-03-16 16:09:49 +01002005 s->changeFrameRateStrategy = changeFrameRateStrategy;
Steven Thomas3172e202020-01-06 19:25:30 -08002006 return *this;
2007}
2008
Andy Labrada096227e2022-06-15 16:58:11 +00002009SurfaceComposerClient::Transaction&
2010SurfaceComposerClient::Transaction::setDefaultFrameRateCompatibility(const sp<SurfaceControl>& sc,
2011 int8_t compatibility) {
2012 layer_state_t* s = getLayerState(sc);
2013 if (!s) {
2014 mStatus = BAD_INDEX;
2015 return *this;
2016 }
2017 s->what |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
2018 s->defaultFrameRateCompatibility = compatibility;
2019 return *this;
2020}
2021
Vishnu Nair6213bd92020-05-08 17:42:25 -07002022SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
2023 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
2024 layer_state_t* s = getLayerState(sc);
2025 if (!s) {
2026 mStatus = BAD_INDEX;
2027 return *this;
2028 }
2029
2030 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
2031 ? ui::Transform::ROT_INVALID
2032 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
2033 s->what |= layer_state_t::eFixedTransformHintChanged;
2034 s->fixedTransformHint = transform;
2035 return *this;
2036}
2037
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10002038SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
2039 const FrameTimelineInfo& frameTimelineInfo) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002040 mergeFrameTimelineInfo(mFrameTimelineInfo, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -07002041 return *this;
2042}
2043
Vishnu Naircf26a0a2020-11-13 12:56:20 -08002044SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAutoRefresh(
2045 const sp<SurfaceControl>& sc, bool autoRefresh) {
2046 layer_state_t* s = getLayerState(sc);
2047 if (!s) {
2048 mStatus = BAD_INDEX;
2049 return *this;
2050 }
2051
2052 s->what |= layer_state_t::eAutoRefreshChanged;
2053 s->autoRefresh = autoRefresh;
2054 return *this;
2055}
2056
Winson Chunga30f7c92021-06-29 15:42:56 -07002057SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedOverlay(
2058 const sp<SurfaceControl>& sc, bool isTrustedOverlay) {
2059 layer_state_t* s = getLayerState(sc);
2060 if (!s) {
2061 mStatus = BAD_INDEX;
2062 return *this;
2063 }
2064
2065 s->what |= layer_state_t::eTrustedOverlayChanged;
2066 s->isTrustedOverlay = isTrustedOverlay;
2067 return *this;
2068}
2069
Vishnu Nair277142c2021-01-05 18:35:29 -08002070SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken(
2071 const sp<IBinder>& applyToken) {
2072 mApplyToken = applyToken;
2073 return *this;
2074}
2075
John Reckcdb4ed72021-02-04 13:39:33 -05002076SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setStretchEffect(
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002077 const sp<SurfaceControl>& sc, const StretchEffect& stretchEffect) {
John Reckcdb4ed72021-02-04 13:39:33 -05002078 layer_state_t* s = getLayerState(sc);
2079 if (!s) {
2080 mStatus = BAD_INDEX;
2081 return *this;
2082 }
2083
2084 s->what |= layer_state_t::eStretchChanged;
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002085 s->stretchEffect = stretchEffect;
John Reckcdb4ed72021-02-04 13:39:33 -05002086 return *this;
2087}
2088
chaviwf3f40fe2021-04-27 15:54:02 -05002089SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferCrop(
2090 const sp<SurfaceControl>& sc, const Rect& bufferCrop) {
2091 layer_state_t* s = getLayerState(sc);
2092 if (!s) {
2093 mStatus = BAD_INDEX;
2094 return *this;
2095 }
2096
2097 s->what |= layer_state_t::eBufferCropChanged;
2098 s->bufferCrop = bufferCrop;
2099
2100 registerSurfaceControlForCallback(sc);
2101 return *this;
2102}
2103
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07002104SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDestinationFrame(
2105 const sp<SurfaceControl>& sc, const Rect& destinationFrame) {
2106 layer_state_t* s = getLayerState(sc);
2107 if (!s) {
2108 mStatus = BAD_INDEX;
2109 return *this;
2110 }
2111
2112 s->what |= layer_state_t::eDestinationFrameChanged;
2113 s->destinationFrame = destinationFrame;
2114
2115 registerSurfaceControlForCallback(sc);
2116 return *this;
2117}
2118
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002119SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDropInputMode(
2120 const sp<SurfaceControl>& sc, gui::DropInputMode mode) {
2121 layer_state_t* s = getLayerState(sc);
2122 if (!s) {
2123 mStatus = BAD_INDEX;
2124 return *this;
2125 }
2126
2127 s->what |= layer_state_t::eDropInputModeChanged;
2128 s->dropInputMode = mode;
2129
2130 registerSurfaceControlForCallback(sc);
2131 return *this;
2132}
2133
Tianhao Yao67dd7122022-02-22 17:48:33 +00002134SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::enableBorder(
Tianhao Yao10cea3c2022-03-30 01:37:22 +00002135 const sp<SurfaceControl>& sc, bool shouldEnable, float width, const half4& color) {
Tianhao Yao67dd7122022-02-22 17:48:33 +00002136 layer_state_t* s = getLayerState(sc);
2137 if (!s) {
2138 mStatus = BAD_INDEX;
2139 return *this;
2140 }
2141
2142 s->what |= layer_state_t::eRenderBorderChanged;
2143 s->borderEnabled = shouldEnable;
Tianhao Yao10cea3c2022-03-30 01:37:22 +00002144 s->borderWidth = width;
2145 s->borderColor = color;
Tianhao Yao67dd7122022-02-22 17:48:33 +00002146
2147 registerSurfaceControlForCallback(sc);
2148 return *this;
2149}
2150
Mathias Agopian698c0872011-06-28 19:09:31 -07002151// ---------------------------------------------------------------------------
2152
chaviw763ef572018-02-22 16:04:57 -08002153DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002154 DisplayState s;
2155 s.token = token;
2156 ssize_t index = mDisplayStates.indexOf(s);
2157 if (index < 0) {
2158 // we don't have it, add an initialized layer_state to our list
2159 s.what = 0;
2160 index = mDisplayStates.add(s);
2161 }
Dan Stozad723bd72014-11-18 10:24:03 -08002162 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002163}
2164
Robert Carr4cdc58f2017-08-23 14:22:20 -07002165status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
2166 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07002167 if (bufferProducer.get() != nullptr) {
2168 // Make sure that composition can never be stalled by a virtual display
2169 // consumer that isn't processing buffers fast enough.
2170 status_t err = bufferProducer->setAsyncMode(true);
2171 if (err != NO_ERROR) {
2172 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
2173 "BufferQueue. This BufferQueue cannot be used for virtual "
2174 "display. (%d)", err);
2175 return err;
2176 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002177 }
chaviw763ef572018-02-22 16:04:57 -08002178 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08002179 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002180 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002181 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002182}
2183
Robert Carr4cdc58f2017-08-23 14:22:20 -07002184void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002185 ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08002186 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002187 s.layerStack = layerStack;
2188 s.what |= DisplayState::eLayerStackChanged;
2189}
2190
Evan Rosky2239b372021-05-20 13:43:47 -07002191void SurfaceComposerClient::Transaction::setDisplayFlags(const sp<IBinder>& token, uint32_t flags) {
2192 DisplayState& s(getDisplayState(token));
2193 s.flags = flags;
2194 s.what |= DisplayState::eFlagsChanged;
2195}
2196
Robert Carr4cdc58f2017-08-23 14:22:20 -07002197void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08002198 ui::Rotation orientation,
2199 const Rect& layerStackRect,
2200 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08002201 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002202 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002203 s.layerStackSpaceRect = layerStackRect;
2204 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002205 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002206}
2207
Robert Carr4cdc58f2017-08-23 14:22:20 -07002208void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08002209 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07002210 s.width = width;
2211 s.height = height;
2212 s.what |= DisplayState::eDisplaySizeChanged;
2213}
2214
Huihong Luo3bdef862022-03-03 11:57:19 -08002215// copied from FrameTimelineInfo::merge()
2216void SurfaceComposerClient::Transaction::mergeFrameTimelineInfo(FrameTimelineInfo& t,
2217 const FrameTimelineInfo& other) {
2218 // When merging vsync Ids we take the oldest valid one
2219 if (t.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID &&
2220 other.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
2221 if (other.vsyncId > t.vsyncId) {
2222 t.vsyncId = other.vsyncId;
2223 t.inputEventId = other.inputEventId;
2224 t.startTimeNanos = other.startTimeNanos;
2225 }
2226 } else if (t.vsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) {
2227 t.vsyncId = other.vsyncId;
2228 t.inputEventId = other.inputEventId;
2229 t.startTimeNanos = other.startTimeNanos;
2230 }
2231}
2232
2233// copied from FrameTimelineInfo::clear()
2234void SurfaceComposerClient::Transaction::clearFrameTimelineInfo(FrameTimelineInfo& t) {
2235 t.vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID;
2236 t.inputEventId = os::IInputConstants::INVALID_INPUT_EVENT_ID;
2237 t.startTimeNanos = 0;
2238}
2239
Chavi Weingarten076acac2023-01-19 17:20:43 +00002240SurfaceComposerClient::Transaction&
2241SurfaceComposerClient::Transaction::setTrustedPresentationCallback(
2242 const sp<SurfaceControl>& sc, TrustedPresentationCallback cb,
2243 const TrustedPresentationThresholds& thresholds, void* context,
2244 sp<SurfaceComposerClient::PresentationCallbackRAII>& outCallbackRef) {
2245 auto listener = TransactionCompletedListener::getInstance();
2246 outCallbackRef = listener->addTrustedPresentationCallback(cb, sc->getLayerId(), context);
2247
2248 layer_state_t* s = getLayerState(sc);
2249 if (!s) {
2250 mStatus = BAD_INDEX;
2251 return *this;
2252 }
2253 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2254 s->trustedPresentationThresholds = thresholds;
2255 s->trustedPresentationListener.callbackInterface = TransactionCompletedListener::getIInstance();
2256 s->trustedPresentationListener.callbackId = sc->getLayerId();
2257
2258 return *this;
2259}
2260
2261SurfaceComposerClient::Transaction&
2262SurfaceComposerClient::Transaction::clearTrustedPresentationCallback(const sp<SurfaceControl>& sc) {
2263 auto listener = TransactionCompletedListener::getInstance();
2264 listener->clearTrustedPresentationCallback(sc->getLayerId());
2265
2266 layer_state_t* s = getLayerState(sc);
2267 if (!s) {
2268 mStatus = BAD_INDEX;
2269 return *this;
2270 }
2271 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2272 s->trustedPresentationThresholds = TrustedPresentationThresholds();
2273 s->trustedPresentationListener.callbackInterface = nullptr;
2274 s->trustedPresentationListener.callbackId = -1;
2275
2276 return *this;
2277}
2278
Mathias Agopiane57f2922012-08-09 16:29:12 -07002279// ---------------------------------------------------------------------------
2280
chaviw60c9d3e2021-06-04 12:52:17 -05002281SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002282
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002283SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
chaviw60c9d3e2021-06-04 12:52:17 -05002284 : mStatus(NO_ERROR), mClient(client) {}
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002285
Mathias Agopian698c0872011-06-28 19:09:31 -07002286void SurfaceComposerClient::onFirstRef() {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002287 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002288 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08002289 sp<ISurfaceComposerClient> conn;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002290 binder::Status status = sf->createConnection(&conn);
2291 if (status.isOk() && conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002292 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002293 mStatus = NO_ERROR;
2294 }
2295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002296}
2297
Mathias Agopian698c0872011-06-28 19:09:31 -07002298SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07002299 dispose();
2300}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07002301
Mathias Agopian698c0872011-06-28 19:09:31 -07002302status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002303 return mStatus;
2304}
2305
Mathias Agopian698c0872011-06-28 19:09:31 -07002306sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002307 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308}
2309
Mathias Agopiand4784a32010-05-27 19:41:15 -07002310status_t SurfaceComposerClient::linkToComposerDeath(
2311 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07002312 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002313 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2314 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002315}
2316
Mathias Agopian698c0872011-06-28 19:09:31 -07002317void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002318 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07002319 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002320 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07002321 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002322 client = mClient; // hold ref while lock is held
2323 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002324 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07002325 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002326}
2327
Carlos Martinez Romero4609a4a2022-11-30 00:38:11 +00002328status_t SurfaceComposerClient::bootFinished() {
2329 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2330 binder::Status status = sf->bootFinished();
2331 return statusTFromBinderStatus(status);
2332}
2333
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002334sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002335 PixelFormat format, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002336 const sp<IBinder>& parentHandle,
Valerie Hau1acd6962019-10-28 16:35:48 -07002337 LayerMetadata metadata,
2338 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07002339 sp<SurfaceControl> s;
Vishnu Nair992496b2020-10-22 17:27:21 -07002340 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07002341 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07002342 return s;
2343}
2344
Patrick Williamsa361de62022-10-06 20:34:10 +00002345static std::string toString(const String16& string) {
2346 return std::string(String8(string).c_str());
2347}
2348
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002349status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
2350 PixelFormat format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002351 sp<SurfaceControl>* outSurface, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002352 const sp<IBinder>& parentHandle,
2353 LayerMetadata metadata,
Valerie Hau1acd6962019-10-28 16:35:48 -07002354 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002355 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07002356 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07002357
Mathias Agopian698c0872011-06-28 19:09:31 -07002358 if (mStatus == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002359 gui::CreateSurfaceResult result;
2360 binder::Status status = mClient->createSurface(std::string(name.string()), flags,
2361 parentHandle, std::move(metadata), &result);
2362 err = statusTFromBinderStatus(status);
Valerie Hau1acd6962019-10-28 16:35:48 -07002363 if (outTransformHint) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002364 *outTransformHint = result.transformHint;
Valerie Hau1acd6962019-10-28 16:35:48 -07002365 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002366 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
2367 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002368 *outSurface = new SurfaceControl(this, result.handle, result.layerId,
2369 toString(result.layerName), w, h, format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002370 result.transformHint, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -07002371 }
2372 }
Robert Carr3b382ed2018-03-14 13:49:41 -07002373 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07002374}
2375
chaviwfe94a222019-08-21 13:52:59 -07002376sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
2377 if (mirrorFromSurface == nullptr) {
2378 return nullptr;
2379 }
2380
chaviwfe94a222019-08-21 13:52:59 -07002381 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Patrick Williamsa361de62022-10-06 20:34:10 +00002382 gui::CreateSurfaceResult result;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002383 const binder::Status status = mClient->mirrorSurface(mirrorFromHandle, &result);
2384 const status_t err = statusTFromBinderStatus(status);
chaviwfe94a222019-08-21 13:52:59 -07002385 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002386 return new SurfaceControl(this, result.handle, result.layerId, toString(result.layerName));
chaviwfe94a222019-08-21 13:52:59 -07002387 }
2388 return nullptr;
2389}
2390
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002391sp<SurfaceControl> SurfaceComposerClient::mirrorDisplay(DisplayId displayId) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002392 gui::CreateSurfaceResult result;
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002393 const binder::Status status = mClient->mirrorDisplay(displayId.value, &result);
2394 const status_t err = statusTFromBinderStatus(status);
2395 if (err == NO_ERROR) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002396 return new SurfaceControl(this, result.handle, result.layerId, toString(result.layerName));
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002397 }
2398 return nullptr;
2399}
2400
Svetoslavd85084b2014-03-20 10:28:31 -07002401status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
2402 if (mStatus != NO_ERROR) {
2403 return mStatus;
2404 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002405 const binder::Status status = mClient->clearLayerFrameStats(token);
2406 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002407}
2408
2409status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
2410 FrameStats* outStats) const {
2411 if (mStatus != NO_ERROR) {
2412 return mStatus;
2413 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002414 gui::FrameStats stats;
2415 const binder::Status status = mClient->getLayerFrameStats(token, &stats);
2416 if (status.isOk()) {
2417 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2418 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2419 for (const auto& t : stats.desiredPresentTimesNano) {
2420 outStats->desiredPresentTimesNano.add(t);
2421 }
2422 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2423 for (const auto& t : stats.actualPresentTimesNano) {
2424 outStats->actualPresentTimesNano.add(t);
2425 }
2426 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2427 for (const auto& t : stats.frameReadyTimesNano) {
2428 outStats->frameReadyTimesNano.add(t);
2429 }
2430 }
2431 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002432}
2433
Mathias Agopian698c0872011-06-28 19:09:31 -07002434// ----------------------------------------------------------------------------
2435
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002436status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
2437 ui::DisplayState* state) {
Huihong Luoaa7fc2e2022-02-15 10:43:00 -08002438 gui::DisplayState ds;
2439 binder::Status status =
2440 ComposerServiceAIDL::getComposerService()->getDisplayState(display, &ds);
2441 if (status.isOk()) {
2442 state->layerStack = ui::LayerStack::fromValue(ds.layerStack);
2443 state->orientation = static_cast<ui::Rotation>(ds.orientation);
2444 state->layerStackSpaceRect =
2445 ui::Size(ds.layerStackSpaceRect.width, ds.layerStackSpaceRect.height);
2446 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002447 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002448}
2449
Sally Qi6bb12822022-10-05 11:42:30 -07002450status_t SurfaceComposerClient::getStaticDisplayInfo(int64_t displayId,
Huihong Luoa79ddf42022-02-17 00:01:38 -08002451 ui::StaticDisplayInfo* outInfo) {
2452 using Tag = android::gui::DeviceProductInfo::ManufactureOrModelDate::Tag;
2453 gui::StaticDisplayInfo ginfo;
2454 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002455 ComposerServiceAIDL::getComposerService()->getStaticDisplayInfo(displayId, &ginfo);
Huihong Luoa79ddf42022-02-17 00:01:38 -08002456 if (status.isOk()) {
2457 // convert gui::StaticDisplayInfo to ui::StaticDisplayInfo
2458 outInfo->connectionType = static_cast<ui::DisplayConnectionType>(ginfo.connectionType);
2459 outInfo->density = ginfo.density;
2460 outInfo->secure = ginfo.secure;
2461 outInfo->installOrientation = static_cast<ui::Rotation>(ginfo.installOrientation);
2462
2463 DeviceProductInfo info;
2464 std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo;
2465 gui::DeviceProductInfo::ManufactureOrModelDate& date = dpi->manufactureOrModelDate;
2466 info.name = dpi->name;
2467 if (dpi->manufacturerPnpId.size() > 0) {
2468 // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
2469 constexpr int kMaxPnpIdSize = 4;
2470 size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
2471 std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
2472 }
Kriti Dang1de958d2022-05-30 15:26:58 +02002473 if (dpi->relativeAddress.size() > 0) {
2474 std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
2475 std::back_inserter(info.relativeAddress));
2476 }
Huihong Luoa79ddf42022-02-17 00:01:38 -08002477 info.productId = dpi->productId;
2478 if (date.getTag() == Tag::modelYear) {
2479 DeviceProductInfo::ModelYear modelYear;
2480 modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
2481 info.manufactureOrModelDate = modelYear;
2482 } else if (date.getTag() == Tag::manufactureYear) {
2483 DeviceProductInfo::ManufactureYear manufactureYear;
2484 manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
2485 info.manufactureOrModelDate = manufactureYear;
2486 } else if (date.getTag() == Tag::manufactureWeekAndYear) {
2487 DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
2488 weekAndYear.year =
2489 date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
2490 weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
2491 info.manufactureOrModelDate = weekAndYear;
2492 }
2493
2494 outInfo->deviceProductInfo = info;
2495 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002496 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002497}
2498
Sally Qi6bb12822022-10-05 11:42:30 -07002499void SurfaceComposerClient::getDynamicDisplayInfoInternal(gui::DynamicDisplayInfo& ginfo,
2500 ui::DynamicDisplayInfo*& outInfo) {
2501 // convert gui::DynamicDisplayInfo to ui::DynamicDisplayInfo
2502 outInfo->supportedDisplayModes.clear();
2503 outInfo->supportedDisplayModes.reserve(ginfo.supportedDisplayModes.size());
2504 for (const auto& mode : ginfo.supportedDisplayModes) {
2505 ui::DisplayMode outMode;
2506 outMode.id = mode.id;
2507 outMode.resolution.width = mode.resolution.width;
2508 outMode.resolution.height = mode.resolution.height;
2509 outMode.xDpi = mode.xDpi;
2510 outMode.yDpi = mode.yDpi;
2511 outMode.refreshRate = mode.refreshRate;
2512 outMode.appVsyncOffset = mode.appVsyncOffset;
2513 outMode.sfVsyncOffset = mode.sfVsyncOffset;
2514 outMode.presentationDeadline = mode.presentationDeadline;
2515 outMode.group = mode.group;
2516 std::transform(mode.supportedHdrTypes.begin(), mode.supportedHdrTypes.end(),
2517 std::back_inserter(outMode.supportedHdrTypes),
2518 [](const int32_t& value) { return static_cast<ui::Hdr>(value); });
2519 outInfo->supportedDisplayModes.push_back(outMode);
2520 }
2521
2522 outInfo->activeDisplayModeId = ginfo.activeDisplayModeId;
2523 outInfo->renderFrameRate = ginfo.renderFrameRate;
2524
2525 outInfo->supportedColorModes.clear();
2526 outInfo->supportedColorModes.reserve(ginfo.supportedColorModes.size());
2527 for (const auto& cmode : ginfo.supportedColorModes) {
2528 outInfo->supportedColorModes.push_back(static_cast<ui::ColorMode>(cmode));
2529 }
2530
2531 outInfo->activeColorMode = static_cast<ui::ColorMode>(ginfo.activeColorMode);
2532
2533 std::vector<ui::Hdr> types;
2534 types.reserve(ginfo.hdrCapabilities.supportedHdrTypes.size());
2535 for (const auto& hdr : ginfo.hdrCapabilities.supportedHdrTypes) {
2536 types.push_back(static_cast<ui::Hdr>(hdr));
2537 }
2538 outInfo->hdrCapabilities = HdrCapabilities(types, ginfo.hdrCapabilities.maxLuminance,
2539 ginfo.hdrCapabilities.maxAverageLuminance,
2540 ginfo.hdrCapabilities.minLuminance);
2541
2542 outInfo->autoLowLatencyModeSupported = ginfo.autoLowLatencyModeSupported;
2543 outInfo->gameContentTypeSupported = ginfo.gameContentTypeSupported;
2544 outInfo->preferredBootDisplayMode = ginfo.preferredBootDisplayMode;
2545}
2546
2547status_t SurfaceComposerClient::getDynamicDisplayInfoFromId(int64_t displayId,
2548 ui::DynamicDisplayInfo* outInfo) {
Huihong Luo38603fd2022-02-21 14:32:54 -08002549 gui::DynamicDisplayInfo ginfo;
2550 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002551 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromId(displayId,
2552 &ginfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002553 if (status.isOk()) {
Sally Qi6bb12822022-10-05 11:42:30 -07002554 getDynamicDisplayInfoInternal(ginfo, outInfo);
2555 }
2556 return statusTFromBinderStatus(status);
2557}
Huihong Luo38603fd2022-02-21 14:32:54 -08002558
Sally Qi6bb12822022-10-05 11:42:30 -07002559status_t SurfaceComposerClient::getDynamicDisplayInfoFromToken(const sp<IBinder>& display,
2560 ui::DynamicDisplayInfo* outInfo) {
2561 gui::DynamicDisplayInfo ginfo;
2562 binder::Status status =
2563 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromToken(display,
2564 &ginfo);
2565 if (status.isOk()) {
2566 getDynamicDisplayInfoInternal(ginfo, outInfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002567 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002568 return statusTFromBinderStatus(status);
Dan Stoza7f7da322014-05-02 15:26:25 -07002569}
2570
Marin Shalamanova7fe3042021-01-29 21:02:08 +01002571status_t SurfaceComposerClient::getActiveDisplayMode(const sp<IBinder>& display,
2572 ui::DisplayMode* mode) {
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002573 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -07002574
2575 status_t result = getDynamicDisplayInfoFromToken(display, &info);
Dan Stoza7f7da322014-05-02 15:26:25 -07002576 if (result != NO_ERROR) {
2577 return result;
2578 }
2579
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002580 if (const auto activeMode = info.getActiveDisplayMode()) {
2581 *mode = *activeMode;
2582 return NO_ERROR;
Dan Stoza7f7da322014-05-02 15:26:25 -07002583 }
2584
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002585 ALOGE("Active display mode not found.");
2586 return NAME_NOT_FOUND;
Dan Stoza7f7da322014-05-02 15:26:25 -07002587}
2588
Ady Abraham285f8c12022-10-11 17:12:14 -07002589status_t SurfaceComposerClient::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
2590 const gui::DisplayModeSpecs& specs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002591 binder::Status status =
Ady Abraham285f8c12022-10-11 17:12:14 -07002592 ComposerServiceAIDL::getComposerService()->setDesiredDisplayModeSpecs(displayToken,
2593 specs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002594 return statusTFromBinderStatus(status);
Ana Krulec0782b882019-10-15 17:34:54 -07002595}
2596
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002597status_t SurfaceComposerClient::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002598 gui::DisplayModeSpecs* outSpecs) {
2599 if (!outSpecs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002600 return BAD_VALUE;
2601 }
Huihong Luo02186fb2022-02-23 14:21:54 -08002602 binder::Status status =
2603 ComposerServiceAIDL::getComposerService()->getDesiredDisplayModeSpecs(displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002604 outSpecs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002605 return statusTFromBinderStatus(status);
Ana Krulec234bb162019-11-10 22:55:55 +01002606}
2607
Daniel Solomon42d04562019-01-20 21:03:19 -08002608status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
2609 ui::DisplayPrimaries& outPrimaries) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002610 gui::DisplayPrimaries primaries;
2611 binder::Status status =
2612 ComposerServiceAIDL::getComposerService()->getDisplayNativePrimaries(display,
2613 &primaries);
2614 if (status.isOk()) {
2615 outPrimaries.red.X = primaries.red.X;
2616 outPrimaries.red.Y = primaries.red.Y;
2617 outPrimaries.red.Z = primaries.red.Z;
2618
2619 outPrimaries.green.X = primaries.green.X;
2620 outPrimaries.green.Y = primaries.green.Y;
2621 outPrimaries.green.Z = primaries.green.Z;
2622
2623 outPrimaries.blue.X = primaries.blue.X;
2624 outPrimaries.blue.Y = primaries.blue.Y;
2625 outPrimaries.blue.Z = primaries.blue.Z;
2626
2627 outPrimaries.white.X = primaries.white.X;
2628 outPrimaries.white.Y = primaries.white.Y;
2629 outPrimaries.white.Z = primaries.white.Z;
2630 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002631 return statusTFromBinderStatus(status);
Daniel Solomon42d04562019-01-20 21:03:19 -08002632}
2633
Michael Wright28f24d02016-07-12 13:30:53 -07002634status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07002635 ColorMode colorMode) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002636 binder::Status status = ComposerServiceAIDL::getComposerService()
2637 ->setActiveColorMode(display, static_cast<int>(colorMode));
Huihong Luo3bdef862022-03-03 11:57:19 -08002638 return statusTFromBinderStatus(status);
Michael Wright28f24d02016-07-12 13:30:53 -07002639}
2640
Kriti Dang7defaf32021-11-15 11:55:43 +01002641status_t SurfaceComposerClient::getBootDisplayModeSupport(bool* support) {
Huihong Luo37396db2022-02-15 10:43:00 -08002642 binder::Status status =
2643 ComposerServiceAIDL::getComposerService()->getBootDisplayModeSupport(support);
Huihong Luo3bdef862022-03-03 11:57:19 -08002644 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002645}
2646
Sally Qi0cbd08b2022-08-17 12:12:28 -07002647status_t SurfaceComposerClient::getOverlaySupport(gui::OverlayProperties* outProperties) {
2648 binder::Status status =
2649 ComposerServiceAIDL::getComposerService()->getOverlaySupport(outProperties);
2650 return statusTFromBinderStatus(status);
2651}
2652
Kriti Dang7defaf32021-11-15 11:55:43 +01002653status_t SurfaceComposerClient::setBootDisplayMode(const sp<IBinder>& display,
2654 ui::DisplayModeId displayModeId) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002655 binder::Status status = ComposerServiceAIDL::getComposerService()
2656 ->setBootDisplayMode(display, static_cast<int>(displayModeId));
Huihong Luo3bdef862022-03-03 11:57:19 -08002657 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002658}
2659
2660status_t SurfaceComposerClient::clearBootDisplayMode(const sp<IBinder>& display) {
Huihong Luo37396db2022-02-15 10:43:00 -08002661 binder::Status status =
2662 ComposerServiceAIDL::getComposerService()->clearBootDisplayMode(display);
Huihong Luo3bdef862022-03-03 11:57:19 -08002663 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002664}
2665
Kriti Dang674b9372022-11-18 10:58:44 +01002666status_t SurfaceComposerClient::getHdrConversionCapabilities(
2667 std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) {
2668 binder::Status status = ComposerServiceAIDL::getComposerService()->getHdrConversionCapabilities(
2669 hdrConversionCapabilities);
2670 return statusTFromBinderStatus(status);
2671}
2672
2673status_t SurfaceComposerClient::setHdrConversionStrategy(
Kriti Dangd432bb52023-02-09 18:21:04 +01002674 gui::HdrConversionStrategy hdrConversionStrategy, ui::Hdr* outPreferredHdrOutputType) {
2675 int hdrType;
2676 binder::Status status = ComposerServiceAIDL::getComposerService()
2677 ->setHdrConversionStrategy(hdrConversionStrategy, &hdrType);
2678 *outPreferredHdrOutputType = static_cast<ui::Hdr>(hdrType);
Kriti Dang674b9372022-11-18 10:58:44 +01002679 return statusTFromBinderStatus(status);
2680}
2681
Kriti Dangafd1fda2023-01-17 13:33:15 +01002682status_t SurfaceComposerClient::getHdrOutputConversionSupport(bool* isSupported) {
2683 binder::Status status =
2684 ComposerServiceAIDL::getComposerService()->getHdrOutputConversionSupport(isSupported);
2685 return statusTFromBinderStatus(status);
2686}
2687
Andy Yu2ae6b6b2021-11-18 14:51:06 -08002688status_t SurfaceComposerClient::setOverrideFrameRate(uid_t uid, float frameRate) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002689 binder::Status status =
2690 ComposerServiceAIDL::getComposerService()->setOverrideFrameRate(uid, frameRate);
2691 return statusTFromBinderStatus(status);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08002692}
2693
Galia Peycheva5492cb52019-10-30 14:13:16 +01002694void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002695 ComposerServiceAIDL::getComposerService()->setAutoLowLatencyMode(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002696}
2697
Galia Peycheva5492cb52019-10-30 14:13:16 +01002698void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08002699 ComposerServiceAIDL::getComposerService()->setGameContentType(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01002700}
2701
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002702void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
2703 int mode) {
Huihong Luo37396db2022-02-15 10:43:00 -08002704 ComposerServiceAIDL::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07002705}
2706
Peiyong Linc6780972018-10-28 15:24:08 -07002707status_t SurfaceComposerClient::getCompositionPreference(
2708 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
2709 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
Huihong Luo05539a12022-02-23 10:29:40 -08002710 gui::CompositionPreference pref;
2711 binder::Status status =
2712 ComposerServiceAIDL::getComposerService()->getCompositionPreference(&pref);
2713 if (status.isOk()) {
2714 *defaultDataspace = static_cast<ui::Dataspace>(pref.defaultDataspace);
2715 *defaultPixelFormat = static_cast<ui::PixelFormat>(pref.defaultPixelFormat);
2716 *wideColorGamutDataspace = static_cast<ui::Dataspace>(pref.wideColorGamutDataspace);
2717 *wideColorGamutPixelFormat = static_cast<ui::PixelFormat>(pref.wideColorGamutPixelFormat);
2718 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002719 return statusTFromBinderStatus(status);
Peiyong Lin0256f722018-08-31 15:45:10 -07002720}
2721
Peiyong Lin08d10512019-01-16 13:27:35 -08002722bool SurfaceComposerClient::getProtectedContentSupport() {
2723 bool supported = false;
Huihong Luo05539a12022-02-23 10:29:40 -08002724 ComposerServiceAIDL::getComposerService()->getProtectedContentSupport(&supported);
Peiyong Lin08d10512019-01-16 13:27:35 -08002725 return supported;
2726}
2727
Svetoslavd85084b2014-03-20 10:28:31 -07002728status_t SurfaceComposerClient::clearAnimationFrameStats() {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002729 binder::Status status = ComposerServiceAIDL::getComposerService()->clearAnimationFrameStats();
Huihong Luo3bdef862022-03-03 11:57:19 -08002730 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002731}
2732
2733status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
Huihong Luo4ed1c912022-02-22 14:30:01 -08002734 gui::FrameStats stats;
2735 binder::Status status =
2736 ComposerServiceAIDL::getComposerService()->getAnimationFrameStats(&stats);
2737 if (status.isOk()) {
2738 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2739 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2740 for (const auto& t : stats.desiredPresentTimesNano) {
2741 outStats->desiredPresentTimesNano.add(t);
2742 }
2743 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2744 for (const auto& t : stats.actualPresentTimesNano) {
2745 outStats->actualPresentTimesNano.add(t);
2746 }
2747 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2748 for (const auto& t : stats.frameReadyTimesNano) {
2749 outStats->frameReadyTimesNano.add(t);
2750 }
2751 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002752 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002753}
2754
Kriti Dang49ad4132021-01-08 11:49:56 +01002755status_t SurfaceComposerClient::overrideHdrTypes(const sp<IBinder>& display,
2756 const std::vector<ui::Hdr>& hdrTypes) {
Huihong Luo05539a12022-02-23 10:29:40 -08002757 std::vector<int32_t> hdrTypesVector;
2758 hdrTypesVector.reserve(hdrTypes.size());
2759 for (auto t : hdrTypes) {
2760 hdrTypesVector.push_back(static_cast<int32_t>(t));
2761 }
2762
2763 binder::Status status =
2764 ComposerServiceAIDL::getComposerService()->overrideHdrTypes(display, hdrTypesVector);
Huihong Luo3bdef862022-03-03 11:57:19 -08002765 return statusTFromBinderStatus(status);
Kriti Dang49ad4132021-01-08 11:49:56 +01002766}
2767
Tej Singhe2751772021-04-06 22:05:29 -07002768status_t SurfaceComposerClient::onPullAtom(const int32_t atomId, std::string* outData,
2769 bool* success) {
Huihong Luo05539a12022-02-23 10:29:40 -08002770 gui::PullAtomData pad;
2771 binder::Status status = ComposerServiceAIDL::getComposerService()->onPullAtom(atomId, &pad);
2772 if (status.isOk()) {
Huihong Luo30aa4372022-10-03 14:54:12 -07002773 outData->assign(pad.data.begin(), pad.data.end());
Huihong Luo05539a12022-02-23 10:29:40 -08002774 *success = pad.success;
2775 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002776 return statusTFromBinderStatus(status);
Tej Singhe2751772021-04-06 22:05:29 -07002777}
2778
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002779status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
2780 ui::PixelFormat* outFormat,
2781 ui::Dataspace* outDataspace,
2782 uint8_t* outComponentMask) {
Huihong Luo05539a12022-02-23 10:29:40 -08002783 if (!outFormat || !outDataspace || !outComponentMask) {
2784 return BAD_VALUE;
2785 }
2786
2787 gui::ContentSamplingAttributes attrs;
2788 binder::Status status = ComposerServiceAIDL::getComposerService()
2789 ->getDisplayedContentSamplingAttributes(display, &attrs);
2790 if (status.isOk()) {
2791 *outFormat = static_cast<ui::PixelFormat>(attrs.format);
2792 *outDataspace = static_cast<ui::Dataspace>(attrs.dataspace);
2793 *outComponentMask = static_cast<uint8_t>(attrs.componentMask);
2794 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002795 return statusTFromBinderStatus(status);
Kevin DuBois9c0a1762018-10-16 13:32:31 -07002796}
2797
Kevin DuBois74e53772018-11-19 10:52:38 -08002798status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
2799 bool enable, uint8_t componentMask,
2800 uint64_t maxFrames) {
Huihong Luo05539a12022-02-23 10:29:40 -08002801 binder::Status status =
2802 ComposerServiceAIDL::getComposerService()
2803 ->setDisplayContentSamplingEnabled(display, enable,
2804 static_cast<int8_t>(componentMask),
2805 static_cast<int64_t>(maxFrames));
Huihong Luo3bdef862022-03-03 11:57:19 -08002806 return statusTFromBinderStatus(status);
Kevin DuBois74e53772018-11-19 10:52:38 -08002807}
2808
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002809status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
2810 uint64_t maxFrames, uint64_t timestamp,
2811 DisplayedFrameStats* outStats) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002812 if (!outStats) {
2813 return BAD_VALUE;
2814 }
2815
2816 gui::DisplayedFrameStats stats;
2817 binder::Status status =
2818 ComposerServiceAIDL::getComposerService()->getDisplayedContentSample(display, maxFrames,
2819 timestamp, &stats);
2820 if (status.isOk()) {
2821 // convert gui::DisplayedFrameStats to ui::DisplayedFrameStats
2822 outStats->numFrames = static_cast<uint64_t>(stats.numFrames);
2823 outStats->component_0_sample.reserve(stats.component_0_sample.size());
2824 for (const auto& s : stats.component_0_sample) {
2825 outStats->component_0_sample.push_back(static_cast<uint64_t>(s));
2826 }
2827 outStats->component_1_sample.reserve(stats.component_1_sample.size());
2828 for (const auto& s : stats.component_1_sample) {
2829 outStats->component_1_sample.push_back(static_cast<uint64_t>(s));
2830 }
2831 outStats->component_2_sample.reserve(stats.component_2_sample.size());
2832 for (const auto& s : stats.component_2_sample) {
2833 outStats->component_2_sample.push_back(static_cast<uint64_t>(s));
2834 }
2835 outStats->component_3_sample.reserve(stats.component_3_sample.size());
2836 for (const auto& s : stats.component_3_sample) {
2837 outStats->component_3_sample.push_back(static_cast<uint64_t>(s));
2838 }
2839 }
2840 return statusTFromBinderStatus(status);
Kevin DuBois1d4249a2018-08-29 10:45:14 -07002841}
Marissa Wall35187b32019-01-08 10:08:52 -08002842
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002843status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
2844 bool* outIsWideColorDisplay) {
Huihong Luo37396db2022-02-15 10:43:00 -08002845 binder::Status status =
2846 ComposerServiceAIDL::getComposerService()->isWideColorDisplay(display,
2847 outIsWideColorDisplay);
Huihong Luo3bdef862022-03-03 11:57:19 -08002848 return statusTFromBinderStatus(status);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08002849}
2850
Kevin DuBois00c66832019-02-18 16:21:31 -08002851status_t SurfaceComposerClient::addRegionSamplingListener(
2852 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
2853 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002854 gui::ARect rect;
2855 rect.left = samplingArea.left;
2856 rect.top = samplingArea.top;
2857 rect.right = samplingArea.right;
2858 rect.bottom = samplingArea.bottom;
2859 binder::Status status =
2860 ComposerServiceAIDL::getComposerService()->addRegionSamplingListener(rect,
2861 stopLayerHandle,
2862 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002863 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002864}
2865
2866status_t SurfaceComposerClient::removeRegionSamplingListener(
2867 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002868 binder::Status status =
2869 ComposerServiceAIDL::getComposerService()->removeRegionSamplingListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002870 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08002871}
2872
Alec Mouria9a68a62021-03-04 19:14:50 -08002873status_t SurfaceComposerClient::addFpsListener(int32_t taskId,
Alec Mouriadebf5c2021-01-05 12:57:36 -08002874 const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002875 binder::Status status =
2876 ComposerServiceAIDL::getComposerService()->addFpsListener(taskId, listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002877 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08002878}
2879
2880status_t SurfaceComposerClient::removeFpsListener(const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002881 binder::Status status = ComposerServiceAIDL::getComposerService()->removeFpsListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002882 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08002883}
2884
Galia Peycheva8f04b302021-04-27 13:25:38 +02002885status_t SurfaceComposerClient::addTunnelModeEnabledListener(
2886 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002887 binder::Status status =
2888 ComposerServiceAIDL::getComposerService()->addTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002889 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02002890}
2891
2892status_t SurfaceComposerClient::removeTunnelModeEnabledListener(
2893 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002894 binder::Status status =
2895 ComposerServiceAIDL::getComposerService()->removeTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002896 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02002897}
2898
Dan Gittik57e63c52019-01-18 16:37:54 +00002899bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
2900 bool support = false;
Huihong Luo37396db2022-02-15 10:43:00 -08002901 binder::Status status =
2902 ComposerServiceAIDL::getComposerService()->getDisplayBrightnessSupport(displayToken,
2903 &support);
2904 return status.isOk() ? support : false;
Dan Gittik57e63c52019-01-18 16:37:54 +00002905}
2906
2907status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
John Reck22be6962021-03-10 12:59:54 -05002908 const gui::DisplayBrightness& brightness) {
Huihong Luo37396db2022-02-15 10:43:00 -08002909 binder::Status status =
2910 ComposerServiceAIDL::getComposerService()->setDisplayBrightness(displayToken,
2911 brightness);
Huihong Luo3bdef862022-03-03 11:57:19 -08002912 return statusTFromBinderStatus(status);
Dan Gittik57e63c52019-01-18 16:37:54 +00002913}
2914
John Reck88270902021-03-18 11:27:35 -04002915status_t SurfaceComposerClient::addHdrLayerInfoListener(
2916 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08002917 binder::Status status =
2918 ComposerServiceAIDL::getComposerService()->addHdrLayerInfoListener(displayToken,
2919 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002920 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04002921}
2922
2923status_t SurfaceComposerClient::removeHdrLayerInfoListener(
2924 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08002925 binder::Status status =
2926 ComposerServiceAIDL::getComposerService()->removeHdrLayerInfoListener(displayToken,
2927 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08002928 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04002929}
2930
Lais Andrade3a6e47d2020-04-02 11:20:16 +01002931status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
Huihong Luo37396db2022-02-15 10:43:00 -08002932 binder::Status status = ComposerServiceAIDL::getComposerService()->notifyPowerBoost(boostId);
Huihong Luo3bdef862022-03-03 11:57:19 -08002933 return statusTFromBinderStatus(status);
Ady Abraham8532d012019-05-08 14:50:56 -07002934}
2935
Vishnu Nairb13bb952019-11-15 10:24:08 -08002936status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
2937 const half4& spotColor, float lightPosY,
2938 float lightPosZ, float lightRadius) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002939 gui::Color ambientColorG, spotColorG;
2940 ambientColorG.r = ambientColor.r;
2941 ambientColorG.g = ambientColor.g;
2942 ambientColorG.b = ambientColor.b;
2943 ambientColorG.a = ambientColor.a;
2944 spotColorG.r = spotColor.r;
2945 spotColorG.g = spotColor.g;
2946 spotColorG.b = spotColor.b;
2947 spotColorG.a = spotColor.a;
2948 binder::Status status =
2949 ComposerServiceAIDL::getComposerService()->setGlobalShadowSettings(ambientColorG,
2950 spotColorG,
2951 lightPosY, lightPosZ,
2952 lightRadius);
2953 return statusTFromBinderStatus(status);
Vishnu Nairb13bb952019-11-15 10:24:08 -08002954}
2955
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05002956std::optional<DisplayDecorationSupport> SurfaceComposerClient::getDisplayDecorationSupport(
2957 const sp<IBinder>& displayToken) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002958 std::optional<gui::DisplayDecorationSupport> gsupport;
2959 binder::Status status =
2960 ComposerServiceAIDL::getComposerService()->getDisplayDecorationSupport(displayToken,
2961 &gsupport);
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05002962 std::optional<DisplayDecorationSupport> support;
Leon Scroggins III9a11a2e2022-10-04 12:46:21 -04002963 if (status.isOk() && gsupport.has_value()) {
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04002964 support.emplace(DisplayDecorationSupport{
2965 .format =
2966 static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
2967 gsupport->format),
2968 .alphaInterpretation =
Huihong Luo3bdef862022-03-03 11:57:19 -08002969 static_cast<aidl::android::hardware::graphics::common::AlphaInterpretation>(
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04002970 gsupport->alphaInterpretation)
2971 });
Huihong Luo3bdef862022-03-03 11:57:19 -08002972 }
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -05002973 return support;
2974}
2975
Huihong Luo02186fb2022-02-23 14:21:54 -08002976int SurfaceComposerClient::getGpuContextPriority() {
2977 int priority;
2978 binder::Status status =
2979 ComposerServiceAIDL::getComposerService()->getGpuContextPriority(&priority);
2980 if (!status.isOk()) {
Huihong Luo3bdef862022-03-03 11:57:19 -08002981 status_t err = statusTFromBinderStatus(status);
Huihong Luo02186fb2022-02-23 14:21:54 -08002982 ALOGE("getGpuContextPriority failed to read data: %s (%d)", strerror(-err), err);
2983 return 0;
2984 }
2985 return priority;
Ana Krulec31f2b3c2020-12-14 14:30:09 -08002986}
2987
chaviw60c9d3e2021-06-04 12:52:17 -05002988status_t SurfaceComposerClient::addWindowInfosListener(
chaviwa2948ad2022-03-08 11:07:01 -06002989 const sp<WindowInfosListener>& windowInfosListener,
2990 std::pair<std::vector<gui::WindowInfo>, std::vector<gui::DisplayInfo>>* outInitialInfo) {
chaviw60c9d3e2021-06-04 12:52:17 -05002991 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08002992 ->addWindowInfosListener(windowInfosListener, ComposerServiceAIDL::getComposerService(),
chaviwa2948ad2022-03-08 11:07:01 -06002993 outInitialInfo);
chaviw60c9d3e2021-06-04 12:52:17 -05002994}
2995
2996status_t SurfaceComposerClient::removeWindowInfosListener(
2997 const sp<WindowInfosListener>& windowInfosListener) {
2998 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08002999 ->removeWindowInfosListener(windowInfosListener,
3000 ComposerServiceAIDL::getComposerService());
chaviw60c9d3e2021-06-04 12:52:17 -05003001}
3002
Mathias Agopian698c0872011-06-28 19:09:31 -07003003// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003004
chaviw690db382020-07-27 16:46:46 -07003005status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003006 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003007 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003008 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003009
Huihong Luof5029222021-12-16 14:33:46 -08003010 binder::Status status = s->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003011 return statusTFromBinderStatus(status);
Robert Carr673134e2017-01-09 19:48:38 -08003012}
3013
Dominik Laskowskif1833852021-03-23 15:06:50 -07003014status_t ScreenshotClient::captureDisplay(DisplayId displayId,
chaviwe7b9f272020-08-18 16:08:59 -07003015 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003016 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
chaviw93df2ea2019-04-30 16:45:12 -07003017 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003018
Huihong Luof5029222021-12-16 14:33:46 -08003019 binder::Status status = s->captureDisplayById(displayId.value, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003020 return statusTFromBinderStatus(status);
chaviw93df2ea2019-04-30 16:45:12 -07003021}
3022
chaviw26c52482020-07-28 16:25:52 -07003023status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003024 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003025 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003026 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003027
Huihong Luof5029222021-12-16 14:33:46 -08003028 binder::Status status = s->captureLayers(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003029 return statusTFromBinderStatus(status);
chaviwa76b2712017-09-20 12:02:26 -07003030}
Dominik Laskowski718f9602019-11-09 20:01:35 -08003031
chaviw69058fb2021-09-27 09:37:30 -05003032// ---------------------------------------------------------------------------------
3033
3034void ReleaseCallbackThread::addReleaseCallback(const ReleaseCallbackId callbackId,
3035 sp<Fence> releaseFence) {
3036 std::scoped_lock<std::mutex> lock(mMutex);
3037 if (!mStarted) {
3038 mThread = std::thread(&ReleaseCallbackThread::threadMain, this);
3039 mStarted = true;
3040 }
3041
3042 mCallbackInfos.emplace(callbackId, std::move(releaseFence));
3043 mReleaseCallbackPending.notify_one();
3044}
3045
3046void ReleaseCallbackThread::threadMain() {
3047 const auto listener = TransactionCompletedListener::getInstance();
3048 std::queue<std::tuple<const ReleaseCallbackId, const sp<Fence>>> callbackInfos;
3049 while (true) {
3050 {
3051 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003052 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003053 callbackInfos = std::move(mCallbackInfos);
3054 mCallbackInfos = {};
3055 }
3056
3057 while (!callbackInfos.empty()) {
3058 auto [callbackId, releaseFence] = callbackInfos.front();
Huihong Luoffee3bc2023-01-17 16:14:35 +00003059 listener->onReleaseBuffer(callbackId, std::move(releaseFence), UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -05003060 callbackInfos.pop();
3061 }
3062
3063 {
3064 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003065 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003066 if (mCallbackInfos.size() == 0) {
3067 mReleaseCallbackPending.wait(lock);
3068 }
3069 }
3070 }
3071}
3072
Dominik Laskowski718f9602019-11-09 20:01:35 -08003073} // namespace android