From ee7b436210b09635d032b50eefecb7369be136e7 Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Wed, 7 Oct 2020 20:58:26 +0900 Subject: [PATCH] Ensure not to emit the resource ready signal during the callback Change-Id: Icc496c34a11df14aae8b99affe846c14bca308fa --- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 34 ++++++++-- .../controls/control/control-data-impl.cpp | 72 ++++++++++++++++++++-- .../internal/controls/control/control-data-impl.h | 13 ++++ 3 files changed, 111 insertions(+), 8 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index 7b0b54d..f31da7c 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -2749,11 +2749,20 @@ void OnResourceReadySignal( Control control ) { gResourceReadySignalCounter++; - if( gResourceReadySignalCounter == 1 ) + if(control.GetVisualResourceStatus(ImageView::Property::IMAGE) == Visual::ResourceStatus::READY) { - // Set image twice - ImageView::DownCast( control ).SetImage( gImage_34_RGBA ); - ImageView::DownCast( control ).SetImage( gImage_34_RGBA ); + if( gResourceReadySignalCounter == 1 ) + { + // Set image twice + // It makes the first new visual be deleted immediately + ImageView::DownCast( control ).SetImage( gImage_34_RGBA ); + ImageView::DownCast( control ).SetImage( gImage_34_RGBA ); + } + } + else if(control.GetVisualResourceStatus(ImageView::Property::IMAGE) == Visual::ResourceStatus::FAILED) + { + // Make the resource ready immediately + control[ImageView::Property::IMAGE] = TEST_RESOURCE_DIR "/svg1.svg"; } } @@ -2781,5 +2790,22 @@ int UtcDaliImageViewSetImageOnResourceReadySignal(void) DALI_TEST_EQUALS( imageView.IsResourceReady(), true, TEST_LOCATION ); + // Reset count + gResourceReadySignalCounter = 0; + + imageView[ImageView::Property::IMAGE] = "invalid.jpg"; + + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + + application.SendNotification(); + application.Render(); + + // Run idle callback + application.RunIdles(); + + DALI_TEST_EQUALS( gResourceReadySignalCounter, 2, TEST_LOCATION ); + + DALI_TEST_EQUALS( imageView.IsResourceReady(), true, TEST_LOCATION ); + END_TEST; } diff --git a/dali-toolkit/internal/controls/control/control-data-impl.cpp b/dali-toolkit/internal/controls/control/control-data-impl.cpp index 8a2dfd7..368ab0a 100755 --- a/dali-toolkit/internal/controls/control/control-data-impl.cpp +++ b/dali-toolkit/internal/controls/control/control-data-impl.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -322,7 +323,7 @@ Control::Impl::Impl( Control& controlImpl ) mDownFocusableActorId( -1 ), mStyleName(""), mBackgroundColor(Color::TRANSPARENT), - mStartingPinchScale( NULL ), + mStartingPinchScale(nullptr), mMargin( 0, 0, 0, 0 ), mPadding( 0, 0, 0, 0 ), mKeyEventSignal(), @@ -336,9 +337,12 @@ Control::Impl::Impl( Control& controlImpl ) mLongPressGestureDetector(), mTooltip( NULL ), mInputMethodContext(), + mIdleCallback(nullptr), mFlags( Control::ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ), mIsKeyboardNavigationSupported( false ), - mIsKeyboardFocusGroup( false ) + mIsKeyboardFocusGroup( false ), + mIsEmittingResourceReadySignal(false), + mNeedToEmitResourceReady(false) { } @@ -346,6 +350,12 @@ Control::Impl::~Impl() { // All gesture detectors will be destroyed so no need to disconnect. delete mStartingPinchScale; + + if(mIdleCallback && Adaptor::IsAvailable()) + { + // Removes the callback from the callback manager in case the control is destroyed before the callback is executed. + Adaptor::Get().RemoveIdle(mIdleCallback); + } } Control::Impl& Control::Impl::Get( Internal::Control& internalControl ) @@ -670,8 +680,10 @@ void Control::Impl::ResourceReady( Visual::Base& object) // Emit signal if all enabled visuals registered by the control are ready. if( IsResourceReady() ) { - Dali::Toolkit::Control handle( mControlImpl.GetOwner() ); - mResourceReadySignal.Emit( handle ); + // Reset the flag + mNeedToEmitResourceReady = false; + + EmitResourceReadySignal(); } } @@ -1448,6 +1460,58 @@ void Control::Impl::ClearShadow() mControlImpl.RelayoutRequest(); } +void Control::Impl::EmitResourceReadySignal() +{ + if(!mIsEmittingResourceReadySignal) + { + // Guard against calls to emit the signal during the callback + mIsEmittingResourceReadySignal = true; + + // If the signal handler changes visual, it may become ready during this call & therefore this method will + // get called again recursively. If so, mNeedToEmitResourceReady is set below, and we act on it after that secondary + // invocation has completed by notifying in an Idle callback to prevent further recursion. + Dali::Toolkit::Control handle(mControlImpl.GetOwner()); + mResourceReadySignal.Emit(handle); + + if(mNeedToEmitResourceReady) + { + // Add idler to emit the signal again + if(!mIdleCallback) + { + // The callback manager takes the ownership of the callback object. + mIdleCallback = MakeCallback( this, &Control::Impl::OnIdleCallback); + Adaptor::Get().AddIdle(mIdleCallback, false); + } + } + + mIsEmittingResourceReadySignal = false; + } + else + { + mNeedToEmitResourceReady = true; + } +} + +void Control::Impl::OnIdleCallback() +{ + if(mNeedToEmitResourceReady) + { + // Reset the flag + mNeedToEmitResourceReady = false; + + // A visual is ready so control may need relayouting if staged + if(mControlImpl.Self().GetProperty(Actor::Property::CONNECTED_TO_SCENE)) + { + mControlImpl.RelayoutRequest(); + } + + EmitResourceReadySignal(); + } + + // Set the pointer to null as the callback manager deletes the callback after execute it. + mIdleCallback = nullptr; +} + } // namespace Internal } // namespace Toolkit diff --git a/dali-toolkit/internal/controls/control/control-data-impl.h b/dali-toolkit/internal/controls/control/control-data-impl.h index 43cab69..e042113 100755 --- a/dali-toolkit/internal/controls/control/control-data-impl.h +++ b/dali-toolkit/internal/controls/control/control-data-impl.h @@ -386,6 +386,16 @@ private: */ void RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0 ); + /** + * @brief Emits the resource ready signal. + */ + void EmitResourceReadySignal(); + + /** + * @brief Callbacks called on idle. + */ + void OnIdleCallback(); + public: Control& mControlImpl; @@ -419,10 +429,13 @@ public: TooltipPtr mTooltip; InputMethodContext mInputMethodContext; + CallbackBase* mIdleCallback; ///< The idle callback to emit the resource ready signal. ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor. bool mIsKeyboardNavigationSupported :1; ///< Stores whether keyboard navigation is supported by the control. bool mIsKeyboardFocusGroup :1; ///< Stores whether the control is a focus group. + bool mIsEmittingResourceReadySignal :1; ///< True during ResourceReady(). + bool mNeedToEmitResourceReady :1; ///< True if need to emit the resource ready signal again. RegisteredVisualContainer mRemoveVisuals; ///< List of visuals that are being replaced by another visual once ready -- 2.7.4