blob: d41da9b3081809ef27aea8dadb6e8916283ade04 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-04 03:31:441/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4420#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4421
The Android Open Source Projectedbf3b62009-03-04 03:31:4422#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4423#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 21:20:5924#include <utils/SortedVector.h>
25#include <utils/String8.h>
26#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4427
Marissa Wall7a9b6ff2018-08-22 00:26:2028#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 21:20:5929#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-22 00:26:2030#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-10 01:46:3731
Michael Wright28f24d02016-07-12 20:30:5332#include <system/graphics.h>
33
Robert Carr673134e2017-01-10 03:48:3834#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-20 05:22:2135#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-15 01:11:0236#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-26 02:48:3537#include <gui/ISurfaceComposer.h>
38#include <gui/ISurfaceComposerClient.h>
Robert Carr4cdc58f2017-08-23 21:22:2039#include <gui/LayerState.h>
Robert Carr0d480722017-01-11 00:42:5440#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-26 02:48:3541#include <gui/SurfaceComposerClient.h>
Marin Shalamanova7fe3042021-01-29 20:02:0842#include <ui/DisplayMode.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4443
Robert Carr2c358bf2018-08-08 22:58:1544#ifndef NO_INPUT
45#include <input/InputWindow.h>
46#endif
47
Mathias Agopian41f673c2011-11-18 01:48:3548#include <private/gui/ComposerService.h>
The Android Open Source Projectedbf3b62009-03-04 03:31:4449
Marissa Wall73411622019-01-25 18:45:4150// This server size should always be smaller than the server cache size
51#define BUFFER_CACHE_MAX_SIZE 64
52
The Android Open Source Projectedbf3b62009-03-04 03:31:4453namespace android {
Peiyong Lin9f034472018-03-28 22:29:0054
55using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-04 03:31:4456// ---------------------------------------------------------------------------
57
Mathias Agopian7e27f052010-05-28 21:22:2358ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
59
Mathias Agopianb7e930d2010-06-01 22:12:5860ComposerService::ComposerService()
61: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-07 01:45:5662 Mutex::Autolock _l(mLock);
63 connectLocked();
64}
65
66void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 22:12:5867 const String16 name("SurfaceFlinger");
68 while (getService(name, &mComposerService) != NO_ERROR) {
69 usleep(250000);
70 }
Yi Konga03e0442018-07-17 18:16:5771 assert(mComposerService != nullptr);
Andy McFadden6652b3e2012-09-07 01:45:5672
73 // Create the death listener.
74 class DeathObserver : public IBinder::DeathRecipient {
75 ComposerService& mComposerService;
76 virtual void binderDied(const wp<IBinder>& who) {
77 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
78 who.unsafe_get());
79 mComposerService.composerServiceDied();
80 }
81 public:
Chih-Hung Hsiehe2347b72016-04-25 22:41:0582 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-07 01:45:5683 };
84
85 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 16:01:0186 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 22:12:5887}
88
Andy McFadden6652b3e2012-09-07 01:45:5689/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
90 ComposerService& instance = ComposerService::getInstance();
91 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 23:34:5992 if (instance.mComposerService == nullptr) {
Andy McFadden6652b3e2012-09-07 01:45:5693 ComposerService::getInstance().connectLocked();
Yi Konga03e0442018-07-17 18:16:5794 assert(instance.mComposerService != nullptr);
Andy McFadden6652b3e2012-09-07 01:45:5695 ALOGD("ComposerService reconnected");
96 }
97 return instance.mComposerService;
98}
99
100void ComposerService::composerServiceDied()
101{
102 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 23:34:59103 mComposerService = nullptr;
104 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 22:12:58105}
106
Robert Carrfb4d58b2019-01-15 17:21:27107class DefaultComposerClient: public Singleton<DefaultComposerClient> {
108 Mutex mLock;
109 sp<SurfaceComposerClient> mClient;
110 friend class Singleton<ComposerService>;
111public:
112 static sp<SurfaceComposerClient> getComposerClient() {
113 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
114 Mutex::Autolock _l(dc.mLock);
115 if (dc.mClient == nullptr) {
116 dc.mClient = new SurfaceComposerClient;
117 }
118 return dc.mClient;
119 }
120};
121ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
122
123
124sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
125 return DefaultComposerClient::getComposerClient();
126}
127
Jorim Jaggi9c03b502020-11-24 22:51:31128JankDataListener::~JankDataListener() {
129}
130
Mathias Agopian7e27f052010-05-28 21:22:23131// ---------------------------------------------------------------------------
132
Marissa Wall7a9b6ff2018-08-22 00:26:20133// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
134// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
135// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
136
Marissa Wallc837b5e2018-10-12 17:04:44137// 0 is an invalid callback id
138TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
139
140CallbackId TransactionCompletedListener::getNextIdLocked() {
141 return mCallbackIdCounter++;
142}
143
Marissa Wall7a9b6ff2018-08-22 00:26:20144sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
145 static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener;
146 return sInstance;
147}
148
Marissa Wallc837b5e2018-10-12 17:04:44149sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
150 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
151}
152
153void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-22 00:26:20154 if (mListening) {
155 return;
156 }
157 ProcessState::self()->startThreadPool();
158 mListening = true;
159}
160
Marissa Wall80d94ad2019-01-19 00:04:36161CallbackId TransactionCompletedListener::addCallbackFunction(
162 const TransactionCompletedCallback& callbackFunction,
163 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
164 surfaceControls) {
Marissa Wallc837b5e2018-10-12 17:04:44165 std::lock_guard<std::mutex> lock(mMutex);
166 startListeningLocked();
167
168 CallbackId callbackId = getNextIdLocked();
Marissa Wall80d94ad2019-01-19 00:04:36169 mCallbacks[callbackId].callbackFunction = callbackFunction;
170
171 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
172
173 for (const auto& surfaceControl : surfaceControls) {
174 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
175 }
176
Marissa Wallc837b5e2018-10-12 17:04:44177 return callbackId;
178}
179
Jorim Jaggi9c03b502020-11-24 22:51:31180void TransactionCompletedListener::addJankListener(const sp<JankDataListener>& listener,
181 sp<SurfaceControl> surfaceControl) {
182 std::lock_guard<std::mutex> lock(mMutex);
183 mJankListeners.insert({surfaceControl->getHandle(), listener});
184}
185
186void TransactionCompletedListener::removeJankListener(const sp<JankDataListener>& listener) {
187 std::lock_guard<std::mutex> lock(mMutex);
188 for (auto it = mJankListeners.begin(); it != mJankListeners.end();) {
189 if (it->second == listener) {
190 it = mJankListeners.erase(it);
191 } else {
192 it++;
193 }
194 }
195}
196
Jorim Jaggif51775d2021-01-14 22:44:15197void TransactionCompletedListener::addSurfaceStatsListener(void* context, void* cookie,
198 sp<SurfaceControl> surfaceControl, SurfaceStatsCallback listener) {
199 std::lock_guard<std::mutex> lock(mMutex);
200 mSurfaceStatsListeners.insert({surfaceControl->getHandle(),
201 SurfaceStatsCallbackEntry(context, cookie, listener)});
202}
203
204void TransactionCompletedListener::removeSurfaceStatsListener(void* context, void* cookie) {
205 std::lock_guard<std::mutex> lock(mMutex);
206 for (auto it = mSurfaceStatsListeners.begin(); it != mSurfaceStatsListeners.end();) {
207 auto [itContext, itCookie, itListener] = it->second;
208 if (itContext == context && itCookie == cookie) {
209 it = mSurfaceStatsListeners.erase(it);
210 } else {
211 it++;
212 }
213 }
214}
215
Marissa Wall80d94ad2019-01-19 00:04:36216void TransactionCompletedListener::addSurfaceControlToCallbacks(
217 const sp<SurfaceControl>& surfaceControl,
218 const std::unordered_set<CallbackId>& callbackIds) {
219 std::lock_guard<std::mutex> lock(mMutex);
220
221 for (auto callbackId : callbackIds) {
222 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
223 std::forward_as_tuple(
224 surfaceControl->getHandle()),
225 std::forward_as_tuple(surfaceControl));
226 }
227}
228
Marissa Walle2ffb422018-10-12 18:33:52229void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Valerie Haud3b90d22019-11-06 17:37:31230 std::unordered_map<CallbackId, CallbackTranslation> callbacksMap;
Jorim Jaggi9c03b502020-11-24 22:51:31231 std::multimap<sp<IBinder>, sp<JankDataListener>> jankListenersMap;
Jorim Jaggif51775d2021-01-14 22:44:15232 std::multimap<sp<IBinder>, SurfaceStatsCallbackEntry> surfaceListeners;
Valerie Haud3b90d22019-11-06 17:37:31233 {
234 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 18:33:52235
Valerie Haud3b90d22019-11-06 17:37:31236 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
237 * callbackIds, except for when Transactions are merged together. This probably cannot be
238 * solved before this point because the Transactions could be merged together and applied in
239 * a different process.
240 *
241 * Fortunately, we get all the callbacks for this listener for the same frame together at
242 * the same time. This means if any Transactions were merged together, we will get their
243 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
244 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
245 * sp<SurfaceControl> that could possibly exist for the callbacks.
246 */
247 callbacksMap = mCallbacks;
Jorim Jaggi9c03b502020-11-24 22:51:31248 jankListenersMap = mJankListeners;
Jorim Jaggif51775d2021-01-14 22:44:15249 surfaceListeners = mSurfaceStatsListeners;
Valerie Haud3b90d22019-11-06 17:37:31250 for (const auto& transactionStats : listenerStats.transactionStats) {
251 for (auto& callbackId : transactionStats.callbackIds) {
252 mCallbacks.erase(callbackId);
253 }
Marissa Wall80d94ad2019-01-19 00:04:36254 }
255 }
Marissa Walld600d572019-03-26 22:38:50256 for (const auto& transactionStats : listenerStats.transactionStats) {
257 for (auto callbackId : transactionStats.callbackIds) {
Valerie Haud3b90d22019-11-06 17:37:31258 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-19 00:04:36259 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 18:33:52260 ALOGE("cannot call null callback function, skipping");
261 continue;
262 }
Marissa Wall80d94ad2019-01-19 00:04:36263 std::vector<SurfaceControlStats> surfaceControlStats;
264 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 17:37:31265 surfaceControlStats
266 .emplace_back(callbacksMap[callbackId]
267 .surfaceControls[surfaceStats.surfaceControl],
Valerie Hau871d6352020-01-29 16:44:02268 transactionStats.latchTime, surfaceStats.acquireTime,
269 transactionStats.presentFence,
270 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
271 surfaceStats.eventStats);
Valerie Hau84d87ff2020-01-09 01:23:21272 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl]) {
273 callbacksMap[callbackId]
274 .surfaceControls[surfaceStats.surfaceControl]
275 ->setTransformHint(surfaceStats.transformHint);
276 }
Marissa Wall80d94ad2019-01-19 00:04:36277 }
278
279 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
280 surfaceControlStats);
Marissa Walle2ffb422018-10-12 18:33:52281 }
Jorim Jaggi9c03b502020-11-24 22:51:31282 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Jorim Jaggif51775d2021-01-14 22:44:15283 auto listenerRange = surfaceListeners.equal_range(surfaceStats.surfaceControl);
284 for (auto it = listenerRange.first; it != listenerRange.second; it++) {
285 auto entry = it->second;
286 entry.callback(entry.context, transactionStats.latchTime,
287 transactionStats.presentFence, surfaceStats);
288 }
289
Jorim Jaggi9c03b502020-11-24 22:51:31290 if (surfaceStats.jankData.empty()) continue;
Jorim Jaggif51775d2021-01-14 22:44:15291 auto jankRange = jankListenersMap.equal_range(surfaceStats.surfaceControl);
292 for (auto it = jankRange.first; it != jankRange.second; it++) {
Jorim Jaggi9c03b502020-11-24 22:51:31293 it->second->onJankDataAvailable(surfaceStats.jankData);
294 }
295 }
Marissa Walle2ffb422018-10-12 18:33:52296 }
Marissa Wall7a9b6ff2018-08-22 00:26:20297}
298
299// ---------------------------------------------------------------------------
300
Robert Carre06ad2b2020-04-10 22:09:33301void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 21:58:34302
Robert Carre06ad2b2020-04-10 22:09:33303/**
304 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
305 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
306 * 1. Cost of sending the FD
307 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
308 * To ease this cost we implement the following scheme of caching buffers to integers,
309 * or said-otherwise, naming them with integers. This is the scheme known as slots in
310 * the legacy BufferQueue system.
311 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
312 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
313 * send the cached integer.
314 * 3. If there is a cache miss, we cache the new buffer and send the integer
315 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
316 * entry, and we use the integer for further communication.
317 * A few details about lifetime:
318 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
319 * which is per process Unique. The server side cache is larger than the client side
320 * cache so that the server will never evict entries before the client.
321 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
322 * transaction.
323 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
324 * to auto-evict destroyed buffers.
325 */
Marissa Wall73411622019-01-25 18:45:41326class BufferCache : public Singleton<BufferCache> {
327public:
328 BufferCache() : token(new BBinder()) {}
329
330 sp<IBinder> getToken() {
331 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
332 }
333
Marissa Wall78b72202019-03-15 21:58:34334 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 19:58:32335 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 18:45:41336
Marissa Wall78b72202019-03-15 21:58:34337 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 18:45:41338 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 21:58:34339 return BAD_VALUE;
Marissa Wall73411622019-01-25 18:45:41340 }
Marissa Wall78b72202019-03-15 21:58:34341 itr->second = getCounter();
342 *cacheId = buffer->getId();
343 return NO_ERROR;
Marissa Wall73411622019-01-25 18:45:41344 }
345
Marissa Wall78b72202019-03-15 21:58:34346 uint64_t cache(const sp<GraphicBuffer>& buffer) {
Yi Kongd2639aa2019-02-28 19:58:32347 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 18:45:41348
Marissa Wall78b72202019-03-15 21:58:34349 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
350 evictLeastRecentlyUsedBuffer();
351 }
Marissa Wall73411622019-01-25 18:45:41352
Robert Carre06ad2b2020-04-10 22:09:33353 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 21:58:34354
355 mBuffers[buffer->getId()] = getCounter();
356 return buffer->getId();
357 }
358
359 void uncache(uint64_t cacheId) {
360 std::lock_guard<std::mutex> lock(mMutex);
361 uncacheLocked(cacheId);
362 }
363
364 void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) {
365 mBuffers.erase(cacheId);
366 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
Marissa Wall73411622019-01-25 18:45:41367 }
368
369private:
Marissa Wall78b72202019-03-15 21:58:34370 void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 18:45:41371 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 21:58:34372 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 18:45:41373 auto minBuffer = itr;
374 itr++;
375
376 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 21:58:34377 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 18:45:41378 if (counter < minCounter) {
379 minCounter = counter;
380 minBuffer = itr;
381 }
382 itr++;
383 }
Marissa Wall78b72202019-03-15 21:58:34384 uncacheLocked(minBuffer->first);
Marissa Wall73411622019-01-25 18:45:41385 }
386
387 uint64_t getCounter() REQUIRES(mMutex) {
388 static uint64_t counter = 0;
389 return counter++;
390 }
391
Marissa Wall73411622019-01-25 18:45:41392 std::mutex mMutex;
Marissa Wall78b72202019-03-15 21:58:34393 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 18:45:41394
395 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
396 sp<IBinder> token;
397};
398
399ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
400
Robert Carre06ad2b2020-04-10 22:09:33401void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 21:58:34402 // GraphicBuffer id's are used as the cache ids.
403 BufferCache::getInstance().uncache(graphicBufferId);
404}
405
Marissa Wall73411622019-01-25 18:45:41406// ---------------------------------------------------------------------------
407
Pablo Gamito7eb7ee72020-08-05 10:57:05408// Initialize transaction id counter used to generate transaction ids
409// Transactions will start counting at 1, 0 is used for invalid transactions
410std::atomic<uint32_t> SurfaceComposerClient::Transaction::idCounter = 1;
411
412SurfaceComposerClient::Transaction::Transaction() {
413 mId = generateId();
414}
415
Marissa Wall17b4e452018-12-27 00:32:34416SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
Pablo Gamito7eb7ee72020-08-05 10:57:05417 : mId(other.mId),
418 mForceSynchronous(other.mForceSynchronous),
Marissa Wall17b4e452018-12-27 00:32:34419 mTransactionNestCount(other.mTransactionNestCount),
420 mAnimation(other.mAnimation),
421 mEarlyWakeup(other.mEarlyWakeup),
Ady Abrahambf1349c2020-06-12 21:26:18422 mExplicitEarlyWakeupStart(other.mExplicitEarlyWakeupStart),
423 mExplicitEarlyWakeupEnd(other.mExplicitEarlyWakeupEnd),
Vishnu Nair621102e2019-06-12 21:16:57424 mContainsBuffer(other.mContainsBuffer),
Ady Abraham22c7b5c2020-09-23 02:33:40425 mDesiredPresentTime(other.mDesiredPresentTime),
Ady Abrahamf0c56492020-12-18 02:04:15426 mIsAutoTimestamp(other.mIsAutoTimestamp),
Siarhei Vishniakoufc434ac2021-01-13 20:28:00427 mFrameTimelineInfo(other.mFrameTimelineInfo),
Vishnu Nair277142c2021-01-06 02:35:29428 mApplyToken(other.mApplyToken) {
Robert Carr4cdc58f2017-08-23 21:22:20429 mDisplayStates = other.mDisplayStates;
430 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 19:46:30431 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 21:16:57432 mListenerCallbacks = other.mListenerCallbacks;
433}
434
435std::unique_ptr<SurfaceComposerClient::Transaction>
436SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
437 auto transaction = std::make_unique<Transaction>();
438 if (transaction->readFromParcel(parcel) == NO_ERROR) {
439 return transaction;
440 }
441 return nullptr;
442}
443
Pablo Gamito7eb7ee72020-08-05 10:57:05444int64_t SurfaceComposerClient::Transaction::generateId() {
445 return (((int64_t)getpid()) << 32) | idCounter++;
446}
447
Vishnu Nair621102e2019-06-12 21:16:57448status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
449 const uint32_t forceSynchronous = parcel->readUint32();
450 const uint32_t transactionNestCount = parcel->readUint32();
451 const bool animation = parcel->readBool();
452 const bool earlyWakeup = parcel->readBool();
Ady Abrahambf1349c2020-06-12 21:26:18453 const bool explicitEarlyWakeupStart = parcel->readBool();
454 const bool explicitEarlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 21:16:57455 const bool containsBuffer = parcel->readBool();
456 const int64_t desiredPresentTime = parcel->readInt64();
Ady Abrahamf0c56492020-12-18 02:04:15457 const bool isAutoTimestamp = parcel->readBool();
Siarhei Vishniakoufc434ac2021-01-13 20:28:00458 FrameTimelineInfo frameTimelineInfo;
459 SAFE_PARCEL(frameTimelineInfo.read, *parcel);
460
Vishnu Nair277142c2021-01-06 02:35:29461 sp<IBinder> applyToken;
462 parcel->readNullableStrongBinder(&applyToken);
Vishnu Nair621102e2019-06-12 21:16:57463 size_t count = static_cast<size_t>(parcel->readUint32());
464 if (count > parcel->dataSize()) {
465 return BAD_VALUE;
466 }
467 SortedVector<DisplayState> displayStates;
468 displayStates.setCapacity(count);
469 for (size_t i = 0; i < count; i++) {
470 DisplayState displayState;
471 if (displayState.read(*parcel) == BAD_VALUE) {
472 return BAD_VALUE;
473 }
474 displayStates.add(displayState);
475 }
476
477 count = static_cast<size_t>(parcel->readUint32());
478 if (count > parcel->dataSize()) {
479 return BAD_VALUE;
480 }
Valerie Hau9dab9732019-08-20 16:29:25481 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
482 listenerCallbacks.reserve(count);
483 for (size_t i = 0; i < count; i++) {
484 sp<ITransactionCompletedListener> listener =
485 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
486 size_t numCallbackIds = parcel->readUint32();
487 if (numCallbackIds > parcel->dataSize()) {
488 return BAD_VALUE;
489 }
490 for (size_t j = 0; j < numCallbackIds; j++) {
491 listenerCallbacks[listener].callbackIds.insert(parcel->readInt64());
492 }
493 size_t numSurfaces = parcel->readUint32();
494 if (numSurfaces > parcel->dataSize()) {
495 return BAD_VALUE;
496 }
497 for (size_t j = 0; j < numSurfaces; j++) {
498 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45499 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 16:29:25500 listenerCallbacks[listener].surfaceControls.insert(surface);
501 }
502 }
503
504 count = static_cast<size_t>(parcel->readUint32());
505 if (count > parcel->dataSize()) {
506 return BAD_VALUE;
507 }
Vishnu Nairf03652d2019-07-17 00:56:56508 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 21:16:57509 composerStates.reserve(count);
510 for (size_t i = 0; i < count; i++) {
Pablo Gamitodbc31672020-09-01 18:28:58511 sp<IBinder> surfaceControlHandle;
512 SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
Vishnu Nair621102e2019-06-12 21:16:57513
514 ComposerState composerState;
515 if (composerState.read(*parcel) == BAD_VALUE) {
516 return BAD_VALUE;
517 }
Pablo Gamitodbc31672020-09-01 18:28:58518
Vishnu Nairf03652d2019-07-17 00:56:56519 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 21:16:57520 }
521
522 InputWindowCommands inputWindowCommands;
523 inputWindowCommands.read(*parcel);
524
525 // Parsing was successful. Update the object.
526 mForceSynchronous = forceSynchronous;
527 mTransactionNestCount = transactionNestCount;
528 mAnimation = animation;
529 mEarlyWakeup = earlyWakeup;
Ady Abrahambf1349c2020-06-12 21:26:18530 mExplicitEarlyWakeupStart = explicitEarlyWakeupStart;
531 mExplicitEarlyWakeupEnd = explicitEarlyWakeupEnd;
Vishnu Nair621102e2019-06-12 21:16:57532 mContainsBuffer = containsBuffer;
533 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-18 02:04:15534 mIsAutoTimestamp = isAutoTimestamp;
Siarhei Vishniakoufc434ac2021-01-13 20:28:00535 mFrameTimelineInfo = frameTimelineInfo;
Vishnu Nair621102e2019-06-12 21:16:57536 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 16:29:25537 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 21:16:57538 mComposerStates = composerStates;
539 mInputWindowCommands = inputWindowCommands;
Vishnu Nair277142c2021-01-06 02:35:29540 mApplyToken = applyToken;
Vishnu Nair621102e2019-06-12 21:16:57541 return NO_ERROR;
542}
543
544status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 17:53:30545 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
546 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
547 // but is unlikely to use them again as they are owned by the other process.
548 // You may be asking yourself, is this const cast safe? Const cast is safe up
549 // until the point where you try and write to an object that was originally const at which
550 // point we enter undefined behavior. In this case we are safe though, because there are
551 // two possibilities:
552 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
553 // 2. It was originall const! In this case not only was it useless, but it by definition
554 // contains no composer states and so cacheBuffers will not perform any writes.
555
556 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
557
Vishnu Nair621102e2019-06-12 21:16:57558 parcel->writeUint32(mForceSynchronous);
559 parcel->writeUint32(mTransactionNestCount);
560 parcel->writeBool(mAnimation);
561 parcel->writeBool(mEarlyWakeup);
Ady Abrahambf1349c2020-06-12 21:26:18562 parcel->writeBool(mExplicitEarlyWakeupStart);
563 parcel->writeBool(mExplicitEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 21:16:57564 parcel->writeBool(mContainsBuffer);
565 parcel->writeInt64(mDesiredPresentTime);
Ady Abrahamf0c56492020-12-18 02:04:15566 parcel->writeBool(mIsAutoTimestamp);
Siarhei Vishniakoufc434ac2021-01-13 20:28:00567 SAFE_PARCEL(mFrameTimelineInfo.write, *parcel);
Vishnu Nair277142c2021-01-06 02:35:29568 parcel->writeStrongBinder(mApplyToken);
Vishnu Nair621102e2019-06-12 21:16:57569 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
570 for (auto const& displayState : mDisplayStates) {
571 displayState.write(*parcel);
572 }
573
Valerie Hau9dab9732019-08-20 16:29:25574 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
575 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
576 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
577 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
578 for (auto callbackId : callbackInfo.callbackIds) {
579 parcel->writeInt64(callbackId);
580 }
581 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
582 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45583 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 16:29:25584 }
585 }
586
Vishnu Nair621102e2019-06-12 21:16:57587 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Pablo Gamitodbc31672020-09-01 18:28:58588 for (auto const& [handle, composerState] : mComposerStates) {
589 SAFE_PARCEL(parcel->writeStrongBinder, handle);
Vishnu Nair621102e2019-06-12 21:16:57590 composerState.write(*parcel);
591 }
592
593 mInputWindowCommands.write(*parcel);
594 return NO_ERROR;
Mathias Agopian698c0872011-06-29 02:09:31595}
596
Robert Carr2c5f6d22017-09-26 19:30:35597SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Pablo Gamitodbc31672020-09-01 18:28:58598 for (auto const& [handle, composerState] : other.mComposerStates) {
599 if (mComposerStates.count(handle) == 0) {
600 mComposerStates[handle] = composerState;
Robert Carr2c5f6d22017-09-26 19:30:35601 } else {
Pablo Gamitodbc31672020-09-01 18:28:58602 mComposerStates[handle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 19:30:35603 }
604 }
Robert Carr2c5f6d22017-09-26 19:30:35605
606 for (auto const& state : other.mDisplayStates) {
607 ssize_t index = mDisplayStates.indexOf(state);
608 if (index < 0) {
609 mDisplayStates.add(state);
610 } else {
611 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
612 }
613 }
Robert Carr2c5f6d22017-09-26 19:30:35614
Marissa Wallc837b5e2018-10-12 17:04:44615 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
616 auto& [callbackIds, surfaceControls] = callbackInfo;
617 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
618 callbackIds.begin()),
619 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 16:29:25620
Valerie Hau236eba32020-01-04 00:53:39621 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
622 surfaceControls.end());
623
624 auto& currentProcessCallbackInfo =
625 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
626 currentProcessCallbackInfo.surfaceControls
627 .insert(std::make_move_iterator(surfaceControls.begin()),
628 std::make_move_iterator(surfaceControls.end()));
629
630 // register all surface controls for all callbackIds for this listener that is merging
631 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
632 TransactionCompletedListener::getInstance()
633 ->addSurfaceControlToCallbacks(surfaceControl,
634 currentProcessCallbackInfo.callbackIds);
635 }
Marissa Wallc837b5e2018-10-12 17:04:44636 }
Marissa Wallc837b5e2018-10-12 17:04:44637
chaviw273171b2018-12-26 19:46:30638 mInputWindowCommands.merge(other.mInputWindowCommands);
639
Robert Carrbbc85622020-04-08 17:45:12640 mContainsBuffer |= other.mContainsBuffer;
Jorim Jaggie3b57002019-07-22 15:18:52641 mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup;
Ady Abrahambf1349c2020-06-12 21:26:18642 mExplicitEarlyWakeupStart = mExplicitEarlyWakeupStart || other.mExplicitEarlyWakeupStart;
643 mExplicitEarlyWakeupEnd = mExplicitEarlyWakeupEnd || other.mExplicitEarlyWakeupEnd;
Vishnu Nair277142c2021-01-06 02:35:29644 mApplyToken = other.mApplyToken;
Ady Abraham22c7b5c2020-09-23 02:33:40645
Siarhei Vishniakoufc434ac2021-01-13 20:28:00646 mFrameTimelineInfo.merge(other.mFrameTimelineInfo);
Ady Abraham22c7b5c2020-09-23 02:33:40647
Vishnu Nairfef244e2019-06-18 01:07:51648 other.clear();
Robert Carr2c5f6d22017-09-26 19:30:35649 return *this;
650}
651
Vishnu Nairfef244e2019-06-18 01:07:51652void SurfaceComposerClient::Transaction::clear() {
653 mComposerStates.clear();
654 mDisplayStates.clear();
655 mListenerCallbacks.clear();
656 mInputWindowCommands.clear();
657 mContainsBuffer = false;
658 mForceSynchronous = 0;
659 mTransactionNestCount = 0;
660 mAnimation = false;
661 mEarlyWakeup = false;
Ady Abrahambf1349c2020-06-12 21:26:18662 mExplicitEarlyWakeupStart = false;
663 mExplicitEarlyWakeupEnd = false;
Ady Abrahamf0c56492020-12-18 02:04:15664 mDesiredPresentTime = 0;
665 mIsAutoTimestamp = true;
Siarhei Vishniakoufc434ac2021-01-13 20:28:00666 mFrameTimelineInfo.clear();
Vishnu Nair277142c2021-01-06 02:35:29667 mApplyToken = nullptr;
Vishnu Nairfef244e2019-06-18 01:07:51668}
669
Marissa Wall78b72202019-03-15 21:58:34670void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
671 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
672
Marissa Wall947d34e2019-03-29 21:03:53673 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 21:58:34674 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 21:03:53675 uncacheBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 21:58:34676
Valerie Haufa889122019-04-15 20:56:05677 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Siarhei Vishniakoufc434ac2021-01-13 20:28:00678 sf->setTransactionState(FrameTimelineInfo{}, {}, {}, 0, applyToken, {}, systemTime(), true,
679 uncacheBuffer, false, {}, 0 /* Undefined transactionId */);
Marissa Wall78b72202019-03-15 21:58:34680}
681
682void SurfaceComposerClient::Transaction::cacheBuffers() {
683 if (!mContainsBuffer) {
684 return;
685 }
686
687 size_t count = 0;
Vishnu Nairf03652d2019-07-17 00:56:56688 for (auto& [handle, cs] : mComposerStates) {
Pablo Gamitodbc31672020-09-01 18:28:58689 layer_state_t* s = &(mComposerStates[handle].state);
Marissa Wall78b72202019-03-15 21:58:34690 if (!(s->what & layer_state_t::eBufferChanged)) {
691 continue;
Robert Carr28037922020-04-08 17:57:07692 } else if (s->what & layer_state_t::eCachedBufferChanged) {
693 // If eBufferChanged and eCachedBufferChanged are both trued then that means
694 // we already cached the buffer in a previous call to cacheBuffers, perhaps
695 // from writeToParcel on a Transaction that was merged in to this one.
696 continue;
Marissa Wall78b72202019-03-15 21:58:34697 }
698
Marissa Wall00597242019-03-27 17:35:19699 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
700 // time trying to cache them.
701 if (!s->buffer) {
702 continue;
703 }
704
Marissa Wall78b72202019-03-15 21:58:34705 uint64_t cacheId = 0;
706 status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId);
707 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 22:09:33708 // Cache-hit. Strip the buffer and send only the id.
Marissa Walla141abe2019-03-27 23:28:07709 s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
Marissa Wall78b72202019-03-15 21:58:34710 s->buffer = nullptr;
711 } else {
Robert Carre06ad2b2020-04-10 22:09:33712 // Cache-miss. Include the buffer and send the new cacheId.
Marissa Wall78b72202019-03-15 21:58:34713 cacheId = BufferCache::getInstance().cache(s->buffer);
714 }
715 s->what |= layer_state_t::eCachedBufferChanged;
716 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 21:03:53717 s->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 21:58:34718
719 // If we have more buffers than the size of the cache, we should stop caching so we don't
720 // evict other buffers in this transaction
721 count++;
722 if (count >= BUFFER_CACHE_MAX_SIZE) {
723 break;
724 }
725 }
Robert Carr6fb1a7e2018-12-11 20:07:25726}
727
Robert Carr4cdc58f2017-08-23 21:22:20728status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
729 if (mStatus != NO_ERROR) {
730 return mStatus;
731 }
732
733 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
734
Valerie Hau9dab9732019-08-20 16:29:25735 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 21:03:19736 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 17:04:44737 // For every listener with registered callbacks
738 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
739 auto& [callbackIds, surfaceControls] = callbackInfo;
740 if (callbackIds.empty()) {
741 continue;
742 }
743
Valerie Hau9dab9732019-08-20 16:29:25744 if (surfaceControls.empty()) {
745 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
746 } else {
747 // If the listener has any SurfaceControls set on this Transaction update the surface
748 // state
749 for (const auto& surfaceControl : surfaceControls) {
750 layer_state_t* s = getLayerState(surfaceControl);
751 if (!s) {
752 ALOGE("failed to get layer state");
753 continue;
754 }
755 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
756 s->what |= layer_state_t::eHasListenerCallbacksChanged;
757 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 17:04:44758 }
Marissa Wallc837b5e2018-10-12 17:04:44759 }
760 }
Valerie Hau9dab9732019-08-20 16:29:25761
Marissa Wall78b72202019-03-15 21:58:34762 cacheBuffers();
763
Robert Carr4cdc58f2017-08-23 21:22:20764 Vector<ComposerState> composerStates;
765 Vector<DisplayState> displayStates;
766 uint32_t flags = 0;
767
768 mForceSynchronous |= synchronous;
769
chaviw8e3fe5d2018-02-22 18:55:42770 for (auto const& kv : mComposerStates){
771 composerStates.add(kv.second);
772 }
773
Adithya Srinivasand3efcb32020-10-21 01:08:22774 displayStates = std::move(mDisplayStates);
Robert Carr4cdc58f2017-08-23 21:22:20775
776 if (mForceSynchronous) {
777 flags |= ISurfaceComposer::eSynchronous;
778 }
779 if (mAnimation) {
780 flags |= ISurfaceComposer::eAnimation;
781 }
Dan Stoza84d619e2018-03-29 00:07:36782 if (mEarlyWakeup) {
783 flags |= ISurfaceComposer::eEarlyWakeup;
784 }
Robert Carr4cdc58f2017-08-23 21:22:20785
Ady Abrahambf1349c2020-06-12 21:26:18786 // If both mExplicitEarlyWakeupStart and mExplicitEarlyWakeupEnd are set
787 // it is equivalent for none
788 if (mExplicitEarlyWakeupStart && !mExplicitEarlyWakeupEnd) {
789 flags |= ISurfaceComposer::eExplicitEarlyWakeupStart;
790 }
791 if (mExplicitEarlyWakeupEnd && !mExplicitEarlyWakeupStart) {
792 flags |= ISurfaceComposer::eExplicitEarlyWakeupEnd;
793 }
794
Vishnu Nair277142c2021-01-06 02:35:29795 sp<IBinder> applyToken = mApplyToken
796 ? mApplyToken
797 : IInterface::asBinder(TransactionCompletedListener::getIInstance());
798
Siarhei Vishniakoufc434ac2021-01-13 20:28:00799 sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
Ady Abrahamf0c56492020-12-18 02:04:15800 mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
Marissa Wall3dad52d2019-03-22 21:03:19801 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
Adithya Srinivasand3efcb32020-10-21 01:08:22802 hasListenerCallbacks, listenerCallbacks, mId);
803 mId = generateId();
804
805 // Clear the current states and flags
806 clear();
807
Robert Carr4cdc58f2017-08-23 21:22:20808 mStatus = NO_ERROR;
809 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 23:29:12810}
811
The Android Open Source Projectedbf3b62009-03-04 03:31:44812// ---------------------------------------------------------------------------
813
Robert Carr4cdc58f2017-08-23 21:22:20814sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-20 01:19:11815 return ComposerService::getComposerService()->createDisplay(displayName,
816 secure);
Mathias Agopiane57f2922012-08-09 23:29:12817}
818
Robert Carr4cdc58f2017-08-23 21:22:20819void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 19:15:49820 return ComposerService::getComposerService()->destroyDisplay(display);
821}
822
Dominik Laskowskidcb38bb2019-01-25 10:35:50823std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
824 return ComposerService::getComposerService()->getPhysicalDisplayIds();
825}
826
827std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
828 return ComposerService::getComposerService()->getInternalDisplayId();
829}
830
831sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
832 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
833}
834
835sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
836 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-25 03:00:51837}
838
Robert Carr4cdc58f2017-08-23 21:22:20839void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-16 01:24:43840 mAnimation = true;
841}
842
Dan Stoza84d619e2018-03-29 00:07:36843void SurfaceComposerClient::Transaction::setEarlyWakeup() {
844 mEarlyWakeup = true;
845}
846
Ady Abrahambf1349c2020-06-12 21:26:18847void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupStart() {
848 mExplicitEarlyWakeupStart = true;
849}
850
851void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupEnd() {
852 mExplicitEarlyWakeupEnd = true;
853}
854
Pablo Gamitodbc31672020-09-01 18:28:58855layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
856 auto handle = sc->getHandle();
857
Vishnu Nairf03652d2019-07-17 00:56:56858 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-29 02:09:31859 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 18:55:42860 ComposerState s;
Pablo Gamitodbc31672020-09-01 18:28:58861
Vishnu Nairf03652d2019-07-17 00:56:56862 s.state.surface = handle;
Pablo Gamitodbc31672020-09-01 18:28:58863 s.state.layerId = sc->getLayerId();
864
Vishnu Nairf03652d2019-07-17 00:56:56865 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-29 02:09:31866 }
867
Vishnu Nairf03652d2019-07-17 00:56:56868 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-29 02:09:31869}
870
Marissa Wallc837b5e2018-10-12 17:04:44871void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
872 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-19 00:04:36873 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
874 callbackInfo.surfaceControls.insert(sc);
875
876 TransactionCompletedListener::getInstance()
877 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 17:04:44878}
879
Robert Carr4cdc58f2017-08-23 21:22:20880SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
881 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-23 00:04:57882 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20883 if (!s) {
884 mStatus = BAD_INDEX;
885 return *this;
886 }
Mathias Agopian3165cc22012-08-09 02:42:09887 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-29 02:09:31888 s->x = x;
889 s->y = y;
Marissa Wallc837b5e2018-10-12 17:04:44890
891 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20892 return *this;
Mathias Agopian698c0872011-06-29 02:09:31893}
894
Robert Carr4cdc58f2017-08-23 21:22:20895SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
896 const sp<SurfaceControl>& sc) {
897 return setFlags(sc, 0, layer_state_t::eLayerHidden);
898}
899
900SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
901 const sp<SurfaceControl>& sc) {
902 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
903}
904
905SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
906 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-23 00:04:57907 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20908 if (!s) {
909 mStatus = BAD_INDEX;
910 return *this;
911 }
Mathias Agopian3165cc22012-08-09 02:42:09912 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-29 02:09:31913 s->w = w;
914 s->h = h;
Jamie Gennis28378392011-10-13 00:39:00915
Marissa Wallc837b5e2018-10-12 17:04:44916 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20917 return *this;
Mathias Agopian698c0872011-06-29 02:09:31918}
919
Robert Carr4cdc58f2017-08-23 21:22:20920SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
921 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-23 00:04:57922 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20923 if (!s) {
924 mStatus = BAD_INDEX;
925 return *this;
926 }
Mathias Agopian3165cc22012-08-09 02:42:09927 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 18:15:19928 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-29 02:09:31929 s->z = z;
Marissa Wallc837b5e2018-10-12 17:04:44930
931 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20932 return *this;
Mathias Agopian698c0872011-06-29 02:09:31933}
934
Pablo Gamito11dcc222020-09-12 15:49:39935SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
936 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-23 00:04:57937 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 23:55:57938 if (!s) {
Robert Carr4cdc58f2017-08-23 21:22:20939 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 20:12:49940 return *this;
Robert Carrdb66e622017-04-10 23:55:57941 }
942 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 18:15:19943 s->what &= ~layer_state_t::eLayerChanged;
Pablo Gamito11dcc222020-09-12 15:49:39944 s->relativeLayerSurfaceControl = relativeTo;
Robert Carrdb66e622017-04-10 23:55:57945 s->z = z;
Marissa Wallc837b5e2018-10-12 17:04:44946
947 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20948 return *this;
Robert Carrdb66e622017-04-10 23:55:57949}
950
Robert Carr4cdc58f2017-08-23 21:22:20951SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
952 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-29 02:09:31953 uint32_t mask) {
chaviw763ef572018-02-23 00:04:57954 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20955 if (!s) {
956 mStatus = BAD_INDEX;
957 return *this;
958 }
chaviwc5676c62020-09-18 22:01:04959 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
Vishnu Nairf6eddb62021-01-28 06:02:11960 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot) ||
961 (mask & layer_state_t::eEnableBackpressure)) {
Dan Stoza23116082015-06-18 21:58:39962 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-30 01:17:11963 }
Mathias Agopian698c0872011-06-29 02:09:31964 s->flags &= ~mask;
965 s->flags |= (flags & mask);
966 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 17:04:44967
968 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20969 return *this;
Mathias Agopian698c0872011-06-29 02:09:31970}
971
Robert Carr4cdc58f2017-08-23 21:22:20972SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
973 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-29 02:09:31974 const Region& transparentRegion) {
chaviw763ef572018-02-23 00:04:57975 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20976 if (!s) {
977 mStatus = BAD_INDEX;
978 return *this;
979 }
Mathias Agopian3165cc22012-08-09 02:42:09980 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-29 02:09:31981 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 17:04:44982
983 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20984 return *this;
Mathias Agopian698c0872011-06-29 02:09:31985}
986
Robert Carr4cdc58f2017-08-23 21:22:20987SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
988 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-23 00:04:57989 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:20990 if (!s) {
991 mStatus = BAD_INDEX;
992 return *this;
993 }
Mathias Agopian3165cc22012-08-09 02:42:09994 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-29 02:09:31995 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 17:04:44996
997 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:20998 return *this;
Mathias Agopian698c0872011-06-29 02:09:31999}
1000
Robert Carr4cdc58f2017-08-23 21:22:201001SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
1002 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-23 00:04:571003 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:201004 if (!s) {
1005 mStatus = BAD_INDEX;
1006 return *this;
1007 }
Mathias Agopian3165cc22012-08-09 02:42:091008 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-25 04:41:091009 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 17:04:441010
1011 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201012 return *this;
Mathias Agopian87855782012-07-25 04:41:091013}
1014
Evan Rosky1f6d6d52018-12-06 18:47:261015SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 23:44:211016 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 18:47:261017 layer_state_t* s = getLayerState(sc);
1018 if (!s) {
1019 mStatus = BAD_INDEX;
1020 return *this;
1021 }
1022 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 23:44:211023
1024 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 18:47:261025
1026 registerSurfaceControlForCallback(sc);
1027 return *this;
1028}
1029
Robert Carr4cdc58f2017-08-23 21:22:201030SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
1031 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-22 03:48:261032 float dtdy, float dsdy) {
chaviw763ef572018-02-23 00:04:571033 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:201034 if (!s) {
1035 mStatus = BAD_INDEX;
1036 return *this;
1037 }
Mathias Agopian3165cc22012-08-09 02:42:091038 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-29 02:09:311039 layer_state_t::matrix22_t matrix;
1040 matrix.dsdx = dsdx;
1041 matrix.dtdx = dtdx;
1042 matrix.dsdy = dsdy;
1043 matrix.dtdy = dtdy;
1044 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 17:04:441045
1046 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201047 return *this;
Mathias Agopian698c0872011-06-29 02:09:311048}
1049
Marissa Wallf58c14b2018-07-24 17:50:431050SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 21:22:201051 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-23 00:04:571052 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 21:22:201053 if (!s) {
1054 mStatus = BAD_INDEX;
1055 return *this;
1056 }
Marissa Wallf58c14b2018-07-24 17:50:431057 s->what |= layer_state_t::eCropChanged_legacy;
1058 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 17:04:441059
1060 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201061 return *this;
Jamie Gennisf15a83f2012-05-11 03:43:551062}
1063
Lucas Dupin1b6531c2018-07-06 00:18:211064SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
1065 const sp<SurfaceControl>& sc, float cornerRadius) {
1066 layer_state_t* s = getLayerState(sc);
1067 if (!s) {
1068 mStatus = BAD_INDEX;
1069 return *this;
1070 }
1071 s->what |= layer_state_t::eCornerRadiusChanged;
1072 s->cornerRadius = cornerRadius;
1073 return *this;
1074}
1075
Lucas Dupin19c8f0e2019-11-26 01:55:441076SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1077 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1078 layer_state_t* s = getLayerState(sc);
1079 if (!s) {
1080 mStatus = BAD_INDEX;
1081 return *this;
1082 }
1083 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1084 s->backgroundBlurRadius = backgroundBlurRadius;
1085 return *this;
1086}
1087
Lucas Dupinc3800b82020-10-02 23:24:481088SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBlurRegions(
1089 const sp<SurfaceControl>& sc, const std::vector<BlurRegion>& blurRegions) {
1090 layer_state_t* s = getLayerState(sc);
1091 if (!s) {
1092 mStatus = BAD_INDEX;
1093 return *this;
1094 }
1095 s->what |= layer_state_t::eBlurRegionsChanged;
1096 s->blurRegions = blurRegions;
1097 return *this;
1098}
1099
Marissa Wallf58c14b2018-07-24 17:50:431100SurfaceComposerClient::Transaction&
Pablo Gamito11dcc222020-09-12 15:49:391101SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(
1102 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& barrierSurfaceControl,
1103 uint64_t frameNumber) {
chaviw763ef572018-02-23 00:04:571104 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 16:51:441105 if (!s) {
Robert Carr4cdc58f2017-08-23 21:22:201106 mStatus = BAD_INDEX;
1107 return *this;
Dan Stoza7dde5992015-05-22 16:51:441108 }
Marissa Wallf58c14b2018-07-24 17:50:431109 s->what |= layer_state_t::eDeferTransaction_legacy;
Pablo Gamito11dcc222020-09-12 15:49:391110 s->barrierSurfaceControl_legacy = barrierSurfaceControl;
Vishnu Nair6b7c5c92020-09-30 00:27:051111 s->barrierFrameNumber = frameNumber;
Marissa Wallc837b5e2018-10-12 17:04:441112
1113 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201114 return *this;
Robert Carr0d480722017-01-11 00:42:541115}
1116
Robert Carr4cdc58f2017-08-23 21:22:201117SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
Pablo Gamito11dcc222020-09-12 15:49:391118 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-23 00:04:571119 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 20:58:511120 if (!s) {
Robert Carr4cdc58f2017-08-23 21:22:201121 mStatus = BAD_INDEX;
1122 return *this;
Robert Carr1db73f62016-12-21 20:58:511123 }
1124 s->what |= layer_state_t::eReparentChildren;
Pablo Gamito11dcc222020-09-12 15:49:391125 s->reparentSurfaceControl = newParent;
Marissa Wallc837b5e2018-10-12 17:04:441126
1127 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201128 return *this;
Robert Carr1db73f62016-12-21 20:58:511129}
1130
Robert Carr4cdc58f2017-08-23 21:22:201131SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:391132 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-23 00:04:571133 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 17:25:591134 if (!s) {
Robert Carr4cdc58f2017-08-23 21:22:201135 mStatus = BAD_INDEX;
1136 return *this;
chaviw06178942017-07-27 17:25:591137 }
chaviwf1961f72017-09-18 23:41:071138 s->what |= layer_state_t::eReparent;
Pablo Gamito11dcc222020-09-12 15:49:391139 s->parentSurfaceControlForChild = newParent;
Marissa Wallc837b5e2018-10-12 17:04:441140
1141 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201142 return *this;
chaviw06178942017-07-27 17:25:591143}
1144
Robert Carr4cdc58f2017-08-23 21:22:201145SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1146 const sp<SurfaceControl>& sc,
1147 const half3& color) {
chaviw763ef572018-02-23 00:04:571148 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 19:32:321149 if (!s) {
Robert Carr4cdc58f2017-08-23 21:22:201150 mStatus = BAD_INDEX;
1151 return *this;
1152 }
1153 s->what |= layer_state_t::eColorChanged;
1154 s->color = color;
Marissa Wallc837b5e2018-10-12 17:04:441155
1156 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 21:22:201157 return *this;
1158}
1159
Valerie Haudd0b7572019-01-29 22:59:271160SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1161 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-12 04:03:141162 layer_state_t* s = getLayerState(sc);
1163 if (!s) {
1164 mStatus = BAD_INDEX;
1165 return *this;
1166 }
1167
Valerie Haudd0b7572019-01-29 22:59:271168 s->what |= layer_state_t::eBackgroundColorChanged;
1169 s->color = color;
1170 s->bgColorAlpha = alpha;
1171 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-12 04:03:141172
1173 registerSurfaceControlForCallback(sc);
1174 return *this;
1175}
1176
Marissa Wall61c58622018-07-18 17:12:201177SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1178 const sp<SurfaceControl>& sc, uint32_t transform) {
1179 layer_state_t* s = getLayerState(sc);
1180 if (!s) {
1181 mStatus = BAD_INDEX;
1182 return *this;
1183 }
1184 s->what |= layer_state_t::eTransformChanged;
1185 s->transform = transform;
Marissa Wallc837b5e2018-10-12 17:04:441186
1187 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201188 return *this;
1189}
1190
1191SurfaceComposerClient::Transaction&
1192SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1193 bool transformToDisplayInverse) {
1194 layer_state_t* s = getLayerState(sc);
1195 if (!s) {
1196 mStatus = BAD_INDEX;
1197 return *this;
1198 }
1199 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1200 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 17:04:441201
1202 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201203 return *this;
1204}
1205
1206SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
1207 const sp<SurfaceControl>& sc, const Rect& crop) {
1208 layer_state_t* s = getLayerState(sc);
1209 if (!s) {
1210 mStatus = BAD_INDEX;
1211 return *this;
1212 }
1213 s->what |= layer_state_t::eCropChanged;
1214 s->crop = crop;
Marissa Wallc837b5e2018-10-12 17:04:441215
1216 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201217 return *this;
1218}
1219
Marissa Wall861616d2018-10-22 19:52:231220SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
1221 const sp<SurfaceControl>& sc, const Rect& frame) {
1222 layer_state_t* s = getLayerState(sc);
1223 if (!s) {
1224 mStatus = BAD_INDEX;
1225 return *this;
1226 }
1227 s->what |= layer_state_t::eFrameChanged;
Marin Shalamanov6ad317c2020-07-29 21:34:071228 s->orientedDisplaySpaceRect = frame;
Marissa Wall861616d2018-10-22 19:52:231229
1230 registerSurfaceControlForCallback(sc);
1231 return *this;
1232}
1233
Marissa Wall61c58622018-07-18 17:12:201234SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
1235 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
1236 layer_state_t* s = getLayerState(sc);
1237 if (!s) {
1238 mStatus = BAD_INDEX;
1239 return *this;
1240 }
Marissa Wall78b72202019-03-15 21:58:341241 s->what |= layer_state_t::eBufferChanged;
1242 s->buffer = buffer;
Ady Abrahamf0c56492020-12-18 02:04:151243 if (mIsAutoTimestamp) {
1244 mDesiredPresentTime = systemTime();
1245 }
Marissa Wallebc2c052019-01-17 03:16:551246
1247 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 21:58:341248
1249 mContainsBuffer = true;
Marissa Wallebc2c052019-01-17 03:16:551250 return *this;
1251}
1252
Marissa Wall61c58622018-07-18 17:12:201253SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
1254 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
1255 layer_state_t* s = getLayerState(sc);
1256 if (!s) {
1257 mStatus = BAD_INDEX;
1258 return *this;
1259 }
1260 s->what |= layer_state_t::eAcquireFenceChanged;
1261 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 17:04:441262
1263 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201264 return *this;
1265}
1266
1267SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1268 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1269 layer_state_t* s = getLayerState(sc);
1270 if (!s) {
1271 mStatus = BAD_INDEX;
1272 return *this;
1273 }
1274 s->what |= layer_state_t::eDataspaceChanged;
1275 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 17:04:441276
1277 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201278 return *this;
1279}
1280
1281SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1282 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1283 layer_state_t* s = getLayerState(sc);
1284 if (!s) {
1285 mStatus = BAD_INDEX;
1286 return *this;
1287 }
1288 s->what |= layer_state_t::eHdrMetadataChanged;
1289 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 17:04:441290
1291 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201292 return *this;
1293}
1294
1295SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1296 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1297 layer_state_t* s = getLayerState(sc);
1298 if (!s) {
1299 mStatus = BAD_INDEX;
1300 return *this;
1301 }
1302 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1303 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 17:04:441304
1305 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201306 return *this;
1307}
1308
1309SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1310 const sp<SurfaceControl>& sc, int32_t api) {
1311 layer_state_t* s = getLayerState(sc);
1312 if (!s) {
1313 mStatus = BAD_INDEX;
1314 return *this;
1315 }
1316 s->what |= layer_state_t::eApiChanged;
1317 s->api = api;
Marissa Wallc837b5e2018-10-12 17:04:441318
1319 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 17:12:201320 return *this;
1321}
1322
1323SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1324 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1325 layer_state_t* s = getLayerState(sc);
1326 if (!s) {
1327 mStatus = BAD_INDEX;
1328 return *this;
1329 }
1330 s->what |= layer_state_t::eSidebandStreamChanged;
1331 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 17:04:441332
1333 registerSurfaceControlForCallback(sc);
1334 return *this;
1335}
1336
Marissa Wall17b4e452018-12-27 00:32:341337SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1338 nsecs_t desiredPresentTime) {
1339 mDesiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-18 02:04:151340 mIsAutoTimestamp = false;
Marissa Wall17b4e452018-12-27 00:32:341341 return *this;
1342}
1343
Peiyong Linc502cb72019-03-01 23:00:231344SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1345 const sp<SurfaceControl>& sc, const bool agnostic) {
1346 layer_state_t* s = getLayerState(sc);
1347 if (!s) {
1348 mStatus = BAD_INDEX;
1349 return *this;
1350 }
1351 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1352 s->colorSpaceAgnostic = agnostic;
1353
1354 registerSurfaceControlForCallback(sc);
1355 return *this;
1356}
1357
Marissa Wallc837b5e2018-10-12 17:04:441358SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 22:10:291359SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1360 int32_t priority) {
1361 layer_state_t* s = getLayerState(sc);
1362 if (!s) {
1363 mStatus = BAD_INDEX;
1364 return *this;
1365 }
1366
1367 s->what |= layer_state_t::eFrameRateSelectionPriority;
1368 s->frameRateSelectionPriority = priority;
1369
1370 registerSurfaceControlForCallback(sc);
1371 return *this;
1372}
1373
1374SurfaceComposerClient::Transaction&
Marissa Wallc837b5e2018-10-12 17:04:441375SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 18:33:521376 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 17:04:441377 auto listener = TransactionCompletedListener::getInstance();
1378
Marissa Wall80d94ad2019-01-19 00:04:361379 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1380 std::placeholders::_2, std::placeholders::_3);
1381 const auto& surfaceControls =
1382 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 17:04:441383
Marissa Wall80d94ad2019-01-19 00:04:361384 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 17:04:441385
1386 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1387 callbackId);
Marissa Wall61c58622018-07-18 17:12:201388 return *this;
1389}
1390
Valerie Hau871d6352020-01-29 16:44:021391SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1392 const sp<SurfaceControl>& sc) {
1393 layer_state_t* s = getLayerState(sc);
1394 if (!s) {
1395 mStatus = BAD_INDEX;
1396 return *this;
1397 }
1398
1399 s->what |= layer_state_t::eProducerDisconnect;
1400 return *this;
1401}
1402
Vishnu Nair6b7c5c92020-09-30 00:27:051403SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameNumber(
1404 const sp<SurfaceControl>& sc, uint64_t frameNumber) {
1405 layer_state_t* s = getLayerState(sc);
1406 if (!s) {
1407 mStatus = BAD_INDEX;
1408 return *this;
1409 }
1410
1411 s->what |= layer_state_t::eFrameNumberChanged;
1412 s->frameNumber = frameNumber;
1413
1414 return *this;
1415}
1416
Robert Carr2c358bf2018-08-08 22:58:151417#ifndef NO_INPUT
1418SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1419 const sp<SurfaceControl>& sc,
1420 const InputWindowInfo& info) {
1421 layer_state_t* s = getLayerState(sc);
1422 if (!s) {
1423 mStatus = BAD_INDEX;
1424 return *this;
1425 }
Chris Ye0783e992020-06-03 04:34:491426 s->inputHandle = new InputWindowHandle(info);
Robert Carr2c358bf2018-08-08 22:58:151427 s->what |= layer_state_t::eInputInfoChanged;
1428 return *this;
1429}
chaviw273171b2018-12-26 19:46:301430
Vishnu Naire798b472020-07-23 20:52:211431SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Naire798b472020-07-23 20:52:211432 const FocusRequest& request) {
1433 mInputWindowCommands.focusRequests.push_back(request);
1434 return *this;
1435}
1436
chaviwa911b102019-02-14 18:18:331437SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1438 mInputWindowCommands.syncInputWindows = true;
1439 return *this;
1440}
1441
Robert Carr2c358bf2018-08-08 22:58:151442#endif
1443
Peiyong Lind3788632018-09-18 23:01:311444SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1445 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1446 layer_state_t* s = getLayerState(sc);
1447 if (!s) {
1448 mStatus = BAD_INDEX;
1449 return *this;
1450 }
1451 s->what |= layer_state_t::eColorTransformChanged;
1452 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 17:04:441453
1454 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 23:01:311455 return *this;
1456}
1457
Robert Carrfb4d58b2019-01-15 17:21:271458SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1459 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1460 setCrop_legacy(sc, source);
1461
1462 int x = dst.left;
1463 int y = dst.top;
Robert Carr66365e42019-04-08 23:58:041464
1465 float sourceWidth = source.getWidth();
1466 float sourceHeight = source.getHeight();
1467
1468 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1469 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 17:21:271470 float matrix[4] = {1, 0, 0, 1};
1471
1472 switch (transform) {
1473 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1474 matrix[0] = -xScale; matrix[1] = 0;
1475 matrix[2] = 0; matrix[3] = yScale;
1476 x += source.getWidth();
1477 break;
1478 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1479 matrix[0] = xScale; matrix[1] = 0;
1480 matrix[2] = 0; matrix[3] = -yScale;
1481 y += source.getHeight();
1482 break;
1483 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1484 matrix[0] = 0; matrix[1] = -yScale;
1485 matrix[2] = xScale; matrix[3] = 0;
1486 x += source.getHeight();
1487 break;
1488 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1489 matrix[0] = -xScale; matrix[1] = 0;
1490 matrix[2] = 0; matrix[3] = -yScale;
1491 x += source.getWidth();
1492 y += source.getHeight();
1493 break;
1494 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1495 matrix[0] = 0; matrix[1] = yScale;
1496 matrix[2] = -xScale; matrix[3] = 0;
1497 y += source.getWidth();
1498 break;
1499 default:
1500 matrix[0] = xScale; matrix[1] = 0;
1501 matrix[2] = 0; matrix[3] = yScale;
1502 break;
1503 }
1504 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 17:15:511505 float offsetX = xScale * source.left;
1506 float offsetY = yScale * source.top;
1507 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 17:21:271508
1509 return *this;
1510}
1511
Vishnu Nairc97b8db2019-10-30 01:19:351512SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1513 const sp<SurfaceControl>& sc, float shadowRadius) {
1514 layer_state_t* s = getLayerState(sc);
1515 if (!s) {
1516 mStatus = BAD_INDEX;
1517 return *this;
1518 }
1519 s->what |= layer_state_t::eShadowRadiusChanged;
1520 s->shadowRadius = shadowRadius;
1521 return *this;
1522}
1523
Steven Thomas3172e202020-01-07 03:25:301524SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Marin Shalamanov46084422020-10-13 10:33:421525 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility,
1526 bool shouldBeSeamless) {
Steven Thomas3172e202020-01-07 03:25:301527 layer_state_t* s = getLayerState(sc);
1528 if (!s) {
1529 mStatus = BAD_INDEX;
1530 return *this;
1531 }
Ady Abrahamdd5bfa92021-01-08 01:56:081532 // Allow privileged values as well here, those will be ignored by SF if
1533 // the caller is not privileged
1534 if (!ValidateFrameRate(frameRate, compatibility, "Transaction::setFrameRate",
1535 /*privileged=*/true)) {
Steven Thomas62a4cf82020-01-31 20:04:031536 mStatus = BAD_VALUE;
1537 return *this;
1538 }
Steven Thomas3172e202020-01-07 03:25:301539 s->what |= layer_state_t::eFrameRateChanged;
1540 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 20:04:031541 s->frameRateCompatibility = compatibility;
Marin Shalamanov46084422020-10-13 10:33:421542 s->shouldBeSeamless = shouldBeSeamless;
Steven Thomas3172e202020-01-07 03:25:301543 return *this;
1544}
1545
Vishnu Nair6213bd92020-05-09 00:42:251546SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
1547 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
1548 layer_state_t* s = getLayerState(sc);
1549 if (!s) {
1550 mStatus = BAD_INDEX;
1551 return *this;
1552 }
1553
1554 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
1555 ? ui::Transform::ROT_INVALID
1556 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
1557 s->what |= layer_state_t::eFixedTransformHintChanged;
1558 s->fixedTransformHint = transform;
1559 return *this;
1560}
1561
Siarhei Vishniakoufc434ac2021-01-13 20:28:001562SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
1563 const FrameTimelineInfo& frameTimelineInfo) {
1564 mFrameTimelineInfo = frameTimelineInfo;
Ady Abraham22c7b5c2020-09-23 02:33:401565 return *this;
1566}
1567
Siarhei Vishniakoufc434ac2021-01-13 20:28:001568SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineInfo(
1569 const sp<SurfaceControl>& sc, const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 19:00:231570 layer_state_t* s = getLayerState(sc);
1571 if (!s) {
1572 mStatus = BAD_INDEX;
1573 return *this;
1574 }
1575
Siarhei Vishniakoufc434ac2021-01-13 20:28:001576 s->what |= layer_state_t::eFrameTimelineInfoChanged;
1577 s->frameTimelineInfo = frameTimelineInfo;
Robert Carr9b611b72020-10-19 19:00:231578 return *this;
1579}
1580
Vishnu Naircf26a0a2020-11-13 20:56:201581SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAutoRefresh(
1582 const sp<SurfaceControl>& sc, bool autoRefresh) {
1583 layer_state_t* s = getLayerState(sc);
1584 if (!s) {
1585 mStatus = BAD_INDEX;
1586 return *this;
1587 }
1588
1589 s->what |= layer_state_t::eAutoRefreshChanged;
1590 s->autoRefresh = autoRefresh;
1591 return *this;
1592}
1593
Vishnu Nair277142c2021-01-06 02:35:291594SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApplyToken(
1595 const sp<IBinder>& applyToken) {
1596 mApplyToken = applyToken;
1597 return *this;
1598}
1599
John Reckcdb4ed72021-02-04 18:39:331600SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setStretchEffect(
1601 const sp<SurfaceControl>& sc, float left, float top, float right, float bottom, float vecX,
1602 float vecY, float maxAmount) {
1603 layer_state_t* s = getLayerState(sc);
1604 if (!s) {
1605 mStatus = BAD_INDEX;
1606 return *this;
1607 }
1608
1609 s->what |= layer_state_t::eStretchChanged;
1610 s->stretchEffect = StretchEffect{.area = {left, top, right, bottom},
1611 .vectorX = vecX,
1612 .vectorY = vecY,
1613 .maxAmount = maxAmount};
1614 return *this;
1615}
1616
Mathias Agopian698c0872011-06-29 02:09:311617// ---------------------------------------------------------------------------
1618
chaviw763ef572018-02-23 00:04:571619DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 23:29:121620 DisplayState s;
1621 s.token = token;
1622 ssize_t index = mDisplayStates.indexOf(s);
1623 if (index < 0) {
1624 // we don't have it, add an initialized layer_state to our list
1625 s.what = 0;
1626 index = mDisplayStates.add(s);
1627 }
Dan Stozad723bd72014-11-18 18:24:031628 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 23:29:121629}
1630
Robert Carr4cdc58f2017-08-23 21:22:201631status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1632 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 18:21:211633 if (bufferProducer.get() != nullptr) {
1634 // Make sure that composition can never be stalled by a virtual display
1635 // consumer that isn't processing buffers fast enough.
1636 status_t err = bufferProducer->setAsyncMode(true);
1637 if (err != NO_ERROR) {
1638 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1639 "BufferQueue. This BufferQueue cannot be used for virtual "
1640 "display. (%d)", err);
1641 return err;
1642 }
Pablo Ceballos1aad24c2016-08-04 17:24:221643 }
chaviw763ef572018-02-23 00:04:571644 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 17:49:451645 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 23:29:121646 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 17:24:221647 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 23:29:121648}
1649
Robert Carr4cdc58f2017-08-23 21:22:201650void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 23:29:121651 uint32_t layerStack) {
chaviw763ef572018-02-23 00:04:571652 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 23:29:121653 s.layerStack = layerStack;
1654 s.what |= DisplayState::eLayerStackChanged;
1655}
1656
Robert Carr4cdc58f2017-08-23 21:22:201657void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-10 04:01:351658 ui::Rotation orientation,
1659 const Rect& layerStackRect,
1660 const Rect& displayRect) {
chaviw763ef572018-02-23 00:04:571661 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 23:29:121662 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 21:34:071663 s.layerStackSpaceRect = layerStackRect;
1664 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-05 02:30:461665 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:351666 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 23:29:121667}
1668
Robert Carr4cdc58f2017-08-23 21:22:201669void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-23 00:04:571670 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 23:01:021671 s.width = width;
1672 s.height = height;
1673 s.what |= DisplayState::eDisplaySizeChanged;
1674}
1675
Mathias Agopiane57f2922012-08-09 23:29:121676// ---------------------------------------------------------------------------
1677
The Android Open Source Projectedbf3b62009-03-04 03:31:441678SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 21:22:201679 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-04 03:31:441680{
The Android Open Source Projectedbf3b62009-03-04 03:31:441681}
1682
Jorim Jaggif3cf4bc2017-11-30 13:19:231683SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1684 : mStatus(NO_ERROR), mClient(client)
1685{
1686}
1687
Mathias Agopian698c0872011-06-29 02:09:311688void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 21:22:201689 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 23:34:591690 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 20:58:511691 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 21:01:141692 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 23:34:591693 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-28 02:41:151694 mClient = conn;
Mathias Agopiand4784a32010-05-28 02:41:151695 mStatus = NO_ERROR;
1696 }
1697 }
The Android Open Source Projectedbf3b62009-03-04 03:31:441698}
1699
Mathias Agopian698c0872011-06-29 02:09:311700SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-26 00:51:341701 dispose();
1702}
Mathias Agopiandd3423c2009-09-23 22:44:051703
Mathias Agopian698c0872011-06-29 02:09:311704status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-04 03:31:441705 return mStatus;
1706}
1707
Mathias Agopian698c0872011-06-29 02:09:311708sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 16:01:011709 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-04 03:31:441710}
1711
Mathias Agopiand4784a32010-05-28 02:41:151712status_t SurfaceComposerClient::linkToComposerDeath(
1713 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-29 02:09:311714 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 21:22:201715 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1716 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-04 03:31:441717}
1718
Mathias Agopian698c0872011-06-29 02:09:311719void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-04 03:31:441720 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 21:22:231721 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-28 02:41:151722 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 23:34:591723 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-28 02:41:151724 client = mClient; // hold ref while lock is held
1725 mClient.clear();
The Android Open Source Projectedbf3b62009-03-04 03:31:441726 }
Mathias Agopiand4784a32010-05-28 02:41:151727 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-04 03:31:441728}
1729
Evan Rosky1f6d6d52018-12-06 18:47:261730sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1731 PixelFormat format, uint32_t flags,
Vishnu Nair992496b2020-10-23 00:27:211732 const sp<IBinder>& parentHandle,
Valerie Hau1acd6962019-10-28 23:35:481733 LayerMetadata metadata,
1734 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 20:49:411735 sp<SurfaceControl> s;
Vishnu Nair992496b2020-10-23 00:27:211736 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 23:35:481737 outTransformHint);
Robert Carr3b382ed2018-03-14 20:49:411738 return s;
1739}
1740
Marissa Wall35187b32019-01-08 18:08:521741sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1742 uint32_t h, PixelFormat format,
1743 uint32_t flags, Surface* parent,
Valerie Hau1acd6962019-10-28 23:35:481744 LayerMetadata metadata,
1745 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 18:08:521746 sp<SurfaceControl> sur;
1747 status_t err = mStatus;
1748
1749 if (mStatus == NO_ERROR) {
1750 sp<IBinder> handle;
1751 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1752 sp<IGraphicBufferProducer> gbp;
1753
Valerie Hau1acd6962019-10-28 23:35:481754 uint32_t transformHint = 0;
Pablo Gamito2ec1f7b2020-09-01 14:18:491755 int32_t id = -1;
Evan Rosky1f6d6d52018-12-06 18:47:261756 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
Pablo Gamito2ec1f7b2020-09-01 14:18:491757 std::move(metadata), &handle, &gbp, &id,
1758 &transformHint);
Valerie Hau1acd6962019-10-28 23:35:481759 if (outTransformHint) {
1760 *outTransformHint = transformHint;
1761 }
Marissa Wall35187b32019-01-08 18:08:521762 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1763 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:581764 return new SurfaceControl(this, handle, gbp, id, transformHint);
Marissa Wall35187b32019-01-08 18:08:521765 }
1766 }
1767 return nullptr;
1768}
1769
Evan Rosky1f6d6d52018-12-06 18:47:261770status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1771 PixelFormat format,
1772 sp<SurfaceControl>* outSurface, uint32_t flags,
Vishnu Nair992496b2020-10-23 00:27:211773 const sp<IBinder>& parentHandle,
1774 LayerMetadata metadata,
Valerie Hau1acd6962019-10-28 23:35:481775 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-13 00:11:481776 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 19:59:181777 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 20:49:411778
Mathias Agopian698c0872011-06-29 02:09:311779 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-13 00:11:481780 sp<IBinder> handle;
1781 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 23:27:391782
Valerie Hau1acd6962019-10-28 23:35:481783 uint32_t transformHint = 0;
Pablo Gamito2ec1f7b2020-09-01 14:18:491784 int32_t id = -1;
Evan Rosky1f6d6d52018-12-06 18:47:261785 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
Pablo Gamito2ec1f7b2020-09-01 14:18:491786 &handle, &gbp, &id, &transformHint);
1787
Valerie Hau1acd6962019-10-28 23:35:481788 if (outTransformHint) {
1789 *outTransformHint = transformHint;
1790 }
Mathias Agopian4d9b8222013-03-13 00:11:481791 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1792 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:581793 *outSurface = new SurfaceControl(this, handle, gbp, id, transformHint);
Mathias Agopian698c0872011-06-29 02:09:311794 }
1795 }
Robert Carr3b382ed2018-03-14 20:49:411796 return err;
Mathias Agopian698c0872011-06-29 02:09:311797}
1798
chaviwfe94a222019-08-21 20:52:591799sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
1800 if (mirrorFromSurface == nullptr) {
1801 return nullptr;
1802 }
1803
1804 sp<IBinder> handle;
1805 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Pablo Gamito2ec1f7b2020-09-01 14:18:491806 int32_t layer_id = -1;
1807 status_t err = mClient->mirrorSurface(mirrorFromHandle, &handle, &layer_id);
chaviwfe94a222019-08-21 20:52:591808 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:581809 return new SurfaceControl(this, handle, nullptr, layer_id, true /* owned */);
chaviwfe94a222019-08-21 20:52:591810 }
1811 return nullptr;
1812}
1813
Svetoslavd85084b2014-03-20 17:28:311814status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1815 if (mStatus != NO_ERROR) {
1816 return mStatus;
1817 }
1818 return mClient->clearLayerFrameStats(token);
1819}
1820
1821status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1822 FrameStats* outStats) const {
1823 if (mStatus != NO_ERROR) {
1824 return mStatus;
1825 }
1826 return mClient->getLayerFrameStats(token, outStats);
1827}
1828
Mathias Agopian698c0872011-06-29 02:09:311829// ----------------------------------------------------------------------------
1830
Sahil Dhanjuc1ba5c42016-06-08 03:09:201831status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 21:22:201832 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1833 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-08 03:09:201834}
1835
1836status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 21:22:201837 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1838 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-08 03:09:201839}
1840
Dominik Laskowski3cb3d4e2019-11-21 19:14:451841status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
1842 ui::DisplayState* state) {
1843 return ComposerService::getComposerService()->getDisplayState(display, state);
1844}
1845
1846status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
1847 return ComposerService::getComposerService()->getDisplayInfo(display, info);
1848}
1849
Marin Shalamanova7fe3042021-01-29 20:02:081850status_t SurfaceComposerClient::getDisplayModes(const sp<IBinder>& display,
1851 Vector<ui::DisplayMode>* modes) {
1852 return ComposerService::getComposerService()->getDisplayModes(display, modes);
Dan Stoza7f7da322014-05-02 22:26:251853}
1854
Marin Shalamanova7fe3042021-01-29 20:02:081855status_t SurfaceComposerClient::getActiveDisplayMode(const sp<IBinder>& display,
1856 ui::DisplayMode* mode) {
1857 Vector<ui::DisplayMode> modes;
1858 status_t result = getDisplayModes(display, &modes);
Dan Stoza7f7da322014-05-02 22:26:251859 if (result != NO_ERROR) {
1860 return result;
1861 }
1862
Marin Shalamanova7fe3042021-01-29 20:02:081863 int activeId = getActiveDisplayModeId(display);
Dan Stoza7f7da322014-05-02 22:26:251864 if (activeId < 0) {
Marin Shalamanova7fe3042021-01-29 20:02:081865 ALOGE("No active mode found");
Dan Stoza7f7da322014-05-02 22:26:251866 return NAME_NOT_FOUND;
1867 }
1868
Marin Shalamanova7fe3042021-01-29 20:02:081869 *mode = modes[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 22:26:251870 return NO_ERROR;
1871}
1872
Marin Shalamanova7fe3042021-01-29 20:02:081873int SurfaceComposerClient::getActiveDisplayModeId(const sp<IBinder>& display) {
1874 return ComposerService::getComposerService()->getActiveDisplayModeId(display);
Dan Stoza7f7da322014-05-02 22:26:251875}
1876
Marin Shalamanova7fe3042021-01-29 20:02:081877status_t SurfaceComposerClient::setDesiredDisplayModeSpecs(
1878 const sp<IBinder>& displayToken, size_t defaultMode, bool allowGroupSwitching,
Marin Shalamanov30b0b3c2020-10-13 17:15:061879 float primaryRefreshRateMin, float primaryRefreshRateMax, float appRequestRefreshRateMin,
1880 float appRequestRefreshRateMax) {
Steven Thomasf734df42020-04-14 04:09:281881 return ComposerService::getComposerService()
Marin Shalamanova7fe3042021-01-29 20:02:081882 ->setDesiredDisplayModeSpecs(displayToken, defaultMode, allowGroupSwitching,
1883 primaryRefreshRateMin, primaryRefreshRateMax,
1884 appRequestRefreshRateMin, appRequestRefreshRateMax);
Ana Krulec0782b882019-10-16 00:34:541885}
1886
Marin Shalamanova7fe3042021-01-29 20:02:081887status_t SurfaceComposerClient::getDesiredDisplayModeSpecs(
1888 const sp<IBinder>& displayToken, size_t* outDefaultMode, bool* outAllowGroupSwitching,
Marin Shalamanov30b0b3c2020-10-13 17:15:061889 float* outPrimaryRefreshRateMin, float* outPrimaryRefreshRateMax,
1890 float* outAppRequestRefreshRateMin, float* outAppRequestRefreshRateMax) {
Steven Thomasf734df42020-04-14 04:09:281891 return ComposerService::getComposerService()
Marin Shalamanova7fe3042021-01-29 20:02:081892 ->getDesiredDisplayModeSpecs(displayToken, outDefaultMode, outAllowGroupSwitching,
1893 outPrimaryRefreshRateMin, outPrimaryRefreshRateMax,
1894 outAppRequestRefreshRateMin, outAppRequestRefreshRateMax);
Ana Krulec234bb162019-11-10 21:55:551895}
1896
Michael Wright28f24d02016-07-12 20:30:531897status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-15 00:26:311898 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 20:30:531899 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1900}
1901
Daniel Solomon42d04562019-01-21 05:03:191902status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1903 ui::DisplayPrimaries& outPrimaries) {
1904 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1905}
1906
Peiyong Lina52f0292018-03-15 00:26:311907ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 20:30:531908 return ComposerService::getComposerService()->getActiveColorMode(display);
1909}
1910
1911status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-15 00:26:311912 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 20:30:531913 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1914}
1915
Galia Peycheva5492cb52019-10-30 13:13:161916bool SurfaceComposerClient::getAutoLowLatencyModeSupport(const sp<IBinder>& display) {
1917 bool supported = false;
1918 ComposerService::getComposerService()->getAutoLowLatencyModeSupport(display, &supported);
1919 return supported;
1920}
1921
1922void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
1923 ComposerService::getComposerService()->setAutoLowLatencyMode(display, on);
1924}
1925
1926bool SurfaceComposerClient::getGameContentTypeSupport(const sp<IBinder>& display) {
1927 bool supported = false;
1928 ComposerService::getComposerService()->getGameContentTypeSupport(display, &supported);
1929 return supported;
1930}
1931
1932void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
1933 ComposerService::getComposerService()->setGameContentType(display, on);
1934}
1935
Prashant Malani2c9b11f2014-05-25 08:36:311936void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1937 int mode) {
1938 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-09 02:13:571939}
1940
Peiyong Linc6780972018-10-28 22:24:081941status_t SurfaceComposerClient::getCompositionPreference(
1942 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1943 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1944 return ComposerService::getComposerService()
1945 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1946 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 22:45:101947}
1948
Peiyong Lin08d10512019-01-16 21:27:351949bool SurfaceComposerClient::getProtectedContentSupport() {
1950 bool supported = false;
1951 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1952 return supported;
1953}
1954
Svetoslavd85084b2014-03-20 17:28:311955status_t SurfaceComposerClient::clearAnimationFrameStats() {
1956 return ComposerService::getComposerService()->clearAnimationFrameStats();
1957}
1958
1959status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1960 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1961}
1962
Dan Stozac4f471e2016-03-24 16:31:081963status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1964 HdrCapabilities* outCapabilities) {
1965 return ComposerService::getComposerService()->getHdrCapabilities(display,
1966 outCapabilities);
1967}
1968
Kevin DuBois9c0a1762018-10-16 20:32:311969status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1970 ui::PixelFormat* outFormat,
1971 ui::Dataspace* outDataspace,
1972 uint8_t* outComponentMask) {
1973 return ComposerService::getComposerService()
1974 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1975 outComponentMask);
1976}
1977
Kevin DuBois74e53772018-11-19 18:52:381978status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1979 bool enable, uint8_t componentMask,
1980 uint64_t maxFrames) {
1981 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1982 componentMask,
1983 maxFrames);
1984}
1985
Kevin DuBois1d4249a2018-08-29 17:45:141986status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1987 uint64_t maxFrames, uint64_t timestamp,
1988 DisplayedFrameStats* outStats) {
1989 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1990 timestamp, outStats);
1991}
Marissa Wall35187b32019-01-08 18:08:521992
Peiyong Lin4f3fddf2019-01-25 01:21:241993status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1994 bool* outIsWideColorDisplay) {
1995 return ComposerService::getComposerService()->isWideColorDisplay(display,
1996 outIsWideColorDisplay);
1997}
1998
Kevin DuBois00c66832019-02-19 00:21:311999status_t SurfaceComposerClient::addRegionSamplingListener(
2000 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
2001 const sp<IRegionSamplingListener>& listener) {
2002 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
2003 stopLayerHandle,
2004 listener);
2005}
2006
2007status_t SurfaceComposerClient::removeRegionSamplingListener(
2008 const sp<IRegionSamplingListener>& listener) {
2009 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
2010}
2011
Dan Gittik57e63c52019-01-18 16:37:542012bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
2013 bool support = false;
2014 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
2015 return support;
2016}
2017
2018status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
2019 float brightness) {
2020 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
2021}
2022
Lais Andrade3a6e47d2020-04-02 10:20:162023status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
2024 return ComposerService::getComposerService()->notifyPowerBoost(boostId);
Ady Abraham8532d012019-05-08 21:50:562025}
2026
Vishnu Nairb13bb952019-11-15 18:24:082027status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
2028 const half4& spotColor, float lightPosY,
2029 float lightPosZ, float lightRadius) {
2030 return ComposerService::getComposerService()->setGlobalShadowSettings(ambientColor, spotColor,
2031 lightPosY, lightPosZ,
2032 lightRadius);
2033}
2034
Ana Krulec31f2b3c2020-12-14 22:30:092035int SurfaceComposerClient::getGPUContextPriority() {
2036 return ComposerService::getComposerService()->getGPUContextPriority();
2037}
2038
Mathias Agopian698c0872011-06-29 02:09:312039// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-04 03:31:442040
chaviw690db382020-07-27 23:46:462041status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 23:08:592042 const sp<IScreenCaptureListener>& captureListener) {
Mathias Agopian2a9fc492013-03-01 21:42:572043 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 23:34:592044 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 18:25:372045
chaviwe7b9f272020-08-18 23:08:592046 return s->captureDisplay(captureArgs, captureListener);
Robert Carr673134e2017-01-10 03:48:382047}
2048
chaviw690db382020-07-27 23:46:462049status_t ScreenshotClient::captureDisplay(uint64_t displayOrLayerStack,
chaviwe7b9f272020-08-18 23:08:592050 const sp<IScreenCaptureListener>& captureListener) {
chaviw93df2ea2019-04-30 23:45:122051 sp<ISurfaceComposer> s(ComposerService::getComposerService());
2052 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 18:25:372053
chaviwe7b9f272020-08-18 23:08:592054 return s->captureDisplay(displayOrLayerStack, captureListener);
chaviw93df2ea2019-04-30 23:45:122055}
2056
chaviw26c52482020-07-28 23:25:522057status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 23:08:592058 const sp<IScreenCaptureListener>& captureListener) {
chaviwa76b2712017-09-20 19:02:262059 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 23:34:592060 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 18:25:372061
chaviwe7b9f272020-08-18 23:08:592062 return s->captureLayers(captureArgs, captureListener);
chaviwa76b2712017-09-20 19:02:262063}
Dominik Laskowski718f9602019-11-10 04:01:352064
2065} // namespace android