1 /*
2  * Copyright (C) 2024 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 <powermanager/PowerHintSessionWrapper.h>
18 
19 using namespace aidl::android::hardware::power;
20 
21 namespace android::power {
22 
23 // Caches support for a given call in a static variable, checking both
24 // the return value and interface version.
25 #define CACHE_SUPPORT(version, method)                      \
26     ({                                                      \
27         static bool support = mInterfaceVersion >= version; \
28         !support ? decltype(method)::unsupported() : ({     \
29             auto result = method;                           \
30             if (result.isUnsupported()) {                   \
31                 support = false;                            \
32             }                                               \
33             std::move(result);                              \
34         });                                                 \
35     })
36 
37 #define CHECK_SESSION(resultType)                                    \
38     if (mSession == nullptr) {                                       \
39         return HalResult<resultType>::failed("Session not running"); \
40     }
41 
42 // FWD_CALL just forwards calls from the wrapper to the session object.
43 // It only works if the call has no return object, as is the case with all calls
44 // except getSessionConfig.
45 #define FWD_CALL(version, name, args, untypedArgs)                                              \
46     HalResult<void> PowerHintSessionWrapper::name args {                                        \
47         CHECK_SESSION(void)                                                                     \
48         return CACHE_SUPPORT(version, HalResult<void>::fromStatus(mSession->name untypedArgs)); \
49     }
50 
PowerHintSessionWrapper(std::shared_ptr<IPowerHintSession> && session)51 PowerHintSessionWrapper::PowerHintSessionWrapper(std::shared_ptr<IPowerHintSession>&& session)
52       : mSession(session) {
53     if (mSession != nullptr) {
54         mSession->getInterfaceVersion(&mInterfaceVersion);
55     }
56 }
57 
58 // Support for individual hints/modes is not really handled here since there
59 // is no way to check for it, so in the future if a way to check that is added,
60 // this will need to be updated.
61 
62 FWD_CALL(2, updateTargetWorkDuration, (int64_t in_targetDurationNanos), (in_targetDurationNanos));
63 FWD_CALL(2, reportActualWorkDuration, (const std::vector<WorkDuration>& in_durations),
64          (in_durations));
65 FWD_CALL(2, pause, (), ());
66 FWD_CALL(2, resume, (), ());
67 FWD_CALL(2, close, (), ());
68 FWD_CALL(4, sendHint, (SessionHint in_hint), (in_hint));
69 FWD_CALL(4, setThreads, (const std::vector<int32_t>& in_threadIds), (in_threadIds));
70 FWD_CALL(5, setMode, (SessionMode in_type, bool in_enabled), (in_type, in_enabled));
71 
getSessionConfig()72 HalResult<SessionConfig> PowerHintSessionWrapper::getSessionConfig() {
73     CHECK_SESSION(SessionConfig);
74     SessionConfig config;
75     return CACHE_SUPPORT(5,
76                          HalResult<SessionConfig>::fromStatus(mSession->getSessionConfig(&config),
77                                                               std::move(config)));
78 }
79 
80 } // namespace android::power
81