summaryrefslogtreecommitdiff
path: root/hwc3/Composer.cpp
blob: 40cadc8e6a33c7d57a0ef0823bde92662f0ceba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)

#include "Composer.h"

#include <android-base/logging.h>
#include <android/binder_ibinder_platform.h>

#include "Util.h"

namespace aidl::android::hardware::graphics::composer3::impl {

Composer::Composer() {
    int32_t composerInterfaceVersion = 1;
    const auto status = getInterfaceVersion(&composerInterfaceVersion);
    if (!status.isOk()) {
        ALOGE("Get interface version from Composer constructor failed %s",
              status.getDescription().c_str());
    }
    mHal = IComposerHal::create(composerInterfaceVersion);
    CHECK(mHal != nullptr);
}

ndk::ScopedAStatus Composer::createClient(std::shared_ptr<IComposerClient>* outClient) {
    DEBUG_FUNC();
    std::unique_lock<std::mutex> lock(mClientMutex);
    if (!waitForClientDestroyedLocked(lock)) {
        *outClient = nullptr;
        return TO_BINDER_STATUS(EX_NO_RESOURCES);
    }

    auto client = ndk::SharedRefBase::make<ComposerClient>(mHal.get());
    if (!client || !client->init()) {
        *outClient = nullptr;
        return TO_BINDER_STATUS(EX_NO_RESOURCES);
    }

    auto clientDestroyed = [this]() { onClientDestroyed(); };
    client->setOnClientDestroyed(clientDestroyed);

    mClientAlive = true;
    *outClient = client;

    return ndk::ScopedAStatus::ok();
}

binder_status_t Composer::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
    std::string output;
    mHal->dumpDebugInfo(&output);
    write(fd, output.c_str(), output.size());
    return STATUS_OK;
}

ndk::ScopedAStatus Composer::getCapabilities(std::vector<Capability>* caps) {
    DEBUG_FUNC();
    mHal->getCapabilities(caps);
    return ndk::ScopedAStatus::ok();
}

bool Composer::waitForClientDestroyedLocked(std::unique_lock<std::mutex>& lock) {
    if (mClientAlive) {
        using namespace std::chrono_literals;

        // In surface flinger we delete a composer client on one thread and
        // then create a new client on another thread. Although surface
        // flinger ensures the calls are made in that sequence (destroy and
        // then create), sometimes the calls land in the composer service
        // inverted (create and then destroy). Wait for a brief period to
        // see if the existing client is destroyed.
        LOG(DEBUG) << "waiting for previous client to be destroyed";
        mClientDestroyedCondition.wait_for(lock, 1s,
                                           [this]() -> bool { return !mClientAlive; });
        if (mClientAlive) {
            LOG(DEBUG) << "previous client was not destroyed";
        }
    }

    return !mClientAlive;
}

void Composer::onClientDestroyed() {
    std::lock_guard<std::mutex> lock(mClientMutex);
    mClientAlive = false;
    mClientDestroyedCondition.notify_all();
}

::ndk::SpAIBinder Composer::createBinder() {
    auto binder = BnComposer::createBinder();
    AIBinder_setInheritRt(binder.get(), true);
    return binder;
}

} // namespace aidl::android::hardware::graphics::composer3::impl