audio: Update ChildInterface to set scheduler policy lazily am: 780fefb331 am: e35c2c8368

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/2674338

Change-Id: Ia3d33ab611d4fb766450a5070f6888f6fd127e28
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/audio/aidl/default/Module.cpp b/audio/aidl/default/Module.cpp
index eb9cbc3..b59bd7c 100644
--- a/audio/aidl/default/Module.cpp
+++ b/audio/aidl/default/Module.cpp
@@ -1129,7 +1129,7 @@
     if (!mSoundDose) {
         mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
     }
-    *_aidl_return = mSoundDose.getPtr();
+    *_aidl_return = mSoundDose.getInstance();
     LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
     return ndk::ScopedAStatus::ok();
 }
diff --git a/audio/aidl/default/ModulePrimary.cpp b/audio/aidl/default/ModulePrimary.cpp
index d8ea9e7..29e8126 100644
--- a/audio/aidl/default/ModulePrimary.cpp
+++ b/audio/aidl/default/ModulePrimary.cpp
@@ -37,7 +37,7 @@
     if (!mTelephony) {
         mTelephony = ndk::SharedRefBase::make<Telephony>();
     }
-    *_aidl_return = mTelephony.getPtr();
+    *_aidl_return = mTelephony.getInstance();
     LOG(DEBUG) << __func__
                << ": returning instance of ITelephony: " << _aidl_return->get()->asBinder().get();
     return ndk::ScopedAStatus::ok();
diff --git a/audio/aidl/default/Stream.cpp b/audio/aidl/default/Stream.cpp
index f4194d2..032fa87 100644
--- a/audio/aidl/default/Stream.cpp
+++ b/audio/aidl/default/Stream.cpp
@@ -612,7 +612,7 @@
     if (!mCommon) {
         LOG(FATAL) << __func__ << ": the common interface was not created";
     }
-    *_aidl_return = mCommon.getPtr();
+    *_aidl_return = mCommon.getInstance();
     LOG(DEBUG) << __func__ << ": returning " << _aidl_return->get()->asBinder().get();
     return ndk::ScopedAStatus::ok();
 }
diff --git a/audio/aidl/default/include/core-impl/ChildInterface.h b/audio/aidl/default/include/core-impl/ChildInterface.h
index 1b31691..2421b59 100644
--- a/audio/aidl/default/include/core-impl/ChildInterface.h
+++ b/audio/aidl/default/include/core-impl/ChildInterface.h
@@ -35,14 +35,20 @@
     }
     ChildInterface& operator=(std::shared_ptr<C>&& c) {
         this->first = std::move(c);
-        this->second = this->first->asBinder();
-        AIBinder_setMinSchedulerPolicy(this->second.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
         return *this;
     }
     explicit operator bool() const { return !!this->first; }
     C& operator*() const { return *(this->first); }
     C* operator->() const { return this->first; }
-    std::shared_ptr<C> getPtr() const { return this->first; }
+    // Use 'getInstance' when returning the interface instance.
+    std::shared_ptr<C> getInstance() {
+        if (this->second.get() == nullptr) {
+            this->second = this->first->asBinder();
+            AIBinder_setMinSchedulerPolicy(this->second.get(), SCHED_NORMAL,
+                                           ANDROID_PRIORITY_AUDIO);
+        }
+        return this->first;
+    }
 };
 
 }  // namespace aidl::android::hardware::audio::core
diff --git a/audio/aidl/default/stub/ModuleStub.cpp b/audio/aidl/default/stub/ModuleStub.cpp
index 5e4cd88..9f6e0b4 100644
--- a/audio/aidl/default/stub/ModuleStub.cpp
+++ b/audio/aidl/default/stub/ModuleStub.cpp
@@ -37,7 +37,7 @@
     if (!mBluetooth) {
         mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
     }
-    *_aidl_return = mBluetooth.getPtr();
+    *_aidl_return = mBluetooth.getInstance();
     LOG(DEBUG) << __func__
                << ": returning instance of IBluetooth: " << _aidl_return->get()->asBinder().get();
     return ndk::ScopedAStatus::ok();
@@ -47,7 +47,7 @@
     if (!mBluetoothA2dp) {
         mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
     }
-    *_aidl_return = mBluetoothA2dp.getPtr();
+    *_aidl_return = mBluetoothA2dp.getInstance();
     LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: "
                << _aidl_return->get()->asBinder().get();
     return ndk::ScopedAStatus::ok();
@@ -57,7 +57,7 @@
     if (!mBluetoothLe) {
         mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
     }
-    *_aidl_return = mBluetoothLe.getPtr();
+    *_aidl_return = mBluetoothLe.getInstance();
     LOG(DEBUG) << __func__
                << ": returning instance of IBluetoothLe: " << _aidl_return->get()->asBinder().get();
     return ndk::ScopedAStatus::ok();