summaryrefslogtreecommitdiff
path: root/services/powermanager/PowerHalWrapper.cpp
blob: 76afbfc6465a7a7a328c725bbcfd28a5a1fa0cdc (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2020 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 LOG_TAG "HalWrapper"
#include <aidl/android/hardware/power/Boost.h>
#include <aidl/android/hardware/power/IPowerHintSession.h>
#include <aidl/android/hardware/power/Mode.h>
#include <powermanager/PowerHalWrapper.h>
#include <utils/Log.h>

#include <cinttypes>

using namespace android::hardware::power;
namespace Aidl = aidl::android::hardware::power;

namespace android {

namespace power {

// -------------------------------------------------------------------------------------------------
inline HalResult<void> toHalResult(const ndk::ScopedAStatus& result) {
    if (result.isOk()) {
        return HalResult<void>::ok();
    }
    ALOGE("Power HAL request failed: %s", result.getDescription().c_str());
    return HalResult<void>::failed(result.getDescription());
}

// -------------------------------------------------------------------------------------------------

HalResult<void> EmptyHalWrapper::setBoost(Aidl::Boost boost, int32_t durationMs) {
    ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available",
          toString(boost).c_str(), durationMs);
    return HalResult<void>::unsupported();
}

HalResult<void> EmptyHalWrapper::setMode(Aidl::Mode mode, bool enabled) {
    ALOGV("Skipped setMode %s to %s because Power HAL not available", toString(mode).c_str(),
          enabled ? "true" : "false");
    return HalResult<void>::unsupported();
}

HalResult<std::shared_ptr<Aidl::IPowerHintSession>> EmptyHalWrapper::createHintSession(
        int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t) {
    ALOGV("Skipped createHintSession(task num=%zu) because Power HAL not available",
          threadIds.size());
    return HalResult<std::shared_ptr<Aidl::IPowerHintSession>>::unsupported();
}

HalResult<int64_t> EmptyHalWrapper::getHintSessionPreferredRate() {
    ALOGV("Skipped getHintSessionPreferredRate because Power HAL not available");
    return HalResult<int64_t>::unsupported();
}

// -------------------------------------------------------------------------------------------------

HalResult<void> HidlHalWrapperV1_0::setBoost(Aidl::Boost boost, int32_t durationMs) {
    if (boost == Aidl::Boost::INTERACTION) {
        return sendPowerHint(V1_3::PowerHint::INTERACTION, durationMs);
    } else {
        ALOGV("Skipped setBoost %s because Power HAL AIDL not available", toString(boost).c_str());
        return HalResult<void>::unsupported();
    }
}

HalResult<void> HidlHalWrapperV1_0::setMode(Aidl::Mode mode, bool enabled) {
    uint32_t data = enabled ? 1 : 0;
    switch (mode) {
        case Aidl::Mode::LAUNCH:
            return sendPowerHint(V1_3::PowerHint::LAUNCH, data);
        case Aidl::Mode::LOW_POWER:
            return sendPowerHint(V1_3::PowerHint::LOW_POWER, data);
        case Aidl::Mode::SUSTAINED_PERFORMANCE:
            return sendPowerHint(V1_3::PowerHint::SUSTAINED_PERFORMANCE, data);
        case Aidl::Mode::VR:
            return sendPowerHint(V1_3::PowerHint::VR_MODE, data);
        case Aidl::Mode::INTERACTIVE:
            return setInteractive(enabled);
        case Aidl::Mode::DOUBLE_TAP_TO_WAKE:
            return setFeature(V1_0::Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
        default:
            ALOGV("Skipped setMode %s because Power HAL AIDL not available",
                  toString(mode).c_str());
            return HalResult<void>::unsupported();
    }
}

HalResult<void> HidlHalWrapperV1_0::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
    auto ret = mHandleV1_0->powerHint(static_cast<V1_0::PowerHint>(hintId), data);
    return HalResult<void>::fromReturn(ret);
}

HalResult<void> HidlHalWrapperV1_0::setInteractive(bool enabled) {
    auto ret = mHandleV1_0->setInteractive(enabled);
    return HalResult<void>::fromReturn(ret);
}

HalResult<void> HidlHalWrapperV1_0::setFeature(V1_0::Feature feature, bool enabled) {
    auto ret = mHandleV1_0->setFeature(feature, enabled);
    return HalResult<void>::fromReturn(ret);
}

HalResult<std::shared_ptr<Aidl::IPowerHintSession>> HidlHalWrapperV1_0::createHintSession(
        int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t) {
    ALOGV("Skipped createHintSession(task num=%zu) because Power HAL not available",
          threadIds.size());
    return HalResult<std::shared_ptr<Aidl::IPowerHintSession>>::unsupported();
}

HalResult<int64_t> HidlHalWrapperV1_0::getHintSessionPreferredRate() {
    ALOGV("Skipped getHintSessionPreferredRate because Power HAL not available");
    return HalResult<int64_t>::unsupported();
}

// -------------------------------------------------------------------------------------------------

HalResult<void> HidlHalWrapperV1_1::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
    auto handle = static_cast<V1_1::IPower*>(mHandleV1_0.get());
    auto ret = handle->powerHintAsync(static_cast<V1_0::PowerHint>(hintId), data);
    return HalResult<void>::fromReturn(ret);
}

// -------------------------------------------------------------------------------------------------

HalResult<void> HidlHalWrapperV1_2::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
    auto handle = static_cast<V1_2::IPower*>(mHandleV1_0.get());
    auto ret = handle->powerHintAsync_1_2(static_cast<V1_2::PowerHint>(hintId), data);
    return HalResult<void>::fromReturn(ret);
}

