From: tscholb Date: Fri, 6 Jan 2023 03:30:02 +0000 (+0900) Subject: Use AsyncTaskManager in animated-vector-image X-Git-Tag: dali_2.2.9~8^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F36%2F286436%2F1;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git Use AsyncTaskManager in animated-vector-image Change-Id: I05356863dd5bbf63b76bcfa15a1925abb18a4fb6 --- diff --git a/dali-toolkit/internal/file.list b/dali-toolkit/internal/file.list index a7f5397..b859b39 100644 --- a/dali-toolkit/internal/file.list +++ b/dali-toolkit/internal/file.list @@ -26,7 +26,6 @@ SET( toolkit_src_files ${toolkit_src_dir}/visuals/animated-vector-image/vector-animation-manager.cpp ${toolkit_src_dir}/visuals/animated-vector-image/vector-animation-task.cpp ${toolkit_src_dir}/visuals/animated-vector-image/vector-animation-thread.cpp - ${toolkit_src_dir}/visuals/animated-vector-image/vector-rasterize-thread.cpp ${toolkit_src_dir}/visuals/arc/arc-visual.cpp ${toolkit_src_dir}/visuals/border/border-visual.cpp ${toolkit_src_dir}/visuals/color/color-visual.cpp diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.cpp b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.cpp index 45d3484..158ef83 100644 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.cpp +++ b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 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. @@ -46,7 +46,8 @@ Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, } // unnamed namespace VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache) -: mUrl(), +: AsyncTask(MakeCallback(this, &VectorAnimationTask::TaskCompleted), AsyncTask::ThreadType::WORKER_THREAD), + mUrl(), mVectorRenderer(VectorAnimationRenderer::New()), mAnimationData(), mVectorAnimationThread(factoryCache.GetVectorAnimationManager().GetVectorAnimationThread()), @@ -76,7 +77,9 @@ VectorAnimationTask::VectorAnimationTask(VisualFactoryCache& factoryCache) mAnimationDataUpdated(false), mDestroyTask(false), mLoadRequest(false), - mLoadFailed(false) + mLoadFailed(false), + mRasterized(false), + mKeepAnimation(false) { mVectorRenderer.UploadCompletedSignal().Connect(this, &VectorAnimationTask::OnUploadCompleted); } @@ -86,6 +89,16 @@ VectorAnimationTask::~VectorAnimationTask() DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationTask::~VectorAnimationTask: destructor [%p]\n", this); } +void VectorAnimationTask::Process() +{ + mRasterized = Rasterize(); +} + +bool VectorAnimationTask::IsReady() +{ + return true; +} + void VectorAnimationTask::Finalize() { ConditionalWait::ScopedLock lock(mConditionalWait); @@ -105,6 +118,21 @@ void VectorAnimationTask::Finalize() mDestroyTask = true; } +void VectorAnimationTask::TaskCompleted(VectorAnimationTaskPtr task) +{ + mVectorAnimationThread.OnTaskCompleted(task, task->IsRasterized(), task->IsAnimating()); +} + +bool VectorAnimationTask::IsRasterized() +{ + return mRasterized; +} + +bool VectorAnimationTask::IsAnimating() +{ + return mKeepAnimation; +} + bool VectorAnimationTask::Load(bool synchronousLoading) { if(!mVectorRenderer.Load(mUrl)) @@ -401,11 +429,11 @@ VectorAnimationTask::ResourceReadySignalType& VectorAnimationTask::ResourceReady return mResourceReadySignal; } -bool VectorAnimationTask::Rasterize(bool& keepAnimation) +bool VectorAnimationTask::Rasterize() { bool stopped = false; uint32_t currentFrame; - keepAnimation = false; + mKeepAnimation = false; { ConditionalWait::ScopedLock lock(mConditionalWait); @@ -530,7 +558,7 @@ bool VectorAnimationTask::Rasterize(bool& keepAnimation) if(mPlayState != PlayState::PAUSED && mPlayState != PlayState::STOPPED) { - keepAnimation = true; + mKeepAnimation = true; } return true; diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h index 4a0c497..0b2c43e 100644 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h +++ b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h @@ -2,7 +2,7 @@ #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H /* - * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 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. @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -44,7 +45,7 @@ typedef IntrusivePtr VectorAnimationTaskPtr; /** * The task of the vector animation. */ -class VectorAnimationTask : public RefObject, public ConnectionTracker +class VectorAnimationTask : public AsyncTask, public ConnectionTracker { public: enum class ResourceStatus @@ -134,6 +135,17 @@ public: ~VectorAnimationTask() override; /** + * Process the task accodring to the type + */ + void Process() override; + + /** + * Whether the task is ready to process. + * @return True if the task is ready to process. + */ + bool IsReady() override; + + /** * @brief Finalizes the task. */ void Finalize(); @@ -210,10 +222,9 @@ public: /** * @brief Rasterizes the current frame. - * @param[out] keepAnimation true if the animation is running, false otherwise. * @return true if the rasterization succeeded, false otherwise. */ - bool Rasterize(bool& keepAnimation); + bool Rasterize(); /** * @brief Calculates the time for the next frame rasterization. @@ -227,6 +238,24 @@ public: */ TimePoint GetNextFrameTime(); + /** + * @brief Called when the rasterization is completed from the asyncTaskManager + * @param[in] task The completed task + */ + void TaskCompleted(VectorAnimationTaskPtr task); + + /** + * @brief Check the rasterization succeeded + * @return true if the rasterization succeeded, false otherwise. + */ + bool IsRasterized(); + + /** + * @brief Check the animation is running + * @return true if the animation is running, false otherwise. + */ + bool IsAnimating(); + private: /** * @brief Loads the animation file. @@ -357,6 +386,8 @@ private: bool mDestroyTask; bool mLoadRequest; bool mLoadFailed; + bool mRasterized; + bool mKeepAnimation; }; } // namespace Internal diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp index c6dbdb6..02198f0 100644 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp +++ b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 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. @@ -33,19 +33,6 @@ namespace Internal { namespace { -constexpr auto DEFAULT_NUMBER_OF_RASTERIZE_THREADS = size_t{4u}; -constexpr auto NUMBER_OF_RASTERIZE_THREADS_ENV = "DALI_VECTOR_RASTERIZE_THREADS"; - -size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue) -{ - using Dali::EnvironmentVariable::GetEnvironmentVariable; - auto numberString = GetEnvironmentVariable(environmentVariable); - auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0; - constexpr auto MAX_NUMBER_OF_THREADS = 100u; - DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS); - return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue; -} - #if defined(DEBUG_ENABLED) Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION"); #endif @@ -56,13 +43,13 @@ VectorAnimationThread::VectorAnimationThread() : mAnimationTasks(), mCompletedTasks(), mWorkingTasks(), - mRasterizers(GetNumberOfThreads(NUMBER_OF_RASTERIZE_THREADS_ENV, DEFAULT_NUMBER_OF_RASTERIZE_THREADS), [&]() { return RasterizeHelper(*this); }), mSleepThread(MakeCallback(this, &VectorAnimationThread::OnAwakeFromSleep)), mConditionalWait(), mNeedToSleep(false), mDestroyThread(false), mLogFactory(Dali::Adaptor::Get().GetLogFactory()) { + mAsyncTaskManager = Dali::AsyncTaskManager::Get(); mSleepThread.Start(); } @@ -226,7 +213,6 @@ void VectorAnimationThread::Rasterize() // DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationThread::Rasterize: [next time = %lld]\n", duration.count()); #endif - if(nextFrameTime <= currentTime) { // If the task is not in the working list @@ -236,11 +222,7 @@ void VectorAnimationThread::Rasterize() // Add it to the working list mWorkingTasks.push_back(nextTask); - - auto rasterizerHelperIt = mRasterizers.GetNext(); - DALI_ASSERT_ALWAYS(rasterizerHelperIt != mRasterizers.End()); - - rasterizerHelperIt->Rasterize(nextTask); + mAsyncTaskManager.AddTask(nextTask); } else { @@ -255,31 +237,6 @@ void VectorAnimationThread::Rasterize() } } -VectorAnimationThread::RasterizeHelper::RasterizeHelper(VectorAnimationThread& animationThread) -: RasterizeHelper(std::unique_ptr(new VectorRasterizeThread()), animationThread) -{ -} - -VectorAnimationThread::RasterizeHelper::RasterizeHelper(RasterizeHelper&& rhs) -: RasterizeHelper(std::move(rhs.mRasterizer), rhs.mAnimationThread) -{ -} - -VectorAnimationThread::RasterizeHelper::RasterizeHelper(std::unique_ptr rasterizer, VectorAnimationThread& animationThread) -: mRasterizer(std::move(rasterizer)), - mAnimationThread(animationThread) -{ - mRasterizer->SetCompletedCallback(MakeCallback(&mAnimationThread, &VectorAnimationThread::OnTaskCompleted)); -} - -void VectorAnimationThread::RasterizeHelper::Rasterize(VectorAnimationTaskPtr task) -{ - if(task) - { - mRasterizer->AddTask(task); - } -} - VectorAnimationThread::SleepThread::SleepThread(CallbackBase* callback) : mConditionalWait(), mAwakeCallback(std::unique_ptr(callback)), diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h index 0d2dfb0..0fd37e4 100644 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h +++ b/dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h @@ -2,7 +2,7 @@ #define DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H /* - * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 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. @@ -21,13 +21,12 @@ #include #include #include -#include #include +#include #include // INTERNAL INCLUDES #include -#include namespace Dali { @@ -83,45 +82,6 @@ private: */ void Rasterize(); -private: - /** - * @brief Helper class to keep the relation between VectorRasterizeThread and corresponding container - */ - class RasterizeHelper : public ConnectionTracker - { - public: - /** - * @brief Create an RasterizeHelper. - * - * @param[in] animationThread Reference to the VectorAnimationThread - */ - RasterizeHelper(VectorAnimationThread& animationThread); - - /** - * @brief Rasterizes the task. - * - * @param[in] task The task to rasterize. - */ - void Rasterize(VectorAnimationTaskPtr task); - - public: - RasterizeHelper(const RasterizeHelper&) = delete; - RasterizeHelper& operator=(const RasterizeHelper&) = delete; - - RasterizeHelper(RasterizeHelper&& rhs); - RasterizeHelper& operator=(RasterizeHelper&& rhs) = delete; - - private: - /** - * @brief Main constructor that used by all other constructors - */ - RasterizeHelper(std::unique_ptr rasterizer, VectorAnimationThread& animationThread); - - private: - std::unique_ptr mRasterizer; - VectorAnimationThread& mAnimationThread; - }; - /** * @brief The thread to sleep until the next frame time. */ @@ -170,15 +130,15 @@ private: VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete; private: - std::vector mAnimationTasks; - std::vector mCompletedTasks; - std::vector mWorkingTasks; - RoundRobinContainerView mRasterizers; - SleepThread mSleepThread; - ConditionalWait mConditionalWait; - bool mNeedToSleep; - bool mDestroyThread; - const Dali::LogFactoryInterface& mLogFactory; + std::vector mAnimationTasks; + std::vector mCompletedTasks; + std::vector mWorkingTasks; + SleepThread mSleepThread; + ConditionalWait mConditionalWait; + bool mNeedToSleep; + bool mDestroyThread; + const Dali::LogFactoryInterface& mLogFactory; + Dali::AsyncTaskManager mAsyncTaskManager; }; } // namespace Internal diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp b/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp deleted file mode 100644 index 0ba9f52..0000000 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// CLASS HEADER -#include - -// EXTERNAL INCLUDES -#include -#include -#include -#include -#include - -namespace Dali -{ -namespace Toolkit -{ -namespace Internal -{ -namespace -{ -#if defined(DEBUG_ENABLED) -Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION"); -#endif - -} // unnamed namespace - -VectorRasterizeThread::VectorRasterizeThread() -: mRasterizeTasks(), - mConditionalWait(), - mCompletedCallback(), - mDestroyThread(false), - mIsThreadStarted(false), - mLogFactory(Dali::Adaptor::Get().GetLogFactory()) -{ -} - -VectorRasterizeThread::~VectorRasterizeThread() -{ - // Stop the thread - { - ConditionalWait::ScopedLock lock(mConditionalWait); - mDestroyThread = true; - mConditionalWait.Notify(lock); - } - - DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this); - - Join(); -} - -void VectorRasterizeThread::SetCompletedCallback(CallbackBase* callback) -{ - ConditionalWait::ScopedLock lock(mConditionalWait); - - mCompletedCallback = std::unique_ptr(callback); -} - -void VectorRasterizeThread::AddTask(VectorAnimationTaskPtr task) -{ - // Lock while adding task to the queue - ConditionalWait::ScopedLock lock(mConditionalWait); - - if(!mIsThreadStarted) - { - Start(); - mIsThreadStarted = true; - } - - if(mRasterizeTasks.end() == std::find(mRasterizeTasks.begin(), mRasterizeTasks.end(), task)) - { - mRasterizeTasks.push_back(task); - - // wake up the animation thread - mConditionalWait.Notify(lock); - } -} - -void VectorRasterizeThread::Run() -{ - SetThreadName("VectorRasterizeThread"); - mLogFactory.InstallLogFunction(); - - while(!mDestroyThread) - { - Rasterize(); - } -} - -void VectorRasterizeThread::Rasterize() -{ - VectorAnimationTaskPtr nextTask; - { - // Lock while popping task out from the queue - ConditionalWait::ScopedLock lock(mConditionalWait); - - // conditional wait - if(mRasterizeTasks.empty()) - { - mConditionalWait.Wait(lock); - } - - // pop out the next task from the queue - if(!mRasterizeTasks.empty()) - { - std::vector::iterator next = mRasterizeTasks.begin(); - nextTask = *next; - mRasterizeTasks.erase(next); - } - } - - if(nextTask) - { - bool keepAnimation; - bool success = nextTask->Rasterize(keepAnimation); - - if(mCompletedCallback) - { - CallbackBase::Execute(*mCompletedCallback, nextTask, success, keepAnimation); - } - } -} - -} // namespace Internal - -} // namespace Toolkit - -} // namespace Dali diff --git a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h b/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h deleted file mode 100644 index 25bd4e5..0000000 --- a/dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h +++ /dev/null @@ -1,100 +0,0 @@ -#ifndef DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H -#define DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H - -/* - * Copyright (c) 2021 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// EXTERNAL INCLUDES -#include -#include -#include -#include -#include - -// INTERNAL INCLUDES -#include - -namespace Dali -{ -namespace Toolkit -{ -namespace Internal -{ -/** - * The worker thread for vector image rasterization. - */ -class VectorRasterizeThread : public Thread -{ -public: - /** - * @brief Constructor. - */ - VectorRasterizeThread(); - - /** - * @brief Destructor. - */ - ~VectorRasterizeThread() override; - - /** - * The callback is called from the rasterize thread after the rasterization is completed. - * @param[in] callBack The function to call. - */ - void SetCompletedCallback(CallbackBase* callback); - - /** - * Add a task to rasterize. - * - * @param[in] task The task to rasterize - */ - void AddTask(VectorAnimationTaskPtr task); - -protected: - /** - * @brief The entry function of the worker thread. - * It rasterizes the vector image. - */ - void Run() override; - -private: - /** - * Rasterizes the tasks. - */ - void Rasterize(); - -private: - // Undefined - VectorRasterizeThread(const VectorRasterizeThread& thread) = delete; - - // Undefined - VectorRasterizeThread& operator=(const VectorRasterizeThread& thread) = delete; - -private: - std::vector mRasterizeTasks; - ConditionalWait mConditionalWait; - std::unique_ptr mCompletedCallback; - bool mDestroyThread; ///< Whether the thread be destroyed - bool mIsThreadStarted; - const Dali::LogFactoryInterface& mLogFactory; ///< The log factory -}; - -} // namespace Internal - -} // namespace Toolkit - -} // namespace Dali - -#endif // DALI_TOOLKIT_VECTOR_IMAGE_RASTERIZE_THREAD_H