blob: 9854274cb11b2e09470ce9d45d61ae773d9373de [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>
Vishnu Nair7cea62b2025-01-16 23:56:45 +000022#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Patrick Williams641f7f22022-06-22 19:25:35 +000024#include <android/gui/BnWindowInfosReportedListener.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080025#include <android/gui/DisplayState.h>
Marzia Favarodcc9d9b2024-01-10 10:17:00 +000026#include <android/gui/EdgeExtensionParameters.h>
Huihong Luod3d8f8e2022-03-08 14:48:46 -080027#include <android/gui/ISurfaceComposerClient.h>
chaviw60c9d3e2021-06-04 12:52:17 -050028#include <android/gui/IWindowInfosListener.h>
Chavi Weingarten076acac2023-01-19 17:20:43 +000029#include <android/gui/TrustedPresentationThresholds.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080030#include <android/os/IInputConstants.h>
Brian Lindahl07dcd492024-10-30 11:43:23 -060031#include <com_android_graphics_libgui_flags.h>
Sally Qi0abc4a52024-09-26 16:13:06 -070032#include <gui/DisplayLuts.h>
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070033#include <gui/FrameRateUtils.h>
Patrick Williams640b7292022-10-11 19:25:06 +000034#include <gui/TraceUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <utils/SortedVector.h>
38#include <utils/String8.h>
39#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070041#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070042#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070043#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080044
Michael Wright28f24d02016-07-12 13:30:53 -070045#include <system/graphics.h>
46
Alec Mouri924f9502024-08-15 20:01:08 +000047#include <gui/AidlUtil.h>
Robert Carr673134e2017-01-09 19:48:38 -080048#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070049#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080050#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080051#include <gui/ISurfaceComposer.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070052#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080053#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080054#include <gui/SurfaceComposerClient.h>
chaviw98318de2021-05-19 16:45:23 -050055#include <gui/WindowInfo.h>
Marin Shalamanov3b1f7bc2021-03-16 15:51:53 +010056#include <private/gui/ParcelUtils.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010057#include <ui/DisplayMode.h>
Huihong Luoaa7fc2e2022-02-15 10:43:00 -080058#include <ui/DisplayState.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010059#include <ui/DynamicDisplayInfo.h>
ramindani8204da22024-10-18 15:22:52 -070060#include <ui/FrameRateCategoryRate.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Chavi Weingartene0237bb2023-02-06 21:48:32 +000062#include <android-base/thread_annotations.h>
Chavi Weingartenc78f53c2023-04-14 18:50:53 +000063#include <gui/LayerStatePermissions.h>
Chavi Weingartenf6fb4452024-01-23 21:10:30 +000064#include <gui/ScreenCaptureResults.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080065#include <private/gui/ComposerService.h>
Huihong Luof5029222021-12-16 14:33:46 -080066#include <private/gui/ComposerServiceAIDL.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Marissa Wall73411622019-01-25 10:45:41 -080068// This server size should always be smaller than the server cache size
Patrick Williams271afa92023-08-09 18:30:22 -050069#define BUFFER_CACHE_MAX_SIZE 4096
Marissa Wall73411622019-01-25 10:45:41 -080070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070072
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -050073using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
chaviw98318de2021-05-19 16:45:23 -050074using gui::FocusRequest;
Huihong Luoecc1f902021-11-20 11:55:05 -080075using gui::IRegionSamplingListener;
Chavi Weingarten076acac2023-01-19 17:20:43 +000076using gui::TrustedPresentationThresholds;
chaviw98318de2021-05-19 16:45:23 -050077using gui::WindowInfo;
78using gui::WindowInfoHandle;
chaviw60c9d3e2021-06-04 12:52:17 -050079using gui::WindowInfosListener;
Huihong Luo3bdef862022-03-03 11:57:19 -080080using gui::aidl_utils::statusTFromBinderStatus;
Peiyong Lin9f034472018-03-28 15:29:00 -070081using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082// ---------------------------------------------------------------------------
83
Mathias Agopian7e27f052010-05-28 14:22:23 -070084ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
Huihong Luof5029222021-12-16 14:33:46 -080085ANDROID_SINGLETON_STATIC_INSTANCE(ComposerServiceAIDL);
Mathias Agopian7e27f052010-05-28 14:22:23 -070086
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080087namespace {
88// Initialize transaction id counter used to generate transaction ids
89std::atomic<uint32_t> idCounter = 0;
90int64_t generateId() {
91 return (((int64_t)getpid()) << 32) | ++idCounter;
92}
Pascal Muetschard29247e52022-11-08 11:33:10 +010093
Pascal Mütschardd56514e2024-05-24 17:37:13 +020094constexpr int64_t INVALID_VSYNC = -1;
Melody Hsuc034fbf2024-10-25 22:46:40 +000095const constexpr char* LOG_SURFACE_CONTROL_REGISTRY = "SurfaceControlRegistry";
Pascal Mütschardd56514e2024-05-24 17:37:13 +020096
Vishnu Nairab6dc5c2021-12-09 08:39:57 -080097} // namespace
98
Alan Dingfc6eab52024-05-02 18:28:45 -070099const std::string SurfaceComposerClient::kEmpty{};
100
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700101ComposerService::ComposerService()
102: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -0700103 Mutex::Autolock _l(mLock);
104 connectLocked();
105}
106
Brian Lindahl9b85a192021-05-10 09:56:33 +0000107bool ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700108 const String16 name("SurfaceFlinger");
Brian Lindahl9b85a192021-05-10 09:56:33 +0000109 mComposerService = waitForService<ISurfaceComposer>(name);
110 if (mComposerService == nullptr) {
111 return false; // fatal error or permission problem
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700112 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700113
114 // Create the death listener.
115 class DeathObserver : public IBinder::DeathRecipient {
116 ComposerService& mComposerService;
117 virtual void binderDied(const wp<IBinder>& who) {
118 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
119 who.unsafe_get());
120 mComposerService.composerServiceDied();
121 }
122 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700123 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700124 };
125
Anton Ivanov8ed86592025-02-20 11:52:50 -0800126 mDeathObserver = sp<DeathObserver>::make(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800127 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Brian Lindahl9b85a192021-05-10 09:56:33 +0000128 return true;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129}
130
Andy McFadden6652b3e2012-09-06 18:45:56 -0700131/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
132 ComposerService& instance = ComposerService::getInstance();
133 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700134 if (instance.mComposerService == nullptr) {
Brian Lindahl9b85a192021-05-10 09:56:33 +0000135 if (ComposerService::getInstance().connectLocked()) {
136 ALOGD("ComposerService reconnected");
137 }
Andy McFadden6652b3e2012-09-06 18:45:56 -0700138 }
139 return instance.mComposerService;
140}
141
142void ComposerService::composerServiceDied()
143{
144 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700145 mComposerService = nullptr;
146 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700147}
148
Huihong Luof5029222021-12-16 14:33:46 -0800149ComposerServiceAIDL::ComposerServiceAIDL() : Singleton<ComposerServiceAIDL>() {
150 std::scoped_lock lock(mMutex);
151 connectLocked();
152}
153
154bool ComposerServiceAIDL::connectLocked() {
155 const String16 name("SurfaceFlingerAIDL");
156 mComposerService = waitForService<gui::ISurfaceComposer>(name);
157 if (mComposerService == nullptr) {
158 return false; // fatal error or permission problem
159 }
160
161 // Create the death listener.
162 class DeathObserver : public IBinder::DeathRecipient {
163 ComposerServiceAIDL& mComposerService;
164 virtual void binderDied(const wp<IBinder>& who) {
165 ALOGW("ComposerService aidl remote (surfaceflinger) died [%p]", who.unsafe_get());
166 mComposerService.composerServiceDied();
167 }
168
169 public:
170 explicit DeathObserver(ComposerServiceAIDL& mgr) : mComposerService(mgr) {}
171 };
172
Anton Ivanov8ed86592025-02-20 11:52:50 -0800173 mDeathObserver = sp<DeathObserver>::make(*const_cast<ComposerServiceAIDL*>(this));
Huihong Luof5029222021-12-16 14:33:46 -0800174 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
175 return true;
176}
177
178/*static*/ sp<gui::ISurfaceComposer> ComposerServiceAIDL::getComposerService() {
179 ComposerServiceAIDL& instance = ComposerServiceAIDL::getInstance();
180 std::scoped_lock lock(instance.mMutex);
181 if (instance.mComposerService == nullptr) {
182 if (ComposerServiceAIDL::getInstance().connectLocked()) {
183 ALOGD("ComposerServiceAIDL reconnected");
Huihong Luo02186fb2022-02-23 14:21:54 -0800184 WindowInfosListenerReporter::getInstance()->reconnect(instance.mComposerService);
Huihong Luof5029222021-12-16 14:33:46 -0800185 }
186 }
187 return instance.mComposerService;
188}
189
190void ComposerServiceAIDL::composerServiceDied() {
191 std::scoped_lock lock(mMutex);
192 mComposerService = nullptr;
193 mDeathObserver = nullptr;
194}
195
Robert Carrfb4d58b2019-01-15 09:21:27 -0800196class DefaultComposerClient: public Singleton<DefaultComposerClient> {
197 Mutex mLock;
198 sp<SurfaceComposerClient> mClient;
199 friend class Singleton<ComposerService>;
200public:
201 static sp<SurfaceComposerClient> getComposerClient() {
202 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
203 Mutex::Autolock _l(dc.mLock);
204 if (dc.mClient == nullptr) {
Anton Ivanov8ed86592025-02-20 11:52:50 -0800205 dc.mClient = sp<SurfaceComposerClient>::make();
Robert Carrfb4d58b2019-01-15 09:21:27 -0800206 }
207 return dc.mClient;
208 }
209};
210ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
211
212
213sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
214 return DefaultComposerClient::getComposerClient();
215}
216
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200217// ---------------------------------------------------------------------------
218
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100219JankDataListener::~JankDataListener() {
220}
221
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200222status_t JankDataListener::flushJankData() {
223 if (mLayerId == -1) {
224 return INVALID_OPERATION;
225 }
226
227 binder::Status status = ComposerServiceAIDL::getComposerService()->flushJankData(mLayerId);
228 return statusTFromBinderStatus(status);
229}
230
231std::mutex JankDataListenerFanOut::sFanoutInstanceMutex;
232std::unordered_map<int32_t, sp<JankDataListenerFanOut>> JankDataListenerFanOut::sFanoutInstances;
233
234binder::Status JankDataListenerFanOut::onJankData(const std::vector<gui::JankData>& jankData) {
235 // Find the highest VSync ID.
236 int64_t lastVsync = jankData.empty()
237 ? 0
238 : std::max_element(jankData.begin(), jankData.end(),
239 [](const gui::JankData& jd1, const gui::JankData& jd2) {
240 return jd1.frameVsyncId < jd2.frameVsyncId;
241 })
242 ->frameVsyncId;
243
244 // Fan out the jank data callback.
245 std::vector<wp<JankDataListener>> listenersToRemove;
246 for (auto listener : getActiveListeners()) {
247 if (!listener->onJankDataAvailable(jankData) ||
248 (listener->mRemoveAfter >= 0 && listener->mRemoveAfter <= lastVsync)) {
249 listenersToRemove.push_back(listener);
250 }
251 }
252
253 return removeListeners(listenersToRemove)
254 ? binder::Status::ok()
255 : binder::Status::fromExceptionCode(binder::Status::EX_NULL_POINTER);
256}
257
258status_t JankDataListenerFanOut::addListener(sp<SurfaceControl> sc, sp<JankDataListener> listener) {
259 sp<IBinder> layer = sc->getHandle();
260 if (layer == nullptr) {
261 return UNEXPECTED_NULL;
262 }
263 int32_t layerId = sc->getLayerId();
264
265 sFanoutInstanceMutex.lock();
266 auto it = sFanoutInstances.find(layerId);
267 bool registerNeeded = it == sFanoutInstances.end();
268 sp<JankDataListenerFanOut> fanout;
269 if (registerNeeded) {
270 fanout = sp<JankDataListenerFanOut>::make(layerId);
271 sFanoutInstances.insert({layerId, fanout});
272 } else {
273 fanout = it->second;
274 }
275
276 fanout->mMutex.lock();
277 fanout->mListeners.insert(listener);
278 fanout->mMutex.unlock();
279
280 sFanoutInstanceMutex.unlock();
281
282 if (registerNeeded) {
283 binder::Status status =
284 ComposerServiceAIDL::getComposerService()->addJankListener(layer, fanout);
285 return statusTFromBinderStatus(status);
286 }
287 return OK;
288}
289
290status_t JankDataListenerFanOut::removeListener(sp<JankDataListener> listener) {
291 int32_t layerId = listener->mLayerId;
292 if (layerId == -1) {
293 return INVALID_OPERATION;
294 }
295
296 int64_t removeAfter = INVALID_VSYNC;
297 sp<JankDataListenerFanOut> fanout;
298
299 sFanoutInstanceMutex.lock();
300 auto it = sFanoutInstances.find(layerId);
301 if (it != sFanoutInstances.end()) {
302 fanout = it->second;
303 removeAfter = fanout->updateAndGetRemovalVSync();
304 }
305
306 if (removeAfter != INVALID_VSYNC) {
307 // Remove this instance from the map, so that no new listeners are added
308 // while we're scheduled to be removed.
309 sFanoutInstances.erase(layerId);
310 }
311 sFanoutInstanceMutex.unlock();
312
313 if (removeAfter < 0) {
314 return OK;
315 }
316
317 binder::Status status =
318 ComposerServiceAIDL::getComposerService()->removeJankListener(layerId, fanout,
319 removeAfter);
320 return statusTFromBinderStatus(status);
321}
322
323std::vector<sp<JankDataListener>> JankDataListenerFanOut::getActiveListeners() {
324 std::scoped_lock<std::mutex> lock(mMutex);
325
326 std::vector<sp<JankDataListener>> listeners;
327 for (auto it = mListeners.begin(); it != mListeners.end();) {
328 auto listener = it->promote();
329 if (!listener) {
330 it = mListeners.erase(it);
331 } else {
332 listeners.push_back(std::move(listener));
333 it++;
334 }
335 }
336 return listeners;
337}
338
339bool JankDataListenerFanOut::removeListeners(const std::vector<wp<JankDataListener>>& listeners) {
340 std::scoped_lock<std::mutex> fanoutLock(sFanoutInstanceMutex);
341 std::scoped_lock<std::mutex> listenersLock(mMutex);
342
343 for (auto listener : listeners) {
344 mListeners.erase(listener);
345 }
346
347 if (mListeners.empty()) {
348 sFanoutInstances.erase(mLayerId);
349 return false;
350 }
351 return true;
352}
353
354int64_t JankDataListenerFanOut::updateAndGetRemovalVSync() {
355 std::scoped_lock<std::mutex> lock(mMutex);
356 if (mRemoveAfter >= 0) {
357 // We've already been scheduled to be removed. Don't schedule again.
358 return INVALID_VSYNC;
359 }
360
361 int64_t removeAfter = 0;
362 for (auto it = mListeners.begin(); it != mListeners.end();) {
363 auto listener = it->promote();
364 if (!listener) {
365 it = mListeners.erase(it);
366 } else if (listener->mRemoveAfter < 0) {
367 // We have at least one listener that's still interested. Don't remove.
368 return INVALID_VSYNC;
369 } else {
370 removeAfter = std::max(removeAfter, listener->mRemoveAfter);
371 it++;
372 }
373 }
374
375 mRemoveAfter = removeAfter;
376 return removeAfter;
377}
378
Mathias Agopian7e27f052010-05-28 14:22:23 -0700379// ---------------------------------------------------------------------------
380
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700381// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
382// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
383// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
384
Marissa Wallc837b5e2018-10-12 10:04:44 -0700385// 0 is an invalid callback id
386TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
387
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700388int64_t TransactionCompletedListener::getNextIdLocked() {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700389 return mCallbackIdCounter++;
390}
391
chaviw0b06a8d2021-08-06 11:49:08 -0500392sp<TransactionCompletedListener> TransactionCompletedListener::sInstance = nullptr;
Robert Carr2df7c452022-04-04 12:21:37 -0700393static std::mutex sListenerInstanceMutex;
chaviw0b06a8d2021-08-06 11:49:08 -0500394
395void TransactionCompletedListener::setInstance(const sp<TransactionCompletedListener>& listener) {
396 sInstance = listener;
397}
398
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700399sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
Robert Carr2df7c452022-04-04 12:21:37 -0700400 std::lock_guard<std::mutex> lock(sListenerInstanceMutex);
chaviw0b06a8d2021-08-06 11:49:08 -0500401 if (sInstance == nullptr) {
Anton Ivanov8ed86592025-02-20 11:52:50 -0800402 sInstance = sp<TransactionCompletedListener>::make();
chaviw0b06a8d2021-08-06 11:49:08 -0500403 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700404 return sInstance;
405}
406
Marissa Wallc837b5e2018-10-12 10:04:44 -0700407sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
408 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
409}
410
411void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700412 if (mListening) {
413 return;
414 }
415 ProcessState::self()->startThreadPool();
416 mListening = true;
417}
418
Marissa Wall80d94ad2019-01-18 16:04:36 -0800419CallbackId TransactionCompletedListener::addCallbackFunction(
420 const TransactionCompletedCallback& callbackFunction,
421 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700422 surfaceControls,
423 CallbackId::Type callbackType) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700424 std::lock_guard<std::mutex> lock(mMutex);
425 startListeningLocked();
426
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700427 CallbackId callbackId(getNextIdLocked(), callbackType);
Marissa Wall80d94ad2019-01-18 16:04:36 -0800428 mCallbacks[callbackId].callbackFunction = callbackFunction;
Marissa Wall80d94ad2019-01-18 16:04:36 -0800429 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
430
431 for (const auto& surfaceControl : surfaceControls) {
432 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
433 }
434
Marissa Wallc837b5e2018-10-12 10:04:44 -0700435 return callbackId;
436}
437
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700438void TransactionCompletedListener::setReleaseBufferCallback(const ReleaseCallbackId& callbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800439 ReleaseBufferCallback listener) {
440 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700441 mReleaseBufferCallbacks[callbackId] = listener;
Vishnu Nair1506b182021-02-22 14:35:15 -0800442}
443
Jorim Jaggif51775d2021-01-14 23:44:15 +0100444void TransactionCompletedListener::addSurfaceStatsListener(void* context, void* cookie,
445 sp<SurfaceControl> surfaceControl, SurfaceStatsCallback listener) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200446 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500447 mSurfaceStatsListeners.insert(
448 {surfaceControl->getLayerId(), SurfaceStatsCallbackEntry(context, cookie, listener)});
Jorim Jaggif51775d2021-01-14 23:44:15 +0100449}
450
451void TransactionCompletedListener::removeSurfaceStatsListener(void* context, void* cookie) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200452 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
Jorim Jaggif51775d2021-01-14 23:44:15 +0100453 for (auto it = mSurfaceStatsListeners.begin(); it != mSurfaceStatsListeners.end();) {
454 auto [itContext, itCookie, itListener] = it->second;
455 if (itContext == context && itCookie == cookie) {
456 it = mSurfaceStatsListeners.erase(it);
457 } else {
458 it++;
459 }
460 }
461}
462
Marissa Wall80d94ad2019-01-18 16:04:36 -0800463void TransactionCompletedListener::addSurfaceControlToCallbacks(
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200464 const sp<SurfaceControl>& surfaceControl,
465 const std::unordered_set<CallbackId, CallbackIdHash>& callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800466 std::lock_guard<std::mutex> lock(mMutex);
467
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200468 for (auto callbackId : callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800469 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
470 std::forward_as_tuple(
471 surfaceControl->getHandle()),
472 std::forward_as_tuple(surfaceControl));
473 }
474}
475
Huihong Luoffee3bc2023-01-17 16:14:35 +0000476void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700477 std::unordered_map<CallbackId, CallbackTranslation, CallbackIdHash> callbacksMap;
Valerie Haud3b90d22019-11-06 09:37:31 -0800478 {
479 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700480
Valerie Haud3b90d22019-11-06 09:37:31 -0800481 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
482 * callbackIds, except for when Transactions are merged together. This probably cannot be
483 * solved before this point because the Transactions could be merged together and applied in
484 * a different process.
485 *
486 * Fortunately, we get all the callbacks for this listener for the same frame together at
487 * the same time. This means if any Transactions were merged together, we will get their
488 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
489 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
490 * sp<SurfaceControl> that could possibly exist for the callbacks.
491 */
492 callbacksMap = mCallbacks;
493 for (const auto& transactionStats : listenerStats.transactionStats) {
494 for (auto& callbackId : transactionStats.callbackIds) {
495 mCallbacks.erase(callbackId);
496 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800497 }
498 }
Marissa Walld600d572019-03-26 15:38:50 -0700499 for (const auto& transactionStats : listenerStats.transactionStats) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700500 // handle on commit callbacks
Marissa Walld600d572019-03-26 15:38:50 -0700501 for (auto callbackId : transactionStats.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700502 if (callbackId.type != CallbackId::Type::ON_COMMIT) {
503 continue;
504 }
505 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
506 if (!callbackFunction) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700507 continue;
508 }
509 std::vector<SurfaceControlStats> surfaceControlStats;
510 for (const auto& surfaceStats : transactionStats.surfaceStats) {
511 surfaceControlStats
512 .emplace_back(callbacksMap[callbackId]
513 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800514 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700515 transactionStats.presentFence,
516 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800517 surfaceStats.eventStats,
518 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700519 }
520
521 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
522 surfaceControlStats);
Patrick Williams30da3672023-07-25 16:52:01 -0500523
524 // More than one transaction may contain the same callback id. Erase the callback from
525 // the map to ensure that it is only called once. This can happen if transactions are
526 // parcelled out of process and applied in both processes.
527 callbacksMap.erase(callbackId);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700528 }
529
530 // handle on complete callbacks
531 for (auto callbackId : transactionStats.callbackIds) {
532 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
533 continue;
534 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800535 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-18 16:04:36 -0800536 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700537 ALOGE("cannot call null callback function, skipping");
538 continue;
539 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800540 std::vector<SurfaceControlStats> surfaceControlStats;
541 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800542 surfaceControlStats
543 .emplace_back(callbacksMap[callbackId]
544 .surfaceControls[surfaceStats.surfaceControl],
Ady Abraham461296a2022-01-21 11:11:31 -0800545 transactionStats.latchTime, surfaceStats.acquireTimeOrFence,
Valerie Hau871d6352020-01-29 08:44:02 -0800546 transactionStats.presentFence,
547 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
Robert Carr405e2f62021-12-31 16:59:34 -0800548 surfaceStats.eventStats,
549 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair71fcf912022-10-18 09:14:20 -0700550 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl] &&
551 surfaceStats.transformHint.has_value()) {
Valerie Hau84d87ff2020-01-08 17:23:21 -0800552 callbacksMap[callbackId]
553 .surfaceControls[surfaceStats.surfaceControl]
Vishnu Nair71fcf912022-10-18 09:14:20 -0700554 ->setTransformHint(*surfaceStats.transformHint);
Valerie Hau84d87ff2020-01-08 17:23:21 -0800555 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800556 // If there is buffer id set, we look up any pending client release buffer callbacks
557 // and call them. This is a performance optimization when we have a transaction
558 // callback and a release buffer callback happening at the same time to avoid an
559 // additional ipc call from the server.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700560 if (surfaceStats.previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
Vishnu Nair5faedf52021-03-22 11:32:58 -0700561 ReleaseBufferCallback callback;
562 {
563 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700564 callback = popReleaseBufferCallbackLocked(
565 surfaceStats.previousReleaseCallbackId);
Vishnu Nair5faedf52021-03-22 11:32:58 -0700566 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800567 if (callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700568 callback(surfaceStats.previousReleaseCallbackId,
Vishnu Nair1506b182021-02-22 14:35:15 -0800569 surfaceStats.previousReleaseFence
570 ? surfaceStats.previousReleaseFence
Robert Carr97e7cc02021-06-07 10:45:40 -0700571 : Fence::NO_FENCE,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700572 surfaceStats.currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800573 }
574 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800575 }
576
577 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
578 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700579 }
Vishnu Nair6f4d2f52023-08-10 12:33:12 -0700580 }
chaviw29bc5362021-09-07 16:17:13 -0500581
Vishnu Nair6f4d2f52023-08-10 12:33:12 -0700582 for (const auto& transactionStats : listenerStats.transactionStats) {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100583 for (const auto& surfaceStats : transactionStats.surfaceStats) {
chaviw29bc5362021-09-07 16:17:13 -0500584 // The callbackMap contains the SurfaceControl object, which we need to look up the
585 // layerId. Since we don't know which callback contains the SurfaceControl, iterate
586 // through all until the SC is found.
587 int32_t layerId = -1;
588 for (auto callbackId : transactionStats.callbackIds) {
Pablo Gamito10026472021-11-22 22:16:45 +0100589 if (callbackId.type != CallbackId::Type::ON_COMPLETE) {
590 // We only want to run the stats callback for ON_COMPLETE
591 continue;
592 }
chaviw29bc5362021-09-07 16:17:13 -0500593 sp<SurfaceControl> sc =
594 callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl];
595 if (sc != nullptr) {
596 layerId = sc->getLayerId();
597 break;
598 }
599 }
600
Pablo Gamito10026472021-11-22 22:16:45 +0100601 if (layerId != -1) {
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200602 // Acquire surface stats listener lock such that we guarantee that after calling
603 // unregister, there won't be any further callback.
604 std::scoped_lock<std::recursive_mutex> lock(mSurfaceStatsListenerMutex);
chaviw29bc5362021-09-07 16:17:13 -0500605 auto listenerRange = mSurfaceStatsListeners.equal_range(layerId);
Jorim Jaggi6ed2ea82021-06-16 17:49:08 +0200606 for (auto it = listenerRange.first; it != listenerRange.second; it++) {
607 auto entry = it->second;
608 entry.callback(entry.context, transactionStats.latchTime,
609 transactionStats.presentFence, surfaceStats);
610 }
Jorim Jaggif51775d2021-01-14 23:44:15 +0100611 }
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100612 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700613 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700614}
615
Huihong Luoffee3bc2023-01-17 16:14:35 +0000616void TransactionCompletedListener::onTransactionQueueStalled(const String8& reason) {
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000617 std::unordered_map<void*, std::function<void(const std::string&)>> callbackCopy;
618 {
619 std::scoped_lock<std::mutex> lock(mMutex);
620 callbackCopy = mQueueStallListeners;
621 }
622 for (auto const& it : callbackCopy) {
623 it.second(reason.c_str());
624 }
Robert Carr4c1b6462021-12-21 10:30:50 -0800625}
626
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000627void TransactionCompletedListener::addQueueStallListener(
628 std::function<void(const std::string&)> stallListener, void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800629 std::scoped_lock<std::mutex> lock(mMutex);
630 mQueueStallListeners[id] = stallListener;
631}
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000632
633void TransactionCompletedListener::removeQueueStallListener(void* id) {
Robert Carr4c1b6462021-12-21 10:30:50 -0800634 std::scoped_lock<std::mutex> lock(mMutex);
635 mQueueStallListeners.erase(id);
636}
637
Huihong Luoffee3bc2023-01-17 16:14:35 +0000638void TransactionCompletedListener::onReleaseBuffer(ReleaseCallbackId callbackId,
639 sp<Fence> releaseFence,
640 uint32_t currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800641 ReleaseBufferCallback callback;
642 {
643 std::scoped_lock<std::mutex> lock(mMutex);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700644 callback = popReleaseBufferCallbackLocked(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800645 }
646 if (!callback) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700647 ALOGE("Could not call release buffer callback, buffer not found %s",
648 callbackId.to_string().c_str());
Huihong Luoffee3bc2023-01-17 16:14:35 +0000649 return;
Vishnu Nair1506b182021-02-22 14:35:15 -0800650 }
chaviw69058fb2021-09-27 09:37:30 -0500651 std::optional<uint32_t> optionalMaxAcquiredBufferCount =
Huihong Luoffee3bc2023-01-17 16:14:35 +0000652 currentMaxAcquiredBufferCount == UINT_MAX
chaviw69058fb2021-09-27 09:37:30 -0500653 ? std::nullopt
654 : std::make_optional<uint32_t>(currentMaxAcquiredBufferCount);
655 callback(callbackId, releaseFence, optionalMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800656}
657
658ReleaseBufferCallback TransactionCompletedListener::popReleaseBufferCallbackLocked(
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700659 const ReleaseCallbackId& callbackId) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800660 ReleaseBufferCallback callback;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700661 auto itr = mReleaseBufferCallbacks.find(callbackId);
Vishnu Nair1506b182021-02-22 14:35:15 -0800662 if (itr == mReleaseBufferCallbacks.end()) {
663 return nullptr;
664 }
665 callback = itr->second;
666 mReleaseBufferCallbacks.erase(itr);
667 return callback;
668}
669
chaviw0acd33a2021-11-02 11:55:37 -0500670void TransactionCompletedListener::removeReleaseBufferCallback(
671 const ReleaseCallbackId& callbackId) {
672 {
673 std::scoped_lock<std::mutex> lock(mMutex);
674 popReleaseBufferCallbackLocked(callbackId);
675 }
676}
677
Chavi Weingarten076acac2023-01-19 17:20:43 +0000678SurfaceComposerClient::PresentationCallbackRAII::PresentationCallbackRAII(
679 TransactionCompletedListener* tcl, int id) {
Priyanka Advani (xWF)f73db7a2025-02-19 17:35:16 -0800680 mTcl = tcl;
Chavi Weingarten076acac2023-01-19 17:20:43 +0000681 mId = id;
682}
683
684SurfaceComposerClient::PresentationCallbackRAII::~PresentationCallbackRAII() {
685 mTcl->clearTrustedPresentationCallback(mId);
686}
687
688sp<SurfaceComposerClient::PresentationCallbackRAII>
689TransactionCompletedListener::addTrustedPresentationCallback(TrustedPresentationCallback tpc,
690 int id, void* context) {
691 std::scoped_lock<std::mutex> lock(mMutex);
692 mTrustedPresentationCallbacks[id] =
693 std::tuple<TrustedPresentationCallback, void*>(tpc, context);
Anton Ivanov8ed86592025-02-20 11:52:50 -0800694 return sp<SurfaceComposerClient::PresentationCallbackRAII>::make(this, id);
Chavi Weingarten076acac2023-01-19 17:20:43 +0000695}
696
697void TransactionCompletedListener::clearTrustedPresentationCallback(int id) {
698 std::scoped_lock<std::mutex> lock(mMutex);
699 mTrustedPresentationCallbacks.erase(id);
700}
701
702void TransactionCompletedListener::onTrustedPresentationChanged(int id,
703 bool presentedWithinThresholds) {
704 TrustedPresentationCallback tpc;
705 void* context;
706 {
707 std::scoped_lock<std::mutex> lock(mMutex);
708 auto it = mTrustedPresentationCallbacks.find(id);
709 if (it == mTrustedPresentationCallbacks.end()) {
710 return;
711 }
712 std::tie(tpc, context) = it->second;
713 }
714 tpc(context, presentedWithinThresholds);
715}
716
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700717// ---------------------------------------------------------------------------
718
Robert Carre06ad2b2020-04-10 15:09:33 -0700719void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 14:58:34 -0700720
Robert Carre06ad2b2020-04-10 15:09:33 -0700721/**
722 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
723 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
724 * 1. Cost of sending the FD
725 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
726 * To ease this cost we implement the following scheme of caching buffers to integers,
727 * or said-otherwise, naming them with integers. This is the scheme known as slots in
728 * the legacy BufferQueue system.
729 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
730 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
731 * send the cached integer.
732 * 3. If there is a cache miss, we cache the new buffer and send the integer
733 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
734 * entry, and we use the integer for further communication.
735 * A few details about lifetime:
736 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
737 * which is per process Unique. The server side cache is larger than the client side
738 * cache so that the server will never evict entries before the client.
739 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
740 * transaction.
741 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
742 * to auto-evict destroyed buffers.
743 */
Marissa Wall73411622019-01-25 10:45:41 -0800744class BufferCache : public Singleton<BufferCache> {
745public:
Anton Ivanov8ed86592025-02-20 11:52:50 -0800746 BufferCache() : token(sp<BBinder>::make()) {}
Marissa Wall73411622019-01-25 10:45:41 -0800747
748 sp<IBinder> getToken() {
749 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
750 }
751
Marissa Wall78b72202019-03-15 14:58:34 -0700752 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800753 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800754
Marissa Wall78b72202019-03-15 14:58:34 -0700755 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800756 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700757 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800758 }
Marissa Wall78b72202019-03-15 14:58:34 -0700759 itr->second = getCounter();
760 *cacheId = buffer->getId();
761 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800762 }
763
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000764 uint64_t cache(const sp<GraphicBuffer>& buffer,
765 std::optional<client_cache_t>& outUncacheBuffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800766 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800767
Marissa Wall78b72202019-03-15 14:58:34 -0700768 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000769 outUncacheBuffer = findLeastRecentlyUsedBuffer();
770 mBuffers.erase(outUncacheBuffer->id);
Marissa Wall78b72202019-03-15 14:58:34 -0700771 }
Marissa Wall73411622019-01-25 10:45:41 -0800772
Robert Carre06ad2b2020-04-10 15:09:33 -0700773 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 14:58:34 -0700774
775 mBuffers[buffer->getId()] = getCounter();
776 return buffer->getId();
777 }
778
779 void uncache(uint64_t cacheId) {
780 std::lock_guard<std::mutex> lock(mMutex);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000781 if (mBuffers.erase(cacheId)) {
782 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
783 }
Marissa Wall73411622019-01-25 10:45:41 -0800784 }
785
786private:
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000787 client_cache_t findLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800788 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700789 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800790 auto minBuffer = itr;
791 itr++;
792
793 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700794 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800795 if (counter < minCounter) {
796 minCounter = counter;
797 minBuffer = itr;
798 }
799 itr++;
800 }
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000801
802 return {.token = getToken(), .id = minBuffer->first};
Marissa Wall73411622019-01-25 10:45:41 -0800803 }
804
805 uint64_t getCounter() REQUIRES(mMutex) {
806 static uint64_t counter = 0;
807 return counter++;
808 }
809
Marissa Wall73411622019-01-25 10:45:41 -0800810 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700811 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800812
813 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
814 sp<IBinder> token;
815};
816
817ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
818
Robert Carre06ad2b2020-04-10 15:09:33 -0700819void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 14:58:34 -0700820 // GraphicBuffer id's are used as the cache ids.
821 BufferCache::getInstance().uncache(graphicBufferId);
822}
823
Marissa Wall73411622019-01-25 10:45:41 -0800824// ---------------------------------------------------------------------------
825
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000826SurfaceComposerClient::Transaction::Transaction() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700827 mId = generateId();
Vishnu Naira74ba0f2024-04-16 11:25:06 -0700828 mTransactionCompletedListener = TransactionCompletedListener::getInstance();
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000829}
830
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700831SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
832 : mId(other.mId),
833 mFlags(other.mFlags),
834 mMayContainBuffer(other.mMayContainBuffer),
835 mDesiredPresentTime(other.mDesiredPresentTime),
836 mIsAutoTimestamp(other.mIsAutoTimestamp),
837 mFrameTimelineInfo(other.mFrameTimelineInfo),
838 mApplyToken(other.mApplyToken) {
839 mDisplayStates = other.mDisplayStates;
840 mComposerStates = other.mComposerStates;
841 mInputWindowCommands = other.mInputWindowCommands;
842 mListenerCallbacks = other.mListenerCallbacks;
843 mTransactionCompletedListener = TransactionCompletedListener::getInstance();
844}
Vishnu Nair621102e2019-06-12 14:16:57 -0700845
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000846void SurfaceComposerClient::Transaction::sanitize(int pid, int uid) {
847 uint32_t permissions = LayerStatePermissions::getTransactionPermissions(pid, uid);
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700848 for (auto& composerState : mComposerStates) {
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000849 composerState.state.sanitize(permissions);
Robert Carrde6d7b42022-01-07 18:23:06 -0800850 }
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700851 if (!mInputWindowCommands.empty() &&
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000852 (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) {
853 ALOGE("Only privileged callers are allowed to send input commands.");
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700854 mInputWindowCommands.clear();
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000855 }
Robert Carrde6d7b42022-01-07 18:23:06 -0800856}
857
Vishnu Nair621102e2019-06-12 14:16:57 -0700858std::unique_ptr<SurfaceComposerClient::Transaction>
859SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
860 auto transaction = std::make_unique<Transaction>();
861 if (transaction->readFromParcel(parcel) == NO_ERROR) {
862 return transaction;
863 }
864 return nullptr;
865}
866
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000867
Vishnu Nair621102e2019-06-12 14:16:57 -0700868status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700869 const uint64_t transactionId = parcel->readUint64();
870 const uint32_t flags = parcel->readUint32();
871 const int64_t desiredPresentTime = parcel->readInt64();
872 const bool isAutoTimestamp = parcel->readBool();
873 const bool logCallPoints = parcel->readBool();
874 FrameTimelineInfo frameTimelineInfo;
875 frameTimelineInfo.readFromParcel(parcel);
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000876
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700877 sp<IBinder> applyToken;
878 parcel->readNullableStrongBinder(&applyToken);
Vishnu Nair621102e2019-06-12 14:16:57 -0700879 size_t count = static_cast<size_t>(parcel->readUint32());
880 if (count > parcel->dataSize()) {
881 return BAD_VALUE;
882 }
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700883 Vector<DisplayState> displayStates;
884 displayStates.setCapacity(count);
885 for (size_t i = 0; i < count; i++) {
886 DisplayState displayState;
887 if (displayState.read(*parcel) == BAD_VALUE) {
888 return BAD_VALUE;
889 }
890 displayStates.add(displayState);
891 }
892
893 count = static_cast<size_t>(parcel->readUint32());
894 if (count > parcel->dataSize()) {
895 return BAD_VALUE;
896 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700897 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
898 listenerCallbacks.reserve(count);
899 for (size_t i = 0; i < count; i++) {
900 sp<ITransactionCompletedListener> listener =
901 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
902 size_t numCallbackIds = parcel->readUint32();
903 if (numCallbackIds > parcel->dataSize()) {
904 return BAD_VALUE;
905 }
906 for (size_t j = 0; j < numCallbackIds; j++) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700907 CallbackId id;
908 parcel->readParcelable(&id);
909 listenerCallbacks[listener].callbackIds.insert(id);
Valerie Hau9dab9732019-08-20 09:29:25 -0700910 }
911 size_t numSurfaces = parcel->readUint32();
912 if (numSurfaces > parcel->dataSize()) {
913 return BAD_VALUE;
914 }
915 for (size_t j = 0; j < numSurfaces; j++) {
916 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000917 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 09:29:25 -0700918 listenerCallbacks[listener].surfaceControls.insert(surface);
919 }
920 }
921
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700922 count = static_cast<size_t>(parcel->readUint32());
923 if (count > parcel->dataSize()) {
924 return BAD_VALUE;
925 }
926 Vector<ComposerState> composerStates;
927 composerStates.setCapacity(count);
928 for (size_t i = 0; i < count; i++) {
929 ComposerState composerState;
930 if (composerState.read(*parcel) == BAD_VALUE) {
931 return BAD_VALUE;
932 }
933 composerStates.add(composerState);
934 }
935
936 InputWindowCommands inputWindowCommands;
937 inputWindowCommands.read(*parcel);
938
939 count = static_cast<size_t>(parcel->readUint32());
940 if (count > parcel->dataSize()) {
941 return BAD_VALUE;
942 }
943 std::vector<client_cache_t> uncacheBuffers(count);
944 for (size_t i = 0; i < count; i++) {
945 sp<IBinder> tmpBinder;
946 SAFE_PARCEL(parcel->readStrongBinder, &tmpBinder);
947 uncacheBuffers[i].token = tmpBinder;
948 SAFE_PARCEL(parcel->readUint64, &uncacheBuffers[i].id);
949 }
950
951 count = static_cast<size_t>(parcel->readUint32());
952 if (count > parcel->dataSize()) {
953 return BAD_VALUE;
954 }
955 std::vector<uint64_t> mergedTransactionIds(count);
956 for (size_t i = 0; i < count; i++) {
957 SAFE_PARCEL(parcel->readUint64, &mergedTransactionIds[i]);
958 }
959
960 // Parsing was successful. Update the object.
961 mId = transactionId;
962 mFlags = flags;
963 mDesiredPresentTime = desiredPresentTime;
964 mIsAutoTimestamp = isAutoTimestamp;
965 mFrameTimelineInfo = frameTimelineInfo;
966 mDisplayStates = std::move(displayStates);
967 mListenerCallbacks = listenerCallbacks;
968 mComposerStates = std::move(composerStates);
969 mInputWindowCommands = inputWindowCommands;
970 mApplyToken = applyToken;
971 mUncacheBuffers = std::move(uncacheBuffers);
972 mMergedTransactionIds = std::move(mergedTransactionIds);
Vishnu Nair621102e2019-06-12 14:16:57 -0700973 return NO_ERROR;
974}
975
976status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700977 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
978 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
979 // but is unlikely to use them again as they are owned by the other process.
980 // You may be asking yourself, is this const cast safe? Const cast is safe up
981 // until the point where you try and write to an object that was originally const at which
982 // point we enter undefined behavior. In this case we are safe though, because there are
983 // two possibilities:
984 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
985 // 2. It was originall const! In this case not only was it useless, but it by definition
986 // contains no composer states and so cacheBuffers will not perform any writes.
987
988 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
989
Anton Ivanov4f8b4f32025-03-17 16:29:09 -0700990 parcel->writeUint64(mId);
991 parcel->writeUint32(mFlags);
992 parcel->writeInt64(mDesiredPresentTime);
993 parcel->writeBool(mIsAutoTimestamp);
994 parcel->writeBool(mLogCallPoints);
995 mFrameTimelineInfo.writeToParcel(parcel);
996 parcel->writeStrongBinder(mApplyToken);
997 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
998 for (auto const& displayState : mDisplayStates) {
999 displayState.write(*parcel);
1000 }
Vishnu Nair621102e2019-06-12 14:16:57 -07001001
Valerie Hau9dab9732019-08-20 09:29:25 -07001002 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
1003 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
1004 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
1005 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
1006 for (auto callbackId : callbackInfo.callbackIds) {
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07001007 parcel->writeParcelable(callbackId);
Valerie Hau9dab9732019-08-20 09:29:25 -07001008 }
1009 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
1010 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45 +00001011 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 09:29:25 -07001012 }
1013 }
1014
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001015 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
1016 for (auto const& composerState : mComposerStates) {
1017 composerState.write(*parcel);
1018 }
1019
1020 mInputWindowCommands.write(*parcel);
1021
1022 SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mUncacheBuffers.size()));
1023 for (const client_cache_t& uncacheBuffer : mUncacheBuffers) {
1024 SAFE_PARCEL(parcel->writeStrongBinder, uncacheBuffer.token.promote());
1025 SAFE_PARCEL(parcel->writeUint64, uncacheBuffer.id);
1026 }
1027
1028 SAFE_PARCEL(parcel->writeUint32, static_cast<uint32_t>(mMergedTransactionIds.size()));
1029 for (auto mergedTransactionId : mMergedTransactionIds) {
1030 SAFE_PARCEL(parcel->writeUint64, mergedTransactionId);
1031 }
1032
Vishnu Nair621102e2019-06-12 14:16:57 -07001033 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -07001034}
1035
chaviw69058fb2021-09-27 09:37:30 -05001036void SurfaceComposerClient::Transaction::releaseBufferIfOverwriting(const layer_state_t& state) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001037 if (!(state.what & layer_state_t::eBufferChanged) || !state.bufferData->hasBuffer()) {
chaviw69058fb2021-09-27 09:37:30 -05001038 return;
1039 }
1040
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001041 auto listener = state.bufferData->releaseBufferListener;
chaviw69058fb2021-09-27 09:37:30 -05001042 sp<Fence> fence =
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001043 state.bufferData->acquireFence ? state.bufferData->acquireFence : Fence::NO_FENCE;
1044 if (state.bufferData->releaseBufferEndpoint ==
chaviw69058fb2021-09-27 09:37:30 -05001045 IInterface::asBinder(TransactionCompletedListener::getIInstance())) {
1046 // if the callback is in process, run on a different thread to avoid any lock contigency
1047 // issues in the client.
1048 SurfaceComposerClient::getDefault()
chaviw8dd181f2022-01-05 18:36:46 -06001049 ->mReleaseCallbackThread
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001050 .addReleaseCallback(state.bufferData->generateReleaseCallbackId(), fence);
chaviw69058fb2021-09-27 09:37:30 -05001051 } else {
Huihong Luoffee3bc2023-01-17 16:14:35 +00001052 listener->onReleaseBuffer(state.bufferData->generateReleaseCallbackId(), fence, UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -05001053 }
1054}
1055
Robert Carr2c5f6d22017-09-26 12:30:35 -07001056SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001057 while (mMergedTransactionIds.size() + other.mMergedTransactionIds.size() >
1058 MAX_MERGE_HISTORY_LENGTH - 1 &&
1059 mMergedTransactionIds.size() > 0) {
1060 mMergedTransactionIds.pop_back();
1061 }
1062 if (other.mMergedTransactionIds.size() == MAX_MERGE_HISTORY_LENGTH) {
1063 mMergedTransactionIds.insert(mMergedTransactionIds.begin(),
1064 other.mMergedTransactionIds.begin(),
1065 other.mMergedTransactionIds.end() - 1);
1066 } else if (other.mMergedTransactionIds.size() > 0u) {
1067 mMergedTransactionIds.insert(mMergedTransactionIds.begin(),
1068 other.mMergedTransactionIds.begin(),
1069 other.mMergedTransactionIds.end());
1070 }
1071 mMergedTransactionIds.insert(mMergedTransactionIds.begin(), other.mId);
1072
1073 for (auto const& otherState : other.mComposerStates) {
1074 if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(),
1075 [&otherState](const auto& composerState) {
1076 return composerState.state.surface ==
1077 otherState.state.surface;
1078 });
1079 it != mComposerStates.end()) {
1080 if (otherState.state.what & layer_state_t::eBufferChanged) {
1081 releaseBufferIfOverwriting(it->state);
1082 }
1083 it->state.merge(otherState.state);
1084 } else {
1085 mComposerStates.add(otherState);
1086 }
1087 }
1088
1089 for (auto const& state : other.mDisplayStates) {
1090 if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(),
1091 [&state](const auto& displayState) {
1092 return displayState.token == state.token;
1093 });
1094 it != mDisplayStates.end()) {
1095 it->merge(state);
1096 } else {
1097 mDisplayStates.add(state);
1098 }
1099 }
1100
Marissa Wallc837b5e2018-10-12 10:04:44 -07001101 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
1102 auto& [callbackIds, surfaceControls] = callbackInfo;
1103 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
1104 callbackIds.begin()),
1105 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -07001106
Valerie Hau236eba32020-01-03 16:53:39 -08001107 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
1108 surfaceControls.end());
1109
1110 auto& currentProcessCallbackInfo =
1111 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
1112 currentProcessCallbackInfo.surfaceControls
1113 .insert(std::make_move_iterator(surfaceControls.begin()),
1114 std::make_move_iterator(surfaceControls.end()));
1115
1116 // register all surface controls for all callbackIds for this listener that is merging
1117 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
Pascal Mütschardd56514e2024-05-24 17:37:13 +02001118 mTransactionCompletedListener
1119 ->addSurfaceControlToCallbacks(surfaceControl,
1120 currentProcessCallbackInfo.callbackIds);
Valerie Hau236eba32020-01-03 16:53:39 -08001121 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001122 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001123
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001124 for (const auto& cacheId : other.mUncacheBuffers) {
1125 mUncacheBuffers.push_back(cacheId);
1126 }
1127
1128 mInputWindowCommands.merge(other.mInputWindowCommands);
1129
1130 mMayContainBuffer |= other.mMayContainBuffer;
1131 mFlags |= other.mFlags;
1132 mApplyToken = other.mApplyToken;
1133
1134 mergeFrameTimelineInfo(mFrameTimelineInfo, other.mFrameTimelineInfo);
1135
1136 mLogCallPoints |= other.mLogCallPoints;
1137 if (mLogCallPoints) {
1138 ALOG(LOG_DEBUG, LOG_SURFACE_CONTROL_REGISTRY,
1139 "Transaction %" PRIu64 " merged with transaction %" PRIu64, other.getId(), mId);
1140 }
1141
Vishnu Nairfef244e2019-06-17 18:07:51 -07001142 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -07001143 return *this;
1144}
1145
Vishnu Nairfef244e2019-06-17 18:07:51 -07001146void SurfaceComposerClient::Transaction::clear() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001147 mComposerStates.clear();
1148 mDisplayStates.clear();
Vishnu Nairfef244e2019-06-17 18:07:51 -07001149 mListenerCallbacks.clear();
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001150 mInputWindowCommands.clear();
1151 mUncacheBuffers.clear();
1152 mMayContainBuffer = false;
1153 mDesiredPresentTime = 0;
1154 mIsAutoTimestamp = true;
1155 mFrameTimelineInfo = {};
1156 mApplyToken = nullptr;
1157 mMergedTransactionIds.clear();
1158 mLogCallPoints = false;
1159 mFlags = 0;
Vishnu Nairfef244e2019-06-17 18:07:51 -07001160}
1161
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001162uint64_t SurfaceComposerClient::Transaction::getId() {
1163 return mId;
Pablo Gamito3e4e0be2022-05-27 17:26:24 +00001164}
1165
Pablo Gamito23780be2023-04-18 08:30:00 +00001166std::vector<uint64_t> SurfaceComposerClient::Transaction::getMergedTransactionIds() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001167 return mMergedTransactionIds;
Pablo Gamito23780be2023-04-18 08:30:00 +00001168}
1169
Marissa Wall78b72202019-03-15 14:58:34 -07001170void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
1171 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1172
Marissa Wall947d34e2019-03-29 14:03:53 -07001173 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -07001174 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -07001175 uncacheBuffer.id = cacheId;
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001176 Vector<ComposerState> composerStates;
1177 Vector<DisplayState> displayStates;
1178 status_t status = sf->setTransactionState(FrameTimelineInfo{}, composerStates, displayStates,
1179 ISurfaceComposer::eOneWay,
1180 Transaction::getDefaultApplyToken(), {}, systemTime(),
1181 true, {uncacheBuffer}, false, {}, generateId(), {});
Patrick Williams640b7292022-10-11 19:25:06 +00001182 if (status != NO_ERROR) {
1183 ALOGE_AND_TRACE("SurfaceComposerClient::doUncacheBufferTransaction - %s",
1184 strerror(-status));
1185 }
Marissa Wall78b72202019-03-15 14:58:34 -07001186}
1187
1188void SurfaceComposerClient::Transaction::cacheBuffers() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001189 if (!mMayContainBuffer) {
Marissa Wall78b72202019-03-15 14:58:34 -07001190 return;
1191 }
1192
1193 size_t count = 0;
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001194 for (auto& cs : mComposerStates) {
Vishnu Nair7cea62b2025-01-16 23:56:45 +00001195 layer_state_t* s = &cs.state;
Marissa Wall78b72202019-03-15 14:58:34 -07001196 if (!(s->what & layer_state_t::eBufferChanged)) {
1197 continue;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001198 } else if (s->bufferData &&
1199 s->bufferData->flags.test(BufferData::BufferDataChange::cachedBufferChanged)) {
Robert Carr28037922020-04-08 10:57:07 -07001200 // If eBufferChanged and eCachedBufferChanged are both trued then that means
1201 // we already cached the buffer in a previous call to cacheBuffers, perhaps
1202 // from writeToParcel on a Transaction that was merged in to this one.
1203 continue;
Marissa Wall78b72202019-03-15 14:58:34 -07001204 }
1205
Marissa Wall00597242019-03-27 10:35:19 -07001206 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
1207 // time trying to cache them.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001208 if (!s->bufferData || !s->bufferData->buffer) {
Marissa Wall00597242019-03-27 10:35:19 -07001209 continue;
1210 }
1211
Marissa Wall78b72202019-03-15 14:58:34 -07001212 uint64_t cacheId = 0;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001213 status_t ret = BufferCache::getInstance().getCacheId(s->bufferData->buffer, &cacheId);
Marissa Wall78b72202019-03-15 14:58:34 -07001214 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -07001215 // Cache-hit. Strip the buffer and send only the id.
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001216 s->bufferData->buffer = nullptr;
Marissa Wall78b72202019-03-15 14:58:34 -07001217 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -07001218 // Cache-miss. Include the buffer and send the new cacheId.
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001219 std::optional<client_cache_t> uncacheBuffer;
1220 cacheId = BufferCache::getInstance().cache(s->bufferData->buffer, uncacheBuffer);
1221 if (uncacheBuffer) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001222 mUncacheBuffers.push_back(*uncacheBuffer);
Patrick Williams6c6dd3b2023-02-13 22:53:06 +00001223 }
Marissa Wall78b72202019-03-15 14:58:34 -07001224 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001225 s->bufferData->flags |= BufferData::BufferDataChange::cachedBufferChanged;
1226 s->bufferData->cachedBuffer.token = BufferCache::getInstance().getToken();
1227 s->bufferData->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -07001228
1229 // If we have more buffers than the size of the cache, we should stop caching so we don't
1230 // evict other buffers in this transaction
1231 count++;
1232 if (count >= BUFFER_CACHE_MAX_SIZE) {
1233 break;
1234 }
1235 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001236}
1237
Vishnu Nair9402bb02022-09-28 16:45:06 -07001238class SyncCallback {
1239public:
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001240 static auto getCallback(std::shared_ptr<SyncCallback>& callbackContext) {
1241 return [callbackContext](void* /* unused context */, nsecs_t /* latchTime */,
1242 const sp<Fence>& /* presentFence */,
1243 const std::vector<SurfaceControlStats>& /* stats */) {
1244 if (!callbackContext) {
1245 ALOGE("failed to get callback context for SyncCallback");
1246 return;
1247 }
1248 LOG_ALWAYS_FATAL_IF(sem_post(&callbackContext->mSemaphore), "sem_post failed");
1249 };
Vishnu Nair9402bb02022-09-28 16:45:06 -07001250 }
1251 ~SyncCallback() {
1252 if (mInitialized) {
1253 LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
1254 }
1255 }
1256 void init() {
1257 LOG_ALWAYS_FATAL_IF(clock_gettime(CLOCK_MONOTONIC, &mTimeoutTimespec) == -1,
1258 "clock_gettime() fail! in SyncCallback::init");
1259 mTimeoutTimespec.tv_sec += 4;
1260 LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed");
1261 mInitialized = true;
1262 }
1263 void wait() {
1264 int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &mTimeoutTimespec);
1265 if (result && errno != ETIMEDOUT && errno != EINTR) {
1266 LOG_ALWAYS_FATAL("sem_clockwait failed(%d)", errno);
1267 } else if (errno == ETIMEDOUT) {
1268 ALOGW("Sync transaction timed out waiting for commit callback.");
1269 }
1270 }
1271 void* getContext() { return static_cast<void*>(this); }
1272
1273private:
1274 sem_t mSemaphore;
1275 bool mInitialized = false;
1276 timespec mTimeoutTimespec;
1277};
1278
Robert Carr79dc06a2022-02-22 15:28:59 -08001279status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001280 if (mStatus != NO_ERROR) {
1281 return mStatus;
1282 }
1283
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001284 std::shared_ptr<SyncCallback> syncCallback = std::make_shared<SyncCallback>();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001285 if (synchronous) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001286 syncCallback->init();
1287 addTransactionCommittedCallback(SyncCallback::getCallback(syncCallback),
1288 /*callbackContext=*/nullptr);
Vishnu Nair9402bb02022-09-28 16:45:06 -07001289 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001290
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001291 bool hasListenerCallbacks = !mListenerCallbacks.empty();
1292 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001293 // For every listener with registered callbacks
1294 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
1295 auto& [callbackIds, surfaceControls] = callbackInfo;
1296 if (callbackIds.empty()) {
1297 continue;
1298 }
1299
Valerie Hau9dab9732019-08-20 09:29:25 -07001300 if (surfaceControls.empty()) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001301 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
Valerie Hau9dab9732019-08-20 09:29:25 -07001302 } else {
1303 // If the listener has any SurfaceControls set on this Transaction update the surface
1304 // state
1305 for (const auto& surfaceControl : surfaceControls) {
1306 layer_state_t* s = getLayerState(surfaceControl);
1307 if (!s) {
1308 ALOGE("failed to get layer state");
1309 continue;
1310 }
1311 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
1312 s->what |= layer_state_t::eHasListenerCallbacksChanged;
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001313 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001314 }
Marissa Wallc837b5e2018-10-12 10:04:44 -07001315 }
1316 }
Valerie Hau9dab9732019-08-20 09:29:25 -07001317
Marissa Wall78b72202019-03-15 14:58:34 -07001318 cacheBuffers();
1319
Robert Carr79dc06a2022-02-22 15:28:59 -08001320 if (oneWay) {
Vishnu Nair2709efe2022-09-28 17:17:08 -07001321 if (synchronous) {
1322 ALOGE("Transaction attempted to set synchronous and one way at the same time"
1323 " this is an invalid request. Synchronous will win for safety");
1324 } else {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001325 mFlags |= ISurfaceComposer::eOneWay;
Vishnu Nair2709efe2022-09-28 17:17:08 -07001326 }
Robert Carr79dc06a2022-02-22 15:28:59 -08001327 }
Robert Carr4cdc58f2017-08-23 14:22:20 -07001328
Vishnu Nair5f319772025-01-14 13:31:09 -08001329 // If both ISurfaceComposer::eEarlyWakeupStart and ISurfaceComposer::eEarlyWakeupEnd are set
Ady Abrahambf1349c2020-06-12 14:26:18 -07001330 // it is equivalent for none
Vishnu Nair5f319772025-01-14 13:31:09 -08001331 uint32_t wakeupFlags = ISurfaceComposer::eEarlyWakeupStart | ISurfaceComposer::eEarlyWakeupEnd;
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001332 if ((mFlags & wakeupFlags) == wakeupFlags) {
1333 mFlags &= ~(wakeupFlags);
Ady Abrahambf1349c2020-06-12 14:26:18 -07001334 }
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001335 sp<IBinder> applyToken = mApplyToken ? mApplyToken : getDefaultApplyToken();
Vishnu Nair277142c2021-01-05 18:35:29 -08001336
Vishnu Nair9402bb02022-09-28 16:45:06 -07001337 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001338 status_t binderStatus =
1339 sf->setTransactionState(mFrameTimelineInfo, mComposerStates, mDisplayStates, mFlags,
1340 applyToken, mInputWindowCommands, mDesiredPresentTime,
1341 mIsAutoTimestamp, mUncacheBuffers, hasListenerCallbacks,
1342 listenerCallbacks, mId, mMergedTransactionIds);
1343 mId = generateId();
Adithya Srinivasand3efcb32020-10-20 18:08:22 -07001344
1345 // Clear the current states and flags
1346 clear();
1347
Patrick Williamsa419faa2024-10-29 16:50:27 -05001348 if (synchronous && binderStatus == OK) {
Vishnu Nair4e04c3a2022-12-09 20:48:00 +00001349 syncCallback->wait();
Vishnu Nair9402bb02022-09-28 16:45:06 -07001350 }
1351
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001352 if (mLogCallPoints) {
1353 ALOG(LOG_DEBUG, LOG_SURFACE_CONTROL_REGISTRY, "Transaction %" PRIu64 " applied", mId);
Melody Hsuc034fbf2024-10-25 22:46:40 +00001354 }
1355
Robert Carr4cdc58f2017-08-23 14:22:20 -07001356 mStatus = NO_ERROR;
Patrick Williamsa419faa2024-10-29 16:50:27 -05001357 return binderStatus;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001358}
1359
Anton Ivanov8ed86592025-02-20 11:52:50 -08001360sp<IBinder> SurfaceComposerClient::Transaction::sApplyToken = sp<BBinder>::make();
Vishnu Nair80da86b2022-08-11 16:32:58 -07001361
Patrick Williams84541d12023-09-29 11:00:23 -05001362std::mutex SurfaceComposerClient::Transaction::sApplyTokenMutex;
1363
Vishnu Nair80da86b2022-08-11 16:32:58 -07001364sp<IBinder> SurfaceComposerClient::Transaction::getDefaultApplyToken() {
Patrick Williams84541d12023-09-29 11:00:23 -05001365 std::scoped_lock lock{sApplyTokenMutex};
Vishnu Nair80da86b2022-08-11 16:32:58 -07001366 return sApplyToken;
1367}
1368
1369void SurfaceComposerClient::Transaction::setDefaultApplyToken(sp<IBinder> applyToken) {
Patrick Williams84541d12023-09-29 11:00:23 -05001370 std::scoped_lock lock{sApplyTokenMutex};
Patrick Williamsa419faa2024-10-29 16:50:27 -05001371 sApplyToken = std::move(applyToken);
Vishnu Nair80da86b2022-08-11 16:32:58 -07001372}
Pascal Muetschardb39918f2023-01-27 11:36:11 +01001373
1374status_t SurfaceComposerClient::Transaction::sendSurfaceFlushJankDataTransaction(
1375 const sp<SurfaceControl>& sc) {
1376 Transaction t;
1377 layer_state_t* s = t.getLayerState(sc);
1378 if (!s) {
1379 return BAD_INDEX;
1380 }
1381
1382 s->what |= layer_state_t::eFlushJankData;
1383 t.registerSurfaceControlForCallback(sc);
1384 return t.apply(/*sync=*/false, /* oneWay=*/true);
1385}
Melody Hsuc034fbf2024-10-25 22:46:40 +00001386
1387void SurfaceComposerClient::Transaction::enableDebugLogCallPoints() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001388 mLogCallPoints = true;
Melody Hsuc034fbf2024-10-25 22:46:40 +00001389}
1390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391// ---------------------------------------------------------------------------
1392
Alan Dingd53801c2024-05-08 16:45:29 -07001393sp<IBinder> SurfaceComposerClient::createVirtualDisplay(const std::string& displayName,
Dennis Kiilerich85202062025-02-22 00:26:53 -08001394 bool isSecure, bool optimizeForPower,
1395 const std::string& uniqueId,
Alan Dingd53801c2024-05-08 16:45:29 -07001396 float requestedRefreshRate) {
Dennis Kiilerich85202062025-02-22 00:26:53 -08001397 const gui::ISurfaceComposer::OptimizationPolicy optimizationPolicy = optimizeForPower
1398 ? gui::ISurfaceComposer::OptimizationPolicy::optimizeForPower
1399 : gui::ISurfaceComposer::OptimizationPolicy::optimizeForPerformance;
Huihong Luo07e72362022-02-14 14:26:04 -08001400 sp<IBinder> display = nullptr;
Alan Dingd53801c2024-05-08 16:45:29 -07001401 binder::Status status =
1402 ComposerServiceAIDL::getComposerService()->createVirtualDisplay(displayName, isSecure,
Dennis Kiilerich85202062025-02-22 00:26:53 -08001403 optimizationPolicy,
Alan Dingd53801c2024-05-08 16:45:29 -07001404 uniqueId,
1405 requestedRefreshRate,
1406 &display);
Huihong Luo07e72362022-02-14 14:26:04 -08001407 return status.isOk() ? display : nullptr;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001408}
1409
Alan Dingd53801c2024-05-08 16:45:29 -07001410status_t SurfaceComposerClient::destroyVirtualDisplay(const sp<IBinder>& displayToken) {
1411 return ComposerServiceAIDL::getComposerService()
1412 ->destroyVirtualDisplay(displayToken)
1413 .transactionError();
Jesse Hall6c913be2013-08-08 12:15:49 -07001414}
1415
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001416std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
Huihong Luo07e72362022-02-14 14:26:04 -08001417 std::vector<int64_t> displayIds;
1418 std::vector<PhysicalDisplayId> physicalDisplayIds;
1419 binder::Status status =
1420 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayIds(&displayIds);
1421 if (status.isOk()) {
1422 physicalDisplayIds.reserve(displayIds.size());
Gil Dekelbddaa242025-01-29 17:31:34 -05001423 for (auto id : displayIds) {
1424 physicalDisplayIds.push_back(PhysicalDisplayId::fromValue(static_cast<uint64_t>(id)));
Huihong Luo07e72362022-02-14 14:26:04 -08001425 }
1426 }
1427 return physicalDisplayIds;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001428}
1429
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001430sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
Huihong Luo07e72362022-02-14 14:26:04 -08001431 sp<IBinder> display = nullptr;
1432 binder::Status status =
1433 ComposerServiceAIDL::getComposerService()->getPhysicalDisplayToken(displayId.value,
1434 &display);
1435 return status.isOk() ? display : nullptr;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -08001436}
1437
Patrick Williams090ad062023-08-08 12:30:10 -05001438std::optional<gui::StalledTransactionInfo> SurfaceComposerClient::getStalledTransactionInfo(
1439 pid_t pid) {
1440 std::optional<gui::StalledTransactionInfo> result;
1441 ComposerServiceAIDL::getComposerService()->getStalledTransactionInfo(pid, &result);
1442 return result;
1443}
1444
Robert Carr4cdc58f2017-08-23 14:22:20 -07001445void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001446 mFlags |= ISurfaceComposer::eAnimation;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001447}
1448
Ady Abraham8cbd3072021-03-15 16:39:06 -07001449void SurfaceComposerClient::Transaction::setEarlyWakeupStart() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001450 mFlags |= ISurfaceComposer::eEarlyWakeupStart;
Dan Stoza84d619e2018-03-28 17:07:36 -07001451}
1452
Ady Abraham8cbd3072021-03-15 16:39:06 -07001453void SurfaceComposerClient::Transaction::setEarlyWakeupEnd() {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001454 mFlags |= ISurfaceComposer::eEarlyWakeupEnd;
Ady Abrahambf1349c2020-06-12 14:26:18 -07001455}
1456
Pablo Gamitodbc31672020-09-01 18:28:58 +00001457layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001458 auto handle = sc->getLayerStateHandle();
1459 if (auto it = std::find_if(mComposerStates.begin(), mComposerStates.end(),
1460 [&handle](const auto& composerState) {
1461 return composerState.state.surface == handle;
1462 });
1463 it != mComposerStates.end()) {
1464 return &it->state;
1465 }
1466
1467 // we don't have it, add an initialized layer_state to our list
1468 ComposerState s;
1469 s.state.surface = handle;
1470 s.state.layerId = sc->getLayerId();
1471 mComposerStates.add(s);
1472
1473 return &mComposerStates.editItemAt(mComposerStates.size() - 1).state;
Mathias Agopian698c0872011-06-28 19:09:31 -07001474}
1475
Marissa Wallc837b5e2018-10-12 10:04:44 -07001476void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
1477 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -08001478 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
1479 callbackInfo.surfaceControls.insert(sc);
1480
Pascal Mütschardd56514e2024-05-24 17:37:13 +02001481 mTransactionCompletedListener->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001482}
1483
Robert Carr4cdc58f2017-08-23 14:22:20 -07001484SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
1485 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -08001486 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001487 if (!s) {
1488 mStatus = BAD_INDEX;
1489 return *this;
1490 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001491 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001492 s->x = x;
1493 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001494
1495 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001496 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001497}
1498
Robert Carr4cdc58f2017-08-23 14:22:20 -07001499SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
1500 const sp<SurfaceControl>& sc) {
1501 return setFlags(sc, 0, layer_state_t::eLayerHidden);
1502}
1503
1504SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
1505 const sp<SurfaceControl>& sc) {
1506 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
1507}
1508
Robert Carr4cdc58f2017-08-23 14:22:20 -07001509SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
1510 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001511 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001512 if (!s) {
1513 mStatus = BAD_INDEX;
1514 return *this;
1515 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001516 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -07001517 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001518 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001519
1520 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001521 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001522}
1523
Pablo Gamito11dcc222020-09-12 15:49:39 +00001524SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
1525 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -08001526 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -07001527 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001528 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -07001529 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001530 }
Anton Ivanovc7f87932025-02-25 00:58:09 -08001531 s->updateRelativeLayer(relativeTo, z);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001532 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001533 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -07001534}
1535
Robert Carr4cdc58f2017-08-23 14:22:20 -07001536SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
1537 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -07001538 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -08001539 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001540 if (!s) {
1541 mStatus = BAD_INDEX;
1542 return *this;
1543 }
Melody Hsu117556e2024-10-03 21:58:56 +00001544 s->what |= layer_state_t::eFlagsChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001545 s->flags &= ~mask;
1546 s->flags |= (flags & mask);
1547 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001548
1549 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001550 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001551}
1552
Robert Carr4cdc58f2017-08-23 14:22:20 -07001553SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
1554 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -07001555 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -08001556 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001557 if (!s) {
1558 mStatus = BAD_INDEX;
1559 return *this;
1560 }
Anton Ivanovc7f87932025-02-25 00:58:09 -08001561 s->updateTransparentRegion(transparentRegion);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001562 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001563 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001564}
1565
Sally Qi421ffb02022-03-21 19:41:33 -07001566SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDimmingEnabled(
1567 const sp<SurfaceControl>& sc, bool dimmingEnabled) {
1568 layer_state_t* s = getLayerState(sc);
1569 if (!s) {
1570 mStatus = BAD_INDEX;
1571 return *this;
1572 }
1573 s->what |= layer_state_t::eDimmingEnabledChanged;
1574 s->dimmingEnabled = dimmingEnabled;
1575
1576 registerSurfaceControlForCallback(sc);
1577 return *this;
1578}
1579
Robert Carr4cdc58f2017-08-23 14:22:20 -07001580SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
1581 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -08001582 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001583 if (!s) {
1584 mStatus = BAD_INDEX;
1585 return *this;
1586 }
Ady Abrahamff930b42022-08-05 19:04:16 +00001587 if (alpha < 0.0f || alpha > 1.0f) {
Ady Abraham24d0b2a2022-08-11 16:27:34 -07001588 ALOGE("SurfaceComposerClient::Transaction::setAlpha: invalid alpha %f, clamping", alpha);
Ady Abrahamff930b42022-08-05 19:04:16 +00001589 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001590 s->what |= layer_state_t::eAlphaChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001591 s->color.a = std::clamp(alpha, 0.f, 1.f);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001592
1593 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001594 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001595}
1596
Robert Carr4cdc58f2017-08-23 14:22:20 -07001597SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001598 const sp<SurfaceControl>& sc, ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001599 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001600 if (!s) {
1601 mStatus = BAD_INDEX;
1602 return *this;
1603 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001604 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -07001605 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001606
1607 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001608 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -07001609}
1610
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001611SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -07001612 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001613 layer_state_t* s = getLayerState(sc);
1614 if (!s) {
1615 mStatus = BAD_INDEX;
1616 return *this;
1617 }
1618 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -07001619
1620 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001621
1622 registerSurfaceControlForCallback(sc);
1623 return *this;
1624}
1625
Robert Carr4cdc58f2017-08-23 14:22:20 -07001626SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
1627 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -08001628 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -08001629 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001630 if (!s) {
1631 mStatus = BAD_INDEX;
1632 return *this;
1633 }
Mathias Agopian3165cc22012-08-08 19:42:09 -07001634 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -07001635 layer_state_t::matrix22_t matrix;
1636 matrix.dsdx = dsdx;
1637 matrix.dtdx = dtdx;
1638 matrix.dsdy = dsdy;
1639 matrix.dtdy = dtdy;
1640 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001641
1642 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001643 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -07001644}
1645
chaviw25714502021-02-11 10:01:08 -08001646SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
Robert Carr4cdc58f2017-08-23 14:22:20 -07001647 const sp<SurfaceControl>& sc, const Rect& crop) {
Vishnu Naira9123c82024-10-03 03:56:44 +00001648 return setCrop(sc, crop.toFloatRect());
1649}
1650
1651SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
1652 const sp<SurfaceControl>& sc, const FloatRect& crop) {
chaviw763ef572018-02-22 16:04:57 -08001653 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001654 if (!s) {
1655 mStatus = BAD_INDEX;
1656 return *this;
1657 }
chaviw25714502021-02-11 10:01:08 -08001658 s->what |= layer_state_t::eCropChanged;
1659 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001660
1661 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001662 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001663}
1664
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001665SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
1666 const sp<SurfaceControl>& sc, float cornerRadius) {
1667 layer_state_t* s = getLayerState(sc);
1668 if (!s) {
1669 mStatus = BAD_INDEX;
1670 return *this;
1671 }
1672 s->what |= layer_state_t::eCornerRadiusChanged;
1673 s->cornerRadius = cornerRadius;
1674 return *this;
1675}
1676
Surbhi Kadam9674ffa2024-11-14 23:06:27 +00001677SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setClientDrawnCornerRadius(
1678 const sp<SurfaceControl>& sc, float clientDrawnCornerRadius) {
1679 layer_state_t* s = getLayerState(sc);
1680 if (!s) {
1681 mStatus = BAD_INDEX;
1682 return *this;
1683 }
1684 s->what |= layer_state_t::eClientDrawnCornerRadiusChanged;
1685 s->clientDrawnCornerRadius = clientDrawnCornerRadius;
1686 return *this;
1687}
1688
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001689SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1690 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1691 layer_state_t* s = getLayerState(sc);
1692 if (!s) {
1693 mStatus = BAD_INDEX;
1694 return *this;
1695 }
1696 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1697 s->backgroundBlurRadius = backgroundBlurRadius;
1698 return *this;
1699}
1700
Lucas Dupinc3800b82020-10-02 16:24:48 -07001701SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBlurRegions(
1702 const sp<SurfaceControl>& sc, const std::vector<BlurRegion>& blurRegions) {
1703 layer_state_t* s = getLayerState(sc);
1704 if (!s) {
1705 mStatus = BAD_INDEX;
1706 return *this;
1707 }
1708 s->what |= layer_state_t::eBlurRegionsChanged;
1709 s->blurRegions = blurRegions;
1710 return *this;
1711}
1712
Robert Carr4cdc58f2017-08-23 14:22:20 -07001713SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001714 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001715 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001716 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001717 mStatus = BAD_INDEX;
1718 return *this;
chaviw06178942017-07-27 10:25:59 -07001719 }
Robert Carr5b3b9142021-02-22 12:27:32 -08001720 if (SurfaceControl::isSameSurface(sc, newParent)) {
1721 return *this;
1722 }
Anton Ivanovc7f87932025-02-25 00:58:09 -08001723 s->updateParentLayer(newParent);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001724 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001725 return *this;
chaviw06178942017-07-27 10:25:59 -07001726}
1727
Robert Carr4cdc58f2017-08-23 14:22:20 -07001728SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1729 const sp<SurfaceControl>& sc,
1730 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001731 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001732 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001733 mStatus = BAD_INDEX;
1734 return *this;
1735 }
1736 s->what |= layer_state_t::eColorChanged;
Vishnu Nairbbceb462022-10-10 04:52:13 +00001737 s->color.rgb = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001738
1739 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001740 return *this;
1741}
1742
Valerie Haudd0b7572019-01-29 14:59:27 -08001743SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1744 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001745 layer_state_t* s = getLayerState(sc);
1746 if (!s) {
1747 mStatus = BAD_INDEX;
1748 return *this;
1749 }
1750
Valerie Haudd0b7572019-01-29 14:59:27 -08001751 s->what |= layer_state_t::eBackgroundColorChanged;
Vishnu Naird47bcee2023-02-24 18:08:51 +00001752 s->bgColor.rgb = color;
1753 s->bgColor.a = alpha;
Valerie Haudd0b7572019-01-29 14:59:27 -08001754 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001755
1756 registerSurfaceControlForCallback(sc);
1757 return *this;
1758}
1759
Marissa Wall61c58622018-07-18 10:12:20 -07001760SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1761 const sp<SurfaceControl>& sc, uint32_t transform) {
1762 layer_state_t* s = getLayerState(sc);
1763 if (!s) {
1764 mStatus = BAD_INDEX;
1765 return *this;
1766 }
Vishnu Nairbbceb462022-10-10 04:52:13 +00001767 s->what |= layer_state_t::eBufferTransformChanged;
1768 s->bufferTransform = transform;
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&
1775SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1776 bool transformToDisplayInverse) {
1777 layer_state_t* s = getLayerState(sc);
1778 if (!s) {
1779 mStatus = BAD_INDEX;
1780 return *this;
1781 }
1782 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1783 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001784
1785 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001786 return *this;
1787}
1788
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001789std::shared_ptr<BufferData> SurfaceComposerClient::Transaction::getAndClearBuffer(
chaviw0acd33a2021-11-02 11:55:37 -05001790 const sp<SurfaceControl>& sc) {
1791 layer_state_t* s = getLayerState(sc);
1792 if (!s) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001793 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001794 }
1795 if (!(s->what & layer_state_t::eBufferChanged)) {
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001796 return nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001797 }
1798
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001799 std::shared_ptr<BufferData> bufferData = std::move(s->bufferData);
chaviw0acd33a2021-11-02 11:55:37 -05001800
Vishnu Naira74ba0f2024-04-16 11:25:06 -07001801 mTransactionCompletedListener->removeReleaseBufferCallback(
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001802 bufferData->generateReleaseCallbackId());
chaviw0acd33a2021-11-02 11:55:37 -05001803 s->what &= ~layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001804 s->bufferData = nullptr;
chaviw0acd33a2021-11-02 11:55:37 -05001805
chaviw0acd33a2021-11-02 11:55:37 -05001806 return bufferData;
1807}
1808
Robert Carr79dc06a2022-02-22 15:28:59 -08001809SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferHasBarrier(
1810 const sp<SurfaceControl>& sc, uint64_t barrierFrameNumber) {
1811 layer_state_t* s = getLayerState(sc);
1812 if (!s) {
1813 mStatus = BAD_INDEX;
1814 return *this;
1815 }
1816 s->bufferData->hasBarrier = true;
1817 s->bufferData->barrierFrameNumber = barrierFrameNumber;
1818 return *this;
1819}
1820
Marissa Wall61c58622018-07-18 10:12:20 -07001821SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
chaviwba4320c2021-09-15 15:20:53 -05001822 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer,
John Reck57f748c2022-03-17 17:23:49 -04001823 const std::optional<sp<Fence>>& fence, const std::optional<uint64_t>& optFrameNumber,
Nergi Rahardi39f510f2024-05-23 15:16:54 +09001824 uint32_t producerId, ReleaseBufferCallback callback, nsecs_t dequeueTime) {
Marissa Wall61c58622018-07-18 10:12:20 -07001825 layer_state_t* s = getLayerState(sc);
1826 if (!s) {
1827 mStatus = BAD_INDEX;
1828 return *this;
1829 }
chaviw69058fb2021-09-27 09:37:30 -05001830
1831 releaseBufferIfOverwriting(*s);
1832
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001833 std::shared_ptr<BufferData> bufferData = std::make_shared<BufferData>();
1834 bufferData->buffer = buffer;
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001835 if (buffer) {
1836 uint64_t frameNumber = sc->resolveFrameNumber(optFrameNumber);
1837 bufferData->frameNumber = frameNumber;
1838 bufferData->producerId = producerId;
1839 bufferData->flags |= BufferData::BufferDataChange::frameNumberChanged;
Nergi Rahardi39f510f2024-05-23 15:16:54 +09001840 bufferData->dequeueTime = dequeueTime;
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001841 if (fence) {
1842 bufferData->acquireFence = *fence;
1843 bufferData->flags |= BufferData::BufferDataChange::fenceChanged;
1844 }
Vishnu Naira74ba0f2024-04-16 11:25:06 -07001845 bufferData->releaseBufferEndpoint = IInterface::asBinder(mTransactionCompletedListener);
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001846 setReleaseBufferCallback(bufferData.get(), callback);
chaviwba4320c2021-09-15 15:20:53 -05001847 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001848
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001849 if (mIsAutoTimestamp) {
1850 mDesiredPresentTime = systemTime();
Ady Abrahamf0c56492020-12-17 18:04:15 -08001851 }
chaviwba4320c2021-09-15 15:20:53 -05001852 s->what |= layer_state_t::eBufferChanged;
Vishnu Nair9f0835e2022-01-07 09:33:19 -08001853 s->bufferData = std::move(bufferData);
Marissa Wallebc2c052019-01-16 19:16:55 -08001854 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001855
chaviw1a227f02022-04-25 16:23:44 -05001856 // With the current infrastructure, a release callback will not be invoked if there's no
1857 // transaction callback in the case when a buffer is latched and not released early. This is
1858 // because the legacy implementation didn't have a release callback and sent releases in the
1859 // transaction callback. Because of this, we need to make sure to have a transaction callback
1860 // set up when a buffer is sent in a transaction to ensure the caller gets the release
1861 // callback, regardless if they set up a transaction callback.
1862 //
1863 // TODO (b/230380821): Remove when release callbacks are separated from transaction callbacks
1864 addTransactionCompletedCallback([](void*, nsecs_t, const sp<Fence>&,
1865 const std::vector<SurfaceControlStats>&) {},
1866 nullptr);
1867
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07001868 mMayContainBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001869 return *this;
1870}
1871
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001872SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::unsetBuffer(
1873 const sp<SurfaceControl>& sc) {
1874 layer_state_t* s = getLayerState(sc);
1875 if (!s) {
1876 mStatus = BAD_INDEX;
1877 return *this;
1878 }
1879
1880 if (!(s->what & layer_state_t::eBufferChanged)) {
1881 return *this;
1882 }
1883
1884 releaseBufferIfOverwriting(*s);
1885
1886 s->what &= ~layer_state_t::eBufferChanged;
1887 s->bufferData = nullptr;
1888 return *this;
1889}
1890
chaviwba4320c2021-09-15 15:20:53 -05001891void SurfaceComposerClient::Transaction::setReleaseBufferCallback(BufferData* bufferData,
Vishnu Nair1506b182021-02-22 14:35:15 -08001892 ReleaseBufferCallback callback) {
1893 if (!callback) {
1894 return;
1895 }
1896
chaviwba4320c2021-09-15 15:20:53 -05001897 if (!bufferData->buffer) {
Vishnu Nair1506b182021-02-22 14:35:15 -08001898 ALOGW("Transaction::setReleaseBufferCallback"
1899 "ignored trying to set a callback on a null buffer.");
1900 return;
1901 }
1902
Vishnu Naira74ba0f2024-04-16 11:25:06 -07001903 bufferData->releaseBufferListener =
1904 static_cast<sp<ITransactionCompletedListener>>(mTransactionCompletedListener);
1905 mTransactionCompletedListener->setReleaseBufferCallback(bufferData->generateReleaseCallbackId(),
1906 callback);
Vishnu Nair1506b182021-02-22 14:35:15 -08001907}
1908
Marissa Wall61c58622018-07-18 10:12:20 -07001909SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1910 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1911 layer_state_t* s = getLayerState(sc);
1912 if (!s) {
1913 mStatus = BAD_INDEX;
1914 return *this;
1915 }
1916 s->what |= layer_state_t::eDataspaceChanged;
1917 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001918
1919 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001920 return *this;
1921}
1922
John Reck68796592023-01-25 13:47:12 -05001923SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setExtendedRangeBrightness(
1924 const sp<SurfaceControl>& sc, float currentBufferRatio, float desiredRatio) {
1925 layer_state_t* s = getLayerState(sc);
1926 if (!s) {
1927 mStatus = BAD_INDEX;
1928 return *this;
1929 }
1930 s->what |= layer_state_t::eExtendedRangeBrightnessChanged;
Sally Qi963049b2023-03-23 14:06:21 -07001931 s->currentHdrSdrRatio = currentBufferRatio;
1932 s->desiredHdrSdrRatio = desiredRatio;
John Reck68796592023-01-25 13:47:12 -05001933
1934 registerSurfaceControlForCallback(sc);
1935 return *this;
1936}
1937
Alec Mouri1b0d4e12024-02-12 22:27:19 +00001938SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredHdrHeadroom(
1939 const sp<SurfaceControl>& sc, float desiredRatio) {
1940 layer_state_t* s = getLayerState(sc);
1941 if (!s) {
1942 mStatus = BAD_INDEX;
1943 return *this;
1944 }
1945 s->what |= layer_state_t::eDesiredHdrHeadroomChanged;
1946 s->desiredHdrSdrRatio = desiredRatio;
1947
1948 registerSurfaceControlForCallback(sc);
1949 return *this;
1950}
1951
Sally Qi95f669a2024-08-27 11:31:42 -07001952SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLuts(
Sally Qi9e848112025-02-11 17:20:25 -08001953 const sp<SurfaceControl>& sc, base::unique_fd&& lutFd, const std::vector<int32_t>& offsets,
1954 const std::vector<int32_t>& dimensions, const std::vector<int32_t>& sizes,
1955 const std::vector<int32_t>& samplingKeys) {
Sally Qi95f669a2024-08-27 11:31:42 -07001956 layer_state_t* s = getLayerState(sc);
1957 if (!s) {
1958 mStatus = BAD_INDEX;
1959 return *this;
1960 }
Sally Qi0abc4a52024-09-26 16:13:06 -07001961
Sally Qi0abc4a52024-09-26 16:13:06 -07001962 s->what |= layer_state_t::eLutsChanged;
Sally Qief006582024-10-11 13:23:09 -07001963 if (lutFd.ok()) {
Sally Qi9e848112025-02-11 17:20:25 -08001964 s->luts = std::make_shared<gui::DisplayLuts>(std::move(lutFd), offsets, dimensions, sizes,
1965 samplingKeys);
Sally Qief006582024-10-11 13:23:09 -07001966 } else {
1967 s->luts = nullptr;
1968 }
Sally Qi0abc4a52024-09-26 16:13:06 -07001969
Sally Qi95f669a2024-08-27 11:31:42 -07001970 registerSurfaceControlForCallback(sc);
1971 return *this;
1972}
1973
Alec Mourif4af03e2023-02-11 00:25:24 +00001974SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCachingHint(
1975 const sp<SurfaceControl>& sc, gui::CachingHint cachingHint) {
1976 layer_state_t* s = getLayerState(sc);
1977 if (!s) {
1978 mStatus = BAD_INDEX;
1979 return *this;
1980 }
1981 s->what |= layer_state_t::eCachingHintChanged;
1982 s->cachingHint = cachingHint;
1983
1984 registerSurfaceControlForCallback(sc);
1985 return *this;
1986}
1987
Marissa Wall61c58622018-07-18 10:12:20 -07001988SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1989 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1990 layer_state_t* s = getLayerState(sc);
1991 if (!s) {
1992 mStatus = BAD_INDEX;
1993 return *this;
1994 }
1995 s->what |= layer_state_t::eHdrMetadataChanged;
1996 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001997
1998 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001999 return *this;
2000}
2001
2002SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
2003 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
2004 layer_state_t* s = getLayerState(sc);
2005 if (!s) {
2006 mStatus = BAD_INDEX;
2007 return *this;
2008 }
Anton Ivanovc7f87932025-02-25 00:58:09 -08002009 s->updateSurfaceDamageRegion(surfaceDamageRegion);
Marissa Wallc837b5e2018-10-12 10:04:44 -07002010 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07002011 return *this;
2012}
2013
2014SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
2015 const sp<SurfaceControl>& sc, int32_t api) {
2016 layer_state_t* s = getLayerState(sc);
2017 if (!s) {
2018 mStatus = BAD_INDEX;
2019 return *this;
2020 }
2021 s->what |= layer_state_t::eApiChanged;
2022 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07002023
2024 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07002025 return *this;
2026}
2027
2028SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
2029 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
2030 layer_state_t* s = getLayerState(sc);
2031 if (!s) {
2032 mStatus = BAD_INDEX;
2033 return *this;
2034 }
2035 s->what |= layer_state_t::eSidebandStreamChanged;
2036 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07002037
2038 registerSurfaceControlForCallback(sc);
2039 return *this;
2040}
2041
Marissa Wall17b4e452018-12-26 16:32:34 -08002042SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
2043 nsecs_t desiredPresentTime) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002044 mDesiredPresentTime = desiredPresentTime;
2045 mIsAutoTimestamp = false;
Marissa Wall17b4e452018-12-26 16:32:34 -08002046 return *this;
2047}
2048
Peiyong Linc502cb72019-03-01 15:00:23 -08002049SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
2050 const sp<SurfaceControl>& sc, const bool agnostic) {
2051 layer_state_t* s = getLayerState(sc);
2052 if (!s) {
2053 mStatus = BAD_INDEX;
2054 return *this;
2055 }
2056 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
2057 s->colorSpaceAgnostic = agnostic;
2058
2059 registerSurfaceControlForCallback(sc);
2060 return *this;
2061}
2062
Marissa Wallc837b5e2018-10-12 10:04:44 -07002063SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01002064SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
2065 int32_t priority) {
2066 layer_state_t* s = getLayerState(sc);
2067 if (!s) {
2068 mStatus = BAD_INDEX;
2069 return *this;
2070 }
2071
2072 s->what |= layer_state_t::eFrameRateSelectionPriority;
2073 s->frameRateSelectionPriority = priority;
2074
2075 registerSurfaceControlForCallback(sc);
2076 return *this;
2077}
2078
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07002079SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::addTransactionCallback(
2080 TransactionCompletedCallbackTakesContext callback, void* callbackContext,
2081 CallbackId::Type callbackType) {
Patrick Williams5312ec12024-08-23 16:11:10 -05002082 auto callbackWithContext =
2083 std::bind(std::move(callback), callbackContext, std::placeholders::_1,
2084 std::placeholders::_2, std::placeholders::_3);
Vishnu Naira74ba0f2024-04-16 11:25:06 -07002085 const auto& surfaceControls = mListenerCallbacks[mTransactionCompletedListener].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07002086
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07002087 CallbackId callbackId =
Vishnu Naira74ba0f2024-04-16 11:25:06 -07002088 mTransactionCompletedListener->addCallbackFunction(callbackWithContext, surfaceControls,
2089 callbackType);
Marissa Wallc837b5e2018-10-12 10:04:44 -07002090
Vishnu Naira74ba0f2024-04-16 11:25:06 -07002091 mListenerCallbacks[mTransactionCompletedListener].callbackIds.emplace(callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07002092 return *this;
2093}
2094
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07002095SurfaceComposerClient::Transaction&
2096SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
2097 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Patrick Williams5312ec12024-08-23 16:11:10 -05002098 return addTransactionCallback(std::move(callback), callbackContext,
2099 CallbackId::Type::ON_COMPLETE);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07002100}
2101
2102SurfaceComposerClient::Transaction&
2103SurfaceComposerClient::Transaction::addTransactionCommittedCallback(
2104 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Patrick Williams5312ec12024-08-23 16:11:10 -05002105 return addTransactionCallback(std::move(callback), callbackContext,
2106 CallbackId::Type::ON_COMMIT);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -07002107}
2108
Valerie Hau871d6352020-01-29 08:44:02 -08002109SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
2110 const sp<SurfaceControl>& sc) {
2111 layer_state_t* s = getLayerState(sc);
2112 if (!s) {
2113 mStatus = BAD_INDEX;
2114 return *this;
2115 }
2116
2117 s->what |= layer_state_t::eProducerDisconnect;
2118 return *this;
2119}
2120
Robert Carr2c358bf2018-08-08 15:58:15 -07002121SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
Patrick Williams87602162024-11-05 10:13:19 -06002122 const sp<SurfaceControl>& sc, sp<WindowInfoHandle> info) {
Robert Carr2c358bf2018-08-08 15:58:15 -07002123 layer_state_t* s = getLayerState(sc);
2124 if (!s) {
2125 mStatus = BAD_INDEX;
2126 return *this;
2127 }
Anton Ivanovc7f87932025-02-25 00:58:09 -08002128 s->updateInputWindowInfo(std::move(info));
Robert Carr2c358bf2018-08-08 15:58:15 -07002129 return *this;
2130}
chaviw273171b2018-12-26 11:46:30 -08002131
Vishnu Naire798b472020-07-23 13:52:21 -07002132SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Naire798b472020-07-23 13:52:21 -07002133 const FocusRequest& request) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002134 mInputWindowCommands.addFocusRequest(request);
Vishnu Naire798b472020-07-23 13:52:21 -07002135 return *this;
2136}
2137
Patrick Williamse5991f52022-06-23 20:23:43 +00002138SurfaceComposerClient::Transaction&
2139SurfaceComposerClient::Transaction::addWindowInfosReportedListener(
2140 sp<gui::IWindowInfosReportedListener> windowInfosReportedListener) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002141 mInputWindowCommands.addWindowInfosReportedListener(windowInfosReportedListener);
chaviwa911b102019-02-14 10:18:33 -08002142 return *this;
2143}
2144
Peiyong Lind3788632018-09-18 16:01:31 -07002145SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
2146 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
2147 layer_state_t* s = getLayerState(sc);
2148 if (!s) {
2149 mStatus = BAD_INDEX;
2150 return *this;
2151 }
2152 s->what |= layer_state_t::eColorTransformChanged;
2153 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07002154
2155 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07002156 return *this;
2157}
2158
Robert Carrfb4d58b2019-01-15 09:21:27 -08002159SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
2160 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
chaviw25714502021-02-11 10:01:08 -08002161 setCrop(sc, source);
Robert Carrfb4d58b2019-01-15 09:21:27 -08002162
2163 int x = dst.left;
2164 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07002165
2166 float sourceWidth = source.getWidth();
2167 float sourceHeight = source.getHeight();
2168
2169 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
2170 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08002171 float matrix[4] = {1, 0, 0, 1};
2172
2173 switch (transform) {
2174 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
2175 matrix[0] = -xScale; matrix[1] = 0;
2176 matrix[2] = 0; matrix[3] = yScale;
2177 x += source.getWidth();
2178 break;
2179 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
2180 matrix[0] = xScale; matrix[1] = 0;
2181 matrix[2] = 0; matrix[3] = -yScale;
2182 y += source.getHeight();
2183 break;
2184 case NATIVE_WINDOW_TRANSFORM_ROT_90:
2185 matrix[0] = 0; matrix[1] = -yScale;
2186 matrix[2] = xScale; matrix[3] = 0;
2187 x += source.getHeight();
2188 break;
2189 case NATIVE_WINDOW_TRANSFORM_ROT_180:
2190 matrix[0] = -xScale; matrix[1] = 0;
2191 matrix[2] = 0; matrix[3] = -yScale;
2192 x += source.getWidth();
2193 y += source.getHeight();
2194 break;
2195 case NATIVE_WINDOW_TRANSFORM_ROT_270:
2196 matrix[0] = 0; matrix[1] = yScale;
2197 matrix[2] = -xScale; matrix[3] = 0;
2198 y += source.getWidth();
2199 break;
2200 default:
2201 matrix[0] = xScale; matrix[1] = 0;
2202 matrix[2] = 0; matrix[3] = yScale;
2203 break;
2204 }
2205 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07002206 float offsetX = xScale * source.left;
2207 float offsetY = yScale * source.top;
2208 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08002209
2210 return *this;
2211}
2212
Vishnu Nairc97b8db2019-10-29 18:19:35 -07002213SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
2214 const sp<SurfaceControl>& sc, float shadowRadius) {
2215 layer_state_t* s = getLayerState(sc);
2216 if (!s) {
2217 mStatus = BAD_INDEX;
2218 return *this;
2219 }
2220 s->what |= layer_state_t::eShadowRadiusChanged;
2221 s->shadowRadius = shadowRadius;
2222 return *this;
2223}
2224
Steven Thomas3172e202020-01-06 19:25:30 -08002225SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Marin Shalamanov46084422020-10-13 12:33:42 +02002226 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility,
Marin Shalamanovc5986772021-03-16 16:09:49 +01002227 int8_t changeFrameRateStrategy) {
Steven Thomas3172e202020-01-06 19:25:30 -08002228 layer_state_t* s = getLayerState(sc);
2229 if (!s) {
2230 mStatus = BAD_INDEX;
2231 return *this;
2232 }
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002233 // Allow privileged values as well here, those will be ignored by SF if
2234 // the caller is not privileged
Marin Shalamanovc5986772021-03-16 16:09:49 +01002235 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
2236 "Transaction::setFrameRate",
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002237 /*privileged=*/true)) {
Steven Thomas62a4cf82020-01-31 12:04:03 -08002238 mStatus = BAD_VALUE;
2239 return *this;
2240 }
Steven Thomas3172e202020-01-06 19:25:30 -08002241 s->what |= layer_state_t::eFrameRateChanged;
2242 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002243 s->frameRateCompatibility = compatibility;
Marin Shalamanovc5986772021-03-16 16:09:49 +01002244 s->changeFrameRateStrategy = changeFrameRateStrategy;
Steven Thomas3172e202020-01-06 19:25:30 -08002245 return *this;
2246}
2247
Andy Labrada096227e2022-06-15 16:58:11 +00002248SurfaceComposerClient::Transaction&
2249SurfaceComposerClient::Transaction::setDefaultFrameRateCompatibility(const sp<SurfaceControl>& sc,
2250 int8_t compatibility) {
2251 layer_state_t* s = getLayerState(sc);
2252 if (!s) {
2253 mStatus = BAD_INDEX;
2254 return *this;
2255 }
2256 s->what |= layer_state_t::eDefaultFrameRateCompatibilityChanged;
2257 s->defaultFrameRateCompatibility = compatibility;
2258 return *this;
2259}
2260
Rachel Leece6e0042023-06-27 11:22:54 -07002261SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRateCategory(
Rachel Lee67afbea2023-09-28 15:35:07 -07002262 const sp<SurfaceControl>& sc, int8_t category, bool smoothSwitchOnly) {
Rachel Leece6e0042023-06-27 11:22:54 -07002263 layer_state_t* s = getLayerState(sc);
2264 if (!s) {
2265 mStatus = BAD_INDEX;
2266 return *this;
2267 }
2268 s->what |= layer_state_t::eFrameRateCategoryChanged;
2269 s->frameRateCategory = category;
Rachel Lee67afbea2023-09-28 15:35:07 -07002270 s->frameRateCategorySmoothSwitchOnly = smoothSwitchOnly;
Rachel Leece6e0042023-06-27 11:22:54 -07002271 return *this;
2272}
2273
Rachel Lee58cc90d2023-09-05 18:50:20 -07002274SurfaceComposerClient::Transaction&
2275SurfaceComposerClient::Transaction::setFrameRateSelectionStrategy(const sp<SurfaceControl>& sc,
2276 int8_t strategy) {
2277 layer_state_t* s = getLayerState(sc);
2278 if (!s) {
2279 mStatus = BAD_INDEX;
2280 return *this;
2281 }
2282 s->what |= layer_state_t::eFrameRateSelectionStrategyChanged;
2283 s->frameRateSelectionStrategy = strategy;
2284 return *this;
2285}
2286
Vishnu Nair6213bd92020-05-08 17:42:25 -07002287SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
2288 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
2289 layer_state_t* s = getLayerState(sc);
2290 if (!s) {
2291 mStatus = BAD_INDEX;
2292 return *this;
2293 }
2294
2295 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
2296 ? ui::Transform::ROT_INVALID
2297 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
2298 s->what |= layer_state_t::eFixedTransformHintChanged;
2299 s->fixedTransformHint = transform;
2300 return *this;
2301}
2302
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10002303SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
2304 const FrameTimelineInfo& frameTimelineInfo) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002305 mergeFrameTimelineInfo(mFrameTimelineInfo, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -07002306 return *this;
2307}
2308
Vishnu Naircf26a0a2020-11-13 12:56:20 -08002309SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAutoRefresh(
2310 const sp<SurfaceControl>& sc, bool autoRefresh) {
2311 layer_state_t* s = getLayerState(sc);
2312 if (!s) {
2313 mStatus = BAD_INDEX;
2314 return *this;
2315 }
2316
2317 s->what |= layer_state_t::eAutoRefreshChanged;
2318 s->autoRefresh = autoRefresh;
2319 return *this;
2320}
2321
Winson Chunga30f7c92021-06-29 15:42:56 -07002322SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedOverlay(
2323 const sp<SurfaceControl>& sc, bool isTrustedOverlay) {
Vishnu Nair9e0017e2024-05-22 19:02:44 +00002324 return setTrustedOverlay(sc,
2325 isTrustedOverlay ? gui::TrustedOverlay::ENABLED
2326 : gui::TrustedOverlay::UNSET);
2327}
2328
2329SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTrustedOverlay(
2330 const sp<SurfaceControl>& sc, gui::TrustedOverlay trustedOverlay) {
Winson Chunga30f7c92021-06-29 15:42:56 -07002331 layer_state_t* s = getLayerState(sc);
2332 if (!s) {
2333 mStatus = BAD_INDEX;
2334 return *this;
2335 }
2336
2337 s->what |= layer_state_t::eTrustedOverlayChanged;
Vishnu Nair9e0017e2024-05-22 19:02:44 +00002338 s->trustedOverlay = trustedOverlay;
Winson Chunga30f7c92021-06-29 15:42:56 -07002339 return *this;
2340}
2341
Vishnu Nair277142c2021-01-05 18:35:29 -08002342SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken(
2343 const sp<IBinder>& applyToken) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002344 mApplyToken = applyToken;
Vishnu Nair277142c2021-01-05 18:35:29 -08002345 return *this;
2346}
2347
John Reckcdb4ed72021-02-04 13:39:33 -05002348SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setStretchEffect(
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002349 const sp<SurfaceControl>& sc, const StretchEffect& stretchEffect) {
John Reckcdb4ed72021-02-04 13:39:33 -05002350 layer_state_t* s = getLayerState(sc);
2351 if (!s) {
2352 mStatus = BAD_INDEX;
2353 return *this;
2354 }
2355
2356 s->what |= layer_state_t::eStretchChanged;
Nader Jawad2dfc98b2021-04-08 20:35:39 -07002357 s->stretchEffect = stretchEffect;
John Reckcdb4ed72021-02-04 13:39:33 -05002358 return *this;
2359}
2360
Marzia Favarodcc9d9b2024-01-10 10:17:00 +00002361SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setEdgeExtensionEffect(
2362 const sp<SurfaceControl>& sc, const gui::EdgeExtensionParameters& effect) {
2363 layer_state_t* s = getLayerState(sc);
2364 if (!s) {
2365 mStatus = BAD_INDEX;
2366 return *this;
2367 }
2368
2369 s->what |= layer_state_t::eEdgeExtensionChanged;
2370 s->edgeExtensionParameters = effect;
2371 return *this;
2372}
2373
chaviwf3f40fe2021-04-27 15:54:02 -05002374SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferCrop(
2375 const sp<SurfaceControl>& sc, const Rect& bufferCrop) {
2376 layer_state_t* s = getLayerState(sc);
2377 if (!s) {
2378 mStatus = BAD_INDEX;
2379 return *this;
2380 }
2381
2382 s->what |= layer_state_t::eBufferCropChanged;
2383 s->bufferCrop = bufferCrop;
2384
2385 registerSurfaceControlForCallback(sc);
2386 return *this;
2387}
2388
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07002389SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDestinationFrame(
2390 const sp<SurfaceControl>& sc, const Rect& destinationFrame) {
2391 layer_state_t* s = getLayerState(sc);
2392 if (!s) {
2393 mStatus = BAD_INDEX;
2394 return *this;
2395 }
2396
2397 s->what |= layer_state_t::eDestinationFrameChanged;
2398 s->destinationFrame = destinationFrame;
2399
2400 registerSurfaceControlForCallback(sc);
2401 return *this;
2402}
2403
Vishnu Nair9cf4a4d2021-09-17 12:16:08 -07002404SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDropInputMode(
2405 const sp<SurfaceControl>& sc, gui::DropInputMode mode) {
2406 layer_state_t* s = getLayerState(sc);
2407 if (!s) {
2408 mStatus = BAD_INDEX;
2409 return *this;
2410 }
2411
2412 s->what |= layer_state_t::eDropInputModeChanged;
2413 s->dropInputMode = mode;
2414
2415 registerSurfaceControlForCallback(sc);
2416 return *this;
2417}
2418
Patrick Williamsf693bcf2024-08-02 09:55:23 -05002419SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBufferReleaseChannel(
2420 const sp<SurfaceControl>& sc,
2421 const std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint>& channel) {
2422 layer_state_t* s = getLayerState(sc);
2423 if (!s) {
2424 mStatus = BAD_INDEX;
2425 return *this;
2426 }
2427
2428 s->what |= layer_state_t::eBufferReleaseChannelChanged;
2429 s->bufferReleaseChannel = channel;
2430
2431 registerSurfaceControlForCallback(sc);
2432 return *this;
2433}
2434
Brian Lindahl07dcd492024-10-30 11:43:23 -06002435SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPictureProfileHandle(
2436 const sp<SurfaceControl>& sc, const PictureProfileHandle& pictureProfileHandle) {
2437 if (com_android_graphics_libgui_flags_apply_picture_profiles()) {
2438 layer_state_t* s = getLayerState(sc);
2439 if (!s) {
2440 mStatus = BAD_INDEX;
2441 return *this;
2442 }
2443
2444 s->what |= layer_state_t::ePictureProfileHandleChanged;
2445 s->pictureProfileHandle = pictureProfileHandle;
2446
2447 registerSurfaceControlForCallback(sc);
2448 }
2449 return *this;
2450}
2451
2452SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setContentPriority(
2453 const sp<SurfaceControl>& sc, int32_t priority) {
2454 if (com_android_graphics_libgui_flags_apply_picture_profiles()) {
2455 layer_state_t* s = getLayerState(sc);
2456 if (!s) {
2457 mStatus = BAD_INDEX;
2458 return *this;
2459 }
2460
2461 s->what |= layer_state_t::eAppContentPriorityChanged;
2462 s->appContentPriority = priority;
2463
2464 registerSurfaceControlForCallback(sc);
2465 }
2466 return *this;
2467}
2468
Mathias Agopian698c0872011-06-28 19:09:31 -07002469// ---------------------------------------------------------------------------
2470
chaviw763ef572018-02-22 16:04:57 -08002471DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002472 if (auto it = std::find_if(mDisplayStates.begin(), mDisplayStates.end(),
2473 [token](const auto& display) { return display.token == token; });
2474 it != mDisplayStates.end()) {
2475 return *it;
2476 }
2477
2478 // If display state doesn't exist, add a new one.
2479 DisplayState s;
2480 s.token = token;
2481 mDisplayStates.add(s);
2482 return mDisplayStates.editItemAt(mDisplayStates.size() - 1);
Mathias Agopiane57f2922012-08-09 16:29:12 -07002483}
2484
Robert Carr4cdc58f2017-08-23 14:22:20 -07002485status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
2486 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07002487 if (bufferProducer.get() != nullptr) {
2488 // Make sure that composition can never be stalled by a virtual display
2489 // consumer that isn't processing buffers fast enough.
2490 status_t err = bufferProducer->setAsyncMode(true);
2491 if (err != NO_ERROR) {
2492 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
2493 "BufferQueue. This BufferQueue cannot be used for virtual "
2494 "display. (%d)", err);
2495 return err;
2496 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002497 }
chaviw763ef572018-02-22 16:04:57 -08002498 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08002499 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002500 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07002501 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002502}
2503
Robert Carr4cdc58f2017-08-23 14:22:20 -07002504void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002505 ui::LayerStack layerStack) {
chaviw763ef572018-02-22 16:04:57 -08002506 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002507 s.layerStack = layerStack;
2508 s.what |= DisplayState::eLayerStackChanged;
2509}
2510
Evan Rosky2239b372021-05-20 13:43:47 -07002511void SurfaceComposerClient::Transaction::setDisplayFlags(const sp<IBinder>& token, uint32_t flags) {
2512 DisplayState& s(getDisplayState(token));
2513 s.flags = flags;
2514 s.what |= DisplayState::eFlagsChanged;
2515}
2516
Robert Carr4cdc58f2017-08-23 14:22:20 -07002517void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08002518 ui::Rotation orientation,
2519 const Rect& layerStackRect,
2520 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08002521 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002522 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002523 s.layerStackSpaceRect = layerStackRect;
2524 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002525 s.what |= DisplayState::eDisplayProjectionChanged;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002526}
2527
Robert Carr4cdc58f2017-08-23 14:22:20 -07002528void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08002529 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07002530 s.width = width;
2531 s.height = height;
2532 s.what |= DisplayState::eDisplaySizeChanged;
2533}
2534
Anton Ivanov4f8b4f32025-03-17 16:29:09 -07002535// copied from FrameTimelineInfo::merge()
2536void SurfaceComposerClient::Transaction::mergeFrameTimelineInfo(FrameTimelineInfo& t,
2537 const FrameTimelineInfo& other) {
2538 // When merging vsync Ids we take the oldest valid one
2539 if (t.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID &&
2540 other.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
2541 if (other.vsyncId > t.vsyncId) {
2542 t = other;
2543 }
2544 } else if (t.vsyncId == FrameTimelineInfo::INVALID_VSYNC_ID) {
2545 t = other;
2546 }
2547}
2548
Chavi Weingarten076acac2023-01-19 17:20:43 +00002549SurfaceComposerClient::Transaction&
2550SurfaceComposerClient::Transaction::setTrustedPresentationCallback(
2551 const sp<SurfaceControl>& sc, TrustedPresentationCallback cb,
2552 const TrustedPresentationThresholds& thresholds, void* context,
2553 sp<SurfaceComposerClient::PresentationCallbackRAII>& outCallbackRef) {
Vishnu Naira74ba0f2024-04-16 11:25:06 -07002554 outCallbackRef =
2555 mTransactionCompletedListener->addTrustedPresentationCallback(cb, sc->getLayerId(),
2556 context);
Chavi Weingarten076acac2023-01-19 17:20:43 +00002557
2558 layer_state_t* s = getLayerState(sc);
2559 if (!s) {
2560 mStatus = BAD_INDEX;
2561 return *this;
2562 }
2563 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2564 s->trustedPresentationThresholds = thresholds;
Anton Ivanovc7f87932025-02-25 00:58:09 -08002565 s->trustedPresentationListener.configure(
2566 {.callbackInterface = TransactionCompletedListener::getIInstance(),
2567 .callbackId = sc->getLayerId()});
Chavi Weingarten076acac2023-01-19 17:20:43 +00002568
2569 return *this;
2570}
2571
2572SurfaceComposerClient::Transaction&
2573SurfaceComposerClient::Transaction::clearTrustedPresentationCallback(const sp<SurfaceControl>& sc) {
Vishnu Naira74ba0f2024-04-16 11:25:06 -07002574 mTransactionCompletedListener->clearTrustedPresentationCallback(sc->getLayerId());
Chavi Weingarten076acac2023-01-19 17:20:43 +00002575
2576 layer_state_t* s = getLayerState(sc);
2577 if (!s) {
2578 mStatus = BAD_INDEX;
2579 return *this;
2580 }
2581 s->what |= layer_state_t::eTrustedPresentationInfoChanged;
2582 s->trustedPresentationThresholds = TrustedPresentationThresholds();
Anton Ivanovc7f87932025-02-25 00:58:09 -08002583 s->trustedPresentationListener.clear();
Chavi Weingarten076acac2023-01-19 17:20:43 +00002584
2585 return *this;
2586}
2587
Mathias Agopiane57f2922012-08-09 16:29:12 -07002588// ---------------------------------------------------------------------------
2589
chaviw60c9d3e2021-06-04 12:52:17 -05002590SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002592SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
chaviw60c9d3e2021-06-04 12:52:17 -05002593 : mStatus(NO_ERROR), mClient(client) {}
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01002594
Mathias Agopian698c0872011-06-28 19:09:31 -07002595void SurfaceComposerClient::onFirstRef() {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002596 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07002597 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08002598 sp<ISurfaceComposerClient> conn;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002599 binder::Status status = sf->createConnection(&conn);
2600 if (status.isOk() && conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002601 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002602 mStatus = NO_ERROR;
2603 }
2604 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002605}
2606
Mathias Agopian698c0872011-06-28 19:09:31 -07002607SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07002608 dispose();
2609}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07002610
Mathias Agopian698c0872011-06-28 19:09:31 -07002611status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002612 return mStatus;
2613}
2614
Mathias Agopian698c0872011-06-28 19:09:31 -07002615sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002616 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002617}
2618
Mathias Agopiand4784a32010-05-27 19:41:15 -07002619status_t SurfaceComposerClient::linkToComposerDeath(
2620 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07002621 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07002622 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
2623 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002624}
2625
Mathias Agopian698c0872011-06-28 19:09:31 -07002626void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07002628 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07002629 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07002630 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07002631 client = mClient; // hold ref while lock is held
2632 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07002634 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635}
2636
Carlos Martinez Romero4609a4a2022-11-30 00:38:11 +00002637status_t SurfaceComposerClient::bootFinished() {
2638 sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService());
2639 binder::Status status = sf->bootFinished();
2640 return statusTFromBinderStatus(status);
2641}
2642
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002643sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002644 PixelFormat format, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002645 const sp<IBinder>& parentHandle,
Valerie Hau1acd6962019-10-28 16:35:48 -07002646 LayerMetadata metadata,
2647 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07002648 sp<SurfaceControl> s;
Vishnu Nair992496b2020-10-22 17:27:21 -07002649 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07002650 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07002651 return s;
2652}
2653
Patrick Williamsa361de62022-10-06 20:34:10 +00002654static std::string toString(const String16& string) {
2655 return std::string(String8(string).c_str());
2656}
2657
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002658status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
2659 PixelFormat format,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002660 sp<SurfaceControl>* outSurface, int32_t flags,
Vishnu Nair992496b2020-10-22 17:27:21 -07002661 const sp<IBinder>& parentHandle,
2662 LayerMetadata metadata,
Valerie Hau1acd6962019-10-28 16:35:48 -07002663 uint32_t* outTransformHint) {
Robert Carr740eaf02018-03-27 12:59:18 -07002664 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07002665
Mathias Agopian698c0872011-06-28 19:09:31 -07002666 if (mStatus == NO_ERROR) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002667 gui::CreateSurfaceResult result;
Tomasz Wasilczyk02fd95c2023-08-30 17:51:31 +00002668 binder::Status status = mClient->createSurface(std::string(name.c_str()), flags,
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002669 parentHandle, std::move(metadata), &result);
2670 err = statusTFromBinderStatus(status);
Valerie Hau1acd6962019-10-28 16:35:48 -07002671 if (outTransformHint) {
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002672 *outTransformHint = result.transformHint;
Valerie Hau1acd6962019-10-28 16:35:48 -07002673 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002674 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
2675 if (err == NO_ERROR) {
Anton Ivanov8ed86592025-02-20 11:52:50 -08002676 *outSurface = sp<SurfaceControl>::make(this, result.handle, result.layerId,
2677 toString(result.layerName), w, h, format,
2678 result.transformHint, flags);
Mathias Agopian698c0872011-06-28 19:09:31 -07002679 }
2680 }
Robert Carr3b382ed2018-03-14 13:49:41 -07002681 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07002682}
2683
chaviwfe94a222019-08-21 13:52:59 -07002684sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
2685 if (mirrorFromSurface == nullptr) {
2686 return nullptr;
2687 }
2688
chaviwfe94a222019-08-21 13:52:59 -07002689 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Patrick Williamsa361de62022-10-06 20:34:10 +00002690 gui::CreateSurfaceResult result;
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002691 const binder::Status status = mClient->mirrorSurface(mirrorFromHandle, &result);
2692 const status_t err = statusTFromBinderStatus(status);
chaviwfe94a222019-08-21 13:52:59 -07002693 if (err == NO_ERROR) {
Anton Ivanov8ed86592025-02-20 11:52:50 -08002694 return sp<SurfaceControl>::make(this, result.handle, result.layerId,
2695 toString(result.layerName));
chaviwfe94a222019-08-21 13:52:59 -07002696 }
2697 return nullptr;
2698}
2699
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002700sp<SurfaceControl> SurfaceComposerClient::mirrorDisplay(DisplayId displayId) {
Patrick Williamsa361de62022-10-06 20:34:10 +00002701 gui::CreateSurfaceResult result;
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002702 const binder::Status status = mClient->mirrorDisplay(displayId.value, &result);
2703 const status_t err = statusTFromBinderStatus(status);
2704 if (err == NO_ERROR) {
Anton Ivanov8ed86592025-02-20 11:52:50 -08002705 return sp<SurfaceControl>::make(this, result.handle, result.layerId,
2706 toString(result.layerName));
Chavi Weingarten7043a7d2022-07-19 23:40:35 +00002707 }
2708 return nullptr;
2709}
2710
Svetoslavd85084b2014-03-20 10:28:31 -07002711status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
2712 if (mStatus != NO_ERROR) {
2713 return mStatus;
2714 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002715 const binder::Status status = mClient->clearLayerFrameStats(token);
2716 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002717}
2718
2719status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
2720 FrameStats* outStats) const {
2721 if (mStatus != NO_ERROR) {
2722 return mStatus;
2723 }
Huihong Luod3d8f8e2022-03-08 14:48:46 -08002724 gui::FrameStats stats;
2725 const binder::Status status = mClient->getLayerFrameStats(token, &stats);
2726 if (status.isOk()) {
2727 outStats->refreshPeriodNano = stats.refreshPeriodNano;
2728 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
2729 for (const auto& t : stats.desiredPresentTimesNano) {
2730 outStats->desiredPresentTimesNano.add(t);
2731 }
2732 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
2733 for (const auto& t : stats.actualPresentTimesNano) {
2734 outStats->actualPresentTimesNano.add(t);
2735 }
2736 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
2737 for (const auto& t : stats.frameReadyTimesNano) {
2738 outStats->frameReadyTimesNano.add(t);
2739 }
2740 }
2741 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07002742}
2743
Mathias Agopian698c0872011-06-28 19:09:31 -07002744// ----------------------------------------------------------------------------
2745
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002746status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
2747 ui::DisplayState* state) {
Huihong Luoaa7fc2e2022-02-15 10:43:00 -08002748 gui::DisplayState ds;
2749 binder::Status status =
2750 ComposerServiceAIDL::getComposerService()->getDisplayState(display, &ds);
2751 if (status.isOk()) {
2752 state->layerStack = ui::LayerStack::fromValue(ds.layerStack);
2753 state->orientation = static_cast<ui::Rotation>(ds.orientation);
2754 state->layerStackSpaceRect =
2755 ui::Size(ds.layerStackSpaceRect.width, ds.layerStackSpaceRect.height);
2756 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002757 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002758}
2759
Sally Qi6bb12822022-10-05 11:42:30 -07002760status_t SurfaceComposerClient::getStaticDisplayInfo(int64_t displayId,
Huihong Luoa79ddf42022-02-17 00:01:38 -08002761 ui::StaticDisplayInfo* outInfo) {
2762 using Tag = android::gui::DeviceProductInfo::ManufactureOrModelDate::Tag;
2763 gui::StaticDisplayInfo ginfo;
2764 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002765 ComposerServiceAIDL::getComposerService()->getStaticDisplayInfo(displayId, &ginfo);
Huihong Luoa79ddf42022-02-17 00:01:38 -08002766 if (status.isOk()) {
2767 // convert gui::StaticDisplayInfo to ui::StaticDisplayInfo
2768 outInfo->connectionType = static_cast<ui::DisplayConnectionType>(ginfo.connectionType);
Gil Dekel9897d792025-02-28 12:06:03 -05002769 outInfo->port = ginfo.port;
Huihong Luoa79ddf42022-02-17 00:01:38 -08002770 outInfo->density = ginfo.density;
2771 outInfo->secure = ginfo.secure;
2772 outInfo->installOrientation = static_cast<ui::Rotation>(ginfo.installOrientation);
2773
Snild Dolkow8d675642023-07-14 12:43:35 +02002774 if (const std::optional<gui::DeviceProductInfo> dpi = ginfo.deviceProductInfo) {
2775 DeviceProductInfo info;
2776 info.name = dpi->name;
2777 if (dpi->manufacturerPnpId.size() > 0) {
2778 // copid from PnpId = std::array<char, 4> in ui/DeviceProductInfo.h
2779 constexpr int kMaxPnpIdSize = 4;
2780 size_t count = std::max<size_t>(kMaxPnpIdSize, dpi->manufacturerPnpId.size());
2781 std::copy_n(dpi->manufacturerPnpId.begin(), count, info.manufacturerPnpId.begin());
2782 }
2783 if (dpi->relativeAddress.size() > 0) {
2784 std::copy(dpi->relativeAddress.begin(), dpi->relativeAddress.end(),
2785 std::back_inserter(info.relativeAddress));
2786 }
2787 info.productId = dpi->productId;
Huihong Luoa79ddf42022-02-17 00:01:38 -08002788
Snild Dolkow8d675642023-07-14 12:43:35 +02002789 const gui::DeviceProductInfo::ManufactureOrModelDate& date =
2790 dpi->manufactureOrModelDate;
2791 if (date.getTag() == Tag::modelYear) {
2792 DeviceProductInfo::ModelYear modelYear;
2793 modelYear.year = static_cast<uint32_t>(date.get<Tag::modelYear>().year);
2794 info.manufactureOrModelDate = modelYear;
2795 } else if (date.getTag() == Tag::manufactureYear) {
2796 DeviceProductInfo::ManufactureYear manufactureYear;
2797 manufactureYear.year = date.get<Tag::manufactureYear>().modelYear.year;
2798 info.manufactureOrModelDate = manufactureYear;
2799 } else if (date.getTag() == Tag::manufactureWeekAndYear) {
2800 DeviceProductInfo::ManufactureWeekAndYear weekAndYear;
2801 weekAndYear.year =
2802 date.get<Tag::manufactureWeekAndYear>().manufactureYear.modelYear.year;
2803 weekAndYear.week = date.get<Tag::manufactureWeekAndYear>().week;
2804 info.manufactureOrModelDate = weekAndYear;
2805 }
2806
2807 outInfo->deviceProductInfo = info;
2808 }
Huihong Luoa79ddf42022-02-17 00:01:38 -08002809 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002810 return statusTFromBinderStatus(status);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08002811}
2812
Sally Qi6bb12822022-10-05 11:42:30 -07002813void SurfaceComposerClient::getDynamicDisplayInfoInternal(gui::DynamicDisplayInfo& ginfo,
2814 ui::DynamicDisplayInfo*& outInfo) {
2815 // convert gui::DynamicDisplayInfo to ui::DynamicDisplayInfo
2816 outInfo->supportedDisplayModes.clear();
2817 outInfo->supportedDisplayModes.reserve(ginfo.supportedDisplayModes.size());
2818 for (const auto& mode : ginfo.supportedDisplayModes) {
2819 ui::DisplayMode outMode;
2820 outMode.id = mode.id;
2821 outMode.resolution.width = mode.resolution.width;
2822 outMode.resolution.height = mode.resolution.height;
2823 outMode.xDpi = mode.xDpi;
2824 outMode.yDpi = mode.yDpi;
Alec Mouri55e31032023-10-02 20:34:18 +00002825 outMode.peakRefreshRate = mode.peakRefreshRate;
Alec Mourid122a1c2023-09-30 01:36:33 +00002826 outMode.vsyncRate = mode.vsyncRate;
Sally Qi6bb12822022-10-05 11:42:30 -07002827 outMode.appVsyncOffset = mode.appVsyncOffset;
2828 outMode.sfVsyncOffset = mode.sfVsyncOffset;
2829 outMode.presentationDeadline = mode.presentationDeadline;
2830 outMode.group = mode.group;
2831 std::transform(mode.supportedHdrTypes.begin(), mode.supportedHdrTypes.end(),
2832 std::back_inserter(outMode.supportedHdrTypes),
2833 [](const int32_t& value) { return static_cast<ui::Hdr>(value); });
2834 outInfo->supportedDisplayModes.push_back(outMode);
2835 }
2836
2837 outInfo->activeDisplayModeId = ginfo.activeDisplayModeId;
2838 outInfo->renderFrameRate = ginfo.renderFrameRate;
2839
2840 outInfo->supportedColorModes.clear();
2841 outInfo->supportedColorModes.reserve(ginfo.supportedColorModes.size());
2842 for (const auto& cmode : ginfo.supportedColorModes) {
2843 outInfo->supportedColorModes.push_back(static_cast<ui::ColorMode>(cmode));
2844 }
2845
2846 outInfo->activeColorMode = static_cast<ui::ColorMode>(ginfo.activeColorMode);
2847
2848 std::vector<ui::Hdr> types;
2849 types.reserve(ginfo.hdrCapabilities.supportedHdrTypes.size());
2850 for (const auto& hdr : ginfo.hdrCapabilities.supportedHdrTypes) {
2851 types.push_back(static_cast<ui::Hdr>(hdr));
2852 }
2853 outInfo->hdrCapabilities = HdrCapabilities(types, ginfo.hdrCapabilities.maxLuminance,
2854 ginfo.hdrCapabilities.maxAverageLuminance,
2855 ginfo.hdrCapabilities.minLuminance);
2856
2857 outInfo->autoLowLatencyModeSupported = ginfo.autoLowLatencyModeSupported;
2858 outInfo->gameContentTypeSupported = ginfo.gameContentTypeSupported;
2859 outInfo->preferredBootDisplayMode = ginfo.preferredBootDisplayMode;
ramindani89fbb6e2024-09-26 11:30:24 -07002860 outInfo->hasArrSupport = ginfo.hasArrSupport;
ramindani8204da22024-10-18 15:22:52 -07002861 outInfo->frameRateCategoryRate = ui::FrameRateCategoryRate(ginfo.frameRateCategoryRate.normal,
2862 ginfo.frameRateCategoryRate.high);
ramindanibee19022024-10-29 09:20:22 -07002863 outInfo->supportedRefreshRates.clear();
2864 outInfo->supportedRefreshRates.reserve(ginfo.supportedRefreshRates.size());
2865 for (const auto rate : ginfo.supportedRefreshRates) {
2866 outInfo->supportedRefreshRates.push_back(static_cast<float>(rate));
2867 }
Sally Qi6bb12822022-10-05 11:42:30 -07002868}
2869
2870status_t SurfaceComposerClient::getDynamicDisplayInfoFromId(int64_t displayId,
2871 ui::DynamicDisplayInfo* outInfo) {
Huihong Luo38603fd2022-02-21 14:32:54 -08002872 gui::DynamicDisplayInfo ginfo;
2873 binder::Status status =
Sally Qi6bb12822022-10-05 11:42:30 -07002874 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromId(displayId,
2875 &ginfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002876 if (status.isOk()) {
Sally Qi6bb12822022-10-05 11:42:30 -07002877 getDynamicDisplayInfoInternal(ginfo, outInfo);
2878 }
2879 return statusTFromBinderStatus(status);
2880}
Huihong Luo38603fd2022-02-21 14:32:54 -08002881
Sally Qi6bb12822022-10-05 11:42:30 -07002882status_t SurfaceComposerClient::getDynamicDisplayInfoFromToken(const sp<IBinder>& display,
2883 ui::DynamicDisplayInfo* outInfo) {
2884 gui::DynamicDisplayInfo ginfo;
2885 binder::Status status =
2886 ComposerServiceAIDL::getComposerService()->getDynamicDisplayInfoFromToken(display,
2887 &ginfo);
2888 if (status.isOk()) {
2889 getDynamicDisplayInfoInternal(ginfo, outInfo);
Huihong Luo38603fd2022-02-21 14:32:54 -08002890 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002891 return statusTFromBinderStatus(status);
Dan Stoza7f7da322014-05-02 15:26:25 -07002892}
2893
Marin Shalamanova7fe3042021-01-29 21:02:08 +01002894status_t SurfaceComposerClient::getActiveDisplayMode(const sp<IBinder>& display,
2895 ui::DisplayMode* mode) {
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002896 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -07002897
2898 status_t result = getDynamicDisplayInfoFromToken(display, &info);
Dan Stoza7f7da322014-05-02 15:26:25 -07002899 if (result != NO_ERROR) {
2900 return result;
2901 }
2902
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002903 if (const auto activeMode = info.getActiveDisplayMode()) {
2904 *mode = *activeMode;
2905 return NO_ERROR;
Dan Stoza7f7da322014-05-02 15:26:25 -07002906 }
2907
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002908 ALOGE("Active display mode not found.");
2909 return NAME_NOT_FOUND;
Dan Stoza7f7da322014-05-02 15:26:25 -07002910}
2911
Ady Abraham285f8c12022-10-11 17:12:14 -07002912status_t SurfaceComposerClient::setDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
2913 const gui::DisplayModeSpecs& specs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002914 binder::Status status =
Ady Abraham285f8c12022-10-11 17:12:14 -07002915 ComposerServiceAIDL::getComposerService()->setDesiredDisplayModeSpecs(displayToken,
2916 specs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002917 return statusTFromBinderStatus(status);
Ana Krulec0782b882019-10-15 17:34:54 -07002918}
2919
Marin Shalamanov228f46b2021-01-28 21:11:45 +01002920status_t SurfaceComposerClient::getDesiredDisplayModeSpecs(const sp<IBinder>& displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002921 gui::DisplayModeSpecs* outSpecs) {
2922 if (!outSpecs) {
Huihong Luo02186fb2022-02-23 14:21:54 -08002923 return BAD_VALUE;
2924 }
Huihong Luo02186fb2022-02-23 14:21:54 -08002925 binder::Status status =
2926 ComposerServiceAIDL::getComposerService()->getDesiredDisplayModeSpecs(displayToken,
Ady Abraham285f8c12022-10-11 17:12:14 -07002927 outSpecs);
Huihong Luo3bdef862022-03-03 11:57:19 -08002928 return statusTFromBinderStatus(status);
Ana Krulec234bb162019-11-10 22:55:55 +01002929}
2930
Daniel Solomon42d04562019-01-20 21:03:19 -08002931status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
2932 ui::DisplayPrimaries& outPrimaries) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002933 gui::DisplayPrimaries primaries;
2934 binder::Status status =
2935 ComposerServiceAIDL::getComposerService()->getDisplayNativePrimaries(display,
2936 &primaries);
2937 if (status.isOk()) {
2938 outPrimaries.red.X = primaries.red.X;
2939 outPrimaries.red.Y = primaries.red.Y;
2940 outPrimaries.red.Z = primaries.red.Z;
2941
2942 outPrimaries.green.X = primaries.green.X;
2943 outPrimaries.green.Y = primaries.green.Y;
2944 outPrimaries.green.Z = primaries.green.Z;
2945
2946 outPrimaries.blue.X = primaries.blue.X;
2947 outPrimaries.blue.Y = primaries.blue.Y;
2948 outPrimaries.blue.Z = primaries.blue.Z;
2949
2950 outPrimaries.white.X = primaries.white.X;
2951 outPrimaries.white.Y = primaries.white.Y;
2952 outPrimaries.white.Z = primaries.white.Z;
2953 }
Huihong Luo3bdef862022-03-03 11:57:19 -08002954 return statusTFromBinderStatus(status);
Daniel Solomon42d04562019-01-20 21:03:19 -08002955}
2956
Michael Wright28f24d02016-07-12 13:30:53 -07002957status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07002958 ColorMode colorMode) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002959 binder::Status status = ComposerServiceAIDL::getComposerService()
2960 ->setActiveColorMode(display, static_cast<int>(colorMode));
Huihong Luo3bdef862022-03-03 11:57:19 -08002961 return statusTFromBinderStatus(status);
Michael Wright28f24d02016-07-12 13:30:53 -07002962}
2963
Kriti Dang7defaf32021-11-15 11:55:43 +01002964status_t SurfaceComposerClient::getBootDisplayModeSupport(bool* support) {
Huihong Luo37396db2022-02-15 10:43:00 -08002965 binder::Status status =
2966 ComposerServiceAIDL::getComposerService()->getBootDisplayModeSupport(support);
Huihong Luo3bdef862022-03-03 11:57:19 -08002967 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002968}
2969
Sally Qi0cbd08b2022-08-17 12:12:28 -07002970status_t SurfaceComposerClient::getOverlaySupport(gui::OverlayProperties* outProperties) {
2971 binder::Status status =
2972 ComposerServiceAIDL::getComposerService()->getOverlaySupport(outProperties);
2973 return statusTFromBinderStatus(status);
2974}
2975
Kriti Dang7defaf32021-11-15 11:55:43 +01002976status_t SurfaceComposerClient::setBootDisplayMode(const sp<IBinder>& display,
2977 ui::DisplayModeId displayModeId) {
Huihong Luoca3d9a42022-02-22 11:07:34 -08002978 binder::Status status = ComposerServiceAIDL::getComposerService()
2979 ->setBootDisplayMode(display, static_cast<int>(displayModeId));
Huihong Luo3bdef862022-03-03 11:57:19 -08002980 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002981}
2982
2983status_t SurfaceComposerClient::clearBootDisplayMode(const sp<IBinder>& display) {
Huihong Luo37396db2022-02-15 10:43:00 -08002984 binder::Status status =
2985 ComposerServiceAIDL::getComposerService()->clearBootDisplayMode(display);
Huihong Luo3bdef862022-03-03 11:57:19 -08002986 return statusTFromBinderStatus(status);
Kriti Dang7defaf32021-11-15 11:55:43 +01002987}
2988
Kriti Dang674b9372022-11-18 10:58:44 +01002989status_t SurfaceComposerClient::getHdrConversionCapabilities(
2990 std::vector<gui::HdrConversionCapability>* hdrConversionCapabilities) {
2991 binder::Status status = ComposerServiceAIDL::getComposerService()->getHdrConversionCapabilities(
2992 hdrConversionCapabilities);
2993 return statusTFromBinderStatus(status);
2994}
2995
2996status_t SurfaceComposerClient::setHdrConversionStrategy(
Kriti Dangd432bb52023-02-09 18:21:04 +01002997 gui::HdrConversionStrategy hdrConversionStrategy, ui::Hdr* outPreferredHdrOutputType) {
2998 int hdrType;
2999 binder::Status status = ComposerServiceAIDL::getComposerService()
3000 ->setHdrConversionStrategy(hdrConversionStrategy, &hdrType);
3001 *outPreferredHdrOutputType = static_cast<ui::Hdr>(hdrType);
Kriti Dang674b9372022-11-18 10:58:44 +01003002 return statusTFromBinderStatus(status);
3003}
3004
Kriti Dangafd1fda2023-01-17 13:33:15 +01003005status_t SurfaceComposerClient::getHdrOutputConversionSupport(bool* isSupported) {
3006 binder::Status status =
3007 ComposerServiceAIDL::getComposerService()->getHdrOutputConversionSupport(isSupported);
3008 return statusTFromBinderStatus(status);
3009}
3010
Andy Yu8c2703d2023-11-03 11:22:46 -07003011status_t SurfaceComposerClient::setGameModeFrameRateOverride(uid_t uid, float frameRate) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003012 binder::Status status =
Andy Yu8c2703d2023-11-03 11:22:46 -07003013 ComposerServiceAIDL::getComposerService()->setGameModeFrameRateOverride(uid, frameRate);
3014 return statusTFromBinderStatus(status);
3015}
3016
3017status_t SurfaceComposerClient::setGameDefaultFrameRateOverride(uid_t uid, float frameRate) {
3018 binder::Status status =
3019 ComposerServiceAIDL::getComposerService()->setGameDefaultFrameRateOverride(uid,
3020 frameRate);
Huihong Luo3bdef862022-03-03 11:57:19 -08003021 return statusTFromBinderStatus(status);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08003022}
3023
Tony Huangf3621102023-09-04 17:14:22 +08003024status_t SurfaceComposerClient::updateSmallAreaDetection(std::vector<int32_t>& appIds,
Tony Huang9ac5e6e2023-08-24 09:01:44 +00003025 std::vector<float>& thresholds) {
3026 binder::Status status =
Tony Huangf3621102023-09-04 17:14:22 +08003027 ComposerServiceAIDL::getComposerService()->updateSmallAreaDetection(appIds, thresholds);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00003028 return statusTFromBinderStatus(status);
3029}
3030
Tony Huangf3621102023-09-04 17:14:22 +08003031status_t SurfaceComposerClient::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00003032 binder::Status status =
Tony Huangf3621102023-09-04 17:14:22 +08003033 ComposerServiceAIDL::getComposerService()->setSmallAreaDetectionThreshold(appId,
Tony Huang9ac5e6e2023-08-24 09:01:44 +00003034 threshold);
3035 return statusTFromBinderStatus(status);
3036}
3037
Galia Peycheva5492cb52019-10-30 14:13:16 +01003038void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08003039 ComposerServiceAIDL::getComposerService()->setAutoLowLatencyMode(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01003040}
3041
Galia Peycheva5492cb52019-10-30 14:13:16 +01003042void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
Huihong Luo37396db2022-02-15 10:43:00 -08003043 ComposerServiceAIDL::getComposerService()->setGameContentType(display, on);
Galia Peycheva5492cb52019-10-30 14:13:16 +01003044}
3045
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003046void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
3047 int mode) {
Huihong Luo37396db2022-02-15 10:43:00 -08003048 ComposerServiceAIDL::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07003049}
3050
Brian Lindahl5541f122024-11-12 13:22:34 -07003051status_t SurfaceComposerClient::getMaxLayerPictureProfiles(const sp<IBinder>& token,
3052 int32_t* outMaxProfiles) {
3053 binder::Status status =
3054 ComposerServiceAIDL::getComposerService()->getMaxLayerPictureProfiles(token,
3055 outMaxProfiles);
3056 return statusTFromBinderStatus(status);
3057}
3058
Peiyong Linc6780972018-10-28 15:24:08 -07003059status_t SurfaceComposerClient::getCompositionPreference(
3060 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
3061 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
Huihong Luo05539a12022-02-23 10:29:40 -08003062 gui::CompositionPreference pref;
3063 binder::Status status =
3064 ComposerServiceAIDL::getComposerService()->getCompositionPreference(&pref);
3065 if (status.isOk()) {
3066 *defaultDataspace = static_cast<ui::Dataspace>(pref.defaultDataspace);
3067 *defaultPixelFormat = static_cast<ui::PixelFormat>(pref.defaultPixelFormat);
3068 *wideColorGamutDataspace = static_cast<ui::Dataspace>(pref.wideColorGamutDataspace);
3069 *wideColorGamutPixelFormat = static_cast<ui::PixelFormat>(pref.wideColorGamutPixelFormat);
3070 }
Huihong Luo3bdef862022-03-03 11:57:19 -08003071 return statusTFromBinderStatus(status);
Peiyong Lin0256f722018-08-31 15:45:10 -07003072}
3073
Peiyong Lin08d10512019-01-16 13:27:35 -08003074bool SurfaceComposerClient::getProtectedContentSupport() {
3075 bool supported = false;
Huihong Luo05539a12022-02-23 10:29:40 -08003076 ComposerServiceAIDL::getComposerService()->getProtectedContentSupport(&supported);
Peiyong Lin08d10512019-01-16 13:27:35 -08003077 return supported;
3078}
3079
Svetoslavd85084b2014-03-20 10:28:31 -07003080status_t SurfaceComposerClient::clearAnimationFrameStats() {
Huihong Luo4ed1c912022-02-22 14:30:01 -08003081 binder::Status status = ComposerServiceAIDL::getComposerService()->clearAnimationFrameStats();
Huihong Luo3bdef862022-03-03 11:57:19 -08003082 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07003083}
3084
3085status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
Huihong Luo4ed1c912022-02-22 14:30:01 -08003086 gui::FrameStats stats;
3087 binder::Status status =
3088 ComposerServiceAIDL::getComposerService()->getAnimationFrameStats(&stats);
3089 if (status.isOk()) {
3090 outStats->refreshPeriodNano = stats.refreshPeriodNano;
3091 outStats->desiredPresentTimesNano.setCapacity(stats.desiredPresentTimesNano.size());
3092 for (const auto& t : stats.desiredPresentTimesNano) {
3093 outStats->desiredPresentTimesNano.add(t);
3094 }
3095 outStats->actualPresentTimesNano.setCapacity(stats.actualPresentTimesNano.size());
3096 for (const auto& t : stats.actualPresentTimesNano) {
3097 outStats->actualPresentTimesNano.add(t);
3098 }
3099 outStats->frameReadyTimesNano.setCapacity(stats.frameReadyTimesNano.size());
3100 for (const auto& t : stats.frameReadyTimesNano) {
3101 outStats->frameReadyTimesNano.add(t);
3102 }
3103 }
Huihong Luo3bdef862022-03-03 11:57:19 -08003104 return statusTFromBinderStatus(status);
Svetoslavd85084b2014-03-20 10:28:31 -07003105}
3106
Kriti Dang49ad4132021-01-08 11:49:56 +01003107status_t SurfaceComposerClient::overrideHdrTypes(const sp<IBinder>& display,
3108 const std::vector<ui::Hdr>& hdrTypes) {
Huihong Luo05539a12022-02-23 10:29:40 -08003109 std::vector<int32_t> hdrTypesVector;
3110 hdrTypesVector.reserve(hdrTypes.size());
3111 for (auto t : hdrTypes) {
3112 hdrTypesVector.push_back(static_cast<int32_t>(t));
3113 }
3114
3115 binder::Status status =
3116 ComposerServiceAIDL::getComposerService()->overrideHdrTypes(display, hdrTypesVector);
Huihong Luo3bdef862022-03-03 11:57:19 -08003117 return statusTFromBinderStatus(status);
Kriti Dang49ad4132021-01-08 11:49:56 +01003118}
3119
Tej Singhe2751772021-04-06 22:05:29 -07003120status_t SurfaceComposerClient::onPullAtom(const int32_t atomId, std::string* outData,
3121 bool* success) {
Huihong Luo05539a12022-02-23 10:29:40 -08003122 gui::PullAtomData pad;
3123 binder::Status status = ComposerServiceAIDL::getComposerService()->onPullAtom(atomId, &pad);
3124 if (status.isOk()) {
Huihong Luo30aa4372022-10-03 14:54:12 -07003125 outData->assign(pad.data.begin(), pad.data.end());
Huihong Luo05539a12022-02-23 10:29:40 -08003126 *success = pad.success;
3127 }
Huihong Luo3bdef862022-03-03 11:57:19 -08003128 return statusTFromBinderStatus(status);
Tej Singhe2751772021-04-06 22:05:29 -07003129}
3130
Kevin DuBois9c0a1762018-10-16 13:32:31 -07003131status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
3132 ui::PixelFormat* outFormat,
3133 ui::Dataspace* outDataspace,
3134 uint8_t* outComponentMask) {
Huihong Luo05539a12022-02-23 10:29:40 -08003135 if (!outFormat || !outDataspace || !outComponentMask) {
3136 return BAD_VALUE;
3137 }
3138
3139 gui::ContentSamplingAttributes attrs;
3140 binder::Status status = ComposerServiceAIDL::getComposerService()
3141 ->getDisplayedContentSamplingAttributes(display, &attrs);
3142 if (status.isOk()) {
3143 *outFormat = static_cast<ui::PixelFormat>(attrs.format);
3144 *outDataspace = static_cast<ui::Dataspace>(attrs.dataspace);
3145 *outComponentMask = static_cast<uint8_t>(attrs.componentMask);
3146 }
Huihong Luo3bdef862022-03-03 11:57:19 -08003147 return statusTFromBinderStatus(status);
Kevin DuBois9c0a1762018-10-16 13:32:31 -07003148}
3149
Kevin DuBois74e53772018-11-19 10:52:38 -08003150status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
3151 bool enable, uint8_t componentMask,
3152 uint64_t maxFrames) {
Huihong Luo05539a12022-02-23 10:29:40 -08003153 binder::Status status =
3154 ComposerServiceAIDL::getComposerService()
3155 ->setDisplayContentSamplingEnabled(display, enable,
3156 static_cast<int8_t>(componentMask),
3157 static_cast<int64_t>(maxFrames));
Huihong Luo3bdef862022-03-03 11:57:19 -08003158 return statusTFromBinderStatus(status);
Kevin DuBois74e53772018-11-19 10:52:38 -08003159}
3160
Kevin DuBois1d4249a2018-08-29 10:45:14 -07003161status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
3162 uint64_t maxFrames, uint64_t timestamp,
3163 DisplayedFrameStats* outStats) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003164 if (!outStats) {
3165 return BAD_VALUE;
3166 }
3167
3168 gui::DisplayedFrameStats stats;
3169 binder::Status status =
3170 ComposerServiceAIDL::getComposerService()->getDisplayedContentSample(display, maxFrames,
3171 timestamp, &stats);
3172 if (status.isOk()) {
3173 // convert gui::DisplayedFrameStats to ui::DisplayedFrameStats
3174 outStats->numFrames = static_cast<uint64_t>(stats.numFrames);
3175 outStats->component_0_sample.reserve(stats.component_0_sample.size());
3176 for (const auto& s : stats.component_0_sample) {
3177 outStats->component_0_sample.push_back(static_cast<uint64_t>(s));
3178 }
3179 outStats->component_1_sample.reserve(stats.component_1_sample.size());
3180 for (const auto& s : stats.component_1_sample) {
3181 outStats->component_1_sample.push_back(static_cast<uint64_t>(s));
3182 }
3183 outStats->component_2_sample.reserve(stats.component_2_sample.size());
3184 for (const auto& s : stats.component_2_sample) {
3185 outStats->component_2_sample.push_back(static_cast<uint64_t>(s));
3186 }
3187 outStats->component_3_sample.reserve(stats.component_3_sample.size());
3188 for (const auto& s : stats.component_3_sample) {
3189 outStats->component_3_sample.push_back(static_cast<uint64_t>(s));
3190 }
3191 }
3192 return statusTFromBinderStatus(status);
Kevin DuBois1d4249a2018-08-29 10:45:14 -07003193}
Marissa Wall35187b32019-01-08 10:08:52 -08003194
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08003195status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
3196 bool* outIsWideColorDisplay) {
Huihong Luo37396db2022-02-15 10:43:00 -08003197 binder::Status status =
3198 ComposerServiceAIDL::getComposerService()->isWideColorDisplay(display,
3199 outIsWideColorDisplay);
Huihong Luo3bdef862022-03-03 11:57:19 -08003200 return statusTFromBinderStatus(status);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08003201}
3202
Kevin DuBois00c66832019-02-18 16:21:31 -08003203status_t SurfaceComposerClient::addRegionSamplingListener(
3204 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
3205 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003206 gui::ARect rect;
3207 rect.left = samplingArea.left;
3208 rect.top = samplingArea.top;
3209 rect.right = samplingArea.right;
3210 rect.bottom = samplingArea.bottom;
3211 binder::Status status =
3212 ComposerServiceAIDL::getComposerService()->addRegionSamplingListener(rect,
3213 stopLayerHandle,
3214 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003215 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08003216}
3217
3218status_t SurfaceComposerClient::removeRegionSamplingListener(
3219 const sp<IRegionSamplingListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003220 binder::Status status =
3221 ComposerServiceAIDL::getComposerService()->removeRegionSamplingListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003222 return statusTFromBinderStatus(status);
Kevin DuBois00c66832019-02-18 16:21:31 -08003223}
3224
Alec Mouria9a68a62021-03-04 19:14:50 -08003225status_t SurfaceComposerClient::addFpsListener(int32_t taskId,
Alec Mouriadebf5c2021-01-05 12:57:36 -08003226 const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003227 binder::Status status =
3228 ComposerServiceAIDL::getComposerService()->addFpsListener(taskId, listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003229 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08003230}
3231
3232status_t SurfaceComposerClient::removeFpsListener(const sp<gui::IFpsListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003233 binder::Status status = ComposerServiceAIDL::getComposerService()->removeFpsListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003234 return statusTFromBinderStatus(status);
Alec Mouriadebf5c2021-01-05 12:57:36 -08003235}
3236
Galia Peycheva8f04b302021-04-27 13:25:38 +02003237status_t SurfaceComposerClient::addTunnelModeEnabledListener(
3238 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003239 binder::Status status =
3240 ComposerServiceAIDL::getComposerService()->addTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003241 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02003242}
3243
3244status_t SurfaceComposerClient::removeTunnelModeEnabledListener(
3245 const sp<gui::ITunnelModeEnabledListener>& listener) {
Huihong Luo02186fb2022-02-23 14:21:54 -08003246 binder::Status status =
3247 ComposerServiceAIDL::getComposerService()->removeTunnelModeEnabledListener(listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003248 return statusTFromBinderStatus(status);
Galia Peycheva8f04b302021-04-27 13:25:38 +02003249}
3250
Dan Gittik57e63c52019-01-18 16:37:54 +00003251bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
3252 bool support = false;
Huihong Luo37396db2022-02-15 10:43:00 -08003253 binder::Status status =
3254 ComposerServiceAIDL::getComposerService()->getDisplayBrightnessSupport(displayToken,
3255 &support);
3256 return status.isOk() ? support : false;
Dan Gittik57e63c52019-01-18 16:37:54 +00003257}
3258
3259status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
John Reck22be6962021-03-10 12:59:54 -05003260 const gui::DisplayBrightness& brightness) {
Huihong Luo37396db2022-02-15 10:43:00 -08003261 binder::Status status =
3262 ComposerServiceAIDL::getComposerService()->setDisplayBrightness(displayToken,
3263 brightness);
Huihong Luo3bdef862022-03-03 11:57:19 -08003264 return statusTFromBinderStatus(status);
Dan Gittik57e63c52019-01-18 16:37:54 +00003265}
3266
John Reck88270902021-03-18 11:27:35 -04003267status_t SurfaceComposerClient::addHdrLayerInfoListener(
3268 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08003269 binder::Status status =
3270 ComposerServiceAIDL::getComposerService()->addHdrLayerInfoListener(displayToken,
3271 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003272 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04003273}
3274
3275status_t SurfaceComposerClient::removeHdrLayerInfoListener(
3276 const sp<IBinder>& displayToken, const sp<gui::IHdrLayerInfoListener>& listener) {
Huihong Luo37396db2022-02-15 10:43:00 -08003277 binder::Status status =
3278 ComposerServiceAIDL::getComposerService()->removeHdrLayerInfoListener(displayToken,
3279 listener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003280 return statusTFromBinderStatus(status);
John Reck88270902021-03-18 11:27:35 -04003281}
3282
Brian Lindahlf1cfbf62024-12-06 08:07:16 -07003283status_t SurfaceComposerClient::addActivePictureListener(
Brian Lindahlf5fdff82024-11-01 09:28:47 -06003284 const sp<gui::IActivePictureListener>& listener) {
3285 binder::Status status =
Brian Lindahlf1cfbf62024-12-06 08:07:16 -07003286 ComposerServiceAIDL::getComposerService()->addActivePictureListener(listener);
3287 return statusTFromBinderStatus(status);
3288}
3289
3290status_t SurfaceComposerClient::removeActivePictureListener(
3291 const sp<gui::IActivePictureListener>& listener) {
3292 binder::Status status =
3293 ComposerServiceAIDL::getComposerService()->removeActivePictureListener(listener);
Brian Lindahlf5fdff82024-11-01 09:28:47 -06003294 return statusTFromBinderStatus(status);
3295}
3296
Lais Andrade3a6e47d2020-04-02 11:20:16 +01003297status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
Huihong Luo37396db2022-02-15 10:43:00 -08003298 binder::Status status = ComposerServiceAIDL::getComposerService()->notifyPowerBoost(boostId);
Huihong Luo3bdef862022-03-03 11:57:19 -08003299 return statusTFromBinderStatus(status);
Ady Abraham8532d012019-05-08 14:50:56 -07003300}
3301
Vishnu Nairb13bb952019-11-15 10:24:08 -08003302status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
3303 const half4& spotColor, float lightPosY,
3304 float lightPosZ, float lightRadius) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003305 gui::Color ambientColorG, spotColorG;
3306 ambientColorG.r = ambientColor.r;
3307 ambientColorG.g = ambientColor.g;
3308 ambientColorG.b = ambientColor.b;
3309 ambientColorG.a = ambientColor.a;
3310 spotColorG.r = spotColor.r;
3311 spotColorG.g = spotColor.g;
3312 spotColorG.b = spotColor.b;
3313 spotColorG.a = spotColor.a;
3314 binder::Status status =
3315 ComposerServiceAIDL::getComposerService()->setGlobalShadowSettings(ambientColorG,
3316 spotColorG,
3317 lightPosY, lightPosZ,
3318 lightRadius);
3319 return statusTFromBinderStatus(status);
Vishnu Nairb13bb952019-11-15 10:24:08 -08003320}
3321
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05003322std::optional<DisplayDecorationSupport> SurfaceComposerClient::getDisplayDecorationSupport(
3323 const sp<IBinder>& displayToken) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003324 std::optional<gui::DisplayDecorationSupport> gsupport;
3325 binder::Status status =
3326 ComposerServiceAIDL::getComposerService()->getDisplayDecorationSupport(displayToken,
3327 &gsupport);
Leon Scroggins IIIe7c51c62022-02-01 15:53:54 -05003328 std::optional<DisplayDecorationSupport> support;
Leon Scroggins III9a11a2e2022-10-04 12:46:21 -04003329 if (status.isOk() && gsupport.has_value()) {
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04003330 support.emplace(DisplayDecorationSupport{
3331 .format =
3332 static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
3333 gsupport->format),
3334 .alphaInterpretation =
Huihong Luo3bdef862022-03-03 11:57:19 -08003335 static_cast<aidl::android::hardware::graphics::common::AlphaInterpretation>(
Leon Scroggins III0f365ab2022-08-03 15:35:40 -04003336 gsupport->alphaInterpretation)
3337 });
Huihong Luo3bdef862022-03-03 11:57:19 -08003338 }
Leon Scroggins IIIe5cff632021-12-29 11:53:36 -05003339 return support;
3340}
3341
Huihong Luo02186fb2022-02-23 14:21:54 -08003342int SurfaceComposerClient::getGpuContextPriority() {
3343 int priority;
3344 binder::Status status =
3345 ComposerServiceAIDL::getComposerService()->getGpuContextPriority(&priority);
3346 if (!status.isOk()) {
Huihong Luo3bdef862022-03-03 11:57:19 -08003347 status_t err = statusTFromBinderStatus(status);
Huihong Luo02186fb2022-02-23 14:21:54 -08003348 ALOGE("getGpuContextPriority failed to read data: %s (%d)", strerror(-err), err);
3349 return 0;
3350 }
3351 return priority;
Ana Krulec31f2b3c2020-12-14 14:30:09 -08003352}
3353
chaviw60c9d3e2021-06-04 12:52:17 -05003354status_t SurfaceComposerClient::addWindowInfosListener(
chaviwa2948ad2022-03-08 11:07:01 -06003355 const sp<WindowInfosListener>& windowInfosListener,
3356 std::pair<std::vector<gui::WindowInfo>, std::vector<gui::DisplayInfo>>* outInitialInfo) {
chaviw60c9d3e2021-06-04 12:52:17 -05003357 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08003358 ->addWindowInfosListener(windowInfosListener, ComposerServiceAIDL::getComposerService(),
chaviwa2948ad2022-03-08 11:07:01 -06003359 outInitialInfo);
chaviw60c9d3e2021-06-04 12:52:17 -05003360}
3361
3362status_t SurfaceComposerClient::removeWindowInfosListener(
3363 const sp<WindowInfosListener>& windowInfosListener) {
3364 return WindowInfosListenerReporter::getInstance()
Huihong Luo02186fb2022-02-23 14:21:54 -08003365 ->removeWindowInfosListener(windowInfosListener,
3366 ComposerServiceAIDL::getComposerService());
chaviw60c9d3e2021-06-04 12:52:17 -05003367}
Wenhui Yangeafc18a2024-05-14 17:15:52 +00003368
3369void SurfaceComposerClient::notifyShutdown() {
3370 ComposerServiceAIDL::getComposerService()->notifyShutdown();
3371}
Mathias Agopian698c0872011-06-28 19:09:31 -07003372// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003373
chaviw690db382020-07-27 16:46:46 -07003374status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003375 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003376 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003377 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003378
Huihong Luof5029222021-12-16 14:33:46 -08003379 binder::Status status = s->captureDisplay(captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003380 return statusTFromBinderStatus(status);
Robert Carr673134e2017-01-09 19:48:38 -08003381}
3382
John Reck7e4c4872023-11-14 18:31:03 -05003383status_t ScreenshotClient::captureDisplay(DisplayId displayId, const gui::CaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07003384 const sp<IScreenCaptureListener>& captureListener) {
Huihong Luof5029222021-12-16 14:33:46 -08003385 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
chaviw93df2ea2019-04-30 16:45:12 -07003386 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003387
John Reck7e4c4872023-11-14 18:31:03 -05003388 binder::Status status = s->captureDisplayById(displayId.value, captureArgs, captureListener);
Huihong Luo3bdef862022-03-03 11:57:19 -08003389 return statusTFromBinderStatus(status);
chaviw93df2ea2019-04-30 16:45:12 -07003390}
3391
chaviw26c52482020-07-28 16:25:52 -07003392status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
Chavi Weingartenf6fb4452024-01-23 21:10:30 +00003393 const sp<IScreenCaptureListener>& captureListener,
3394 bool sync) {
Huihong Luof5029222021-12-16 14:33:46 -08003395 sp<gui::ISurfaceComposer> s(ComposerServiceAIDL::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07003396 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07003397
Chavi Weingartenf6fb4452024-01-23 21:10:30 +00003398 binder::Status status;
3399 if (sync) {
3400 gui::ScreenCaptureResults captureResults;
3401 status = s->captureLayersSync(captureArgs, &captureResults);
3402 captureListener->onScreenCaptureCompleted(captureResults);
3403 } else {
3404 status = s->captureLayers(captureArgs, captureListener);
3405 }
Huihong Luo3bdef862022-03-03 11:57:19 -08003406 return statusTFromBinderStatus(status);
chaviwa76b2712017-09-20 12:02:26 -07003407}
Dominik Laskowski718f9602019-11-09 20:01:35 -08003408
chaviw69058fb2021-09-27 09:37:30 -05003409// ---------------------------------------------------------------------------------
3410
3411void ReleaseCallbackThread::addReleaseCallback(const ReleaseCallbackId callbackId,
3412 sp<Fence> releaseFence) {
3413 std::scoped_lock<std::mutex> lock(mMutex);
3414 if (!mStarted) {
3415 mThread = std::thread(&ReleaseCallbackThread::threadMain, this);
3416 mStarted = true;
3417 }
3418
3419 mCallbackInfos.emplace(callbackId, std::move(releaseFence));
3420 mReleaseCallbackPending.notify_one();
3421}
3422
3423void ReleaseCallbackThread::threadMain() {
3424 const auto listener = TransactionCompletedListener::getInstance();
3425 std::queue<std::tuple<const ReleaseCallbackId, const sp<Fence>>> callbackInfos;
3426 while (true) {
3427 {
3428 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003429 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003430 callbackInfos = std::move(mCallbackInfos);
3431 mCallbackInfos = {};
3432 }
3433
3434 while (!callbackInfos.empty()) {
3435 auto [callbackId, releaseFence] = callbackInfos.front();
Huihong Luoffee3bc2023-01-17 16:14:35 +00003436 listener->onReleaseBuffer(callbackId, std::move(releaseFence), UINT_MAX);
chaviw69058fb2021-09-27 09:37:30 -05003437 callbackInfos.pop();
3438 }
3439
3440 {
3441 std::unique_lock<std::mutex> lock(mMutex);
Chavi Weingartene0237bb2023-02-06 21:48:32 +00003442 base::ScopedLockAssertion assumeLocked(mMutex);
chaviw69058fb2021-09-27 09:37:30 -05003443 if (mCallbackInfos.size() == 0) {
3444 mReleaseCallbackPending.wait(lock);
3445 }
3446 }
3447 }
3448}
3449
Dominik Laskowski718f9602019-11-09 20:01:35 -08003450} // namespace android