blob: dfc61851c245d17f6263a9ad289c891ccd5d461a [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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Robert Carr673134e2017-01-09 19:48:38 -080036#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070037#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/ISurfaceComposerClient.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070041#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080042#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Robert Carr2c358bf2018-08-08 15:58:15 -070045#ifndef NO_INPUT
46#include <input/InputWindow.h>
47#endif
48
Mathias Agopian41f673c2011-11-17 17:48:35 -080049#include <private/gui/ComposerService.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Marissa Wall73411622019-01-25 10:45:41 -080051// This server size should always be smaller than the server cache size
52#define BUFFER_CACHE_MAX_SIZE 64
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070055
56using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057// ---------------------------------------------------------------------------
58
Mathias Agopian7e27f052010-05-28 14:22:23 -070059ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
60
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061ComposerService::ComposerService()
62: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070063 Mutex::Autolock _l(mLock);
64 connectLocked();
65}
66
67void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 const String16 name("SurfaceFlinger");
69 while (getService(name, &mComposerService) != NO_ERROR) {
70 usleep(250000);
71 }
Yi Konga03e0442018-07-17 11:16:57 -070072 assert(mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070073
74 // Create the death listener.
75 class DeathObserver : public IBinder::DeathRecipient {
76 ComposerService& mComposerService;
77 virtual void binderDied(const wp<IBinder>& who) {
78 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
79 who.unsafe_get());
80 mComposerService.composerServiceDied();
81 }
82 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070083 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -070084 };
85
86 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080087 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070088}
89
Andy McFadden6652b3e2012-09-06 18:45:56 -070090/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
91 ComposerService& instance = ComposerService::getInstance();
92 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -070093 if (instance.mComposerService == nullptr) {
Andy McFadden6652b3e2012-09-06 18:45:56 -070094 ComposerService::getInstance().connectLocked();
Yi Konga03e0442018-07-17 11:16:57 -070095 assert(instance.mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070096 ALOGD("ComposerService reconnected");
97 }
98 return instance.mComposerService;
99}
100
101void ComposerService::composerServiceDied()
102{
103 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700104 mComposerService = nullptr;
105 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106}
107
Robert Carrfb4d58b2019-01-15 09:21:27 -0800108class DefaultComposerClient: public Singleton<DefaultComposerClient> {
109 Mutex mLock;
110 sp<SurfaceComposerClient> mClient;
111 friend class Singleton<ComposerService>;
112public:
113 static sp<SurfaceComposerClient> getComposerClient() {
114 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
115 Mutex::Autolock _l(dc.mLock);
116 if (dc.mClient == nullptr) {
117 dc.mClient = new SurfaceComposerClient;
118 }
119 return dc.mClient;
120 }
121};
122ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
123
124
125sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
126 return DefaultComposerClient::getComposerClient();
127}
128
Mathias Agopian7e27f052010-05-28 14:22:23 -0700129// ---------------------------------------------------------------------------
130
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700131// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
132// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
133// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
134
Marissa Wallc837b5e2018-10-12 10:04:44 -0700135// 0 is an invalid callback id
136TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
137
138CallbackId TransactionCompletedListener::getNextIdLocked() {
139 return mCallbackIdCounter++;
140}
141
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700142sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
143 static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener;
144 return sInstance;
145}
146
Marissa Wallc837b5e2018-10-12 10:04:44 -0700147sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
148 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
149}
150
151void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700152 if (mListening) {
153 return;
154 }
155 ProcessState::self()->startThreadPool();
156 mListening = true;
157}
158
Marissa Wall80d94ad2019-01-18 16:04:36 -0800159CallbackId TransactionCompletedListener::addCallbackFunction(
160 const TransactionCompletedCallback& callbackFunction,
161 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
162 surfaceControls) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700163 std::lock_guard<std::mutex> lock(mMutex);
164 startListeningLocked();
165
166 CallbackId callbackId = getNextIdLocked();
Marissa Wall80d94ad2019-01-18 16:04:36 -0800167 mCallbacks[callbackId].callbackFunction = callbackFunction;
168
169 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
170
171 for (const auto& surfaceControl : surfaceControls) {
172 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
173 }
174
Marissa Wallc837b5e2018-10-12 10:04:44 -0700175 return callbackId;
176}
177
Marissa Wall80d94ad2019-01-18 16:04:36 -0800178void TransactionCompletedListener::addSurfaceControlToCallbacks(
179 const sp<SurfaceControl>& surfaceControl,
180 const std::unordered_set<CallbackId>& callbackIds) {
181 std::lock_guard<std::mutex> lock(mMutex);
182
183 for (auto callbackId : callbackIds) {
184 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
185 std::forward_as_tuple(
186 surfaceControl->getHandle()),
187 std::forward_as_tuple(surfaceControl));
188 }
189}
190
Marissa Walle2ffb422018-10-12 11:33:52 -0700191void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800192 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700193
Marissa Wall80d94ad2019-01-18 16:04:36 -0800194 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
195 * callbackIds, except for when Transactions are merged together. This probably cannot be
196 * solved before this point because the Transactions could be merged together and applied in a
197 * different process.
198 *
199 * Fortunately, we get all the callbacks for this listener for the same frame together at the
200 * same time. This means if any Transactions were merged together, we will get their callbacks
201 * at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps for all the
202 * callbackIds to generate one super map that contains all the sp<IBinder> to sp<SurfaceControl>
203 * that could possibly exist for the callbacks.
204 */
205 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, IBinderHash> surfaceControls;
Marissa Walld600d572019-03-26 15:38:50 -0700206 for (const auto& transactionStats : listenerStats.transactionStats) {
207 for (auto callbackId : transactionStats.callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800208 auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId];
209 surfaceControls.insert(callbackSurfaceControls.begin(), callbackSurfaceControls.end());
210 }
211 }
212
Marissa Walld600d572019-03-26 15:38:50 -0700213 for (const auto& transactionStats : listenerStats.transactionStats) {
214 for (auto callbackId : transactionStats.callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800215 auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId];
216 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) {
222 surfaceControlStats.emplace_back(surfaceControls[surfaceStats.surfaceControl],
223 surfaceStats.acquireTime,
224 surfaceStats.previousReleaseFence);
225 }
226
227 callbackFunction(transactionStats.latchTime, transactionStats.presentFence,
228 surfaceControlStats);
Marissa Walle2ffb422018-10-12 11:33:52 -0700229 mCallbacks.erase(callbackId);
230 }
231 }
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700232}
233
234// ---------------------------------------------------------------------------
235
Marissa Wall78b72202019-03-15 14:58:34 -0700236void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId);
237
Marissa Wall73411622019-01-25 10:45:41 -0800238class BufferCache : public Singleton<BufferCache> {
239public:
240 BufferCache() : token(new BBinder()) {}
241
242 sp<IBinder> getToken() {
243 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
244 }
245
Marissa Wall78b72202019-03-15 14:58:34 -0700246 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800247 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800248
Marissa Wall78b72202019-03-15 14:58:34 -0700249 auto itr = mBuffers.find(buffer->getId());
Marissa Wall73411622019-01-25 10:45:41 -0800250 if (itr == mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700251 return BAD_VALUE;
Marissa Wall73411622019-01-25 10:45:41 -0800252 }
Marissa Wall78b72202019-03-15 14:58:34 -0700253 itr->second = getCounter();
254 *cacheId = buffer->getId();
255 return NO_ERROR;
Marissa Wall73411622019-01-25 10:45:41 -0800256 }
257
Marissa Wall78b72202019-03-15 14:58:34 -0700258 uint64_t cache(const sp<GraphicBuffer>& buffer) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800259 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800260
Marissa Wall78b72202019-03-15 14:58:34 -0700261 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
262 evictLeastRecentlyUsedBuffer();
263 }
Marissa Wall73411622019-01-25 10:45:41 -0800264
Marissa Wall78b72202019-03-15 14:58:34 -0700265 buffer->addDeathCallback(bufferCacheCallback, nullptr);
266
267 mBuffers[buffer->getId()] = getCounter();
268 return buffer->getId();
269 }
270
271 void uncache(uint64_t cacheId) {
272 std::lock_guard<std::mutex> lock(mMutex);
273 uncacheLocked(cacheId);
274 }
275
276 void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) {
277 mBuffers.erase(cacheId);
278 SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
Marissa Wall73411622019-01-25 10:45:41 -0800279 }
280
281private:
Marissa Wall78b72202019-03-15 14:58:34 -0700282 void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
Marissa Wall73411622019-01-25 10:45:41 -0800283 auto itr = mBuffers.begin();
Marissa Wall78b72202019-03-15 14:58:34 -0700284 uint64_t minCounter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800285 auto minBuffer = itr;
286 itr++;
287
288 while (itr != mBuffers.end()) {
Marissa Wall78b72202019-03-15 14:58:34 -0700289 uint64_t counter = itr->second;
Marissa Wall73411622019-01-25 10:45:41 -0800290 if (counter < minCounter) {
291 minCounter = counter;
292 minBuffer = itr;
293 }
294 itr++;
295 }
Marissa Wall78b72202019-03-15 14:58:34 -0700296 uncacheLocked(minBuffer->first);
Marissa Wall73411622019-01-25 10:45:41 -0800297 }
298
299 uint64_t getCounter() REQUIRES(mMutex) {
300 static uint64_t counter = 0;
301 return counter++;
302 }
303
Marissa Wall73411622019-01-25 10:45:41 -0800304 std::mutex mMutex;
Marissa Wall78b72202019-03-15 14:58:34 -0700305 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800306
307 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
308 sp<IBinder> token;
309};
310
311ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
312
Marissa Wall78b72202019-03-15 14:58:34 -0700313void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId) {
314 // GraphicBuffer id's are used as the cache ids.
315 BufferCache::getInstance().uncache(graphicBufferId);
316}
317
Marissa Wall73411622019-01-25 10:45:41 -0800318// ---------------------------------------------------------------------------
319
Marissa Wall17b4e452018-12-26 16:32:34 -0800320SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
321 : mForceSynchronous(other.mForceSynchronous),
322 mTransactionNestCount(other.mTransactionNestCount),
323 mAnimation(other.mAnimation),
324 mEarlyWakeup(other.mEarlyWakeup),
325 mDesiredPresentTime(other.mDesiredPresentTime) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700326 mDisplayStates = other.mDisplayStates;
327 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800328 mInputWindowCommands = other.mInputWindowCommands;
Mathias Agopian698c0872011-06-28 19:09:31 -0700329}
330
Robert Carr2c5f6d22017-09-26 12:30:35 -0700331SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
chaviw8e3fe5d2018-02-22 10:55:42 -0800332 for (auto const& kv : other.mComposerStates) {
333 if (mComposerStates.count(kv.first) == 0) {
334 mComposerStates[kv.first] = kv.second;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700335 } else {
chaviw8e3fe5d2018-02-22 10:55:42 -0800336 mComposerStates[kv.first].state.merge(kv.second.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700337 }
338 }
339 other.mComposerStates.clear();
340
341 for (auto const& state : other.mDisplayStates) {
342 ssize_t index = mDisplayStates.indexOf(state);
343 if (index < 0) {
344 mDisplayStates.add(state);
345 } else {
346 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
347 }
348 }
349 other.mDisplayStates.clear();
350
Marissa Wallc837b5e2018-10-12 10:04:44 -0700351 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
352 auto& [callbackIds, surfaceControls] = callbackInfo;
353 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
354 callbackIds.begin()),
355 std::make_move_iterator(callbackIds.end()));
356 mListenerCallbacks[listener]
357 .surfaceControls.insert(std::make_move_iterator(surfaceControls.begin()),
358 std::make_move_iterator(surfaceControls.end()));
359 }
360 other.mListenerCallbacks.clear();
361
chaviw273171b2018-12-26 11:46:30 -0800362 mInputWindowCommands.merge(other.mInputWindowCommands);
Vishnu Nairec0ab382019-02-13 15:32:56 -0800363 other.mInputWindowCommands.clear();
chaviw273171b2018-12-26 11:46:30 -0800364
Marissa Wall78b72202019-03-15 14:58:34 -0700365 mContainsBuffer = other.mContainsBuffer;
366 other.mContainsBuffer = false;
367
Jorim Jaggi40deef52019-07-22 17:18:52 +0200368 mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup;
369 other.mEarlyWakeup = false;
370
Robert Carr2c5f6d22017-09-26 12:30:35 -0700371 return *this;
372}
373
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800374void SurfaceComposerClient::doDropReferenceTransaction(const sp<IBinder>& handle,
375 const sp<ISurfaceComposerClient>& client) {
376 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
377 Vector<ComposerState> composerStates;
378 Vector<DisplayState> displayStates;
379
380 ComposerState s;
381 s.client = client;
382 s.state.surface = handle;
383 s.state.what |= layer_state_t::eReparent;
384 s.state.parentHandleForChild = nullptr;
385
386 composerStates.add(s);
Valerie Haufa889122019-04-15 13:56:05 -0700387 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
388 sf->setTransactionState(composerStates, displayStates, 0, applyToken, {}, -1, {}, {});
Marissa Wall78b72202019-03-15 14:58:34 -0700389}
390
391void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
392 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
393
Marissa Wall947d34e2019-03-29 14:03:53 -0700394 client_cache_t uncacheBuffer;
Marissa Wall78b72202019-03-15 14:58:34 -0700395 uncacheBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700396 uncacheBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700397
Valerie Haufa889122019-04-15 13:56:05 -0700398 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
399 sf->setTransactionState({}, {}, 0, applyToken, {}, -1, uncacheBuffer, {});
Marissa Wall78b72202019-03-15 14:58:34 -0700400}
401
402void SurfaceComposerClient::Transaction::cacheBuffers() {
403 if (!mContainsBuffer) {
404 return;
405 }
406
407 size_t count = 0;
408 for (auto& [sc, cs] : mComposerStates) {
409 layer_state_t* s = getLayerState(sc);
410 if (!(s->what & layer_state_t::eBufferChanged)) {
411 continue;
412 }
413
Marissa Wall00597242019-03-27 10:35:19 -0700414 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste
415 // time trying to cache them.
416 if (!s->buffer) {
417 continue;
418 }
419
Marissa Wall78b72202019-03-15 14:58:34 -0700420 uint64_t cacheId = 0;
421 status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId);
422 if (ret == NO_ERROR) {
Marissa Walla141abe2019-03-27 16:28:07 -0700423 s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
Marissa Wall78b72202019-03-15 14:58:34 -0700424 s->buffer = nullptr;
425 } else {
426 cacheId = BufferCache::getInstance().cache(s->buffer);
427 }
428 s->what |= layer_state_t::eCachedBufferChanged;
429 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wall947d34e2019-03-29 14:03:53 -0700430 s->cachedBuffer.id = cacheId;
Marissa Wall78b72202019-03-15 14:58:34 -0700431
432 // If we have more buffers than the size of the cache, we should stop caching so we don't
433 // evict other buffers in this transaction
434 count++;
435 if (count >= BUFFER_CACHE_MAX_SIZE) {
436 break;
437 }
438 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800439}
440
Robert Carr4cdc58f2017-08-23 14:22:20 -0700441status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
442 if (mStatus != NO_ERROR) {
443 return mStatus;
444 }
445
446 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
447
Marissa Wall3dad52d2019-03-22 14:03:19 -0700448 std::vector<ListenerCallbacks> listenerCallbacks;
449
Marissa Wallc837b5e2018-10-12 10:04:44 -0700450 // For every listener with registered callbacks
451 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
452 auto& [callbackIds, surfaceControls] = callbackInfo;
453 if (callbackIds.empty()) {
454 continue;
455 }
456
Marissa Wall3dad52d2019-03-22 14:03:19 -0700457 listenerCallbacks.emplace_back(listener, std::move(callbackIds));
Marissa Walle2ffb422018-10-12 11:33:52 -0700458
Marissa Wallc837b5e2018-10-12 10:04:44 -0700459 // If the listener has any SurfaceControls set on this Transaction update the surface state
460 for (const auto& surfaceControl : surfaceControls) {
461 layer_state_t* s = getLayerState(surfaceControl);
462 if (!s) {
463 ALOGE("failed to get layer state");
464 continue;
465 }
Marissa Wall3dad52d2019-03-22 14:03:19 -0700466 s->what |= layer_state_t::eHasListenerCallbacksChanged;
467 s->hasListenerCallbacks = true;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700468 }
469 }
470 mListenerCallbacks.clear();
471
Marissa Wall78b72202019-03-15 14:58:34 -0700472 cacheBuffers();
473
Robert Carr4cdc58f2017-08-23 14:22:20 -0700474 Vector<ComposerState> composerStates;
475 Vector<DisplayState> displayStates;
476 uint32_t flags = 0;
477
478 mForceSynchronous |= synchronous;
479
chaviw8e3fe5d2018-02-22 10:55:42 -0800480 for (auto const& kv : mComposerStates){
481 composerStates.add(kv.second);
482 }
483
Robert Carr4cdc58f2017-08-23 14:22:20 -0700484 mComposerStates.clear();
485
486 displayStates = mDisplayStates;
487 mDisplayStates.clear();
488
489 if (mForceSynchronous) {
490 flags |= ISurfaceComposer::eSynchronous;
491 }
492 if (mAnimation) {
493 flags |= ISurfaceComposer::eAnimation;
494 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700495 if (mEarlyWakeup) {
496 flags |= ISurfaceComposer::eEarlyWakeup;
497 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700498
499 mForceSynchronous = false;
500 mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700501 mEarlyWakeup = false;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700502
Marissa Wall713b63f2018-10-17 15:42:43 -0700503 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Marissa Wall17b4e452018-12-26 16:32:34 -0800504 sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands,
Marissa Wall78b72202019-03-15 14:58:34 -0700505 mDesiredPresentTime,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700506 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
507 listenerCallbacks);
chaviw273171b2018-12-26 11:46:30 -0800508 mInputWindowCommands.clear();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700509 mStatus = NO_ERROR;
510 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700511}
512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513// ---------------------------------------------------------------------------
514
Robert Carr4cdc58f2017-08-23 14:22:20 -0700515sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700516 return ComposerService::getComposerService()->createDisplay(displayName,
517 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700518}
519
Robert Carr4cdc58f2017-08-23 14:22:20 -0700520void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700521 return ComposerService::getComposerService()->destroyDisplay(display);
522}
523
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800524std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
525 return ComposerService::getComposerService()->getPhysicalDisplayIds();
526}
527
528std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
529 return ComposerService::getComposerService()->getInternalDisplayId();
530}
531
532sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
533 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
534}
535
536sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
537 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700538}
539
Robert Carr4cdc58f2017-08-23 14:22:20 -0700540void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700541 mAnimation = true;
542}
543
Dan Stoza84d619e2018-03-28 17:07:36 -0700544void SurfaceComposerClient::Transaction::setEarlyWakeup() {
545 mEarlyWakeup = true;
546}
547
chaviw763ef572018-02-22 16:04:57 -0800548layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
chaviw8e3fe5d2018-02-22 10:55:42 -0800549 if (mComposerStates.count(sc) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700550 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -0800551 ComposerState s;
552 s.client = sc->getClient()->mClient;
553 s.state.surface = sc->getHandle();
554 mComposerStates[sc] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -0700555 }
556
chaviw8e3fe5d2018-02-22 10:55:42 -0800557 return &(mComposerStates[sc].state);
Mathias Agopian698c0872011-06-28 19:09:31 -0700558}
559
Marissa Wallc837b5e2018-10-12 10:04:44 -0700560void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
561 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800562 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
563 callbackInfo.surfaceControls.insert(sc);
564
565 TransactionCompletedListener::getInstance()
566 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700567}
568
Robert Carr4cdc58f2017-08-23 14:22:20 -0700569SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
570 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -0800571 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700572 if (!s) {
573 mStatus = BAD_INDEX;
574 return *this;
575 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700576 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700577 s->x = x;
578 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700579
580 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700581 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700582}
583
Robert Carr4cdc58f2017-08-23 14:22:20 -0700584SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
585 const sp<SurfaceControl>& sc) {
586 return setFlags(sc, 0, layer_state_t::eLayerHidden);
587}
588
589SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
590 const sp<SurfaceControl>& sc) {
591 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
592}
593
594SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
595 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-22 16:04:57 -0800596 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700597 if (!s) {
598 mStatus = BAD_INDEX;
599 return *this;
600 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700601 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700602 s->w = w;
603 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700604
Marissa Wallc837b5e2018-10-12 10:04:44 -0700605 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700606 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700607}
608
Robert Carr4cdc58f2017-08-23 14:22:20 -0700609SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
610 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800611 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700612 if (!s) {
613 mStatus = BAD_INDEX;
614 return *this;
615 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700616 s->what |= layer_state_t::eLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700617 s->what &= ~layer_state_t::eRelativeLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700618 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700619
620 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700621 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700622}
623
Robert Carr4cdc58f2017-08-23 14:22:20 -0700624SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
Robert Carrdb66e622017-04-10 16:55:57 -0700625 int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800626 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700627 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700628 mStatus = BAD_INDEX;
Robert Carrdb66e622017-04-10 16:55:57 -0700629 }
630 s->what |= layer_state_t::eRelativeLayerChanged;
chaviw32377582019-05-13 11:15:19 -0700631 s->what &= ~layer_state_t::eLayerChanged;
Robert Carrdb66e622017-04-10 16:55:57 -0700632 s->relativeLayerHandle = relativeTo;
633 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700634
635 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700636 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700637}
638
Robert Carr4cdc58f2017-08-23 14:22:20 -0700639SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
640 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700641 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -0800642 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700643 if (!s) {
644 mStatus = BAD_INDEX;
645 return *this;
646 }
Pablo Ceballos53390e12015-08-04 11:25:59 -0700647 if ((mask & layer_state_t::eLayerOpaque) ||
648 (mask & layer_state_t::eLayerHidden) ||
649 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700650 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800651 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700652 s->flags &= ~mask;
653 s->flags |= (flags & mask);
654 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700655
656 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700657 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700658}
659
Robert Carr4cdc58f2017-08-23 14:22:20 -0700660SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
661 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700662 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800663 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700664 if (!s) {
665 mStatus = BAD_INDEX;
666 return *this;
667 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700668 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700669 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700670
671 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700672 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700673}
674
Robert Carr4cdc58f2017-08-23 14:22:20 -0700675SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
676 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800677 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700678 if (!s) {
679 mStatus = BAD_INDEX;
680 return *this;
681 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700682 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700683 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700684
685 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700686 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700687}
688
Robert Carr4cdc58f2017-08-23 14:22:20 -0700689SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
690 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -0800691 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700692 if (!s) {
693 mStatus = BAD_INDEX;
694 return *this;
695 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700696 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700697 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700698
699 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700700 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700701}
702
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800703SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
Garfield Tanb0412cf2019-08-05 16:44:21 -0700704 const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800705 layer_state_t* s = getLayerState(sc);
706 if (!s) {
707 mStatus = BAD_INDEX;
708 return *this;
709 }
710 s->what |= layer_state_t::eMetadataChanged;
Garfield Tanb0412cf2019-08-05 16:44:21 -0700711
712 s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800713
714 registerSurfaceControlForCallback(sc);
715 return *this;
716}
717
Robert Carr4cdc58f2017-08-23 14:22:20 -0700718SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
719 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800720 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800721 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700722 if (!s) {
723 mStatus = BAD_INDEX;
724 return *this;
725 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700726 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700727 layer_state_t::matrix22_t matrix;
728 matrix.dsdx = dsdx;
729 matrix.dtdx = dtdx;
730 matrix.dsdy = dsdy;
731 matrix.dtdy = dtdy;
732 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700733
734 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700735 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700736}
737
Marissa Wallf58c14b2018-07-24 10:50:43 -0700738SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700739 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800740 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700741 if (!s) {
742 mStatus = BAD_INDEX;
743 return *this;
744 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700745 s->what |= layer_state_t::eCropChanged_legacy;
746 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700747
748 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700749 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700750}
751
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700752SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
753 const sp<SurfaceControl>& sc, float cornerRadius) {
754 layer_state_t* s = getLayerState(sc);
755 if (!s) {
756 mStatus = BAD_INDEX;
757 return *this;
758 }
759 s->what |= layer_state_t::eCornerRadiusChanged;
760 s->cornerRadius = cornerRadius;
761 return *this;
762}
763
Marissa Wallf58c14b2018-07-24 10:50:43 -0700764SurfaceComposerClient::Transaction&
765SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
766 const sp<IBinder>& handle,
767 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800768 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700769 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700770 mStatus = BAD_INDEX;
771 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700772 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700773 s->what |= layer_state_t::eDeferTransaction_legacy;
774 s->barrierHandle_legacy = handle;
775 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700776
777 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700778 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800779}
780
Marissa Wallf58c14b2018-07-24 10:50:43 -0700781SurfaceComposerClient::Transaction&
782SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
783 const sp<Surface>& barrierSurface,
784 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800785 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -0800786 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700787 mStatus = BAD_INDEX;
788 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800789 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700790 s->what |= layer_state_t::eDeferTransaction_legacy;
791 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
792 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700793
794 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700795 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700796}
797
Robert Carr4cdc58f2017-08-23 14:22:20 -0700798SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
799 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -0800800 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800801 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -0800802 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700803 mStatus = BAD_INDEX;
804 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800805 }
806 s->what |= layer_state_t::eReparentChildren;
807 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700808
809 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700810 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800811}
812
Robert Carr4cdc58f2017-08-23 14:22:20 -0700813SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
814 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -0700815 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800816 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -0700817 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700818 mStatus = BAD_INDEX;
819 return *this;
chaviw06178942017-07-27 10:25:59 -0700820 }
chaviwf1961f72017-09-18 16:41:07 -0700821 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700822 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700823
824 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700825 return *this;
chaviw06178942017-07-27 10:25:59 -0700826}
827
Robert Carr4cdc58f2017-08-23 14:22:20 -0700828SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
829 const sp<SurfaceControl>& sc,
830 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -0800831 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -0800832 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700833 mStatus = BAD_INDEX;
834 return *this;
835 }
836 s->what |= layer_state_t::eColorChanged;
837 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700838
839 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700840 return *this;
841}
842
Valerie Haudd0b7572019-01-29 14:59:27 -0800843SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
844 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -0800845 layer_state_t* s = getLayerState(sc);
846 if (!s) {
847 mStatus = BAD_INDEX;
848 return *this;
849 }
850
Valerie Haudd0b7572019-01-29 14:59:27 -0800851 s->what |= layer_state_t::eBackgroundColorChanged;
852 s->color = color;
853 s->bgColorAlpha = alpha;
854 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -0800855
856 registerSurfaceControlForCallback(sc);
857 return *this;
858}
859
Marissa Wall61c58622018-07-18 10:12:20 -0700860SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
861 const sp<SurfaceControl>& sc, uint32_t transform) {
862 layer_state_t* s = getLayerState(sc);
863 if (!s) {
864 mStatus = BAD_INDEX;
865 return *this;
866 }
867 s->what |= layer_state_t::eTransformChanged;
868 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700869
870 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700871 return *this;
872}
873
874SurfaceComposerClient::Transaction&
875SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
876 bool transformToDisplayInverse) {
877 layer_state_t* s = getLayerState(sc);
878 if (!s) {
879 mStatus = BAD_INDEX;
880 return *this;
881 }
882 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
883 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700884
885 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700886 return *this;
887}
888
889SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
890 const sp<SurfaceControl>& sc, const Rect& crop) {
891 layer_state_t* s = getLayerState(sc);
892 if (!s) {
893 mStatus = BAD_INDEX;
894 return *this;
895 }
896 s->what |= layer_state_t::eCropChanged;
897 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700898
899 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700900 return *this;
901}
902
Marissa Wall861616d2018-10-22 12:52:23 -0700903SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
904 const sp<SurfaceControl>& sc, const Rect& frame) {
905 layer_state_t* s = getLayerState(sc);
906 if (!s) {
907 mStatus = BAD_INDEX;
908 return *this;
909 }
910 s->what |= layer_state_t::eFrameChanged;
911 s->frame = frame;
912
913 registerSurfaceControlForCallback(sc);
914 return *this;
915}
916
Marissa Wall61c58622018-07-18 10:12:20 -0700917SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
918 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
919 layer_state_t* s = getLayerState(sc);
920 if (!s) {
921 mStatus = BAD_INDEX;
922 return *this;
923 }
Marissa Wall78b72202019-03-15 14:58:34 -0700924 s->what |= layer_state_t::eBufferChanged;
925 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -0800926
927 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -0700928
929 mContainsBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -0800930 return *this;
931}
932
Marissa Wall61c58622018-07-18 10:12:20 -0700933SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
934 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
935 layer_state_t* s = getLayerState(sc);
936 if (!s) {
937 mStatus = BAD_INDEX;
938 return *this;
939 }
940 s->what |= layer_state_t::eAcquireFenceChanged;
941 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700942
943 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700944 return *this;
945}
946
947SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
948 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
949 layer_state_t* s = getLayerState(sc);
950 if (!s) {
951 mStatus = BAD_INDEX;
952 return *this;
953 }
954 s->what |= layer_state_t::eDataspaceChanged;
955 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700956
957 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700958 return *this;
959}
960
961SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
962 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
963 layer_state_t* s = getLayerState(sc);
964 if (!s) {
965 mStatus = BAD_INDEX;
966 return *this;
967 }
968 s->what |= layer_state_t::eHdrMetadataChanged;
969 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700970
971 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700972 return *this;
973}
974
975SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
976 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
977 layer_state_t* s = getLayerState(sc);
978 if (!s) {
979 mStatus = BAD_INDEX;
980 return *this;
981 }
982 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
983 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700984
985 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700986 return *this;
987}
988
989SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
990 const sp<SurfaceControl>& sc, int32_t api) {
991 layer_state_t* s = getLayerState(sc);
992 if (!s) {
993 mStatus = BAD_INDEX;
994 return *this;
995 }
996 s->what |= layer_state_t::eApiChanged;
997 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700998
999 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -07001000 return *this;
1001}
1002
1003SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1004 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1005 layer_state_t* s = getLayerState(sc);
1006 if (!s) {
1007 mStatus = BAD_INDEX;
1008 return *this;
1009 }
1010 s->what |= layer_state_t::eSidebandStreamChanged;
1011 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001012
1013 registerSurfaceControlForCallback(sc);
1014 return *this;
1015}
1016
Marissa Wall17b4e452018-12-26 16:32:34 -08001017SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1018 nsecs_t desiredPresentTime) {
1019 mDesiredPresentTime = desiredPresentTime;
1020 return *this;
1021}
1022
Peiyong Linc502cb72019-03-01 15:00:23 -08001023SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1024 const sp<SurfaceControl>& sc, const bool agnostic) {
1025 layer_state_t* s = getLayerState(sc);
1026 if (!s) {
1027 mStatus = BAD_INDEX;
1028 return *this;
1029 }
1030 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1031 s->colorSpaceAgnostic = agnostic;
1032
1033 registerSurfaceControlForCallback(sc);
1034 return *this;
1035}
1036
Marissa Wallc837b5e2018-10-12 10:04:44 -07001037SurfaceComposerClient::Transaction&
1038SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001039 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001040 auto listener = TransactionCompletedListener::getInstance();
1041
Marissa Wall80d94ad2019-01-18 16:04:36 -08001042 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1043 std::placeholders::_2, std::placeholders::_3);
1044 const auto& surfaceControls =
1045 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001046
Marissa Wall80d94ad2019-01-18 16:04:36 -08001047 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001048
1049 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1050 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001051 return *this;
1052}
1053
Robert Carr4cdc58f2017-08-23 14:22:20 -07001054SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1055 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001056 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001057 if (!s) {
1058 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -08001059 }
1060 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001061
1062 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001063 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001064}
1065
Robert Carr4cdc58f2017-08-23 14:22:20 -07001066SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1067 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001068 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001069 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001070 mStatus = BAD_INDEX;
1071 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001072 }
1073
1074 switch (overrideScalingMode) {
1075 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1076 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1077 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1078 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1079 case -1:
1080 break;
1081 default:
1082 ALOGE("unknown scaling mode: %d",
1083 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001084 mStatus = BAD_VALUE;
1085 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001086 }
1087
1088 s->what |= layer_state_t::eOverrideScalingModeChanged;
1089 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001090
1091 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001092 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001093}
1094
Robert Carr4cdc58f2017-08-23 14:22:20 -07001095SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1096 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001097 layer_state_t* s = getLayerState(sc);
Robert Carr82364e32016-05-15 11:27:47 -07001098 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001099 mStatus = BAD_INDEX;
1100 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001101 }
Robert Carr99e27f02016-06-16 15:18:02 -07001102 s->what |= layer_state_t::eGeometryAppliesWithResize;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001103
1104 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001105 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001106}
1107
Robert Carr2c358bf2018-08-08 15:58:15 -07001108#ifndef NO_INPUT
1109SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1110 const sp<SurfaceControl>& sc,
1111 const InputWindowInfo& info) {
1112 layer_state_t* s = getLayerState(sc);
1113 if (!s) {
1114 mStatus = BAD_INDEX;
1115 return *this;
1116 }
1117 s->inputInfo = info;
1118 s->what |= layer_state_t::eInputInfoChanged;
1119 return *this;
1120}
chaviw273171b2018-12-26 11:46:30 -08001121
1122SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus(
1123 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
1124 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand;
1125 transferTouchFocusCommand.fromToken = fromToken;
1126 transferTouchFocusCommand.toToken = toToken;
1127 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand);
1128 return *this;
1129}
1130
chaviwa911b102019-02-14 10:18:33 -08001131SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1132 mInputWindowCommands.syncInputWindows = true;
1133 return *this;
1134}
1135
Robert Carr2c358bf2018-08-08 15:58:15 -07001136#endif
1137
Peiyong Lind3788632018-09-18 16:01:31 -07001138SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1139 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1140 layer_state_t* s = getLayerState(sc);
1141 if (!s) {
1142 mStatus = BAD_INDEX;
1143 return *this;
1144 }
1145 s->what |= layer_state_t::eColorTransformChanged;
1146 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001147
1148 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001149 return *this;
1150}
1151
Robert Carrfb4d58b2019-01-15 09:21:27 -08001152SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1153 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1154 setCrop_legacy(sc, source);
1155
1156 int x = dst.left;
1157 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001158
1159 float sourceWidth = source.getWidth();
1160 float sourceHeight = source.getHeight();
1161
1162 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1163 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001164 float matrix[4] = {1, 0, 0, 1};
1165
1166 switch (transform) {
1167 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1168 matrix[0] = -xScale; matrix[1] = 0;
1169 matrix[2] = 0; matrix[3] = yScale;
1170 x += source.getWidth();
1171 break;
1172 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1173 matrix[0] = xScale; matrix[1] = 0;
1174 matrix[2] = 0; matrix[3] = -yScale;
1175 y += source.getHeight();
1176 break;
1177 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1178 matrix[0] = 0; matrix[1] = -yScale;
1179 matrix[2] = xScale; matrix[3] = 0;
1180 x += source.getHeight();
1181 break;
1182 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1183 matrix[0] = -xScale; matrix[1] = 0;
1184 matrix[2] = 0; matrix[3] = -yScale;
1185 x += source.getWidth();
1186 y += source.getHeight();
1187 break;
1188 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1189 matrix[0] = 0; matrix[1] = yScale;
1190 matrix[2] = -xScale; matrix[3] = 0;
1191 y += source.getWidth();
1192 break;
1193 default:
1194 matrix[0] = xScale; matrix[1] = 0;
1195 matrix[2] = 0; matrix[3] = yScale;
1196 break;
1197 }
1198 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
1199 setPosition(sc, x, y);
1200
1201 return *this;
1202}
1203
Mathias Agopian698c0872011-06-28 19:09:31 -07001204// ---------------------------------------------------------------------------
1205
chaviw763ef572018-02-22 16:04:57 -08001206DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001207 DisplayState s;
1208 s.token = token;
1209 ssize_t index = mDisplayStates.indexOf(s);
1210 if (index < 0) {
1211 // we don't have it, add an initialized layer_state to our list
1212 s.what = 0;
1213 index = mDisplayStates.add(s);
1214 }
Dan Stozad723bd72014-11-18 10:24:03 -08001215 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001216}
1217
Robert Carr4cdc58f2017-08-23 14:22:20 -07001218status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1219 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001220 if (bufferProducer.get() != nullptr) {
1221 // Make sure that composition can never be stalled by a virtual display
1222 // consumer that isn't processing buffers fast enough.
1223 status_t err = bufferProducer->setAsyncMode(true);
1224 if (err != NO_ERROR) {
1225 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1226 "BufferQueue. This BufferQueue cannot be used for virtual "
1227 "display. (%d)", err);
1228 return err;
1229 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001230 }
chaviw763ef572018-02-22 16:04:57 -08001231 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001232 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001233 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001234 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001235}
1236
Robert Carr4cdc58f2017-08-23 14:22:20 -07001237void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001238 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001239 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001240 s.layerStack = layerStack;
1241 s.what |= DisplayState::eLayerStackChanged;
1242}
1243
Robert Carr4cdc58f2017-08-23 14:22:20 -07001244void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001245 uint32_t orientation,
1246 const Rect& layerStackRect,
1247 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001248 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001249 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001250 s.viewport = layerStackRect;
1251 s.frame = displayRect;
1252 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001253 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001254}
1255
Robert Carr4cdc58f2017-08-23 14:22:20 -07001256void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001257 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001258 s.width = width;
1259 s.height = height;
1260 s.what |= DisplayState::eDisplaySizeChanged;
1261}
1262
Mathias Agopiane57f2922012-08-09 16:29:12 -07001263// ---------------------------------------------------------------------------
1264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001266 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268}
1269
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001270SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1271 : mStatus(NO_ERROR), mClient(client)
1272{
1273}
1274
Mathias Agopian698c0872011-06-28 19:09:31 -07001275void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001276 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001277 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001278 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001279 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001280 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001281 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001282 mStatus = NO_ERROR;
1283 }
1284 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285}
1286
Mathias Agopian698c0872011-06-28 19:09:31 -07001287SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001288 dispose();
1289}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001290
Mathias Agopian698c0872011-06-28 19:09:31 -07001291status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 return mStatus;
1293}
1294
Mathias Agopian698c0872011-06-28 19:09:31 -07001295sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001296 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297}
1298
Mathias Agopiand4784a32010-05-27 19:41:15 -07001299status_t SurfaceComposerClient::linkToComposerDeath(
1300 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001301 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001302 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1303 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304}
1305
Mathias Agopian698c0872011-06-28 19:09:31 -07001306void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001308 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001309 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001310 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001311 client = mClient; // hold ref while lock is held
1312 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001314 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315}
1316
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001317sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1318 PixelFormat format, uint32_t flags,
1319 SurfaceControl* parent,
1320 LayerMetadata metadata) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001321 sp<SurfaceControl> s;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001322 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
Robert Carr3b382ed2018-03-14 13:49:41 -07001323 return s;
1324}
1325
Marissa Wall35187b32019-01-08 10:08:52 -08001326sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1327 uint32_t h, PixelFormat format,
1328 uint32_t flags, Surface* parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001329 LayerMetadata metadata) {
Marissa Wall35187b32019-01-08 10:08:52 -08001330 sp<SurfaceControl> sur;
1331 status_t err = mStatus;
1332
1333 if (mStatus == NO_ERROR) {
1334 sp<IBinder> handle;
1335 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1336 sp<IGraphicBufferProducer> gbp;
1337
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001338 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1339 std::move(metadata), &handle, &gbp);
Marissa Wall35187b32019-01-08 10:08:52 -08001340 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1341 if (err == NO_ERROR) {
1342 return new SurfaceControl(this, handle, gbp, true /* owned */);
1343 }
1344 }
1345 return nullptr;
1346}
1347
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001348status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1349 PixelFormat format,
1350 sp<SurfaceControl>* outSurface, uint32_t flags,
1351 SurfaceControl* parent,
1352 LayerMetadata metadata) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001353 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001354 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001355
Mathias Agopian698c0872011-06-28 19:09:31 -07001356 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001357 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001358 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001359 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001360
1361 if (parent != nullptr) {
1362 parentHandle = parent->getHandle();
1363 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001364
1365 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1366 &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001367 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1368 if (err == NO_ERROR) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001369 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Mathias Agopian698c0872011-06-28 19:09:31 -07001370 }
1371 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001372 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001373}
1374
Svetoslavd85084b2014-03-20 10:28:31 -07001375status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1376 if (mStatus != NO_ERROR) {
1377 return mStatus;
1378 }
1379 return mClient->clearLayerFrameStats(token);
1380}
1381
1382status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1383 FrameStats* outStats) const {
1384 if (mStatus != NO_ERROR) {
1385 return mStatus;
1386 }
1387 return mClient->getLayerFrameStats(token, outStats);
1388}
1389
Mathias Agopian698c0872011-06-28 19:09:31 -07001390// ----------------------------------------------------------------------------
1391
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001392status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001393 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1394 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001395}
1396
1397status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001398 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1399 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001400}
1401
Dan Stoza7f7da322014-05-02 15:26:25 -07001402status_t SurfaceComposerClient::getDisplayConfigs(
1403 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404{
Dan Stoza7f7da322014-05-02 15:26:25 -07001405 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1406}
1407
1408status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
1409 DisplayInfo* info) {
1410 Vector<DisplayInfo> configs;
1411 status_t result = getDisplayConfigs(display, &configs);
1412 if (result != NO_ERROR) {
1413 return result;
1414 }
1415
1416 int activeId = getActiveConfig(display);
1417 if (activeId < 0) {
1418 ALOGE("No active configuration found");
1419 return NAME_NOT_FOUND;
1420 }
1421
Dan Stozad723bd72014-11-18 10:24:03 -08001422 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001423 return NO_ERROR;
1424}
1425
1426int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1427 return ComposerService::getComposerService()->getActiveConfig(display);
1428}
1429
1430status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1431 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432}
1433
Ady Abraham838de062019-02-04 10:24:03 -08001434status_t SurfaceComposerClient::setAllowedDisplayConfigs(
1435 const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) {
1436 return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken,
1437 allowedConfigs);
1438}
1439
Ady Abrahamd9b3ea62019-02-26 14:08:03 -08001440status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
1441 std::vector<int32_t>* outAllowedConfigs) {
1442 return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken,
1443 outAllowedConfigs);
1444}
1445
Michael Wright28f24d02016-07-12 13:30:53 -07001446status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001447 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001448 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1449}
1450
Daniel Solomon42d04562019-01-20 21:03:19 -08001451status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1452 ui::DisplayPrimaries& outPrimaries) {
1453 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1454}
1455
Peiyong Lina52f0292018-03-14 17:26:31 -07001456ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001457 return ComposerService::getComposerService()->getActiveColorMode(display);
1458}
1459
1460status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001461 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001462 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1463}
1464
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001465void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1466 int mode) {
1467 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001468}
1469
Peiyong Linc6780972018-10-28 15:24:08 -07001470status_t SurfaceComposerClient::getCompositionPreference(
1471 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1472 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1473 return ComposerService::getComposerService()
1474 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1475 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001476}
1477
Peiyong Lin08d10512019-01-16 13:27:35 -08001478bool SurfaceComposerClient::getProtectedContentSupport() {
1479 bool supported = false;
1480 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1481 return supported;
1482}
1483
Svetoslavd85084b2014-03-20 10:28:31 -07001484status_t SurfaceComposerClient::clearAnimationFrameStats() {
1485 return ComposerService::getComposerService()->clearAnimationFrameStats();
1486}
1487
1488status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1489 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1490}
1491
Dan Stozac4f471e2016-03-24 09:31:08 -07001492status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1493 HdrCapabilities* outCapabilities) {
1494 return ComposerService::getComposerService()->getHdrCapabilities(display,
1495 outCapabilities);
1496}
1497
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001498status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1499 ui::PixelFormat* outFormat,
1500 ui::Dataspace* outDataspace,
1501 uint8_t* outComponentMask) {
1502 return ComposerService::getComposerService()
1503 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1504 outComponentMask);
1505}
1506
Kevin DuBois74e53772018-11-19 10:52:38 -08001507status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1508 bool enable, uint8_t componentMask,
1509 uint64_t maxFrames) {
1510 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1511 componentMask,
1512 maxFrames);
1513}
1514
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001515status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1516 uint64_t maxFrames, uint64_t timestamp,
1517 DisplayedFrameStats* outStats) {
1518 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1519 timestamp, outStats);
1520}
Marissa Wall35187b32019-01-08 10:08:52 -08001521
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001522status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1523 bool* outIsWideColorDisplay) {
1524 return ComposerService::getComposerService()->isWideColorDisplay(display,
1525 outIsWideColorDisplay);
1526}
1527
Kevin DuBois00c66832019-02-18 16:21:31 -08001528status_t SurfaceComposerClient::addRegionSamplingListener(
1529 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1530 const sp<IRegionSamplingListener>& listener) {
1531 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1532 stopLayerHandle,
1533 listener);
1534}
1535
1536status_t SurfaceComposerClient::removeRegionSamplingListener(
1537 const sp<IRegionSamplingListener>& listener) {
1538 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1539}
1540
Dan Gittik57e63c52019-01-18 16:37:54 +00001541bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1542 bool support = false;
1543 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1544 return support;
1545}
1546
1547status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1548 float brightness) {
1549 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1550}
1551
Ady Abraham8532d012019-05-08 14:50:56 -07001552status_t SurfaceComposerClient::notifyPowerHint(int32_t hintId) {
1553 return ComposerService::getComposerService()->notifyPowerHint(hintId);
1554}
1555
Mathias Agopian698c0872011-06-28 19:09:31 -07001556// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557
Peiyong Lin0e003c92018-09-17 11:09:51 -07001558status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1559 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1560 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Robert Carr108b2c72019-04-02 16:32:58 -07001561 uint32_t rotation, bool captureSecureLayers,
1562 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001563 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001564 if (s == nullptr) return NO_INIT;
Robert Carr108b2c72019-04-02 16:32:58 -07001565 status_t ret =
1566 s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace,
1567 reqPixelFormat, sourceCrop, reqWidth, reqHeight, useIdentityTransform,
1568 static_cast<ISurfaceComposer::Rotation>(rotation),
1569 captureSecureLayers);
Robert Carr673134e2017-01-09 19:48:38 -08001570 if (ret != NO_ERROR) {
1571 return ret;
1572 }
Robert Carr673134e2017-01-09 19:48:38 -08001573 return ret;
1574}
1575
Robert Carrfa8855f2019-02-19 10:05:00 -08001576status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1577 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1578 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
1579 uint32_t rotation, sp<GraphicBuffer>* outBuffer) {
Robert Carr108b2c72019-04-02 16:32:58 -07001580 bool ignored;
1581 return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1582 useIdentityTransform, rotation, false, outBuffer, ignored);
Robert Carrfa8855f2019-02-19 10:05:00 -08001583}
1584
chaviw93df2ea2019-04-30 16:45:12 -07001585status_t ScreenshotClient::capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
1586 sp<GraphicBuffer>* outBuffer) {
1587 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1588 if (s == nullptr) return NO_INIT;
1589 return s->captureScreen(displayOrLayerStack, outDataspace, outBuffer);
1590}
1591
Peiyong Lin0e003c92018-09-17 11:09:51 -07001592status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1593 const ui::Dataspace reqDataSpace,
1594 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001595 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001596 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001597 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001598 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
Robert Carr866455f2019-04-02 16:28:26 -07001599 sourceCrop, {}, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001600 return ret;
1601}
1602
Robert Carr866455f2019-04-02 16:28:26 -07001603status_t ScreenshotClient::captureChildLayers(
1604 const sp<IBinder>& layerHandle, const ui::Dataspace reqDataSpace,
1605 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1606 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& excludeHandles,
1607 float frameScale, sp<GraphicBuffer>* outBuffer) {
Robert Carr578038f2018-03-09 12:25:24 -08001608 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001609 if (s == nullptr) return NO_INIT;
Robert Carr866455f2019-04-02 16:28:26 -07001610 status_t ret =
1611 s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1612 excludeHandles, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001613 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001614}
Mathias Agopian74c40c02010-09-29 13:02:36 -07001615// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616}; // namespace android