[dali_2.3.25] 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) 2024 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   mVectorAnimationThread(nullptr),
48   mProcessorRegistered(false)
49 {
50 }
51
52 VectorAnimationManager::~VectorAnimationManager()
53 {
54   mEventCallbacks.Clear();
55
56   if(mProcessorRegistered && Adaptor::IsAvailable())
57   {
58     Adaptor::Get().UnregisterProcessor(*this, true);
59   }
60 }
61
62 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
63 {
64   if(!mVectorAnimationThread)
65   {
66     mVectorAnimationThread = std::unique_ptr<VectorAnimationThread>(new VectorAnimationThread());
67     mVectorAnimationThread->Start();
68   }
69   return *mVectorAnimationThread;
70 }
71
72 void VectorAnimationManager::RegisterEventCallback(CallbackBase* callback)
73 {
74   mEventCallbacks.PushBack(callback); ///< Take ownership of callback.
75
76   if(!mProcessorRegistered)
77   {
78     Adaptor::Get().RegisterProcessor(*this, true); // Use post processor to trigger after layoutting
79     mProcessorRegistered = true;
80   }
81 }
82
83 void VectorAnimationManager::UnregisterEventCallback(CallbackBase* callback)
84 {
85   auto iter = mEventCallbacks.Find(callback);
86   if(iter != mEventCallbacks.End())
87   {
88     mEventCallbacks.Erase(iter);
89
90     if(mEventCallbacks.Count() == 0u)
91     {
92       if(Adaptor::IsAvailable())
93       {
94         Adaptor::Get().UnregisterProcessor(*this, true);
95         mProcessorRegistered = false;
96       }
97     }
98   }
99 }
100
101 void VectorAnimationManager::Process(bool postProcessor)
102 {
103 #ifdef TRACE_ENABLED
104   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
105   {
106     if(mEventCallbacks.Count() > 0u)
107     {
108       std::ostringstream oss;
109       oss << "[" << mEventCallbacks.Count() << "]";
110       DALI_TRACE_BEGIN_WITH_MESSAGE(gTraceFilter, "DALI_VECTOR_ANIMATION_MANAGER_PROCESS", oss.str().c_str());
111     }
112   }
113 #endif
114
115   for(auto&& iter : mEventCallbacks)
116   {
117     CallbackBase::Execute(*iter);
118   }
119
120 #ifdef TRACE_ENABLED
121   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
122   {
123     if(mEventCallbacks.Count() > 0u)
124     {
125       std::ostringstream oss;
126       oss << "[" << mEventCallbacks.Count() << "]";
127       DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_VECTOR_ANIMATION_MANAGER_PROCESS", oss.str().c_str());
128     }
129   }
130 #endif
131   mEventCallbacks.Clear();
132
133   Adaptor::Get().UnregisterProcessor(*this, true);
134   mProcessorRegistered = false;
135 }
136
137 } // namespace Internal
138
139 } // namespace Toolkit
140
141 } // namespace Dali