blob: f24dfca6801691557a9ba7630aadc702dc2c34f9 [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
Marissa Wall17b4e452018-12-26 16:32:34 -0800351SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
352 : mForceSynchronous(other.mForceSynchronous),
353 mTransactionNestCount(other.mTransactionNestCount),
354 mAnimation(other.mAnimation),
355 mEarlyWakeup(other.mEarlyWakeup),
Ady Abrahambf1349c2020-06-12 14:26:18 -0700356 mExplicitEarlyWakeupStart(other.mExplicitEarlyWakeupStart),
357 mExplicitEarlyWakeupEnd(other.mExplicitEarlyWakeupEnd),
Vishnu Nair621102e2019-06-12 14:16:57 -0700358 mContainsBuffer(other.mContainsBuffer),
Marissa Wall17b4e452018-12-26 16:32:34 -0800359 mDesiredPresentTime(other.mDesiredPresentTime) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700360 mDisplayStates = other.mDisplayStates;
361 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800362 mInputWindowCommands = other.mInputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700363 mListenerCallbacks = other.mListenerCallbacks;
364}
365
366std::unique_ptr<SurfaceComposerClient::Transaction>
367SurfaceComposerClient::Transaction::createFromParcel(const Parcel* parcel) {
368 auto transaction = std::make_unique<Transaction>();
369 if (transaction->readFromParcel(parcel) == NO_ERROR) {
370 return transaction;
371 }
372 return nullptr;
373}
374
375status_t SurfaceComposerClient::Transaction::readFromParcel(const Parcel* parcel) {
376 const uint32_t forceSynchronous = parcel->readUint32();
377 const uint32_t transactionNestCount = parcel->readUint32();
378 const bool animation = parcel->readBool();
379 const bool earlyWakeup = parcel->readBool();
Ady Abrahambf1349c2020-06-12 14:26:18 -0700380 const bool explicitEarlyWakeupStart = parcel->readBool();
381 const bool explicitEarlyWakeupEnd = parcel->readBool();
Vishnu Nair621102e2019-06-12 14:16:57 -0700382 const bool containsBuffer = parcel->readBool();
383 const int64_t desiredPresentTime = parcel->readInt64();
384
385 size_t count = static_cast<size_t>(parcel->readUint32());
386 if (count > parcel->dataSize()) {
387 return BAD_VALUE;
388 }
389 SortedVector<DisplayState> displayStates;
390 displayStates.setCapacity(count);
391 for (size_t i = 0; i < count; i++) {
392 DisplayState displayState;
393 if (displayState.read(*parcel) == BAD_VALUE) {
394 return BAD_VALUE;
395 }
396 displayStates.add(displayState);
397 }
398
399 count = static_cast<size_t>(parcel->readUint32());
400 if (count > parcel->dataSize()) {
401 return BAD_VALUE;
402 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700403 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> listenerCallbacks;
404 listenerCallbacks.reserve(count);
405 for (size_t i = 0; i < count; i++) {
406 sp<ITransactionCompletedListener> listener =
407 interface_cast<ITransactionCompletedListener>(parcel->readStrongBinder());
408 size_t numCallbackIds = parcel->readUint32();
409 if (numCallbackIds > parcel->dataSize()) {
410 return BAD_VALUE;
411 }
412 for (size_t j = 0; j < numCallbackIds; j++) {
413 listenerCallbacks[listener].callbackIds.insert(parcel->readInt64());
414 }
415 size_t numSurfaces = parcel->readUint32();
416 if (numSurfaces > parcel->dataSize()) {
417 return BAD_VALUE;
418 }
419 for (size_t j = 0; j < numSurfaces; j++) {
420 sp<SurfaceControl> surface;
421 surface = SurfaceControl::readFromParcel(parcel);
422 listenerCallbacks[listener].surfaceControls.insert(surface);
423 }
424 }
425
426 count = static_cast<size_t>(parcel->readUint32());
427 if (count > parcel->dataSize()) {
428 return BAD_VALUE;
429 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700430 std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> composerStates;
Vishnu Nair621102e2019-06-12 14:16:57 -0700431 composerStates.reserve(count);
432 for (size_t i = 0; i < count; i++) {
Vishnu Nairf03652d2019-07-16 17:56:56 -0700433 sp<IBinder> surfaceControlHandle = parcel->readStrongBinder();
Vishnu Nair621102e2019-06-12 14:16:57 -0700434
435 ComposerState composerState;
436 if (composerState.read(*parcel) == BAD_VALUE) {
437 return BAD_VALUE;
438 }
Vishnu Nairf03652d2019-07-16 17:56:56 -0700439 composerStates[surfaceControlHandle] = composerState;
Vishnu Nair621102e2019-06-12 14:16:57 -0700440 }
441
442 InputWindowCommands inputWindowCommands;
443 inputWindowCommands.read(*parcel);
444
445 // Parsing was successful. Update the object.
446 mForceSynchronous = forceSynchronous;
447 mTransactionNestCount = transactionNestCount;
448 mAnimation = animation;
449 mEarlyWakeup = earlyWakeup;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700450 mExplicitEarlyWakeupStart = explicitEarlyWakeupStart;
451 mExplicitEarlyWakeupEnd = explicitEarlyWakeupEnd;
Vishnu Nair621102e2019-06-12 14:16:57 -0700452 mContainsBuffer = containsBuffer;
453 mDesiredPresentTime = desiredPresentTime;
454 mDisplayStates = displayStates;
Valerie Hau9dab9732019-08-20 09:29:25 -0700455 mListenerCallbacks = listenerCallbacks;
Vishnu Nair621102e2019-06-12 14:16:57 -0700456 mComposerStates = composerStates;
457 mInputWindowCommands = inputWindowCommands;
Vishnu Nair621102e2019-06-12 14:16:57 -0700458 return NO_ERROR;
459}
460
461status_t SurfaceComposerClient::Transaction::writeToParcel(Parcel* parcel) const {
Robert Carr158531d2020-04-08 10:53:30 -0700462 // If we write the Transaction to a parcel, we want to ensure the Buffers are cached
463 // before crossing the IPC boundary. Otherwise the receiving party will cache the buffers
464 // but is unlikely to use them again as they are owned by the other process.
465 // You may be asking yourself, is this const cast safe? Const cast is safe up
466 // until the point where you try and write to an object that was originally const at which
467 // point we enter undefined behavior. In this case we are safe though, because there are
468 // two possibilities:
469 // 1. The SurfaceComposerClient::Transaction was originally non-const. Safe.
470 // 2. It was originall const! In this case not only was it useless, but it by definition
471 // contains no composer states and so cacheBuffers will not perform any writes.
472
473 const_cast<SurfaceComposerClient::Transaction*>(this)->cacheBuffers();
474
Vishnu Nair621102e2019-06-12 14:16:57 -0700475 parcel->writeUint32(mForceSynchronous);
476 parcel->writeUint32(mTransactionNestCount);
477 parcel->writeBool(mAnimation);
478 parcel->writeBool(mEarlyWakeup);
Ady Abrahambf1349c2020-06-12 14:26:18 -0700479 parcel->writeBool(mExplicitEarlyWakeupStart);
480 parcel->writeBool(mExplicitEarlyWakeupEnd);
Vishnu Nair621102e2019-06-12 14:16:57 -0700481 parcel->writeBool(mContainsBuffer);
482 parcel->writeInt64(mDesiredPresentTime);
483 parcel->writeUint32(static_cast<uint32_t>(mDisplayStates.size()));
484 for (auto const& displayState : mDisplayStates) {
485 displayState.write(*parcel);
486 }
487
Valerie Hau9dab9732019-08-20 09:29:25 -0700488 parcel->writeUint32(static_cast<uint32_t>(mListenerCallbacks.size()));
489 for (auto const& [listener, callbackInfo] : mListenerCallbacks) {
490 parcel->writeStrongBinder(ITransactionCompletedListener::asBinder(listener));
491 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.callbackIds.size()));
492 for (auto callbackId : callbackInfo.callbackIds) {
493 parcel->writeInt64(callbackId);
494 }
495 parcel->writeUint32(static_cast<uint32_t>(callbackInfo.surfaceControls.size()));
496 for (auto surfaceControl : callbackInfo.surfaceControls) {
497 surfaceControl->writeToParcel(parcel);
498 }
499 }
500
Vishnu Nair621102e2019-06-12 14:16:57 -0700501 parcel->writeUint32(static_cast<uint32_t>(mComposerStates.size()));
Vishnu Nairf03652d2019-07-16 17:56:56 -0700502 for (auto const& [surfaceHandle, composerState] : mComposerStates) {
503 parcel->writeStrongBinder(surfaceHandle);
Vishnu Nair621102e2019-06-12 14:16:57 -0700504 composerState.write(*parcel);
505 }
506
507 mInputWindowCommands.write(*parcel);
508 return NO_ERROR;
Mathias Agopian698c0872011-06-28 19:09:31 -0700509}
510
Robert Carr2c5f6d22017-09-26 12:30:35 -0700511SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
Vishnu Nairf03652d2019-07-16 17:56:56 -0700512 for (auto const& [surfaceHandle, composerState] : other.mComposerStates) {
513 if (mComposerStates.count(surfaceHandle) == 0) {
514 mComposerStates[surfaceHandle] = composerState;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700515 } else {
Vishnu Nairf03652d2019-07-16 17:56:56 -0700516 mComposerStates[surfaceHandle].state.merge(composerState.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700517 }
518 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700519
520 for (auto const& state : other.mDisplayStates) {
521 ssize_t index = mDisplayStates.indexOf(state);
522 if (index < 0) {
523 mDisplayStates.add(state);
524 } else {
525 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
526 }
527 }
Robert Carr2c5f6d22017-09-26 12:30:35 -0700528
Marissa Wallc837b5e2018-10-12 10:04:44 -0700529 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
530 auto& [callbackIds, surfaceControls] = callbackInfo;
531 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
532 callbackIds.begin()),
533 std::make_move_iterator(callbackIds.end()));
Valerie Hau9dab9732019-08-20 09:29:25 -0700534
Valerie Hau236eba32020-01-03 16:53:39 -0800535 mListenerCallbacks[listener].surfaceControls.insert(surfaceControls.begin(),
536 surfaceControls.end());
537
538 auto& currentProcessCallbackInfo =
539 mListenerCallbacks[TransactionCompletedListener::getIInstance()];
540 currentProcessCallbackInfo.surfaceControls
541 .insert(std::make_move_iterator(surfaceControls.begin()),
542 std::make_move_iterator(surfaceControls.end()));
543
544 // register all surface controls for all callbackIds for this listener that is merging
545 for (const auto& surfaceControl : currentProcessCallbackInfo.surfaceControls) {
546 TransactionCompletedListener::getInstance()
547 ->addSurfaceControlToCallbacks(surfaceControl,
548 currentProcessCallbackInfo.callbackIds);
549 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700550 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700551
chaviw273171b2018-12-26 11:46:30 -0800552 mInputWindowCommands.merge(other.mInputWindowCommands);
553
Robert Carrbbc85622020-04-08 10:45:12 -0700554 mContainsBuffer |= other.mContainsBuffer;
Jorim Jaggie3b57002019-07-22 17:18:52 +0200555 mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700556 mExplicitEarlyWakeupStart = mExplicitEarlyWakeupStart || other.mExplicitEarlyWakeupStart;
557 mExplicitEarlyWakeupEnd = mExplicitEarlyWakeupEnd || other.mExplicitEarlyWakeupEnd;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700558 other.clear();
Robert Carr2c5f6d22017-09-26 12:30:35 -0700559 return *this;
560}
561
Vishnu Nairfef244e2019-06-17 18:07:51 -0700562void SurfaceComposerClient::Transaction::clear() {
563 mComposerStates.clear();
564 mDisplayStates.clear();
565 mListenerCallbacks.clear();
566 mInputWindowCommands.clear();
567 mContainsBuffer = false;
568 mForceSynchronous = 0;
569 mTransactionNestCount = 0;
570 mAnimation = false;
571 mEarlyWakeup = false;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700572 mExplicitEarlyWakeupStart = false;
573 mExplicitEarlyWakeupEnd = false;
Vishnu Nairfef244e2019-06-17 18:07:51 -0700574 mDesiredPresentTime = -1;
575}
576
Marissa Wall78b72202019-03-15 14:58:34 -0700577void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
578 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
579
Marissa Wall947d34e2019-03-29 14:03:53 -0700580 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -0700581 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700582 uncacheBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700583
Valerie Haufa889122019-04-15 13:56:05 -0700584 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Valerie Hau9dab9732019-08-20 09:29:25 -0700585 sf->setTransactionState({}, {}, 0, applyToken, {}, -1, uncacheBuffer, false, {});
Marissa Wall78b72202019-03-15 14:58:34 -0700586}
587
588void SurfaceComposerClient::Transaction::cacheBuffers() {
589 if (!mContainsBuffer) {
590 return;
591 }
592
593 size_t count = 0;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700594 for (auto& [handle, cs] : mComposerStates) {
595 layer_state_t* s = getLayerState(handle);
Marissa Wall78b72202019-03-15 14:58:34 -0700596 if (!(s->what & layer_state_t::eBufferChanged)) {
597 continue;
Robert Carr28037922020-04-08 10:57:07 -0700598 } else if (s->what & layer_state_t::eCachedBufferChanged) {
599 // If eBufferChanged and eCachedBufferChanged are both trued then that means
600 // we already cached the buffer in a previous call to cacheBuffers, perhaps
601 // from writeToParcel on a Transaction that was merged in to this one.
602 continue;
Marissa Wall78b72202019-03-15 14:58:34 -0700603 }
604
Marissa Wall00597242019-03-27 10:35:19 -0700605 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
606 // time trying to cache them.
607 if (!s->buffer) {
608 continue;
609 }
610
Marissa Wall78b72202019-03-15 14:58:34 -0700611 uint64_t cacheId = 0;
612 status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId);
613 if (ret == NO_ERROR) {
Robert Carre06ad2b2020-04-10 15:09:33 -0700614 // Cache-hit. Strip the buffer and send only the id.
Marissa Walla141abe2019-03-27 16:28:07 -0700615 s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
Marissa Wall78b72202019-03-15 14:58:34 -0700616 s->buffer = nullptr;
617 } else {
Robert Carre06ad2b2020-04-10 15:09:33 -0700618 // Cache-miss. Include the buffer and send the new cacheId.
Marissa Wall78b72202019-03-15 14:58:34 -0700619 cacheId = BufferCache::getInstance().cache(s->buffer);
620 }
621 s->what |= layer_state_t::eCachedBufferChanged;
622 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700623 s->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700624
625 // If we have more buffers than the size of the cache, we should stop caching so we don't
626 // evict other buffers in this transaction
627 count++;
628 if (count >= BUFFER_CACHE_MAX_SIZE) {
629 break;
630 }
631 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800632}
633
Robert Carr4cdc58f2017-08-23 14:22:20 -0700634status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
635 if (mStatus != NO_ERROR) {
636 return mStatus;
637 }
638
639 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
640
Valerie Hau9dab9732019-08-20 09:29:25 -0700641 bool hasListenerCallbacks = !mListenerCallbacks.empty();
Marissa Wall3dad52d2019-03-22 14:03:19 -0700642 std::vector<ListenerCallbacks> listenerCallbacks;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700643 // For every listener with registered callbacks
644 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
645 auto& [callbackIds, surfaceControls] = callbackInfo;
646 if (callbackIds.empty()) {
647 continue;
648 }
649
Valerie Hau9dab9732019-08-20 09:29:25 -0700650 if (surfaceControls.empty()) {
651 listenerCallbacks.emplace_back(IInterface::asBinder(listener), std::move(callbackIds));
652 } else {
653 // If the listener has any SurfaceControls set on this Transaction update the surface
654 // state
655 for (const auto& surfaceControl : surfaceControls) {
656 layer_state_t* s = getLayerState(surfaceControl);
657 if (!s) {
658 ALOGE("failed to get layer state");
659 continue;
660 }
661 std::vector<CallbackId> callbacks(callbackIds.begin(), callbackIds.end());
662 s->what |= layer_state_t::eHasListenerCallbacksChanged;
663 s->listeners.emplace_back(IInterface::asBinder(listener), callbacks);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700664 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700665 }
666 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700667
Marissa Wallc837b5e2018-10-12 10:04:44 -0700668 mListenerCallbacks.clear();
669
Marissa Wall78b72202019-03-15 14:58:34 -0700670 cacheBuffers();
671
Robert Carr4cdc58f2017-08-23 14:22:20 -0700672 Vector<ComposerState> composerStates;
673 Vector<DisplayState> displayStates;
674 uint32_t flags = 0;
675
676 mForceSynchronous |= synchronous;
677
chaviw8e3fe5d2018-02-22 10:55:42 -0800678 for (auto const& kv : mComposerStates){
679 composerStates.add(kv.second);
680 }
681
Robert Carr4cdc58f2017-08-23 14:22:20 -0700682 mComposerStates.clear();
683
684 displayStates = mDisplayStates;
685 mDisplayStates.clear();
686
687 if (mForceSynchronous) {
688 flags |= ISurfaceComposer::eSynchronous;
689 }
690 if (mAnimation) {
691 flags |= ISurfaceComposer::eAnimation;
692 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700693 if (mEarlyWakeup) {
694 flags |= ISurfaceComposer::eEarlyWakeup;
695 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700696
Ady Abrahambf1349c2020-06-12 14:26:18 -0700697 // If both mExplicitEarlyWakeupStart and mExplicitEarlyWakeupEnd are set
698 // it is equivalent for none
699 if (mExplicitEarlyWakeupStart && !mExplicitEarlyWakeupEnd) {
700 flags |= ISurfaceComposer::eExplicitEarlyWakeupStart;
701 }
702 if (mExplicitEarlyWakeupEnd && !mExplicitEarlyWakeupStart) {
703 flags |= ISurfaceComposer::eExplicitEarlyWakeupEnd;
704 }
705
Robert Carr4cdc58f2017-08-23 14:22:20 -0700706 mForceSynchronous = false;
707 mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700708 mEarlyWakeup = false;
Ady Abrahambf1349c2020-06-12 14:26:18 -0700709 mExplicitEarlyWakeupStart = false;
710 mExplicitEarlyWakeupEnd = false;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700711
Marissa Wall713b63f2018-10-17 15:42:43 -0700712 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Marissa Wall17b4e452018-12-26 16:32:34 -0800713 sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands,
Marissa Wall78b72202019-03-15 14:58:34 -0700714 mDesiredPresentTime,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700715 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
Valerie Hau9dab9732019-08-20 09:29:25 -0700716 hasListenerCallbacks, listenerCallbacks);
chaviw273171b2018-12-26 11:46:30 -0800717 mInputWindowCommands.clear();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700718 mStatus = NO_ERROR;
719 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700720}
721
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722// ---------------------------------------------------------------------------
723
Robert Carr4cdc58f2017-08-23 14:22:20 -0700724sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700725 return ComposerService::getComposerService()->createDisplay(displayName,
726 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700727}
728
Robert Carr4cdc58f2017-08-23 14:22:20 -0700729void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700730 return ComposerService::getComposerService()->destroyDisplay(display);
731}
732
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800733std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
734 return ComposerService::getComposerService()->getPhysicalDisplayIds();
735}
736
737std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
738 return ComposerService::getComposerService()->getInternalDisplayId();
739}
740
741sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
742 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
743}
744
745sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
746 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700747}
748
Robert Carr4cdc58f2017-08-23 14:22:20 -0700749void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700750 mAnimation = true;
751}
752
Dan Stoza84d619e2018-03-28 17:07:36 -0700753void SurfaceComposerClient::Transaction::setEarlyWakeup() {
754 mEarlyWakeup = true;
755}
756
Ady Abrahambf1349c2020-06-12 14:26:18 -0700757void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupStart() {
758 mExplicitEarlyWakeupStart = true;
759}
760
761void SurfaceComposerClient::Transaction::setExplicitEarlyWakeupEnd() {
762 mExplicitEarlyWakeupEnd = true;
763}
764
Vishnu Nairf03652d2019-07-16 17:56:56 -0700765layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<IBinder>& handle) {
766 if (mComposerStates.count(handle) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700767 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -0800768 ComposerState s;
Vishnu Nairf03652d2019-07-16 17:56:56 -0700769 s.state.surface = handle;
770 mComposerStates[handle] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -0700771 }
772
Vishnu Nairf03652d2019-07-16 17:56:56 -0700773 return &(mComposerStates[handle].state);
Mathias Agopian698c0872011-06-28 19:09:31 -0700774}
775
Marissa Wallc837b5e2018-10-12 10:04:44 -0700776void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
777 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800778 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
779 callbackInfo.surfaceControls.insert(sc);
780
781 TransactionCompletedListener::getInstance()
782 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700783}
784
Robert Carr4cdc58f2017-08-23 14:22:20 -0700785SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
786 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -0800787 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700788 if (!s) {
789 mStatus = BAD_INDEX;
790 return *this;
791 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700792 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700793 s->x = x;
794 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700795
796 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700797 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700798}
799
Robert Carr4cdc58f2017-08-23 14:22:20 -0700800SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
801 const sp<SurfaceControl>& sc) {
802 return setFlags(sc, 0, layer_state_t::eLayerHidden);
803}
804
805SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
806 const sp<SurfaceControl>& sc) {
807 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
808}
809
810SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
811 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-22 16:04:57 -0800812 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700813 if (!s) {
814 mStatus = BAD_INDEX;
815 return *this;
816 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700817 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700818 s->w = w;
819 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700820
Marissa Wallc837b5e2018-10-12 10:04:44 -0700821 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700822 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700823}
824
Robert Carr4cdc58f2017-08-23 14:22:20 -0700825SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
826 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800827 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700828 if (!s) {
829 mStatus = BAD_INDEX;
830 return *this;
831 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700832 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700833 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700834 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700835
836 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700837 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700838}
839
Robert Carr4cdc58f2017-08-23 14:22:20 -0700840SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
Robert Carrdb66e622017-04-10 16:55:57 -0700841 int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800842 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700843 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700844 mStatus = BAD_INDEX;
Robert Carr30c8d902019-04-04 13:12:49 -0700845 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700846 }
847 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700848 s->what &= ~layer_state_t::eLayerChanged;
Robert Carrdb66e622017-04-10 16:55:57 -0700849 s->relativeLayerHandle = relativeTo;
850 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700851
852 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700853 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700854}
855
Robert Carr4cdc58f2017-08-23 14:22:20 -0700856SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
857 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700858 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -0800859 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700860 if (!s) {
861 mStatus = BAD_INDEX;
862 return *this;
863 }
chaviwc5676c62020-09-18 15:01:04 -0700864 if ((mask & layer_state_t::eLayerOpaque) || (mask & layer_state_t::eLayerHidden) ||
865 (mask & layer_state_t::eLayerSecure) || (mask & layer_state_t::eLayerSkipScreenshot)) {
Dan Stoza23116082015-06-18 14:58:39 -0700866 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800867 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700868 s->flags &= ~mask;
869 s->flags |= (flags & mask);
870 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700871
872 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700873 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700874}
875
Robert Carr4cdc58f2017-08-23 14:22:20 -0700876SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
877 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700878 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800879 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700880 if (!s) {
881 mStatus = BAD_INDEX;
882 return *this;
883 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700884 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700885 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700886
887 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700888 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700889}
890
Robert Carr4cdc58f2017-08-23 14:22:20 -0700891SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
892 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800893 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700894 if (!s) {
895 mStatus = BAD_INDEX;
896 return *this;
897 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700898 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700899 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700900
901 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700902 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700903}
904
Robert Carr4cdc58f2017-08-23 14:22:20 -0700905SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
906 const sp<SurfaceControl>& sc, uint32_t layerStack) {
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::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700913 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700914
915 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700916 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700917}
918
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800919SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -0700920 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800921 layer_state_t* s = getLayerState(sc);
922 if (!s) {
923 mStatus = BAD_INDEX;
924 return *this;
925 }
926 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -0700927
928 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800929
930 registerSurfaceControlForCallback(sc);
931 return *this;
932}
933
Robert Carr4cdc58f2017-08-23 14:22:20 -0700934SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
935 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800936 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800937 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700938 if (!s) {
939 mStatus = BAD_INDEX;
940 return *this;
941 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700942 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700943 layer_state_t::matrix22_t matrix;
944 matrix.dsdx = dsdx;
945 matrix.dtdx = dtdx;
946 matrix.dsdy = dsdy;
947 matrix.dtdy = dtdy;
948 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700949
950 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700951 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700952}
953
Marissa Wallf58c14b2018-07-24 10:50:43 -0700954SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700955 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800956 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700957 if (!s) {
958 mStatus = BAD_INDEX;
959 return *this;
960 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700961 s->what |= layer_state_t::eCropChanged_legacy;
962 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700963
964 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700965 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700966}
967
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700968SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
969 const sp<SurfaceControl>& sc, float cornerRadius) {
970 layer_state_t* s = getLayerState(sc);
971 if (!s) {
972 mStatus = BAD_INDEX;
973 return *this;
974 }
975 s->what |= layer_state_t::eCornerRadiusChanged;
976 s->cornerRadius = cornerRadius;
977 return *this;
978}
979
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800980SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
981 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
982 layer_state_t* s = getLayerState(sc);
983 if (!s) {
984 mStatus = BAD_INDEX;
985 return *this;
986 }
987 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
988 s->backgroundBlurRadius = backgroundBlurRadius;
989 return *this;
990}
991
Marissa Wallf58c14b2018-07-24 10:50:43 -0700992SurfaceComposerClient::Transaction&
993SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
994 const sp<IBinder>& handle,
995 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800996 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700997 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700998 mStatus = BAD_INDEX;
999 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001001 s->what |= layer_state_t::eDeferTransaction_legacy;
1002 s->barrierHandle_legacy = handle;
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001003 s->barrierFrameNumber = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001004
1005 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001006 return *this;
Robert Carr0d480722017-01-10 16:42:54 -08001007}
1008
Marissa Wallf58c14b2018-07-24 10:50:43 -07001009SurfaceComposerClient::Transaction&
1010SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
1011 const sp<Surface>& barrierSurface,
1012 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -08001013 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -08001014 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001015 mStatus = BAD_INDEX;
1016 return *this;
Robert Carr0d480722017-01-10 16:42:54 -08001017 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001018 s->what |= layer_state_t::eDeferTransaction_legacy;
1019 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001020 s->barrierFrameNumber = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001021
1022 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001023 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -07001024}
1025
Robert Carr4cdc58f2017-08-23 14:22:20 -07001026SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
1027 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -08001028 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -08001029 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -08001030 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001031 mStatus = BAD_INDEX;
1032 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001033 }
1034 s->what |= layer_state_t::eReparentChildren;
1035 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001036
1037 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001038 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001039}
1040
Robert Carr4cdc58f2017-08-23 14:22:20 -07001041SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
1042 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -07001043 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -08001044 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001045 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001046 mStatus = BAD_INDEX;
1047 return *this;
chaviw06178942017-07-27 10:25:59 -07001048 }
chaviwf1961f72017-09-18 16:41:07 -07001049 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -07001050 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001051
1052 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001053 return *this;
chaviw06178942017-07-27 10:25:59 -07001054}
1055
Robert Carr4cdc58f2017-08-23 14:22:20 -07001056SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1057 const sp<SurfaceControl>& sc,
1058 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001059 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001060 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001061 mStatus = BAD_INDEX;
1062 return *this;
1063 }
1064 s->what |= layer_state_t::eColorChanged;
1065 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001066
1067 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001068 return *this;
1069}
1070
Valerie Haudd0b7572019-01-29 14:59:27 -08001071SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1072 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001073 layer_state_t* s = getLayerState(sc);
1074 if (!s) {
1075 mStatus = BAD_INDEX;
1076 return *this;
1077 }
1078
Valerie Haudd0b7572019-01-29 14:59:27 -08001079 s->what |= layer_state_t::eBackgroundColorChanged;
1080 s->color = color;
1081 s->bgColorAlpha = alpha;
1082 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001083
1084 registerSurfaceControlForCallback(sc);
1085 return *this;
1086}
1087
Marissa Wall61c58622018-07-18 10:12:20 -07001088SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1089 const sp<SurfaceControl>& sc, uint32_t transform) {
1090 layer_state_t* s = getLayerState(sc);
1091 if (!s) {
1092 mStatus = BAD_INDEX;
1093 return *this;
1094 }
1095 s->what |= layer_state_t::eTransformChanged;
1096 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001097
1098 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001099 return *this;
1100}
1101
1102SurfaceComposerClient::Transaction&
1103SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1104 bool transformToDisplayInverse) {
1105 layer_state_t* s = getLayerState(sc);
1106 if (!s) {
1107 mStatus = BAD_INDEX;
1108 return *this;
1109 }
1110 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1111 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001112
1113 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001114 return *this;
1115}
1116
1117SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
1118 const sp<SurfaceControl>& sc, const Rect& crop) {
1119 layer_state_t* s = getLayerState(sc);
1120 if (!s) {
1121 mStatus = BAD_INDEX;
1122 return *this;
1123 }
1124 s->what |= layer_state_t::eCropChanged;
1125 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001126
1127 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001128 return *this;
1129}
1130
Marissa Wall861616d2018-10-22 12:52:23 -07001131SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
1132 const sp<SurfaceControl>& sc, const Rect& frame) {
1133 layer_state_t* s = getLayerState(sc);
1134 if (!s) {
1135 mStatus = BAD_INDEX;
1136 return *this;
1137 }
1138 s->what |= layer_state_t::eFrameChanged;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001139 s->orientedDisplaySpaceRect = frame;
Marissa Wall861616d2018-10-22 12:52:23 -07001140
1141 registerSurfaceControlForCallback(sc);
1142 return *this;
1143}
1144
Marissa Wall61c58622018-07-18 10:12:20 -07001145SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
1146 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
1147 layer_state_t* s = getLayerState(sc);
1148 if (!s) {
1149 mStatus = BAD_INDEX;
1150 return *this;
1151 }
Marissa Wall78b72202019-03-15 14:58:34 -07001152 s->what |= layer_state_t::eBufferChanged;
1153 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -08001154
1155 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001156
1157 mContainsBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001158 return *this;
1159}
1160
Marissa Wall61c58622018-07-18 10:12:20 -07001161SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
1162 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
1163 layer_state_t* s = getLayerState(sc);
1164 if (!s) {
1165 mStatus = BAD_INDEX;
1166 return *this;
1167 }
1168 s->what |= layer_state_t::eAcquireFenceChanged;
1169 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001170
1171 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001172 return *this;
1173}
1174
1175SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1176 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1177 layer_state_t* s = getLayerState(sc);
1178 if (!s) {
1179 mStatus = BAD_INDEX;
1180 return *this;
1181 }
1182 s->what |= layer_state_t::eDataspaceChanged;
1183 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001184
1185 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001186 return *this;
1187}
1188
1189SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1190 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1191 layer_state_t* s = getLayerState(sc);
1192 if (!s) {
1193 mStatus = BAD_INDEX;
1194 return *this;
1195 }
1196 s->what |= layer_state_t::eHdrMetadataChanged;
1197 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001198
1199 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001200 return *this;
1201}
1202
1203SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1204 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1205 layer_state_t* s = getLayerState(sc);
1206 if (!s) {
1207 mStatus = BAD_INDEX;
1208 return *this;
1209 }
1210 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1211 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001212
1213 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001214 return *this;
1215}
1216
1217SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1218 const sp<SurfaceControl>& sc, int32_t api) {
1219 layer_state_t* s = getLayerState(sc);
1220 if (!s) {
1221 mStatus = BAD_INDEX;
1222 return *this;
1223 }
1224 s->what |= layer_state_t::eApiChanged;
1225 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001226
1227 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001228 return *this;
1229}
1230
1231SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1232 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1233 layer_state_t* s = getLayerState(sc);
1234 if (!s) {
1235 mStatus = BAD_INDEX;
1236 return *this;
1237 }
1238 s->what |= layer_state_t::eSidebandStreamChanged;
1239 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001240
1241 registerSurfaceControlForCallback(sc);
1242 return *this;
1243}
1244
Marissa Wall17b4e452018-12-26 16:32:34 -08001245SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1246 nsecs_t desiredPresentTime) {
1247 mDesiredPresentTime = desiredPresentTime;
1248 return *this;
1249}
1250
Peiyong Linc502cb72019-03-01 15:00:23 -08001251SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1252 const sp<SurfaceControl>& sc, const bool agnostic) {
1253 layer_state_t* s = getLayerState(sc);
1254 if (!s) {
1255 mStatus = BAD_INDEX;
1256 return *this;
1257 }
1258 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1259 s->colorSpaceAgnostic = agnostic;
1260
1261 registerSurfaceControlForCallback(sc);
1262 return *this;
1263}
1264
Marissa Wallc837b5e2018-10-12 10:04:44 -07001265SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001266SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1267 int32_t priority) {
1268 layer_state_t* s = getLayerState(sc);
1269 if (!s) {
1270 mStatus = BAD_INDEX;
1271 return *this;
1272 }
1273
1274 s->what |= layer_state_t::eFrameRateSelectionPriority;
1275 s->frameRateSelectionPriority = priority;
1276
1277 registerSurfaceControlForCallback(sc);
1278 return *this;
1279}
1280
1281SurfaceComposerClient::Transaction&
Marissa Wallc837b5e2018-10-12 10:04:44 -07001282SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001283 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001284 auto listener = TransactionCompletedListener::getInstance();
1285
Marissa Wall80d94ad2019-01-18 16:04:36 -08001286 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1287 std::placeholders::_2, std::placeholders::_3);
1288 const auto& surfaceControls =
1289 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001290
Marissa Wall80d94ad2019-01-18 16:04:36 -08001291 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001292
1293 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1294 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001295 return *this;
1296}
1297
Valerie Hau871d6352020-01-29 08:44:02 -08001298SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1299 const sp<SurfaceControl>& sc) {
1300 layer_state_t* s = getLayerState(sc);
1301 if (!s) {
1302 mStatus = BAD_INDEX;
1303 return *this;
1304 }
1305
1306 s->what |= layer_state_t::eProducerDisconnect;
1307 return *this;
1308}
1309
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07001310SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameNumber(
1311 const sp<SurfaceControl>& sc, uint64_t frameNumber) {
1312 layer_state_t* s = getLayerState(sc);
1313 if (!s) {
1314 mStatus = BAD_INDEX;
1315 return *this;
1316 }
1317
1318 s->what |= layer_state_t::eFrameNumberChanged;
1319 s->frameNumber = frameNumber;
1320
1321 return *this;
1322}
1323
Robert Carr4cdc58f2017-08-23 14:22:20 -07001324SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1325 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001326 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001327 if (!s) {
1328 mStatus = BAD_INDEX;
Greg Kaiserd45fdc32019-04-30 06:14:19 -07001329 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001330 }
1331 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001332
1333 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001334 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001335}
1336
Robert Carr4cdc58f2017-08-23 14:22:20 -07001337SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1338 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001339 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001340 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001341 mStatus = BAD_INDEX;
1342 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001343 }
1344
1345 switch (overrideScalingMode) {
1346 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1347 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1348 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1349 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1350 case -1:
1351 break;
1352 default:
1353 ALOGE("unknown scaling mode: %d",
1354 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001355 mStatus = BAD_VALUE;
1356 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001357 }
1358
1359 s->what |= layer_state_t::eOverrideScalingModeChanged;
1360 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001361
1362 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001363 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001364}
1365
Robert Carr2c358bf2018-08-08 15:58:15 -07001366#ifndef NO_INPUT
1367SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1368 const sp<SurfaceControl>& sc,
1369 const InputWindowInfo& info) {
1370 layer_state_t* s = getLayerState(sc);
1371 if (!s) {
1372 mStatus = BAD_INDEX;
1373 return *this;
1374 }
Chris Ye0783e992020-06-02 21:34:49 -07001375 s->inputHandle = new InputWindowHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001376 s->what |= layer_state_t::eInputInfoChanged;
1377 return *this;
1378}
chaviw273171b2018-12-26 11:46:30 -08001379
Vishnu Naire798b472020-07-23 13:52:21 -07001380SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
Vishnu Nair958da932020-08-21 17:12:37 -07001381 const sp<IBinder>& token, const sp<IBinder>& focusedToken, nsecs_t timestampNanos,
1382 int32_t displayId) {
Vishnu Naire798b472020-07-23 13:52:21 -07001383 FocusRequest request;
1384 request.token = token;
1385 request.focusedToken = focusedToken;
1386 request.timestamp = timestampNanos;
Vishnu Nair958da932020-08-21 17:12:37 -07001387 request.displayId = displayId;
Vishnu Naire798b472020-07-23 13:52:21 -07001388 return setFocusedWindow(request);
1389}
1390
1391SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
1392 const FocusRequest& request) {
1393 mInputWindowCommands.focusRequests.push_back(request);
1394 return *this;
1395}
1396
chaviwa911b102019-02-14 10:18:33 -08001397SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1398 mInputWindowCommands.syncInputWindows = true;
1399 return *this;
1400}
1401
Robert Carr2c358bf2018-08-08 15:58:15 -07001402#endif
1403
Peiyong Lind3788632018-09-18 16:01:31 -07001404SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1405 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1406 layer_state_t* s = getLayerState(sc);
1407 if (!s) {
1408 mStatus = BAD_INDEX;
1409 return *this;
1410 }
1411 s->what |= layer_state_t::eColorTransformChanged;
1412 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001413
1414 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001415 return *this;
1416}
1417
Robert Carrfb4d58b2019-01-15 09:21:27 -08001418SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1419 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1420 setCrop_legacy(sc, source);
1421
1422 int x = dst.left;
1423 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001424
1425 float sourceWidth = source.getWidth();
1426 float sourceHeight = source.getHeight();
1427
1428 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1429 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001430 float matrix[4] = {1, 0, 0, 1};
1431
1432 switch (transform) {
1433 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1434 matrix[0] = -xScale; matrix[1] = 0;
1435 matrix[2] = 0; matrix[3] = yScale;
1436 x += source.getWidth();
1437 break;
1438 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1439 matrix[0] = xScale; matrix[1] = 0;
1440 matrix[2] = 0; matrix[3] = -yScale;
1441 y += source.getHeight();
1442 break;
1443 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1444 matrix[0] = 0; matrix[1] = -yScale;
1445 matrix[2] = xScale; matrix[3] = 0;
1446 x += source.getHeight();
1447 break;
1448 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1449 matrix[0] = -xScale; matrix[1] = 0;
1450 matrix[2] = 0; matrix[3] = -yScale;
1451 x += source.getWidth();
1452 y += source.getHeight();
1453 break;
1454 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1455 matrix[0] = 0; matrix[1] = yScale;
1456 matrix[2] = -xScale; matrix[3] = 0;
1457 y += source.getWidth();
1458 break;
1459 default:
1460 matrix[0] = xScale; matrix[1] = 0;
1461 matrix[2] = 0; matrix[3] = yScale;
1462 break;
1463 }
1464 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07001465 float offsetX = xScale * source.left;
1466 float offsetY = yScale * source.top;
1467 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001468
1469 return *this;
1470}
1471
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001472SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1473 const sp<SurfaceControl>& sc, float shadowRadius) {
1474 layer_state_t* s = getLayerState(sc);
1475 if (!s) {
1476 mStatus = BAD_INDEX;
1477 return *this;
1478 }
1479 s->what |= layer_state_t::eShadowRadiusChanged;
1480 s->shadowRadius = shadowRadius;
1481 return *this;
1482}
1483
Steven Thomas3172e202020-01-06 19:25:30 -08001484SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Steven Thomas62a4cf82020-01-31 12:04:03 -08001485 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility) {
Steven Thomas3172e202020-01-06 19:25:30 -08001486 layer_state_t* s = getLayerState(sc);
1487 if (!s) {
1488 mStatus = BAD_INDEX;
1489 return *this;
1490 }
Steven Thomas62a4cf82020-01-31 12:04:03 -08001491 if (!ValidateFrameRate(frameRate, compatibility, "Transaction::setFrameRate")) {
1492 mStatus = BAD_VALUE;
1493 return *this;
1494 }
Steven Thomas3172e202020-01-06 19:25:30 -08001495 s->what |= layer_state_t::eFrameRateChanged;
1496 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08001497 s->frameRateCompatibility = compatibility;
Steven Thomas3172e202020-01-06 19:25:30 -08001498 return *this;
1499}
1500
Vishnu Nair6213bd92020-05-08 17:42:25 -07001501SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
1502 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
1503 layer_state_t* s = getLayerState(sc);
1504 if (!s) {
1505 mStatus = BAD_INDEX;
1506 return *this;
1507 }
1508
1509 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
1510 ? ui::Transform::ROT_INVALID
1511 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
1512 s->what |= layer_state_t::eFixedTransformHintChanged;
1513 s->fixedTransformHint = transform;
1514 return *this;
1515}
1516
Mathias Agopian698c0872011-06-28 19:09:31 -07001517// ---------------------------------------------------------------------------
1518
chaviw763ef572018-02-22 16:04:57 -08001519DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001520 DisplayState s;
1521 s.token = token;
1522 ssize_t index = mDisplayStates.indexOf(s);
1523 if (index < 0) {
1524 // we don't have it, add an initialized layer_state to our list
1525 s.what = 0;
1526 index = mDisplayStates.add(s);
1527 }
Dan Stozad723bd72014-11-18 10:24:03 -08001528 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001529}
1530
Robert Carr4cdc58f2017-08-23 14:22:20 -07001531status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1532 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001533 if (bufferProducer.get() != nullptr) {
1534 // Make sure that composition can never be stalled by a virtual display
1535 // consumer that isn't processing buffers fast enough.
1536 status_t err = bufferProducer->setAsyncMode(true);
1537 if (err != NO_ERROR) {
1538 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1539 "BufferQueue. This BufferQueue cannot be used for virtual "
1540 "display. (%d)", err);
1541 return err;
1542 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001543 }
chaviw763ef572018-02-22 16:04:57 -08001544 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001545 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001546 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001547 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001548}
1549
Robert Carr4cdc58f2017-08-23 14:22:20 -07001550void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001551 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001552 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001553 s.layerStack = layerStack;
1554 s.what |= DisplayState::eLayerStackChanged;
1555}
1556
Robert Carr4cdc58f2017-08-23 14:22:20 -07001557void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08001558 ui::Rotation orientation,
1559 const Rect& layerStackRect,
1560 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001561 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001562 s.orientation = orientation;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001563 s.layerStackSpaceRect = layerStackRect;
1564 s.orientedDisplaySpaceRect = displayRect;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001565 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001566 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001567}
1568
Robert Carr4cdc58f2017-08-23 14:22:20 -07001569void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001570 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001571 s.width = width;
1572 s.height = height;
1573 s.what |= DisplayState::eDisplaySizeChanged;
1574}
1575
Mathias Agopiane57f2922012-08-09 16:29:12 -07001576// ---------------------------------------------------------------------------
1577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001579 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581}
1582
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001583SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1584 : mStatus(NO_ERROR), mClient(client)
1585{
1586}
1587
Mathias Agopian698c0872011-06-28 19:09:31 -07001588void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001589 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001590 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001591 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001592 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001593 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001594 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001595 mStatus = NO_ERROR;
1596 }
1597 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598}
1599
Mathias Agopian698c0872011-06-28 19:09:31 -07001600SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001601 dispose();
1602}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001603
Mathias Agopian698c0872011-06-28 19:09:31 -07001604status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 return mStatus;
1606}
1607
Mathias Agopian698c0872011-06-28 19:09:31 -07001608sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001609 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610}
1611
Mathias Agopiand4784a32010-05-27 19:41:15 -07001612status_t SurfaceComposerClient::linkToComposerDeath(
1613 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001614 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001615 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1616 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Mathias Agopian698c0872011-06-28 19:09:31 -07001619void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001621 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001622 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001623 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001624 client = mClient; // hold ref while lock is held
1625 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001627 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628}
1629
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001630sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1631 PixelFormat format, uint32_t flags,
1632 SurfaceControl* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001633 LayerMetadata metadata,
1634 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001635 sp<SurfaceControl> s;
Valerie Hau1acd6962019-10-28 16:35:48 -07001636 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata),
1637 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07001638 return s;
1639}
1640
Marissa Wall35187b32019-01-08 10:08:52 -08001641sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1642 uint32_t h, PixelFormat format,
1643 uint32_t flags, Surface* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001644 LayerMetadata metadata,
1645 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -08001646 sp<SurfaceControl> sur;
1647 status_t err = mStatus;
1648
1649 if (mStatus == NO_ERROR) {
1650 sp<IBinder> handle;
1651 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1652 sp<IGraphicBufferProducer> gbp;
1653
Valerie Hau1acd6962019-10-28 16:35:48 -07001654 uint32_t transformHint = 0;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001655 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
Valerie Hau1acd6962019-10-28 16:35:48 -07001656 std::move(metadata), &handle, &gbp, &transformHint);
1657 if (outTransformHint) {
1658 *outTransformHint = transformHint;
1659 }
Marissa Wall35187b32019-01-08 10:08:52 -08001660 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1661 if (err == NO_ERROR) {
Robert Carr0e328f62020-02-06 17:12:08 -08001662 return new SurfaceControl(this, handle, gbp, transformHint);
Marissa Wall35187b32019-01-08 10:08:52 -08001663 }
1664 }
1665 return nullptr;
1666}
1667
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001668status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1669 PixelFormat format,
1670 sp<SurfaceControl>* outSurface, uint32_t flags,
Valerie Hau1acd6962019-10-28 16:35:48 -07001671 SurfaceControl* parent, LayerMetadata metadata,
1672 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001673 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001674 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001675
Mathias Agopian698c0872011-06-28 19:09:31 -07001676 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001677 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001678 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001679 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001680
1681 if (parent != nullptr) {
1682 parentHandle = parent->getHandle();
1683 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001684
Valerie Hau1acd6962019-10-28 16:35:48 -07001685 uint32_t transformHint = 0;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001686 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07001687 &handle, &gbp, &transformHint);
1688 if (outTransformHint) {
1689 *outTransformHint = transformHint;
1690 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001691 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1692 if (err == NO_ERROR) {
Robert Carr0e328f62020-02-06 17:12:08 -08001693 *outSurface = new SurfaceControl(this, handle, gbp, transformHint);
Mathias Agopian698c0872011-06-28 19:09:31 -07001694 }
1695 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001696 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001697}
1698
chaviwfe94a222019-08-21 13:52:59 -07001699sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
1700 if (mirrorFromSurface == nullptr) {
1701 return nullptr;
1702 }
1703
1704 sp<IBinder> handle;
1705 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
1706 status_t err = mClient->mirrorSurface(mirrorFromHandle, &handle);
1707 if (err == NO_ERROR) {
1708 return new SurfaceControl(this, handle, nullptr, true /* owned */);
1709 }
1710 return nullptr;
1711}
1712
Svetoslavd85084b2014-03-20 10:28:31 -07001713status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1714 if (mStatus != NO_ERROR) {
1715 return mStatus;
1716 }
1717 return mClient->clearLayerFrameStats(token);
1718}
1719
1720status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1721 FrameStats* outStats) const {
1722 if (mStatus != NO_ERROR) {
1723 return mStatus;
1724 }
1725 return mClient->getLayerFrameStats(token, outStats);
1726}
1727
Mathias Agopian698c0872011-06-28 19:09:31 -07001728// ----------------------------------------------------------------------------
1729
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001730status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001731 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1732 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001733}
1734
1735status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001736 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1737 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001738}
1739
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001740status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
1741 ui::DisplayState* state) {
1742 return ComposerService::getComposerService()->getDisplayState(display, state);
1743}
1744
1745status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
1746 return ComposerService::getComposerService()->getDisplayInfo(display, info);
1747}
1748
1749status_t SurfaceComposerClient::getDisplayConfigs(const sp<IBinder>& display,
1750 Vector<DisplayConfig>* configs) {
Dan Stoza7f7da322014-05-02 15:26:25 -07001751 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1752}
1753
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001754status_t SurfaceComposerClient::getActiveDisplayConfig(const sp<IBinder>& display,
1755 DisplayConfig* config) {
1756 Vector<DisplayConfig> configs;
Dan Stoza7f7da322014-05-02 15:26:25 -07001757 status_t result = getDisplayConfigs(display, &configs);
1758 if (result != NO_ERROR) {
1759 return result;
1760 }
1761
1762 int activeId = getActiveConfig(display);
1763 if (activeId < 0) {
1764 ALOGE("No active configuration found");
1765 return NAME_NOT_FOUND;
1766 }
1767
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001768 *config = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001769 return NO_ERROR;
1770}
1771
1772int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1773 return ComposerService::getComposerService()->getActiveConfig(display);
1774}
1775
Ana Krulec0782b882019-10-15 17:34:54 -07001776status_t SurfaceComposerClient::setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001777 int32_t defaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001778 float primaryRefreshRateMin,
1779 float primaryRefreshRateMax,
1780 float appRequestRefreshRateMin,
1781 float appRequestRefreshRateMax) {
1782 return ComposerService::getComposerService()
1783 ->setDesiredDisplayConfigSpecs(displayToken, defaultConfig, primaryRefreshRateMin,
1784 primaryRefreshRateMax, appRequestRefreshRateMin,
1785 appRequestRefreshRateMax);
Ana Krulec0782b882019-10-15 17:34:54 -07001786}
1787
Ana Krulec234bb162019-11-10 22:55:55 +01001788status_t SurfaceComposerClient::getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001789 int32_t* outDefaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001790 float* outPrimaryRefreshRateMin,
1791 float* outPrimaryRefreshRateMax,
1792 float* outAppRequestRefreshRateMin,
1793 float* outAppRequestRefreshRateMax) {
1794 return ComposerService::getComposerService()
1795 ->getDesiredDisplayConfigSpecs(displayToken, outDefaultConfig, outPrimaryRefreshRateMin,
1796 outPrimaryRefreshRateMax, outAppRequestRefreshRateMin,
1797 outAppRequestRefreshRateMax);
Ana Krulec234bb162019-11-10 22:55:55 +01001798}
1799
Michael Wright28f24d02016-07-12 13:30:53 -07001800status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001801 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001802 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1803}
1804
Daniel Solomon42d04562019-01-20 21:03:19 -08001805status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1806 ui::DisplayPrimaries& outPrimaries) {
1807 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1808}
1809
Peiyong Lina52f0292018-03-14 17:26:31 -07001810ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001811 return ComposerService::getComposerService()->getActiveColorMode(display);
1812}
1813
1814status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001815 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001816 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1817}
1818
Galia Peycheva5492cb52019-10-30 14:13:16 +01001819bool SurfaceComposerClient::getAutoLowLatencyModeSupport(const sp<IBinder>& display) {
1820 bool supported = false;
1821 ComposerService::getComposerService()->getAutoLowLatencyModeSupport(display, &supported);
1822 return supported;
1823}
1824
1825void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
1826 ComposerService::getComposerService()->setAutoLowLatencyMode(display, on);
1827}
1828
1829bool SurfaceComposerClient::getGameContentTypeSupport(const sp<IBinder>& display) {
1830 bool supported = false;
1831 ComposerService::getComposerService()->getGameContentTypeSupport(display, &supported);
1832 return supported;
1833}
1834
1835void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
1836 ComposerService::getComposerService()->setGameContentType(display, on);
1837}
1838
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001839void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1840 int mode) {
1841 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001842}
1843
Peiyong Linc6780972018-10-28 15:24:08 -07001844status_t SurfaceComposerClient::getCompositionPreference(
1845 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1846 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1847 return ComposerService::getComposerService()
1848 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1849 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001850}
1851
Peiyong Lin08d10512019-01-16 13:27:35 -08001852bool SurfaceComposerClient::getProtectedContentSupport() {
1853 bool supported = false;
1854 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1855 return supported;
1856}
1857
Svetoslavd85084b2014-03-20 10:28:31 -07001858status_t SurfaceComposerClient::clearAnimationFrameStats() {
1859 return ComposerService::getComposerService()->clearAnimationFrameStats();
1860}
1861
1862status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1863 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1864}
1865
Dan Stozac4f471e2016-03-24 09:31:08 -07001866status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1867 HdrCapabilities* outCapabilities) {
1868 return ComposerService::getComposerService()->getHdrCapabilities(display,
1869 outCapabilities);
1870}
1871
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001872status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1873 ui::PixelFormat* outFormat,
1874 ui::Dataspace* outDataspace,
1875 uint8_t* outComponentMask) {
1876 return ComposerService::getComposerService()
1877 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1878 outComponentMask);
1879}
1880
Kevin DuBois74e53772018-11-19 10:52:38 -08001881status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1882 bool enable, uint8_t componentMask,
1883 uint64_t maxFrames) {
1884 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1885 componentMask,
1886 maxFrames);
1887}
1888
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001889status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1890 uint64_t maxFrames, uint64_t timestamp,
1891 DisplayedFrameStats* outStats) {
1892 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1893 timestamp, outStats);
1894}
Marissa Wall35187b32019-01-08 10:08:52 -08001895
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001896status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1897 bool* outIsWideColorDisplay) {
1898 return ComposerService::getComposerService()->isWideColorDisplay(display,
1899 outIsWideColorDisplay);
1900}
1901
Kevin DuBois00c66832019-02-18 16:21:31 -08001902status_t SurfaceComposerClient::addRegionSamplingListener(
1903 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1904 const sp<IRegionSamplingListener>& listener) {
1905 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1906 stopLayerHandle,
1907 listener);
1908}
1909
1910status_t SurfaceComposerClient::removeRegionSamplingListener(
1911 const sp<IRegionSamplingListener>& listener) {
1912 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1913}
1914
Dan Gittik57e63c52019-01-18 16:37:54 +00001915bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1916 bool support = false;
1917 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1918 return support;
1919}
1920
1921status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1922 float brightness) {
1923 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1924}
1925
Lais Andrade3a6e47d2020-04-02 11:20:16 +01001926status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
1927 return ComposerService::getComposerService()->notifyPowerBoost(boostId);
Ady Abraham8532d012019-05-08 14:50:56 -07001928}
1929
Vishnu Nairb13bb952019-11-15 10:24:08 -08001930status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
1931 const half4& spotColor, float lightPosY,
1932 float lightPosZ, float lightRadius) {
1933 return ComposerService::getComposerService()->setGlobalShadowSettings(ambientColor, spotColor,
1934 lightPosY, lightPosZ,
1935 lightRadius);
1936}
1937
Mathias Agopian698c0872011-06-28 19:09:31 -07001938// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939
chaviw690db382020-07-27 16:46:46 -07001940status_t ScreenshotClient::captureDisplay(const DisplayCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07001941 const sp<IScreenCaptureListener>& captureListener) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001942 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001943 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001944
chaviwe7b9f272020-08-18 16:08:59 -07001945 return s->captureDisplay(captureArgs, captureListener);
Robert Carr673134e2017-01-09 19:48:38 -08001946}
1947
chaviw690db382020-07-27 16:46:46 -07001948status_t ScreenshotClient::captureDisplay(uint64_t displayOrLayerStack,
chaviwe7b9f272020-08-18 16:08:59 -07001949 const sp<IScreenCaptureListener>& captureListener) {
chaviw93df2ea2019-04-30 16:45:12 -07001950 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1951 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001952
chaviwe7b9f272020-08-18 16:08:59 -07001953 return s->captureDisplay(displayOrLayerStack, captureListener);
chaviw93df2ea2019-04-30 16:45:12 -07001954}
1955
chaviw26c52482020-07-28 16:25:52 -07001956status_t ScreenshotClient::captureLayers(const LayerCaptureArgs& captureArgs,
chaviwe7b9f272020-08-18 16:08:59 -07001957 const sp<IScreenCaptureListener>& captureListener) {
chaviwa76b2712017-09-20 12:02:26 -07001958 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001959 if (s == nullptr) return NO_INIT;
chaviw8ffc7b82020-08-18 11:25:37 -07001960
chaviwe7b9f272020-08-18 16:08:59 -07001961 return s->captureLayers(captureArgs, captureListener);
chaviwa76b2712017-09-20 12:02:26 -07001962}
Dominik Laskowski718f9602019-11-09 20:01:35 -08001963
1964} // namespace android