blob: 60966e191550761b549000ec429d08c779425d5e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070024#include <utils/SortedVector.h>
25#include <utils/String8.h>
26#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070028#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070030#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
Michael Wright28f24d02016-07-12 13:30:53 -070032#include <system/graphics.h>
33
Robert Carr673134e2017-01-09 19:48:38 -080034#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070035#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/ISurfaceComposerClient.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070039#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080040#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/SurfaceComposerClient.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080042#include <ui/DisplayConfig.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Robert Carr2c358bf2018-08-08 15:58:15 -070044#ifndef NO_INPUT
45#include <input/InputWindow.h>
46#endif
47
Mathias Agopian41f673c2011-11-17 17:48:35 -080048#include <private/gui/ComposerService.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Marissa Wall73411622019-01-25 10:45:41 -080050// 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-03 19:31:44 -080053namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070054
55using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056// ---------------------------------------------------------------------------
57
Mathias Agopian7e27f052010-05-28 14:22:23 -070058ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
59
Mathias Agopianb7e930d2010-06-01 15:12:58 -070060ComposerService::ComposerService()
61: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070062 Mutex::Autolock _l(mLock);
63 connectLocked();
64}
65
66void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070067 const String16 name("SurfaceFlinger");
68 while (getService(name, &mComposerService) != NO_ERROR) {
69 usleep(250000);
70 }
Yi Konga03e0442018-07-17 11:16:57 -070071 assert(mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070072
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 15:41:05 -070082 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -070083 };
84
85 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080086 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070087}
88
Andy McFadden6652b3e2012-09-06 18:45:56 -070089/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
90 ComposerService& instance = ComposerService::getInstance();
91 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -070092 if (instance.mComposerService == nullptr) {
Andy McFadden6652b3e2012-09-06 18:45:56 -070093 ComposerService::getInstance().connectLocked();
Yi Konga03e0442018-07-17 11:16:57 -070094 assert(instance.mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070095 ALOGD("ComposerService reconnected");
96 }
97 return instance.mComposerService;
98}
99
100void ComposerService::composerServiceDied()
101{
102 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700103 mComposerService = nullptr;
104 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700105}
106
Robert Carrfb4d58b2019-01-15 09:21:27 -0800107class 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
Mathias Agopian7e27f052010-05-28 14:22:23 -0700128// ---------------------------------------------------------------------------
129
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700130// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
131// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
132// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
133
Marissa Wallc837b5e2018-10-12 10:04:44 -0700134// 0 is an invalid callback id
135TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
136
137CallbackId TransactionCompletedListener::getNextIdLocked() {
138 return mCallbackIdCounter++;
139}
140
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700141sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
142 static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener;
143 return sInstance;
144}
145
Marissa Wallc837b5e2018-10-12 10:04:44 -0700146sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
147 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
148}
149
150void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700151 if (mListening) {
152 return;
153 }
154 ProcessState::self()->startThreadPool();
155 mListening = true;
156}
157
Marissa Wall80d94ad2019-01-18 16:04:36 -0800158CallbackId TransactionCompletedListener::addCallbackFunction(
159 const TransactionCompletedCallback& callbackFunction,
160 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
161 surfaceControls) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700162 std::lock_guard<std::mutex> lock(mMutex);
163 startListeningLocked();
164
165 CallbackId callbackId = getNextIdLocked();
Marissa Wall80d94ad2019-01-18 16:04:36 -0800166 mCallbacks[callbackId].callbackFunction = callbackFunction;
167
168 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
169
170 for (const auto& surfaceControl : surfaceControls) {
171 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
172 }
173
Marissa Wallc837b5e2018-10-12 10:04:44 -0700174 return callbackId;
175}
176
Marissa Wall80d94ad2019-01-18 16:04:36 -0800177void TransactionCompletedListener::addSurfaceControlToCallbacks(
178 const sp<SurfaceControl>& surfaceControl,
179 const std::unordered_set<CallbackId>& callbackIds) {
180 std::lock_guard<std::mutex> lock(mMutex);
181
182 for (auto callbackId : callbackIds) {
183 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
184 std::forward_as_tuple(
185 surfaceControl->getHandle()),
186 std::forward_as_tuple(surfaceControl));
187 }
188}
189
Marissa Walle2ffb422018-10-12 11:33:52 -0700190void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800191 std::unordered_map<CallbackId, CallbackTranslation> callbacksMap;
192 {
193 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700194
Valerie Haud3b90d22019-11-06 09:37:31 -0800195 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
196 * callbackIds, except for when Transactions are merged together. This probably cannot be
197 * solved before this point because the Transactions could be merged together and applied in
198 * a different process.
199 *
200 * Fortunately, we get all the callbacks for this listener for the same frame together at
201 * the same time. This means if any Transactions were merged together, we will get their
202 * callbacks at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps
203 * for all the callbackIds to generate one super map that contains all the sp<IBinder> to
204 * sp<SurfaceControl> that could possibly exist for the callbacks.
205 */
206 callbacksMap = mCallbacks;
207 for (const auto& transactionStats : listenerStats.transactionStats) {
208 for (auto& callbackId : transactionStats.callbackIds) {
209 mCallbacks.erase(callbackId);
210 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800211 }
212 }
Marissa Walld600d572019-03-26 15:38:50 -0700213 for (const auto& transactionStats : listenerStats.transactionStats) {
214 for (auto callbackId : transactionStats.callbackIds) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800215 auto& [callbackFunction, callbackSurfaceControls] = callbacksMap[callbackId];
Marissa Wall80d94ad2019-01-18 16:04:36 -0800216 if (!callbackFunction) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700217 ALOGE("cannot call null callback function, skipping");
218 continue;
219 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800220 std::vector<SurfaceControlStats> surfaceControlStats;
221 for (const auto& surfaceStats : transactionStats.surfaceStats) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800222 surfaceControlStats
223 .emplace_back(callbacksMap[callbackId]
224 .surfaceControls[surfaceStats.surfaceControl],
Valerie Hau871d6352020-01-29 08:44:02 -0800225 transactionStats.latchTime, surfaceStats.acquireTime,
226 transactionStats.presentFence,
227 surfaceStats.previousReleaseFence, surfaceStats.transformHint,
228 surfaceStats.eventStats);
Valerie Hau84d87ff2020-01-08 17:23:21 -0800229 if (callbacksMap[callbackId].surfaceControls[surfaceStats.surfaceControl]) {
230 callbacksMap[callbackId]
231 .surfaceControls[surfaceStats.surfaceControl]
232 ->setTransformHint(surfaceStats.transformHint);
233 }
Marissa Wall80d94ad2019-01-18 16:04:36 -0800234 }
235
236 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
237 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700238 }
239 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700240}
241
242// ---------------------------------------------------------------------------
243
Robert Carre06ad2b2020-04-10 15:09:33 -0700244void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId);
Marissa Wall78b72202019-03-15 14:58:34 -0700245
Robert Carre06ad2b2020-04-10 15:09:33 -0700246/**
247 * We use the BufferCache to reduce the overhead of exchanging GraphicBuffers with
248 * the server. If we were to simply parcel the GraphicBuffer we would pay two overheads
249 * 1. Cost of sending the FD
250 * 2. Cost of importing the GraphicBuffer with the mapper in the receiving process.
251 * To ease this cost we implement the following scheme of caching buffers to integers,
252 * or said-otherwise, naming them with integers. This is the scheme known as slots in
253 * the legacy BufferQueue system.
254 * 1. When sending Buffers to SurfaceFlinger we look up the Buffer in the cache.
255 * 2. If there is a cache-hit we remove the Buffer from the Transaction and instead
256 * send the cached integer.
257 * 3. If there is a cache miss, we cache the new buffer and send the integer
258 * along with the Buffer, SurfaceFlinger on it's side creates a new cache
259 * entry, and we use the integer for further communication.
260 * A few details about lifetime:
261 * 1. The cache evicts by LRU. The server side cache is keyed by BufferCache::getToken
262 * which is per process Unique. The server side cache is larger than the client side
263 * cache so that the server will never evict entries before the client.
264 * 2. When the client evicts an entry it notifies the server via an uncacheBuffer
265 * transaction.
266 * 3. The client only references the Buffers by ID, and uses buffer->addDeathCallback
267 * to auto-evict destroyed buffers.
268 */
Marissa Wall73411622019-01-25 10:45:41 -0800269class BufferCache : public Singleton<BufferCache> {
270public:
271 BufferCache() : token(new BBinder()) {}
272
273 sp<IBinder> getToken() {
274 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
275 }
276
Marissa Wall78b72202019-03-15 14:58:34 -0700277 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800278 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800279
Marissa Wall78b72202019-03-15 14:58:34 -0700280 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800281 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700282 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800283 }
Marissa Wall78b72202019-03-15 14:58:34 -0700284 itr->second = getCounter();
285 *cacheId = buffer->getId();
286 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800287 }
288
Marissa Wall78b72202019-03-15 14:58:34 -0700289 uint64_t cache(const sp<GraphicBuffer>& buffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800290 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800291
Marissa Wall78b72202019-03-15 14:58:34 -0700292 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
293 evictLeastRecentlyUsedBuffer();
294 }
Marissa Wall73411622019-01-25 10:45:41 -0800295
Robert Carre06ad2b2020-04-10 15:09:33 -0700296 buffer->addDeathCallback(removeDeadBufferCallback, nullptr);
Marissa Wall78b72202019-03-15 14:58:34 -0700297
298 mBuffers[buffer->getId()] = getCounter();
299 return buffer->getId();
300 }
301
302 void uncache(uint64_t cacheId) {
303 std::lock_guard<std::mutex> lock(mMutex);
304 uncacheLocked(cacheId);
305 }
306
307 void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) {
308 mBuffers.erase(cacheId);
309 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
Marissa Wall73411622019-01-25 10:45:41 -0800310 }
311
312private:
Marissa Wall78b72202019-03-15 14:58:34 -0700313 void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800314 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700315 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800316 auto minBuffer = itr;
317 itr++;
318
319 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700320 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800321 if (counter < minCounter) {
322 minCounter = counter;
323 minBuffer = itr;
324 }
325 itr++;
326 }
Marissa Wall78b72202019-03-15 14:58:34 -0700327 uncacheLocked(minBuffer->first);
Marissa Wall73411622019-01-25 10:45:41 -0800328 }
329
330 uint64_t getCounter() REQUIRES(mMutex) {
331 static uint64_t counter = 0;
332 return counter++;
333 }
334
Marissa Wall73411622019-01-25 10:45:41 -0800335 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700336 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800337
338 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
339 sp<IBinder> token;
340};
341
342ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
343
Robert Carre06ad2b2020-04-10 15:09:33 -0700344void removeDeadBufferCallback(void* /*context*/, uint64_t graphicBufferId) {
Marissa Wall78b72202019-03-15 14:58:34 -0700345 // GraphicBuffer id's are used as the cache ids.
346 BufferCache::getInstance().uncache(graphicBufferId);
347}
348
Marissa Wall73411622019-01-25 10:45:41 -0800349// ---------------------------------------------------------------------------
350
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000351// Initialize transaction id counter used to generate transaction ids
352// Transactions will start counting at 1, 0 is used for invalid transactions
353std::atomic<uint32_t> SurfaceComposerClient::Transaction::idCounter = 1;
354
355SurfaceComposerClient::Transaction::Transaction() {
356 mId = generateId();
357}
358
Marissa Wall17b4e452018-12-26 16:32:34 -0800359SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000360 : mId(other.mId),
361 mForceSynchronous(other.mForceSynchronous),
Marissa Wall17b4e452018-12-26 16:32:34 -0800362 mTransactionNestCount(other.mTransactionNestCount),
363 mAnimation(other.mAnimation),
364 mEarlyWakeup(other.mEarlyWakeup),
Ady Abrahambf1349c2020-06-12 14:26:18 -0700365 mExplicitEarlyWakeupStart(other.mExplicitEarlyWakeupStart),
366 mExplicitEarlyWakeupEnd(other.mExplicitEarlyWakeupEnd),
Vishnu Nair621102e2019-06-12 14:16:57 -0700367 mContainsBuffer(other.mContainsBuffer),
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700368 mDesiredPresentTime(other.mDesiredPresentTime),
369 mFrameTimelineVsyncId(other.mFrameTimelineVsyncId) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700370 mDisplayStates = other.mDisplayStates;
371 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800372 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700373 mListenerCallbacks = other.mListenerCallbacks;
374}
375
376std::unique_ptr<SurfaceComposerClient::Transaction>
377SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
378 auto transaction = std::make_unique<Transaction>();
379 if (transaction->readFromParcel(parcel) == NO_ERROR) {
380 return transaction;
381 }
382 return nullptr;
383}
384
Pablo Gamito7eb7ee72020-08-05 10:57:05 +0000385int64_t SurfaceComposerClient::Transaction::generateId() {
386 return (((int64_t)getpid()) << 32) | idCounter++;
387}
388
Vishnu Nair621102e2019-06-12 14:16:57 -0700389status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
390 const uint32_t forceSynchronous = parcel->readUint32();
391 const uint32_t transactionNestCount = parcel->readUint32();
392 const bool animation = parcel->readBool();
393 const bool earlyWakeup = parcel->readBool();
Ady Abrahambf1349c2020-06-12 14:26:18 -0700394 const bool explicitEarlyWakeupStart = parcel->readBool();
395 const bool explicitEarlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 14:16:57 -0700396 const bool containsBuffer = parcel->readBool();
397 const int64_t desiredPresentTime = parcel->readInt64();
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700398 const int64_t frameTimelineVsyncId = parcel->readInt64();
Vishnu Nair621102e2019-06-12 14:16:57 -0700399
400 size_t count = static_cast<size_t>(parcel->readUint32());
401 if (count > parcel->dataSize()) {
402 return BAD_VALUE;
403 }
404 SortedVector<DisplayState> displayStates;
405 displayStates.setCapacity(count);
406 for (size_t i = 0; i < count; i++) {
407 DisplayState displayState;
408 if (displayState.read(*parcel) == BAD_VALUE) {
409 return BAD_VALUE;
410 }
411 displayStates.add(displayState);
412 }
413
414 count = static_cast<size_t>(parcel->readUint32());
415 if (count > parcel->dataSize()) {
416 return BAD_VALUE;
417 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700418 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
419 listenerCallbacks.reserve(count);
420 for (size_t i = 0; i < count; i++) {
421 sp<ITransactionCompletedListener> listener =
422 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
423 size_t numCallbackIds = parcel->readUint32();
424 if (numCallbackIds > parcel->dataSize()) {
425 return BAD_VALUE;
426 }
427 for (size_t j = 0; j < numCallbackIds; j++) {
428 listenerCallbacks[listener].callbackIds.insert(parcel->readInt64());
429 }
430 size_t numSurfaces = parcel->readUint32();
431 if (numSurfaces > parcel->dataSize()) {
432 return BAD_VALUE;
433 }
434 for (size_t j = 0; j < numSurfaces; j++) {
435 sp<SurfaceControl> surface;
Pablo Gamito421dfd52020-09-22 18:11:45 +0000436 SAFE_PARCEL(SurfaceControl::readFromParcel, *parcel, &surface);
Valerie Hau9dab9732019-08-20 09:29:25 -0700437 listenerCallbacks[listener].surfaceControls.insert(surface);
438 }
439 }
440
441 count = static_cast<size_t>(parcel->readUint32());
442 if (count > parcel->dataSize()) {
443 return BAD_VALUE;
444 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700445 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 14:16:57 -0700446 composerStates.reserve(count);
447 for (size_t i = 0; i < count; i++) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000448 sp<IBinder> surfaceControlHandle;
449 SAFE_PARCEL(parcel->readStrongBinder, &surfaceControlHandle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700450
451 ComposerState composerState;
452 if (composerState.read(*parcel) == BAD_VALUE) {
453 return BAD_VALUE;
454 }
Pablo Gamitodbc31672020-09-01 18:28:58 +0000455
Vishnu Nairf03652d2019-07-16 17:56:56 -0700456 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 14:16:57 -0700457 }
458
459 InputWindowCommands inputWindowCommands;
460 inputWindowCommands.read(*parcel);
461
462 // Parsing was successful. Update the object.
463 mForceSynchronous = forceSynchronous;
464 mTransactionNestCount = transactionNestCount;
465 mAnimation = animation;
466 mEarlyWakeup = earlyWakeup;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700467 mExplicitEarlyWakeupStart = explicitEarlyWakeupStart;
468 mExplicitEarlyWakeupEnd = explicitEarlyWakeupEnd;
Vishnu Nair621102e2019-06-12 14:16:57 -0700469 mContainsBuffer = containsBuffer;
470 mDesiredPresentTime = desiredPresentTime;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700471 mFrameTimelineVsyncId = frameTimelineVsyncId;
Vishnu Nair621102e2019-06-12 14:16:57 -0700472 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 09:29:25 -0700473 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 14:16:57 -0700474 mComposerStates = composerStates;
475 mInputWindowCommands = inputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700476 return NO_ERROR;
477}
478
479status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700480 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
481 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
482 // but is unlikely to use them again as they are owned by the other process.
483 // You may be asking yourself, is this const cast safe? Const cast is safe up
484 // until the point where you try and write to an object that was originally const at which
485 // point we enter undefined behavior. In this case we are safe though, because there are
486 // two possibilities:
487 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
488 // 2. It was originall const! In this case not only was it useless, but it by definition
489 // contains no composer states and so cacheBuffers will not perform any writes.
490
491 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
492
Vishnu Nair621102e2019-06-12 14:16:57 -0700493 parcel->writeUint32(mForceSynchronous);
494 parcel->writeUint32(mTransactionNestCount);
495 parcel->writeBool(mAnimation);
496 parcel->writeBool(mEarlyWakeup);
Ady Abrahambf1349c2020-06-12 14:26:18 -0700497 parcel->writeBool(mExplicitEarlyWakeupStart);
498 parcel->writeBool(mExplicitEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 14:16:57 -0700499 parcel->writeBool(mContainsBuffer);
500 parcel->writeInt64(mDesiredPresentTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700501 parcel->writeInt64(mFrameTimelineVsyncId);
Vishnu Nair621102e2019-06-12 14:16:57 -0700502 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
503 for (auto const& displayState : mDisplayStates) {
504 displayState.write(*parcel);
505 }
506
Valerie Hau9dab9732019-08-20 09:29:25 -0700507 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
508 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
509 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
510 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
511 for (auto callbackId : callbackInfo.callbackIds) {
512 parcel->writeInt64(callbackId);
513 }
514 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
515 for (auto surfaceControl : callbackInfo.surfaceControls) {
Pablo Gamito421dfd52020-09-22 18:11:45 +0000516 SAFE_PARCEL(surfaceControl->writeToParcel, *parcel);
Valerie Hau9dab9732019-08-20 09:29:25 -0700517 }
518 }
519
Vishnu Nair621102e2019-06-12 14:16:57 -0700520 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Pablo Gamitodbc31672020-09-01 18:28:58 +0000521 for (auto const& [handle, composerState] : mComposerStates) {
522 SAFE_PARCEL(parcel->writeStrongBinder, handle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700523 composerState.write(*parcel);
524 }
525
526 mInputWindowCommands.write(*parcel);
527 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -0700528}
529
Robert Carr2c5f6d22017-09-26 12:30:35 -0700530SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000531 for (auto const& [handle, composerState] : other.mComposerStates) {
532 if (mComposerStates.count(handle) == 0) {
533 mComposerStates[handle] = composerState;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700534 } else {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000535 mComposerStates[handle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700536 }
537 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700538
539 for (auto const& state : other.mDisplayStates) {
540 ssize_t index = mDisplayStates.indexOf(state);
541 if (index < 0) {
542 mDisplayStates.add(state);
543 } else {
544 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
545 }
546 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700547
Marissa Wallc837b5e2018-10-12 10:04:44 -0700548 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
549 auto& [callbackIds, surfaceControls] = callbackInfo;
550 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
551 callbackIds.begin()),
552 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -0700553
Valerie Hau236eba32020-01-03 16:53:39 -0800554 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
555 surfaceControls.end());
556
557 auto& currentProcessCallbackInfo =
558 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
559 currentProcessCallbackInfo.surfaceControls
560 .insert(std::make_move_iterator(surfaceControls.begin()),
561 std::make_move_iterator(surfaceControls.end()));
562
563 // register all surface controls for all callbackIds for this listener that is merging
564 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
565 TransactionCompletedListener::getInstance()
566 ->addSurfaceControlToCallbacks(surfaceControl,
567 currentProcessCallbackInfo.callbackIds);
568 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700569 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700570
chaviw273171b2018-12-26 11:46:30 -0800571 mInputWindowCommands.merge(other.mInputWindowCommands);
572
Robert Carrbbc85622020-04-08 10:45:12 -0700573 mContainsBuffer |= other.mContainsBuffer;
Jorim Jaggie3b57002019-07-22 17:18:52 +0200574 mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700575 mExplicitEarlyWakeupStart = mExplicitEarlyWakeupStart || other.mExplicitEarlyWakeupStart;
576 mExplicitEarlyWakeupEnd = mExplicitEarlyWakeupEnd || other.mExplicitEarlyWakeupEnd;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700577
578 // When merging vsync Ids we take the oldest one
579 if (mFrameTimelineVsyncId != ISurfaceComposer::INVALID_VSYNC_ID &&
580 other.mFrameTimelineVsyncId != ISurfaceComposer::INVALID_VSYNC_ID) {
581 mFrameTimelineVsyncId = std::max(mFrameTimelineVsyncId, other.mFrameTimelineVsyncId);
582 } else if (mFrameTimelineVsyncId == ISurfaceComposer::INVALID_VSYNC_ID) {
583 mFrameTimelineVsyncId = other.mFrameTimelineVsyncId;
584 }
585
Vishnu Nairfef244e2019-06-17 18:07:51 -0700586 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -0700587 return *this;
588}
589
Vishnu Nairfef244e2019-06-17 18:07:51 -0700590void SurfaceComposerClient::Transaction::clear() {
591 mComposerStates.clear();
592 mDisplayStates.clear();
593 mListenerCallbacks.clear();
594 mInputWindowCommands.clear();
595 mContainsBuffer = false;
596 mForceSynchronous = 0;
597 mTransactionNestCount = 0;
598 mAnimation = false;
599 mEarlyWakeup = false;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700600 mExplicitEarlyWakeupStart = false;
601 mExplicitEarlyWakeupEnd = false;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700602 mDesiredPresentTime = -1;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700603 mFrameTimelineVsyncId = ISurfaceComposer::INVALID_VSYNC_ID;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700604}
605
Marissa Wall78b72202019-03-15 14:58:34 -0700606void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
607 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
608
Marissa Wall947d34e2019-03-29 14:03:53 -0700609 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -0700610 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700611 uncacheBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700612
Valerie Haufa889122019-04-15 13:56:05 -0700613 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700614 sf->setTransactionState(ISurfaceComposer::INVALID_VSYNC_ID, {}, {}, 0, applyToken, {}, -1,
615 uncacheBuffer, false, {}, 0 /* Undefined transactionId */);
Marissa Wall78b72202019-03-15 14:58:34 -0700616}
617
618void SurfaceComposerClient::Transaction::cacheBuffers() {
619 if (!mContainsBuffer) {
620 return;
621 }
622
623 size_t count = 0;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700624 for (auto& [handle, cs] : mComposerStates) {
Pablo Gamitodbc31672020-09-01 18:28:58 +0000625 layer_state_t* s = &(mComposerStates[handle].state);
Marissa Wall78b72202019-03-15 14:58:34 -0700626 if (!(s->what & layer_state_t::eBufferChanged)) {
627 continue;
Robert Carr28037922020-04-08 10:57:07 -0700628 } else if (s->what & layer_state_t::eCachedBufferChanged) {
629 // If eBufferChanged and eCachedBufferChanged are both trued then that means
630 // we already cached the buffer in a previous call to cacheBuffers, perhaps
631 // from writeToParcel on a Transaction that was merged in to this one.
632 continue;
Marissa Wall78b72202019-03-15 14:58:34 -0700633 }
634
Marissa Wall00597242019-03-27 10:35:19 -0700635 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
636 // time trying to cache them.
637 if (!s->buffer) {
638 continue;
639 }
640
Marissa Wall78b72202019-03-15 14:58:34 -0700641 uint64_t cacheId = 0;
642 status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId);
643 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -0700644 // Cache-hit. Strip the buffer and send only the id.
Marissa Walla141abe2019-03-27 16:28:07 -0700645 s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
Marissa Wall78b72202019-03-15 14:58:34 -0700646 s->buffer = nullptr;
647 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -0700648 // Cache-miss. Include the buffer and send the new cacheId.
Marissa Wall78b72202019-03-15 14:58:34 -0700649 cacheId = BufferCache::getInstance().cache(s->buffer);
650 }
651 s->what |= layer_state_t::eCachedBufferChanged;
652 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700653 s->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700654
655 // If we have more buffers than the size of the cache, we should stop caching so we don't
656 // evict other buffers in this transaction
657 count++;
658 if (count >= BUFFER_CACHE_MAX_SIZE) {
659 break;
660 }
661 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800662}
663
Robert Carr4cdc58f2017-08-23 14:22:20 -0700664status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
665 if (mStatus != NO_ERROR) {
666 return mStatus;
667 }
668
669 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
670
Valerie Hau9dab9732019-08-20 09:29:25 -0700671 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 14:03:19 -0700672 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700673 // For every listener with registered callbacks
674 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
675 auto& [callbackIds, surfaceControls] = callbackInfo;
676 if (callbackIds.empty()) {
677 continue;
678 }
679
Valerie Hau9dab9732019-08-20 09:29:25 -0700680 if (surfaceControls.empty()) {
681 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
682 } else {
683 // If the listener has any SurfaceControls set on this Transaction update the surface
684 // state
685 for (const auto& surfaceControl : surfaceControls) {
686 layer_state_t* s = getLayerState(surfaceControl);
687 if (!s) {
688 ALOGE("failed to get layer state");
689 continue;
690 }
691 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
692 s->what |= layer_state_t::eHasListenerCallbacksChanged;
693 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700694 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700695 }
696 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700697
Marissa Wall78b72202019-03-15 14:58:34 -0700698 cacheBuffers();
699
Robert Carr4cdc58f2017-08-23 14:22:20 -0700700 Vector<ComposerState> composerStates;
701 Vector<DisplayState> displayStates;
702 uint32_t flags = 0;
703
704 mForceSynchronous |= synchronous;
705
chaviw8e3fe5d2018-02-22 10:55:42 -0800706 for (auto const& kv : mComposerStates){
707 composerStates.add(kv.second);
708 }
709
Adithya Srinivasand3efcb32020-10-20 18:08:22 -0700710 displayStates = std::move(mDisplayStates);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700711
712 if (mForceSynchronous) {
713 flags |= ISurfaceComposer::eSynchronous;
714 }
715 if (mAnimation) {
716 flags |= ISurfaceComposer::eAnimation;
717 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700718 if (mEarlyWakeup) {
719 flags |= ISurfaceComposer::eEarlyWakeup;
720 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700721
Ady Abrahambf1349c2020-06-12 14:26:18 -0700722 // If both mExplicitEarlyWakeupStart and mExplicitEarlyWakeupEnd are set
723 // it is equivalent for none
724 if (mExplicitEarlyWakeupStart && !mExplicitEarlyWakeupEnd) {
725 flags |= ISurfaceComposer::eExplicitEarlyWakeupStart;
726 }
727 if (mExplicitEarlyWakeupEnd && !mExplicitEarlyWakeupStart) {
728 flags |= ISurfaceComposer::eExplicitEarlyWakeupEnd;
729 }
730
Marissa Wall713b63f2018-10-17 15:42:43 -0700731 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700732 sf->setTransactionState(mFrameTimelineVsyncId, composerStates, displayStates, flags, applyToken,
733 mInputWindowCommands, mDesiredPresentTime,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700734 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
Adithya Srinivasand3efcb32020-10-20 18:08:22 -0700735 hasListenerCallbacks, listenerCallbacks, mId);
736 mId = generateId();
737
738 // Clear the current states and flags
739 clear();
740
Robert Carr4cdc58f2017-08-23 14:22:20 -0700741 mStatus = NO_ERROR;
742 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700743}
744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745// ---------------------------------------------------------------------------
746
Robert Carr4cdc58f2017-08-23 14:22:20 -0700747sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700748 return ComposerService::getComposerService()->createDisplay(displayName,
749 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700750}
751
Robert Carr4cdc58f2017-08-23 14:22:20 -0700752void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700753 return ComposerService::getComposerService()->destroyDisplay(display);
754}
755
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800756std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
757 return ComposerService::getComposerService()->getPhysicalDisplayIds();
758}
759
760std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
761 return ComposerService::getComposerService()->getInternalDisplayId();
762}
763
764sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
765 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
766}
767
768sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
769 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700770}
771
Robert Carr4cdc58f2017-08-23 14:22:20 -0700772void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700773 mAnimation = true;
774}
775
Dan Stoza84d619e2018-03-28 17:07:36 -0700776void SurfaceComposerClient::Transaction::setEarlyWakeup() {
777 mEarlyWakeup = true;
778}
779
Ady Abrahambf1349c2020-06-12 14:26:18 -0700780void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupStart() {
781 mExplicitEarlyWakeupStart = true;
782}
783
784void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupEnd() {
785 mExplicitEarlyWakeupEnd = true;
786}
787
Pablo Gamitodbc31672020-09-01 18:28:58 +0000788layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
789 auto handle = sc->getHandle();
790
Vishnu Nairf03652d2019-07-16 17:56:56 -0700791 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700792 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -0800793 ComposerState s;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000794
Vishnu Nairf03652d2019-07-16 17:56:56 -0700795 s.state.surface = handle;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000796 s.state.layerId = sc->getLayerId();
797
Vishnu Nairf03652d2019-07-16 17:56:56 -0700798 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -0700799 }
800
Vishnu Nairf03652d2019-07-16 17:56:56 -0700801 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-28 19:09:31 -0700802}
803
Marissa Wallc837b5e2018-10-12 10:04:44 -0700804void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
805 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800806 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
807 callbackInfo.surfaceControls.insert(sc);
808
809 TransactionCompletedListener::getInstance()
810 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700811}
812
Robert Carr4cdc58f2017-08-23 14:22:20 -0700813SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
814 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -0800815 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700816 if (!s) {
817 mStatus = BAD_INDEX;
818 return *this;
819 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700820 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700821 s->x = x;
822 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700823
824 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700825 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700826}
827
Robert Carr4cdc58f2017-08-23 14:22:20 -0700828SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
829 const sp<SurfaceControl>& sc) {
830 return setFlags(sc, 0, layer_state_t::eLayerHidden);
831}
832
833SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
834 const sp<SurfaceControl>& sc) {
835 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
836}
837
838SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
839 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-22 16:04:57 -0800840 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700841 if (!s) {
842 mStatus = BAD_INDEX;
843 return *this;
844 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700845 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700846 s->w = w;
847 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700848
Marissa Wallc837b5e2018-10-12 10:04:44 -0700849 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700850 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700851}
852
Robert Carr4cdc58f2017-08-23 14:22:20 -0700853SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
854 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800855 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700856 if (!s) {
857 mStatus = BAD_INDEX;
858 return *this;
859 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700860 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700861 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700862 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700863
864 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700865 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700866}
867
Pablo Gamito11dcc222020-09-12 15:49:39 +0000868SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(
869 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& relativeTo, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800870 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700871 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700872 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -0700873 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700874 }
875 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700876 s->what &= ~layer_state_t::eLayerChanged;
Pablo Gamito11dcc222020-09-12 15:49:39 +0000877 s->relativeLayerSurfaceControl = relativeTo;
Robert Carrdb66e622017-04-10 16:55:57 -0700878 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700879
880 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700881 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700882}
883
Robert Carr4cdc58f2017-08-23 14:22:20 -0700884SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
885 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700886 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -0800887 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700888 if (!s) {
889 mStatus = BAD_INDEX;
890 return *this;
891 }
chaviwc5676c62020-09-18 15:01:04 -0700892 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
893 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot)) {
Dan Stoza23116082015-06-18 14:58:39 -0700894 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800895 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700896 s->flags &= ~mask;
897 s->flags |= (flags & mask);
898 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700899
900 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700901 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700902}
903
Robert Carr4cdc58f2017-08-23 14:22:20 -0700904SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
905 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700906 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800907 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700908 if (!s) {
909 mStatus = BAD_INDEX;
910 return *this;
911 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700912 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700913 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700914
915 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700916 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700917}
918
Robert Carr4cdc58f2017-08-23 14:22:20 -0700919SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
920 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800921 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700922 if (!s) {
923 mStatus = BAD_INDEX;
924 return *this;
925 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700926 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700927 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700928
929 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700930 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700931}
932
Robert Carr4cdc58f2017-08-23 14:22:20 -0700933SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
934 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -0800935 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700936 if (!s) {
937 mStatus = BAD_INDEX;
938 return *this;
939 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700940 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700941 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700942
943 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700944 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700945}
946
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800947SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -0700948 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800949 layer_state_t* s = getLayerState(sc);
950 if (!s) {
951 mStatus = BAD_INDEX;
952 return *this;
953 }
954 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -0700955
956 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800957
958 registerSurfaceControlForCallback(sc);
959 return *this;
960}
961
Robert Carr4cdc58f2017-08-23 14:22:20 -0700962SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
963 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800964 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800965 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700966 if (!s) {
967 mStatus = BAD_INDEX;
968 return *this;
969 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700970 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700971 layer_state_t::matrix22_t matrix;
972 matrix.dsdx = dsdx;
973 matrix.dtdx = dtdx;
974 matrix.dsdy = dsdy;
975 matrix.dtdy = dtdy;
976 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700977
978 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700979 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700980}
981
Marissa Wallf58c14b2018-07-24 10:50:43 -0700982SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700983 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800984 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700985 if (!s) {
986 mStatus = BAD_INDEX;
987 return *this;
988 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700989 s->what |= layer_state_t::eCropChanged_legacy;
990 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700991
992 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700993 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700994}
995
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700996SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
997 const sp<SurfaceControl>& sc, float cornerRadius) {
998 layer_state_t* s = getLayerState(sc);
999 if (!s) {
1000 mStatus = BAD_INDEX;
1001 return *this;
1002 }
1003 s->what |= layer_state_t::eCornerRadiusChanged;
1004 s->cornerRadius = cornerRadius;
1005 return *this;
1006}
1007
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001008SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
1009 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
1010 layer_state_t* s = getLayerState(sc);
1011 if (!s) {
1012 mStatus = BAD_INDEX;
1013 return *this;
1014 }
1015 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
1016 s->backgroundBlurRadius = backgroundBlurRadius;
1017 return *this;
1018}
1019
Marissa Wallf58c14b2018-07-24 10:50:43 -07001020SurfaceComposerClient::Transaction&
Pablo Gamito11dcc222020-09-12 15:49:39 +00001021SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(
1022 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& barrierSurfaceControl,
1023 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -08001024 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -07001025 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001026 mStatus = BAD_INDEX;
1027 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -07001028 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001029 s->what |= layer_state_t::eDeferTransaction_legacy;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001030 s->barrierSurfaceControl_legacy = barrierSurfaceControl;
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001031 s->barrierFrameNumber = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001032
1033 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001034 return *this;
Robert Carr0d480722017-01-10 16:42:54 -08001035}
1036
Robert Carr4cdc58f2017-08-23 14:22:20 -07001037SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001038 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001039 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -08001040 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001041 mStatus = BAD_INDEX;
1042 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001043 }
1044 s->what |= layer_state_t::eReparentChildren;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001045 s->reparentSurfaceControl = newParent;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001046
1047 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001048 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001049}
1050
Robert Carr4cdc58f2017-08-23 14:22:20 -07001051SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
Pablo Gamito11dcc222020-09-12 15:49:39 +00001052 const sp<SurfaceControl>& sc, const sp<SurfaceControl>& newParent) {
chaviw763ef572018-02-22 16:04:57 -08001053 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001054 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001055 mStatus = BAD_INDEX;
1056 return *this;
chaviw06178942017-07-27 10:25:59 -07001057 }
chaviwf1961f72017-09-18 16:41:07 -07001058 s->what |= layer_state_t::eReparent;
Pablo Gamito11dcc222020-09-12 15:49:39 +00001059 s->parentSurfaceControlForChild = newParent;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001060
1061 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001062 return *this;
chaviw06178942017-07-27 10:25:59 -07001063}
1064
Robert Carr4cdc58f2017-08-23 14:22:20 -07001065SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1066 const sp<SurfaceControl>& sc,
1067 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001068 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001069 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001070 mStatus = BAD_INDEX;
1071 return *this;
1072 }
1073 s->what |= layer_state_t::eColorChanged;
1074 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001075
1076 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001077 return *this;
1078}
1079
Valerie Haudd0b7572019-01-29 14:59:27 -08001080SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1081 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001082 layer_state_t* s = getLayerState(sc);
1083 if (!s) {
1084 mStatus = BAD_INDEX;
1085 return *this;
1086 }
1087
Valerie Haudd0b7572019-01-29 14:59:27 -08001088 s->what |= layer_state_t::eBackgroundColorChanged;
1089 s->color = color;
1090 s->bgColorAlpha = alpha;
1091 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001092
1093 registerSurfaceControlForCallback(sc);
1094 return *this;
1095}
1096
Marissa Wall61c58622018-07-18 10:12:20 -07001097SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1098 const sp<SurfaceControl>& sc, uint32_t transform) {
1099 layer_state_t* s = getLayerState(sc);
1100 if (!s) {
1101 mStatus = BAD_INDEX;
1102 return *this;
1103 }
1104 s->what |= layer_state_t::eTransformChanged;
1105 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001106
1107 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001108 return *this;
1109}
1110
1111SurfaceComposerClient::Transaction&
1112SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1113 bool transformToDisplayInverse) {
1114 layer_state_t* s = getLayerState(sc);
1115 if (!s) {
1116 mStatus = BAD_INDEX;
1117 return *this;
1118 }
1119 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1120 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001121
1122 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001123 return *this;
1124}
1125
1126SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
1127 const sp<SurfaceControl>& sc, const Rect& crop) {
1128 layer_state_t* s = getLayerState(sc);
1129 if (!s) {
1130 mStatus = BAD_INDEX;
1131 return *this;
1132 }
1133 s->what |= layer_state_t::eCropChanged;
1134 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001135
1136 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001137 return *this;
1138}
1139
Marissa Wall861616d2018-10-22 12:52:23 -07001140SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
1141 const sp<SurfaceControl>& sc, const Rect& frame) {
1142 layer_state_t* s = getLayerState(sc);
1143 if (!s) {
1144 mStatus = BAD_INDEX;
1145 return *this;
1146 }
1147 s->what |= layer_state_t::eFrameChanged;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001148 s->orientedDisplaySpaceRect = frame;
Marissa Wall861616d2018-10-22 12:52:23 -07001149
1150 registerSurfaceControlForCallback(sc);
1151 return *this;
1152}
1153
Marissa Wall61c58622018-07-18 10:12:20 -07001154SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
1155 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
1156 layer_state_t* s = getLayerState(sc);
1157 if (!s) {
1158 mStatus = BAD_INDEX;
1159 return *this;
1160 }
Marissa Wall78b72202019-03-15 14:58:34 -07001161 s->what |= layer_state_t::eBufferChanged;
1162 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -08001163
1164 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001165
1166 mContainsBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001167 return *this;
1168}
1169
Marissa Wall61c58622018-07-18 10:12:20 -07001170SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
1171 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
1172 layer_state_t* s = getLayerState(sc);
1173 if (!s) {
1174 mStatus = BAD_INDEX;
1175 return *this;
1176 }
1177 s->what |= layer_state_t::eAcquireFenceChanged;
1178 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001179
1180 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001181 return *this;
1182}
1183
1184SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1185 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1186 layer_state_t* s = getLayerState(sc);
1187 if (!s) {
1188 mStatus = BAD_INDEX;
1189 return *this;
1190 }
1191 s->what |= layer_state_t::eDataspaceChanged;
1192 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001193
1194 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001195 return *this;
1196}
1197
1198SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1199 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1200 layer_state_t* s = getLayerState(sc);
1201 if (!s) {
1202 mStatus = BAD_INDEX;
1203 return *this;
1204 }
1205 s->what |= layer_state_t::eHdrMetadataChanged;
1206 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001207
1208 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001209 return *this;
1210}
1211
1212SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1213 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1214 layer_state_t* s = getLayerState(sc);
1215 if (!s) {
1216 mStatus = BAD_INDEX;
1217 return *this;
1218 }
1219 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1220 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001221
1222 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001223 return *this;
1224}
1225
1226SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1227 const sp<SurfaceControl>& sc, int32_t api) {
1228 layer_state_t* s = getLayerState(sc);
1229 if (!s) {
1230 mStatus = BAD_INDEX;
1231 return *this;
1232 }
1233 s->what |= layer_state_t::eApiChanged;
1234 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001235
1236 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001237 return *this;
1238}
1239
1240SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1241 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1242 layer_state_t* s = getLayerState(sc);
1243 if (!s) {
1244 mStatus = BAD_INDEX;
1245 return *this;
1246 }
1247 s->what |= layer_state_t::eSidebandStreamChanged;
1248 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001249
1250 registerSurfaceControlForCallback(sc);
1251 return *this;
1252}
1253
Marissa Wall17b4e452018-12-26 16:32:34 -08001254SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1255 nsecs_t desiredPresentTime) {
1256 mDesiredPresentTime = desiredPresentTime;
1257 return *this;
1258}
1259
Peiyong Linc502cb72019-03-01 15:00:23 -08001260SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1261 const sp<SurfaceControl>& sc, const bool agnostic) {
1262 layer_state_t* s = getLayerState(sc);
1263 if (!s) {
1264 mStatus = BAD_INDEX;
1265 return *this;
1266 }
1267 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1268 s->colorSpaceAgnostic = agnostic;
1269
1270 registerSurfaceControlForCallback(sc);
1271 return *this;
1272}
1273
Marissa Wallc837b5e2018-10-12 10:04:44 -07001274SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001275SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1276 int32_t priority) {
1277 layer_state_t* s = getLayerState(sc);
1278 if (!s) {
1279 mStatus = BAD_INDEX;
1280 return *this;
1281 }
1282
1283 s->what |= layer_state_t::eFrameRateSelectionPriority;
1284 s->frameRateSelectionPriority = priority;
1285
1286 registerSurfaceControlForCallback(sc);
1287 return *this;
1288}
1289
1290SurfaceComposerClient::Transaction&
Marissa Wallc837b5e2018-10-12 10:04:44 -07001291SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001292 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001293 auto listener = TransactionCompletedListener::getInstance();
1294
Marissa Wall80d94ad2019-01-18 16:04:36 -08001295 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1296 std::placeholders::_2, std::placeholders::_3);
1297 const auto& surfaceControls =
1298 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001299
Marissa Wall80d94ad2019-01-18 16:04:36 -08001300 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001301
1302 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1303 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001304 return *this;
1305}
1306
Valerie Hau871d6352020-01-29 08:44:02 -08001307SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1308 const sp<SurfaceControl>& sc) {
1309 layer_state_t* s = getLayerState(sc);
1310 if (!s) {
1311 mStatus = BAD_INDEX;
1312 return *this;
1313 }
1314
1315 s->what |= layer_state_t::eProducerDisconnect;
1316 return *this;
1317}
1318
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001319SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameNumber(
1320 const sp<SurfaceControl>& sc, uint64_t frameNumber) {
1321 layer_state_t* s = getLayerState(sc);
1322 if (!s) {
1323 mStatus = BAD_INDEX;
1324 return *this;
1325 }
1326
1327 s->what |= layer_state_t::eFrameNumberChanged;
1328 s->frameNumber = frameNumber;
1329
1330 return *this;
1331}
1332
Robert Carr4cdc58f2017-08-23 14:22:20 -07001333SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1334 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001335 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001336 if (!s) {
1337 mStatus = BAD_INDEX;
Greg Kaiserd45fdc32019-04-30 06:14:19 -07001338 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001339 }
1340 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001341
1342 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001343 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001344}
1345
Robert Carr2c358bf2018-08-08 15:58:15 -07001346#ifndef NO_INPUT
1347SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1348 const sp<SurfaceControl>& sc,
1349 const InputWindowInfo& info) {
1350 layer_state_t* s = getLayerState(sc);
1351 if (!s) {
1352 mStatus = BAD_INDEX;
1353 return *this;
1354 }
Chris Ye0783e992020-06-02 21:34:49 -07001355 s->inputHandle = new InputWindowHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001356 s->what |= layer_state_t::eInputInfoChanged;
1357 return *this;
1358}
chaviw273171b2018-12-26 11:46:30 -08001359
Vishnu Naire798b472020-07-23 13:52:21 -07001360SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Nair958da932020-08-21 17:12:37 -07001361 const sp<IBinder>& token, const sp<IBinder>& focusedToken, nsecs_t timestampNanos,
1362 int32_t displayId) {
Vishnu Naire798b472020-07-23 13:52:21 -07001363 FocusRequest request;
1364 request.token = token;
1365 request.focusedToken = focusedToken;
1366 request.timestamp = timestampNanos;
Vishnu Nair958da932020-08-21 17:12:37 -07001367 request.displayId = displayId;
Vishnu Naire798b472020-07-23 13:52:21 -07001368 return setFocusedWindow(request);
1369}
1370
1371SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
1372 const FocusRequest& request) {
1373 mInputWindowCommands.focusRequests.push_back(request);
1374 return *this;
1375}
1376
chaviwa911b102019-02-14 10:18:33 -08001377SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1378 mInputWindowCommands.syncInputWindows = true;
1379 return *this;
1380}
1381
Robert Carr2c358bf2018-08-08 15:58:15 -07001382#endif
1383
Peiyong Lind3788632018-09-18 16:01:31 -07001384SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1385 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1386 layer_state_t* s = getLayerState(sc);
1387 if (!s) {
1388 mStatus = BAD_INDEX;
1389 return *this;
1390 }
1391 s->what |= layer_state_t::eColorTransformChanged;
1392 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001393
1394 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001395 return *this;
1396}
1397
Robert Carrfb4d58b2019-01-15 09:21:27 -08001398SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1399 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1400 setCrop_legacy(sc, source);
1401
1402 int x = dst.left;
1403 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001404
1405 float sourceWidth = source.getWidth();
1406 float sourceHeight = source.getHeight();
1407
1408 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1409 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001410 float matrix[4] = {1, 0, 0, 1};
1411
1412 switch (transform) {
1413 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1414 matrix[0] = -xScale; matrix[1] = 0;
1415 matrix[2] = 0; matrix[3] = yScale;
1416 x += source.getWidth();
1417 break;
1418 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1419 matrix[0] = xScale; matrix[1] = 0;
1420 matrix[2] = 0; matrix[3] = -yScale;
1421 y += source.getHeight();
1422 break;
1423 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1424 matrix[0] = 0; matrix[1] = -yScale;
1425 matrix[2] = xScale; matrix[3] = 0;
1426 x += source.getHeight();
1427 break;
1428 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1429 matrix[0] = -xScale; matrix[1] = 0;
1430 matrix[2] = 0; matrix[3] = -yScale;
1431 x += source.getWidth();
1432 y += source.getHeight();
1433 break;
1434 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1435 matrix[0] = 0; matrix[1] = yScale;
1436 matrix[2] = -xScale; matrix[3] = 0;
1437 y += source.getWidth();
1438 break;
1439 default:
1440 matrix[0] = xScale; matrix[1] = 0;
1441 matrix[2] = 0; matrix[3] = yScale;
1442 break;
1443 }
1444 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07001445 float offsetX = xScale * source.left;
1446 float offsetY = yScale * source.top;
1447 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001448
1449 return *this;
1450}
1451
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001452SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1453 const sp<SurfaceControl>& sc, float shadowRadius) {
1454 layer_state_t* s = getLayerState(sc);
1455 if (!s) {
1456 mStatus = BAD_INDEX;
1457 return *this;
1458 }
1459 s->what |= layer_state_t::eShadowRadiusChanged;
1460 s->shadowRadius = shadowRadius;
1461 return *this;
1462}
1463
Steven Thomas3172e202020-01-06 19:25:30 -08001464SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Steven Thomas62a4cf82020-01-31 12:04:03 -08001465 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility) {
Steven Thomas3172e202020-01-06 19:25:30 -08001466 layer_state_t* s = getLayerState(sc);
1467 if (!s) {
1468 mStatus = BAD_INDEX;
1469 return *this;
1470 }
Steven Thomas62a4cf82020-01-31 12:04:03 -08001471 if (!ValidateFrameRate(frameRate, compatibility, "Transaction::setFrameRate")) {
1472 mStatus = BAD_VALUE;
1473 return *this;
1474 }
Steven Thomas3172e202020-01-06 19:25:30 -08001475 s->what |= layer_state_t::eFrameRateChanged;
1476 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08001477 s->frameRateCompatibility = compatibility;
Steven Thomas3172e202020-01-06 19:25:30 -08001478 return *this;
1479}
1480
Vishnu Nair6213bd92020-05-08 17:42:25 -07001481SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
1482 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
1483 layer_state_t* s = getLayerState(sc);
1484 if (!s) {
1485 mStatus = BAD_INDEX;
1486 return *this;
1487 }
1488
1489 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
1490 ? ui::Transform::ROT_INVALID
1491 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
1492 s->what |= layer_state_t::eFixedTransformHintChanged;
1493 s->fixedTransformHint = transform;
1494 return *this;
1495}
1496
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001497SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineVsync(
1498 int64_t frameTimelineVsyncId) {
1499 mFrameTimelineVsyncId = frameTimelineVsyncId;
1500 return *this;
1501}
1502
Robert Carr9b611b72020-10-19 12:00:23 -07001503SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameTimelineVsync(
1504 const sp<SurfaceControl>& sc, int64_t frameTimelineVsyncId) {
1505 layer_state_t* s = getLayerState(sc);
1506 if (!s) {
1507 mStatus = BAD_INDEX;
1508 return *this;
1509 }
1510
1511 s->what |= layer_state_t::eFrameTimelineVsyncChanged;
1512 s->frameTimelineVsyncId = frameTimelineVsyncId;
1513 return *this;
1514}
1515
Mathias Agopian698c0872011-06-28 19:09:31 -07001516// ---------------------------------------------------------------------------
1517
chaviw763ef572018-02-22 16:04:57 -08001518DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001519 DisplayState s;
1520 s.token = token;
1521 ssize_t index = mDisplayStates.indexOf(s);
1522 if (index < 0) {
1523 // we don't have it, add an initialized layer_state to our list
1524 s.what = 0;
1525 index = mDisplayStates.add(s);
1526 }
Dan Stozad723bd72014-11-18 10:24:03 -08001527 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001528}
1529
Robert Carr4cdc58f2017-08-23 14:22:20 -07001530status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1531 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001532 if (bufferProducer.get() != nullptr) {
1533 // Make sure that composition can never be stalled by a virtual display
1534 // consumer that isn't processing buffers fast enough.
1535 status_t err = bufferProducer->setAsyncMode(true);
1536 if (err != NO_ERROR) {
1537 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1538 "BufferQueue. This BufferQueue cannot be used for virtual "
1539 "display. (%d)", err);
1540 return err;
1541 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001542 }
chaviw763ef572018-02-22 16:04:57 -08001543 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001544 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001545 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001546 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001547}
1548
Robert Carr4cdc58f2017-08-23 14:22:20 -07001549void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001550 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001551 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001552 s.layerStack = layerStack;
1553 s.what |= DisplayState::eLayerStackChanged;
1554}
1555
Robert Carr4cdc58f2017-08-23 14:22:20 -07001556void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08001557 ui::Rotation orientation,
1558 const Rect& layerStackRect,
1559 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001560 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001561 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001562 s.layerStackSpaceRect = layerStackRect;
1563 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001564 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001565 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001566}
1567
Robert Carr4cdc58f2017-08-23 14:22:20 -07001568void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001569 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001570 s.width = width;
1571 s.height = height;
1572 s.what |= DisplayState::eDisplaySizeChanged;
1573}
1574
Mathias Agopiane57f2922012-08-09 16:29:12 -07001575// ---------------------------------------------------------------------------
1576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001578 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580}
1581
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001582SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1583 : mStatus(NO_ERROR), mClient(client)
1584{
1585}
1586
Mathias Agopian698c0872011-06-28 19:09:31 -07001587void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001588 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001589 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001590 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001591 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001592 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001593 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001594 mStatus = NO_ERROR;
1595 }
1596 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597}
1598
Mathias Agopian698c0872011-06-28 19:09:31 -07001599SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001600 dispose();
1601}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001602
Mathias Agopian698c0872011-06-28 19:09:31 -07001603status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 return mStatus;
1605}
1606
Mathias Agopian698c0872011-06-28 19:09:31 -07001607sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001608 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609}
1610
Mathias Agopiand4784a32010-05-27 19:41:15 -07001611status_t SurfaceComposerClient::linkToComposerDeath(
1612 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001613 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001614 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1615 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616}
1617
Mathias Agopian698c0872011-06-28 19:09:31 -07001618void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001620 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001621 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001622 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001623 client = mClient; // hold ref while lock is held
1624 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001626 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627}
1628
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001629sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1630 PixelFormat format, uint32_t flags,
1631 SurfaceControl* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001632 LayerMetadata metadata,
1633 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001634 sp<SurfaceControl> s;
Valerie Hau1acd6962019-10-28 16:35:48 -07001635 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata),
1636 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07001637 return s;
1638}
1639
Marissa Wall35187b32019-01-08 10:08:52 -08001640sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1641 uint32_t h, PixelFormat format,
1642 uint32_t flags, Surface* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001643 LayerMetadata metadata,
1644 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -08001645 sp<SurfaceControl> sur;
1646 status_t err = mStatus;
1647
1648 if (mStatus == NO_ERROR) {
1649 sp<IBinder> handle;
1650 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1651 sp<IGraphicBufferProducer> gbp;
1652
Valerie Hau1acd6962019-10-28 16:35:48 -07001653 uint32_t transformHint = 0;
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +00001654 int32_t id = -1;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001655 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +00001656 std::move(metadata), &handle, &gbp, &id,
1657 &transformHint);
Valerie Hau1acd6962019-10-28 16:35:48 -07001658 if (outTransformHint) {
1659 *outTransformHint = transformHint;
1660 }
Marissa Wall35187b32019-01-08 10:08:52 -08001661 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1662 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:58 +00001663 return new SurfaceControl(this, handle, gbp, id, transformHint);
Marissa Wall35187b32019-01-08 10:08:52 -08001664 }
1665 }
1666 return nullptr;
1667}
1668
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001669status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1670 PixelFormat format,
1671 sp<SurfaceControl>* outSurface, uint32_t flags,
Valerie Hau1acd6962019-10-28 16:35:48 -07001672 SurfaceControl* parent, LayerMetadata metadata,
1673 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001674 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001675 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001676
Mathias Agopian698c0872011-06-28 19:09:31 -07001677 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001678 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001680 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681
1682 if (parent != nullptr) {
1683 parentHandle = parent->getHandle();
1684 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001685
Valerie Hau1acd6962019-10-28 16:35:48 -07001686 uint32_t transformHint = 0;
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +00001687 int32_t id = -1;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001688 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +00001689 &handle, &gbp, &id, &transformHint);
1690
Valerie Hau1acd6962019-10-28 16:35:48 -07001691 if (outTransformHint) {
1692 *outTransformHint = transformHint;
1693 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001694 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1695 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:58 +00001696 *outSurface = new SurfaceControl(this, handle, gbp, id, transformHint);
Mathias Agopian698c0872011-06-28 19:09:31 -07001697 }
1698 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001699 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001700}
1701
chaviwfe94a222019-08-21 13:52:59 -07001702sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
1703 if (mirrorFromSurface == nullptr) {
1704 return nullptr;
1705 }
1706
1707 sp<IBinder> handle;
1708 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
Pablo Gamito2ec1f7b2020-09-01 14:18:49 +00001709 int32_t layer_id = -1;
1710 status_t err = mClient->mirrorSurface(mirrorFromHandle, &handle, &layer_id);
chaviwfe94a222019-08-21 13:52:59 -07001711 if (err == NO_ERROR) {
Pablo Gamitodbc31672020-09-01 18:28:58 +00001712 return new SurfaceControl(this, handle, nullptr, layer_id, true /* owned */);
chaviwfe94a222019-08-21 13:52:59 -07001713 }
1714 return nullptr;
1715}
1716
Svetoslavd85084b2014-03-20 10:28:31 -07001717status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1718 if (mStatus != NO_ERROR) {
1719 return mStatus;
1720 }
1721 return mClient->clearLayerFrameStats(token);
1722}
1723
1724status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1725 FrameStats* outStats) const {
1726 if (mStatus != NO_ERROR) {
1727 return mStatus;
1728 }
1729 return mClient->getLayerFrameStats(token, outStats);
1730}
1731
Mathias Agopian698c0872011-06-28 19:09:31 -07001732// ----------------------------------------------------------------------------
1733
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001734status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001735 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1736 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001737}
1738
1739status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001740 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1741 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001742}
1743
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001744status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
1745 ui::DisplayState* state) {
1746 return ComposerService::getComposerService()->getDisplayState(display, state);
1747}
1748
1749status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
1750 return ComposerService::getComposerService()->getDisplayInfo(display, info);
1751}
1752
1753status_t SurfaceComposerClient::getDisplayConfigs(const sp<IBinder>& display,
1754 Vector<DisplayConfig>* configs) {
Dan Stoza7f7da322014-05-02 15:26:25 -07001755 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1756}
1757
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001758status_t SurfaceComposerClient::getActiveDisplayConfig(const sp<IBinder>& display,
1759 DisplayConfig* config) {
1760 Vector<DisplayConfig> configs;
Dan Stoza7f7da322014-05-02 15:26:25 -07001761 status_t result = getDisplayConfigs(display, &configs);
1762 if (result != NO_ERROR) {
1763 return result;
1764 }
1765
1766 int activeId = getActiveConfig(display);
1767 if (activeId < 0) {
1768 ALOGE("No active configuration found");
1769 return NAME_NOT_FOUND;
1770 }
1771
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001772 *config = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001773 return NO_ERROR;
1774}
1775
1776int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1777 return ComposerService::getComposerService()->getActiveConfig(display);
1778}
1779
Ana Krulec0782b882019-10-15 17:34:54 -07001780status_t SurfaceComposerClient::setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001781 int32_t defaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001782 float primaryRefreshRateMin,
1783 float primaryRefreshRateMax,
1784 float appRequestRefreshRateMin,
1785 float appRequestRefreshRateMax) {
1786 return ComposerService::getComposerService()
1787 ->setDesiredDisplayConfigSpecs(displayToken, defaultConfig, primaryRefreshRateMin,
1788 primaryRefreshRateMax, appRequestRefreshRateMin,
1789 appRequestRefreshRateMax);
Ana Krulec0782b882019-10-15 17:34:54 -07001790}
1791
Ana Krulec234bb162019-11-10 22:55:55 +01001792status_t SurfaceComposerClient::getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001793 int32_t* outDefaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001794 float* outPrimaryRefreshRateMin,
1795 float* outPrimaryRefreshRateMax,
1796 float* outAppRequestRefreshRateMin,
1797 float* outAppRequestRefreshRateMax) {
1798 return ComposerService::getComposerService()
1799 ->getDesiredDisplayConfigSpecs(displayToken, outDefaultConfig, outPrimaryRefreshRateMin,
1800 outPrimaryRefreshRateMax, outAppRequestRefreshRateMin,
1801 outAppRequestRefreshRateMax);
Ana Krulec234bb162019-11-10 22:55:55 +01001802}
1803
Michael Wright28f24d02016-07-12 13:30:53 -07001804status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001805 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001806 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1807}
1808
Daniel Solomon42d04562019-01-20 21:03:19 -08001809status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1810 ui::DisplayPrimaries& outPrimaries) {
1811 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1812}
1813
Peiyong Lina52f0292018-03-14 17:26:31 -07001814ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001815 return ComposerService::getComposerService()->getActiveColorMode(display);
1816}
1817
1818status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001819 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001820 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1821}
1822
Galia Peycheva5492cb52019-10-30 14:13:16 +01001823bool SurfaceComposerClient::getAutoLowLatencyModeSupport(const sp<IBinder>& display) {
1824 bool supported = false;
1825 ComposerService::getComposerService()->getAutoLowLatencyModeSupport(display, &supported);
1826 return supported;
1827}
1828
1829void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
1830 ComposerService::getComposerService()->setAutoLowLatencyMode(display, on);
1831}
1832
1833bool SurfaceComposerClient::getGameContentTypeSupport(const sp<IBinder>& display) {
1834 bool supported = false;
1835 ComposerService::getComposerService()->getGameContentTypeSupport(display, &supported);
1836 return supported;
1837}
1838
1839void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
1840 ComposerService::getComposerService()->setGameContentType(display, on);
1841}
1842
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001843void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1844 int mode) {
1845 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001846}
1847
Peiyong Linc6780972018-10-28 15:24:08 -07001848status_t SurfaceComposerClient::getCompositionPreference(
1849 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1850 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1851 return ComposerService::getComposerService()
1852 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1853 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001854}
1855
Peiyong Lin08d10512019-01-16 13:27:35 -08001856bool SurfaceComposerClient::getProtectedContentSupport() {
1857 bool supported = false;
1858 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1859 return supported;
1860}
1861
Svetoslavd85084b2014-03-20 10:28:31 -07001862status_t SurfaceComposerClient::clearAnimationFrameStats() {
1863 return ComposerService::getComposerService()->clearAnimationFrameStats();
1864}
1865
1866status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1867 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1868}
1869
Dan Stozac4f471e2016-03-24 09:31:08 -07001870status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1871 HdrCapabilities* outCapabilities) {
1872 return ComposerService::getComposerService()->getHdrCapabilities(display,
1873 outCapabilities);
1874}
1875
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001876status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1877 ui::PixelFormat* outFormat,
1878 ui::Dataspace* outDataspace,
1879 uint8_t* outComponentMask) {
1880 return ComposerService::getComposerService()
1881 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1882 outComponentMask);
1883}
1884
Kevin DuBois74e53772018-11-19 10:52:38 -08001885status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1886 bool enable, uint8_t componentMask,
1887 uint64_t maxFrames) {
1888 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1889 componentMask,
1890 maxFrames);
1891}
1892
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001893status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1894 uint64_t maxFrames, uint64_t timestamp,
1895 DisplayedFrameStats* outStats) {
1896 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1897 timestamp, outStats);
1898}
Marissa Wall35187b32019-01-08 10:08:52 -08001899
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001900status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1901 bool* outIsWideColorDisplay) {
1902 return ComposerService::getComposerService()->isWideColorDisplay(display,
1903 outIsWideColorDisplay);
1904}
1905
Kevin DuBois00c66832019-02-18 16:21:31 -08001906status_t SurfaceComposerClient::addRegionSamplingListener(
1907 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1908 const sp<IRegionSamplingListener>& listener) {
1909 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1910 stopLayerHandle,
1911 listener);
1912}
1913
1914status_t SurfaceComposerClient::removeRegionSamplingListener(
1915 const sp<IRegionSamplingListener>& listener) {
1916 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1917}
1918
Dan Gittik57e63c52019-01-18 16:37:54 +00001919bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1920 bool support = false;
1921 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1922 return support;
1923}
1924
1925status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1926 float brightness) {
1927 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1928}
1929
Lais Andrade3a6e47d2020-04-02 11:20:16 +01001930status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
1931 return ComposerService::getComposerService()->notifyPowerBoost(boostId);
Ady Abraham8532d012019-05-08 14:50:56 -07001932}
1933
Vishnu Nairb13bb952019-11-15 10:24:08 -08001934status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
1935 const half4& spotColor, float lightPosY,
1936 float lightPosZ, float lightRadius) {
1937 return ComposerService::getComposerService()->setGlobalShadowSettings(ambientColor, spotColor,
1938 lightPosY, lightPosZ,
1939 lightRadius);
1940}
1941
Mathias Agopian698c0872011-06-28 19:09:31 -07001942// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943
chaviw690db382020-07-27 16:46:46 -07001944status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07001945 const sp<IScreenCaptureListener>& captureListener) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001946 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001947 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001948
chaviwe7b9f272020-08-18 16:08:59 -07001949 return s->captureDisplay(captureArgs, captureListener);
Robert Carr673134e2017-01-09 19:48:38 -08001950}
1951
chaviw690db382020-07-27 16:46:46 -07001952status_t ScreenshotClient::captureDisplay(uint64_t displayOrLayerStack,
chaviwe7b9f272020-08-18 16:08:59 -07001953 const sp<IScreenCaptureListener>& captureListener) {
chaviw93df2ea2019-04-30 16:45:12 -07001954 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1955 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001956
chaviwe7b9f272020-08-18 16:08:59 -07001957 return s->captureDisplay(displayOrLayerStack, captureListener);
chaviw93df2ea2019-04-30 16:45:12 -07001958}
1959
chaviw26c52482020-07-28 16:25:52 -07001960status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07001961 const sp<IScreenCaptureListener>& captureListener) {
chaviwa76b2712017-09-20 12:02:26 -07001962 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001963 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001964
chaviwe7b9f272020-08-18 16:08:59 -07001965 return s->captureLayers(captureArgs, captureListener);
chaviwa76b2712017-09-20 12:02:26 -07001966}
Dominik Laskowski718f9602019-11-09 20:01:35 -08001967
1968} // namespace android