2 * Copyright (c) 2022 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-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/math/math-utils.h>
24 #include <dali/public-api/object/property-array.h>
27 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
28 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
29 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
39 constexpr auto LOOP_FOREVER = -1;
40 constexpr auto MICROSECONDS_PER_SECOND(1e+6);
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
46 } // unnamed namespace
48 VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
50 mVectorRenderer(VectorAnimationRenderer::New()),
52 mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
54 mResourceReadySignal(),
55 mAnimationFinishedTrigger(),
56 mLoadCompletedTrigger(new EventThreadCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted))),
57 mPlayState(PlayState::STOPPED),
58 mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
59 mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
60 mNextFrameStartTime(),
61 mFrameDurationMicroSeconds(MICROSECONDS_PER_SECOND / 60.0f),
70 mAnimationDataIndex(0),
71 mLoopCount(LOOP_FOREVER),
74 mUpdateFrameNumber(false),
75 mNeedAnimationFinishedTrigger(true),
76 mAnimationDataUpdated(false),
81 mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
84 VectorAnimationTask::~VectorAnimationTask()
86 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this);
89 void VectorAnimationTask::Finalize()
91 ConditionalWait::ScopedLock lock(mConditionalWait);
93 // Release some objects in the main thread
94 if(mAnimationFinishedTrigger)
96 mAnimationFinishedTrigger.reset();
99 mVectorRenderer.Finalize();
104 bool VectorAnimationTask::Load()
106 if(!mVectorRenderer.Load(mUrl))
108 DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
109 mLoadRequest = false;
111 mLoadCompletedTrigger->Trigger();
115 mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
117 mEndFrame = mTotalFrame - 1;
119 mFrameRate = mVectorRenderer.GetFrameRate();
120 mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
122 mLoadRequest = false;
123 mLoadCompletedTrigger->Trigger();
125 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
130 void VectorAnimationTask::SetRenderer(Renderer renderer)
132 ConditionalWait::ScopedLock lock(mConditionalWait);
134 mVectorRenderer.SetRenderer(renderer);
136 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
139 void VectorAnimationTask::RequestLoad(const std::string& url)
144 mVectorAnimationThread.AddTask(this);
147 void VectorAnimationTask::SetAnimationData(const AnimationData& data)
149 ConditionalWait::ScopedLock lock(mConditionalWait);
151 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this);
153 uint32_t index = mAnimationDataIndex == 0 ? 1 : 0; // Use the other buffer
155 mAnimationData[index] = data;
156 mAnimationDataUpdated = true;
158 if(data.resendFlag & VectorAnimationTask::RESEND_SIZE)
160 // The size should be changed in the main thread.
161 SetSize(data.width, data.height);
164 mVectorAnimationThread.AddTask(this);
167 void VectorAnimationTask::SetSize(uint32_t width, uint32_t height)
169 if(mWidth != width || mHeight != height)
171 mVectorRenderer.SetSize(width, height);
176 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this);
180 void VectorAnimationTask::PlayAnimation()
182 if(mPlayState != PlayState::PLAYING)
184 mNeedAnimationFinishedTrigger = true;
185 mUpdateFrameNumber = false;
186 mPlayState = PlayState::PLAYING;
188 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PlayAnimation: Play [%p]\n", this);
192 void VectorAnimationTask::StopAnimation()
194 if(mPlayState != PlayState::STOPPING)
196 mNeedAnimationFinishedTrigger = false;
197 mPlayState = PlayState::STOPPING;
199 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::StopAnimation: Stop [%p]\n", this);
203 void VectorAnimationTask::PauseAnimation()
205 if(mPlayState == PlayState::PLAYING)
207 mPlayState = PlayState::PAUSED;
209 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this);
213 void VectorAnimationTask::SetAnimationFinishedCallback(EventThreadCallback* callback)
215 ConditionalWait::ScopedLock lock(mConditionalWait);
218 mAnimationFinishedTrigger = std::unique_ptr<EventThreadCallback>(callback);
222 void VectorAnimationTask::SetLoopCount(int32_t count)
224 if(mLoopCount != count)
229 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopCount: [%d] [%p]\n", count, this);
233 void VectorAnimationTask::SetPlayRange(const Property::Array& playRange)
236 uint32_t startFrame = 0, endFrame = 0;
237 size_t count = playRange.Count();
241 int32_t start = 0, end = 0;
242 if(playRange.GetElementAt(0).Get(start) && playRange.GetElementAt(1).Get(end))
244 startFrame = static_cast<uint32_t>(start);
245 endFrame = static_cast<uint32_t>(end);
250 std::string startMarker, endMarker;
251 if(playRange.GetElementAt(0).Get(startMarker) && playRange.GetElementAt(1).Get(endMarker))
255 uint32_t frame; // We don't use this later
256 if(mVectorRenderer.GetMarkerInfo(startMarker, startFrame, frame) && mVectorRenderer.GetMarkerInfo(endMarker, frame, endFrame))
267 if(playRange.GetElementAt(0).Get(marker))
269 if(mVectorRenderer && mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame))
278 DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range [%p]\n", this);
282 // Make sure the range specified is between 0 and the total frame number
283 if(startFrame < mTotalFrame && endFrame < mTotalFrame)
285 // If the range is not in order swap values
286 if(startFrame > endFrame)
288 uint32_t temp = startFrame;
289 startFrame = endFrame;
293 if(startFrame != mStartFrame || endFrame != mEndFrame)
295 mStartFrame = startFrame;
296 mEndFrame = endFrame;
298 // If the current frame is out of the range, change the current frame also.
299 if(mStartFrame > mCurrentFrame)
301 mCurrentFrame = mStartFrame;
303 else if(mEndFrame < mCurrentFrame)
305 mCurrentFrame = mEndFrame;
308 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mUrl.c_str(), this);
313 DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%s] [%p]\n", startFrame, endFrame, mUrl.c_str(), this);
318 void VectorAnimationTask::GetPlayRange(uint32_t& startFrame, uint32_t& endFrame)
320 startFrame = mStartFrame;
321 endFrame = mEndFrame;
324 void VectorAnimationTask::SetCurrentFrameNumber(uint32_t frameNumber)
326 if(mCurrentFrame == frameNumber)
328 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: Set same frame [%d] [%p]\n", frameNumber, this);
332 if(frameNumber >= mStartFrame && frameNumber <= mEndFrame)
334 mCurrentFrame = frameNumber;
335 mUpdateFrameNumber = false;
337 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this);
341 DALI_LOG_ERROR("Invalid frame number [%d (%d, %d)] [%p]\n", frameNumber, mStartFrame, mEndFrame, this);
345 uint32_t VectorAnimationTask::GetCurrentFrameNumber() const
347 return mCurrentFrame;
350 uint32_t VectorAnimationTask::GetTotalFrameNumber() const
355 void VectorAnimationTask::GetDefaultSize(uint32_t& width, uint32_t& height) const
357 mVectorRenderer.GetDefaultSize(width, height);
360 void VectorAnimationTask::SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior)
362 mStopBehavior = stopBehavior;
364 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetStopBehavior: stop behavor = %d [%p]\n", mStopBehavior, this);
367 void VectorAnimationTask::SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode)
369 mLoopingMode = loopingMode;
371 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetLoopingMode: looping mode = %d [%p]\n", mLoopingMode, this);
374 void VectorAnimationTask::GetLayerInfo(Property::Map& map) const
376 mVectorRenderer.GetLayerInfo(map);
379 VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReadySignal()
381 return mResourceReadySignal;
384 bool VectorAnimationTask::Rasterize(bool& keepAnimation)
386 bool stopped = false;
387 uint32_t currentFrame;
388 keepAnimation = false;
391 ConditionalWait::ScopedLock lock(mConditionalWait);
394 // The task will be destroyed. We don't need rasterization.
400 bool result = Load();
413 ApplyAnimationData();
415 if(mPlayState == PlayState::PLAYING && mUpdateFrameNumber)
417 mCurrentFrame = mForward ? mCurrentFrame + mDroppedFrames + 1 : (mCurrentFrame > mDroppedFrames ? mCurrentFrame - mDroppedFrames - 1 : 0);
418 Dali::ClampInPlace(mCurrentFrame, mStartFrame, mEndFrame);
421 currentFrame = mCurrentFrame;
423 mUpdateFrameNumber = true;
425 if(mPlayState == PlayState::STOPPING)
427 mCurrentFrame = GetStoppedFrame(mStartFrame, mEndFrame, mCurrentFrame);
428 currentFrame = mCurrentFrame;
431 else if(mPlayState == PlayState::PLAYING)
433 bool animationFinished = false;
435 if(currentFrame >= mEndFrame) // last frame
437 if(mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE)
443 if(mLoopCount < 0 || ++mCurrentLoop < mLoopCount) // repeat forever or before the last loop
445 mCurrentFrame = mStartFrame;
446 mUpdateFrameNumber = false;
450 animationFinished = true; // end of animation
454 else if(currentFrame == mStartFrame && !mForward) // first frame
456 if(mLoopCount < 0 || ++mCurrentLoop < mLoopCount) // repeat forever or before the last loop
462 animationFinished = true; // end of animation
466 if(animationFinished)
468 if(mStopBehavior == DevelImageVisual::StopBehavior::CURRENT_FRAME)
474 mPlayState = PlayState::STOPPING;
480 bool renderSuccess = false;
483 renderSuccess = mVectorRenderer.Render(currentFrame);
486 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Rendering failed. Try again later.[%d] [%p]\n", currentFrame, this);
487 mUpdateFrameNumber = false;
491 if(stopped && renderSuccess)
493 mPlayState = PlayState::STOPPED;
497 // Animation is finished
499 ConditionalWait::ScopedLock lock(mConditionalWait);
500 if(mNeedAnimationFinishedTrigger && mAnimationFinishedTrigger)
502 mAnimationFinishedTrigger->Trigger();
506 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this);
509 if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
511 keepAnimation = true;
517 uint32_t VectorAnimationTask::GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame)
519 uint32_t frame = currentFrame;
521 switch(mStopBehavior)
523 case DevelImageVisual::StopBehavior::FIRST_FRAME:
528 case DevelImageVisual::StopBehavior::LAST_FRAME:
530 if(mLoopingMode == DevelImageVisual::LoopingMode::AUTO_REVERSE)
540 case DevelImageVisual::StopBehavior::CURRENT_FRAME:
542 frame = currentFrame;
550 VectorAnimationTask::TimePoint VectorAnimationTask::CalculateNextFrameTime(bool renderNow)
552 // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::steady_clock supported
553 // duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
554 // is casted to use the default duration.
555 mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
556 auto current = std::chrono::steady_clock::now();
561 mNextFrameStartTime = current;
563 else if(mNextFrameStartTime < current)
565 uint32_t droppedFrames = 0;
567 while(current > std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds)) && droppedFrames < mTotalFrame)
570 mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
573 mNextFrameStartTime = current;
574 mDroppedFrames = droppedFrames;
577 return mNextFrameStartTime;
580 VectorAnimationTask::TimePoint VectorAnimationTask::GetNextFrameTime()
582 return mNextFrameStartTime;
585 void VectorAnimationTask::ApplyAnimationData()
590 ConditionalWait::ScopedLock lock(mConditionalWait);
592 if(!mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].resendFlag != 0)
594 // Data is not updated or the previous data is not applied yet.
598 mAnimationDataIndex = mAnimationDataIndex == 0 ? 1 : 0; // Swap index
599 mAnimationDataUpdated = false;
601 index = mAnimationDataIndex;
604 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOP_COUNT)
606 SetLoopCount(mAnimationData[index].loopCount);
609 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_RANGE)
611 SetPlayRange(mAnimationData[index].playRange);
614 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_STOP_BEHAVIOR)
616 SetStopBehavior(mAnimationData[index].stopBehavior);
619 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_LOOPING_MODE)
621 SetLoopingMode(mAnimationData[index].loopingMode);
624 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_CURRENT_FRAME)
626 SetCurrentFrameNumber(mAnimationData[index].currentFrame);
629 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
631 mVectorRenderer.InvalidateBuffer();
634 if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
636 if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
640 else if(mAnimationData[index].playState == DevelImageVisual::PlayState::PAUSED)
644 else if(mAnimationData[index].playState == DevelImageVisual::PlayState::STOPPED)
650 mAnimationData[index].resendFlag = 0;
653 void VectorAnimationTask::OnUploadCompleted()
655 mResourceReadySignal.Emit(true);
658 void VectorAnimationTask::OnLoadCompleted()
662 if(mWidth == 0 && mHeight == 0)
664 uint32_t width, height;
665 mVectorRenderer.GetDefaultSize(width, height);
667 SetSize(width, height);
669 mVectorAnimationThread.AddTask(this);
675 mResourceReadySignal.Emit(false);
678 } // namespace Internal
680 } // namespace Toolkit