[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-manager.cpp
1 /*
2  * Copyright (c) 2023 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 #include <dali/integration-api/trace.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 namespace
36 {
37 #if defined(DEBUG_ENABLED)
38 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
39 #endif
40
41 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
42
43 } // unnamed namespace
44
45 VectorAnimationManager::VectorAnimationManager()
46 : mEventCallbacks(),
47   mLifecycleObservers(),
48   mVectorAnimationThread(nullptr),
49   mProcessorRegistered(false)
50 {
51 }
52
53 VectorAnimationManager::~VectorAnimationManager()
54 {
55   mEventCallbacks.clear();
56
57   if(mProcessorRegistered && Adaptor::IsAvailable())
58   {
59     Adaptor::Get().UnregisterProcessor(*this, true);
60   }
61
62   for(auto observer : mLifecycleObservers)
63   {
64     observer->VectorAnimationManagerDestroyed();
65   }
66 }
67
68 void VectorAnimationManager::AddObserver(VectorAnimationManager::LifecycleObserver& observer)
69 {
70   DALI_ASSERT_DEBUG(mLifecycleObservers.end() == std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer));
71   mLifecycleObservers.push_back(&observer);
72 }
73
74 void VectorAnimationManager::RemoveObserver(VectorAnimationManager::LifecycleObserver& observer)
75 {
76   auto iterator = std::find(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer);
77   if(iterator != mLifecycleObservers.end())
78   {
79     mLifecycleObservers.erase(iterator);
80   }
81 }
82
83 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
84 {
85   if(!mVectorAnimationThread)
86   {
87     mVectorAnimationThread = std::unique_ptr<VectorAnimationThread>(new VectorAnimationThread());
88     mVectorAnimationThread->Start();
89   }
90   return *mVectorAnimationThread;
91 }
92
93 void VectorAnimationManager::RegisterEventCallback(CallbackBase* callback)
94 {
95   mEventCallbacks.emplace_back(std::unique_ptr<Dali::CallbackBase>(callback));
96
97   if(!mProcessorRegistered)
98   {
99     Adaptor::Get().RegisterProcessor(*this, true); // Use post processor to trigger after layoutting
100     mProcessorRegistered = true;
101   }
102 }
103
104 void VectorAnimationManager::UnregisterEventCallback(CallbackBase* callback)
105 {
106   auto iter = std::find_if(mEventCallbacks.begin(),
107                            mEventCallbacks.end(),
108                            [callback](const std::unique_ptr<CallbackBase>& element) {
109                              return element.get() == callback;
110                            });
111   if(iter != mEventCallbacks.end())
112   {
113     mEventCallbacks.erase(iter);
114
115     if(mEventCallbacks.empty())
116     {
117       if(Adaptor::IsAvailable())
118       {
119         Adaptor::Get().UnregisterProcessor(*this, true);
120         mProcessorRegistered = false;
121       }
122     }
123   }
124 }
125
126 void VectorAnimationManager::Process(bool postProcessor)
127 {
128 #ifdef TRACE_ENABLED
129   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
130   {
131     if(mEventCallbacks.size() > 0u)
132     {
133       std::ostringstream oss;
134       oss << "[" << mEventCallbacks.size() << "]";
135       DALI_TRACE_BEGIN_WITH_MESSAGE(gTraceFilter, "DALI_VECTOR_ANIMATION_MANAGER_PROCESS", oss.str().c_str());
136     }
137   }
138 #endif
139
140   for(auto&& iter : mEventCallbacks)
141   {
142     CallbackBase::Execute(*iter);
143   }
144
145 #ifdef TRACE_ENABLED
146   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
147   {
148     if(mEventCallbacks.size() > 0u)
149     {
150       std::ostringstream oss;
151       oss << "[" << mEventCallbacks.size() << "]";
152       DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_VECTOR_ANIMATION_MANAGER_PROCESS", oss.str().c_str());
153     }
154   }
155 #endif
156   mEventCallbacks.clear();
157
158   Adaptor::Get().UnregisterProcessor(*this, true);
159   mProcessorRegistered = false;
160 }
161
162 } // namespace Internal
163
164 } // namespace Toolkit
165
166 } // namespace Dali