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 ** args,uint32_t numArgs)64 binder_status_t Composer::dump(int fd, const char** args, uint32_t numArgs) {
65     std::vector<std::string> argsVector(numArgs);
66     for (uint32_t i = 0; i < numArgs; ++i) {
67         argsVector[i] = args[i];
68     }
69 
70     std::string output;
71     mHal->dumpDebugInfo(&output, argsVector);
72     write(fd, output.c_str(), output.size());
73     return STATUS_OK;
74 }
75 
getCapabilities(std::vector<Capability> * caps)76 ndk::ScopedAStatus Composer::getCapabilities(std::vector<Capability>* caps) {
77     DEBUG_FUNC();
78     mHal->getCapabilities(caps);
79     return ndk::ScopedAStatus::ok();
80 }
81 
waitForClientDestroyedLocked(std::unique_lock<std::mutex> & lock)82 bool Composer::waitForClientDestroyedLocked(std::unique_lock<std::mutex>& lock) {
83     if (mClientAlive) {
84         using namespace std::chrono_literals;
85 
86         // In surface flinger we delete a composer client on one thread and
87         // then create a new client on another thread. Although surface
88         // flinger ensures the calls are made in that sequence (destroy and
89         // then create), sometimes the calls land in the composer service
90         // inverted (create and then destroy). Wait for a brief period to
91         // see if the existing client is destroyed.
92         LOG(DEBUG) << "waiting for previous client to be destroyed";
93         mClientDestroyedCondition.wait_for(lock, 1s, [this]() -> bool {
94             ::android::base::ScopedLockAssertion lock_assertion(mClientMutex);
95             return !mClientAlive;
96         });
97         if (mClientAlive) {
98             LOG(DEBUG) << "previous client was not destroyed";
99         }
100     }
101 
102     return !mClientAlive;
103 }
104 
onClientDestroyed()105 void Composer::onClientDestroyed() {
106     std::lock_guard<std::mutex> lock(mClientMutex);
107     mClientAlive = false;
108     mClientDestroyedCondition.notify_all();
109 }
110 
createBinder()111 ::ndk::SpAIBinder Composer::createBinder() {
112     auto binder = BnComposer::createBinder();
113     AIBinder_setInheritRt(binder.get(), true);
114     return binder;
115 }
116 
117 } // namespace aidl::android::hardware::graphics::composer3::impl
118 
119