1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <memory>
20 #include <utility>
21 
22 #include <android/binder_auto_utils.h>
23 #include <android/binder_ibinder_platform.h>
24 #include <system/thread_defs.h>
25 
26 namespace aidl::android::hardware::audio::core {
27 
28 // Helper used for interfaces that require a persistent instance. We hold them via a strong
29 // pointer. The binder token is retained for a call to 'setMinSchedulerPolicy'.
30 template <class C>
31 struct ChildInterface : private std::pair<std::shared_ptr<C>, ndk::SpAIBinder> {
32     ChildInterface() = default;
33     ChildInterface& operator=(const std::shared_ptr<C>& c) {
34         return operator=(std::shared_ptr<C>(c));
35     }
36     ChildInterface& operator=(std::shared_ptr<C>&& c) {
37         this->first = std::move(c);
38         return *this;
39     }
40     explicit operator bool() const { return !!this->first; }
41     C& operator*() const { return *(this->first); }
42     C* operator->() const { return this->first; }
getPtrChildInterface43     std::shared_ptr<C> getPtr() { return this->first; }
44     // Use 'getInstance' when returning the interface instance.
getInstanceChildInterface45     std::shared_ptr<C> getInstance() {
46         (void)getBinder();
47         return this->first;
48     }
getBinderChildInterface49     AIBinder* getBinder() {
50         if (this->second.get() == nullptr) {
51             const auto binder = this->second = this->first->asBinder();
52             AIBinder_setMinSchedulerPolicy(binder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
53             AIBinder_setInheritRt(binder.get(), true);
54         }
55         return this->second.get();
56     }
57 };
58 
59 }  // namespace aidl::android::hardware::audio::core
60