/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
namespace
{
constexpr auto LOOP_FOREVER = -1;
-constexpr auto NANOSECONDS_PER_SECOND(1e+9);
+constexpr auto MICROSECONDS_PER_SECOND(1e+6);
#if defined(DEBUG_ENABLED)
Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
} // unnamed namespace
-VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache, const std::string& url)
-: mUrl(url),
- mVectorRenderer(),
+VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
+: mUrl(),
+ mVectorRenderer(VectorAnimationRenderer::New()),
mAnimationData(),
mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
mConditionalWait(),
+ mResourceReadySignal(),
mAnimationFinishedTrigger(),
+ mLoadCompletedTrigger(new EventThreadCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted))),
mPlayState(PlayState::STOPPED),
mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
mNextFrameStartTime(),
- mFrameDurationNanoSeconds(0),
+ mFrameDurationMicroSeconds(MICROSECONDS_PER_SECOND / 60.0f),
mFrameRate(60.0f),
mCurrentFrame(0),
mTotalFrame(0),
mStartFrame(0),
mEndFrame(0),
+ mDroppedFrames(0),
mWidth(0),
mHeight(0),
mAnimationDataIndex(0),
mUpdateFrameNumber(false),
mNeedAnimationFinishedTrigger(true),
mAnimationDataUpdated(false),
- mDestroyTask(false)
+ mDestroyTask(false),
+ mLoadRequest(false),
+ mLoadFailed(false)
{
- Initialize();
+ mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
}
VectorAnimationTask::~VectorAnimationTask()
mDestroyTask = true;
}
+bool VectorAnimationTask::Load()
+{
+ if(!mVectorRenderer.Load(mUrl))
+ {
+ DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mUrl.c_str());
+ mLoadRequest = false;
+ mLoadFailed = true;
+ mLoadCompletedTrigger->Trigger();
+ return false;
+ }
+
+ mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
+
+ mEndFrame = mTotalFrame - 1;
+
+ mFrameRate = mVectorRenderer.GetFrameRate();
+ mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
+
+ mLoadRequest = false;
+ mLoadCompletedTrigger->Trigger();
+
+ DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
+
+ return true;
+}
+
void VectorAnimationTask::SetRenderer(Renderer renderer)
{
ConditionalWait::ScopedLock lock(mConditionalWait);
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
}
+void VectorAnimationTask::RequestLoad(const std::string& url)
+{
+ mUrl = url;
+ mLoadRequest = true;
+
+ mVectorAnimationThread.AddTask(this);
+}
+
void VectorAnimationTask::SetAnimationData(const AnimationData& data)
{
ConditionalWait::ScopedLock lock(mConditionalWait);
std::string marker;
if(playRange.GetElementAt(0).Get(marker))
{
- if(mVectorRenderer)
+ if(mVectorRenderer && mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame))
{
- mVectorRenderer.GetMarkerInfo(marker, startFrame, endFrame);
valid = true;
}
}
mCurrentFrame = mEndFrame;
}
- DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%p]\n", mStartFrame, mEndFrame, this);
+ DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mUrl.c_str(), this);
}
}
else
{
- DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%p]\n", startFrame, endFrame, this);
+ DALI_LOG_ERROR("VectorAnimationTask::SetPlayRange: Invalid range (%d, %d) [%s] [%p]\n", startFrame, endFrame, mUrl.c_str(), this);
return;
}
}
}
else
{
- DALI_LOG_ERROR("Invalid frame number [%d (%d, %d)]\n", frameNumber, mStartFrame, mEndFrame);
+ DALI_LOG_ERROR("Invalid frame number [%d (%d, %d)] [%p]\n", frameNumber, mStartFrame, mEndFrame, this);
}
}
mVectorRenderer.GetLayerInfo(map);
}
-VectorAnimationTask::UploadCompletedSignalType& VectorAnimationTask::UploadCompletedSignal()
+VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReadySignal()
{
- return mVectorRenderer.UploadCompletedSignal();
-}
-
-void VectorAnimationTask::Initialize()
-{
- mVectorRenderer = VectorAnimationRenderer::New(mUrl);
-
- mTotalFrame = mVectorRenderer.GetTotalFrameNumber();
-
- mEndFrame = mTotalFrame - 1;
-
- mFrameRate = mVectorRenderer.GetFrameRate();
- mFrameDurationNanoSeconds = NANOSECONDS_PER_SECOND / mFrameRate;
-
- uint32_t width, height;
- mVectorRenderer.GetDefaultSize(width, height);
-
- SetSize(width, height);
-
- DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Initialize: file = %s [%d frames, %f fps] [%p]\n", mUrl.c_str(), mTotalFrame, mFrameRate, this);
+ return mResourceReadySignal;
}
-bool VectorAnimationTask::Rasterize()
+bool VectorAnimationTask::Rasterize(bool& keepAnimation)
{
bool stopped = false;
uint32_t currentFrame;
+ keepAnimation = false;
{
ConditionalWait::ScopedLock lock(mConditionalWait);
// The task will be destroyed. We don't need rasterization.
return false;
}
+
+ if(mLoadRequest)
+ {
+ return Load();
+ }
+ }
+
+ if(mLoadFailed)
+ {
+ return false;
}
ApplyAnimationData();
if(mPlayState == PlayState::PLAYING && mUpdateFrameNumber)
{
- mCurrentFrame = mForward ? mCurrentFrame + 1 : mCurrentFrame - 1;
+ mCurrentFrame = mForward ? mCurrentFrame + mDroppedFrames + 1 : (mCurrentFrame > mDroppedFrames ? mCurrentFrame - mDroppedFrames - 1 : 0);
Dali::ClampInPlace(mCurrentFrame, mStartFrame, mEndFrame);
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this);
}
- bool keepAnimation = true;
- if(mPlayState == PlayState::PAUSED || mPlayState == PlayState::STOPPED)
+ if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
{
- keepAnimation = false;
+ keepAnimation = true;
}
- return keepAnimation;
+ return true;
}
uint32_t VectorAnimationTask::GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame)
return frame;
}
-std::chrono::time_point<std::chrono::system_clock> VectorAnimationTask::CalculateNextFrameTime(bool renderNow)
+VectorAnimationTask::TimePoint VectorAnimationTask::CalculateNextFrameTime(bool renderNow)
{
- // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::system_clock supported
+ // std::chrono::time_point template has second parameter duration which defaults to the std::chrono::steady_clock supported
// duration. In some C++11 implementations it is a milliseconds duration, so it fails to compile unless mNextFrameStartTime
// is casted to use the default duration.
- mNextFrameStartTime = std::chrono::time_point_cast<std::chrono::time_point<std::chrono::system_clock>::duration>(
- mNextFrameStartTime + std::chrono::nanoseconds(mFrameDurationNanoSeconds));
- auto current = std::chrono::system_clock::now();
- if(renderNow || mNextFrameStartTime < current)
+ mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
+ auto current = std::chrono::steady_clock::now();
+ mDroppedFrames = 0;
+
+ if(renderNow)
{
mNextFrameStartTime = current;
}
+ else if(mNextFrameStartTime < current)
+ {
+ uint32_t droppedFrames = 0;
+
+ while(current > std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds)) && droppedFrames < mTotalFrame)
+ {
+ droppedFrames++;
+ mNextFrameStartTime = std::chrono::time_point_cast<TimePoint::duration>(mNextFrameStartTime + std::chrono::microseconds(mFrameDurationMicroSeconds));
+ }
+
+ mNextFrameStartTime = current;
+ mDroppedFrames = droppedFrames;
+ }
+
return mNextFrameStartTime;
}
-std::chrono::time_point<std::chrono::system_clock> VectorAnimationTask::GetNextFrameTime()
+VectorAnimationTask::TimePoint VectorAnimationTask::GetNextFrameTime()
{
return mNextFrameStartTime;
}
SetCurrentFrameNumber(mAnimationData[index].currentFrame);
}
+ if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
+ {
+ mVectorRenderer.InvalidateBuffer();
+ }
+
if(mAnimationData[index].resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
{
if(mAnimationData[index].playState == DevelImageVisual::PlayState::PLAYING)
mAnimationData[index].resendFlag = 0;
}
+void VectorAnimationTask::OnUploadCompleted()
+{
+ mResourceReadySignal.Emit(ResourceStatus::READY);
+}
+
+void VectorAnimationTask::OnLoadCompleted()
+{
+ if(!mLoadFailed)
+ {
+ mResourceReadySignal.Emit(ResourceStatus::LOADED);
+ }
+ else
+ {
+ mResourceReadySignal.Emit(ResourceStatus::FAILED);
+ }
+}
} // namespace Internal
} // namespace Toolkit