aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChih-Yu Huang <akahuang@google.com>2021-09-01 16:55:02 +0900
committerChih-Yu Huang <akahuang@google.com>2021-10-20 12:07:13 +0900
commit5e994b906a08e24633e016f010a3a4b21a7a5804 (patch)
tree1e6c2638015cb3da2cdf509dcf05e2c1d090f09d
parent8ca36d563692249e2753302a28d32a22b6de6087 (diff)
downloadv4l2_codec2-5e994b906a08e24633e016f010a3a4b21a7a5804.tar.gz
components: pass weak_ptr to Component::Listener
After the component is destroyed, we cannot use shared_from_this() because there must be a valid shared_ptr pointing to the component. This CL changes to use weak_from_this() to get weak_ptr, and locks the weak_ptr when we need shared_ptr. Also, the parameter of Component::Listener's methods is weak_ptr, instead of shared_ptr. This CL also changes to pass weak_ptr to the listener. Bug: 198215986 Test: android.media.cts.VideoEncoderTest#testOtherH264SurfArbitraryH Change-Id: I3bae943f211db5f68e3b9a86bc193eee09cba65e
-rw-r--r--components/V4L2DecodeComponent.cpp28
-rw-r--r--components/V4L2EncodeComponent.cpp16
-rw-r--r--components/include/v4l2_codec2/components/V4L2DecodeComponent.h3
3 files changed, 16 insertions, 31 deletions
diff --git a/components/V4L2DecodeComponent.cpp b/components/V4L2DecodeComponent.cpp
index 2807c88..456f3c4 100644
--- a/components/V4L2DecodeComponent.cpp
+++ b/components/V4L2DecodeComponent.cpp
@@ -202,7 +202,6 @@ c2_status_t V4L2DecodeComponent::start() {
}
mDecoderTaskRunner = mDecoderThread.task_runner();
mWeakThis = mWeakThisFactory.GetWeakPtr();
- mStdWeakThis = weak_from_this();
c2_status_t status = C2_CORRUPTED;
::base::WaitableEvent done;
@@ -261,7 +260,7 @@ std::unique_ptr<VideoFramePool> V4L2DecodeComponent::getVideoFramePool(const ui:
ALOGV("%s()", __func__);
ALOG_ASSERT(mDecoderTaskRunner->RunsTasksInCurrentSequence());
- auto sharedThis = mStdWeakThis.lock();
+ auto sharedThis = weak_from_this().lock();
if (sharedThis == nullptr) {
ALOGE("%s(): V4L2DecodeComponent instance is destroyed.", __func__);
return nullptr;
@@ -348,7 +347,6 @@ void V4L2DecodeComponent::releaseTask() {
ALOG_ASSERT(mDecoderTaskRunner->RunsTasksInCurrentSequence());
mWeakThisFactory.InvalidateWeakPtrs();
- mStdWeakThis.reset();
mDecoder = nullptr;
}
@@ -698,12 +696,6 @@ bool V4L2DecodeComponent::reportWork(std::unique_ptr<C2Work> work) {
ALOGV("%s(work=%llu)", __func__, work->input.ordinal.frameIndex.peekull());
ALOG_ASSERT(mDecoderTaskRunner->RunsTasksInCurrentSequence());
- auto sharedThis = mStdWeakThis.lock();
- if (sharedThis == nullptr) {
- ALOGE("%s(): V4L2DecodeComponent instance is destroyed.", __func__);
- return false;
- }
-
if (!mListener) {
ALOGE("mListener is nullptr, setListener_vb() not called?");
return false;
@@ -711,7 +703,7 @@ bool V4L2DecodeComponent::reportWork(std::unique_ptr<C2Work> work) {
std::list<std::unique_ptr<C2Work>> finishedWorks;
finishedWorks.emplace_back(std::move(work));
- mListener->onWorkDone_nb(std::move(sharedThis), std::move(finishedWorks));
+ mListener->onWorkDone_nb(weak_from_this(), std::move(finishedWorks));
return true;
}
@@ -748,12 +740,6 @@ void V4L2DecodeComponent::reportAbandonedWorks() {
ALOGV("%s()", __func__);
ALOG_ASSERT(mDecoderTaskRunner->RunsTasksInCurrentSequence());
- auto sharedThis = mStdWeakThis.lock();
- if (sharedThis == nullptr) {
- ALOGE("%s(): V4L2DecodeComponent instance is destroyed.", __func__);
- return;
- }
-
std::list<std::unique_ptr<C2Work>> abandonedWorks;
while (!mPendingWorks.empty()) {
abandonedWorks.emplace_back(std::move(mPendingWorks.front()));
@@ -777,7 +763,7 @@ void V4L2DecodeComponent::reportAbandonedWorks() {
ALOGE("mListener is nullptr, setListener_vb() not called?");
return;
}
- mListener->onWorkDone_nb(std::move(sharedThis), std::move(abandonedWorks));
+ mListener->onWorkDone_nb(weak_from_this(), std::move(abandonedWorks));
}
}
@@ -851,12 +837,6 @@ void V4L2DecodeComponent::reportError(c2_status_t error) {
ALOGE("%s(error=%u)", __func__, static_cast<uint32_t>(error));
ALOG_ASSERT(mDecoderTaskRunner->RunsTasksInCurrentSequence());
- auto sharedThis = mStdWeakThis.lock();
- if (sharedThis == nullptr) {
- ALOGE("%s(): V4L2DecodeComponent instance is destroyed.", __func__);
- return;
- }
-
if (mComponentState.load() == ComponentState::ERROR) return;
mComponentState.store(ComponentState::ERROR);
@@ -864,7 +844,7 @@ void V4L2DecodeComponent::reportError(c2_status_t error) {
ALOGE("mListener is nullptr, setListener_vb() not called?");
return;
}
- mListener->onError_nb(std::move(sharedThis), static_cast<uint32_t>(error));
+ mListener->onError_nb(weak_from_this(), static_cast<uint32_t>(error));
}
c2_status_t V4L2DecodeComponent::announce_nb(const std::vector<C2WorkOutline>& /* items */) {
diff --git a/components/V4L2EncodeComponent.cpp b/components/V4L2EncodeComponent.cpp
index f05ccab..a1b46ab 100644
--- a/components/V4L2EncodeComponent.cpp
+++ b/components/V4L2EncodeComponent.cpp
@@ -812,7 +812,7 @@ void V4L2EncodeComponent::flush() {
mWorkQueue.pop_front();
}
if (!abortedWorkItems.empty()) {
- mListener->onWorkDone_nb(shared_from_this(), std::move(abortedWorkItems));
+ mListener->onWorkDone_nb(weak_from_this(), std::move(abortedWorkItems));
}
}
@@ -1011,15 +1011,23 @@ void V4L2EncodeComponent::reportWork(std::unique_ptr<C2Work> work) {
std::list<std::unique_ptr<C2Work>> finishedWorkList;
finishedWorkList.emplace_back(std::move(work));
- mListener->onWorkDone_nb(shared_from_this(), std::move(finishedWorkList));
+ mListener->onWorkDone_nb(weak_from_this(), std::move(finishedWorkList));
}
bool V4L2EncodeComponent::getBlockPool() {
+ ALOG_ASSERT(mEncoderTaskRunner->RunsTasksInCurrentSequence());
+
+ auto sharedThis = weak_from_this().lock();
+ if (!sharedThis) {
+ ALOGI("%s(): V4L2EncodeComponent instance is already destroyed", __func__);
+ return false;
+ }
+
C2BlockPool::local_id_t poolId = mInterface->getBlockPoolId();
if (poolId == C2BlockPool::BASIC_LINEAR) {
ALOGW("Using unoptimized linear block pool");
}
- c2_status_t status = GetCodec2BlockPool(poolId, shared_from_this(), &mOutputBlockPool);
+ c2_status_t status = GetCodec2BlockPool(poolId, std::move(sharedThis), &mOutputBlockPool);
if (status != C2_OK || !mOutputBlockPool) {
ALOGE("Failed to get output block pool, error: %d", status);
return false;
@@ -1035,7 +1043,7 @@ void V4L2EncodeComponent::reportError(c2_status_t error) {
std::lock_guard<std::mutex> lock(mComponentLock);
if (mComponentState != ComponentState::ERROR) {
setComponentState(ComponentState::ERROR);
- mListener->onError_nb(shared_from_this(), static_cast<uint32_t>(error));
+ mListener->onError_nb(weak_from_this(), static_cast<uint32_t>(error));
}
}
diff --git a/components/include/v4l2_codec2/components/V4L2DecodeComponent.h b/components/include/v4l2_codec2/components/V4L2DecodeComponent.h
index 1e98118..962f7d6 100644
--- a/components/include/v4l2_codec2/components/V4L2DecodeComponent.h
+++ b/components/include/v4l2_codec2/components/V4L2DecodeComponent.h
@@ -140,9 +140,6 @@ private:
::base::Thread mDecoderThread{"V4L2DecodeComponentDecoderThread"};
scoped_refptr<::base::SequencedTaskRunner> mDecoderTaskRunner;
- // Hold a weak_ptr of |*this| when |mDecoderThread| is running.
- std::weak_ptr<V4L2DecodeComponent> mStdWeakThis;
-
::base::WeakPtrFactory<V4L2DecodeComponent> mWeakThisFactory{this};
::base::WeakPtr<V4L2DecodeComponent> mWeakThis;
};