1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "SurfaceComposerClient" 18 19 #include <stdint.h> 20 #include <sys/types.h> 21 22 #include <utils/Errors.h> 23 #include <utils/Log.h> 24 #include <utils/SortedVector.h> 25 #include <utils/String8.h> 26 #include <utils/threads.h> 27 28 #include <binder/IPCThreadState.h> 29 #include <binder/IServiceManager.h> 30 #include <binder/ProcessState.h> 31 32 #include <system/graphics.h> 33 34 #include <ui/DisplayInfo.h> 35 36 #include <gui/BufferItemConsumer.h> 37 #include <gui/CpuConsumer.h> 38 #include <gui/IGraphicBufferProducer.h> 39 #include <gui/ISurfaceComposer.h> 40 #include <gui/ISurfaceComposerClient.h> 41 #include <gui/LayerState.h> 42 #include <gui/Surface.h> 43 #include <gui/SurfaceComposerClient.h> 44 45 #ifndef NO_INPUT 46 #include <input/InputWindow.h> 47 #endif 48 49 #include <private/gui/ComposerService.h> 50 51 // This server size should always be smaller than the server cache size 52 #define BUFFER_CACHE_MAX_SIZE 64 53 54 namespace android { 55 56 using ui::ColorMode; 57 // --------------------------------------------------------------------------- 58 59 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService); 60 61 ComposerService::ComposerService() 62 : Singleton<ComposerService>() { 63 Mutex::Autolock _l(mLock); 64 connectLocked(); 65 } 66 67 void ComposerService::connectLocked() { 68 const String16 name("SurfaceFlinger"); 69 while (getService(name, &mComposerService) != NO_ERROR) { 70 usleep(250000); 71 } 72 assert(mComposerService != nullptr); 73 74 // Create the death listener. 75 class DeathObserver : public IBinder::DeathRecipient { 76 ComposerService& mComposerService; 77 virtual void binderDied(const wp<IBinder>& who) { 78 ALOGW("ComposerService remote (surfaceflinger) died [%p]", 79 who.unsafe_get()); 80 mComposerService.composerServiceDied(); 81 } 82 public: 83 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { } 84 }; 85 86 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this)); 87 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver); 88 } 89 90 /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() { 91 ComposerService& instance = ComposerService::getInstance(); 92 Mutex::Autolock _l(instance.mLock); 93 if (instance.mComposerService == nullptr) { 94 ComposerService::getInstance().connectLocked(); 95 assert(instance.mComposerService != nullptr); 96 ALOGD("ComposerService reconnected"); 97 } 98 return instance.mComposerService; 99 } 100 101 void ComposerService::composerServiceDied() 102 { 103 Mutex::Autolock _l(mLock); 104 mComposerService = nullptr; 105 mDeathObserver = nullptr; 106 } 107 108 class DefaultComposerClient: public Singleton<DefaultComposerClient> { 109 Mutex mLock; 110 sp<SurfaceComposerClient> mClient; 111 friend class Singleton<ComposerService>; 112 public: 113 static sp<SurfaceComposerClient> getComposerClient() { 114 DefaultComposerClient& dc = DefaultComposerClient::getInstance(); 115 Mutex::Autolock _l(dc.mLock); 116 if (dc.mClient == nullptr) { 117 dc.mClient = new SurfaceComposerClient; 118 } 119 return dc.mClient; 120 } 121 }; 122 ANDROID_SINGLETON_STATIC_INSTANCE(DefaultComposerClient); 123 124 125 sp<SurfaceComposerClient> SurfaceComposerClient::getDefault() { 126 return DefaultComposerClient::getComposerClient(); 127 } 128 129 // --------------------------------------------------------------------------- 130 131 // TransactionCompletedListener does not use ANDROID_SINGLETON_STATIC_INSTANCE because it needs 132 // to be able to return a sp<> to its instance to pass to SurfaceFlinger. 133 // ANDROID_SINGLETON_STATIC_INSTANCE only allows a reference to an instance. 134 135 // 0 is an invalid callback id 136 TransactionCompletedListener::TransactionCompletedListener() : mCallbackIdCounter(1) {} 137 138 CallbackId TransactionCompletedListener::getNextIdLocked() { 139 return mCallbackIdCounter++; 140 } 141 142 sp<TransactionCompletedListener> TransactionCompletedListener::getInstance() { 143 static sp<TransactionCompletedListener> sInstance = new TransactionCompletedListener; 144 return sInstance; 145 } 146 147 sp<ITransactionCompletedListener> TransactionCompletedListener::getIInstance() { 148 return static_cast<sp<ITransactionCompletedListener>>(getInstance()); 149 } 150 151 void TransactionCompletedListener::startListeningLocked() { 152 if (mListening) { 153 return; 154 } 155 ProcessState::self()->startThreadPool(); 156 mListening = true; 157 } 158 159 CallbackId TransactionCompletedListener::addCallbackFunction( 160 const TransactionCompletedCallback& callbackFunction, 161 const std::unordered_set<sp<SurfaceControl>, SurfaceComposerClient::SCHash>& 162 surfaceControls) { 163 std::lock_guard<std::mutex> lock(mMutex); 164 startListeningLocked(); 165 166 CallbackId callbackId = getNextIdLocked(); 167 mCallbacks[callbackId].callbackFunction = callbackFunction; 168 169 auto& callbackSurfaceControls = mCallbacks[callbackId].surfaceControls; 170 171 for (const auto& surfaceControl : surfaceControls) { 172 callbackSurfaceControls[surfaceControl->getHandle()] = surfaceControl; 173 } 174 175 return callbackId; 176 } 177 178 void TransactionCompletedListener::addSurfaceControlToCallbacks( 179 const sp<SurfaceControl>& surfaceControl, 180 const std::unordered_set<CallbackId>& callbackIds) { 181 std::lock_guard<std::mutex> lock(mMutex); 182 183 for (auto callbackId : callbackIds) { 184 mCallbacks[callbackId].surfaceControls.emplace(std::piecewise_construct, 185 std::forward_as_tuple( 186 surfaceControl->getHandle()), 187 std::forward_as_tuple(surfaceControl)); 188 } 189 } 190 191 void TransactionCompletedListener::onTransactionCompleted(ListenerStats listenerStats) { 192 std::lock_guard<std::mutex> lock(mMutex); 193 194 /* This listener knows all the sp<IBinder> to sp<SurfaceControl> for all its registered 195 * callbackIds, except for when Transactions are merged together. This probably cannot be 196 * solved before this point because the Transactions could be merged together and applied in a 197 * different process. 198 * 199 * Fortunately, we get all the callbacks for this listener for the same frame together at the 200 * same time. This means if any Transactions were merged together, we will get their callbacks 201 * at the same time. We can combine all the sp<IBinder> to sp<SurfaceControl> maps for all the 202 * callbackIds to generate one super map that contains all the sp<IBinder> to sp<SurfaceControl> 203 * that could possibly exist for the callbacks. 204 */ 205 std::unordered_map<sp<IBinder>, sp<SurfaceControl>, IBinderHash> surfaceControls; 206 for (const auto& transactionStats : listenerStats.transactionStats) { 207 for (auto callbackId : transactionStats.callbackIds) { 208 auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId]; 209 surfaceControls.insert(callbackSurfaceControls.begin(), callbackSurfaceControls.end()); 210 } 211 } 212 213 for (const auto& transactionStats : listenerStats.transactionStats) { 214 for (auto callbackId : transactionStats.callbackIds) { 215 auto& [callbackFunction, callbackSurfaceControls] = mCallbacks[callbackId]; 216 if (!callbackFunction) { 217 ALOGE("cannot call null callback function, skipping"); 218 continue; 219 } 220 std::vector<SurfaceControlStats> surfaceControlStats; 221 for (const auto& surfaceStats : transactionStats.surfaceStats) { 222 surfaceControlStats.emplace_back(surfaceControls[surfaceStats.surfaceControl], 223 surfaceStats.acquireTime, 224 surfaceStats.previousReleaseFence); 225 } 226 227 callbackFunction(transactionStats.latchTime, transactionStats.presentFence, 228 surfaceControlStats); 229 mCallbacks.erase(callbackId); 230 } 231 } 232 } 233 234 // --------------------------------------------------------------------------- 235 236 void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId); 237 238 class BufferCache : public Singleton<BufferCache> { 239 public: 240 BufferCache() : token(new BBinder()) {} 241 242 sp<IBinder> getToken() { 243 return IInterface::asBinder(TransactionCompletedListener::getIInstance()); 244 } 245 246 status_t getCacheId(const sp<GraphicBuffer>& buffer, uint64_t* cacheId) { 247 std::lock_guard<std::mutex> lock(mMutex); 248 249 auto itr = mBuffers.find(buffer->getId()); 250 if (itr == mBuffers.end()) { 251 return BAD_VALUE; 252 } 253 itr->second = getCounter(); 254 *cacheId = buffer->getId(); 255 return NO_ERROR; 256 } 257 258 uint64_t cache(const sp<GraphicBuffer>& buffer) { 259 std::lock_guard<std::mutex> lock(mMutex); 260 261 if (mBuffers.size() >= BUFFER_CACHE_MAX_SIZE) { 262 evictLeastRecentlyUsedBuffer(); 263 } 264 265 buffer->addDeathCallback(bufferCacheCallback, nullptr); 266 267 mBuffers[buffer->getId()] = getCounter(); 268 return buffer->getId(); 269 } 270 271 void uncache(uint64_t cacheId) { 272 std::lock_guard<std::mutex> lock(mMutex); 273 uncacheLocked(cacheId); 274 } 275 276 void uncacheLocked(uint64_t cacheId) REQUIRES(mMutex) { 277 mBuffers.erase(cacheId); 278 SurfaceComposerClient::doUncacheBufferTransaction(cacheId); 279 } 280 281 private: 282 void evictLeastRecentlyUsedBuffer() REQUIRES(mMutex) { 283 auto itr = mBuffers.begin(); 284 uint64_t minCounter = itr->second; 285 auto minBuffer = itr; 286 itr++; 287 288 while (itr != mBuffers.end()) { 289 uint64_t counter = itr->second; 290 if (counter < minCounter) { 291 minCounter = counter; 292 minBuffer = itr; 293 } 294 itr++; 295 } 296 uncacheLocked(minBuffer->first); 297 } 298 299 uint64_t getCounter() REQUIRES(mMutex) { 300 static uint64_t counter = 0; 301 return counter++; 302 } 303 304 std::mutex mMutex; 305 std::map<uint64_t /*Cache id*/, uint64_t /*counter*/> mBuffers GUARDED_BY(mMutex); 306 307 // Used by ISurfaceComposer to identify which process is sending the cached buffer. 308 sp<IBinder> token; 309 }; 310 311 ANDROID_SINGLETON_STATIC_INSTANCE(BufferCache); 312 313 void bufferCacheCallback(void* /*context*/, uint64_t graphicBufferId) { 314 // GraphicBuffer id's are used as the cache ids. 315 BufferCache::getInstance().uncache(graphicBufferId); 316 } 317 318 // --------------------------------------------------------------------------- 319 320 SurfaceComposerClient::Transaction::Transaction(const Transaction& other) 321 : mForceSynchronous(other.mForceSynchronous), 322 mTransactionNestCount(other.mTransactionNestCount), 323 mAnimation(other.mAnimation), 324 mEarlyWakeup(other.mEarlyWakeup), 325 mDesiredPresentTime(other.mDesiredPresentTime) { 326 mDisplayStates = other.mDisplayStates; 327 mComposerStates = other.mComposerStates; 328 mInputWindowCommands = other.mInputWindowCommands; 329 } 330 331 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) { 332 for (auto const& kv : other.mComposerStates) { 333 if (mComposerStates.count(kv.first) == 0) { 334 mComposerStates[kv.first] = kv.second; 335 } else { 336 mComposerStates[kv.first].state.merge(kv.second.state); 337 } 338 } 339 other.mComposerStates.clear(); 340 341 for (auto const& state : other.mDisplayStates) { 342 ssize_t index = mDisplayStates.indexOf(state); 343 if (index < 0) { 344 mDisplayStates.add(state); 345 } else { 346 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state); 347 } 348 } 349 other.mDisplayStates.clear(); 350 351 for (const auto& [listener, callbackInfo] : other.mListenerCallbacks) { 352 auto& [callbackIds, surfaceControls] = callbackInfo; 353 mListenerCallbacks[listener].callbackIds.insert(std::make_move_iterator( 354 callbackIds.begin()), 355 std::make_move_iterator(callbackIds.end())); 356 mListenerCallbacks[listener] 357 .surfaceControls.insert(std::make_move_iterator(surfaceControls.begin()), 358 std::make_move_iterator(surfaceControls.end())); 359 } 360 other.mListenerCallbacks.clear(); 361 362 mInputWindowCommands.merge(other.mInputWindowCommands); 363 other.mInputWindowCommands.clear(); 364 365 mContainsBuffer = other.mContainsBuffer; 366 other.mContainsBuffer = false; 367 368 mEarlyWakeup = mEarlyWakeup || other.mEarlyWakeup; 369 other.mEarlyWakeup = false; 370 371 return *this; 372 } 373 374 void SurfaceComposerClient::doDropReferenceTransaction(const sp<IBinder>& handle, 375 const sp<ISurfaceComposerClient>& client) { 376 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 377 Vector<ComposerState> composerStates; 378 Vector<DisplayState> displayStates; 379 380 ComposerState s; 381 s.client = client; 382 s.state.surface = handle; 383 s.state.what |= layer_state_t::eReparent; 384 s.state.parentHandleForChild = nullptr; 385 386 composerStates.add(s); 387 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); 388 sf->setTransactionState(composerStates, displayStates, 0, applyToken, {}, -1, {}, {}); 389 } 390 391 void SurfaceComposerClient::doUncacheBufferTransaction(uint64_t cacheId) { 392 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 393 394 client_cache_t uncacheBuffer; 395 uncacheBuffer.token = BufferCache::getInstance().getToken(); 396 uncacheBuffer.id = cacheId; 397 398 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); 399 sf->setTransactionState({}, {}, 0, applyToken, {}, -1, uncacheBuffer, {}); 400 } 401 402 void SurfaceComposerClient::Transaction::cacheBuffers() { 403 if (!mContainsBuffer) { 404 return; 405 } 406 407 size_t count = 0; 408 for (auto& [sc, cs] : mComposerStates) { 409 layer_state_t* s = getLayerState(sc); 410 if (!(s->what & layer_state_t::eBufferChanged)) { 411 continue; 412 } 413 414 // Don't try to cache a null buffer. Sending null buffers is cheap so we shouldn't waste 415 // time trying to cache them. 416 if (!s->buffer) { 417 continue; 418 } 419 420 uint64_t cacheId = 0; 421 status_t ret = BufferCache::getInstance().getCacheId(s->buffer, &cacheId); 422 if (ret == NO_ERROR) { 423 s->what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged); 424 s->buffer = nullptr; 425 } else { 426 cacheId = BufferCache::getInstance().cache(s->buffer); 427 } 428 s->what |= layer_state_t::eCachedBufferChanged; 429 s->cachedBuffer.token = BufferCache::getInstance().getToken(); 430 s->cachedBuffer.id = cacheId; 431 432 // If we have more buffers than the size of the cache, we should stop caching so we don't 433 // evict other buffers in this transaction 434 count++; 435 if (count >= BUFFER_CACHE_MAX_SIZE) { 436 break; 437 } 438 } 439 } 440 441 status_t SurfaceComposerClient::Transaction::apply(bool synchronous) { 442 if (mStatus != NO_ERROR) { 443 return mStatus; 444 } 445 446 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 447 448 std::vector<ListenerCallbacks> listenerCallbacks; 449 450 // For every listener with registered callbacks 451 for (const auto& [listener, callbackInfo] : mListenerCallbacks) { 452 auto& [callbackIds, surfaceControls] = callbackInfo; 453 if (callbackIds.empty()) { 454 continue; 455 } 456 457 listenerCallbacks.emplace_back(listener, std::move(callbackIds)); 458 459 // If the listener has any SurfaceControls set on this Transaction update the surface state 460 for (const auto& surfaceControl : surfaceControls) { 461 layer_state_t* s = getLayerState(surfaceControl); 462 if (!s) { 463 ALOGE("failed to get layer state"); 464 continue; 465 } 466 s->what |= layer_state_t::eHasListenerCallbacksChanged; 467 s->hasListenerCallbacks = true; 468 } 469 } 470 mListenerCallbacks.clear(); 471 472 cacheBuffers(); 473 474 Vector<ComposerState> composerStates; 475 Vector<DisplayState> displayStates; 476 uint32_t flags = 0; 477 478 mForceSynchronous |= synchronous; 479 480 for (auto const& kv : mComposerStates){ 481 composerStates.add(kv.second); 482 } 483 484 mComposerStates.clear(); 485 486 displayStates = mDisplayStates; 487 mDisplayStates.clear(); 488 489 if (mForceSynchronous) { 490 flags |= ISurfaceComposer::eSynchronous; 491 } 492 if (mAnimation) { 493 flags |= ISurfaceComposer::eAnimation; 494 } 495 if (mEarlyWakeup) { 496 flags |= ISurfaceComposer::eEarlyWakeup; 497 } 498 499 mForceSynchronous = false; 500 mAnimation = false; 501 mEarlyWakeup = false; 502 503 sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); 504 sf->setTransactionState(composerStates, displayStates, flags, applyToken, mInputWindowCommands, 505 mDesiredPresentTime, 506 {} /*uncacheBuffer - only set in doUncacheBufferTransaction*/, 507 listenerCallbacks); 508 mInputWindowCommands.clear(); 509 mStatus = NO_ERROR; 510 return NO_ERROR; 511 } 512 513 // --------------------------------------------------------------------------- 514 515 sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) { 516 return ComposerService::getComposerService()->createDisplay(displayName, 517 secure); 518 } 519 520 void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) { 521 return ComposerService::getComposerService()->destroyDisplay(display); 522 } 523 524 std::vector<PhysicalDisplayId> SurfaceComposerClient::getPhysicalDisplayIds() { 525 return ComposerService::getComposerService()->getPhysicalDisplayIds(); 526 } 527 528 std::optional<PhysicalDisplayId> SurfaceComposerClient::getInternalDisplayId() { 529 return ComposerService::getComposerService()->getInternalDisplayId(); 530 } 531 532 sp<IBinder> SurfaceComposerClient::getPhysicalDisplayToken(PhysicalDisplayId displayId) { 533 return ComposerService::getComposerService()->getPhysicalDisplayToken(displayId); 534 } 535 536 sp<IBinder> SurfaceComposerClient::getInternalDisplayToken() { 537 return ComposerService::getComposerService()->getInternalDisplayToken(); 538 } 539 540 void SurfaceComposerClient::Transaction::setAnimationTransaction() { 541 mAnimation = true; 542 } 543 544 void SurfaceComposerClient::Transaction::setEarlyWakeup() { 545 mEarlyWakeup = true; 546 } 547 548 layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) { 549 if (mComposerStates.count(sc) == 0) { 550 // we don't have it, add an initialized layer_state to our list 551 ComposerState s; 552 s.client = sc->getClient()->mClient; 553 s.state.surface = sc->getHandle(); 554 mComposerStates[sc] = s; 555 } 556 557 return &(mComposerStates[sc].state); 558 } 559 560 void SurfaceComposerClient::Transaction::registerSurfaceControlForCallback( 561 const sp<SurfaceControl>& sc) { 562 auto& callbackInfo = mListenerCallbacks[TransactionCompletedListener::getIInstance()]; 563 callbackInfo.surfaceControls.insert(sc); 564 565 TransactionCompletedListener::getInstance() 566 ->addSurfaceControlToCallbacks(sc, callbackInfo.callbackIds); 567 } 568 569 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition( 570 const sp<SurfaceControl>& sc, float x, float y) { 571 layer_state_t* s = getLayerState(sc); 572 if (!s) { 573 mStatus = BAD_INDEX; 574 return *this; 575 } 576 s->what |= layer_state_t::ePositionChanged; 577 s->x = x; 578 s->y = y; 579 580 registerSurfaceControlForCallback(sc); 581 return *this; 582 } 583 584 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show( 585 const sp<SurfaceControl>& sc) { 586 return setFlags(sc, 0, layer_state_t::eLayerHidden); 587 } 588 589 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide( 590 const sp<SurfaceControl>& sc) { 591 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden); 592 } 593 594 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize( 595 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) { 596 layer_state_t* s = getLayerState(sc); 597 if (!s) { 598 mStatus = BAD_INDEX; 599 return *this; 600 } 601 s->what |= layer_state_t::eSizeChanged; 602 s->w = w; 603 s->h = h; 604 605 registerSurfaceControlForCallback(sc); 606 return *this; 607 } 608 609 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer( 610 const sp<SurfaceControl>& sc, int32_t z) { 611 layer_state_t* s = getLayerState(sc); 612 if (!s) { 613 mStatus = BAD_INDEX; 614 return *this; 615 } 616 s->what |= layer_state_t::eLayerChanged; 617 s->what &= ~layer_state_t::eRelativeLayerChanged; 618 s->z = z; 619 620 registerSurfaceControlForCallback(sc); 621 return *this; 622 } 623 624 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo, 625 int32_t z) { 626 layer_state_t* s = getLayerState(sc); 627 if (!s) { 628 mStatus = BAD_INDEX; 629 } 630 s->what |= layer_state_t::eRelativeLayerChanged; 631 s->what &= ~layer_state_t::eLayerChanged; 632 s->relativeLayerHandle = relativeTo; 633 s->z = z; 634 635 registerSurfaceControlForCallback(sc); 636 return *this; 637 } 638 639 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags( 640 const sp<SurfaceControl>& sc, uint32_t flags, 641 uint32_t mask) { 642 layer_state_t* s = getLayerState(sc); 643 if (!s) { 644 mStatus = BAD_INDEX; 645 return *this; 646 } 647 if ((mask & layer_state_t::eLayerOpaque) || 648 (mask & layer_state_t::eLayerHidden) || 649 (mask & layer_state_t::eLayerSecure)) { 650 s->what |= layer_state_t::eFlagsChanged; 651 } 652 s->flags &= ~mask; 653 s->flags |= (flags & mask); 654 s->mask |= mask; 655 656 registerSurfaceControlForCallback(sc); 657 return *this; 658 } 659 660 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint( 661 const sp<SurfaceControl>& sc, 662 const Region& transparentRegion) { 663 layer_state_t* s = getLayerState(sc); 664 if (!s) { 665 mStatus = BAD_INDEX; 666 return *this; 667 } 668 s->what |= layer_state_t::eTransparentRegionChanged; 669 s->transparentRegion = transparentRegion; 670 671 registerSurfaceControlForCallback(sc); 672 return *this; 673 } 674 675 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha( 676 const sp<SurfaceControl>& sc, float alpha) { 677 layer_state_t* s = getLayerState(sc); 678 if (!s) { 679 mStatus = BAD_INDEX; 680 return *this; 681 } 682 s->what |= layer_state_t::eAlphaChanged; 683 s->alpha = alpha; 684 685 registerSurfaceControlForCallback(sc); 686 return *this; 687 } 688 689 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack( 690 const sp<SurfaceControl>& sc, uint32_t layerStack) { 691 layer_state_t* s = getLayerState(sc); 692 if (!s) { 693 mStatus = BAD_INDEX; 694 return *this; 695 } 696 s->what |= layer_state_t::eLayerStackChanged; 697 s->layerStack = layerStack; 698 699 registerSurfaceControlForCallback(sc); 700 return *this; 701 } 702 703 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata( 704 const sp<SurfaceControl>& sc, uint32_t key, std::vector<uint8_t> data) { 705 layer_state_t* s = getLayerState(sc); 706 if (!s) { 707 mStatus = BAD_INDEX; 708 return *this; 709 } 710 s->what |= layer_state_t::eMetadataChanged; 711 s->metadata.mMap[key] = std::move(data); 712 713 registerSurfaceControlForCallback(sc); 714 return *this; 715 } 716 717 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix( 718 const sp<SurfaceControl>& sc, float dsdx, float dtdx, 719 float dtdy, float dsdy) { 720 layer_state_t* s = getLayerState(sc); 721 if (!s) { 722 mStatus = BAD_INDEX; 723 return *this; 724 } 725 s->what |= layer_state_t::eMatrixChanged; 726 layer_state_t::matrix22_t matrix; 727 matrix.dsdx = dsdx; 728 matrix.dtdx = dtdx; 729 matrix.dsdy = dsdy; 730 matrix.dtdy = dtdy; 731 s->matrix = matrix; 732 733 registerSurfaceControlForCallback(sc); 734 return *this; 735 } 736 737 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop_legacy( 738 const sp<SurfaceControl>& sc, const Rect& crop) { 739 layer_state_t* s = getLayerState(sc); 740 if (!s) { 741 mStatus = BAD_INDEX; 742 return *this; 743 } 744 s->what |= layer_state_t::eCropChanged_legacy; 745 s->crop_legacy = crop; 746 747 registerSurfaceControlForCallback(sc); 748 return *this; 749 } 750 751 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCornerRadius( 752 const sp<SurfaceControl>& sc, float cornerRadius) { 753 layer_state_t* s = getLayerState(sc); 754 if (!s) { 755 mStatus = BAD_INDEX; 756 return *this; 757 } 758 s->what |= layer_state_t::eCornerRadiusChanged; 759 s->cornerRadius = cornerRadius; 760 return *this; 761 } 762 763 SurfaceComposerClient::Transaction& 764 SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc, 765 const sp<IBinder>& handle, 766 uint64_t frameNumber) { 767 layer_state_t* s = getLayerState(sc); 768 if (!s) { 769 mStatus = BAD_INDEX; 770 return *this; 771 } 772 s->what |= layer_state_t::eDeferTransaction_legacy; 773 s->barrierHandle_legacy = handle; 774 s->frameNumber_legacy = frameNumber; 775 776 registerSurfaceControlForCallback(sc); 777 return *this; 778 } 779 780 SurfaceComposerClient::Transaction& 781 SurfaceComposerClient::Transaction::deferTransactionUntil_legacy(const sp<SurfaceControl>& sc, 782 const sp<Surface>& barrierSurface, 783 uint64_t frameNumber) { 784 layer_state_t* s = getLayerState(sc); 785 if (!s) { 786 mStatus = BAD_INDEX; 787 return *this; 788 } 789 s->what |= layer_state_t::eDeferTransaction_legacy; 790 s->barrierGbp_legacy = barrierSurface->getIGraphicBufferProducer(); 791 s->frameNumber_legacy = frameNumber; 792 793 registerSurfaceControlForCallback(sc); 794 return *this; 795 } 796 797 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren( 798 const sp<SurfaceControl>& sc, 799 const sp<IBinder>& newParentHandle) { 800 layer_state_t* s = getLayerState(sc); 801 if (!s) { 802 mStatus = BAD_INDEX; 803 return *this; 804 } 805 s->what |= layer_state_t::eReparentChildren; 806 s->reparentHandle = newParentHandle; 807 808 registerSurfaceControlForCallback(sc); 809 return *this; 810 } 811 812 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent( 813 const sp<SurfaceControl>& sc, 814 const sp<IBinder>& newParentHandle) { 815 layer_state_t* s = getLayerState(sc); 816 if (!s) { 817 mStatus = BAD_INDEX; 818 return *this; 819 } 820 s->what |= layer_state_t::eReparent; 821 s->parentHandleForChild = newParentHandle; 822 823 registerSurfaceControlForCallback(sc); 824 return *this; 825 } 826 827 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor( 828 const sp<SurfaceControl>& sc, 829 const half3& color) { 830 layer_state_t* s = getLayerState(sc); 831 if (!s) { 832 mStatus = BAD_INDEX; 833 return *this; 834 } 835 s->what |= layer_state_t::eColorChanged; 836 s->color = color; 837 838 registerSurfaceControlForCallback(sc); 839 return *this; 840 } 841 842 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBackgroundColor( 843 const sp<SurfaceControl>& sc, const half3& color, float alpha, ui::Dataspace dataspace) { 844 layer_state_t* s = getLayerState(sc); 845 if (!s) { 846 mStatus = BAD_INDEX; 847 return *this; 848 } 849 850 s->what |= layer_state_t::eBackgroundColorChanged; 851 s->color = color; 852 s->bgColorAlpha = alpha; 853 s->bgColorDataspace = dataspace; 854 855 registerSurfaceControlForCallback(sc); 856 return *this; 857 } 858 859 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransform( 860 const sp<SurfaceControl>& sc, uint32_t transform) { 861 layer_state_t* s = getLayerState(sc); 862 if (!s) { 863 mStatus = BAD_INDEX; 864 return *this; 865 } 866 s->what |= layer_state_t::eTransformChanged; 867 s->transform = transform; 868 869 registerSurfaceControlForCallback(sc); 870 return *this; 871 } 872 873 SurfaceComposerClient::Transaction& 874 SurfaceComposerClient::Transaction::setTransformToDisplayInverse(const sp<SurfaceControl>& sc, 875 bool transformToDisplayInverse) { 876 layer_state_t* s = getLayerState(sc); 877 if (!s) { 878 mStatus = BAD_INDEX; 879 return *this; 880 } 881 s->what |= layer_state_t::eTransformToDisplayInverseChanged; 882 s->transformToDisplayInverse = transformToDisplayInverse; 883 884 registerSurfaceControlForCallback(sc); 885 return *this; 886 } 887 888 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop( 889 const sp<SurfaceControl>& sc, const Rect& crop) { 890 layer_state_t* s = getLayerState(sc); 891 if (!s) { 892 mStatus = BAD_INDEX; 893 return *this; 894 } 895 s->what |= layer_state_t::eCropChanged; 896 s->crop = crop; 897 898 registerSurfaceControlForCallback(sc); 899 return *this; 900 } 901 902 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFrame( 903 const sp<SurfaceControl>& sc, const Rect& frame) { 904 layer_state_t* s = getLayerState(sc); 905 if (!s) { 906 mStatus = BAD_INDEX; 907 return *this; 908 } 909 s->what |= layer_state_t::eFrameChanged; 910 s->frame = frame; 911 912 registerSurfaceControlForCallback(sc); 913 return *this; 914 } 915 916 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setBuffer( 917 const sp<SurfaceControl>& sc, const sp<GraphicBuffer>& buffer) { 918 layer_state_t* s = getLayerState(sc); 919 if (!s) { 920 mStatus = BAD_INDEX; 921 return *this; 922 } 923 s->what |= layer_state_t::eBufferChanged; 924 s->buffer = buffer; 925 926 registerSurfaceControlForCallback(sc); 927 928 mContainsBuffer = true; 929 return *this; 930 } 931 932 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAcquireFence( 933 const sp<SurfaceControl>& sc, const sp<Fence>& fence) { 934 layer_state_t* s = getLayerState(sc); 935 if (!s) { 936 mStatus = BAD_INDEX; 937 return *this; 938 } 939 s->what |= layer_state_t::eAcquireFenceChanged; 940 s->acquireFence = fence; 941 942 registerSurfaceControlForCallback(sc); 943 return *this; 944 } 945 946 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDataspace( 947 const sp<SurfaceControl>& sc, ui::Dataspace dataspace) { 948 layer_state_t* s = getLayerState(sc); 949 if (!s) { 950 mStatus = BAD_INDEX; 951 return *this; 952 } 953 s->what |= layer_state_t::eDataspaceChanged; 954 s->dataspace = dataspace; 955 956 registerSurfaceControlForCallback(sc); 957 return *this; 958 } 959 960 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setHdrMetadata( 961 const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata) { 962 layer_state_t* s = getLayerState(sc); 963 if (!s) { 964 mStatus = BAD_INDEX; 965 return *this; 966 } 967 s->what |= layer_state_t::eHdrMetadataChanged; 968 s->hdrMetadata = hdrMetadata; 969 970 registerSurfaceControlForCallback(sc); 971 return *this; 972 } 973 974 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSurfaceDamageRegion( 975 const sp<SurfaceControl>& sc, const Region& surfaceDamageRegion) { 976 layer_state_t* s = getLayerState(sc); 977 if (!s) { 978 mStatus = BAD_INDEX; 979 return *this; 980 } 981 s->what |= layer_state_t::eSurfaceDamageRegionChanged; 982 s->surfaceDamageRegion = surfaceDamageRegion; 983 984 registerSurfaceControlForCallback(sc); 985 return *this; 986 } 987 988 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setApi( 989 const sp<SurfaceControl>& sc, int32_t api) { 990 layer_state_t* s = getLayerState(sc); 991 if (!s) { 992 mStatus = BAD_INDEX; 993 return *this; 994 } 995 s->what |= layer_state_t::eApiChanged; 996 s->api = api; 997 998 registerSurfaceControlForCallback(sc); 999 return *this; 1000 } 1001 1002 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSidebandStream( 1003 const sp<SurfaceControl>& sc, const sp<NativeHandle>& sidebandStream) { 1004 layer_state_t* s = getLayerState(sc); 1005 if (!s) { 1006 mStatus = BAD_INDEX; 1007 return *this; 1008 } 1009 s->what |= layer_state_t::eSidebandStreamChanged; 1010 s->sidebandStream = sidebandStream; 1011 1012 registerSurfaceControlForCallback(sc); 1013 return *this; 1014 } 1015 1016 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredPresentTime( 1017 nsecs_t desiredPresentTime) { 1018 mDesiredPresentTime = desiredPresentTime; 1019 return *this; 1020 } 1021 1022 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorSpaceAgnostic( 1023 const sp<SurfaceControl>& sc, const bool agnostic) { 1024 layer_state_t* s = getLayerState(sc); 1025 if (!s) { 1026 mStatus = BAD_INDEX; 1027 return *this; 1028 } 1029 s->what |= layer_state_t::eColorSpaceAgnosticChanged; 1030 s->colorSpaceAgnostic = agnostic; 1031 1032 registerSurfaceControlForCallback(sc); 1033 return *this; 1034 } 1035 1036 SurfaceComposerClient::Transaction& 1037 SurfaceComposerClient::Transaction::addTransactionCompletedCallback( 1038 TransactionCompletedCallbackTakesContext callback, void* callbackContext) { 1039 auto listener = TransactionCompletedListener::getInstance(); 1040 1041 auto callbackWithContext = std::bind(callback, callbackContext, std::placeholders::_1, 1042 std::placeholders::_2, std::placeholders::_3); 1043 const auto& surfaceControls = 1044 mListenerCallbacks[TransactionCompletedListener::getIInstance()].surfaceControls; 1045 1046 CallbackId callbackId = listener->addCallbackFunction(callbackWithContext, surfaceControls); 1047 1048 mListenerCallbacks[TransactionCompletedListener::getIInstance()].callbackIds.emplace( 1049 callbackId); 1050 return *this; 1051 } 1052 1053 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren( 1054 const sp<SurfaceControl>& sc) { 1055 layer_state_t* s = getLayerState(sc); 1056 if (!s) { 1057 mStatus = BAD_INDEX; 1058 } 1059 s->what |= layer_state_t::eDetachChildren; 1060 1061 registerSurfaceControlForCallback(sc); 1062 return *this; 1063 } 1064 1065 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode( 1066 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) { 1067 layer_state_t* s = getLayerState(sc); 1068 if (!s) { 1069 mStatus = BAD_INDEX; 1070 return *this; 1071 } 1072 1073 switch (overrideScalingMode) { 1074 case NATIVE_WINDOW_SCALING_MODE_FREEZE: 1075 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: 1076 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: 1077 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: 1078 case -1: 1079 break; 1080 default: 1081 ALOGE("unknown scaling mode: %d", 1082 overrideScalingMode); 1083 mStatus = BAD_VALUE; 1084 return *this; 1085 } 1086 1087 s->what |= layer_state_t::eOverrideScalingModeChanged; 1088 s->overrideScalingMode = overrideScalingMode; 1089 1090 registerSurfaceControlForCallback(sc); 1091 return *this; 1092 } 1093 1094 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize( 1095 const sp<SurfaceControl>& sc) { 1096 layer_state_t* s = getLayerState(sc); 1097 if (!s) { 1098 mStatus = BAD_INDEX; 1099 return *this; 1100 } 1101 s->what |= layer_state_t::eGeometryAppliesWithResize; 1102 1103 registerSurfaceControlForCallback(sc); 1104 return *this; 1105 } 1106 1107 #ifndef NO_INPUT 1108 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo( 1109 const sp<SurfaceControl>& sc, 1110 const InputWindowInfo& info) { 1111 layer_state_t* s = getLayerState(sc); 1112 if (!s) { 1113 mStatus = BAD_INDEX; 1114 return *this; 1115 } 1116 s->inputInfo = info; 1117 s->what |= layer_state_t::eInputInfoChanged; 1118 return *this; 1119 } 1120 1121 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::transferTouchFocus( 1122 const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { 1123 InputWindowCommands::TransferTouchFocusCommand transferTouchFocusCommand; 1124 transferTouchFocusCommand.fromToken = fromToken; 1125 transferTouchFocusCommand.toToken = toToken; 1126 mInputWindowCommands.transferTouchFocusCommands.emplace_back(transferTouchFocusCommand); 1127 return *this; 1128 } 1129 1130 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::syncInputWindows() { 1131 mInputWindowCommands.syncInputWindows = true; 1132 return *this; 1133 } 1134 1135 #endif 1136 1137 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColorTransform( 1138 const sp<SurfaceControl>& sc, const mat3& matrix, const vec3& translation) { 1139 layer_state_t* s = getLayerState(sc); 1140 if (!s) { 1141 mStatus = BAD_INDEX; 1142 return *this; 1143 } 1144 s->what |= layer_state_t::eColorTransformChanged; 1145 s->colorTransform = mat4(matrix, translation); 1146 1147 registerSurfaceControlForCallback(sc); 1148 return *this; 1149 } 1150 1151 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometry( 1152 const sp<SurfaceControl>& sc, const Rect& source, const Rect& dst, int transform) { 1153 setCrop_legacy(sc, source); 1154 1155 int x = dst.left; 1156 int y = dst.top; 1157 1158 float sourceWidth = source.getWidth(); 1159 float sourceHeight = source.getHeight(); 1160 1161 float xScale = sourceWidth < 0 ? 1.0f : dst.getWidth() / sourceWidth; 1162 float yScale = sourceHeight < 0 ? 1.0f : dst.getHeight() / sourceHeight; 1163 float matrix[4] = {1, 0, 0, 1}; 1164 1165 switch (transform) { 1166 case NATIVE_WINDOW_TRANSFORM_FLIP_H: 1167 matrix[0] = -xScale; matrix[1] = 0; 1168 matrix[2] = 0; matrix[3] = yScale; 1169 x += source.getWidth(); 1170 break; 1171 case NATIVE_WINDOW_TRANSFORM_FLIP_V: 1172 matrix[0] = xScale; matrix[1] = 0; 1173 matrix[2] = 0; matrix[3] = -yScale; 1174 y += source.getHeight(); 1175 break; 1176 case NATIVE_WINDOW_TRANSFORM_ROT_90: 1177 matrix[0] = 0; matrix[1] = -yScale; 1178 matrix[2] = xScale; matrix[3] = 0; 1179 x += source.getHeight(); 1180 break; 1181 case NATIVE_WINDOW_TRANSFORM_ROT_180: 1182 matrix[0] = -xScale; matrix[1] = 0; 1183 matrix[2] = 0; matrix[3] = -yScale; 1184 x += source.getWidth(); 1185 y += source.getHeight(); 1186 break; 1187 case NATIVE_WINDOW_TRANSFORM_ROT_270: 1188 matrix[0] = 0; matrix[1] = yScale; 1189 matrix[2] = -xScale; matrix[3] = 0; 1190 y += source.getWidth(); 1191 break; 1192 default: 1193 matrix[0] = xScale; matrix[1] = 0; 1194 matrix[2] = 0; matrix[3] = yScale; 1195 break; 1196 } 1197 setMatrix(sc, matrix[0], matrix[1], matrix[2], matrix[3]); 1198 setPosition(sc, x, y); 1199 1200 return *this; 1201 } 1202 1203 // --------------------------------------------------------------------------- 1204 1205 DisplayState& SurfaceComposerClient::Transaction::getDisplayState(const sp<IBinder>& token) { 1206 DisplayState s; 1207 s.token = token; 1208 ssize_t index = mDisplayStates.indexOf(s); 1209 if (index < 0) { 1210 // we don't have it, add an initialized layer_state to our list 1211 s.what = 0; 1212 index = mDisplayStates.add(s); 1213 } 1214 return mDisplayStates.editItemAt(static_cast<size_t>(index)); 1215 } 1216 1217 status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token, 1218 const sp<IGraphicBufferProducer>& bufferProducer) { 1219 if (bufferProducer.get() != nullptr) { 1220 // Make sure that composition can never be stalled by a virtual display 1221 // consumer that isn't processing buffers fast enough. 1222 status_t err = bufferProducer->setAsyncMode(true); 1223 if (err != NO_ERROR) { 1224 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the " 1225 "BufferQueue. This BufferQueue cannot be used for virtual " 1226 "display. (%d)", err); 1227 return err; 1228 } 1229 } 1230 DisplayState& s(getDisplayState(token)); 1231 s.surface = bufferProducer; 1232 s.what |= DisplayState::eSurfaceChanged; 1233 return NO_ERROR; 1234 } 1235 1236 void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token, 1237 uint32_t layerStack) { 1238 DisplayState& s(getDisplayState(token)); 1239 s.layerStack = layerStack; 1240 s.what |= DisplayState::eLayerStackChanged; 1241 } 1242 1243 void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token, 1244 uint32_t orientation, 1245 const Rect& layerStackRect, 1246 const Rect& displayRect) { 1247 DisplayState& s(getDisplayState(token)); 1248 s.orientation = orientation; 1249 s.viewport = layerStackRect; 1250 s.frame = displayRect; 1251 s.what |= DisplayState::eDisplayProjectionChanged; 1252 mForceSynchronous = true; // TODO: do we actually still need this? 1253 } 1254 1255 void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) { 1256 DisplayState& s(getDisplayState(token)); 1257 s.width = width; 1258 s.height = height; 1259 s.what |= DisplayState::eDisplaySizeChanged; 1260 } 1261 1262 // --------------------------------------------------------------------------- 1263 1264 SurfaceComposerClient::SurfaceComposerClient() 1265 : mStatus(NO_INIT) 1266 { 1267 } 1268 1269 SurfaceComposerClient::SurfaceComposerClient(const sp<ISurfaceComposerClient>& client) 1270 : mStatus(NO_ERROR), mClient(client) 1271 { 1272 } 1273 1274 void SurfaceComposerClient::onFirstRef() { 1275 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 1276 if (sf != nullptr && mStatus == NO_INIT) { 1277 sp<ISurfaceComposerClient> conn; 1278 conn = sf->createConnection(); 1279 if (conn != nullptr) { 1280 mClient = conn; 1281 mStatus = NO_ERROR; 1282 } 1283 } 1284 } 1285 1286 SurfaceComposerClient::~SurfaceComposerClient() { 1287 dispose(); 1288 } 1289 1290 status_t SurfaceComposerClient::initCheck() const { 1291 return mStatus; 1292 } 1293 1294 sp<IBinder> SurfaceComposerClient::connection() const { 1295 return IInterface::asBinder(mClient); 1296 } 1297 1298 status_t SurfaceComposerClient::linkToComposerDeath( 1299 const sp<IBinder::DeathRecipient>& recipient, 1300 void* cookie, uint32_t flags) { 1301 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 1302 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags); 1303 } 1304 1305 void SurfaceComposerClient::dispose() { 1306 // this can be called more than once. 1307 sp<ISurfaceComposerClient> client; 1308 Mutex::Autolock _lm(mLock); 1309 if (mClient != nullptr) { 1310 client = mClient; // hold ref while lock is held 1311 mClient.clear(); 1312 } 1313 mStatus = NO_INIT; 1314 } 1315 1316 sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h, 1317 PixelFormat format, uint32_t flags, 1318 SurfaceControl* parent, 1319 LayerMetadata metadata) { 1320 sp<SurfaceControl> s; 1321 createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata)); 1322 return s; 1323 } 1324 1325 sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w, 1326 uint32_t h, PixelFormat format, 1327 uint32_t flags, Surface* parent, 1328 LayerMetadata metadata) { 1329 sp<SurfaceControl> sur; 1330 status_t err = mStatus; 1331 1332 if (mStatus == NO_ERROR) { 1333 sp<IBinder> handle; 1334 sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer(); 1335 sp<IGraphicBufferProducer> gbp; 1336 1337 err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp, 1338 std::move(metadata), &handle, &gbp); 1339 ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err)); 1340 if (err == NO_ERROR) { 1341 return new SurfaceControl(this, handle, gbp, true /* owned */); 1342 } 1343 } 1344 return nullptr; 1345 } 1346 1347 status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h, 1348 PixelFormat format, 1349 sp<SurfaceControl>* outSurface, uint32_t flags, 1350 SurfaceControl* parent, 1351 LayerMetadata metadata) { 1352 sp<SurfaceControl> sur; 1353 status_t err = mStatus; 1354 1355 if (mStatus == NO_ERROR) { 1356 sp<IBinder> handle; 1357 sp<IBinder> parentHandle; 1358 sp<IGraphicBufferProducer> gbp; 1359 1360 if (parent != nullptr) { 1361 parentHandle = parent->getHandle(); 1362 } 1363 1364 err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata), 1365 &handle, &gbp); 1366 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err)); 1367 if (err == NO_ERROR) { 1368 *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */); 1369 } 1370 } 1371 return err; 1372 } 1373 1374 status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const { 1375 if (mStatus != NO_ERROR) { 1376 return mStatus; 1377 } 1378 return mClient->clearLayerFrameStats(token); 1379 } 1380 1381 status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token, 1382 FrameStats* outStats) const { 1383 if (mStatus != NO_ERROR) { 1384 return mStatus; 1385 } 1386 return mClient->getLayerFrameStats(token, outStats); 1387 } 1388 1389 // ---------------------------------------------------------------------------- 1390 1391 status_t SurfaceComposerClient::enableVSyncInjections(bool enable) { 1392 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 1393 return sf->enableVSyncInjections(enable); 1394 } 1395 1396 status_t SurfaceComposerClient::injectVSync(nsecs_t when) { 1397 sp<ISurfaceComposer> sf(ComposerService::getComposerService()); 1398 return sf->injectVSync(when); 1399 } 1400 1401 status_t SurfaceComposerClient::getDisplayConfigs( 1402 const sp<IBinder>& display, Vector<DisplayInfo>* configs) 1403 { 1404 return ComposerService::getComposerService()->getDisplayConfigs(display, configs); 1405 } 1406 1407 status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display, 1408 DisplayInfo* info) { 1409 Vector<DisplayInfo> configs; 1410 status_t result = getDisplayConfigs(display, &configs); 1411 if (result != NO_ERROR) { 1412 return result; 1413 } 1414 1415 int activeId = getActiveConfig(display); 1416 if (activeId < 0) { 1417 ALOGE("No active configuration found"); 1418 return NAME_NOT_FOUND; 1419 } 1420 1421 *info = configs[static_cast<size_t>(activeId)]; 1422 return NO_ERROR; 1423 } 1424 1425 int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) { 1426 return ComposerService::getComposerService()->getActiveConfig(display); 1427 } 1428 1429 status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) { 1430 return ComposerService::getComposerService()->setActiveConfig(display, id); 1431 } 1432 1433 status_t SurfaceComposerClient::setAllowedDisplayConfigs( 1434 const sp<IBinder>& displayToken, const std::vector<int32_t>& allowedConfigs) { 1435 return ComposerService::getComposerService()->setAllowedDisplayConfigs(displayToken, 1436 allowedConfigs); 1437 } 1438 1439 status_t SurfaceComposerClient::getAllowedDisplayConfigs(const sp<IBinder>& displayToken, 1440 std::vector<int32_t>* outAllowedConfigs) { 1441 return ComposerService::getComposerService()->getAllowedDisplayConfigs(displayToken, 1442 outAllowedConfigs); 1443 } 1444 1445 status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display, 1446 Vector<ColorMode>* outColorModes) { 1447 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes); 1448 } 1449 1450 status_t SurfaceComposerClient::getDisplayNativePrimaries(const sp<IBinder>& display, 1451 ui::DisplayPrimaries& outPrimaries) { 1452 return ComposerService::getComposerService()->getDisplayNativePrimaries(display, outPrimaries); 1453 } 1454 1455 ColorMode SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) { 1456 return ComposerService::getComposerService()->getActiveColorMode(display); 1457 } 1458 1459 status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display, 1460 ColorMode colorMode) { 1461 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode); 1462 } 1463 1464 void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token, 1465 int mode) { 1466 ComposerService::getComposerService()->setPowerMode(token, mode); 1467 } 1468 1469 status_t SurfaceComposerClient::getCompositionPreference( 1470 ui::Dataspace* defaultDataspace, ui::PixelFormat* defaultPixelFormat, 1471 ui::Dataspace* wideColorGamutDataspace, ui::PixelFormat* wideColorGamutPixelFormat) { 1472 return ComposerService::getComposerService() 1473 ->getCompositionPreference(defaultDataspace, defaultPixelFormat, 1474 wideColorGamutDataspace, wideColorGamutPixelFormat); 1475 } 1476 1477 bool SurfaceComposerClient::getProtectedContentSupport() { 1478 bool supported = false; 1479 ComposerService::getComposerService()->getProtectedContentSupport(&supported); 1480 return supported; 1481 } 1482 1483 status_t SurfaceComposerClient::clearAnimationFrameStats() { 1484 return ComposerService::getComposerService()->clearAnimationFrameStats(); 1485 } 1486 1487 status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) { 1488 return ComposerService::getComposerService()->getAnimationFrameStats(outStats); 1489 } 1490 1491 status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display, 1492 HdrCapabilities* outCapabilities) { 1493 return ComposerService::getComposerService()->getHdrCapabilities(display, 1494 outCapabilities); 1495 } 1496 1497 status_t SurfaceComposerClient::getDisplayedContentSamplingAttributes(const sp<IBinder>& display, 1498 ui::PixelFormat* outFormat, 1499 ui::Dataspace* outDataspace, 1500 uint8_t* outComponentMask) { 1501 return ComposerService::getComposerService() 1502 ->getDisplayedContentSamplingAttributes(display, outFormat, outDataspace, 1503 outComponentMask); 1504 } 1505 1506 status_t SurfaceComposerClient::setDisplayContentSamplingEnabled(const sp<IBinder>& display, 1507 bool enable, uint8_t componentMask, 1508 uint64_t maxFrames) { 1509 return ComposerService::getComposerService()->setDisplayContentSamplingEnabled(display, enable, 1510 componentMask, 1511 maxFrames); 1512 } 1513 1514 status_t SurfaceComposerClient::getDisplayedContentSample(const sp<IBinder>& display, 1515 uint64_t maxFrames, uint64_t timestamp, 1516 DisplayedFrameStats* outStats) { 1517 return ComposerService::getComposerService()->getDisplayedContentSample(display, maxFrames, 1518 timestamp, outStats); 1519 } 1520 1521 status_t SurfaceComposerClient::isWideColorDisplay(const sp<IBinder>& display, 1522 bool* outIsWideColorDisplay) { 1523 return ComposerService::getComposerService()->isWideColorDisplay(display, 1524 outIsWideColorDisplay); 1525 } 1526 1527 status_t SurfaceComposerClient::addRegionSamplingListener( 1528 const Rect& samplingArea, const sp<IBinder>& stopLayerHandle, 1529 const sp<IRegionSamplingListener>& listener) { 1530 return ComposerService::getComposerService()->addRegionSamplingListener(samplingArea, 1531 stopLayerHandle, 1532 listener); 1533 } 1534 1535 status_t SurfaceComposerClient::removeRegionSamplingListener( 1536 const sp<IRegionSamplingListener>& listener) { 1537 return ComposerService::getComposerService()->removeRegionSamplingListener(listener); 1538 } 1539 1540 bool SurfaceComposerClient::getDisplayBrightnessSupport(const sp<IBinder>& displayToken) { 1541 bool support = false; 1542 ComposerService::getComposerService()->getDisplayBrightnessSupport(displayToken, &support); 1543 return support; 1544 } 1545 1546 status_t SurfaceComposerClient::setDisplayBrightness(const sp<IBinder>& displayToken, 1547 float brightness) { 1548 return ComposerService::getComposerService()->setDisplayBrightness(displayToken, brightness); 1549 } 1550 1551 status_t SurfaceComposerClient::notifyPowerHint(int32_t hintId) { 1552 return ComposerService::getComposerService()->notifyPowerHint(hintId); 1553 } 1554 1555 // ---------------------------------------------------------------------------- 1556 1557 status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace, 1558 const ui::PixelFormat reqPixelFormat, Rect sourceCrop, 1559 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform, 1560 uint32_t rotation, bool captureSecureLayers, 1561 sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) { 1562 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 1563 if (s == nullptr) return NO_INIT; 1564 status_t ret = 1565 s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace, 1566 reqPixelFormat, sourceCrop, reqWidth, reqHeight, useIdentityTransform, 1567 static_cast<ISurfaceComposer::Rotation>(rotation), 1568 captureSecureLayers); 1569 if (ret != NO_ERROR) { 1570 return ret; 1571 } 1572 return ret; 1573 } 1574 1575 status_t ScreenshotClient::capture(const sp<IBinder>& display, const ui::Dataspace reqDataSpace, 1576 const ui::PixelFormat reqPixelFormat, Rect sourceCrop, 1577 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform, 1578 uint32_t rotation, sp<GraphicBuffer>* outBuffer) { 1579 bool ignored; 1580 return capture(display, reqDataSpace, reqPixelFormat, sourceCrop, reqWidth, reqHeight, 1581 useIdentityTransform, rotation, false, outBuffer, ignored); 1582 } 1583 1584 status_t ScreenshotClient::capture(uint64_t displayOrLayerStack, ui::Dataspace* outDataspace, 1585 sp<GraphicBuffer>* outBuffer) { 1586 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 1587 if (s == nullptr) return NO_INIT; 1588 return s->captureScreen(displayOrLayerStack, outDataspace, outBuffer); 1589 } 1590 1591 status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle, 1592 const ui::Dataspace reqDataSpace, 1593 const ui::PixelFormat reqPixelFormat, Rect sourceCrop, 1594 float frameScale, sp<GraphicBuffer>* outBuffer) { 1595 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 1596 if (s == nullptr) return NO_INIT; 1597 status_t ret = s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, 1598 sourceCrop, {}, frameScale, false /* childrenOnly */); 1599 return ret; 1600 } 1601 1602 status_t ScreenshotClient::captureChildLayers( 1603 const sp<IBinder>& layerHandle, const ui::Dataspace reqDataSpace, 1604 const ui::PixelFormat reqPixelFormat, Rect sourceCrop, 1605 const std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>>& excludeHandles, 1606 float frameScale, sp<GraphicBuffer>* outBuffer) { 1607 sp<ISurfaceComposer> s(ComposerService::getComposerService()); 1608 if (s == nullptr) return NO_INIT; 1609 status_t ret = 1610 s->captureLayers(layerHandle, outBuffer, reqDataSpace, reqPixelFormat, sourceCrop, 1611 excludeHandles, frameScale, true /* childrenOnly */); 1612 return ret; 1613 } 1614 // ---------------------------------------------------------------------------- 1615 }; // namespace android 1616