2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/animation/animation-playlist.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/trace.h>
24 #include <dali/internal/event/animation/animation-impl.h>
25 #include <dali/internal/update/animation/scene-graph-animation.h>
26 #include <dali/public-api/common/vector-wrapper.h>
36 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
39 uint64_t GetNanoseconds()
41 // Get the time of a monotonic clock since its epoch.
42 auto epoch = std::chrono::steady_clock::now().time_since_epoch();
44 auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
46 return static_cast<uint64_t>(duration.count());
55 AnimationPlaylist* AnimationPlaylist::New()
57 return new AnimationPlaylist();
60 AnimationPlaylist::AnimationPlaylist() = default;
62 AnimationPlaylist::~AnimationPlaylist() = default;
64 void AnimationPlaylist::AnimationCreated(Animation& animation)
66 mAnimations.PushBack(&animation);
69 void AnimationPlaylist::AnimationDestroyed(Animation& animation)
71 auto iter = mAnimations.Find(&animation);
72 DALI_ASSERT_ALWAYS(iter != mAnimations.End() && "Animation not found");
74 mAnimations.Erase(iter);
77 void AnimationPlaylist::OnPlay(Animation& animation)
79 Dali::Animation handle = Dali::Animation(&animation);
80 auto iter = mPlaylist.lower_bound(handle);
81 if(iter != mPlaylist.end() && (*iter).first == handle)
83 // Just increase reference count.
88 mPlaylist.insert(iter, {handle, 1u});
92 void AnimationPlaylist::OnClear(Animation& animation)
94 Dali::Animation handle = Dali::Animation(&animation);
95 auto iter = mPlaylist.find(handle);
97 // Animation might be removed when NotifyCompleted called.
98 if(DALI_LIKELY(iter != mPlaylist.end()))
100 // Just decrease reference count. But if reference count is zero, remove it.
101 if(--(iter->second) == 0u)
103 mPlaylist.erase(iter);
108 void AnimationPlaylist::NotifyProgressReached(NotifierInterface::NotifyId notifyId)
110 Dali::Animation handle; // Will own handle until all emits have been done.
112 auto* animation = GetEventObject(notifyId);
113 if(DALI_LIKELY(animation))
115 // Check if this animation hold inputed scenegraph animation.
116 DALI_ASSERT_DEBUG(animation->GetSceneObject()->GetNotifyId() == notifyId);
118 handle = Dali::Animation(animation);
119 animation->EmitSignalProgressReached();
123 void AnimationPlaylist::NotifyCompleted(CompleteNotificationInterface::ParameterList notifierIdList)
125 std::vector<Dali::Animation> finishedAnimations; // Will own handle until all emits have been done.
128 std::vector<std::pair<uint64_t, uint32_t>> animationFinishedTimeChecker;
134 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_ANIMATION_FINISHED", [&](std::ostringstream& oss) {
135 oss << "[n:" << notifierIdList.Count() << "]";
138 for(const auto& notifierId : notifierIdList)
140 auto* animation = GetEventObject(notifierId);
141 if(DALI_LIKELY(animation))
143 // Check if this animation hold inputed scenegraph animation.
144 DALI_ASSERT_DEBUG(animation->GetSceneObject()->GetNotifyId() == notifierId);
146 // Update loop count. And check whether animation was finished or not.
147 if(animation->HasFinished())
149 finishedAnimations.push_back(Dali::Animation(animation));
151 // The animation may be present in mPlaylist - remove if necessary
152 // Note that the animation "Finish" signal is emitted after Stop() has been called
158 // Now it's safe to emit the signals
159 for(auto& animation : finishedAnimations)
162 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
164 start = GetNanoseconds();
167 GetImplementation(animation).EmitSignalFinish();
169 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
171 end = GetNanoseconds();
172 animationFinishedTimeChecker.emplace_back(end - start, GetImplementation(animation).GetSceneObject()->GetNotifyId());
177 DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_ANIMATION_FINISHED", [&](std::ostringstream& oss) {
178 oss << "[f:" << finishedAnimations.size() << ",";
180 std::sort(animationFinishedTimeChecker.rbegin(), animationFinishedTimeChecker.rend());
181 auto topCount = std::min(5u, static_cast<uint32_t>(animationFinishedTimeChecker.size()));
183 oss << "top" << topCount;
184 for(auto i = 0u; i < topCount; ++i)
186 oss << "(" << static_cast<float>(animationFinishedTimeChecker[i].first) / 1000000.0f << "ms,";
187 oss << animationFinishedTimeChecker[i].second << ")";
193 uint32_t AnimationPlaylist::GetAnimationCount()
195 return mAnimations.Count();
198 Dali::Animation AnimationPlaylist::GetAnimationAt(uint32_t index)
200 if(index >= mAnimations.Count())
202 DALI_LOG_ERROR("Animation index is out of bounds.\n");
203 return Dali::Animation();
206 // This will spend a lot of time. But GetAnimationAt API will be called very rarely.
207 Animation* ret = nullptr;
208 for(auto iter : mAnimations)
217 DALI_ASSERT_DEBUG(ret != nullptr);
218 return Dali::Animation(ret);
221 } // namespace Internal