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 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
18 
19 #include "Composer.h"
20 
21 #include <android-base/logging.h>
22 #include <android-base/thread_annotations.h>
23 #include <android/binder_ibinder_platform.h>
24 
25 #include "Util.h"
26 
27 namespace aidl::android::hardware::graphics::composer3::impl {
28 
Composer()29 Composer::Composer() {
30     int32_t composerInterfaceVersion = 1;
31     const auto status = getInterfaceVersion(&composerInterfaceVersion);
32     if (!status.isOk()) {
33         ALOGE("Get interface version from Composer constructor failed %s",
34               status.getDescription().c_str());
35     }
36     mHal = IComposerHal::create(composerInterfaceVersion);
37     CHECK(mHal != nullptr);
38 }
39 
createClient(std::shared_ptr<IComposerClient> * outClient)40 ndk::ScopedAStatus Composer::createClient(std::shared_ptr<IComposerClient>* outClient) {
41     DEBUG_FUNC();
42     std::unique_lock<std::mutex> lock(mClientMutex);
43     ::android::base::ScopedLockAssertion lock_assertion(mClientMutex);
44     if (!waitForClientDestroyedLocked(lock)) {
45         *outClient = nullptr;
46         return TO_BINDER_STATUS(EX_NO_RESOURCES);
47     }
48 
49     auto client = ndk::SharedRefBase::make<ComposerClient>(mHal.get());
50     if (!client || !client->init()) {
51         *outClient = nullptr;
52         return TO_BINDER_STATUS(EX_NO_RESOURCES);
53     }
54 
55     auto clientDestroyed = [this]() { onClientDestroyed(); };
56     client->setOnClientDestroyed(clientDestroyed);
57 
58     mClientAlive = true;
59     *outClient = client;
60 
61     return ndk::ScopedAStatus::ok();
62 }
63 
dump(int fd,const char **,uint32_t)64 binder_status_t Composer::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
65     std::string output;
66     mHal->dumpDebugInfo(&output);
67     write(fd, output.c_str(), output.size());
68     return STATUS_OK;
69 }
70 
getCapabilities(std::vector<Capability> * caps)71 ndk::ScopedAStatus Composer::getCapabilities(std::vector<Capability>* caps) {
72     DEBUG_FUNC();
73     mHal->getCapabilities(caps);
74     return ndk::ScopedAStatus::ok();
75 }
76 
waitForClientDestroyedLocked(std::unique_lock<std::mutex> & lock)77 bool Composer::waitForClientDestroyedLocked(std::unique_lock<std::mutex>& lock) {
78     if (mClientAlive) {
79         using namespace std::chrono_literals;
80 
81         // In surface flinger we delete a composer client on one thread and
82         // then create a new client on another thread. Although surface
83         // flinger ensures the calls are made in that sequence (destroy and
84         // then create), sometimes the calls land in the composer service
85         // inverted (create and then destroy). Wait for a brief period to
86         // see if the existing client is destroyed.
87         LOG(DEBUG) << "waiting for previous client to be destroyed";
88         mClientDestroyedCondition.wait_for(lock, 1s, [this]() -> bool {
89             ::android::base::ScopedLockAssertion lock_assertion(mClientMutex);
90             return !mClientAlive;
91         });
92         if (mClientAlive) {
93             LOG(DEBUG) << "previous client was not destroyed";
94         }
95     }
96 
97     return !mClientAlive;
98 }
99 
onClientDestroyed()100 void Composer::onClientDestroyed() {
101     std::lock_guard<std::mutex> lock(mClientMutex);
102     mClientAlive = false;
103     mClientDestroyedCondition.notify_all();
104 }
105 
createBinder()106 ::ndk::SpAIBinder Composer::createBinder() {
107     auto binder = BnComposer::createBinder();
108     AIBinder_setInheritRt(binder.get(), true);
109     return binder;
110 }
111 
112 } // namespace aidl::android::hardware::graphics::composer3::impl
113 
114