blob: 206e05652f3cc680ec7e6ce2845490a4bd8bf576 [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) {
192 std::lock_guard lock(mMutex);
193
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 Walle2ffb422018-10-12 11:33:52 -0700206 for (const auto& [callbackIds, transactionStats] : listenerStats.transactionStats) {
207 for (auto callbackId : callbackIds) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800208 auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId];
209 surfaceControls.insert(callbackSurfaceControls.begin(), callbackSurfaceControls.end());
210 }
211 }
212
213 for (const auto& [callbackIds, transactionStats] : listenerStats.transactionStats) {
214 for (auto callbackId : callbackIds) {
215 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 Wall73411622019-01-25 10:45:41 -0800236class BufferCache : public Singleton<BufferCache> {
237public:
238 BufferCache() : token(new BBinder()) {}
239
240 sp<IBinder> getToken() {
241 return IInterface::asBinder(TransactionCompletedListener::getIInstance());
242 }
243
244 int32_t getId(const sp<GraphicBuffer>& buffer) {
245 std::lock_guard lock(mMutex);
246
247 auto itr = mBuffers.find(buffer);
248 if (itr == mBuffers.end()) {
249 return -1;
250 }
251 itr->second.counter = getCounter();
252 return itr->second.id;
253 }
254
255 int32_t cache(const sp<GraphicBuffer>& buffer) {
256 std::lock_guard lock(mMutex);
257
258 int32_t bufferId = getNextAvailableId();
259
260 mBuffers[buffer].id = bufferId;
261 mBuffers[buffer].counter = getCounter();
262 return bufferId;
263 }
264
265private:
266 int32_t evictDestroyedBuffer() REQUIRES(mMutex) {
267 auto itr = mBuffers.begin();
268 while (itr != mBuffers.end()) {
269 auto& buffer = itr->first;
270 if (buffer == nullptr || buffer.promote() == nullptr) {
271 int32_t bufferId = itr->second.id;
272 mBuffers.erase(itr);
273 return bufferId;
274 }
275 itr++;
276 }
277 return -1;
278 }
279
280 int32_t evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) {
281 if (mBuffers.size() < 0) {
282 return -1;
283 }
284 auto itr = mBuffers.begin();
285 uint64_t minCounter = itr->second.counter;
286 auto minBuffer = itr;
287 itr++;
288
289 while (itr != mBuffers.end()) {
290 uint64_t counter = itr->second.counter;
291 if (counter < minCounter) {
292 minCounter = counter;
293 minBuffer = itr;
294 }
295 itr++;
296 }
297 int32_t minBufferId = minBuffer->second.id;
298 mBuffers.erase(minBuffer);
299 return minBufferId;
300 }
301
302 int32_t getNextAvailableId() REQUIRES(mMutex) {
303 static int32_t id = 0;
304 if (id + 1 < BUFFER_CACHE_MAX_SIZE) {
305 return id++;
306 }
307
308 // There are no more valid cache ids. To set additional buffers, evict existing buffers
309 // and reuse their cache ids.
310 int32_t bufferId = evictDestroyedBuffer();
311 if (bufferId > 0) {
312 return bufferId;
313 }
314 return evictLeastRecentlyUsedBuffer();
315 }
316
317 uint64_t getCounter() REQUIRES(mMutex) {
318 static uint64_t counter = 0;
319 return counter++;
320 }
321
322 struct Metadata {
323 // The cache id of a buffer that can be set to ISurfaceComposer. When ISurfaceComposer
324 // recieves this id, it can retrieve the buffer from its cache. Caching GraphicBuffers
325 // is important because sending them across processes is expensive.
326 int32_t id = 0;
327 // When a buffer is set, a counter is incremented and stored in the cache's metadata.
328 // When an buffer must be evicted, the entry with the lowest counter value is chosen.
329 uint64_t counter = 0;
330 };
331
332 std::mutex mMutex;
333 std::map<wp<GraphicBuffer>, Metadata> mBuffers GUARDED_BY(mMutex);
334
335 // Used by ISurfaceComposer to identify which process is sending the cached buffer.
336 sp<IBinder> token;
337};
338
339ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache);
340
341// ---------------------------------------------------------------------------
342
Marissa Wall17b4e452018-12-26 16:32:34 -0800343SurfaceComposerClient::Transaction::Transaction(const Transaction& other)
344 : mForceSynchronous(other.mForceSynchronous),
345 mTransactionNestCount(other.mTransactionNestCount),
346 mAnimation(other.mAnimation),
347 mEarlyWakeup(other.mEarlyWakeup),
348 mDesiredPresentTime(other.mDesiredPresentTime) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700349 mDisplayStates = other.mDisplayStates;
350 mComposerStates = other.mComposerStates;
chaviw273171b2018-12-26 11:46:30 -0800351 mInputWindowCommands = other.mInputWindowCommands;
Mathias Agopian698c0872011-06-28 19:09:31 -0700352}
353
Robert Carr2c5f6d22017-09-26 12:30:35 -0700354SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
chaviw8e3fe5d2018-02-22 10:55:42 -0800355 for (auto const& kv : other.mComposerStates) {
356 if (mComposerStates.count(kv.first) == 0) {
357 mComposerStates[kv.first] = kv.second;
Robert Carr2c5f6d22017-09-26 12:30:35 -0700358 } else {
chaviw8e3fe5d2018-02-22 10:55:42 -0800359 mComposerStates[kv.first].state.merge(kv.second.state);
Robert Carr2c5f6d22017-09-26 12:30:35 -0700360 }
361 }
362 other.mComposerStates.clear();
363
364 for (auto const& state : other.mDisplayStates) {
365 ssize_t index = mDisplayStates.indexOf(state);
366 if (index < 0) {
367 mDisplayStates.add(state);
368 } else {
369 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
370 }
371 }
372 other.mDisplayStates.clear();
373
Marissa Wallc837b5e2018-10-12 10:04:44 -0700374 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) {
375 auto& [callbackIds, surfaceControls] = callbackInfo;
376 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator(
377 callbackIds.begin()),
378 std::make_move_iterator(callbackIds.end()));
379 mListenerCallbacks[listener]
380 .surfaceControls.insert(std::make_move_iterator(surfaceControls.begin()),
381 std::make_move_iterator(surfaceControls.end()));
382 }
383 other.mListenerCallbacks.clear();
384
chaviw273171b2018-12-26 11:46:30 -0800385 mInputWindowCommands.merge(other.mInputWindowCommands);
Vishnu Nairec0ab382019-02-13 15:32:56 -0800386 other.mInputWindowCommands.clear();
chaviw273171b2018-12-26 11:46:30 -0800387
Robert Carr2c5f6d22017-09-26 12:30:35 -0700388 return *this;
389}
390
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800391void SurfaceComposerClient::doDropReferenceTransaction(const sp<IBinder>& handle,
392 const sp<ISurfaceComposerClient>& client) {
393 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
394 Vector<ComposerState> composerStates;
395 Vector<DisplayState> displayStates;
396
397 ComposerState s;
398 s.client = client;
399 s.state.surface = handle;
400 s.state.what |= layer_state_t::eReparent;
401 s.state.parentHandleForChild = nullptr;
402
403 composerStates.add(s);
Marissa Wall17b4e452018-12-26 16:32:34 -0800404 sf->setTransactionState(composerStates, displayStates, 0, nullptr, {}, -1);
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800405}
406
Robert Carr4cdc58f2017-08-23 14:22:20 -0700407status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
408 if (mStatus != NO_ERROR) {
409 return mStatus;
410 }
411
412 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
413
Marissa Wallc837b5e2018-10-12 10:04:44 -0700414 // For every listener with registered callbacks
415 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
416 auto& [callbackIds, surfaceControls] = callbackInfo;
417 if (callbackIds.empty()) {
418 continue;
419 }
420
Marissa Walle2ffb422018-10-12 11:33:52 -0700421 // If the listener does not have any SurfaceControls set on this Transaction, send the
422 // callback now
423 if (surfaceControls.empty()) {
424 listener->onTransactionCompleted(ListenerStats::createEmpty(listener, callbackIds));
425 }
426
Marissa Wallc837b5e2018-10-12 10:04:44 -0700427 // If the listener has any SurfaceControls set on this Transaction update the surface state
428 for (const auto& surfaceControl : surfaceControls) {
429 layer_state_t* s = getLayerState(surfaceControl);
430 if (!s) {
431 ALOGE("failed to get layer state");
432 continue;
433 }
434 s->what |= layer_state_t::eListenerCallbacksChanged;
435 s->listenerCallbacks.emplace_back(listener, std::move(callbackIds));
436 }
437 }
438 mListenerCallbacks.clear();
439
Robert Carr4cdc58f2017-08-23 14:22:20 -0700440 Vector<ComposerState> composerStates;
441 Vector<DisplayState> displayStates;
442 uint32_t flags = 0;
443
444 mForceSynchronous |= synchronous;
445
chaviw8e3fe5d2018-02-22 10:55:42 -0800446 for (auto const& kv : mComposerStates){
447 composerStates.add(kv.second);
448 }
449
Robert Carr4cdc58f2017-08-23 14:22:20 -0700450 mComposerStates.clear();
451
452 displayStates = mDisplayStates;
453 mDisplayStates.clear();
454
455 if (mForceSynchronous) {
456 flags |= ISurfaceComposer::eSynchronous;
457 }
458 if (mAnimation) {
459 flags |= ISurfaceComposer::eAnimation;
460 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700461 if (mEarlyWakeup) {
462 flags |= ISurfaceComposer::eEarlyWakeup;
463 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700464
465 mForceSynchronous = false;
466 mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700467 mEarlyWakeup = false;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700468
Marissa Wall713b63f2018-10-17 15:42:43 -0700469 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Marissa Wall17b4e452018-12-26 16:32:34 -0800470 sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands,
471 mDesiredPresentTime);
chaviw273171b2018-12-26 11:46:30 -0800472 mInputWindowCommands.clear();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700473 mStatus = NO_ERROR;
474 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700475}
476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477// ---------------------------------------------------------------------------
478
Robert Carr4cdc58f2017-08-23 14:22:20 -0700479sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700480 return ComposerService::getComposerService()->createDisplay(displayName,
481 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700482}
483
Robert Carr4cdc58f2017-08-23 14:22:20 -0700484void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700485 return ComposerService::getComposerService()->destroyDisplay(display);
486}
487
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800488std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
489 return ComposerService::getComposerService()->getPhysicalDisplayIds();
490}
491
492std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
493 return ComposerService::getComposerService()->getInternalDisplayId();
494}
495
496sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
497 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
498}
499
500sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
501 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700502}
503
Robert Carr4cdc58f2017-08-23 14:22:20 -0700504void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700505 mAnimation = true;
506}
507
Dan Stoza84d619e2018-03-28 17:07:36 -0700508void SurfaceComposerClient::Transaction::setEarlyWakeup() {
509 mEarlyWakeup = true;
510}
511
chaviw763ef572018-02-22 16:04:57 -0800512layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
chaviw8e3fe5d2018-02-22 10:55:42 -0800513 if (mComposerStates.count(sc) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700514 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -0800515 ComposerState s;
516 s.client = sc->getClient()->mClient;
517 s.state.surface = sc->getHandle();
518 mComposerStates[sc] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -0700519 }
520
chaviw8e3fe5d2018-02-22 10:55:42 -0800521 return &(mComposerStates[sc].state);
Mathias Agopian698c0872011-06-28 19:09:31 -0700522}
523
Marissa Wallc837b5e2018-10-12 10:04:44 -0700524void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
525 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800526 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
527 callbackInfo.surfaceControls.insert(sc);
528
529 TransactionCompletedListener::getInstance()
530 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700531}
532
Robert Carr4cdc58f2017-08-23 14:22:20 -0700533SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
534 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -0800535 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700536 if (!s) {
537 mStatus = BAD_INDEX;
538 return *this;
539 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700540 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700541 s->x = x;
542 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700543
544 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700545 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700546}
547
Robert Carr4cdc58f2017-08-23 14:22:20 -0700548SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
549 const sp<SurfaceControl>& sc) {
550 return setFlags(sc, 0, layer_state_t::eLayerHidden);
551}
552
553SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
554 const sp<SurfaceControl>& sc) {
555 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
556}
557
558SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
559 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-22 16:04:57 -0800560 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700561 if (!s) {
562 mStatus = BAD_INDEX;
563 return *this;
564 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700565 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700566 s->w = w;
567 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700568
Marissa Wallc837b5e2018-10-12 10:04:44 -0700569 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700570 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700571}
572
Robert Carr4cdc58f2017-08-23 14:22:20 -0700573SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
574 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800575 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700576 if (!s) {
577 mStatus = BAD_INDEX;
578 return *this;
579 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700580 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700581 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700582
583 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700584 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700585}
586
Robert Carr4cdc58f2017-08-23 14:22:20 -0700587SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
Robert Carrdb66e622017-04-10 16:55:57 -0700588 int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800589 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700590 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700591 mStatus = BAD_INDEX;
Robert Carrdb66e622017-04-10 16:55:57 -0700592 }
593 s->what |= layer_state_t::eRelativeLayerChanged;
594 s->relativeLayerHandle = relativeTo;
595 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700596
597 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700598 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700599}
600
Robert Carr4cdc58f2017-08-23 14:22:20 -0700601SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
602 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700603 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -0800604 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700605 if (!s) {
606 mStatus = BAD_INDEX;
607 return *this;
608 }
Pablo Ceballos53390e12015-08-04 11:25:59 -0700609 if ((mask & layer_state_t::eLayerOpaque) ||
610 (mask & layer_state_t::eLayerHidden) ||
611 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700612 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800613 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700614 s->flags &= ~mask;
615 s->flags |= (flags & mask);
616 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700617
618 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700619 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700620}
621
Robert Carr4cdc58f2017-08-23 14:22:20 -0700622SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
623 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700624 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800625 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700626 if (!s) {
627 mStatus = BAD_INDEX;
628 return *this;
629 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700630 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700631 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700632
633 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700634 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700635}
636
Robert Carr4cdc58f2017-08-23 14:22:20 -0700637SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
638 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800639 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700640 if (!s) {
641 mStatus = BAD_INDEX;
642 return *this;
643 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700644 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700645 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700646
647 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700648 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700649}
650
Robert Carr4cdc58f2017-08-23 14:22:20 -0700651SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
652 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -0800653 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700654 if (!s) {
655 mStatus = BAD_INDEX;
656 return *this;
657 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700658 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700659 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700660
661 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700662 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700663}
664
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800665SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
666 const sp<SurfaceControl>& sc, uint32_t key, std::vector<uint8_t> data) {
667 layer_state_t* s = getLayerState(sc);
668 if (!s) {
669 mStatus = BAD_INDEX;
670 return *this;
671 }
672 s->what |= layer_state_t::eMetadataChanged;
673 s->metadata.mMap[key] = std::move(data);
674
675 registerSurfaceControlForCallback(sc);
676 return *this;
677}
678
Robert Carr4cdc58f2017-08-23 14:22:20 -0700679SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
680 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800681 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800682 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700683 if (!s) {
684 mStatus = BAD_INDEX;
685 return *this;
686 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700687 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700688 layer_state_t::matrix22_t matrix;
689 matrix.dsdx = dsdx;
690 matrix.dtdx = dtdx;
691 matrix.dsdy = dsdy;
692 matrix.dtdy = dtdy;
693 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700694
695 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700696 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700697}
698
Marissa Wallf58c14b2018-07-24 10:50:43 -0700699SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700700 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800701 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700702 if (!s) {
703 mStatus = BAD_INDEX;
704 return *this;
705 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700706 s->what |= layer_state_t::eCropChanged_legacy;
707 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700708
709 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700710 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700711}
712
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700713SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
714 const sp<SurfaceControl>& sc, float cornerRadius) {
715 layer_state_t* s = getLayerState(sc);
716 if (!s) {
717 mStatus = BAD_INDEX;
718 return *this;
719 }
720 s->what |= layer_state_t::eCornerRadiusChanged;
721 s->cornerRadius = cornerRadius;
722 return *this;
723}
724
Marissa Wallf58c14b2018-07-24 10:50:43 -0700725SurfaceComposerClient::Transaction&
726SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
727 const sp<IBinder>& handle,
728 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800729 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700731 mStatus = BAD_INDEX;
732 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700733 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700734 s->what |= layer_state_t::eDeferTransaction_legacy;
735 s->barrierHandle_legacy = handle;
736 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700737
738 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700739 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800740}
741
Marissa Wallf58c14b2018-07-24 10:50:43 -0700742SurfaceComposerClient::Transaction&
743SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
744 const sp<Surface>& barrierSurface,
745 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800746 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -0800747 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700748 mStatus = BAD_INDEX;
749 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800750 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700751 s->what |= layer_state_t::eDeferTransaction_legacy;
752 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
753 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700754
755 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700756 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700757}
758
Robert Carr4cdc58f2017-08-23 14:22:20 -0700759SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
760 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -0800761 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800762 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -0800763 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700764 mStatus = BAD_INDEX;
765 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800766 }
767 s->what |= layer_state_t::eReparentChildren;
768 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700769
770 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700771 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800772}
773
Robert Carr4cdc58f2017-08-23 14:22:20 -0700774SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
775 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -0700776 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800777 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -0700778 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700779 mStatus = BAD_INDEX;
780 return *this;
chaviw06178942017-07-27 10:25:59 -0700781 }
chaviwf1961f72017-09-18 16:41:07 -0700782 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700783 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700784
785 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700786 return *this;
chaviw06178942017-07-27 10:25:59 -0700787}
788
Robert Carr4cdc58f2017-08-23 14:22:20 -0700789SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
790 const sp<SurfaceControl>& sc,
791 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -0800792 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -0800793 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700794 mStatus = BAD_INDEX;
795 return *this;
796 }
797 s->what |= layer_state_t::eColorChanged;
798 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700799
800 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700801 return *this;
802}
803
Valerie Haudd0b7572019-01-29 14:59:27 -0800804SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
805 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -0800806 layer_state_t* s = getLayerState(sc);
807 if (!s) {
808 mStatus = BAD_INDEX;
809 return *this;
810 }
811
Valerie Haudd0b7572019-01-29 14:59:27 -0800812 s->what |= layer_state_t::eBackgroundColorChanged;
813 s->color = color;
814 s->bgColorAlpha = alpha;
815 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -0800816
817 registerSurfaceControlForCallback(sc);
818 return *this;
819}
820
Marissa Wall61c58622018-07-18 10:12:20 -0700821SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
822 const sp<SurfaceControl>& sc, uint32_t transform) {
823 layer_state_t* s = getLayerState(sc);
824 if (!s) {
825 mStatus = BAD_INDEX;
826 return *this;
827 }
828 s->what |= layer_state_t::eTransformChanged;
829 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700830
831 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700832 return *this;
833}
834
835SurfaceComposerClient::Transaction&
836SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
837 bool transformToDisplayInverse) {
838 layer_state_t* s = getLayerState(sc);
839 if (!s) {
840 mStatus = BAD_INDEX;
841 return *this;
842 }
843 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
844 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700845
846 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700847 return *this;
848}
849
850SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
851 const sp<SurfaceControl>& sc, const Rect& crop) {
852 layer_state_t* s = getLayerState(sc);
853 if (!s) {
854 mStatus = BAD_INDEX;
855 return *this;
856 }
857 s->what |= layer_state_t::eCropChanged;
858 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700859
860 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700861 return *this;
862}
863
Marissa Wall861616d2018-10-22 12:52:23 -0700864SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
865 const sp<SurfaceControl>& sc, const Rect& frame) {
866 layer_state_t* s = getLayerState(sc);
867 if (!s) {
868 mStatus = BAD_INDEX;
869 return *this;
870 }
871 s->what |= layer_state_t::eFrameChanged;
872 s->frame = frame;
873
874 registerSurfaceControlForCallback(sc);
875 return *this;
876}
877
Marissa Wall61c58622018-07-18 10:12:20 -0700878SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
879 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
880 layer_state_t* s = getLayerState(sc);
881 if (!s) {
882 mStatus = BAD_INDEX;
883 return *this;
884 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700885
Marissa Wall73411622019-01-25 10:45:41 -0800886 int32_t bufferId = BufferCache::getInstance().getId(buffer);
887 if (bufferId < 0) {
888 bufferId = BufferCache::getInstance().cache(buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700889
Marissa Wall73411622019-01-25 10:45:41 -0800890 s->what |= layer_state_t::eBufferChanged;
891 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -0800892 }
Marissa Wall73411622019-01-25 10:45:41 -0800893
Marissa Wallebc2c052019-01-16 19:16:55 -0800894 s->what |= layer_state_t::eCachedBufferChanged;
Marissa Wall73411622019-01-25 10:45:41 -0800895 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wallebc2c052019-01-16 19:16:55 -0800896 s->cachedBuffer.bufferId = bufferId;
897
898 registerSurfaceControlForCallback(sc);
899 return *this;
900}
901
Marissa Wall61c58622018-07-18 10:12:20 -0700902SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
903 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
904 layer_state_t* s = getLayerState(sc);
905 if (!s) {
906 mStatus = BAD_INDEX;
907 return *this;
908 }
909 s->what |= layer_state_t::eAcquireFenceChanged;
910 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700911
912 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700913 return *this;
914}
915
916SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
917 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
918 layer_state_t* s = getLayerState(sc);
919 if (!s) {
920 mStatus = BAD_INDEX;
921 return *this;
922 }
923 s->what |= layer_state_t::eDataspaceChanged;
924 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700925
926 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700927 return *this;
928}
929
930SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
931 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
932 layer_state_t* s = getLayerState(sc);
933 if (!s) {
934 mStatus = BAD_INDEX;
935 return *this;
936 }
937 s->what |= layer_state_t::eHdrMetadataChanged;
938 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700939
940 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700941 return *this;
942}
943
944SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
945 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
946 layer_state_t* s = getLayerState(sc);
947 if (!s) {
948 mStatus = BAD_INDEX;
949 return *this;
950 }
951 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
952 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700953
954 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700955 return *this;
956}
957
958SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
959 const sp<SurfaceControl>& sc, int32_t api) {
960 layer_state_t* s = getLayerState(sc);
961 if (!s) {
962 mStatus = BAD_INDEX;
963 return *this;
964 }
965 s->what |= layer_state_t::eApiChanged;
966 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700967
968 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700969 return *this;
970}
971
972SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
973 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
974 layer_state_t* s = getLayerState(sc);
975 if (!s) {
976 mStatus = BAD_INDEX;
977 return *this;
978 }
979 s->what |= layer_state_t::eSidebandStreamChanged;
980 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700981
982 registerSurfaceControlForCallback(sc);
983 return *this;
984}
985
Marissa Wall17b4e452018-12-26 16:32:34 -0800986SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
987 nsecs_t desiredPresentTime) {
988 mDesiredPresentTime = desiredPresentTime;
989 return *this;
990}
991
Marissa Wallc837b5e2018-10-12 10:04:44 -0700992SurfaceComposerClient::Transaction&
993SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -0700994 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700995 auto listener = TransactionCompletedListener::getInstance();
996
Marissa Wall80d94ad2019-01-18 16:04:36 -0800997 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
998 std::placeholders::_2, std::placeholders::_3);
999 const auto& surfaceControls =
1000 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001001
Marissa Wall80d94ad2019-01-18 16:04:36 -08001002 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001003
1004 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1005 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001006 return *this;
1007}
1008
Robert Carr4cdc58f2017-08-23 14:22:20 -07001009SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1010 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001011 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001012 if (!s) {
1013 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -08001014 }
1015 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001016
1017 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001018 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001019}
1020
Robert Carr4cdc58f2017-08-23 14:22:20 -07001021SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1022 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001023 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001024 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001025 mStatus = BAD_INDEX;
1026 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001027 }
1028
1029 switch (overrideScalingMode) {
1030 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1031 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1032 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1033 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1034 case -1:
1035 break;
1036 default:
1037 ALOGE("unknown scaling mode: %d",
1038 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001039 mStatus = BAD_VALUE;
1040 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001041 }
1042
1043 s->what |= layer_state_t::eOverrideScalingModeChanged;
1044 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001045
1046 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001047 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001048}
1049
Robert Carr4cdc58f2017-08-23 14:22:20 -07001050SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1051 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001052 layer_state_t* s = getLayerState(sc);
Robert Carr82364e32016-05-15 11:27:47 -07001053 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001054 mStatus = BAD_INDEX;
1055 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001056 }
Robert Carr99e27f02016-06-16 15:18:02 -07001057 s->what |= layer_state_t::eGeometryAppliesWithResize;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001058
1059 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001060 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001061}
1062
Robert Carr2c358bf2018-08-08 15:58:15 -07001063#ifndef NO_INPUT
1064SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1065 const sp<SurfaceControl>& sc,
1066 const InputWindowInfo& info) {
1067 layer_state_t* s = getLayerState(sc);
1068 if (!s) {
1069 mStatus = BAD_INDEX;
1070 return *this;
1071 }
1072 s->inputInfo = info;
1073 s->what |= layer_state_t::eInputInfoChanged;
1074 return *this;
1075}
chaviw273171b2018-12-26 11:46:30 -08001076
1077SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus(
1078 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
1079 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand;
1080 transferTouchFocusCommand.fromToken = fromToken;
1081 transferTouchFocusCommand.toToken = toToken;
1082 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand);
1083 return *this;
1084}
1085
chaviwa911b102019-02-14 10:18:33 -08001086SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1087 mInputWindowCommands.syncInputWindows = true;
1088 return *this;
1089}
1090
Robert Carr2c358bf2018-08-08 15:58:15 -07001091#endif
1092
Peiyong Lind3788632018-09-18 16:01:31 -07001093SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1094 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1095 layer_state_t* s = getLayerState(sc);
1096 if (!s) {
1097 mStatus = BAD_INDEX;
1098 return *this;
1099 }
1100 s->what |= layer_state_t::eColorTransformChanged;
1101 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001102
1103 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001104 return *this;
1105}
1106
Robert Carrfb4d58b2019-01-15 09:21:27 -08001107SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1108 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1109 setCrop_legacy(sc, source);
1110
1111 int x = dst.left;
1112 int y = dst.top;
1113 float xScale = dst.getWidth() / static_cast<float>(source.getWidth());
1114 float yScale = dst.getHeight() / static_cast<float>(source.getHeight());
1115 float matrix[4] = {1, 0, 0, 1};
1116
1117 switch (transform) {
1118 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1119 matrix[0] = -xScale; matrix[1] = 0;
1120 matrix[2] = 0; matrix[3] = yScale;
1121 x += source.getWidth();
1122 break;
1123 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1124 matrix[0] = xScale; matrix[1] = 0;
1125 matrix[2] = 0; matrix[3] = -yScale;
1126 y += source.getHeight();
1127 break;
1128 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1129 matrix[0] = 0; matrix[1] = -yScale;
1130 matrix[2] = xScale; matrix[3] = 0;
1131 x += source.getHeight();
1132 break;
1133 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1134 matrix[0] = -xScale; matrix[1] = 0;
1135 matrix[2] = 0; matrix[3] = -yScale;
1136 x += source.getWidth();
1137 y += source.getHeight();
1138 break;
1139 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1140 matrix[0] = 0; matrix[1] = yScale;
1141 matrix[2] = -xScale; matrix[3] = 0;
1142 y += source.getWidth();
1143 break;
1144 default:
1145 matrix[0] = xScale; matrix[1] = 0;
1146 matrix[2] = 0; matrix[3] = yScale;
1147 break;
1148 }
1149 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
1150 setPosition(sc, x, y);
1151
1152 return *this;
1153}
1154
Mathias Agopian698c0872011-06-28 19:09:31 -07001155// ---------------------------------------------------------------------------
1156
chaviw763ef572018-02-22 16:04:57 -08001157DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001158 DisplayState s;
1159 s.token = token;
1160 ssize_t index = mDisplayStates.indexOf(s);
1161 if (index < 0) {
1162 // we don't have it, add an initialized layer_state to our list
1163 s.what = 0;
1164 index = mDisplayStates.add(s);
1165 }
Dan Stozad723bd72014-11-18 10:24:03 -08001166 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167}
1168
Robert Carr4cdc58f2017-08-23 14:22:20 -07001169status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1170 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001171 if (bufferProducer.get() != nullptr) {
1172 // Make sure that composition can never be stalled by a virtual display
1173 // consumer that isn't processing buffers fast enough.
1174 status_t err = bufferProducer->setAsyncMode(true);
1175 if (err != NO_ERROR) {
1176 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1177 "BufferQueue. This BufferQueue cannot be used for virtual "
1178 "display. (%d)", err);
1179 return err;
1180 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001181 }
chaviw763ef572018-02-22 16:04:57 -08001182 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001183 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001184 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001185 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001186}
1187
Robert Carr4cdc58f2017-08-23 14:22:20 -07001188void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001189 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001190 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001191 s.layerStack = layerStack;
1192 s.what |= DisplayState::eLayerStackChanged;
1193}
1194
Robert Carr4cdc58f2017-08-23 14:22:20 -07001195void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001196 uint32_t orientation,
1197 const Rect& layerStackRect,
1198 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001199 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001200 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001201 s.viewport = layerStackRect;
1202 s.frame = displayRect;
1203 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001204 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001205}
1206
Robert Carr4cdc58f2017-08-23 14:22:20 -07001207void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001208 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001209 s.width = width;
1210 s.height = height;
1211 s.what |= DisplayState::eDisplaySizeChanged;
1212}
1213
Mathias Agopiane57f2922012-08-09 16:29:12 -07001214// ---------------------------------------------------------------------------
1215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001217 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219}
1220
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001221SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1222 : mStatus(NO_ERROR), mClient(client)
1223{
1224}
1225
Mathias Agopian698c0872011-06-28 19:09:31 -07001226void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001227 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001228 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001229 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001230 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001231 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001232 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001233 mStatus = NO_ERROR;
1234 }
1235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236}
1237
Mathias Agopian698c0872011-06-28 19:09:31 -07001238SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001239 dispose();
1240}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001241
Mathias Agopian698c0872011-06-28 19:09:31 -07001242status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 return mStatus;
1244}
1245
Mathias Agopian698c0872011-06-28 19:09:31 -07001246sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001247 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248}
1249
Mathias Agopiand4784a32010-05-27 19:41:15 -07001250status_t SurfaceComposerClient::linkToComposerDeath(
1251 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001252 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001253 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1254 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255}
1256
Mathias Agopian698c0872011-06-28 19:09:31 -07001257void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001259 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001260 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001261 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001262 client = mClient; // hold ref while lock is held
1263 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001265 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266}
1267
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001268sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1269 PixelFormat format, uint32_t flags,
1270 SurfaceControl* parent,
1271 LayerMetadata metadata) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001272 sp<SurfaceControl> s;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001273 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
Robert Carr3b382ed2018-03-14 13:49:41 -07001274 return s;
1275}
1276
Marissa Wall35187b32019-01-08 10:08:52 -08001277sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1278 uint32_t h, PixelFormat format,
1279 uint32_t flags, Surface* parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001280 LayerMetadata metadata) {
Marissa Wall35187b32019-01-08 10:08:52 -08001281 sp<SurfaceControl> sur;
1282 status_t err = mStatus;
1283
1284 if (mStatus == NO_ERROR) {
1285 sp<IBinder> handle;
1286 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1287 sp<IGraphicBufferProducer> gbp;
1288
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001289 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1290 std::move(metadata), &handle, &gbp);
Marissa Wall35187b32019-01-08 10:08:52 -08001291 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1292 if (err == NO_ERROR) {
1293 return new SurfaceControl(this, handle, gbp, true /* owned */);
1294 }
1295 }
1296 return nullptr;
1297}
1298
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001299status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1300 PixelFormat format,
1301 sp<SurfaceControl>* outSurface, uint32_t flags,
1302 SurfaceControl* parent,
1303 LayerMetadata metadata) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001304 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001305 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001306
Mathias Agopian698c0872011-06-28 19:09:31 -07001307 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001308 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001309 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001310 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001311
1312 if (parent != nullptr) {
1313 parentHandle = parent->getHandle();
1314 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001315
1316 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1317 &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001318 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1319 if (err == NO_ERROR) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001320 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Mathias Agopian698c0872011-06-28 19:09:31 -07001321 }
1322 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001323 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001324}
1325
Svetoslavd85084b2014-03-20 10:28:31 -07001326status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1327 if (mStatus != NO_ERROR) {
1328 return mStatus;
1329 }
1330 return mClient->clearLayerFrameStats(token);
1331}
1332
1333status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1334 FrameStats* outStats) const {
1335 if (mStatus != NO_ERROR) {
1336 return mStatus;
1337 }
1338 return mClient->getLayerFrameStats(token, outStats);
1339}
1340
Mathias Agopian698c0872011-06-28 19:09:31 -07001341// ----------------------------------------------------------------------------
1342
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001343status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001344 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1345 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001346}
1347
1348status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001349 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1350 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001351}
1352
Dan Stoza7f7da322014-05-02 15:26:25 -07001353status_t SurfaceComposerClient::getDisplayConfigs(
1354 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355{
Dan Stoza7f7da322014-05-02 15:26:25 -07001356 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1357}
1358
1359status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
1360 DisplayInfo* info) {
1361 Vector<DisplayInfo> configs;
1362 status_t result = getDisplayConfigs(display, &configs);
1363 if (result != NO_ERROR) {
1364 return result;
1365 }
1366
1367 int activeId = getActiveConfig(display);
1368 if (activeId < 0) {
1369 ALOGE("No active configuration found");
1370 return NAME_NOT_FOUND;
1371 }
1372
Dan Stozad723bd72014-11-18 10:24:03 -08001373 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001374 return NO_ERROR;
1375}
1376
1377int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1378 return ComposerService::getComposerService()->getActiveConfig(display);
1379}
1380
1381status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1382 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383}
1384
Ady Abraham838de062019-02-04 10:24:03 -08001385status_t SurfaceComposerClient::setAllowedDisplayConfigs(
1386 const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) {
1387 return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken,
1388 allowedConfigs);
1389}
1390
Ady Abrahamd9b3ea62019-02-26 14:08:03 -08001391status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
1392 std::vector<int32_t>* outAllowedConfigs) {
1393 return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken,
1394 outAllowedConfigs);
1395}
1396
Michael Wright28f24d02016-07-12 13:30:53 -07001397status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001398 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001399 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1400}
1401
Daniel Solomon42d04562019-01-20 21:03:19 -08001402status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1403 ui::DisplayPrimaries& outPrimaries) {
1404 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1405}
1406
Peiyong Lina52f0292018-03-14 17:26:31 -07001407ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001408 return ComposerService::getComposerService()->getActiveColorMode(display);
1409}
1410
1411status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001412 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001413 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1414}
1415
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001416void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1417 int mode) {
1418 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001419}
1420
Peiyong Linc6780972018-10-28 15:24:08 -07001421status_t SurfaceComposerClient::getCompositionPreference(
1422 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1423 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1424 return ComposerService::getComposerService()
1425 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1426 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001427}
1428
Peiyong Lin08d10512019-01-16 13:27:35 -08001429bool SurfaceComposerClient::getProtectedContentSupport() {
1430 bool supported = false;
1431 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1432 return supported;
1433}
1434
Svetoslavd85084b2014-03-20 10:28:31 -07001435status_t SurfaceComposerClient::clearAnimationFrameStats() {
1436 return ComposerService::getComposerService()->clearAnimationFrameStats();
1437}
1438
1439status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1440 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1441}
1442
Dan Stozac4f471e2016-03-24 09:31:08 -07001443status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1444 HdrCapabilities* outCapabilities) {
1445 return ComposerService::getComposerService()->getHdrCapabilities(display,
1446 outCapabilities);
1447}
1448
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001449status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1450 ui::PixelFormat* outFormat,
1451 ui::Dataspace* outDataspace,
1452 uint8_t* outComponentMask) {
1453 return ComposerService::getComposerService()
1454 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1455 outComponentMask);
1456}
1457
Kevin DuBois74e53772018-11-19 10:52:38 -08001458status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1459 bool enable, uint8_t componentMask,
1460 uint64_t maxFrames) {
1461 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1462 componentMask,
1463 maxFrames);
1464}
1465
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001466status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1467 uint64_t maxFrames, uint64_t timestamp,
1468 DisplayedFrameStats* outStats) {
1469 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1470 timestamp, outStats);
1471}
Marissa Wall35187b32019-01-08 10:08:52 -08001472
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001473status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1474 bool* outIsWideColorDisplay) {
1475 return ComposerService::getComposerService()->isWideColorDisplay(display,
1476 outIsWideColorDisplay);
1477}
1478
Mathias Agopian698c0872011-06-28 19:09:31 -07001479// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480
Peiyong Lin0e003c92018-09-17 11:09:51 -07001481status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1482 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1483 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
1484 uint32_t rotation, sp<GraphicBuffer>* outBuffer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001485 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001486 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001487 status_t ret = s->captureScreen(display, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1488 reqWidth, reqHeight, useIdentityTransform,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001489 static_cast<ISurfaceComposer::Rotation>(rotation));
Robert Carr673134e2017-01-09 19:48:38 -08001490 if (ret != NO_ERROR) {
1491 return ret;
1492 }
Robert Carr673134e2017-01-09 19:48:38 -08001493 return ret;
1494}
1495
Peiyong Lin0e003c92018-09-17 11:09:51 -07001496status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1497 const ui::Dataspace reqDataSpace,
1498 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001499 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001500 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001501 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001502 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1503 sourceCrop, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001504 return ret;
1505}
1506
Peiyong Lin0e003c92018-09-17 11:09:51 -07001507status_t ScreenshotClient::captureChildLayers(const sp<IBinder>& layerHandle,
1508 const ui::Dataspace reqDataSpace,
1509 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Robert Carr578038f2018-03-09 12:25:24 -08001510 float frameScale, sp<GraphicBuffer>* outBuffer) {
1511 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001512 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001513 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1514 sourceCrop, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001515 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001516}
Mathias Agopian74c40c02010-09-29 13:02:36 -07001517// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518}; // namespace android