(Vector) Fix occasional tc failure
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-manager.cpp
index 8e47d81..7971b4d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
 
 // EXTERNAL INCLUDES
-#include <dali/integration-api/debug.h>
 #include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 namespace
 {
-
 #if defined(DEBUG_ENABLED)
-Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
+Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
 #endif
 
 } // unnamed namespace
 
 VectorAnimationManager::VectorAnimationManager()
 : mEventCallbacks(),
-  mVectorAnimationThread( nullptr ),
-  mProcessorRegistered( false )
+  mLifecycleObservers(),
+  mVectorAnimationThread(nullptr),
+  mProcessorRegistered(false)
 {
 }
 
 VectorAnimationManager::~VectorAnimationManager()
 {
-  for( auto&& iter : mEventCallbacks )
+  for(auto&& iter : mEventCallbacks)
   {
     delete iter;
   }
   mEventCallbacks.clear();
 
-  if( mProcessorRegistered )
+  if(mProcessorRegistered)
+  {
+    Adaptor::Get().UnregisterProcessor(*this, true);
+  }
+
+  for(auto observer : mLifecycleObservers)
+  {
+    observer->VectorAnimationManagerDestroyed();
+  }
+}
+
+void VectorAnimationManager::AddObserver(VectorAnimationManager::LifecycleObserver& observer)
+{
+  DALI_ASSERT_DEBUG(mLifecycleObservers.end() == std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer));
+  mLifecycleObservers.push_back(&observer);
+}
+
+void VectorAnimationManager::RemoveObserver(VectorAnimationManager::LifecycleObserver& observer)
+{
+  auto iterator = std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer);
+  if(iterator != mLifecycleObservers.end())
   {
-    Adaptor::Get().UnregisterProcessor( *this );
+    mLifecycleObservers.erase(iterator);
   }
 }
 
 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
 {
-  if( !mVectorAnimationThread )
+  if(!mVectorAnimationThread)
   {
-    mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
+    mVectorAnimationThread = std::unique_ptr<VectorAnimationThread>(new VectorAnimationThread());
     mVectorAnimationThread->Start();
   }
   return *mVectorAnimationThread;
 }
 
-void VectorAnimationManager::RegisterEventCallback( CallbackBase* callback )
+void VectorAnimationManager::RegisterEventCallback(CallbackBase* callback)
 {
-  mEventCallbacks.push_back( callback );
+  mEventCallbacks.push_back(callback);
 
-  if( !mProcessorRegistered )
+  if(!mProcessorRegistered)
   {
-    Adaptor::Get().RegisterProcessor( *this );
+    Adaptor::Get().RegisterProcessor(*this, true); // Use post processor to trigger after layoutting
     mProcessorRegistered = true;
   }
 }
 
-void VectorAnimationManager::UnregisterEventCallback( CallbackBase* callback )
+void VectorAnimationManager::UnregisterEventCallback(CallbackBase* callback)
 {
-  auto iter = std::find( mEventCallbacks.begin(), mEventCallbacks.end(), callback );
-  if( iter != mEventCallbacks.end() )
+  auto iter = std::find(mEventCallbacks.begin(), mEventCallbacks.end(), callback);
+  if(iter != mEventCallbacks.end())
   {
-    mEventCallbacks.erase( iter );
+    mEventCallbacks.erase(iter);
 
-    if( mEventCallbacks.empty() )
+    if(mEventCallbacks.empty())
     {
-      if( Adaptor::IsAvailable() )
+      if(Adaptor::IsAvailable())
       {
-        Adaptor::Get().UnregisterProcessor( *this );
+        Adaptor::Get().UnregisterProcessor(*this, true);
         mProcessorRegistered = false;
       }
     }
   }
 }
 
-void VectorAnimationManager::Process()
+void VectorAnimationManager::Process(bool postProcessor)
 {
-  for( auto&& iter : mEventCallbacks )
+  for(auto&& iter : mEventCallbacks)
   {
-    CallbackBase::Execute( *iter );
+    CallbackBase::Execute(*iter);
     delete iter;
   }
   mEventCallbacks.clear();
 
-  Adaptor::Get().UnregisterProcessor( *this );
+  Adaptor::Get().UnregisterProcessor(*this, true);
   mProcessorRegistered = false;
 }