Ensure AnimatedVectorImageVisual doesn't hang app
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-manager.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/adaptor-framework/adaptor.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
42 #endif
43
44 } // unnamed namespace
45
46 VectorAnimationManager::VectorAnimationManager()
47 : mEventCallbacks(),
48   mLifecycleObservers(),
49   mVectorAnimationThread( nullptr ),
50   mProcessorRegistered( false )
51 {
52 }
53
54 VectorAnimationManager::~VectorAnimationManager()
55 {
56   for( auto&& iter : mEventCallbacks )
57   {
58     delete iter;
59   }
60   mEventCallbacks.clear();
61
62   if( mProcessorRegistered )
63   {
64     Adaptor::Get().UnregisterProcessor( *this );
65   }
66
67   for( auto observer : mLifecycleObservers )
68   {
69     observer->VectorAnimationManagerDestroyed();
70   }
71 }
72
73 void VectorAnimationManager::AddObserver( VectorAnimationManager::LifecycleObserver& observer )
74 {
75   DALI_ASSERT_DEBUG( mLifecycleObservers.end() == std::find( mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer));
76   mLifecycleObservers.push_back( &observer );
77 }
78
79 void VectorAnimationManager::RemoveObserver( VectorAnimationManager::LifecycleObserver& observer)
80 {
81   auto iterator=std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer);
82   if( iterator != mLifecycleObservers.end() )
83   {
84     mLifecycleObservers.erase(iterator);
85   }
86 }
87
88 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
89 {
90   if( !mVectorAnimationThread )
91   {
92     mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
93     mVectorAnimationThread->Start();
94   }
95   return *mVectorAnimationThread;
96 }
97
98 void VectorAnimationManager::RegisterEventCallback( CallbackBase* callback )
99 {
100   mEventCallbacks.push_back( callback );
101
102   if( !mProcessorRegistered )
103   {
104     Adaptor::Get().RegisterProcessor( *this );
105     mProcessorRegistered = true;
106   }
107 }
108
109 void VectorAnimationManager::UnregisterEventCallback( CallbackBase* callback )
110 {
111   auto iter = std::find( mEventCallbacks.begin(), mEventCallbacks.end(), callback );
112   if( iter != mEventCallbacks.end() )
113   {
114     mEventCallbacks.erase( iter );
115
116     if( mEventCallbacks.empty() )
117     {
118       if( Adaptor::IsAvailable() )
119       {
120         Adaptor::Get().UnregisterProcessor( *this );
121         mProcessorRegistered = false;
122       }
123     }
124   }
125 }
126
127 void VectorAnimationManager::Process()
128 {
129   for( auto&& iter : mEventCallbacks )
130   {
131     CallbackBase::Execute( *iter );
132     delete iter;
133   }
134   mEventCallbacks.clear();
135
136   Adaptor::Get().UnregisterProcessor( *this );
137   mProcessorRegistered = false;
138 }
139
140 } // namespace Internal
141
142 } // namespace Toolkit
143
144 } // namespace Dali