From 2e3b3e4c17c9bdb6d6c4a6bb52fa47a65cfc88e7 Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Fri, 21 Jul 2023 17:01:38 -0700 Subject: [PATCH] audio: Update ChildInterface to set scheduler policy lazily Since the client may end up never retrieving the interface instance for sending it over Binder, postpone setting of the scheduler policy to until that very moment. Rename 'ChildInterface::getPtr' to 'getInstance' for clarity. Bug: 264712385 Test: atest VtsHalAudioCoreTargetTest Change-Id: I31f9dd832c7f85f9632d1d389a8b1063d237d4c1 (cherry picked from commit 780fefb331e915d84e7f19176706740476152bb3) Merged-In: I31f9dd832c7f85f9632d1d389a8b1063d237d4c1 --- audio/aidl/default/Module.cpp | 2 +- audio/aidl/default/ModulePrimary.cpp | 2 +- audio/aidl/default/Stream.cpp | 2 +- .../aidl/default/include/core-impl/ChildInterface.h | 12 +++++++++--- audio/aidl/default/stub/ModuleStub.cpp | 6 +++--- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/audio/aidl/default/Module.cpp b/audio/aidl/default/Module.cpp index eb9cbc3445..b59bd7c081 100644 --- a/audio/aidl/default/Module.cpp +++ b/audio/aidl/default/Module.cpp @@ -1129,7 +1129,7 @@ ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr* _aidl_retur if (!mSoundDose) { mSoundDose = ndk::SharedRefBase::make(); } - *_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 d8ea9e7731..29e81264ac 100644 --- a/audio/aidl/default/ModulePrimary.cpp +++ b/audio/aidl/default/ModulePrimary.cpp @@ -37,7 +37,7 @@ ndk::ScopedAStatus ModulePrimary::getTelephony(std::shared_ptr* _aid if (!mTelephony) { mTelephony = ndk::SharedRefBase::make(); } - *_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 f4194d23c1..032fa87a7f 100644 --- a/audio/aidl/default/Stream.cpp +++ b/audio/aidl/default/Stream.cpp @@ -612,7 +612,7 @@ ndk::ScopedAStatus StreamCommonImpl::getStreamCommonCommon( 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 1b31691556..2421b59a1a 100644 --- a/audio/aidl/default/include/core-impl/ChildInterface.h +++ b/audio/aidl/default/include/core-impl/ChildInterface.h @@ -35,14 +35,20 @@ struct ChildInterface : private std::pair, ndk::SpAIBinder> { } ChildInterface& operator=(std::shared_ptr&& 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 getPtr() const { return this->first; } + // Use 'getInstance' when returning the interface instance. + std::shared_ptr 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 5e4cd88ff6..9f6e0b47cb 100644 --- a/audio/aidl/default/stub/ModuleStub.cpp +++ b/audio/aidl/default/stub/ModuleStub.cpp @@ -37,7 +37,7 @@ ndk::ScopedAStatus ModuleStub::getBluetooth(std::shared_ptr* _aidl_r if (!mBluetooth) { mBluetooth = ndk::SharedRefBase::make(); } - *_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 @@ ndk::ScopedAStatus ModuleStub::getBluetoothA2dp(std::shared_ptr* if (!mBluetoothA2dp) { mBluetoothA2dp = ndk::SharedRefBase::make(); } - *_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 @@ ndk::ScopedAStatus ModuleStub::getBluetoothLe(std::shared_ptr* _ai if (!mBluetoothLe) { mBluetoothLe = ndk::SharedRefBase::make(); } - *_aidl_return = mBluetoothLe.getPtr(); + *_aidl_return = mBluetoothLe.getInstance(); LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get()->asBinder().get(); return ndk::ScopedAStatus::ok();