blob: 46b9128a49a5d2e2b53dbdd426352176de9d8c28 [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);
386
Robert Carr2c5f6d22017-09-26 12:30:35 -0700387 return *this;
388}
389
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800390void SurfaceComposerClient::doDropReferenceTransaction(const sp<IBinder>& handle,
391 const sp<ISurfaceComposerClient>& client) {
392 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
393 Vector<ComposerState> composerStates;
394 Vector<DisplayState> displayStates;
395
396 ComposerState s;
397 s.client = client;
398 s.state.surface = handle;
399 s.state.what |= layer_state_t::eReparent;
400 s.state.parentHandleForChild = nullptr;
401
402 composerStates.add(s);
Marissa Wall17b4e452018-12-26 16:32:34 -0800403 sf->setTransactionState(composerStates, displayStates, 0, nullptr, {}, -1);
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800404}
405
Robert Carr4cdc58f2017-08-23 14:22:20 -0700406status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
407 if (mStatus != NO_ERROR) {
408 return mStatus;
409 }
410
411 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
412
Marissa Wallc837b5e2018-10-12 10:04:44 -0700413 // For every listener with registered callbacks
414 for (const auto& [listener, callbackInfo] : mListenerCallbacks) {
415 auto& [callbackIds, surfaceControls] = callbackInfo;
416 if (callbackIds.empty()) {
417 continue;
418 }
419
Marissa Walle2ffb422018-10-12 11:33:52 -0700420 // If the listener does not have any SurfaceControls set on this Transaction, send the
421 // callback now
422 if (surfaceControls.empty()) {
423 listener->onTransactionCompleted(ListenerStats::createEmpty(listener, callbackIds));
424 }
425
Marissa Wallc837b5e2018-10-12 10:04:44 -0700426 // If the listener has any SurfaceControls set on this Transaction update the surface state
427 for (const auto& surfaceControl : surfaceControls) {
428 layer_state_t* s = getLayerState(surfaceControl);
429 if (!s) {
430 ALOGE("failed to get layer state");
431 continue;
432 }
433 s->what |= layer_state_t::eListenerCallbacksChanged;
434 s->listenerCallbacks.emplace_back(listener, std::move(callbackIds));
435 }
436 }
437 mListenerCallbacks.clear();
438
Robert Carr4cdc58f2017-08-23 14:22:20 -0700439 Vector<ComposerState> composerStates;
440 Vector<DisplayState> displayStates;
441 uint32_t flags = 0;
442
443 mForceSynchronous |= synchronous;
444
chaviw8e3fe5d2018-02-22 10:55:42 -0800445 for (auto const& kv : mComposerStates){
446 composerStates.add(kv.second);
447 }
448
Robert Carr4cdc58f2017-08-23 14:22:20 -0700449 mComposerStates.clear();
450
451 displayStates = mDisplayStates;
452 mDisplayStates.clear();
453
454 if (mForceSynchronous) {
455 flags |= ISurfaceComposer::eSynchronous;
456 }
457 if (mAnimation) {
458 flags |= ISurfaceComposer::eAnimation;
459 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700460 if (mEarlyWakeup) {
461 flags |= ISurfaceComposer::eEarlyWakeup;
462 }
Robert Carr4cdc58f2017-08-23 14:22:20 -0700463
464 mForceSynchronous = false;
465 mAnimation = false;
Dan Stoza84d619e2018-03-28 17:07:36 -0700466 mEarlyWakeup = false;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700467
Marissa Wall713b63f2018-10-17 15:42:43 -0700468 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance());
Marissa Wall17b4e452018-12-26 16:32:34 -0800469 sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands,
470 mDesiredPresentTime);
chaviw273171b2018-12-26 11:46:30 -0800471 mInputWindowCommands.clear();
Robert Carr4cdc58f2017-08-23 14:22:20 -0700472 mStatus = NO_ERROR;
473 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700474}
475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476// ---------------------------------------------------------------------------
477
Robert Carr4cdc58f2017-08-23 14:22:20 -0700478sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700479 return ComposerService::getComposerService()->createDisplay(displayName,
480 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700481}
482
Robert Carr4cdc58f2017-08-23 14:22:20 -0700483void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700484 return ComposerService::getComposerService()->destroyDisplay(display);
485}
486
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800487std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() {
488 return ComposerService::getComposerService()->getPhysicalDisplayIds();
489}
490
491std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() {
492 return ComposerService::getComposerService()->getInternalDisplayId();
493}
494
495sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) {
496 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId);
497}
498
499sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() {
500 return ComposerService::getComposerService()->getInternalDisplayToken();
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700501}
502
Robert Carr4cdc58f2017-08-23 14:22:20 -0700503void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700504 mAnimation = true;
505}
506
Dan Stoza84d619e2018-03-28 17:07:36 -0700507void SurfaceComposerClient::Transaction::setEarlyWakeup() {
508 mEarlyWakeup = true;
509}
510
chaviw763ef572018-02-22 16:04:57 -0800511layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
chaviw8e3fe5d2018-02-22 10:55:42 -0800512 if (mComposerStates.count(sc) == 0) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700513 // we don't have it, add an initialized layer_state to our list
chaviw8e3fe5d2018-02-22 10:55:42 -0800514 ComposerState s;
515 s.client = sc->getClient()->mClient;
516 s.state.surface = sc->getHandle();
517 mComposerStates[sc] = s;
Mathias Agopian698c0872011-06-28 19:09:31 -0700518 }
519
chaviw8e3fe5d2018-02-22 10:55:42 -0800520 return &(mComposerStates[sc].state);
Mathias Agopian698c0872011-06-28 19:09:31 -0700521}
522
Marissa Wallc837b5e2018-10-12 10:04:44 -0700523void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback(
524 const sp<SurfaceControl>& sc) {
Marissa Wall80d94ad2019-01-18 16:04:36 -0800525 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()];
526 callbackInfo.surfaceControls.insert(sc);
527
528 TransactionCompletedListener::getInstance()
529 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds);
Marissa Wallc837b5e2018-10-12 10:04:44 -0700530}
531
Robert Carr4cdc58f2017-08-23 14:22:20 -0700532SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
533 const sp<SurfaceControl>& sc, float x, float y) {
chaviw763ef572018-02-22 16:04:57 -0800534 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700535 if (!s) {
536 mStatus = BAD_INDEX;
537 return *this;
538 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700539 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700540 s->x = x;
541 s->y = y;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700542
543 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700544 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700545}
546
Robert Carr4cdc58f2017-08-23 14:22:20 -0700547SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
548 const sp<SurfaceControl>& sc) {
549 return setFlags(sc, 0, layer_state_t::eLayerHidden);
550}
551
552SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
553 const sp<SurfaceControl>& sc) {
554 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
555}
556
557SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
558 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
chaviw763ef572018-02-22 16:04:57 -0800559 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700560 if (!s) {
561 mStatus = BAD_INDEX;
562 return *this;
563 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700564 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700565 s->w = w;
566 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700567
Marissa Wallc837b5e2018-10-12 10:04:44 -0700568 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700569 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700570}
571
Robert Carr4cdc58f2017-08-23 14:22:20 -0700572SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
573 const sp<SurfaceControl>& sc, int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800574 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700575 if (!s) {
576 mStatus = BAD_INDEX;
577 return *this;
578 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700579 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700580 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700581
582 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700583 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700584}
585
Robert Carr4cdc58f2017-08-23 14:22:20 -0700586SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
Robert Carrdb66e622017-04-10 16:55:57 -0700587 int32_t z) {
chaviw763ef572018-02-22 16:04:57 -0800588 layer_state_t* s = getLayerState(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700589 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700590 mStatus = BAD_INDEX;
Robert Carrdb66e622017-04-10 16:55:57 -0700591 }
592 s->what |= layer_state_t::eRelativeLayerChanged;
593 s->relativeLayerHandle = relativeTo;
594 s->z = z;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700595
596 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700597 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700598}
599
Robert Carr4cdc58f2017-08-23 14:22:20 -0700600SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
601 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700602 uint32_t mask) {
chaviw763ef572018-02-22 16:04:57 -0800603 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700604 if (!s) {
605 mStatus = BAD_INDEX;
606 return *this;
607 }
Pablo Ceballos53390e12015-08-04 11:25:59 -0700608 if ((mask & layer_state_t::eLayerOpaque) ||
609 (mask & layer_state_t::eLayerHidden) ||
610 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700611 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800612 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700613 s->flags &= ~mask;
614 s->flags |= (flags & mask);
615 s->mask |= mask;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700616
617 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700618 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700619}
620
Robert Carr4cdc58f2017-08-23 14:22:20 -0700621SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
622 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700623 const Region& transparentRegion) {
chaviw763ef572018-02-22 16:04:57 -0800624 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700625 if (!s) {
626 mStatus = BAD_INDEX;
627 return *this;
628 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700629 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700630 s->transparentRegion = transparentRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700631
632 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700633 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700634}
635
Robert Carr4cdc58f2017-08-23 14:22:20 -0700636SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
637 const sp<SurfaceControl>& sc, float alpha) {
chaviw763ef572018-02-22 16:04:57 -0800638 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700639 if (!s) {
640 mStatus = BAD_INDEX;
641 return *this;
642 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700643 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700644 s->alpha = alpha;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700645
646 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700647 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700648}
649
Robert Carr4cdc58f2017-08-23 14:22:20 -0700650SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
651 const sp<SurfaceControl>& sc, uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -0800652 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700653 if (!s) {
654 mStatus = BAD_INDEX;
655 return *this;
656 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700657 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700658 s->layerStack = layerStack;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700659
660 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700661 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700662}
663
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800664SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
665 const sp<SurfaceControl>& sc, uint32_t key, std::vector<uint8_t> data) {
666 layer_state_t* s = getLayerState(sc);
667 if (!s) {
668 mStatus = BAD_INDEX;
669 return *this;
670 }
671 s->what |= layer_state_t::eMetadataChanged;
672 s->metadata.mMap[key] = std::move(data);
673
674 registerSurfaceControlForCallback(sc);
675 return *this;
676}
677
Robert Carr4cdc58f2017-08-23 14:22:20 -0700678SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
679 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800680 float dtdy, float dsdy) {
chaviw763ef572018-02-22 16:04:57 -0800681 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700682 if (!s) {
683 mStatus = BAD_INDEX;
684 return *this;
685 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700686 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700687 layer_state_t::matrix22_t matrix;
688 matrix.dsdx = dsdx;
689 matrix.dtdx = dtdx;
690 matrix.dsdy = dsdy;
691 matrix.dtdy = dtdy;
692 s->matrix = matrix;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700693
694 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700695 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700696}
697
Marissa Wallf58c14b2018-07-24 10:50:43 -0700698SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy(
Robert Carr4cdc58f2017-08-23 14:22:20 -0700699 const sp<SurfaceControl>& sc, const Rect& crop) {
chaviw763ef572018-02-22 16:04:57 -0800700 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700701 if (!s) {
702 mStatus = BAD_INDEX;
703 return *this;
704 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700705 s->what |= layer_state_t::eCropChanged_legacy;
706 s->crop_legacy = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700707
708 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700709 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700710}
711
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700712SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius(
713 const sp<SurfaceControl>& sc, float cornerRadius) {
714 layer_state_t* s = getLayerState(sc);
715 if (!s) {
716 mStatus = BAD_INDEX;
717 return *this;
718 }
719 s->what |= layer_state_t::eCornerRadiusChanged;
720 s->cornerRadius = cornerRadius;
721 return *this;
722}
723
Marissa Wallf58c14b2018-07-24 10:50:43 -0700724SurfaceComposerClient::Transaction&
725SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
726 const sp<IBinder>& handle,
727 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800728 layer_state_t* s = getLayerState(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700729 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700730 mStatus = BAD_INDEX;
731 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700732 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700733 s->what |= layer_state_t::eDeferTransaction_legacy;
734 s->barrierHandle_legacy = handle;
735 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700736
737 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700738 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800739}
740
Marissa Wallf58c14b2018-07-24 10:50:43 -0700741SurfaceComposerClient::Transaction&
742SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc,
743 const sp<Surface>& barrierSurface,
744 uint64_t frameNumber) {
chaviw763ef572018-02-22 16:04:57 -0800745 layer_state_t* s = getLayerState(sc);
Robert Carr0d480722017-01-10 16:42:54 -0800746 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700747 mStatus = BAD_INDEX;
748 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800749 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700750 s->what |= layer_state_t::eDeferTransaction_legacy;
751 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer();
752 s->frameNumber_legacy = frameNumber;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700753
754 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700755 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700756}
757
Robert Carr4cdc58f2017-08-23 14:22:20 -0700758SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
759 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -0800760 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800761 layer_state_t* s = getLayerState(sc);
Robert Carr1db73f62016-12-21 12:58:51 -0800762 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700763 mStatus = BAD_INDEX;
764 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800765 }
766 s->what |= layer_state_t::eReparentChildren;
767 s->reparentHandle = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700768
769 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700770 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800771}
772
Robert Carr4cdc58f2017-08-23 14:22:20 -0700773SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
774 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -0700775 const sp<IBinder>& newParentHandle) {
chaviw763ef572018-02-22 16:04:57 -0800776 layer_state_t* s = getLayerState(sc);
chaviw06178942017-07-27 10:25:59 -0700777 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700778 mStatus = BAD_INDEX;
779 return *this;
chaviw06178942017-07-27 10:25:59 -0700780 }
chaviwf1961f72017-09-18 16:41:07 -0700781 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700782 s->parentHandleForChild = newParentHandle;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700783
784 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700785 return *this;
chaviw06178942017-07-27 10:25:59 -0700786}
787
Robert Carr4cdc58f2017-08-23 14:22:20 -0700788SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
789 const sp<SurfaceControl>& sc,
790 const half3& color) {
chaviw763ef572018-02-22 16:04:57 -0800791 layer_state_t* s = getLayerState(sc);
Robert Carr9524cb32017-02-13 11:32:32 -0800792 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700793 mStatus = BAD_INDEX;
794 return *this;
795 }
796 s->what |= layer_state_t::eColorChanged;
797 s->color = color;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700798
799 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700800 return *this;
801}
802
Valerie Haudd0b7572019-01-29 14:59:27 -0800803SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor(
804 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) {
Valerie Haued54efa2019-01-11 20:03:14 -0800805 layer_state_t* s = getLayerState(sc);
806 if (!s) {
807 mStatus = BAD_INDEX;
808 return *this;
809 }
810
Valerie Haudd0b7572019-01-29 14:59:27 -0800811 s->what |= layer_state_t::eBackgroundColorChanged;
812 s->color = color;
813 s->bgColorAlpha = alpha;
814 s->bgColorDataspace = dataspace;
Valerie Haued54efa2019-01-11 20:03:14 -0800815
816 registerSurfaceControlForCallback(sc);
817 return *this;
818}
819
Marissa Wall61c58622018-07-18 10:12:20 -0700820SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform(
821 const sp<SurfaceControl>& sc, uint32_t transform) {
822 layer_state_t* s = getLayerState(sc);
823 if (!s) {
824 mStatus = BAD_INDEX;
825 return *this;
826 }
827 s->what |= layer_state_t::eTransformChanged;
828 s->transform = transform;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700829
830 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700831 return *this;
832}
833
834SurfaceComposerClient::Transaction&
835SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc,
836 bool transformToDisplayInverse) {
837 layer_state_t* s = getLayerState(sc);
838 if (!s) {
839 mStatus = BAD_INDEX;
840 return *this;
841 }
842 s->what |= layer_state_t::eTransformToDisplayInverseChanged;
843 s->transformToDisplayInverse = transformToDisplayInverse;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700844
845 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700846 return *this;
847}
848
849SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
850 const sp<SurfaceControl>& sc, const Rect& crop) {
851 layer_state_t* s = getLayerState(sc);
852 if (!s) {
853 mStatus = BAD_INDEX;
854 return *this;
855 }
856 s->what |= layer_state_t::eCropChanged;
857 s->crop = crop;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700858
859 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700860 return *this;
861}
862
Marissa Wall861616d2018-10-22 12:52:23 -0700863SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame(
864 const sp<SurfaceControl>& sc, const Rect& frame) {
865 layer_state_t* s = getLayerState(sc);
866 if (!s) {
867 mStatus = BAD_INDEX;
868 return *this;
869 }
870 s->what |= layer_state_t::eFrameChanged;
871 s->frame = frame;
872
873 registerSurfaceControlForCallback(sc);
874 return *this;
875}
876
Marissa Wall61c58622018-07-18 10:12:20 -0700877SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer(
878 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) {
879 layer_state_t* s = getLayerState(sc);
880 if (!s) {
881 mStatus = BAD_INDEX;
882 return *this;
883 }
Marissa Wallc837b5e2018-10-12 10:04:44 -0700884
Marissa Wall73411622019-01-25 10:45:41 -0800885 int32_t bufferId = BufferCache::getInstance().getId(buffer);
886 if (bufferId < 0) {
887 bufferId = BufferCache::getInstance().cache(buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700888
Marissa Wall73411622019-01-25 10:45:41 -0800889 s->what |= layer_state_t::eBufferChanged;
890 s->buffer = buffer;
Marissa Wallebc2c052019-01-16 19:16:55 -0800891 }
Marissa Wall73411622019-01-25 10:45:41 -0800892
Marissa Wallebc2c052019-01-16 19:16:55 -0800893 s->what |= layer_state_t::eCachedBufferChanged;
Marissa Wall73411622019-01-25 10:45:41 -0800894 s->cachedBuffer.token = BufferCache::getInstance().getToken();
Marissa Wallebc2c052019-01-16 19:16:55 -0800895 s->cachedBuffer.bufferId = bufferId;
896
897 registerSurfaceControlForCallback(sc);
898 return *this;
899}
900
Marissa Wall61c58622018-07-18 10:12:20 -0700901SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence(
902 const sp<SurfaceControl>& sc, const sp<Fence>& fence) {
903 layer_state_t* s = getLayerState(sc);
904 if (!s) {
905 mStatus = BAD_INDEX;
906 return *this;
907 }
908 s->what |= layer_state_t::eAcquireFenceChanged;
909 s->acquireFence = fence;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700910
911 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700912 return *this;
913}
914
915SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace(
916 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) {
917 layer_state_t* s = getLayerState(sc);
918 if (!s) {
919 mStatus = BAD_INDEX;
920 return *this;
921 }
922 s->what |= layer_state_t::eDataspaceChanged;
923 s->dataspace = dataspace;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700924
925 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700926 return *this;
927}
928
929SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata(
930 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) {
931 layer_state_t* s = getLayerState(sc);
932 if (!s) {
933 mStatus = BAD_INDEX;
934 return *this;
935 }
936 s->what |= layer_state_t::eHdrMetadataChanged;
937 s->hdrMetadata = hdrMetadata;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700938
939 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700940 return *this;
941}
942
943SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion(
944 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) {
945 layer_state_t* s = getLayerState(sc);
946 if (!s) {
947 mStatus = BAD_INDEX;
948 return *this;
949 }
950 s->what |= layer_state_t::eSurfaceDamageRegionChanged;
951 s->surfaceDamageRegion = surfaceDamageRegion;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700952
953 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700954 return *this;
955}
956
957SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi(
958 const sp<SurfaceControl>& sc, int32_t api) {
959 layer_state_t* s = getLayerState(sc);
960 if (!s) {
961 mStatus = BAD_INDEX;
962 return *this;
963 }
964 s->what |= layer_state_t::eApiChanged;
965 s->api = api;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700966
967 registerSurfaceControlForCallback(sc);
Marissa Wall61c58622018-07-18 10:12:20 -0700968 return *this;
969}
970
971SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream(
972 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) {
973 layer_state_t* s = getLayerState(sc);
974 if (!s) {
975 mStatus = BAD_INDEX;
976 return *this;
977 }
978 s->what |= layer_state_t::eSidebandStreamChanged;
979 s->sidebandStream = sidebandStream;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700980
981 registerSurfaceControlForCallback(sc);
982 return *this;
983}
984
Marissa Wall17b4e452018-12-26 16:32:34 -0800985SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime(
986 nsecs_t desiredPresentTime) {
987 mDesiredPresentTime = desiredPresentTime;
988 return *this;
989}
990
Marissa Wallc837b5e2018-10-12 10:04:44 -0700991SurfaceComposerClient::Transaction&
992SurfaceComposerClient::Transaction::addTransactionCompletedCallback(
Marissa Walle2ffb422018-10-12 11:33:52 -0700993 TransactionCompletedCallbackTakesContext callback, void* callbackContext) {
Marissa Wallc837b5e2018-10-12 10:04:44 -0700994 auto listener = TransactionCompletedListener::getInstance();
995
Marissa Wall80d94ad2019-01-18 16:04:36 -0800996 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1,
997 std::placeholders::_2, std::placeholders::_3);
998 const auto& surfaceControls =
999 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001000
Marissa Wall80d94ad2019-01-18 16:04:36 -08001001 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001002
1003 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace(
1004 callbackId);
Marissa Wall61c58622018-07-18 10:12:20 -07001005 return *this;
1006}
1007
Robert Carr4cdc58f2017-08-23 14:22:20 -07001008SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
1009 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001010 layer_state_t* s = getLayerState(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001011 if (!s) {
1012 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -08001013 }
1014 s->what |= layer_state_t::eDetachChildren;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001015
1016 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001017 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -08001018}
1019
Robert Carr4cdc58f2017-08-23 14:22:20 -07001020SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
1021 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
chaviw763ef572018-02-22 16:04:57 -08001022 layer_state_t* s = getLayerState(sc);
Robert Carrc3574f72016-03-24 12:19:32 -07001023 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001024 mStatus = BAD_INDEX;
1025 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001026 }
1027
1028 switch (overrideScalingMode) {
1029 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1030 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1031 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1032 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1033 case -1:
1034 break;
1035 default:
1036 ALOGE("unknown scaling mode: %d",
1037 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001038 mStatus = BAD_VALUE;
1039 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001040 }
1041
1042 s->what |= layer_state_t::eOverrideScalingModeChanged;
1043 s->overrideScalingMode = overrideScalingMode;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001044
1045 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001046 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -07001047}
1048
Robert Carr4cdc58f2017-08-23 14:22:20 -07001049SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
1050 const sp<SurfaceControl>& sc) {
chaviw763ef572018-02-22 16:04:57 -08001051 layer_state_t* s = getLayerState(sc);
Robert Carr82364e32016-05-15 11:27:47 -07001052 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001053 mStatus = BAD_INDEX;
1054 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001055 }
Robert Carr99e27f02016-06-16 15:18:02 -07001056 s->what |= layer_state_t::eGeometryAppliesWithResize;
Marissa Wallc837b5e2018-10-12 10:04:44 -07001057
1058 registerSurfaceControlForCallback(sc);
Robert Carr4cdc58f2017-08-23 14:22:20 -07001059 return *this;
Robert Carr82364e32016-05-15 11:27:47 -07001060}
1061
Robert Carr2c358bf2018-08-08 15:58:15 -07001062#ifndef NO_INPUT
1063SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1064 const sp<SurfaceControl>& sc,
1065 const InputWindowInfo& info) {
1066 layer_state_t* s = getLayerState(sc);
1067 if (!s) {
1068 mStatus = BAD_INDEX;
1069 return *this;
1070 }
1071 s->inputInfo = info;
1072 s->what |= layer_state_t::eInputInfoChanged;
1073 return *this;
1074}
chaviw273171b2018-12-26 11:46:30 -08001075
1076SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus(
1077 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
1078 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand;
1079 transferTouchFocusCommand.fromToken = fromToken;
1080 transferTouchFocusCommand.toToken = toToken;
1081 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand);
1082 return *this;
1083}
1084
Robert Carr2c358bf2018-08-08 15:58:15 -07001085#endif
1086
Peiyong Lind3788632018-09-18 16:01:31 -07001087SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform(
1088 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) {
1089 layer_state_t* s = getLayerState(sc);
1090 if (!s) {
1091 mStatus = BAD_INDEX;
1092 return *this;
1093 }
1094 s->what |= layer_state_t::eColorTransformChanged;
1095 s->colorTransform = mat4(matrix, translation);
Marissa Wallc837b5e2018-10-12 10:04:44 -07001096
1097 registerSurfaceControlForCallback(sc);
Peiyong Lind3788632018-09-18 16:01:31 -07001098 return *this;
1099}
1100
Robert Carrfb4d58b2019-01-15 09:21:27 -08001101SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry(
1102 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) {
1103 setCrop_legacy(sc, source);
1104
1105 int x = dst.left;
1106 int y = dst.top;
1107 float xScale = dst.getWidth() / static_cast<float>(source.getWidth());
1108 float yScale = dst.getHeight() / static_cast<float>(source.getHeight());
1109 float matrix[4] = {1, 0, 0, 1};
1110
1111 switch (transform) {
1112 case NATIVE_WINDOW_TRANSFORM_FLIP_H:
1113 matrix[0] = -xScale; matrix[1] = 0;
1114 matrix[2] = 0; matrix[3] = yScale;
1115 x += source.getWidth();
1116 break;
1117 case NATIVE_WINDOW_TRANSFORM_FLIP_V:
1118 matrix[0] = xScale; matrix[1] = 0;
1119 matrix[2] = 0; matrix[3] = -yScale;
1120 y += source.getHeight();
1121 break;
1122 case NATIVE_WINDOW_TRANSFORM_ROT_90:
1123 matrix[0] = 0; matrix[1] = -yScale;
1124 matrix[2] = xScale; matrix[3] = 0;
1125 x += source.getHeight();
1126 break;
1127 case NATIVE_WINDOW_TRANSFORM_ROT_180:
1128 matrix[0] = -xScale; matrix[1] = 0;
1129 matrix[2] = 0; matrix[3] = -yScale;
1130 x += source.getWidth();
1131 y += source.getHeight();
1132 break;
1133 case NATIVE_WINDOW_TRANSFORM_ROT_270:
1134 matrix[0] = 0; matrix[1] = yScale;
1135 matrix[2] = -xScale; matrix[3] = 0;
1136 y += source.getWidth();
1137 break;
1138 default:
1139 matrix[0] = xScale; matrix[1] = 0;
1140 matrix[2] = 0; matrix[3] = yScale;
1141 break;
1142 }
1143 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]);
1144 setPosition(sc, x, y);
1145
1146 return *this;
1147}
1148
Mathias Agopian698c0872011-06-28 19:09:31 -07001149// ---------------------------------------------------------------------------
1150
chaviw763ef572018-02-22 16:04:57 -08001151DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001152 DisplayState s;
1153 s.token = token;
1154 ssize_t index = mDisplayStates.indexOf(s);
1155 if (index < 0) {
1156 // we don't have it, add an initialized layer_state to our list
1157 s.what = 0;
1158 index = mDisplayStates.add(s);
1159 }
Dan Stozad723bd72014-11-18 10:24:03 -08001160 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001161}
1162
Robert Carr4cdc58f2017-08-23 14:22:20 -07001163status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
1164 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -07001165 if (bufferProducer.get() != nullptr) {
1166 // Make sure that composition can never be stalled by a virtual display
1167 // consumer that isn't processing buffers fast enough.
1168 status_t err = bufferProducer->setAsyncMode(true);
1169 if (err != NO_ERROR) {
1170 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
1171 "BufferQueue. This BufferQueue cannot be used for virtual "
1172 "display. (%d)", err);
1173 return err;
1174 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001175 }
chaviw763ef572018-02-22 16:04:57 -08001176 DisplayState& s(getDisplayState(token));
Andy McFadden2adaf042012-12-18 09:49:45 -08001177 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001178 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -07001179 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001180}
1181
Robert Carr4cdc58f2017-08-23 14:22:20 -07001182void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -07001183 uint32_t layerStack) {
chaviw763ef572018-02-22 16:04:57 -08001184 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001185 s.layerStack = layerStack;
1186 s.what |= DisplayState::eLayerStackChanged;
1187}
1188
Robert Carr4cdc58f2017-08-23 14:22:20 -07001189void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001190 uint32_t orientation,
1191 const Rect& layerStackRect,
1192 const Rect& displayRect) {
chaviw763ef572018-02-22 16:04:57 -08001193 DisplayState& s(getDisplayState(token));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001194 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001195 s.viewport = layerStackRect;
1196 s.frame = displayRect;
1197 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +00001198 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001199}
1200
Robert Carr4cdc58f2017-08-23 14:22:20 -07001201void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
chaviw763ef572018-02-22 16:04:57 -08001202 DisplayState& s(getDisplayState(token));
Michael Wright1f6078a2014-06-26 16:01:02 -07001203 s.width = width;
1204 s.height = height;
1205 s.what |= DisplayState::eDisplaySizeChanged;
1206}
1207
Mathias Agopiane57f2922012-08-09 16:29:12 -07001208// ---------------------------------------------------------------------------
1209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -07001211 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213}
1214
Jorim Jaggif3cf4bc2017-11-30 14:19:23 +01001215SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client)
1216 : mStatus(NO_ERROR), mClient(client)
1217{
1218}
1219
Mathias Agopian698c0872011-06-28 19:09:31 -07001220void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001221 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001222 if (sf != nullptr && mStatus == NO_INIT) {
Robert Carr1db73f62016-12-21 12:58:51 -08001223 sp<ISurfaceComposerClient> conn;
Robert Carrb89ea9d2018-12-10 13:01:14 -08001224 conn = sf->createConnection();
Yi Kong48a619f2018-06-05 16:34:59 -07001225 if (conn != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001226 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001227 mStatus = NO_ERROR;
1228 }
1229 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230}
1231
Mathias Agopian698c0872011-06-28 19:09:31 -07001232SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -07001233 dispose();
1234}
Mathias Agopiandd3423c2009-09-23 15:44:05 -07001235
Mathias Agopian698c0872011-06-28 19:09:31 -07001236status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 return mStatus;
1238}
1239
Mathias Agopian698c0872011-06-28 19:09:31 -07001240sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001241 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242}
1243
Mathias Agopiand4784a32010-05-27 19:41:15 -07001244status_t SurfaceComposerClient::linkToComposerDeath(
1245 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -07001246 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001247 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1248 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249}
1250
Mathias Agopian698c0872011-06-28 19:09:31 -07001251void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -07001253 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -07001254 Mutex::Autolock _lm(mLock);
Yi Kong48a619f2018-06-05 16:34:59 -07001255 if (mClient != nullptr) {
Mathias Agopiand4784a32010-05-27 19:41:15 -07001256 client = mClient; // hold ref while lock is held
1257 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 }
Mathias Agopiand4784a32010-05-27 19:41:15 -07001259 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260}
1261
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001262sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
1263 PixelFormat format, uint32_t flags,
1264 SurfaceControl* parent,
1265 LayerMetadata metadata) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001266 sp<SurfaceControl> s;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001267 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
Robert Carr3b382ed2018-03-14 13:49:41 -07001268 return s;
1269}
1270
Marissa Wall35187b32019-01-08 10:08:52 -08001271sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
1272 uint32_t h, PixelFormat format,
1273 uint32_t flags, Surface* parent,
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001274 LayerMetadata metadata) {
Marissa Wall35187b32019-01-08 10:08:52 -08001275 sp<SurfaceControl> sur;
1276 status_t err = mStatus;
1277
1278 if (mStatus == NO_ERROR) {
1279 sp<IBinder> handle;
1280 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
1281 sp<IGraphicBufferProducer> gbp;
1282
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001283 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
1284 std::move(metadata), &handle, &gbp);
Marissa Wall35187b32019-01-08 10:08:52 -08001285 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
1286 if (err == NO_ERROR) {
1287 return new SurfaceControl(this, handle, gbp, true /* owned */);
1288 }
1289 }
1290 return nullptr;
1291}
1292
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001293status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
1294 PixelFormat format,
1295 sp<SurfaceControl>* outSurface, uint32_t flags,
1296 SurfaceControl* parent,
1297 LayerMetadata metadata) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001298 sp<SurfaceControl> sur;
Robert Carr740eaf02018-03-27 12:59:18 -07001299 status_t err = mStatus;
Robert Carr3b382ed2018-03-14 13:49:41 -07001300
Mathias Agopian698c0872011-06-28 19:09:31 -07001301 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001302 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001303 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001304 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001305
1306 if (parent != nullptr) {
1307 parentHandle = parent->getHandle();
1308 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001309
1310 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
1311 &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001312 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
1313 if (err == NO_ERROR) {
Robert Carr3b382ed2018-03-14 13:49:41 -07001314 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Mathias Agopian698c0872011-06-28 19:09:31 -07001315 }
1316 }
Robert Carr3b382ed2018-03-14 13:49:41 -07001317 return err;
Mathias Agopian698c0872011-06-28 19:09:31 -07001318}
1319
Svetoslavd85084b2014-03-20 10:28:31 -07001320status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
1321 if (mStatus != NO_ERROR) {
1322 return mStatus;
1323 }
1324 return mClient->clearLayerFrameStats(token);
1325}
1326
1327status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
1328 FrameStats* outStats) const {
1329 if (mStatus != NO_ERROR) {
1330 return mStatus;
1331 }
1332 return mClient->getLayerFrameStats(token, outStats);
1333}
1334
Mathias Agopian698c0872011-06-28 19:09:31 -07001335// ----------------------------------------------------------------------------
1336
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001337status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001338 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1339 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001340}
1341
1342status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -07001343 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
1344 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -07001345}
1346
Dan Stoza7f7da322014-05-02 15:26:25 -07001347status_t SurfaceComposerClient::getDisplayConfigs(
1348 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349{
Dan Stoza7f7da322014-05-02 15:26:25 -07001350 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
1351}
1352
1353status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
1354 DisplayInfo* info) {
1355 Vector<DisplayInfo> configs;
1356 status_t result = getDisplayConfigs(display, &configs);
1357 if (result != NO_ERROR) {
1358 return result;
1359 }
1360
1361 int activeId = getActiveConfig(display);
1362 if (activeId < 0) {
1363 ALOGE("No active configuration found");
1364 return NAME_NOT_FOUND;
1365 }
1366
Dan Stozad723bd72014-11-18 10:24:03 -08001367 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -07001368 return NO_ERROR;
1369}
1370
1371int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
1372 return ComposerService::getComposerService()->getActiveConfig(display);
1373}
1374
1375status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
1376 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377}
1378
Michael Wright28f24d02016-07-12 13:30:53 -07001379status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001380 Vector<ColorMode>* outColorModes) {
Michael Wright28f24d02016-07-12 13:30:53 -07001381 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
1382}
1383
Daniel Solomon42d04562019-01-20 21:03:19 -08001384status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display,
1385 ui::DisplayPrimaries& outPrimaries) {
1386 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries);
1387}
1388
Peiyong Lina52f0292018-03-14 17:26:31 -07001389ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
Michael Wright28f24d02016-07-12 13:30:53 -07001390 return ComposerService::getComposerService()->getActiveColorMode(display);
1391}
1392
1393status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
Peiyong Lina52f0292018-03-14 17:26:31 -07001394 ColorMode colorMode) {
Michael Wright28f24d02016-07-12 13:30:53 -07001395 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
1396}
1397
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001398void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
1399 int mode) {
1400 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -07001401}
1402
Peiyong Linc6780972018-10-28 15:24:08 -07001403status_t SurfaceComposerClient::getCompositionPreference(
1404 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat,
1405 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) {
1406 return ComposerService::getComposerService()
1407 ->getCompositionPreference(defaultDataspace, defaultPixelFormat,
1408 wideColorGamutDataspace, wideColorGamutPixelFormat);
Peiyong Lin0256f722018-08-31 15:45:10 -07001409}
1410
Peiyong Lin08d10512019-01-16 13:27:35 -08001411bool SurfaceComposerClient::getProtectedContentSupport() {
1412 bool supported = false;
1413 ComposerService::getComposerService()->getProtectedContentSupport(&supported);
1414 return supported;
1415}
1416
Svetoslavd85084b2014-03-20 10:28:31 -07001417status_t SurfaceComposerClient::clearAnimationFrameStats() {
1418 return ComposerService::getComposerService()->clearAnimationFrameStats();
1419}
1420
1421status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
1422 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
1423}
1424
Dan Stozac4f471e2016-03-24 09:31:08 -07001425status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
1426 HdrCapabilities* outCapabilities) {
1427 return ComposerService::getComposerService()->getHdrCapabilities(display,
1428 outCapabilities);
1429}
1430
Kevin DuBois9c0a1762018-10-16 13:32:31 -07001431status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display,
1432 ui::PixelFormat* outFormat,
1433 ui::Dataspace* outDataspace,
1434 uint8_t* outComponentMask) {
1435 return ComposerService::getComposerService()
1436 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace,
1437 outComponentMask);
1438}
1439
Kevin DuBois74e53772018-11-19 10:52:38 -08001440status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display,
1441 bool enable, uint8_t componentMask,
1442 uint64_t maxFrames) {
1443 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable,
1444 componentMask,
1445 maxFrames);
1446}
1447
Kevin DuBois1d4249a2018-08-29 10:45:14 -07001448status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display,
1449 uint64_t maxFrames, uint64_t timestamp,
1450 DisplayedFrameStats* outStats) {
1451 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames,
1452 timestamp, outStats);
1453}
Marissa Wall35187b32019-01-08 10:08:52 -08001454
Peiyong Lin4f3fddf2019-01-24 17:21:24 -08001455status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display,
1456 bool* outIsWideColorDisplay) {
1457 return ComposerService::getComposerService()->isWideColorDisplay(display,
1458 outIsWideColorDisplay);
1459}
1460
Mathias Agopian698c0872011-06-28 19:09:31 -07001461// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
Peiyong Lin0e003c92018-09-17 11:09:51 -07001463status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace,
1464 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
1465 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
1466 uint32_t rotation, sp<GraphicBuffer>* outBuffer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08001467 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001468 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001469 status_t ret = s->captureScreen(display, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop,
1470 reqWidth, reqHeight, useIdentityTransform,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001471 static_cast<ISurfaceComposer::Rotation>(rotation));
Robert Carr673134e2017-01-09 19:48:38 -08001472 if (ret != NO_ERROR) {
1473 return ret;
1474 }
Robert Carr673134e2017-01-09 19:48:38 -08001475 return ret;
1476}
1477
Peiyong Lin0e003c92018-09-17 11:09:51 -07001478status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
1479 const ui::Dataspace reqDataSpace,
1480 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Chavi Weingarten40482ff2017-11-30 01:51:40 +00001481 float frameScale, sp<GraphicBuffer>* outBuffer) {
chaviwa76b2712017-09-20 12:02:26 -07001482 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001483 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001484 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1485 sourceCrop, frameScale, false /* childrenOnly */);
Robert Carr578038f2018-03-09 12:25:24 -08001486 return ret;
1487}
1488
Peiyong Lin0e003c92018-09-17 11:09:51 -07001489status_t ScreenshotClient::captureChildLayers(const sp<IBinder>& layerHandle,
1490 const ui::Dataspace reqDataSpace,
1491 const ui::PixelFormat reqPixelFormat, Rect sourceCrop,
Robert Carr578038f2018-03-09 12:25:24 -08001492 float frameScale, sp<GraphicBuffer>* outBuffer) {
1493 sp<ISurfaceComposer> s(ComposerService::getComposerService());
Yi Kong48a619f2018-06-05 16:34:59 -07001494 if (s == nullptr) return NO_INIT;
Peiyong Lin0e003c92018-09-17 11:09:51 -07001495 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat,
1496 sourceCrop, frameScale, true /* childrenOnly */);
chaviw7206d492017-11-10 16:16:12 -08001497 return ret;
chaviwa76b2712017-09-20 12:02:26 -07001498}
Mathias Agopian74c40c02010-09-29 13:02:36 -07001499// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500}; // namespace android