blob: b0e827536dbdd99c631954db13d1197a1624af8a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070024#include <utils/SortedVector.h>
25#include <utils/String8.h>
26#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070028#include <binder/IPCThreadState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <binder/IServiceManager.h>
Marissa Wall7a9b6ff2018-08-21 17:26:20 -070030#include <binder/ProcessState.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
Michael Wright28f24d02016-07-12 13:30:53 -070032#include <system/graphics.h>
33
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Robert Carr673134e2017-01-09 19:48:38 -080036#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070037#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080038#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/ISurfaceComposerClient.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070041#include <gui/LayerState.h>
Robert Carr0d480722017-01-10 16:42:54 -080042#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Robert Carr2c358bf2018-08-08 15:58:15 -070045#ifndef NO_INPUT
46#include <input/InputWindow.h>
47#endif
48
Mathias Agopian41f673c2011-11-17 17:48:35 -080049#include <private/gui/ComposerService.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Marissa Wall73411622019-01-25 10:45:41 -080051// This server size should always be smaller than the server cache size
52#define BUFFER_CACHE_MAX_SIZE 64
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054namespace android {
Peiyong Lin9f034472018-03-28 15:29:00 -070055
56using ui::ColorMode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057// ---------------------------------------------------------------------------
58
Mathias Agopian7e27f052010-05-28 14:22:23 -070059ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
60
Mathias Agopianb7e930d2010-06-01 15:12:58 -070061ComposerService::ComposerService()
62: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070063 Mutex::Autolock _l(mLock);
64 connectLocked();
65}
66
67void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070068 const String16 name("SurfaceFlinger");
69 while (getService(name, &mComposerService) != NO_ERROR) {
70 usleep(250000);
71 }
Yi Konga03e0442018-07-17 11:16:57 -070072 assert(mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070073
74 // Create the death listener.
75 class DeathObserver : public IBinder::DeathRecipient {
76 ComposerService& mComposerService;
77 virtual void binderDied(const wp<IBinder>& who) {
78 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
79 who.unsafe_get());
80 mComposerService.composerServiceDied();
81 }
82 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070083 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -070084 };
85
86 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080087 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070088}
89
Andy McFadden6652b3e2012-09-06 18:45:56 -070090/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
91 ComposerService& instance = ComposerService::getInstance();
92 Mutex::Autolock _l(instance.mLock);
Yi Kong48a619f2018-06-05 16:34:59 -070093 if (instance.mComposerService == nullptr) {
Andy McFadden6652b3e2012-09-06 18:45:56 -070094 ComposerService::getInstance().connectLocked();
Yi Konga03e0442018-07-17 11:16:57 -070095 assert(instance.mComposerService != nullptr);
Andy McFadden6652b3e2012-09-06 18:45:56 -070096 ALOGD("ComposerService reconnected");
97 }
98 return instance.mComposerService;
99}
100
101void ComposerService::composerServiceDied()
102{
103 Mutex::Autolock _l(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700104 mComposerService = nullptr;
105 mDeathObserver = nullptr;
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700106}
107
Robert Carrfb4d58b2019-01-15 09:21:27 -0800108class DefaultComposerClient: public Singleton<DefaultComposerClient> {
109 Mutex mLock;
110 sp<SurfaceComposerClient> mClient;
111 friend class Singleton<ComposerService>;
112public:
113 static sp<SurfaceComposerClient> getComposerClient() {
114 DefaultComposerClient& dc = DefaultComposerClient::getInstance();
115 Mutex::Autolock _l(dc.mLock);
116 if (dc.mClient == nullptr) {
117 dc.mClient = new SurfaceComposerClient;
118 }
119 return dc.mClient;
120 }
121};
122ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient);
123
124
125sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() {
126 return DefaultComposerClient::getComposerClient();
127}
128
Mathias Agopian7e27f052010-05-28 14:22:23 -0700129// ---------------------------------------------------------------------------
130
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700131// TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs
132// to be able to return a sp<> to its instance to pass to SurfaceFlinger.
133// ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance.
134
Marissa Wallc837b5e2018-10-12 10:04:44 -0700135// 0 is an invalid callback id
136TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {}
137
138CallbackId TransactionCompletedListener::getNextIdLocked() {
139 return mCallbackIdCounter++;
140}
141
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700142sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() {
143 static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener;
144 return sInstance;
145}
146
Marissa Wallc837b5e2018-10-12 10:04:44 -0700147sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() {
148 return static_cast<sp<ITransactionCompletedListener>>(getInstance());
149}
150
151void TransactionCompletedListener::startListeningLocked() {
Marissa Wall7a9b6ff2018-08-21 17:26:20 -0700152 if (mListening) {
153 return;
154 }
155 ProcessState::self()->startThreadPool();
156 mListening = true;
157}
158
Marissa Wall80d94ad2019-01-18 16:04:36 -0800159CallbackId TransactionCompletedListener::addCallbackFunction(
160 const TransactionCompletedCallback& callbackFunction,
161 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>&
162 surfaceControls) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700163 std::lock_guard<std::mutex> lock(mMutex);
164 startListeningLocked();
165
166 CallbackId callbackId = getNextIdLocked();
Marissa Wall80d94ad2019-01-18 16:04:36 -0800167 mCallbacks[callbackId].callbackFunction = callbackFunction;
168
169 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls;
170
171 for (const auto& surfaceControl : surfaceControls) {
172 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl;
173 }
174
Marissa Wallc837b5e2018-10-12 10:04:44 -0700175 return callbackId;
176}
177
Marissa Wall80d94ad2019-01-18 16:04:36 -0800178void TransactionCompletedListener::addSurfaceControlToCallbacks(
179 const sp<SurfaceControl>& surfaceControl,
180 const std::unordered_set<CallbackId>& callbackIds) {
181 std::lock_guard<std::mutex> lock(mMutex);
182
183 for (auto callbackId : callbackIds) {
184 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct,
185 std::forward_as_tuple(
186 surfaceControl->getHandle()),
187 std::forward_as_tuple(surfaceControl));
188 }
189}
190
Marissa Walle2ffb422018-10-12 11:33:52 -0700191void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800192 std::lock_guard<std::mutex> lock(mMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -0700193
Marissa Wall80d94ad2019-01-18 16:04:36 -0800194 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered
195 * callbackIds, except for when Transactions are merged together. This probably cannot be
196 * solved before this point because the Transactions could be merged together and applied in a
197 * different process.
198 *
199 * Fortunately, we get all the callbacks for this listener for the same frame together at the
200 * same time. This means if any Transactions were merged together, we will get their callbacks
201 * at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps for all the
202 * callbackIds to generate one super map that contains all the sp<IBinder> to sp<SurfaceControl>
203 * that could possibly exist for the callbacks.
204 */
205 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, IBinderHash> surfaceControls;
Marissa 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) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800245 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800246
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) {
Yi Kongd2639aa2019-02-28 11:58:32 -0800256 std::lock_guard<std::mutex> lock(mMutex);
Marissa Wall73411622019-01-25 10:45:41 -0800257
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
Peiyong Linc502cb72019-03-01 15:00:23 -0800992SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic(
993 const sp<SurfaceControl>& sc, const bool agnostic) {
994 layer_state_t* s = getLayerState(sc);
995 if (!s) {
996 mStatus = BAD_INDEX;
997 return *this;
998 }
999 s->what |= layer_state_t::eColorSpaceAgnosticChanged;
1000 s->colorSpaceAgnostic = agnostic;
1001
1002 registerSurfaceControlForCallback(sc);
1003 return *this;
1004}
1005
Marissa Wallc837b5e2018-10-12 10:04:44 -07001006SurfaceComposerClient::Transaction&
1007SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -07001008 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -07001009 auto listener = TransactionCompletedListener::getInstance();
1010
Marissa Wall80d94ad2019-01-18 16:04:36 -08001011 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
1012 std::placeholders::_2, std::placeholders::_3);
1013 const auto& surfaceControls =
1014 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001015
Marissa Wall80d94ad2019-01-18 16:04:36 -08001016 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001017
1018 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1019 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001020 return *this;
1021}
1022
Robert Carr4cdc58f2017-08-23 14:22:20 -07001023SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1024 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001025 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001026 if (!s) {
1027 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -08001028 }
1029 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001030
1031 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001032 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001033}
1034
Robert Carr4cdc58f2017-08-23 14:22:20 -07001035SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1036 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001037 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001038 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001039 mStatus = BAD_INDEX;
1040 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001041 }
1042
1043 switch (overrideScalingMode) {
1044 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1045 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1046 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1047 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1048 case -1:
1049 break;
1050 default:
1051 ALOGE("unknown scaling mode: %d",
1052 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001053 mStatus = BAD_VALUE;
1054 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001055 }
1056
1057 s->what |= layer_state_t::eOverrideScalingModeChanged;
1058 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001059
1060 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001061 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001062}
1063
Robert Carr4cdc58f2017-08-23 14:22:20 -07001064SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1065 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001066 layer_state_t* s = getLayerState(sc);
Robert Carr82364e32016-05-15 11:27:47 -07001067 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001068 mStatus = BAD_INDEX;
1069 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001070 }
Robert Carr99e27f02016-06-16 15:18:02 -07001071 s->what |= layer_state_t::eGeometryAppliesWithResize;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001072
1073 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001074 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001075}
1076
Robert Carr2c358bf2018-08-08 15:58:15 -07001077#ifndef NO_INPUT
1078SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1079 const sp<SurfaceControl>& sc,
1080 const InputWindowInfo& info) {
1081 layer_state_t* s = getLayerState(sc);
1082 if (!s) {
1083 mStatus = BAD_INDEX;
1084 return *this;
1085 }
1086 s->inputInfo = info;
1087 s->what |= layer_state_t::eInputInfoChanged;
1088 return *this;
1089}
chaviw273171b2018-12-26 11:46:30 -08001090
1091SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus(
1092 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
1093 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand;
1094 transferTouchFocusCommand.fromToken = fromToken;
1095 transferTouchFocusCommand.toToken = toToken;
1096 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand);
1097 return *this;
1098}
1099
chaviwa911b102019-02-14 10:18:33 -08001100SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() {
1101 mInputWindowCommands.syncInputWindows = true;
1102 return *this;
1103}
1104
Robert Carr2c358bf2018-08-08 15:58:15 -07001105#endif
1106
Peiyong Lind3788632018-09-18 16:01:31 -07001107SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1108 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1109 layer_state_t* s = getLayerState(sc);
1110 if (!s) {
1111 mStatus = BAD_INDEX;
1112 return *this;
1113 }
1114 s->what |= layer_state_t::eColorTransformChanged;
1115 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001116
1117 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001118 return *this;
1119}
1120
Robert Carrfb4d58b2019-01-15 09:21:27 -08001121SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1122 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1123 setCrop_legacy(sc, source);
1124
1125 int x = dst.left;
1126 int y = dst.top;
1127 float xScale = dst.getWidth() / static_cast<float>(source.getWidth());
1128 float yScale = dst.getHeight() / static_cast<float>(source.getHeight());
1129 float matrix[4] = {1, 0, 0, 1};
1130
1131 switch (transform) {
1132 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1133 matrix[0] = -xScale; matrix[1] = 0;
1134 matrix[2] = 0; matrix[3] = yScale;
1135 x += source.getWidth();
1136 break;
1137 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1138 matrix[0] = xScale; matrix[1] = 0;
1139 matrix[2] = 0; matrix[3] = -yScale;
1140 y += source.getHeight();
1141 break;
1142 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1143 matrix[0] = 0; matrix[1] = -yScale;
1144 matrix[2] = xScale; matrix[3] = 0;
1145 x += source.getHeight();
1146 break;
1147 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1148 matrix[0] = -xScale; matrix[1] = 0;
1149 matrix[2] = 0; matrix[3] = -yScale;
1150 x += source.getWidth();
1151 y += source.getHeight();
1152 break;
1153 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1154 matrix[0] = 0; matrix[1] = yScale;
1155 matrix[2] = -xScale; matrix[3] = 0;
1156 y += source.getWidth();
1157 break;
1158 default:
1159 matrix[0] = xScale; matrix[1] = 0;
1160 matrix[2] = 0; matrix[3] = yScale;
1161 break;
1162 }
1163 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
1164 setPosition(sc, x, y);
1165
1166 return *this;
1167}
1168
Mathias Agopian698c0872011-06-28 19:09:31 -07001169// ---------------------------------------------------------------------------
1170
chaviw763ef572018-02-22 16:04:57 -08001171DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001172 DisplayState s;
1173 s.token = token;
1174 ssize_t index = mDisplayStates.indexOf(s);
1175 if (index < 0) {
1176 // we don't have it, add an initialized layer_state to our list
1177 s.what = 0;
1178 index = mDisplayStates.add(s);
1179 }
Dan Stozad723bd72014-11-18 10:24:03 -08001180 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001181}
1182
Robert Carr4cdc58f2017-08-23 14:22:20 -07001183status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1184 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001185 if (bufferProducer.get() != nullptr) {
1186 // Make sure that composition can never be stalled by a virtual display
1187 // consumer that isn't processing buffers fast enough.
1188 status_t err = bufferProducer->setAsyncMode(true);
1189 if (err != NO_ERROR) {
1190 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1191 "BufferQueue. This BufferQueue cannot be used for virtual "
1192 "display. (%d)", err);
1193 return err;
1194 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001195 }
chaviw763ef572018-02-22 16:04:57 -08001196 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001197 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001198 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001199 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001200}
1201
Robert Carr4cdc58f2017-08-23 14:22:20 -07001202void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001203 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001204 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001205 s.layerStack = layerStack;
1206 s.what |= DisplayState::eLayerStackChanged;
1207}
1208
Robert Carr4cdc58f2017-08-23 14:22:20 -07001209void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001210 uint32_t orientation,
1211 const Rect& layerStackRect,
1212 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001213 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001214 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001215 s.viewport = layerStackRect;
1216 s.frame = displayRect;
1217 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001218 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001219}
1220
Robert Carr4cdc58f2017-08-23 14:22:20 -07001221void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001222 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001223 s.width = width;
1224 s.height = height;
1225 s.what |= DisplayState::eDisplaySizeChanged;
1226}
1227
Mathias Agopiane57f2922012-08-09 16:29:12 -07001228// ---------------------------------------------------------------------------
1229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001231 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233}
1234
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001235SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1236 : mStatus(NO_ERROR), mClient(client)
1237{
1238}
1239
Mathias Agopian698c0872011-06-28 19:09:31 -07001240void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001241 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001242 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001243 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001244 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001245 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001246 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001247 mStatus = NO_ERROR;
1248 }
1249 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250}
1251
Mathias Agopian698c0872011-06-28 19:09:31 -07001252SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001253 dispose();
1254}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001255
Mathias Agopian698c0872011-06-28 19:09:31 -07001256status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 return mStatus;
1258}
1259
Mathias Agopian698c0872011-06-28 19:09:31 -07001260sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001261 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262}
1263
Mathias Agopiand4784a32010-05-27 19:41:15 -07001264status_t SurfaceComposerClient::linkToComposerDeath(
1265 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001266 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001267 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1268 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269}
1270
Mathias Agopian698c0872011-06-28 19:09:31 -07001271void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001273 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001274 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001275 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001276 client = mClient; // hold ref while lock is held
1277 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001279 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280}
1281
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001282sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1283 PixelFormat format, uint32_t flags,
1284 SurfaceControl* parent,
1285 LayerMetadata metadata) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001286 sp<SurfaceControl> s;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001287 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
Robert Carr3b382ed2018-03-14 13:49:41 -07001288 return s;
1289}
1290
Marissa Wall35187b32019-01-08 10:08:52 -08001291sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1292 uint32_t h, PixelFormat format,
1293 uint32_t flags, Surface* parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001294 LayerMetadata metadata) {
Marissa Wall35187b32019-01-08 10:08:52 -08001295 sp<SurfaceControl> sur;
1296 status_t err = mStatus;
1297
1298 if (mStatus == NO_ERROR) {
1299 sp<IBinder> handle;
1300 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1301 sp<IGraphicBufferProducer> gbp;
1302
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001303 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1304 std::move(metadata), &handle, &gbp);
Marissa Wall35187b32019-01-08 10:08:52 -08001305 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1306 if (err == NO_ERROR) {
1307 return new SurfaceControl(this, handle, gbp, true /* owned */);
1308 }
1309 }
1310 return nullptr;
1311}
1312
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001313status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1314 PixelFormat format,
1315 sp<SurfaceControl>* outSurface, uint32_t flags,
1316 SurfaceControl* parent,
1317 LayerMetadata metadata) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001318 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001319 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001320
Mathias Agopian698c0872011-06-28 19:09:31 -07001321 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001322 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001323 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001324 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001325
1326 if (parent != nullptr) {
1327 parentHandle = parent->getHandle();
1328 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001329
1330 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1331 &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001332 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1333 if (err == NO_ERROR) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001334 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Mathias Agopian698c0872011-06-28 19:09:31 -07001335 }
1336 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001337 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001338}
1339
Svetoslavd85084b2014-03-20 10:28:31 -07001340status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1341 if (mStatus != NO_ERROR) {
1342 return mStatus;
1343 }
1344 return mClient->clearLayerFrameStats(token);
1345}
1346
1347status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1348 FrameStats* outStats) const {
1349 if (mStatus != NO_ERROR) {
1350 return mStatus;
1351 }
1352 return mClient->getLayerFrameStats(token, outStats);
1353}
1354
Mathias Agopian698c0872011-06-28 19:09:31 -07001355// ----------------------------------------------------------------------------
1356
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001357status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001358 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1359 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001360}
1361
1362status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001363 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1364 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001365}
1366
Dan Stoza7f7da322014-05-02 15:26:25 -07001367status_t SurfaceComposerClient::getDisplayConfigs(
1368 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369{
Dan Stoza7f7da322014-05-02 15:26:25 -07001370 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1371}
1372
1373status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
1374 DisplayInfo* info) {
1375 Vector<DisplayInfo> configs;
1376 status_t result = getDisplayConfigs(display, &configs);
1377 if (result != NO_ERROR) {
1378 return result;
1379 }
1380
1381 int activeId = getActiveConfig(display);
1382 if (activeId < 0) {
1383 ALOGE("No active configuration found");
1384 return NAME_NOT_FOUND;
1385 }
1386
Dan Stozad723bd72014-11-18 10:24:03 -08001387 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001388 return NO_ERROR;
1389}
1390
1391int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1392 return ComposerService::getComposerService()->getActiveConfig(display);
1393}
1394
1395status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1396 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397}
1398
Ady Abraham838de062019-02-04 10:24:03 -08001399status_t SurfaceComposerClient::setAllowedDisplayConfigs(
1400 const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) {
1401 return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken,
1402 allowedConfigs);
1403}
1404
Ady Abrahamd9b3ea62019-02-26 14:08:03 -08001405status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken,
1406 std::vector<int32_t>* outAllowedConfigs) {
1407 return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken,
1408 outAllowedConfigs);
1409}
1410
Michael Wright28f24d02016-07-12 13:30:53 -07001411status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001412 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001413 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1414}
1415
Daniel Solomon42d04562019-01-20 21:03:19 -08001416status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1417 ui::DisplayPrimaries& outPrimaries) {
1418 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1419}
1420
Peiyong Lina52f0292018-03-14 17:26:31 -07001421ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001422 return ComposerService::getComposerService()->getActiveColorMode(display);
1423}
1424
1425status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001426 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001427 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1428}
1429
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001430void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1431 int mode) {
1432 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001433}
1434
Peiyong Linc6780972018-10-28 15:24:08 -07001435status_t SurfaceComposerClient::getCompositionPreference(
1436 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1437 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1438 return ComposerService::getComposerService()
1439 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1440 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001441}
1442
Peiyong Lin08d10512019-01-16 13:27:35 -08001443bool SurfaceComposerClient::getProtectedContentSupport() {
1444 bool supported = false;
1445 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1446 return supported;
1447}
1448
Svetoslavd85084b2014-03-20 10:28:31 -07001449status_t SurfaceComposerClient::clearAnimationFrameStats() {
1450 return ComposerService::getComposerService()->clearAnimationFrameStats();
1451}
1452
1453status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1454 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1455}
1456
Dan Stozac4f471e2016-03-24 09:31:08 -07001457status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1458 HdrCapabilities* outCapabilities) {
1459 return ComposerService::getComposerService()->getHdrCapabilities(display,
1460 outCapabilities);
1461}
1462
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001463status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1464 ui::PixelFormat* outFormat,
1465 ui::Dataspace* outDataspace,
1466 uint8_t* outComponentMask) {
1467 return ComposerService::getComposerService()
1468 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1469 outComponentMask);
1470}
1471
Kevin DuBois74e53772018-11-19 10:52:38 -08001472status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1473 bool enable, uint8_t componentMask,
1474 uint64_t maxFrames) {
1475 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1476 componentMask,
1477 maxFrames);
1478}
1479
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001480status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1481 uint64_t maxFrames, uint64_t timestamp,
1482 DisplayedFrameStats* outStats) {
1483 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1484 timestamp, outStats);
1485}
Marissa Wall35187b32019-01-08 10:08:52 -08001486
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001487status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1488 bool* outIsWideColorDisplay) {
1489 return ComposerService::getComposerService()->isWideColorDisplay(display,
1490 outIsWideColorDisplay);
1491}
1492
Kevin DuBois00c66832019-02-18 16:21:31 -08001493status_t SurfaceComposerClient::addRegionSamplingListener(
1494 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
1495 const sp<IRegionSamplingListener>& listener) {
1496 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea,
1497 stopLayerHandle,
1498 listener);
1499}
1500
1501status_t SurfaceComposerClient::removeRegionSamplingListener(
1502 const sp<IRegionSamplingListener>& listener) {
1503 return ComposerService::getComposerService()->removeRegionSamplingListener(listener);
1504}
1505
Dan Gittik57e63c52019-01-18 16:37:54 +00001506bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) {
1507 bool support = false;
1508 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support);
1509 return support;
1510}
1511
1512status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken,
1513 float brightness) {
1514 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness);
1515}
1516
Mathias Agopian698c0872011-06-28 19:09:31 -07001517// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518
Peiyong Lin0e003c92018-09-17 11:09:51 -07001519status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1520 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1521 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
Robert Carrfa8855f2019-02-19 10:05:00 -08001522 uint32_t rotation, bool captureSecureLayers, sp<GraphicBuffer>* outBuffer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001523 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001524 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001525 status_t ret = s->captureScreen(display, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
Robert Carrfa8855f2019-02-19 10:05:00 -08001526 reqWidth, reqHeight, useIdentityTransform,
1527 static_cast<ISurfaceComposer::Rotation>(rotation),
1528 captureSecureLayers);
Robert Carr673134e2017-01-09 19:48:38 -08001529 if (ret != NO_ERROR) {
1530 return ret;
1531 }
Robert Carr673134e2017-01-09 19:48:38 -08001532 return ret;
1533}
1534
Robert Carrfa8855f2019-02-19 10:05:00 -08001535status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1536 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1537 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
1538 uint32_t rotation, sp<GraphicBuffer>* outBuffer) {
1539 return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth,
1540 reqHeight, useIdentityTransform, rotation, false, outBuffer);
1541}
1542
Peiyong Lin0e003c92018-09-17 11:09:51 -07001543status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1544 const ui::Dataspace reqDataSpace,
1545 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001546 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001547 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001548 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001549 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1550 sourceCrop, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001551 return ret;
1552}
1553
Peiyong Lin0e003c92018-09-17 11:09:51 -07001554status_t ScreenshotClient::captureChildLayers(const sp<IBinder>& layerHandle,
1555 const ui::Dataspace reqDataSpace,
1556 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Robert Carr578038f2018-03-09 12:25:24 -08001557 float frameScale, sp<GraphicBuffer>* outBuffer) {
1558 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001559 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001560 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1561 sourceCrop, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001562 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001563}
Mathias Agopian74c40c02010-09-29 13:02:36 -07001564// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565}; // namespace android