HalResult<void> HidlHalWrapperV1_2::setBoost(Aidl::Boost boost, int32_t durationMs) {
    switch (boost) {
        case Aidl::Boost::CAMERA_SHOT:
            return sendPowerHint(V1_3::PowerHint::CAMERA_SHOT, durationMs);
        case Aidl::Boost::CAMERA_LAUNCH:
            return sendPowerHint(V1_3::PowerHint::CAMERA_LAUNCH, durationMs);
        default:
            return HidlHalWrapperV1_1::setBoost(boost, durationMs);
    }
}

HalResult<void> HidlHalWrapperV1_2::setMode(Aidl::Mode mode, bool enabled) {
    uint32_t data = enabled ? 1 : 0;
    switch (mode) {
        case Aidl::Mode::CAMERA_STREAMING_SECURE:
        case Aidl::Mode::CAMERA_STREAMING_LOW:
        case Aidl::Mode::CAMERA_STREAMING_MID:
        case Aidl::Mode::CAMERA_STREAMING_HIGH:
            return sendPowerHint(V1_3::PowerHint::CAMERA_STREAMING, data);
        case Aidl::Mode::AUDIO_STREAMING_LOW_LATENCY:
            return sendPowerHint(V1_3::PowerHint::AUDIO_LOW_LATENCY, data);
        default:
            return HidlHalWrapperV1_1::setMode(mode, enabled);
    }
}

// -------------------------------------------------------------------------------------------------

HalResult<void> HidlHalWrapperV1_3::setMode(Aidl::Mode mode, bool enabled) {
    uint32_t data = enabled ? 1 : 0;
    if (mode == Aidl::Mode::EXPENSIVE_RENDERING) {
        return sendPowerHint(V1_3::PowerHint::EXPENSIVE_RENDERING, data);
    }
    return HidlHalWrapperV1_2::setMode(mode, enabled);
}

HalResult<void> HidlHalWrapperV1_3::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
    auto handle = static_cast<V1_3::IPower*>(mHandleV1_0.get());
    auto ret = handle->powerHintAsync_1_3(hintId, data);
    return HalResult<void>::fromReturn(ret);
}

// -------------------------------------------------------------------------------------------------

HalResult<void> AidlHalWrapper::setBoost(Aidl::Boost boost, int32_t durationMs) {
    std::unique_lock<std::mutex> lock(mBoostMutex);
    size_t idx = static_cast<size_t>(boost);

    // Quick return if boost is not supported by HAL
    if (idx >= mBoostSupportedArray.size() || mBoostSupportedArray[idx] == HalSupport::OFF) {
        ALOGV("Skipped setBoost %s because Power HAL doesn't support it", toString(boost).c_str());
        return HalResult<void>::unsupported();
    }

    if (mBoostSupportedArray[idx] == HalSupport::UNKNOWN) {
        bool isSupported = false;
        auto isSupportedRet = mHandle->isBoostSupported(boost, &isSupported);
        if (!isSupportedRet.isOk()) {
            ALOGE("Skipped setBoost %s because check support failed with: %s",
                  toString(boost).c_str(), isSupportedRet.getDescription().c_str());
            // return HalResult::FAILED;
            return HalResult<void>::fromStatus(isSupportedRet);
        }

        mBoostSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
        if (!isSupported) {
            ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
                  toString(boost).c_str());
            return HalResult<void>::unsupported();
        }
    }
    lock.unlock();

    return toHalResult(mHandle->setBoost(boost, durationMs));
}

HalResult<void> AidlHalWrapper::setMode(Aidl::Mode mode, bool enabled) {
    std::unique_lock<std::mutex> lock(mModeMutex);
    size_t idx = static_cast<size_t>(mode);

    // Quick return if mode is not supported by HAL
    if (idx >= mModeSupportedArray.size() || mModeSupportedArray[idx] == HalSupport::OFF) {
        ALOGV("Skipped setMode %s because Power HAL doesn't support it", toString(mode).c_str());
        return HalResult<void>::unsupported();
    }

    if (mModeSupportedArray[idx] == HalSupport::UNKNOWN) {
        bool isSupported = false;
        auto isSupportedRet = mHandle->isModeSupported(mode, &isSupported);
        if (!isSupportedRet.isOk()) {
            return HalResult<void>::failed(isSupportedRet.getDescription());
        }

        mModeSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
        if (!isSupported) {
            ALOGV("Skipped setMode %s because Power HAL doesn't support it",
                  toString(mode).c_str());
            return HalResult<void>::unsupported();
        }
    }
    lock.unlock();

    return toHalResult(mHandle->setMode(mode, enabled));
}

HalResult<std::shared_ptr<Aidl::IPowerHintSession>> AidlHalWrapper::createHintSession(
        int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
    std::shared_ptr<Aidl::IPowerHintSession> appSession;
    return HalResult<std::shared_ptr<Aidl::IPowerHintSession>>::
            fromStatus(mHandle->createHintSession(tgid, uid, threadIds, durationNanos, &appSession),
                       appSession);
}

HalResult<int64_t> AidlHalWrapper::getHintSessionPreferredRate() {
    int64_t rate = -1;
    auto result = mHandle->getHintSessionPreferredRate(&rate);
    return HalResult<int64_t>::fromStatus(result, rate);
}

// -------------------------------------------------------------------------------------------------

} // namespace power

} // namespace android