/*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
#include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
// EXTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-loader.h>
#include <dali/integration-api/debug.h>
#include <dali/integration-api/trace.h>
#include <dali/public-api/math/math-utils.h>
#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
#ifdef TRACE_ENABLED
+#include <chrono>
+#include <iomanip>
#include <sstream>
+#include <thread>
#endif
namespace Dali
DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
+#ifdef TRACE_ENABLED
+uint64_t GetNanoseconds()
+{
+ // Get the time of a monotonic clock since its epoch.
+ auto epoch = std::chrono::steady_clock::now().time_since_epoch();
+ auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
+ return static_cast<uint64_t>(duration.count());
+}
+#endif
+
} // unnamed namespace
VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache)
: AsyncTask(MakeCallback(this, &VectorAnimationTask::TaskCompleted), AsyncTask::PriorityType::HIGH, AsyncTask::ThreadType::WORKER_THREAD),
mImageUrl(),
+ mEncodedImageBuffer(),
mVectorRenderer(VectorAnimationRenderer::New()),
mAnimationData(),
mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()),
- mConditionalWait(),
+ mMutex(),
mResourceReadySignal(),
mLoadCompletedCallback(MakeCallback(this, &VectorAnimationTask::OnLoadCompleted)),
+ mCachedLayerInfo(),
+ mCachedMarkerInfo(),
mPlayState(PlayState::STOPPED),
mStopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
mLoopingMode(DevelImageVisual::LoopingMode::RESTART),
mWidth(0),
mHeight(0),
mAnimationDataIndex(0),
+ mAppliedPlayStateId(0u),
mLoopCount(LOOP_FOREVER),
mCurrentLoop(0),
mForward(true),
mUpdateFrameNumber(false),
mNeedAnimationFinishedTrigger(true),
+ mNeedForceRenderOnceTrigger(false),
mAnimationDataUpdated(false),
mDestroyTask(false),
mLoadRequest(false),
mLoadFailed(false),
mRasterized(false),
- mKeepAnimation(false)
+ mKeepAnimation(false),
+ mLayerInfoCached(false),
+ mMarkerInfoCached(false),
+ mEnableFrameCache(false),
+ mNotifyAfterRasterization(false),
+ mSizeUpdated(false)
{
mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted);
}
void VectorAnimationTask::Finalize()
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
-
- // Release some objects in the main thread
- if(mAnimationFinishedCallback)
{
- mVectorAnimationThread.RemoveEventTriggerCallback(mAnimationFinishedCallback.get());
- mAnimationFinishedCallback.reset();
- }
- if(mLoadCompletedCallback)
- {
- mVectorAnimationThread.RemoveEventTriggerCallback(mLoadCompletedCallback.get());
- mLoadCompletedCallback.reset();
+ Mutex::ScopedLock lock(mMutex);
+
+ // Release some objects in the main thread
+ if(mAnimationFinishedCallback)
+ {
+ mVectorAnimationThread.RemoveEventTriggerCallbacks(mAnimationFinishedCallback.get());
+ mAnimationFinishedCallback.reset();
+ }
+ if(mLoadCompletedCallback)
+ {
+ mVectorAnimationThread.RemoveEventTriggerCallbacks(mLoadCompletedCallback.get());
+ mLoadCompletedCallback.reset();
+ }
+
+ mDestroyTask = true;
}
mVectorRenderer.Finalize();
-
- mDestroyTask = true;
}
void VectorAnimationTask::TaskCompleted(VectorAnimationTaskPtr task)
bool VectorAnimationTask::Load(bool synchronousLoading)
{
#ifdef TRACE_ENABLED
+ uint64_t mStartTimeNanoSceonds = 0;
+ uint64_t mEndTimeNanoSceonds = 0;
if(gTraceFilter && gTraceFilter->IsTraceEnabled())
{
+ mStartTimeNanoSceonds = GetNanoseconds();
std::ostringstream oss;
- oss << "[url:" << mImageUrl.GetUrl() << "]";
+ oss << "[u:" << mImageUrl.GetEllipsedUrl() << "]";
// DALI_TRACE_BEGIN(gTraceFilter, "DALI_LOTTIE_LOADING_TASK"); ///< TODO : Open it if we can control trace log level
DALI_LOG_RELEASE_INFO("BEGIN: DALI_LOTTIE_LOADING_TASK %s", oss.str().c_str());
}
#endif
- if(!mVectorRenderer.Load(mImageUrl.GetUrl()))
+ if(mEncodedImageBuffer)
{
- DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mImageUrl.GetUrl().c_str());
- mLoadFailed = true;
+ if(!mVectorRenderer.Load(mEncodedImageBuffer.GetRawBuffer()))
+ {
+ mLoadFailed = true;
+ }
+
+ // We don't need to hold image buffer anymore.
+ mEncodedImageBuffer.Reset();
+ }
+ else if(mImageUrl.IsLocalResource())
+ {
+ if(!mVectorRenderer.Load(mImageUrl.GetUrl()))
+ {
+ mLoadFailed = true;
+ }
+ }
+ else
+ {
+ Dali::Vector<uint8_t> remoteData;
+ if(!Dali::FileLoader::DownloadFileSynchronously(mImageUrl.GetUrl(), remoteData) || // Failed if we fail to download json file,
+ !mVectorRenderer.Load(remoteData)) // or download data is not valid vector animation file.
+ {
+ mLoadFailed = true;
+ }
}
if(mLoadFailed)
{
+ DALI_LOG_ERROR("VectorAnimationTask::Load: Load failed [%s]\n", mImageUrl.GetUrl().c_str());
mLoadRequest = false;
- if(!synchronousLoading && mLoadCompletedCallback)
{
- mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get());
+ Mutex::ScopedLock lock(mMutex);
+ if(!synchronousLoading && mLoadCompletedCallback)
+ {
+ mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get(), 0u);
+ }
}
#ifdef TRACE_ENABLED
if(gTraceFilter && gTraceFilter->IsTraceEnabled())
{
+ mEndTimeNanoSceonds = GetNanoseconds();
std::ostringstream oss;
- oss << "[url:" << mImageUrl.GetUrl() << "]";
+ oss << std::fixed << std::setprecision(3);
+ oss << "[";
+ oss << "d:" << static_cast<float>(mEndTimeNanoSceonds - mStartTimeNanoSceonds) / 1000000.0f << "ms ";
+ oss << "u:" << mImageUrl.GetEllipsedUrl() << "]";
// DALI_TRACE_END(gTraceFilter, "DALI_LOTTIE_LOADING_TASK"); ///< TODO : Open it if we can control trace log level
DALI_LOG_RELEASE_INFO("END: DALI_LOTTIE_LOADING_TASK %s", oss.str().c_str());
}
mFrameDurationMicroSeconds = MICROSECONDS_PER_SECOND / mFrameRate;
mLoadRequest = false;
- if(!synchronousLoading && mLoadCompletedCallback)
{
- mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get());
+ Mutex::ScopedLock lock(mMutex);
+ if(!synchronousLoading && mLoadCompletedCallback)
+ {
+ mVectorAnimationThread.AddEventTriggerCallback(mLoadCompletedCallback.get(), 0u);
+ }
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Load: file = %s [%d frames, %f fps] [%p]\n", mImageUrl.GetUrl().c_str(), mTotalFrame, mFrameRate, this);
#ifdef TRACE_ENABLED
if(gTraceFilter && gTraceFilter->IsTraceEnabled())
{
+ mEndTimeNanoSceonds = GetNanoseconds();
std::ostringstream oss;
- oss << "[url:" << mImageUrl.GetUrl() << "]";
+ oss << std::fixed << std::setprecision(3);
+ oss << "[";
+ oss << "d:" << static_cast<float>(mEndTimeNanoSceonds - mStartTimeNanoSceonds) / 1000000.0f << "ms ";
+ oss << "u:" << mImageUrl.GetEllipsedUrl() << "]";
// DALI_TRACE_END(gTraceFilter, "DALI_LOTTIE_LOADING_TASK"); ///< TODO : Open it if we can control trace log level
DALI_LOG_RELEASE_INFO("END: DALI_LOTTIE_LOADING_TASK %s", oss.str().c_str());
}
void VectorAnimationTask::SetRenderer(Renderer renderer)
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
-
mVectorRenderer.SetRenderer(renderer);
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetRenderer [%p]\n", this);
}
-void VectorAnimationTask::RequestLoad(const VisualUrl& url, bool synchronousLoading)
+void VectorAnimationTask::RequestLoad(const VisualUrl& url, EncodedImageBuffer encodedImageBuffer, bool synchronousLoading)
{
- mImageUrl = url;
+ mImageUrl = url;
+ mEncodedImageBuffer = encodedImageBuffer;
if(!synchronousLoading)
{
{
Load(true);
- OnLoadCompleted();
+ OnLoadCompleted(0u);
}
}
void VectorAnimationTask::SetAnimationData(const AnimationData& data)
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
+ Mutex::ScopedLock lock(mMutex);
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetAnimationData [%p]\n", this);
mWidth = width;
mHeight = height;
+ // If fixedCache is enabled, Call KeepRasterizedBuffer()
+ if(mEnableFrameCache)
+ {
+ if(mTotalFrame > 0 && !mLoadFailed)
+ {
+ mVectorRenderer.KeepRasterizedBuffer();
+ }
+ else
+ {
+ // If Load is not yet, update the size later.
+ mSizeUpdated = true;
+ }
+ }
+
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetSize: width = %d, height = %d [%p]\n", width, height, this);
}
}
{
mPlayState = PlayState::PAUSED;
+ // Ensure to render paused frame.
+ mNeedForceRenderOnceTrigger = true;
+
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::PauseAnimation: Pause [%p]\n", this);
}
}
void VectorAnimationTask::SetAnimationFinishedCallback(CallbackBase* callback)
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
+ Mutex::ScopedLock lock(mMutex);
mAnimationFinishedCallback = std::unique_ptr<CallbackBase>(callback);
}
if(mStartFrame > mCurrentFrame)
{
mCurrentFrame = mStartFrame;
+
+ if(mPlayState != PlayState::PLAYING)
+ {
+ // Ensure to render current frame.
+ mNeedForceRenderOnceTrigger = true;
+ }
}
else if(mEndFrame < mCurrentFrame)
{
mCurrentFrame = mEndFrame;
+
+ if(mPlayState != PlayState::PLAYING)
+ {
+ // Ensure to render current frame.
+ mNeedForceRenderOnceTrigger = true;
+ }
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetPlayRange: [%d, %d] [%s] [%p]\n", mStartFrame, mEndFrame, mImageUrl.GetUrl().c_str(), this);
mCurrentFrame = frameNumber;
mUpdateFrameNumber = false;
+ if(mPlayState != PlayState::PLAYING)
+ {
+ // Ensure to render current frame.
+ mNeedForceRenderOnceTrigger = true;
+ }
+
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::SetCurrentFrameNumber: frame number = %d [%p]\n", mCurrentFrame, this);
}
else
void VectorAnimationTask::GetLayerInfo(Property::Map& map) const
{
- mVectorRenderer.GetLayerInfo(map);
+ // Fast-out if file is loading, or load failed.
+ if(mLoadFailed || IsLoadRequested())
+ {
+ return;
+ }
+
+ if(DALI_UNLIKELY(!mLayerInfoCached))
+ {
+ // Update only 1 time.
+ mLayerInfoCached = true;
+ mVectorRenderer.GetLayerInfo(mCachedLayerInfo);
+ }
+
+ map = mCachedLayerInfo;
+}
+
+void VectorAnimationTask::GetMarkerInfo(Property::Map& map) const
+{
+ // Fast-out if file is loading, or load failed.
+ if(mLoadFailed || IsLoadRequested())
+ {
+ return;
+ }
+
+ if(DALI_UNLIKELY(!mMarkerInfoCached))
+ {
+ // Update only 1 time.
+ mMarkerInfoCached = true;
+ mVectorRenderer.GetMarkerInfo(mCachedMarkerInfo);
+ }
+
+ map = mCachedMarkerInfo;
}
VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReadySignal()
mKeepAnimation = false;
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
+ Mutex::ScopedLock lock(mMutex);
if(mDestroyTask)
{
// The task will be destroyed. We don't need rasterization.
}
#ifdef TRACE_ENABLED
- if(gTraceFilter && gTraceFilter->IsTraceEnabled())
- {
- std::ostringstream oss;
- oss << "[size:" << mWidth << "x" << mHeight << " ";
- oss << "url:" << mImageUrl.GetUrl() << "]";
- DALI_TRACE_BEGIN_WITH_MESSAGE(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK", oss.str().c_str());
- }
+ uint64_t mStartTimeNanoSceonds = 0;
+ uint64_t mEndTimeNanoSceonds = 0;
#endif
+ DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK", [&](std::ostringstream& oss) {
+ mStartTimeNanoSceonds = GetNanoseconds();
+ oss << "[s:" << mWidth << "x" << mHeight << " ";
+ oss << "u:" << mImageUrl.GetEllipsedUrl() << "]";
+ });
ApplyAnimationData();
mForward = true;
mCurrentLoop = 0;
+ mNeedForceRenderOnceTrigger = true;
+
if(mVectorRenderer)
{
// Notify the Renderer that rendering is stopped.
// Animation is finished
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
+ Mutex::ScopedLock lock(mMutex);
if(mNeedAnimationFinishedTrigger && mAnimationFinishedCallback)
{
- mVectorAnimationThread.AddEventTriggerCallback(mAnimationFinishedCallback.get());
+ mVectorAnimationThread.AddEventTriggerCallback(mAnimationFinishedCallback.get(), mAppliedPlayStateId);
}
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::Rasterize: Animation is finished [current = %d] [%p]\n", currentFrame, this);
}
- if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
+ // Forcely trigger render once if need.
+ if(mNotifyAfterRasterization || mNeedForceRenderOnceTrigger)
{
- mKeepAnimation = true;
+ Mutex::ScopedLock lock(mMutex);
+ mVectorAnimationThread.RequestForceRenderOnce();
+ mNeedForceRenderOnceTrigger = false;
}
-#ifdef TRACE_ENABLED
- if(gTraceFilter && gTraceFilter->IsTraceEnabled())
+ if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED)
{
- std::ostringstream oss;
- oss << "[size:" << mWidth << "x" << mHeight << " ";
- oss << "frame:" << mCurrentFrame << " ";
- oss << "loop:" << mCurrentLoop << " ";
- oss << "state:" << mPlayState << " ";
- oss << "url:" << mImageUrl.GetUrl() << "]";
- DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK", oss.str().c_str());
+ mKeepAnimation = true;
}
-#endif
+
+ DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_LOTTIE_RASTERIZE_TASK", [&](std::ostringstream& oss) {
+ mEndTimeNanoSceonds = GetNanoseconds();
+ oss << std::fixed << std::setprecision(3);
+ oss << "[";
+ oss << "d:" << static_cast<float>(mEndTimeNanoSceonds - mStartTimeNanoSceonds) / 1000000.0f << "ms ";
+ oss << "s:" << mWidth << "x" << mHeight << " ";
+ oss << "f:" << mCurrentFrame << " ";
+ oss << "l:" << mCurrentLoop << " ";
+ oss << "p:" << mPlayState << " ";
+ oss << "u:" << mImageUrl.GetEllipsedUrl() << "]";
+ });
return true;
}
uint32_t index;
{
- ConditionalWait::ScopedLock lock(mConditionalWait);
+ Mutex::ScopedLock lock(mMutex);
if(!mAnimationDataUpdated || mAnimationData[mAnimationDataIndex].size() != 0)
{
SetCurrentFrameNumber(animationData.currentFrame);
}
+ if(animationData.resendFlag & VectorAnimationTask::RESEND_NOTIFY_AFTER_RASTERIZATION)
+ {
+ mNotifyAfterRasterization = animationData.notifyAfterRasterization;
+ }
+
if(animationData.resendFlag & VectorAnimationTask::RESEND_NEED_RESOURCE_READY)
{
mVectorRenderer.InvalidateBuffer();
if(animationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
{
+ mAppliedPlayStateId = animationData.playStateId;
if(animationData.playState == DevelImageVisual::PlayState::PLAYING)
{
PlayAnimation();
mResourceReadySignal.Emit(ResourceStatus::READY);
}
-void VectorAnimationTask::OnLoadCompleted()
+void VectorAnimationTask::OnLoadCompleted(uint32_t /* not used */)
{
if(!mLoadFailed)
{
+ if(mEnableFrameCache && mSizeUpdated)
+ {
+ mVectorRenderer.KeepRasterizedBuffer();
+ mSizeUpdated = false;
+ }
mResourceReadySignal.Emit(ResourceStatus::LOADED);
}
else