blob: 229a5c2e62ab88bf2ca59f6d85db46fe38c9e245 [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 }
Pablo Ceballos53390e12015-08-04 11:25:59 -0700864 if ((mask & layer_state_t::eLayerOpaque) ||
865 (mask & layer_state_t::eLayerHidden) ||
866 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700867 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800868 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700869 s->flags &= ~mask;
870 s->flags |= (flags & mask);
871 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700872
873 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700874 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700875}
876
Robert Carr4cdc58f2017-08-23 14:22:20 -0700877SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
878 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700879 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800880 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700881 if (!s) {
882 mStatus = BAD_INDEX;
883 return *this;
884 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700885 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700886 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700887
888 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700889 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700890}
891
Robert Carr4cdc58f2017-08-23 14:22:20 -0700892SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
893 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800894 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700895 if (!s) {
896 mStatus = BAD_INDEX;
897 return *this;
898 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700899 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700900 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700901
902 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700903 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700904}
905
Robert Carr4cdc58f2017-08-23 14:22:20 -0700906SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
907 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -0800908 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700909 if (!s) {
910 mStatus = BAD_INDEX;
911 return *this;
912 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700913 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700914 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700915
916 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700917 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700918}
919
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800920SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tan01a56132019-08-05 16:44:21 -0700921 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800922 layer_state_t* s = getLayerState(sc);
923 if (!s) {
924 mStatus = BAD_INDEX;
925 return *this;
926 }
927 s->what |= layer_state_t::eMetadataChanged;
Garfield Tan01a56132019-08-05 16:44:21 -0700928
929 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800930
931 registerSurfaceControlForCallback(sc);
932 return *this;
933}
934
Robert Carr4cdc58f2017-08-23 14:22:20 -0700935SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
936 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800937 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800938 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700939 if (!s) {
940 mStatus = BAD_INDEX;
941 return *this;
942 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700943 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700944 layer_state_t::matrix22_t matrix;
945 matrix.dsdx = dsdx;
946 matrix.dtdx = dtdx;
947 matrix.dsdy = dsdy;
948 matrix.dtdy = dtdy;
949 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700950
951 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700952 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700953}
954
Marissa Wallf58c14b2018-07-24 10:50:43 -0700955SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700956 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800957 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700958 if (!s) {
959 mStatus = BAD_INDEX;
960 return *this;
961 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700962 s->what |= layer_state_t::eCropChanged_legacy;
963 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700964
965 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700966 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700967}
968
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700969SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
970 const sp<SurfaceControl>& sc, float cornerRadius) {
971 layer_state_t* s = getLayerState(sc);
972 if (!s) {
973 mStatus = BAD_INDEX;
974 return *this;
975 }
976 s->what |= layer_state_t::eCornerRadiusChanged;
977 s->cornerRadius = cornerRadius;
978 return *this;
979}
980
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800981SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundBlurRadius(
982 const sp<SurfaceControl>& sc, int backgroundBlurRadius) {
983 layer_state_t* s = getLayerState(sc);
984 if (!s) {
985 mStatus = BAD_INDEX;
986 return *this;
987 }
988 s->what |= layer_state_t::eBackgroundBlurRadiusChanged;
989 s->backgroundBlurRadius = backgroundBlurRadius;
990 return *this;
991}
992
Marissa Wallf58c14b2018-07-24 10:50:43 -0700993SurfaceComposerClient::Transaction&
994SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
995 const sp<IBinder>& handle,
996 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800997 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700998 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700999 mStatus = BAD_INDEX;
1000 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -07001001 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001002 s->what |= layer_state_t::eDeferTransaction_legacy;
1003 s->barrierHandle_legacy = handle;
1004 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001005
1006 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001007 return *this;
Robert Carr0d480722017-01-10 16:42:54 -08001008}
1009
Marissa Wallf58c14b2018-07-24 10:50:43 -07001010SurfaceComposerClient::Transaction&
1011SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
1012 const sp<Surface>& barrierSurface,
1013 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -08001014 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -08001015 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001016 mStatus = BAD_INDEX;
1017 return *this;
Robert Carr0d480722017-01-10 16:42:54 -08001018 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001019 s->what |= layer_state_t::eDeferTransaction_legacy;
1020 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
1021 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001022
1023 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001024 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -07001025}
1026
Robert Carr4cdc58f2017-08-23 14:22:20 -07001027SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
1028 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -08001029 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -08001030 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -08001031 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001032 mStatus = BAD_INDEX;
1033 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001034 }
1035 s->what |= layer_state_t::eReparentChildren;
1036 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001037
1038 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001039 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -08001040}
1041
Robert Carr4cdc58f2017-08-23 14:22:20 -07001042SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
1043 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -07001044 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -08001045 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -07001046 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001047 mStatus = BAD_INDEX;
1048 return *this;
chaviw06178942017-07-27 10:25:59 -07001049 }
chaviwf1961f72017-09-18 16:41:07 -07001050 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -07001051 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001052
1053 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001054 return *this;
chaviw06178942017-07-27 10:25:59 -07001055}
1056
Robert Carr4cdc58f2017-08-23 14:22:20 -07001057SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
1058 const sp<SurfaceControl>& sc,
1059 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -08001060 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -08001061 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001062 mStatus = BAD_INDEX;
1063 return *this;
1064 }
1065 s->what |= layer_state_t::eColorChanged;
1066 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001067
1068 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001069 return *this;
1070}
1071
Valerie Haudd0b7572019-01-29 14:59:27 -08001072SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
1073 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -08001074 layer_state_t* s = getLayerState(sc);
1075 if (!s) {
1076 mStatus = BAD_INDEX;
1077 return *this;
1078 }
1079
Valerie Haudd0b7572019-01-29 14:59:27 -08001080 s->what |= layer_state_t::eBackgroundColorChanged;
1081 s->color = color;
1082 s->bgColorAlpha = alpha;
1083 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -08001084
1085 registerSurfaceControlForCallback(sc);
1086 return *this;
1087}
1088
Marissa Wall61c58622018-07-18 10:12:20 -07001089SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
1090 const sp<SurfaceControl>& sc, uint32_t transform) {
1091 layer_state_t* s = getLayerState(sc);
1092 if (!s) {
1093 mStatus = BAD_INDEX;
1094 return *this;
1095 }
1096 s->what |= layer_state_t::eTransformChanged;
1097 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001098
1099 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001100 return *this;
1101}
1102
1103SurfaceComposerClient::Transaction&
1104SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
1105 bool transformToDisplayInverse) {
1106 layer_state_t* s = getLayerState(sc);
1107 if (!s) {
1108 mStatus = BAD_INDEX;
1109 return *this;
1110 }
1111 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
1112 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001113
1114 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001115 return *this;
1116}
1117
1118SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
1119 const sp<SurfaceControl>& sc, const Rect& crop) {
1120 layer_state_t* s = getLayerState(sc);
1121 if (!s) {
1122 mStatus = BAD_INDEX;
1123 return *this;
1124 }
1125 s->what |= layer_state_t::eCropChanged;
1126 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001127
1128 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001129 return *this;
1130}
1131
Marissa Wall861616d2018-10-22 12:52:23 -07001132SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
1133 const sp<SurfaceControl>& sc, const Rect& frame) {
1134 layer_state_t* s = getLayerState(sc);
1135 if (!s) {
1136 mStatus = BAD_INDEX;
1137 return *this;
1138 }
1139 s->what |= layer_state_t::eFrameChanged;
1140 s->frame = frame;
1141
1142 registerSurfaceControlForCallback(sc);
1143 return *this;
1144}
1145
Marissa Wall61c58622018-07-18 10:12:20 -07001146SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
1147 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
1148 layer_state_t* s = getLayerState(sc);
1149 if (!s) {
1150 mStatus = BAD_INDEX;
1151 return *this;
1152 }
Marissa Wall78b72202019-03-15 14:58:34 -07001153 s->what |= layer_state_t::eBufferChanged;
1154 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -08001155
1156 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -07001157
1158 mContainsBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -08001159 return *this;
1160}
1161
Marissa Wall61c58622018-07-18 10:12:20 -07001162SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
1163 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
1164 layer_state_t* s = getLayerState(sc);
1165 if (!s) {
1166 mStatus = BAD_INDEX;
1167 return *this;
1168 }
1169 s->what |= layer_state_t::eAcquireFenceChanged;
1170 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001171
1172 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001173 return *this;
1174}
1175
1176SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
1177 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
1178 layer_state_t* s = getLayerState(sc);
1179 if (!s) {
1180 mStatus = BAD_INDEX;
1181 return *this;
1182 }
1183 s->what |= layer_state_t::eDataspaceChanged;
1184 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001185
1186 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001187 return *this;
1188}
1189
1190SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
1191 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
1192 layer_state_t* s = getLayerState(sc);
1193 if (!s) {
1194 mStatus = BAD_INDEX;
1195 return *this;
1196 }
1197 s->what |= layer_state_t::eHdrMetadataChanged;
1198 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001199
1200 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001201 return *this;
1202}
1203
1204SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
1205 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
1206 layer_state_t* s = getLayerState(sc);
1207 if (!s) {
1208 mStatus = BAD_INDEX;
1209 return *this;
1210 }
1211 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
1212 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001213
1214 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001215 return *this;
1216}
1217
1218SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
1219 const sp<SurfaceControl>& sc, int32_t api) {
1220 layer_state_t* s = getLayerState(sc);
1221 if (!s) {
1222 mStatus = BAD_INDEX;
1223 return *this;
1224 }
1225 s->what |= layer_state_t::eApiChanged;
1226 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001227
1228 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001229 return *this;
1230}
1231
1232SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1233 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1234 layer_state_t* s = getLayerState(sc);
1235 if (!s) {
1236 mStatus = BAD_INDEX;
1237 return *this;
1238 }
1239 s->what |= layer_state_t::eSidebandStreamChanged;
1240 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001241
1242 registerSurfaceControlForCallback(sc);
1243 return *this;
1244}
1245
Marissa Wall17b4e452018-12-26 16:32:34 -08001246SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1247 nsecs_t desiredPresentTime) {
1248 mDesiredPresentTime = desiredPresentTime;
1249 return *this;
1250}
1251
Peiyong Linc502cb72019-03-01 15:00:23 -08001252SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1253 const sp<SurfaceControl>& sc, const bool agnostic) {
1254 layer_state_t* s = getLayerState(sc);
1255 if (!s) {
1256 mStatus = BAD_INDEX;
1257 return *this;
1258 }
1259 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1260 s->colorSpaceAgnostic = agnostic;
1261
1262 registerSurfaceControlForCallback(sc);
1263 return *this;
1264}
1265
Marissa Wallc837b5e2018-10-12 10:04:44 -07001266SurfaceComposerClient::Transaction&
Ana Krulecc84d09b2019-11-02 23:10:29 +01001267SurfaceComposerClient::Transaction::setFrameRateSelectionPriority(const sp<SurfaceControl>& sc,
1268 int32_t priority) {
1269 layer_state_t* s = getLayerState(sc);
1270 if (!s) {
1271 mStatus = BAD_INDEX;
1272 return *this;
1273 }
1274
1275 s->what |= layer_state_t::eFrameRateSelectionPriority;
1276 s->frameRateSelectionPriority = priority;
1277
1278 registerSurfaceControlForCallback(sc);
1279 return *this;
1280}
1281
1282SurfaceComposerClient::Transaction&
Marissa Wallc837b5e2018-10-12 10:04:44 -07001283SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001284 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001285 auto listener = TransactionCompletedListener::getInstance();
1286
Marissa Wall80d94ad2019-01-18 16:04:36 -08001287 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1288 std::placeholders::_2, std::placeholders::_3);
1289 const auto& surfaceControls =
1290 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001291
Marissa Wall80d94ad2019-01-18 16:04:36 -08001292 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001293
1294 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1295 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001296 return *this;
1297}
1298
Valerie Hau871d6352020-01-29 08:44:02 -08001299SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::notifyProducerDisconnect(
1300 const sp<SurfaceControl>& sc) {
1301 layer_state_t* s = getLayerState(sc);
1302 if (!s) {
1303 mStatus = BAD_INDEX;
1304 return *this;
1305 }
1306
1307 s->what |= layer_state_t::eProducerDisconnect;
1308 return *this;
1309}
1310
Robert Carr4cdc58f2017-08-23 14:22:20 -07001311SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1312 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001313 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001314 if (!s) {
1315 mStatus = BAD_INDEX;
Greg Kaiserd45fdc32019-04-30 06:14:19 -07001316 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001317 }
1318 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001319
1320 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001321 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001322}
1323
Robert Carr4cdc58f2017-08-23 14:22:20 -07001324SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1325 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001326 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001327 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001328 mStatus = BAD_INDEX;
1329 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001330 }
1331
1332 switch (overrideScalingMode) {
1333 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1334 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1335 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1336 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1337 case -1:
1338 break;
1339 default:
1340 ALOGE("unknown scaling mode: %d",
1341 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001342 mStatus = BAD_VALUE;
1343 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001344 }
1345
1346 s->what |= layer_state_t::eOverrideScalingModeChanged;
1347 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001348
1349 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001350 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001351}
1352
Robert Carr2c358bf2018-08-08 15:58:15 -07001353#ifndef NO_INPUT
1354SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1355 const sp<SurfaceControl>& sc,
1356 const InputWindowInfo& info) {
1357 layer_state_t* s = getLayerState(sc);
1358 if (!s) {
1359 mStatus = BAD_INDEX;
1360 return *this;
1361 }
Chris Ye0783e992020-06-02 21:34:49 -07001362 s->inputHandle = new InputWindowHandle(info);
Robert Carr2c358bf2018-08-08 15:58:15 -07001363 s->what |= layer_state_t::eInputInfoChanged;
1364 return *this;
1365}
chaviw273171b2018-12-26 11:46:30 -08001366
Vishnu Naire798b472020-07-23 13:52:21 -07001367SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
1368 const sp<IBinder>& token, const sp<IBinder>& focusedToken, nsecs_t timestampNanos) {
1369 FocusRequest request;
1370 request.token = token;
1371 request.focusedToken = focusedToken;
1372 request.timestamp = timestampNanos;
1373 return setFocusedWindow(request);
1374}
1375
1376SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFocusedWindow(
1377 const FocusRequest& request) {
1378 mInputWindowCommands.focusRequests.push_back(request);
1379 return *this;
1380}
1381
chaviwa911b102019-02-14 10:18:33 -08001382SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1383 mInputWindowCommands.syncInputWindows = true;
1384 return *this;
1385}
1386
Robert Carr2c358bf2018-08-08 15:58:15 -07001387#endif
1388
Peiyong Lind3788632018-09-18 16:01:31 -07001389SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1390 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1391 layer_state_t* s = getLayerState(sc);
1392 if (!s) {
1393 mStatus = BAD_INDEX;
1394 return *this;
1395 }
1396 s->what |= layer_state_t::eColorTransformChanged;
1397 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001398
1399 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001400 return *this;
1401}
1402
Robert Carrfb4d58b2019-01-15 09:21:27 -08001403SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1404 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1405 setCrop_legacy(sc, source);
1406
1407 int x = dst.left;
1408 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001409
1410 float sourceWidth = source.getWidth();
1411 float sourceHeight = source.getHeight();
1412
1413 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1414 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001415 float matrix[4] = {1, 0, 0, 1};
1416
1417 switch (transform) {
1418 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1419 matrix[0] = -xScale; matrix[1] = 0;
1420 matrix[2] = 0; matrix[3] = yScale;
1421 x += source.getWidth();
1422 break;
1423 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1424 matrix[0] = xScale; matrix[1] = 0;
1425 matrix[2] = 0; matrix[3] = -yScale;
1426 y += source.getHeight();
1427 break;
1428 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1429 matrix[0] = 0; matrix[1] = -yScale;
1430 matrix[2] = xScale; matrix[3] = 0;
1431 x += source.getHeight();
1432 break;
1433 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1434 matrix[0] = -xScale; matrix[1] = 0;
1435 matrix[2] = 0; matrix[3] = -yScale;
1436 x += source.getWidth();
1437 y += source.getHeight();
1438 break;
1439 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1440 matrix[0] = 0; matrix[1] = yScale;
1441 matrix[2] = -xScale; matrix[3] = 0;
1442 y += source.getWidth();
1443 break;
1444 default:
1445 matrix[0] = xScale; matrix[1] = 0;
1446 matrix[2] = 0; matrix[3] = yScale;
1447 break;
1448 }
1449 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
chaviw76f5f2f2019-09-23 10:15:51 -07001450 float offsetX = xScale * source.left;
1451 float offsetY = yScale * source.top;
1452 setPosition(sc, x - offsetX, y - offsetY);
Robert Carrfb4d58b2019-01-15 09:21:27 -08001453
1454 return *this;
1455}
1456
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001457SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setShadowRadius(
1458 const sp<SurfaceControl>& sc, float shadowRadius) {
1459 layer_state_t* s = getLayerState(sc);
1460 if (!s) {
1461 mStatus = BAD_INDEX;
1462 return *this;
1463 }
1464 s->what |= layer_state_t::eShadowRadiusChanged;
1465 s->shadowRadius = shadowRadius;
1466 return *this;
1467}
1468
Steven Thomas3172e202020-01-06 19:25:30 -08001469SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrameRate(
Steven Thomas62a4cf82020-01-31 12:04:03 -08001470 const sp<SurfaceControl>& sc, float frameRate, int8_t compatibility) {
Steven Thomas3172e202020-01-06 19:25:30 -08001471 layer_state_t* s = getLayerState(sc);
1472 if (!s) {
1473 mStatus = BAD_INDEX;
1474 return *this;
1475 }
Steven Thomas62a4cf82020-01-31 12:04:03 -08001476 if (!ValidateFrameRate(frameRate, compatibility, "Transaction::setFrameRate")) {
1477 mStatus = BAD_VALUE;
1478 return *this;
1479 }
Steven Thomas3172e202020-01-06 19:25:30 -08001480 s->what |= layer_state_t::eFrameRateChanged;
1481 s->frameRate = frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -08001482 s->frameRateCompatibility = compatibility;
Steven Thomas3172e202020-01-06 19:25:30 -08001483 return *this;
1484}
1485
Vishnu Nair6213bd92020-05-08 17:42:25 -07001486SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFixedTransformHint(
1487 const sp<SurfaceControl>& sc, int32_t fixedTransformHint) {
1488 layer_state_t* s = getLayerState(sc);
1489 if (!s) {
1490 mStatus = BAD_INDEX;
1491 return *this;
1492 }
1493
1494 const ui::Transform::RotationFlags transform = fixedTransformHint == -1
1495 ? ui::Transform::ROT_INVALID
1496 : ui::Transform::toRotationFlags(static_cast<ui::Rotation>(fixedTransformHint));
1497 s->what |= layer_state_t::eFixedTransformHintChanged;
1498 s->fixedTransformHint = transform;
1499 return *this;
1500}
1501
Mathias Agopian698c0872011-06-28 19:09:31 -07001502// ---------------------------------------------------------------------------
1503
chaviw763ef572018-02-22 16:04:57 -08001504DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001505 DisplayState s;
1506 s.token = token;
1507 ssize_t index = mDisplayStates.indexOf(s);
1508 if (index < 0) {
1509 // we don't have it, add an initialized layer_state to our list
1510 s.what = 0;
1511 index = mDisplayStates.add(s);
1512 }
Dan Stozad723bd72014-11-18 10:24:03 -08001513 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001514}
1515
Robert Carr4cdc58f2017-08-23 14:22:20 -07001516status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1517 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001518 if (bufferProducer.get() != nullptr) {
1519 // Make sure that composition can never be stalled by a virtual display
1520 // consumer that isn't processing buffers fast enough.
1521 status_t err = bufferProducer->setAsyncMode(true);
1522 if (err != NO_ERROR) {
1523 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1524 "BufferQueue. This BufferQueue cannot be used for virtual "
1525 "display. (%d)", err);
1526 return err;
1527 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001528 }
chaviw763ef572018-02-22 16:04:57 -08001529 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001530 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001531 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001532 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001533}
1534
Robert Carr4cdc58f2017-08-23 14:22:20 -07001535void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001536 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001537 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001538 s.layerStack = layerStack;
1539 s.what |= DisplayState::eLayerStackChanged;
1540}
1541
Robert Carr4cdc58f2017-08-23 14:22:20 -07001542void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Dominik Laskowski718f9602019-11-09 20:01:35 -08001543 ui::Rotation orientation,
1544 const Rect& layerStackRect,
1545 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001546 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001547 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001548 s.viewport = layerStackRect;
1549 s.frame = displayRect;
1550 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001551 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001552}
1553
Robert Carr4cdc58f2017-08-23 14:22:20 -07001554void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001555 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001556 s.width = width;
1557 s.height = height;
1558 s.what |= DisplayState::eDisplaySizeChanged;
1559}
1560
Mathias Agopiane57f2922012-08-09 16:29:12 -07001561// ---------------------------------------------------------------------------
1562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001564 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566}
1567
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001568SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1569 : mStatus(NO_ERROR), mClient(client)
1570{
1571}
1572
Mathias Agopian698c0872011-06-28 19:09:31 -07001573void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001574 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001575 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001576 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001577 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001578 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001579 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001580 mStatus = NO_ERROR;
1581 }
1582 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583}
1584
Mathias Agopian698c0872011-06-28 19:09:31 -07001585SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001586 dispose();
1587}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001588
Mathias Agopian698c0872011-06-28 19:09:31 -07001589status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 return mStatus;
1591}
1592
Mathias Agopian698c0872011-06-28 19:09:31 -07001593sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001594 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595}
1596
Mathias Agopiand4784a32010-05-27 19:41:15 -07001597status_t SurfaceComposerClient::linkToComposerDeath(
1598 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001599 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001600 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1601 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602}
1603
Mathias Agopian698c0872011-06-28 19:09:31 -07001604void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001606 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001607 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001608 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001609 client = mClient; // hold ref while lock is held
1610 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001612 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}
1614
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001615sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1616 PixelFormat format, uint32_t flags,
1617 SurfaceControl* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001618 LayerMetadata metadata,
1619 uint32_t* outTransformHint) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001620 sp<SurfaceControl> s;
Valerie Hau1acd6962019-10-28 16:35:48 -07001621 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata),
1622 outTransformHint);
Robert Carr3b382ed2018-03-14 13:49:41 -07001623 return s;
1624}
1625
Marissa Wall35187b32019-01-08 10:08:52 -08001626sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1627 uint32_t h, PixelFormat format,
1628 uint32_t flags, Surface* parent,
Valerie Hau1acd6962019-10-28 16:35:48 -07001629 LayerMetadata metadata,
1630 uint32_t* outTransformHint) {
Marissa Wall35187b32019-01-08 10:08:52 -08001631 sp<SurfaceControl> sur;
1632 status_t err = mStatus;
1633
1634 if (mStatus == NO_ERROR) {
1635 sp<IBinder> handle;
1636 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1637 sp<IGraphicBufferProducer> gbp;
1638
Valerie Hau1acd6962019-10-28 16:35:48 -07001639 uint32_t transformHint = 0;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001640 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
Valerie Hau1acd6962019-10-28 16:35:48 -07001641 std::move(metadata), &handle, &gbp, &transformHint);
1642 if (outTransformHint) {
1643 *outTransformHint = transformHint;
1644 }
Marissa Wall35187b32019-01-08 10:08:52 -08001645 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1646 if (err == NO_ERROR) {
Robert Carr0e328f62020-02-06 17:12:08 -08001647 return new SurfaceControl(this, handle, gbp, transformHint);
Marissa Wall35187b32019-01-08 10:08:52 -08001648 }
1649 }
1650 return nullptr;
1651}
1652
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001653status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1654 PixelFormat format,
1655 sp<SurfaceControl>* outSurface, uint32_t flags,
Valerie Hau1acd6962019-10-28 16:35:48 -07001656 SurfaceControl* parent, LayerMetadata metadata,
1657 uint32_t* outTransformHint) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001658 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001659 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001660
Mathias Agopian698c0872011-06-28 19:09:31 -07001661 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001662 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001664 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665
1666 if (parent != nullptr) {
1667 parentHandle = parent->getHandle();
1668 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001669
Valerie Hau1acd6962019-10-28 16:35:48 -07001670 uint32_t transformHint = 0;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001671 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
Valerie Hau1acd6962019-10-28 16:35:48 -07001672 &handle, &gbp, &transformHint);
1673 if (outTransformHint) {
1674 *outTransformHint = transformHint;
1675 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001676 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1677 if (err == NO_ERROR) {
Robert Carr0e328f62020-02-06 17:12:08 -08001678 *outSurface = new SurfaceControl(this, handle, gbp, transformHint);
Mathias Agopian698c0872011-06-28 19:09:31 -07001679 }
1680 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001681 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001682}
1683
chaviwfe94a222019-08-21 13:52:59 -07001684sp<SurfaceControl> SurfaceComposerClient::mirrorSurface(SurfaceControl* mirrorFromSurface) {
1685 if (mirrorFromSurface == nullptr) {
1686 return nullptr;
1687 }
1688
1689 sp<IBinder> handle;
1690 sp<IBinder> mirrorFromHandle = mirrorFromSurface->getHandle();
1691 status_t err = mClient->mirrorSurface(mirrorFromHandle, &handle);
1692 if (err == NO_ERROR) {
1693 return new SurfaceControl(this, handle, nullptr, true /* owned */);
1694 }
1695 return nullptr;
1696}
1697
Svetoslavd85084b2014-03-20 10:28:31 -07001698status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1699 if (mStatus != NO_ERROR) {
1700 return mStatus;
1701 }
1702 return mClient->clearLayerFrameStats(token);
1703}
1704
1705status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1706 FrameStats* outStats) const {
1707 if (mStatus != NO_ERROR) {
1708 return mStatus;
1709 }
1710 return mClient->getLayerFrameStats(token, outStats);
1711}
1712
Mathias Agopian698c0872011-06-28 19:09:31 -07001713// ----------------------------------------------------------------------------
1714
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001715status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001716 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1717 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001718}
1719
1720status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001721 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1722 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001723}
1724
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001725status_t SurfaceComposerClient::getDisplayState(const sp<IBinder>& display,
1726 ui::DisplayState* state) {
1727 return ComposerService::getComposerService()->getDisplayState(display, state);
1728}
1729
1730status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
1731 return ComposerService::getComposerService()->getDisplayInfo(display, info);
1732}
1733
1734status_t SurfaceComposerClient::getDisplayConfigs(const sp<IBinder>& display,
1735 Vector<DisplayConfig>* configs) {
Dan Stoza7f7da322014-05-02 15:26:25 -07001736 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1737}
1738
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001739status_t SurfaceComposerClient::getActiveDisplayConfig(const sp<IBinder>& display,
1740 DisplayConfig* config) {
1741 Vector<DisplayConfig> configs;
Dan Stoza7f7da322014-05-02 15:26:25 -07001742 status_t result = getDisplayConfigs(display, &configs);
1743 if (result != NO_ERROR) {
1744 return result;
1745 }
1746
1747 int activeId = getActiveConfig(display);
1748 if (activeId < 0) {
1749 ALOGE("No active configuration found");
1750 return NAME_NOT_FOUND;
1751 }
1752
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -08001753 *config = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001754 return NO_ERROR;
1755}
1756
1757int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1758 return ComposerService::getComposerService()->getActiveConfig(display);
1759}
1760
Ana Krulec0782b882019-10-15 17:34:54 -07001761status_t SurfaceComposerClient::setDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001762 int32_t defaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001763 float primaryRefreshRateMin,
1764 float primaryRefreshRateMax,
1765 float appRequestRefreshRateMin,
1766 float appRequestRefreshRateMax) {
1767 return ComposerService::getComposerService()
1768 ->setDesiredDisplayConfigSpecs(displayToken, defaultConfig, primaryRefreshRateMin,
1769 primaryRefreshRateMax, appRequestRefreshRateMin,
1770 appRequestRefreshRateMax);
Ana Krulec0782b882019-10-15 17:34:54 -07001771}
1772
Ana Krulec234bb162019-11-10 22:55:55 +01001773status_t SurfaceComposerClient::getDesiredDisplayConfigSpecs(const sp<IBinder>& displayToken,
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001774 int32_t* outDefaultConfig,
Steven Thomasf734df42020-04-13 21:09:28 -07001775 float* outPrimaryRefreshRateMin,
1776 float* outPrimaryRefreshRateMax,
1777 float* outAppRequestRefreshRateMin,
1778 float* outAppRequestRefreshRateMax) {
1779 return ComposerService::getComposerService()
1780 ->getDesiredDisplayConfigSpecs(displayToken, outDefaultConfig, outPrimaryRefreshRateMin,
1781 outPrimaryRefreshRateMax, outAppRequestRefreshRateMin,
1782 outAppRequestRefreshRateMax);
Ana Krulec234bb162019-11-10 22:55:55 +01001783}
1784
Michael Wright28f24d02016-07-12 13:30:53 -07001785status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001786 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001787 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1788}
1789
Daniel Solomon42d04562019-01-20 21:03:19 -08001790status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1791 ui::DisplayPrimaries& outPrimaries) {
1792 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1793}
1794
Peiyong Lina52f0292018-03-14 17:26:31 -07001795ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001796 return ComposerService::getComposerService()->getActiveColorMode(display);
1797}
1798
1799status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001800 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001801 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1802}
1803
Galia Peycheva5492cb52019-10-30 14:13:16 +01001804bool SurfaceComposerClient::getAutoLowLatencyModeSupport(const sp<IBinder>& display) {
1805 bool supported = false;
1806 ComposerService::getComposerService()->getAutoLowLatencyModeSupport(display, &supported);
1807 return supported;
1808}
1809
1810void SurfaceComposerClient::setAutoLowLatencyMode(const sp<IBinder>& display, bool on) {
1811 ComposerService::getComposerService()->setAutoLowLatencyMode(display, on);
1812}
1813
1814bool SurfaceComposerClient::getGameContentTypeSupport(const sp<IBinder>& display) {
1815 bool supported = false;
1816 ComposerService::getComposerService()->getGameContentTypeSupport(display, &supported);
1817 return supported;
1818}
1819
1820void SurfaceComposerClient::setGameContentType(const sp<IBinder>& display, bool on) {
1821 ComposerService::getComposerService()->setGameContentType(display, on);
1822}
1823
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001824void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1825 int mode) {
1826 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001827}
1828
Peiyong Linc6780972018-10-28 15:24:08 -07001829status_t SurfaceComposerClient::getCompositionPreference(
1830 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1831 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1832 return ComposerService::getComposerService()
1833 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1834 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001835}
1836
Peiyong Lin08d10512019-01-16 13:27:35 -08001837bool SurfaceComposerClient::getProtectedContentSupport() {
1838 bool supported = false;
1839 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1840 return supported;
1841}
1842
Svetoslavd85084b2014-03-20 10:28:31 -07001843status_t SurfaceComposerClient::clearAnimationFrameStats() {
1844 return ComposerService::getComposerService()->clearAnimationFrameStats();
1845}
1846
1847status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1848 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1849}
1850
Dan Stozac4f471e2016-03-24 09:31:08 -07001851status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1852 HdrCapabilities* outCapabilities) {
1853 return ComposerService::getComposerService()->getHdrCapabilities(display,
1854 outCapabilities);
1855}
1856
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001857status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1858 ui::PixelFormat* outFormat,
1859 ui::Dataspace* outDataspace,
1860 uint8_t* outComponentMask) {
1861 return ComposerService::getComposerService()
1862 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1863 outComponentMask);
1864}
1865
Kevin DuBois74e53772018-11-19 10:52:38 -08001866status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1867 bool enable, uint8_t componentMask,
1868 uint64_t maxFrames) {
1869 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1870 componentMask,
1871 maxFrames);
1872}
1873
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001874status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1875 uint64_t maxFrames, uint64_t timestamp,
1876 DisplayedFrameStats* outStats) {
1877 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1878 timestamp, outStats);
1879}
Marissa Wall35187b32019-01-08 10:08:52 -08001880
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001881status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1882 bool* outIsWideColorDisplay) {
1883 return ComposerService::getComposerService()->isWideColorDisplay(display,
1884 outIsWideColorDisplay);
1885}
1886
Kevin DuBois00c66832019-02-18 16:21:31 -08001887status_t SurfaceComposerClient::addRegionSamplingListener(
1888 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1889 const sp<IRegionSamplingListener>& listener) {
1890 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1891 stopLayerHandle,
1892 listener);
1893}
1894
1895status_t SurfaceComposerClient::removeRegionSamplingListener(
1896 const sp<IRegionSamplingListener>& listener) {
1897 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1898}
1899
Dan Gittik57e63c52019-01-18 16:37:54 +00001900bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1901 bool support = false;
1902 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1903 return support;
1904}
1905
1906status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1907 float brightness) {
1908 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1909}
1910
Lais Andrade3a6e47d2020-04-02 11:20:16 +01001911status_t SurfaceComposerClient::notifyPowerBoost(int32_t boostId) {
1912 return ComposerService::getComposerService()->notifyPowerBoost(boostId);
Ady Abraham8532d012019-05-08 14:50:56 -07001913}
1914
Vishnu Nairb13bb952019-11-15 10:24:08 -08001915status_t SurfaceComposerClient::setGlobalShadowSettings(const half4& ambientColor,
1916 const half4& spotColor, float lightPosY,
1917 float lightPosZ, float lightRadius) {
1918 return ComposerService::getComposerService()->setGlobalShadowSettings(ambientColor, spotColor,
1919 lightPosY, lightPosZ,
1920 lightRadius);
1921}
1922
Mathias Agopian698c0872011-06-28 19:09:31 -07001923// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924
Dominik Laskowski718f9602019-11-09 20:01:35 -08001925status_t ScreenshotClient::capture(const sp<IBinder>& display, ui::Dataspace reqDataSpace,
1926 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Peiyong Lin0e003c92018-09-17 11:09:51 -07001927 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Dominik Laskowski718f9602019-11-09 20:01:35 -08001928 ui::Rotation rotation, bool captureSecureLayers,
Robert Carr108b2c72019-04-02 16:32:58 -07001929 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001930 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001931 if (s == nullptr) return NO_INIT;
Dominik Laskowski718f9602019-11-09 20:01:35 -08001932 status_t ret = s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace,
1933 reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1934 useIdentityTransform, rotation, captureSecureLayers);
Robert Carr673134e2017-01-09 19:48:38 -08001935 if (ret != NO_ERROR) {
1936 return ret;
1937 }
Robert Carr673134e2017-01-09 19:48:38 -08001938 return ret;
1939}
1940
Dominik Laskowski718f9602019-11-09 20:01:35 -08001941status_t ScreenshotClient::capture(const sp<IBinder>& display, ui::Dataspace reqDataSpace,
1942 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Robert Carrfa8855f2019-02-19 10:05:00 -08001943 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Dominik Laskowski718f9602019-11-09 20:01:35 -08001944 ui::Rotation rotation, sp<GraphicBuffer>* outBuffer) {
Robert Carr108b2c72019-04-02 16:32:58 -07001945 bool ignored;
1946 return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1947 useIdentityTransform, rotation, false, outBuffer, ignored);
Robert Carrfa8855f2019-02-19 10:05:00 -08001948}
1949
chaviw93df2ea2019-04-30 16:45:12 -07001950status_t ScreenshotClient::capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
1951 sp<GraphicBuffer>* outBuffer) {
1952 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1953 if (s == nullptr) return NO_INIT;
1954 return s->captureScreen(displayOrLayerStack, outDataspace, outBuffer);
1955}
1956
Dominik Laskowski718f9602019-11-09 20:01:35 -08001957status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle, ui::Dataspace reqDataSpace,
1958 ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001959 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001960 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001961 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001962 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
Robert Carr866455f2019-04-02 16:28:26 -07001963 sourceCrop, {}, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001964 return ret;
1965}
1966
Robert Carr866455f2019-04-02 16:28:26 -07001967status_t ScreenshotClient::captureChildLayers(
Dominik Laskowski718f9602019-11-09 20:01:35 -08001968 const sp<IBinder>& layerHandle, ui::Dataspace reqDataSpace, ui::PixelFormat reqPixelFormat,
1969 const Rect& sourceCrop,
Robert Carr866455f2019-04-02 16:28:26 -07001970 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& excludeHandles,
1971 float frameScale, sp<GraphicBuffer>* outBuffer) {
Robert Carr578038f2018-03-09 12:25:24 -08001972 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001973 if (s == nullptr) return NO_INIT;
Robert Carr866455f2019-04-02 16:28:26 -07001974 status_t ret =
1975 s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1976 excludeHandles, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001977 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001978}
Dominik Laskowski718f9602019-11-09 20:01:35 -08001979
1980} // namespace android