blob: def9fe937cec720bc0550d2451602b710bdf752e [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 Jaggief4b6012019-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(
704 const sp<SurfaceControl>& sc, uint32_t key, std::vector<uint8_t> data) {
705 layer_state_t* s = getLayerState(sc);
706 if (!s) {
707 mStatus = BAD_INDEX;
708 return *this;
709 }
710 s->what |= layer_state_t::eMetadataChanged;
711 s->metadata.mMap[key] = std::move(data);
712
713 registerSurfaceControlForCallback(sc);
714 return *this;
715}
716
Robert Carr4cdc58f2017-08-23 14:22:20 -0700717SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
718 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800719 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800720 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700721 if (!s) {
722 mStatus = BAD_INDEX;
723 return *this;
724 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700725 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700726 layer_state_t::matrix22_t matrix;
727 matrix.dsdx = dsdx;
728 matrix.dtdx = dtdx;
729 matrix.dsdy = dsdy;
730 matrix.dtdy = dtdy;
731 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700732
733 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700734 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700735}
736
Marissa Wallf58c14b2018-07-24 10:50:43 -0700737SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700738 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800739 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700740 if (!s) {
741 mStatus = BAD_INDEX;
742 return *this;
743 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700744 s->what |= layer_state_t::eCropChanged_legacy;
745 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700746
747 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700748 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700749}
750
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700751SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
752 const sp<SurfaceControl>& sc, float cornerRadius) {
753 layer_state_t* s = getLayerState(sc);
754 if (!s) {
755 mStatus = BAD_INDEX;
756 return *this;
757 }
758 s->what |= layer_state_t::eCornerRadiusChanged;
759 s->cornerRadius = cornerRadius;
760 return *this;
761}
762
Marissa Wallf58c14b2018-07-24 10:50:43 -0700763SurfaceComposerClient::Transaction&
764SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
765 const sp<IBinder>& handle,
766 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800767 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700768 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700769 mStatus = BAD_INDEX;
770 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700771 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700772 s->what |= layer_state_t::eDeferTransaction_legacy;
773 s->barrierHandle_legacy = handle;
774 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700775
776 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700777 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800778}
779
Marissa Wallf58c14b2018-07-24 10:50:43 -0700780SurfaceComposerClient::Transaction&
781SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
782 const sp<Surface>& barrierSurface,
783 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800784 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -0800785 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700786 mStatus = BAD_INDEX;
787 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800788 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700789 s->what |= layer_state_t::eDeferTransaction_legacy;
790 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
791 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700792
793 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700794 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700795}
796
Robert Carr4cdc58f2017-08-23 14:22:20 -0700797SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
798 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -0800799 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800800 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -0800801 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700802 mStatus = BAD_INDEX;
803 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800804 }
805 s->what |= layer_state_t::eReparentChildren;
806 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700807
808 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700809 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800810}
811
Robert Carr4cdc58f2017-08-23 14:22:20 -0700812SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
813 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -0700814 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800815 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -0700816 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700817 mStatus = BAD_INDEX;
818 return *this;
chaviw06178942017-07-27 10:25:59 -0700819 }
chaviwf1961f72017-09-18 16:41:07 -0700820 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700821 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700822
823 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700824 return *this;
chaviw06178942017-07-27 10:25:59 -0700825}
826
Robert Carr4cdc58f2017-08-23 14:22:20 -0700827SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
828 const sp<SurfaceControl>& sc,
829 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -0800830 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -0800831 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700832 mStatus = BAD_INDEX;
833 return *this;
834 }
835 s->what |= layer_state_t::eColorChanged;
836 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700837
838 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700839 return *this;
840}
841
Valerie Haudd0b7572019-01-29 14:59:27 -0800842SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
843 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -0800844 layer_state_t* s = getLayerState(sc);
845 if (!s) {
846 mStatus = BAD_INDEX;
847 return *this;
848 }
849
Valerie Haudd0b7572019-01-29 14:59:27 -0800850 s->what |= layer_state_t::eBackgroundColorChanged;
851 s->color = color;
852 s->bgColorAlpha = alpha;
853 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -0800854
855 registerSurfaceControlForCallback(sc);
856 return *this;
857}
858
Marissa Wall61c58622018-07-18 10:12:20 -0700859SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
860 const sp<SurfaceControl>& sc, uint32_t transform) {
861 layer_state_t* s = getLayerState(sc);
862 if (!s) {
863 mStatus = BAD_INDEX;
864 return *this;
865 }
866 s->what |= layer_state_t::eTransformChanged;
867 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700868
869 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700870 return *this;
871}
872
873SurfaceComposerClient::Transaction&
874SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
875 bool transformToDisplayInverse) {
876 layer_state_t* s = getLayerState(sc);
877 if (!s) {
878 mStatus = BAD_INDEX;
879 return *this;
880 }
881 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
882 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700883
884 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700885 return *this;
886}
887
888SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
889 const sp<SurfaceControl>& sc, const Rect& crop) {
890 layer_state_t* s = getLayerState(sc);
891 if (!s) {
892 mStatus = BAD_INDEX;
893 return *this;
894 }
895 s->what |= layer_state_t::eCropChanged;
896 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700897
898 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700899 return *this;
900}
901
Marissa Wall861616d2018-10-22 12:52:23 -0700902SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
903 const sp<SurfaceControl>& sc, const Rect& frame) {
904 layer_state_t* s = getLayerState(sc);
905 if (!s) {
906 mStatus = BAD_INDEX;
907 return *this;
908 }
909 s->what |= layer_state_t::eFrameChanged;
910 s->frame = frame;
911
912 registerSurfaceControlForCallback(sc);
913 return *this;
914}
915
Marissa Wall61c58622018-07-18 10:12:20 -0700916SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
917 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
918 layer_state_t* s = getLayerState(sc);
919 if (!s) {
920 mStatus = BAD_INDEX;
921 return *this;
922 }
Marissa Wall78b72202019-03-15 14:58:34 -0700923 s->what |= layer_state_t::eBufferChanged;
924 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -0800925
926 registerSurfaceControlForCallback(sc);
Marissa Wall78b72202019-03-15 14:58:34 -0700927
928 mContainsBuffer = true;
Marissa Wallebc2c052019-01-16 19:16:55 -0800929 return *this;
930}
931
Marissa Wall61c58622018-07-18 10:12:20 -0700932SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
933 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
934 layer_state_t* s = getLayerState(sc);
935 if (!s) {
936 mStatus = BAD_INDEX;
937 return *this;
938 }
939 s->what |= layer_state_t::eAcquireFenceChanged;
940 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700941
942 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700943 return *this;
944}
945
946SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
947 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
948 layer_state_t* s = getLayerState(sc);
949 if (!s) {
950 mStatus = BAD_INDEX;
951 return *this;
952 }
953 s->what |= layer_state_t::eDataspaceChanged;
954 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700955
956 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700957 return *this;
958}
959
960SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
961 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
962 layer_state_t* s = getLayerState(sc);
963 if (!s) {
964 mStatus = BAD_INDEX;
965 return *this;
966 }
967 s->what |= layer_state_t::eHdrMetadataChanged;
968 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700969
970 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700971 return *this;
972}
973
974SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
975 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
976 layer_state_t* s = getLayerState(sc);
977 if (!s) {
978 mStatus = BAD_INDEX;
979 return *this;
980 }
981 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
982 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700983
984 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700985 return *this;
986}
987
988SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
989 const sp<SurfaceControl>& sc, int32_t api) {
990 layer_state_t* s = getLayerState(sc);
991 if (!s) {
992 mStatus = BAD_INDEX;
993 return *this;
994 }
995 s->what |= layer_state_t::eApiChanged;
996 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700997
998 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700999 return *this;
1000}
1001
1002SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
1003 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
1004 layer_state_t* s = getLayerState(sc);
1005 if (!s) {
1006 mStatus = BAD_INDEX;
1007 return *this;
1008 }
1009 s->what |= layer_state_t::eSidebandStreamChanged;
1010 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001011
1012 registerSurfaceControlForCallback(sc);
1013 return *this;
1014}
1015
Marissa Wall17b4e452018-12-26 16:32:34 -08001016SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1017 nsecs_t desiredPresentTime) {
1018 mDesiredPresentTime = desiredPresentTime;
1019 return *this;
1020}
1021
Peiyong Linc502cb72019-03-01 15:00:23 -08001022SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
1023 const sp<SurfaceControl>& sc, const bool agnostic) {
1024 layer_state_t* s = getLayerState(sc);
1025 if (!s) {
1026 mStatus = BAD_INDEX;
1027 return *this;
1028 }
1029 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1030 s->colorSpaceAgnostic = agnostic;
1031
1032 registerSurfaceControlForCallback(sc);
1033 return *this;
1034}
1035
Marissa Wallc837b5e2018-10-12 10:04:44 -07001036SurfaceComposerClient::Transaction&
1037SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001038 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001039 auto listener = TransactionCompletedListener::getInstance();
1040
Marissa Wall80d94ad2019-01-18 16:04:36 -08001041 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1042 std::placeholders::_2, std::placeholders::_3);
1043 const auto& surfaceControls =
1044 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001045
Marissa Wall80d94ad2019-01-18 16:04:36 -08001046 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001047
1048 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1049 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001050 return *this;
1051}
1052
Robert Carr4cdc58f2017-08-23 14:22:20 -07001053SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1054 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001055 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001056 if (!s) {
1057 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -08001058 }
1059 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001060
1061 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001062 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001063}
1064
Robert Carr4cdc58f2017-08-23 14:22:20 -07001065SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1066 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001067 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001068 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001069 mStatus = BAD_INDEX;
1070 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001071 }
1072
1073 switch (overrideScalingMode) {
1074 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1075 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1076 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1077 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1078 case -1:
1079 break;
1080 default:
1081 ALOGE("unknown scaling mode: %d",
1082 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001083 mStatus = BAD_VALUE;
1084 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001085 }
1086
1087 s->what |= layer_state_t::eOverrideScalingModeChanged;
1088 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001089
1090 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001091 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001092}
1093
Robert Carr4cdc58f2017-08-23 14:22:20 -07001094SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1095 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001096 layer_state_t* s = getLayerState(sc);
Robert Carr82364e32016-05-15 11:27:47 -07001097 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001098 mStatus = BAD_INDEX;
1099 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001100 }
Robert Carr99e27f02016-06-16 15:18:02 -07001101 s->what |= layer_state_t::eGeometryAppliesWithResize;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001102
1103 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001104 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001105}
1106
Robert Carr2c358bf2018-08-08 15:58:15 -07001107#ifndef NO_INPUT
1108SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1109 const sp<SurfaceControl>& sc,
1110 const InputWindowInfo& info) {
1111 layer_state_t* s = getLayerState(sc);
1112 if (!s) {
1113 mStatus = BAD_INDEX;
1114 return *this;
1115 }
1116 s->inputInfo = info;
1117 s->what |= layer_state_t::eInputInfoChanged;
1118 return *this;
1119}
chaviw273171b2018-12-26 11:46:30 -08001120
1121SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus(
1122 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
1123 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand;
1124 transferTouchFocusCommand.fromToken = fromToken;
1125 transferTouchFocusCommand.toToken = toToken;
1126 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand);
1127 return *this;
1128}
1129
chaviwa911b102019-02-14 10:18:33 -08001130SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1131 mInputWindowCommands.syncInputWindows = true;
1132 return *this;
1133}
1134
Robert Carr2c358bf2018-08-08 15:58:15 -07001135#endif
1136
Peiyong Lind3788632018-09-18 16:01:31 -07001137SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1138 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1139 layer_state_t* s = getLayerState(sc);
1140 if (!s) {
1141 mStatus = BAD_INDEX;
1142 return *this;
1143 }
1144 s->what |= layer_state_t::eColorTransformChanged;
1145 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001146
1147 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001148 return *this;
1149}
1150
Robert Carrfb4d58b2019-01-15 09:21:27 -08001151SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1152 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1153 setCrop_legacy(sc, source);
1154
1155 int x = dst.left;
1156 int y = dst.top;
Robert Carr66365e42019-04-08 16:58:04 -07001157
1158 float sourceWidth = source.getWidth();
1159 float sourceHeight = source.getHeight();
1160
1161 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth;
1162 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight;
Robert Carrfb4d58b2019-01-15 09:21:27 -08001163 float matrix[4] = {1, 0, 0, 1};
1164
1165 switch (transform) {
1166 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1167 matrix[0] = -xScale; matrix[1] = 0;
1168 matrix[2] = 0; matrix[3] = yScale;
1169 x += source.getWidth();
1170 break;
1171 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1172 matrix[0] = xScale; matrix[1] = 0;
1173 matrix[2] = 0; matrix[3] = -yScale;
1174 y += source.getHeight();
1175 break;
1176 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1177 matrix[0] = 0; matrix[1] = -yScale;
1178 matrix[2] = xScale; matrix[3] = 0;
1179 x += source.getHeight();
1180 break;
1181 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1182 matrix[0] = -xScale; matrix[1] = 0;
1183 matrix[2] = 0; matrix[3] = -yScale;
1184 x += source.getWidth();
1185 y += source.getHeight();
1186 break;
1187 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1188 matrix[0] = 0; matrix[1] = yScale;
1189 matrix[2] = -xScale; matrix[3] = 0;
1190 y += source.getWidth();
1191 break;
1192 default:
1193 matrix[0] = xScale; matrix[1] = 0;
1194 matrix[2] = 0; matrix[3] = yScale;
1195 break;
1196 }
1197 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
1198 setPosition(sc, x, y);
1199
1200 return *this;
1201}
1202
Mathias Agopian698c0872011-06-28 19:09:31 -07001203// ---------------------------------------------------------------------------
1204
chaviw763ef572018-02-22 16:04:57 -08001205DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001206 DisplayState s;
1207 s.token = token;
1208 ssize_t index = mDisplayStates.indexOf(s);
1209 if (index < 0) {
1210 // we don't have it, add an initialized layer_state to our list
1211 s.what = 0;
1212 index = mDisplayStates.add(s);
1213 }
Dan Stozad723bd72014-11-18 10:24:03 -08001214 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001215}
1216
Robert Carr4cdc58f2017-08-23 14:22:20 -07001217status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1218 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001219 if (bufferProducer.get() != nullptr) {
1220 // Make sure that composition can never be stalled by a virtual display
1221 // consumer that isn't processing buffers fast enough.
1222 status_t err = bufferProducer->setAsyncMode(true);
1223 if (err != NO_ERROR) {
1224 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1225 "BufferQueue. This BufferQueue cannot be used for virtual "
1226 "display. (%d)", err);
1227 return err;
1228 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001229 }
chaviw763ef572018-02-22 16:04:57 -08001230 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001231 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001232 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001233 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001234}
1235
Robert Carr4cdc58f2017-08-23 14:22:20 -07001236void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001237 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001238 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001239 s.layerStack = layerStack;
1240 s.what |= DisplayState::eLayerStackChanged;
1241}
1242
Robert Carr4cdc58f2017-08-23 14:22:20 -07001243void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001244 uint32_t orientation,
1245 const Rect& layerStackRect,
1246 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001247 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001248 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001249 s.viewport = layerStackRect;
1250 s.frame = displayRect;
1251 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001252 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001253}
1254
Robert Carr4cdc58f2017-08-23 14:22:20 -07001255void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001256 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001257 s.width = width;
1258 s.height = height;
1259 s.what |= DisplayState::eDisplaySizeChanged;
1260}
1261
Mathias Agopiane57f2922012-08-09 16:29:12 -07001262// ---------------------------------------------------------------------------
1263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001265 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267}
1268
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001269SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1270 : mStatus(NO_ERROR), mClient(client)
1271{
1272}
1273
Mathias Agopian698c0872011-06-28 19:09:31 -07001274void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001275 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001276 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001277 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001278 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001279 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001280 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001281 mStatus = NO_ERROR;
1282 }
1283 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284}
1285
Mathias Agopian698c0872011-06-28 19:09:31 -07001286SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001287 dispose();
1288}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001289
Mathias Agopian698c0872011-06-28 19:09:31 -07001290status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return mStatus;
1292}
1293
Mathias Agopian698c0872011-06-28 19:09:31 -07001294sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001295 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296}
1297
Mathias Agopiand4784a32010-05-27 19:41:15 -07001298status_t SurfaceComposerClient::linkToComposerDeath(
1299 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001300 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001301 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1302 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303}
1304
Mathias Agopian698c0872011-06-28 19:09:31 -07001305void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001307 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001308 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001309 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001310 client = mClient; // hold ref while lock is held
1311 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001313 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314}
1315
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001316sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1317 PixelFormat format, uint32_t flags,
1318 SurfaceControl* parent,
1319 LayerMetadata metadata) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001320 sp<SurfaceControl> s;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001321 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
Robert Carr3b382ed2018-03-14 13:49:41 -07001322 return s;
1323}
1324
Marissa Wall35187b32019-01-08 10:08:52 -08001325sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1326 uint32_t h, PixelFormat format,
1327 uint32_t flags, Surface* parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001328 LayerMetadata metadata) {
Marissa Wall35187b32019-01-08 10:08:52 -08001329 sp<SurfaceControl> sur;
1330 status_t err = mStatus;
1331
1332 if (mStatus == NO_ERROR) {
1333 sp<IBinder> handle;
1334 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1335 sp<IGraphicBufferProducer> gbp;
1336
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001337 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1338 std::move(metadata), &handle, &gbp);
Marissa Wall35187b32019-01-08 10:08:52 -08001339 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1340 if (err == NO_ERROR) {
1341 return new SurfaceControl(this, handle, gbp, true /* owned */);
1342 }
1343 }
1344 return nullptr;
1345}
1346
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001347status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1348 PixelFormat format,
1349 sp<SurfaceControl>* outSurface, uint32_t flags,
1350 SurfaceControl* parent,
1351 LayerMetadata metadata) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001352 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001353 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001354
Mathias Agopian698c0872011-06-28 19:09:31 -07001355 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001356 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001357 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001358 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001359
1360 if (parent != nullptr) {
1361 parentHandle = parent->getHandle();
1362 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001363
1364 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1365 &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001366 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1367 if (err == NO_ERROR) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001368 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Mathias Agopian698c0872011-06-28 19:09:31 -07001369 }
1370 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001371 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001372}
1373
Svetoslavd85084b2014-03-20 10:28:31 -07001374status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1375 if (mStatus != NO_ERROR) {
1376 return mStatus;
1377 }
1378 return mClient->clearLayerFrameStats(token);
1379}
1380
1381status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1382 FrameStats* outStats) const {
1383 if (mStatus != NO_ERROR) {
1384 return mStatus;
1385 }
1386 return mClient->getLayerFrameStats(token, outStats);
1387}
1388
Mathias Agopian698c0872011-06-28 19:09:31 -07001389// ----------------------------------------------------------------------------
1390
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001391status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001392 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1393 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001394}
1395
1396status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001397 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1398 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001399}
1400
Dan Stoza7f7da322014-05-02 15:26:25 -07001401status_t SurfaceComposerClient::getDisplayConfigs(
1402 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403{
Dan Stoza7f7da322014-05-02 15:26:25 -07001404 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1405}
1406
1407status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
1408 DisplayInfo* info) {
1409 Vector<DisplayInfo> configs;
1410 status_t result = getDisplayConfigs(display, &configs);
1411 if (result != NO_ERROR) {
1412 return result;
1413 }
1414
1415 int activeId = getActiveConfig(display);
1416 if (activeId < 0) {
1417 ALOGE("No active configuration found");
1418 return NAME_NOT_FOUND;
1419 }
1420
Dan Stozad723bd72014-11-18 10:24:03 -08001421 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001422 return NO_ERROR;
1423}
1424
1425int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1426 return ComposerService::getComposerService()->getActiveConfig(display);
1427}
1428
1429status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1430 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431}
1432
Ady Abraham838de062019-02-04 10:24:03 -08001433status_t SurfaceComposerClient::setAllowedDisplayConfigs(
1434 const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) {
1435 return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken,
1436 allowedConfigs);
1437}
1438
Ady Abrahamd9b3ea62019-02-26 14:08:03 -08001439status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
1440 std::vector<int32_t>* outAllowedConfigs) {
1441 return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken,
1442 outAllowedConfigs);
1443}
1444
Michael Wright28f24d02016-07-12 13:30:53 -07001445status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001446 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001447 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1448}
1449
Daniel Solomon42d04562019-01-20 21:03:19 -08001450status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1451 ui::DisplayPrimaries& outPrimaries) {
1452 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1453}
1454
Peiyong Lina52f0292018-03-14 17:26:31 -07001455ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001456 return ComposerService::getComposerService()->getActiveColorMode(display);
1457}
1458
1459status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001460 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001461 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1462}
1463
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001464void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1465 int mode) {
1466 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001467}
1468
Peiyong Linc6780972018-10-28 15:24:08 -07001469status_t SurfaceComposerClient::getCompositionPreference(
1470 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1471 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1472 return ComposerService::getComposerService()
1473 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1474 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001475}
1476
Peiyong Lin08d10512019-01-16 13:27:35 -08001477bool SurfaceComposerClient::getProtectedContentSupport() {
1478 bool supported = false;
1479 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1480 return supported;
1481}
1482
Svetoslavd85084b2014-03-20 10:28:31 -07001483status_t SurfaceComposerClient::clearAnimationFrameStats() {
1484 return ComposerService::getComposerService()->clearAnimationFrameStats();
1485}
1486
1487status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1488 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1489}
1490
Dan Stozac4f471e2016-03-24 09:31:08 -07001491status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1492 HdrCapabilities* outCapabilities) {
1493 return ComposerService::getComposerService()->getHdrCapabilities(display,
1494 outCapabilities);
1495}
1496
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001497status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1498 ui::PixelFormat* outFormat,
1499 ui::Dataspace* outDataspace,
1500 uint8_t* outComponentMask) {
1501 return ComposerService::getComposerService()
1502 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1503 outComponentMask);
1504}
1505
Kevin DuBois74e53772018-11-19 10:52:38 -08001506status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1507 bool enable, uint8_t componentMask,
1508 uint64_t maxFrames) {
1509 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1510 componentMask,
1511 maxFrames);
1512}
1513
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001514status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1515 uint64_t maxFrames, uint64_t timestamp,
1516 DisplayedFrameStats* outStats) {
1517 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1518 timestamp, outStats);
1519}
Marissa Wall35187b32019-01-08 10:08:52 -08001520
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001521status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1522 bool* outIsWideColorDisplay) {
1523 return ComposerService::getComposerService()->isWideColorDisplay(display,
1524 outIsWideColorDisplay);
1525}
1526
Kevin DuBois00c66832019-02-18 16:21:31 -08001527status_t SurfaceComposerClient::addRegionSamplingListener(
1528 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1529 const sp<IRegionSamplingListener>& listener) {
1530 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1531 stopLayerHandle,
1532 listener);
1533}
1534
1535status_t SurfaceComposerClient::removeRegionSamplingListener(
1536 const sp<IRegionSamplingListener>& listener) {
1537 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1538}
1539
Dan Gittik57e63c52019-01-18 16:37:54 +00001540bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1541 bool support = false;
1542 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1543 return support;
1544}
1545
1546status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1547 float brightness) {
1548 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1549}
1550
Ady Abraham8532d012019-05-08 14:50:56 -07001551status_t SurfaceComposerClient::notifyPowerHint(int32_t hintId) {
1552 return ComposerService::getComposerService()->notifyPowerHint(hintId);
1553}
1554
Mathias Agopian698c0872011-06-28 19:09:31 -07001555// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556
Peiyong Lin0e003c92018-09-17 11:09:51 -07001557status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1558 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1559 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Robert Carr108b2c72019-04-02 16:32:58 -07001560 uint32_t rotation, bool captureSecureLayers,
1561 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001562 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001563 if (s == nullptr) return NO_INIT;
Robert Carr108b2c72019-04-02 16:32:58 -07001564 status_t ret =
1565 s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace,
1566 reqPixelFormat, sourceCrop, reqWidth, reqHeight, useIdentityTransform,
1567 static_cast<ISurfaceComposer::Rotation>(rotation),
1568 captureSecureLayers);
Robert Carr673134e2017-01-09 19:48:38 -08001569 if (ret != NO_ERROR) {
1570 return ret;
1571 }
Robert Carr673134e2017-01-09 19:48:38 -08001572 return ret;
1573}
1574
Robert Carrfa8855f2019-02-19 10:05:00 -08001575status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1576 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1577 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
1578 uint32_t rotation, sp<GraphicBuffer>* outBuffer) {
Robert Carr108b2c72019-04-02 16:32:58 -07001579 bool ignored;
1580 return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1581 useIdentityTransform, rotation, false, outBuffer, ignored);
Robert Carrfa8855f2019-02-19 10:05:00 -08001582}
1583
chaviw93df2ea2019-04-30 16:45:12 -07001584status_t ScreenshotClient::capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace,
1585 sp<GraphicBuffer>* outBuffer) {
1586 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1587 if (s == nullptr) return NO_INIT;
1588 return s->captureScreen(displayOrLayerStack, outDataspace, outBuffer);
1589}
1590
Peiyong Lin0e003c92018-09-17 11:09:51 -07001591status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1592 const ui::Dataspace reqDataSpace,
1593 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001594 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001595 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001596 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001597 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
Robert Carr866455f2019-04-02 16:28:26 -07001598 sourceCrop, {}, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001599 return ret;
1600}
1601
Robert Carr866455f2019-04-02 16:28:26 -07001602status_t ScreenshotClient::captureChildLayers(
1603 const sp<IBinder>& layerHandle, const ui::Dataspace reqDataSpace,
1604 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1605 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& excludeHandles,
1606 float frameScale, sp<GraphicBuffer>* outBuffer) {
Robert Carr578038f2018-03-09 12:25:24 -08001607 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001608 if (s == nullptr) return NO_INIT;
Robert Carr866455f2019-04-02 16:28:26 -07001609 status_t ret =
1610 s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1611 excludeHandles, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001612 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001613}
Mathias Agopian74c40c02010-09-29 13:02:36 -07001614// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615}; // namespace android