1 /*
2 * Copyright (C) 2022 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 #include "Composer.h"
18
19 #include <android-base/logging.h>
20 #include <android/binder_ibinder_platform.h>
21
22 #include "Common.h"
23
24 namespace aidl::android::hardware::graphics::composer3::impl {
25
createClient(std::shared_ptr<IComposerClient> * outClient)26 ndk::ScopedAStatus Composer::createClient(std::shared_ptr<IComposerClient>* outClient) {
27 DEBUG_LOG("%s", __FUNCTION__);
28
29 std::unique_lock<std::mutex> lock(mClientMutex);
30
31 const bool previousClientDestroyed = waitForClientDestroyedLocked(lock);
32 if (!previousClientDestroyed) {
33 ALOGE("%s: failed as composer client already exists", __FUNCTION__);
34 *outClient = nullptr;
35 return ToBinderStatus(HWC3::Error::NoResources);
36 }
37
38 auto client = ndk::SharedRefBase::make<ComposerClient>();
39 if (!client) {
40 ALOGE("%s: failed to init composer client", __FUNCTION__);
41 *outClient = nullptr;
42 return ToBinderStatus(HWC3::Error::NoResources);
43 }
44
45 auto error = client->init();
46 if (error != HWC3::Error::None) {
47 *outClient = nullptr;
48 return ToBinderStatus(error);
49 }
50
51 auto clientDestroyed = [this]() { onClientDestroyed(); };
52 client->setOnClientDestroyed(clientDestroyed);
53
54 mClient = client;
55 *outClient = client;
56
57 return ndk::ScopedAStatus::ok();
58 }
59
waitForClientDestroyedLocked(std::unique_lock<std::mutex> & lock)60 bool Composer::waitForClientDestroyedLocked(std::unique_lock<std::mutex>& lock) {
61 if (!mClient.expired()) {
62 // In surface flinger we delete a composer client on one thread and
63 // then create a new client on another thread. Although surface
64 // flinger ensures the calls are made in that sequence (destroy and
65 // then create), sometimes the calls land in the composer service
66 // inverted (create and then destroy). Wait for a brief period to
67 // see if the existing client is destroyed.
68 constexpr const auto kTimeout = std::chrono::seconds(5);
69 mClientDestroyedCondition.wait_for(lock, kTimeout,
70 [this]() -> bool { return mClient.expired(); });
71 if (!mClient.expired()) {
72 ALOGW("%s: previous client was not destroyed", __FUNCTION__);
73 }
74 }
75
76 return mClient.expired();
77 }
78
onClientDestroyed()79 void Composer::onClientDestroyed() {
80 std::lock_guard<std::mutex> lock(mClientMutex);
81
82 mClientDestroyedCondition.notify_all();
83 }
84
dump(int fd,const char **,uint32_t)85 binder_status_t Composer::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
86 DEBUG_LOG("%s", __FUNCTION__);
87
88 std::string output("TODO");
89
90 write(fd, output.c_str(), output.size());
91 return STATUS_OK;
92 }
93
getCapabilities(std::vector<Capability> * caps)94 ndk::ScopedAStatus Composer::getCapabilities(std::vector<Capability>* caps) {
95 DEBUG_LOG("%s", __FUNCTION__);
96
97 caps->clear();
98 caps->emplace_back(Capability::PRESENT_FENCE_IS_NOT_RELIABLE);
99 caps->emplace_back(Capability::BOOT_DISPLAY_CONFIG);
100
101 return ndk::ScopedAStatus::ok();
102 }
103
createBinder()104 ::ndk::SpAIBinder Composer::createBinder() {
105 DEBUG_LOG("%s", __FUNCTION__);
106
107 auto binder = BnComposer::createBinder();
108 AIBinder_setInheritRt(binder.get(), true);
109 return binder;
110 }
111
112 } // namespace aidl::android::hardware::graphics::composer3::impl