1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <stdint.h> 20 #include <sys/types.h> 21 22 #include <android/gui/ISurfaceComposer.h> 23 24 #include <utils/Singleton.h> 25 #include <utils/StrongPointer.h> 26 27 namespace android { 28 29 // --------------------------------------------------------------------------- 30 31 // --------------------------------------------------------------------------- 32 33 // This holds our connection to the composer service (i.e. SurfaceFlinger). 34 // If the remote side goes away, we will re-establish the connection. 35 // Users of this class should not retain the value from 36 // getComposerService() for an extended period. 37 // 38 // (TODO: b/219785927, It's not clear that using Singleton is useful here anymore.) 39 class ComposerServiceAIDL : public Singleton<ComposerServiceAIDL> { 40 sp<gui::ISurfaceComposer> mComposerService; 41 sp<IBinder::DeathRecipient> mDeathObserver; 42 mutable std::mutex mMutex; 43 44 ComposerServiceAIDL(); 45 bool connectLocked(); 46 void composerServiceDied(); 47 friend class Singleton<ComposerServiceAIDL>; 48 49 public: 50 // Get a connection to the Composer Service. This will block until 51 // a connection is established. Returns null if permission is denied. 52 static sp<gui::ISurfaceComposer> getComposerService(); 53 54 // the following two methods are moved from ISurfaceComposer.h 55 // TODO(b/74619554): Remove this stopgap once the framework is display-agnostic. getInternalDisplayId()56 std::optional<PhysicalDisplayId> getInternalDisplayId() const { 57 std::vector<int64_t> displayIds; 58 binder::Status status = mComposerService->getPhysicalDisplayIds(&displayIds); 59 return (!status.isOk() || displayIds.empty()) 60 ? std::nullopt 61 : DisplayId::fromValue<PhysicalDisplayId>( 62 static_cast<uint64_t>(displayIds.front())); 63 } 64 65 // TODO(b/74619554): Remove this stopgap once the framework is display-agnostic. getInternalDisplayToken()66 sp<IBinder> getInternalDisplayToken() const { 67 const auto displayId = getInternalDisplayId(); 68 if (!displayId) return nullptr; 69 sp<IBinder> display; 70 binder::Status status = 71 mComposerService->getPhysicalDisplayToken(static_cast<int64_t>(displayId->value), 72 &display); 73 return status.isOk() ? display : nullptr; 74 } 75 }; 76 77 // --------------------------------------------------------------------------- 78 }; // namespace android 79