1 /*
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>
20 #include <sys/types.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <utils/SortedVector.h>
25 #include <utils/String8.h>
26 #include <utils/threads.h>
27 
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/ProcessState.h>
31 
32 #include <system/graphics.h>
33 
34 #include <ui/DisplayInfo.h>
35 
36 #include <gui/BufferItemConsumer.h>
37 #include <gui/CpuConsumer.h>
38 #include <gui/IGraphicBufferProducer.h>
39 #include <gui/ISurfaceComposer.h>
40 #include <gui/ISurfaceComposerClient.h>
41 #include <gui/LayerState.h>
42 #include <gui/Surface.h>
43 #include <gui/SurfaceComposerClient.h>
44 
45 #ifndef NO_INPUT
46 #include <input/InputWindow.h>
47 #endif
48 
49 #include <private/gui/ComposerService.h>
50 
51 // This server size should always be smaller than the server cache size
52 #define BUFFER_CACHE_MAX_SIZE 64
53 
54 namespace android {
55 
56 using ui::ColorMode;
57 // ---------------------------------------------------------------------------
58 
59 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
60 
ComposerService()61 ComposerService::ComposerService()
62 : Singleton<ComposerService>() {
63     Mutex::Autolock _l(mLock);
64     connectLocked();
65 }
66 
connectLocked()67 void ComposerService::connectLocked() {
68     const String16 name("SurfaceFlinger");
69     while (getService(name, &mComposerService) != NO_ERROR) {
70         usleep(250000);
71     }
72     assert(mComposerService != nullptr);
73 
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:
83         explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
84     };
85 
86     mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
87     IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
88 }
89 
getComposerService()90 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
91     ComposerService& instance = ComposerService::getInstance();
92     Mutex::Autolock _l(instance.mLock);
93     if (instance.mComposerService == nullptr) {
94         ComposerService::getInstance().connectLocked();
95         assert(instance.mComposerService != nullptr);
96         ALOGD("ComposerService reconnected");
97     }
98     return instance.mComposerService;
99 }
100 
composerServiceDied()101 void ComposerService::composerServiceDied()
102 {
103     Mutex::Autolock _l(mLock);
104     mComposerService = nullptr;
105     mDeathObserver = nullptr;
106 }
107 
108 class DefaultComposerClient: public Singleton<DefaultComposerClient> {
109     Mutex mLock;
110     sp<SurfaceComposerClient> mClient;
111     friend class Singleton<ComposerService>;
112 public:
getComposerClient()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 };
122 ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
123 
124 
getDefault()125 sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
126     return DefaultComposerClient::getComposerClient();
127 }
128 
129 // ---------------------------------------------------------------------------
130 
131 // 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 
135 // 0 is an invalid callback id
TransactionCompletedListener()136 TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
137 
getNextIdLocked()138 CallbackId TransactionCompletedListener::getNextIdLocked() {
139     return mCallbackIdCounter++;
140 }
141 
getInstance()142 sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
143     static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener;
144     return sInstance;
145 }
146 
getIInstance()147 sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
148     return static_cast<sp<ITransactionCompletedListener>>(getInstance());
149 }
150 
startListeningLocked()151 void TransactionCompletedListener::startListeningLocked() {
152     if (mListening) {
153         return;
154     }
155     ProcessState::self()->startThreadPool();
156     mListening = true;
157 }
158 
addCallbackFunction(const TransactionCompletedCallback & callbackFunction,const std::unordered_set<sp<SurfaceControl>,SurfaceComposerClient::SCHash> & surfaceControls)159 CallbackId TransactionCompletedListener::addCallbackFunction(
160         const TransactionCompletedCallback& callbackFunction,
161         const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
162                 surfaceControls) {
163     std::lock_guard<std::mutex> lock(mMutex);
164     startListeningLocked();
165 
166     CallbackId callbackId = getNextIdLocked();
167     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 
175     return callbackId;
176 }
177 
addSurfaceControlToCallbacks(const sp<SurfaceControl> & surfaceControl,const std::unordered_set<CallbackId> & callbackIds)178 void 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 
onTransactionCompleted(ListenerStats listenerStats)191 void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
192     std::lock_guard<std::mutex> lock(mMutex);
193 
194     /* 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;
206     for (const auto& transactionStats : listenerStats.transactionStats) {
207         for (auto callbackId : transactionStats.callbackIds) {
208             auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId];
209             surfaceControls.insert(callbackSurfaceControls.begin(), callbackSurfaceControls.end());
210         }
211     }
212 
213     for (const auto& transactionStats : listenerStats.transactionStats) {
214         for (auto callbackId : transactionStats.callbackIds) {
215             auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId];
216             if (!callbackFunction) {
217                 ALOGE("cannot call null callback function, skipping");
218                 continue;
219             }
220             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);
229             mCallbacks.erase(callbackId);
230         }
231     }
232 }
233 
234 // ---------------------------------------------------------------------------
235 
236 void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId);
237 
238 class BufferCache : public Singleton<BufferCache> {
239 public:
BufferCache()240     BufferCache() : token(new BBinder()) {}
241 
getToken()242     sp<IBinder> getToken() {
243         return IInterface::asBinder(TransactionCompletedListener::getIInstance());
244     }
245 
getCacheId(const sp<GraphicBuffer> & buffer,uint64_t * cacheId)246     status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) {
247         std::lock_guard<std::mutex> lock(mMutex);
248 
249         auto itr = mBuffers.find(buffer->getId());
250         if (itr == mBuffers.end()) {
251             return BAD_VALUE;
252         }
253         itr->second = getCounter();
254         *cacheId = buffer->getId();
255         return NO_ERROR;
256     }
257 
cache(const sp<GraphicBuffer> & buffer)258     uint64_t cache(const sp<GraphicBuffer>& buffer) {
259         std::lock_guard<std::mutex> lock(mMutex);
260 
261         if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) {
262             evictLeastRecentlyUsedBuffer();
263         }
264 
265         buffer->addDeathCallback(bufferCacheCallback, nullptr);
266 
267         mBuffers[buffer->getId()] = getCounter();
268         return buffer->getId();
269     }
270 
uncache(uint64_t cacheId)271     void uncache(uint64_t cacheId) {
272         std::lock_guard<std::mutex> lock(mMutex);
273         uncacheLocked(cacheId);
274     }
275 
uncacheLocked(uint64_t cacheId)276     void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) {
277         mBuffers.erase(cacheId);
278         SurfaceComposerClient::doUncacheBufferTransaction(cacheId);
279     }
280 
281 private:
evictLeastRecentlyUsedBuffer()282     void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
283         auto itr = mBuffers.begin();
284         uint64_t minCounter = itr->second;
285         auto minBuffer = itr;
286         itr++;
287 
288         while (itr != mBuffers.end()) {
289             uint64_t counter = itr->second;
290             if (counter < minCounter) {
291                 minCounter = counter;
292                 minBuffer = itr;
293             }
294             itr++;
295         }
296         uncacheLocked(minBuffer->first);
297     }
298 
getCounter()299     uint64_t getCounter() REQUIRES(mMutex) {
300         static uint64_t counter = 0;
301         return counter++;
302     }
303 
304     std::mutex mMutex;
305     std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex);
306 
307     // Used by ISurfaceComposer to identify which process is sending the cached buffer.
308     sp<IBinder> token;
309 };
310 
311 ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
312 
bufferCacheCallback(void *,uint64_t graphicBufferId)313 void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId) {
314     // GraphicBuffer id's are used as the cache ids.
315     BufferCache::getInstance().uncache(graphicBufferId);
316 }
317 
318 // ---------------------------------------------------------------------------
319 
Transaction(const Transaction & other)320 SurfaceComposerClient::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) {
326     mDisplayStates = other.mDisplayStates;
327     mComposerStates = other.mComposerStates;
328     mInputWindowCommands = other.mInputWindowCommands;
329 }
330 
merge(Transaction && other)331 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
332     for (auto const& kv : other.mComposerStates) {
333         if (mComposerStates.count(kv.first) == 0) {
334             mComposerStates[kv.first] = kv.second;
335         } else {
336             mComposerStates[kv.first].state.merge(kv.second.state);
337         }
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 
351     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 
362     mInputWindowCommands.merge(other.mInputWindowCommands);
363     other.mInputWindowCommands.clear();
364 
365     mContainsBuffer = other.mContainsBuffer;
366     other.mContainsBuffer = false;
367 
368     mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup;
369     other.mEarlyWakeup = false;
370 
371     return *this;
372 }
373 
doDropReferenceTransaction(const sp<IBinder> & handle,const sp<ISurfaceComposerClient> & client)374 void 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);
387     sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
388     sf->setTransactionState(composerStates, displayStates, 0, applyToken, {}, -1, {}, {});
389 }
390 
doUncacheBufferTransaction(uint64_t cacheId)391 void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) {
392     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
393 
394     client_cache_t uncacheBuffer;
395     uncacheBuffer.token = BufferCache::getInstance().getToken();
396     uncacheBuffer.id = cacheId;
397 
398     sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
399     sf->setTransactionState({}, {}, 0, applyToken, {}, -1, uncacheBuffer, {});
400 }
401 
cacheBuffers()402 void 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 
414         // 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 
420         uint64_t cacheId = 0;
421         status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId);
422         if (ret == NO_ERROR) {
423             s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
424             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();
430         s->cachedBuffer.id = cacheId;
431 
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     }
439 }
440 
apply(bool synchronous)441 status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
442     if (mStatus != NO_ERROR) {
443         return mStatus;
444     }
445 
446     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
447 
448     std::vector<ListenerCallbacks> listenerCallbacks;
449 
450     // 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 
457         listenerCallbacks.emplace_back(listener, std::move(callbackIds));
458 
459         // 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             }
466             s->what |= layer_state_t::eHasListenerCallbacksChanged;
467             s->hasListenerCallbacks = true;
468         }
469     }
470     mListenerCallbacks.clear();
471 
472     cacheBuffers();
473 
474     Vector<ComposerState> composerStates;
475     Vector<DisplayState> displayStates;
476     uint32_t flags = 0;
477 
478     mForceSynchronous |= synchronous;
479 
480     for (auto const& kv : mComposerStates){
481         composerStates.add(kv.second);
482     }
483 
484     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     }
495     if (mEarlyWakeup) {
496         flags |= ISurfaceComposer::eEarlyWakeup;
497     }
498 
499     mForceSynchronous = false;
500     mAnimation = false;
501     mEarlyWakeup = false;
502 
503     sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
504     sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands,
505                             mDesiredPresentTime,
506                             {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/,
507                             listenerCallbacks);
508     mInputWindowCommands.clear();
509     mStatus = NO_ERROR;
510     return NO_ERROR;
511 }
512 
513 // ---------------------------------------------------------------------------
514 
createDisplay(const String8 & displayName,bool secure)515 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
516     return ComposerService::getComposerService()->createDisplay(displayName,
517             secure);
518 }
519 
destroyDisplay(const sp<IBinder> & display)520 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
521     return ComposerService::getComposerService()->destroyDisplay(display);
522 }
523 
getPhysicalDisplayIds()524 std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
525     return ComposerService::getComposerService()->getPhysicalDisplayIds();
526 }
527 
getInternalDisplayId()528 std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
529     return ComposerService::getComposerService()->getInternalDisplayId();
530 }
531 
getPhysicalDisplayToken(PhysicalDisplayId displayId)532 sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
533     return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
534 }
535 
getInternalDisplayToken()536 sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
537     return ComposerService::getComposerService()->getInternalDisplayToken();
538 }
539 
setAnimationTransaction()540 void SurfaceComposerClient::Transaction::setAnimationTransaction() {
541     mAnimation = true;
542 }
543 
setEarlyWakeup()544 void SurfaceComposerClient::Transaction::setEarlyWakeup() {
545     mEarlyWakeup = true;
546 }
547 
getLayerState(const sp<SurfaceControl> & sc)548 layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
549     if (mComposerStates.count(sc) == 0) {
550         // we don't have it, add an initialized layer_state to our list
551         ComposerState s;
552         s.client = sc->getClient()->mClient;
553         s.state.surface = sc->getHandle();
554         mComposerStates[sc] = s;
555     }
556 
557     return &(mComposerStates[sc].state);
558 }
559 
registerSurfaceControlForCallback(const sp<SurfaceControl> & sc)560 void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
561         const sp<SurfaceControl>& sc) {
562     auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
563     callbackInfo.surfaceControls.insert(sc);
564 
565     TransactionCompletedListener::getInstance()
566             ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
567 }
568 
setPosition(const sp<SurfaceControl> & sc,float x,float y)569 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
570         const sp<SurfaceControl>& sc, float x, float y) {
571     layer_state_t* s = getLayerState(sc);
572     if (!s) {
573         mStatus = BAD_INDEX;
574         return *this;
575     }
576     s->what |= layer_state_t::ePositionChanged;
577     s->x = x;
578     s->y = y;
579 
580     registerSurfaceControlForCallback(sc);
581     return *this;
582 }
583 
show(const sp<SurfaceControl> & sc)584 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
585         const sp<SurfaceControl>& sc) {
586     return setFlags(sc, 0, layer_state_t::eLayerHidden);
587 }
588 
hide(const sp<SurfaceControl> & sc)589 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
590         const sp<SurfaceControl>& sc) {
591     return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
592 }
593 
setSize(const sp<SurfaceControl> & sc,uint32_t w,uint32_t h)594 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
595         const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
596     layer_state_t* s = getLayerState(sc);
597     if (!s) {
598         mStatus = BAD_INDEX;
599         return *this;
600     }
601     s->what |= layer_state_t::eSizeChanged;
602     s->w = w;
603     s->h = h;
604 
605     registerSurfaceControlForCallback(sc);
606     return *this;
607 }
608 
setLayer(const sp<SurfaceControl> & sc,int32_t z)609 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
610         const sp<SurfaceControl>& sc, int32_t z) {
611     layer_state_t* s = getLayerState(sc);
612     if (!s) {
613         mStatus = BAD_INDEX;
614         return *this;
615     }
616     s->what |= layer_state_t::eLayerChanged;
617     s->what &= ~layer_state_t::eRelativeLayerChanged;
618     s->z = z;
619 
620     registerSurfaceControlForCallback(sc);
621     return *this;
622 }
623 
setRelativeLayer(const sp<SurfaceControl> & sc,const sp<IBinder> & relativeTo,int32_t z)624 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
625         int32_t z) {
626     layer_state_t* s = getLayerState(sc);
627     if (!s) {
628         mStatus = BAD_INDEX;
629     }
630     s->what |= layer_state_t::eRelativeLayerChanged;
631     s->what &= ~layer_state_t::eLayerChanged;
632     s->relativeLayerHandle = relativeTo;
633     s->z = z;
634 
635     registerSurfaceControlForCallback(sc);
636     return *this;
637 }
638 
setFlags(const sp<SurfaceControl> & sc,uint32_t flags,uint32_t mask)639 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
640         const sp<SurfaceControl>& sc, uint32_t flags,
641         uint32_t mask) {
642     layer_state_t* s = getLayerState(sc);
643     if (!s) {
644         mStatus = BAD_INDEX;
645         return *this;
646     }
647     if ((mask & layer_state_t::eLayerOpaque) ||
648             (mask & layer_state_t::eLayerHidden) ||
649             (mask & layer_state_t::eLayerSecure)) {
650         s->what |= layer_state_t::eFlagsChanged;
651     }
652     s->flags &= ~mask;
653     s->flags |= (flags & mask);
654     s->mask |= mask;
655 
656     registerSurfaceControlForCallback(sc);
657     return *this;
658 }
659 
setTransparentRegionHint(const sp<SurfaceControl> & sc,const Region & transparentRegion)660 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
661         const sp<SurfaceControl>& sc,
662         const Region& transparentRegion) {
663     layer_state_t* s = getLayerState(sc);
664     if (!s) {
665         mStatus = BAD_INDEX;
666         return *this;
667     }
668     s->what |= layer_state_t::eTransparentRegionChanged;
669     s->transparentRegion = transparentRegion;
670 
671     registerSurfaceControlForCallback(sc);
672     return *this;
673 }
674 
setAlpha(const sp<SurfaceControl> & sc,float alpha)675 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
676         const sp<SurfaceControl>& sc, float alpha) {
677     layer_state_t* s = getLayerState(sc);
678     if (!s) {
679         mStatus = BAD_INDEX;
680         return *this;
681     }
682     s->what |= layer_state_t::eAlphaChanged;
683     s->alpha = alpha;
684 
685     registerSurfaceControlForCallback(sc);
686     return *this;
687 }
688 
setLayerStack(const sp<SurfaceControl> & sc,uint32_t layerStack)689 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
690         const sp<SurfaceControl>& sc, uint32_t layerStack) {
691     layer_state_t* s = getLayerState(sc);
692     if (!s) {
693         mStatus = BAD_INDEX;
694         return *this;
695     }
696     s->what |= layer_state_t::eLayerStackChanged;
697     s->layerStack = layerStack;
698 
699     registerSurfaceControlForCallback(sc);
700     return *this;
701 }
702 
setMetadata(const sp<SurfaceControl> & sc,uint32_t key,const Parcel & p)703 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
704         const sp<SurfaceControl>& sc, uint32_t key, const Parcel& p) {
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 
712     s->metadata.mMap[key] = {p.data(), p.data() + p.dataSize()};
713 
714     registerSurfaceControlForCallback(sc);
715     return *this;
716 }
717 
setMatrix(const sp<SurfaceControl> & sc,float dsdx,float dtdx,float dtdy,float dsdy)718 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
719         const sp<SurfaceControl>& sc, float dsdx, float dtdx,
720         float dtdy, float dsdy) {
721     layer_state_t* s = getLayerState(sc);
722     if (!s) {
723         mStatus = BAD_INDEX;
724         return *this;
725     }
726     s->what |= layer_state_t::eMatrixChanged;
727     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;
733 
734     registerSurfaceControlForCallback(sc);
735     return *this;
736 }
737 
setCrop_legacy(const sp<SurfaceControl> & sc,const Rect & crop)738 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
739         const sp<SurfaceControl>& sc, const Rect& crop) {
740     layer_state_t* s = getLayerState(sc);
741     if (!s) {
742         mStatus = BAD_INDEX;
743         return *this;
744     }
745     s->what |= layer_state_t::eCropChanged_legacy;
746     s->crop_legacy = crop;
747 
748     registerSurfaceControlForCallback(sc);
749     return *this;
750 }
751 
setCornerRadius(const sp<SurfaceControl> & sc,float cornerRadius)752 SurfaceComposerClient::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 
764 SurfaceComposerClient::Transaction&
deferTransactionUntil_legacy(const sp<SurfaceControl> & sc,const sp<IBinder> & handle,uint64_t frameNumber)765 SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
766                                                                  const sp<IBinder>& handle,
767                                                                  uint64_t frameNumber) {
768     layer_state_t* s = getLayerState(sc);
769     if (!s) {
770         mStatus = BAD_INDEX;
771         return *this;
772     }
773     s->what |= layer_state_t::eDeferTransaction_legacy;
774     s->barrierHandle_legacy = handle;
775     s->frameNumber_legacy = frameNumber;
776 
777     registerSurfaceControlForCallback(sc);
778     return *this;
779 }
780 
781 SurfaceComposerClient::Transaction&
deferTransactionUntil_legacy(const sp<SurfaceControl> & sc,const sp<Surface> & barrierSurface,uint64_t frameNumber)782 SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
783                                                                  const sp<Surface>& barrierSurface,
784                                                                  uint64_t frameNumber) {
785     layer_state_t* s = getLayerState(sc);
786     if (!s) {
787         mStatus = BAD_INDEX;
788         return *this;
789     }
790     s->what |= layer_state_t::eDeferTransaction_legacy;
791     s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
792     s->frameNumber_legacy = frameNumber;
793 
794     registerSurfaceControlForCallback(sc);
795     return *this;
796 }
797 
reparentChildren(const sp<SurfaceControl> & sc,const sp<IBinder> & newParentHandle)798 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
799         const sp<SurfaceControl>& sc,
800         const sp<IBinder>& newParentHandle) {
801     layer_state_t* s = getLayerState(sc);
802     if (!s) {
803         mStatus = BAD_INDEX;
804         return *this;
805     }
806     s->what |= layer_state_t::eReparentChildren;
807     s->reparentHandle = newParentHandle;
808 
809     registerSurfaceControlForCallback(sc);
810     return *this;
811 }
812 
reparent(const sp<SurfaceControl> & sc,const sp<IBinder> & newParentHandle)813 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
814         const sp<SurfaceControl>& sc,
815         const sp<IBinder>& newParentHandle) {
816     layer_state_t* s = getLayerState(sc);
817     if (!s) {
818         mStatus = BAD_INDEX;
819         return *this;
820     }
821     s->what |= layer_state_t::eReparent;
822     s->parentHandleForChild = newParentHandle;
823 
824     registerSurfaceControlForCallback(sc);
825     return *this;
826 }
827 
setColor(const sp<SurfaceControl> & sc,const half3 & color)828 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
829         const sp<SurfaceControl>& sc,
830         const half3& color) {
831     layer_state_t* s = getLayerState(sc);
832     if (!s) {
833         mStatus = BAD_INDEX;
834         return *this;
835     }
836     s->what |= layer_state_t::eColorChanged;
837     s->color = color;
838 
839     registerSurfaceControlForCallback(sc);
840     return *this;
841 }
842 
setBackgroundColor(const sp<SurfaceControl> & sc,const half3 & color,float alpha,ui::Dataspace dataspace)843 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
844         const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
845     layer_state_t* s = getLayerState(sc);
846     if (!s) {
847         mStatus = BAD_INDEX;
848         return *this;
849     }
850 
851     s->what |= layer_state_t::eBackgroundColorChanged;
852     s->color = color;
853     s->bgColorAlpha = alpha;
854     s->bgColorDataspace = dataspace;
855 
856     registerSurfaceControlForCallback(sc);
857     return *this;
858 }
859 
setTransform(const sp<SurfaceControl> & sc,uint32_t transform)860 SurfaceComposerClient::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;
869 
870     registerSurfaceControlForCallback(sc);
871     return *this;
872 }
873 
874 SurfaceComposerClient::Transaction&
setTransformToDisplayInverse(const sp<SurfaceControl> & sc,bool transformToDisplayInverse)875 SurfaceComposerClient::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;
884 
885     registerSurfaceControlForCallback(sc);
886     return *this;
887 }
888 
setCrop(const sp<SurfaceControl> & sc,const Rect & crop)889 SurfaceComposerClient::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;
898 
899     registerSurfaceControlForCallback(sc);
900     return *this;
901 }
902 
setFrame(const sp<SurfaceControl> & sc,const Rect & frame)903 SurfaceComposerClient::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 
setBuffer(const sp<SurfaceControl> & sc,const sp<GraphicBuffer> & buffer)917 SurfaceComposerClient::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     }
924     s->what |= layer_state_t::eBufferChanged;
925     s->buffer = buffer;
926 
927     registerSurfaceControlForCallback(sc);
928 
929     mContainsBuffer = true;
930     return *this;
931 }
932 
setAcquireFence(const sp<SurfaceControl> & sc,const sp<Fence> & fence)933 SurfaceComposerClient::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;
942 
943     registerSurfaceControlForCallback(sc);
944     return *this;
945 }
946 
setDataspace(const sp<SurfaceControl> & sc,ui::Dataspace dataspace)947 SurfaceComposerClient::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;
956 
957     registerSurfaceControlForCallback(sc);
958     return *this;
959 }
960 
setHdrMetadata(const sp<SurfaceControl> & sc,const HdrMetadata & hdrMetadata)961 SurfaceComposerClient::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;
970 
971     registerSurfaceControlForCallback(sc);
972     return *this;
973 }
974 
setSurfaceDamageRegion(const sp<SurfaceControl> & sc,const Region & surfaceDamageRegion)975 SurfaceComposerClient::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;
984 
985     registerSurfaceControlForCallback(sc);
986     return *this;
987 }
988 
setApi(const sp<SurfaceControl> & sc,int32_t api)989 SurfaceComposerClient::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;
998 
999     registerSurfaceControlForCallback(sc);
1000     return *this;
1001 }
1002 
setSidebandStream(const sp<SurfaceControl> & sc,const sp<NativeHandle> & sidebandStream)1003 SurfaceComposerClient::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;
1012 
1013     registerSurfaceControlForCallback(sc);
1014     return *this;
1015 }
1016 
setDesiredPresentTime(nsecs_t desiredPresentTime)1017 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
1018         nsecs_t desiredPresentTime) {
1019     mDesiredPresentTime = desiredPresentTime;
1020     return *this;
1021 }
1022 
setColorSpaceAgnostic(const sp<SurfaceControl> & sc,const bool agnostic)1023 SurfaceComposerClient::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 
1037 SurfaceComposerClient::Transaction&
addTransactionCompletedCallback(TransactionCompletedCallbackTakesContext callback,void * callbackContext)1038 SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
1039         TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
1040     auto listener = TransactionCompletedListener::getInstance();
1041 
1042     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;
1046 
1047     CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
1048 
1049     mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1050             callbackId);
1051     return *this;
1052 }
1053 
detachChildren(const sp<SurfaceControl> & sc)1054 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1055         const sp<SurfaceControl>& sc) {
1056     layer_state_t* s = getLayerState(sc);
1057     if (!s) {
1058         mStatus = BAD_INDEX;
1059     }
1060     s->what |= layer_state_t::eDetachChildren;
1061 
1062     registerSurfaceControlForCallback(sc);
1063     return *this;
1064 }
1065 
setOverrideScalingMode(const sp<SurfaceControl> & sc,int32_t overrideScalingMode)1066 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1067         const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
1068     layer_state_t* s = getLayerState(sc);
1069     if (!s) {
1070         mStatus = BAD_INDEX;
1071         return *this;
1072     }
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);
1084             mStatus = BAD_VALUE;
1085             return *this;
1086     }
1087 
1088     s->what |= layer_state_t::eOverrideScalingModeChanged;
1089     s->overrideScalingMode = overrideScalingMode;
1090 
1091     registerSurfaceControlForCallback(sc);
1092     return *this;
1093 }
1094 
setGeometryAppliesWithResize(const sp<SurfaceControl> & sc)1095 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1096         const sp<SurfaceControl>& sc) {
1097     layer_state_t* s = getLayerState(sc);
1098     if (!s) {
1099         mStatus = BAD_INDEX;
1100         return *this;
1101     }
1102     s->what |= layer_state_t::eGeometryAppliesWithResize;
1103 
1104     registerSurfaceControlForCallback(sc);
1105     return *this;
1106 }
1107 
1108 #ifndef NO_INPUT
setInputWindowInfo(const sp<SurfaceControl> & sc,const InputWindowInfo & info)1109 SurfaceComposerClient::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 }
1121 
transferTouchFocus(const sp<IBinder> & fromToken,const sp<IBinder> & toToken)1122 SurfaceComposerClient::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 
syncInputWindows()1131 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1132     mInputWindowCommands.syncInputWindows = true;
1133     return *this;
1134 }
1135 
1136 #endif
1137 
setColorTransform(const sp<SurfaceControl> & sc,const mat3 & matrix,const vec3 & translation)1138 SurfaceComposerClient::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);
1147 
1148     registerSurfaceControlForCallback(sc);
1149     return *this;
1150 }
1151 
setGeometry(const sp<SurfaceControl> & sc,const Rect & source,const Rect & dst,int transform)1152 SurfaceComposerClient::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;
1158 
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;
1164     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 
1204 // ---------------------------------------------------------------------------
1205 
getDisplayState(const sp<IBinder> & token)1206 DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
1207     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     }
1215     return mDisplayStates.editItemAt(static_cast<size_t>(index));
1216 }
1217 
setDisplaySurface(const sp<IBinder> & token,const sp<IGraphicBufferProducer> & bufferProducer)1218 status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1219         const sp<IGraphicBufferProducer>& bufferProducer) {
1220     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         }
1230     }
1231     DisplayState& s(getDisplayState(token));
1232     s.surface = bufferProducer;
1233     s.what |= DisplayState::eSurfaceChanged;
1234     return NO_ERROR;
1235 }
1236 
setDisplayLayerStack(const sp<IBinder> & token,uint32_t layerStack)1237 void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
1238         uint32_t layerStack) {
1239     DisplayState& s(getDisplayState(token));
1240     s.layerStack = layerStack;
1241     s.what |= DisplayState::eLayerStackChanged;
1242 }
1243 
setDisplayProjection(const sp<IBinder> & token,uint32_t orientation,const Rect & layerStackRect,const Rect & displayRect)1244 void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
1245         uint32_t orientation,
1246         const Rect& layerStackRect,
1247         const Rect& displayRect) {
1248     DisplayState& s(getDisplayState(token));
1249     s.orientation = orientation;
1250     s.viewport = layerStackRect;
1251     s.frame = displayRect;
1252     s.what |= DisplayState::eDisplayProjectionChanged;
1253     mForceSynchronous = true; // TODO: do we actually still need this?
1254 }
1255 
setDisplaySize(const sp<IBinder> & token,uint32_t width,uint32_t height)1256 void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
1257     DisplayState& s(getDisplayState(token));
1258     s.width = width;
1259     s.height = height;
1260     s.what |= DisplayState::eDisplaySizeChanged;
1261 }
1262 
1263 // ---------------------------------------------------------------------------
1264 
SurfaceComposerClient()1265 SurfaceComposerClient::SurfaceComposerClient()
1266     : mStatus(NO_INIT)
1267 {
1268 }
1269 
SurfaceComposerClient(const sp<ISurfaceComposerClient> & client)1270 SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1271     : mStatus(NO_ERROR), mClient(client)
1272 {
1273 }
1274 
onFirstRef()1275 void SurfaceComposerClient::onFirstRef() {
1276     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1277     if (sf != nullptr && mStatus == NO_INIT) {
1278         sp<ISurfaceComposerClient> conn;
1279         conn = sf->createConnection();
1280         if (conn != nullptr) {
1281             mClient = conn;
1282             mStatus = NO_ERROR;
1283         }
1284     }
1285 }
1286 
~SurfaceComposerClient()1287 SurfaceComposerClient::~SurfaceComposerClient() {
1288     dispose();
1289 }
1290 
initCheck() const1291 status_t SurfaceComposerClient::initCheck() const {
1292     return mStatus;
1293 }
1294 
connection() const1295 sp<IBinder> SurfaceComposerClient::connection() const {
1296     return IInterface::asBinder(mClient);
1297 }
1298 
linkToComposerDeath(const sp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags)1299 status_t SurfaceComposerClient::linkToComposerDeath(
1300         const sp<IBinder::DeathRecipient>& recipient,
1301         void* cookie, uint32_t flags) {
1302     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1303     return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
1304 }
1305 
dispose()1306 void SurfaceComposerClient::dispose() {
1307     // this can be called more than once.
1308     sp<ISurfaceComposerClient> client;
1309     Mutex::Autolock _lm(mLock);
1310     if (mClient != nullptr) {
1311         client = mClient; // hold ref while lock is held
1312         mClient.clear();
1313     }
1314     mStatus = NO_INIT;
1315 }
1316 
createSurface(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,SurfaceControl * parent,LayerMetadata metadata)1317 sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1318                                                         PixelFormat format, uint32_t flags,
1319                                                         SurfaceControl* parent,
1320                                                         LayerMetadata metadata) {
1321     sp<SurfaceControl> s;
1322     createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
1323     return s;
1324 }
1325 
createWithSurfaceParent(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,uint32_t flags,Surface * parent,LayerMetadata metadata)1326 sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1327                                                                   uint32_t h, PixelFormat format,
1328                                                                   uint32_t flags, Surface* parent,
1329                                                                   LayerMetadata metadata) {
1330     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 
1338         err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1339                                                std::move(metadata), &handle, &gbp);
1340         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 
createSurfaceChecked(const String8 & name,uint32_t w,uint32_t h,PixelFormat format,sp<SurfaceControl> * outSurface,uint32_t flags,SurfaceControl * parent,LayerMetadata metadata)1348 status_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) {
1353     sp<SurfaceControl> sur;
1354     status_t err = mStatus;
1355 
1356     if (mStatus == NO_ERROR) {
1357         sp<IBinder> handle;
1358         sp<IBinder> parentHandle;
1359         sp<IGraphicBufferProducer> gbp;
1360 
1361         if (parent != nullptr) {
1362             parentHandle = parent->getHandle();
1363         }
1364 
1365         err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1366                                      &handle, &gbp);
1367         ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1368         if (err == NO_ERROR) {
1369             *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
1370         }
1371     }
1372     return err;
1373 }
1374 
clearLayerFrameStats(const sp<IBinder> & token) const1375 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1376     if (mStatus != NO_ERROR) {
1377         return mStatus;
1378     }
1379     return mClient->clearLayerFrameStats(token);
1380 }
1381 
getLayerFrameStats(const sp<IBinder> & token,FrameStats * outStats) const1382 status_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 
1390 // ----------------------------------------------------------------------------
1391 
enableVSyncInjections(bool enable)1392 status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
1393     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1394     return sf->enableVSyncInjections(enable);
1395 }
1396 
injectVSync(nsecs_t when)1397 status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
1398     sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1399     return sf->injectVSync(when);
1400 }
1401 
getDisplayConfigs(const sp<IBinder> & display,Vector<DisplayInfo> * configs)1402 status_t SurfaceComposerClient::getDisplayConfigs(
1403         const sp<IBinder>& display, Vector<DisplayInfo>* configs)
1404 {
1405     return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1406 }
1407 
getDisplayInfo(const sp<IBinder> & display,DisplayInfo * info)1408 status_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 
1422     *info = configs[static_cast<size_t>(activeId)];
1423     return NO_ERROR;
1424 }
1425 
getActiveConfig(const sp<IBinder> & display)1426 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1427     return ComposerService::getComposerService()->getActiveConfig(display);
1428 }
1429 
setActiveConfig(const sp<IBinder> & display,int id)1430 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1431     return ComposerService::getComposerService()->setActiveConfig(display, id);
1432 }
1433 
setAllowedDisplayConfigs(const sp<IBinder> & displayToken,const std::vector<int32_t> & allowedConfigs)1434 status_t SurfaceComposerClient::setAllowedDisplayConfigs(
1435         const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) {
1436     return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken,
1437                                                                            allowedConfigs);
1438 }
1439 
getAllowedDisplayConfigs(const sp<IBinder> & displayToken,std::vector<int32_t> * outAllowedConfigs)1440 status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
1441                                                          std::vector<int32_t>* outAllowedConfigs) {
1442     return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken,
1443                                                                            outAllowedConfigs);
1444 }
1445 
getDisplayColorModes(const sp<IBinder> & display,Vector<ColorMode> * outColorModes)1446 status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
1447         Vector<ColorMode>* outColorModes) {
1448     return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1449 }
1450 
getDisplayNativePrimaries(const sp<IBinder> & display,ui::DisplayPrimaries & outPrimaries)1451 status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1452         ui::DisplayPrimaries& outPrimaries) {
1453     return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1454 }
1455 
getActiveColorMode(const sp<IBinder> & display)1456 ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
1457     return ComposerService::getComposerService()->getActiveColorMode(display);
1458 }
1459 
setActiveColorMode(const sp<IBinder> & display,ColorMode colorMode)1460 status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
1461         ColorMode colorMode) {
1462     return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1463 }
1464 
setDisplayPowerMode(const sp<IBinder> & token,int mode)1465 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1466         int mode) {
1467     ComposerService::getComposerService()->setPowerMode(token, mode);
1468 }
1469 
getCompositionPreference(ui::Dataspace * defaultDataspace,ui::PixelFormat * defaultPixelFormat,ui::Dataspace * wideColorGamutDataspace,ui::PixelFormat * wideColorGamutPixelFormat)1470 status_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);
1476 }
1477 
getProtectedContentSupport()1478 bool SurfaceComposerClient::getProtectedContentSupport() {
1479     bool supported = false;
1480     ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1481     return supported;
1482 }
1483 
clearAnimationFrameStats()1484 status_t SurfaceComposerClient::clearAnimationFrameStats() {
1485     return ComposerService::getComposerService()->clearAnimationFrameStats();
1486 }
1487 
getAnimationFrameStats(FrameStats * outStats)1488 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1489     return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1490 }
1491 
getHdrCapabilities(const sp<IBinder> & display,HdrCapabilities * outCapabilities)1492 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1493         HdrCapabilities* outCapabilities) {
1494     return ComposerService::getComposerService()->getHdrCapabilities(display,
1495             outCapabilities);
1496 }
1497 
getDisplayedContentSamplingAttributes(const sp<IBinder> & display,ui::PixelFormat * outFormat,ui::Dataspace * outDataspace,uint8_t * outComponentMask)1498 status_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 
setDisplayContentSamplingEnabled(const sp<IBinder> & display,bool enable,uint8_t componentMask,uint64_t maxFrames)1507 status_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 
getDisplayedContentSample(const sp<IBinder> & display,uint64_t maxFrames,uint64_t timestamp,DisplayedFrameStats * outStats)1515 status_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 }
1521 
isWideColorDisplay(const sp<IBinder> & display,bool * outIsWideColorDisplay)1522 status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1523                                                    bool* outIsWideColorDisplay) {
1524     return ComposerService::getComposerService()->isWideColorDisplay(display,
1525                                                                      outIsWideColorDisplay);
1526 }
1527 
addRegionSamplingListener(const Rect & samplingArea,const sp<IBinder> & stopLayerHandle,const sp<IRegionSamplingListener> & listener)1528 status_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 
removeRegionSamplingListener(const sp<IRegionSamplingListener> & listener)1536 status_t SurfaceComposerClient::removeRegionSamplingListener(
1537         const sp<IRegionSamplingListener>& listener) {
1538     return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1539 }
1540 
getDisplayBrightnessSupport(const sp<IBinder> & displayToken)1541 bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1542     bool support = false;
1543     ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1544     return support;
1545 }
1546 
setDisplayBrightness(const sp<IBinder> & displayToken,float brightness)1547 status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1548                                                      float brightness) {
1549     return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1550 }
1551 
notifyPowerHint(int32_t hintId)1552 status_t SurfaceComposerClient::notifyPowerHint(int32_t hintId) {
1553     return ComposerService::getComposerService()->notifyPowerHint(hintId);
1554 }
1555 
1556 // ----------------------------------------------------------------------------
1557 
capture(const sp<IBinder> & display,const ui::Dataspace reqDataSpace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform,uint32_t rotation,bool captureSecureLayers,sp<GraphicBuffer> * outBuffer,bool & outCapturedSecureLayers)1558 status_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,
1561                                    uint32_t rotation, bool captureSecureLayers,
1562                                    sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) {
1563     sp<ISurfaceComposer> s(ComposerService::getComposerService());
1564     if (s == nullptr) return NO_INIT;
1565     status_t ret =
1566             s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace,
1567                              reqPixelFormat, sourceCrop, reqWidth, reqHeight, useIdentityTransform,
1568                              static_cast<ISurfaceComposer::Rotation>(rotation),
1569                              captureSecureLayers);
1570     if (ret != NO_ERROR) {
1571         return ret;
1572     }
1573     return ret;
1574 }
1575 
capture(const sp<IBinder> & display,const ui::Dataspace reqDataSpace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,uint32_t reqWidth,uint32_t reqHeight,bool useIdentityTransform,uint32_t rotation,sp<GraphicBuffer> * outBuffer)1576 status_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) {
1580     bool ignored;
1581     return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth, reqHeight,
1582                    useIdentityTransform, rotation, false, outBuffer, ignored);
1583 }
1584 
capture(uint64_t displayOrLayerStack,ui::Dataspace * outDataspace,sp<GraphicBuffer> * outBuffer)1585 status_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 
captureLayers(const sp<IBinder> & layerHandle,const ui::Dataspace reqDataSpace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,float frameScale,sp<GraphicBuffer> * outBuffer)1592 status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1593                                          const ui::Dataspace reqDataSpace,
1594                                          const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1595                                          float frameScale, sp<GraphicBuffer>* outBuffer) {
1596     sp<ISurfaceComposer> s(ComposerService::getComposerService());
1597     if (s == nullptr) return NO_INIT;
1598     status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1599                                     sourceCrop, {}, frameScale, false /* childrenOnly */);
1600     return ret;
1601 }
1602 
captureChildLayers(const sp<IBinder> & layerHandle,const ui::Dataspace reqDataSpace,const ui::PixelFormat reqPixelFormat,Rect sourceCrop,const std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> & excludeHandles,float frameScale,sp<GraphicBuffer> * outBuffer)1603 status_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) {
1608     sp<ISurfaceComposer> s(ComposerService::getComposerService());
1609     if (s == nullptr) return NO_INIT;
1610     status_t ret =
1611             s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1612                              excludeHandles, frameScale, true /* childrenOnly */);
1613     return ret;
1614 }
1615 // ----------------------------------------------------------------------------
1616 }; // namespace android
1617