Add APIs of webview context
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-manager.cpp
1 /*
2  * Copyright (c) 2021 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/adaptor-framework/adaptor.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
38 #endif
39
40 } // unnamed namespace
41
42 VectorAnimationManager::VectorAnimationManager()
43 : mEventCallbacks(),
44   mLifecycleObservers(),
45   mVectorAnimationThread(nullptr),
46   mProcessorRegistered(false)
47 {
48 }
49
50 VectorAnimationManager::~VectorAnimationManager()
51 {
52   for(auto&& iter : mEventCallbacks)
53   {
54     delete iter;
55   }
56   mEventCallbacks.clear();
57
58   if(mProcessorRegistered)
59   {
60     Adaptor::Get().UnregisterProcessor(*this);
61   }
62
63   for(auto observer : mLifecycleObservers)
64   {
65     observer->VectorAnimationManagerDestroyed();
66   }
67 }
68
69 void VectorAnimationManager::AddObserver(VectorAnimationManager::LifecycleObserver& observer)
70 {
71   DALI_ASSERT_DEBUG(mLifecycleObservers.end() == std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer));
72   mLifecycleObservers.push_back(&observer);
73 }
74
75 void VectorAnimationManager::RemoveObserver(VectorAnimationManager::LifecycleObserver& observer)
76 {
77   auto iterator = std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer);
78   if(iterator != mLifecycleObservers.end())
79   {
80     mLifecycleObservers.erase(iterator);
81   }
82 }
83
84 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
85 {
86   if(!mVectorAnimationThread)
87   {
88     mVectorAnimationThread = std::unique_ptr<VectorAnimationThread>(new VectorAnimationThread());
89     mVectorAnimationThread->Start();
90   }
91   return *mVectorAnimationThread;
92 }
93
94 void VectorAnimationManager::RegisterEventCallback(CallbackBase* callback)
95 {
96   mEventCallbacks.push_back(callback);
97
98   if(!mProcessorRegistered)
99   {
100     Adaptor::Get().RegisterProcessor(*this);
101     mProcessorRegistered = true;
102   }
103 }
104
105 void VectorAnimationManager::UnregisterEventCallback(CallbackBase* callback)
106 {
107   auto iter = std::find(mEventCallbacks.begin(), mEventCallbacks.end(), callback);
108   if(iter != mEventCallbacks.end())
109   {
110     mEventCallbacks.erase(iter);
111
112     if(mEventCallbacks.empty())
113     {
114       if(Adaptor::IsAvailable())
115       {
116         Adaptor::Get().UnregisterProcessor(*this);
117         mProcessorRegistered = false;
118       }
119     }
120   }
121 }
122
123 void VectorAnimationManager::Process()
124 {
125   for(auto&& iter : mEventCallbacks)
126   {
127     CallbackBase::Execute(*iter);
128     delete iter;
129   }
130   mEventCallbacks.clear();
131
132   Adaptor::Get().UnregisterProcessor(*this);
133   mProcessorRegistered = false;
134 }
135
136 } // namespace Internal
137
138 } // namespace Toolkit
139
140 } // namespace Dali