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-thread.h>
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
36 constexpr auto DEFAULT_NUMBER_OF_RASTERIZE_THREADS = size_t{4u};
37 constexpr auto NUMBER_OF_RASTERIZE_THREADS_ENV = "DALI_VECTOR_RASTERIZE_THREADS";
39 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
41 using Dali::EnvironmentVariable::GetEnvironmentVariable;
42 auto numberString = GetEnvironmentVariable(environmentVariable);
43 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
44 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
45 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
46 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
53 } // unnamed namespace
55 VectorAnimationThread::VectorAnimationThread()
59 mRasterizers(GetNumberOfThreads(NUMBER_OF_RASTERIZE_THREADS_ENV, DEFAULT_NUMBER_OF_RASTERIZE_THREADS), [&]() { return RasterizeHelper(*this); }),
60 mSleepThread(MakeCallback(this, &VectorAnimationThread::OnAwakeFromSleep)),
63 mDestroyThread(false),
64 mLogFactory(Dali::Adaptor::Get().GetLogFactory())
69 VectorAnimationThread::~VectorAnimationThread()
73 ConditionalWait::ScopedLock lock(mConditionalWait);
74 mDestroyThread = true;
76 mConditionalWait.Notify(lock);
79 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationThread::~VectorAnimationThread: Join [%p]\n", this);
84 void VectorAnimationThread::AddTask(VectorAnimationTaskPtr task)
86 ConditionalWait::ScopedLock lock(mConditionalWait);
88 if(mAnimationTasks.end() == std::find(mAnimationTasks.begin(), mAnimationTasks.end(), task))
90 auto currentTime = task->CalculateNextFrameTime(true); // Rasterize as soon as possible
92 bool inserted = false;
93 for(auto iter = mAnimationTasks.begin(); iter != mAnimationTasks.end(); ++iter)
95 auto nextTime = (*iter)->GetNextFrameTime();
96 if(nextTime > currentTime)
98 mAnimationTasks.insert(iter, task);
106 mAnimationTasks.push_back(task);
109 mNeedToSleep = false;
110 // wake up the animation thread
111 mConditionalWait.Notify(lock);
115 void VectorAnimationThread::OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation)
119 ConditionalWait::ScopedLock lock(mConditionalWait);
120 bool needRasterize = false;
122 auto workingTask = std::find(mWorkingTasks.begin(), mWorkingTasks.end(), task);
123 if(workingTask != mWorkingTasks.end())
125 mWorkingTasks.erase(workingTask);
128 // Check pending task
129 if(mAnimationTasks.end() != std::find(mAnimationTasks.begin(), mAnimationTasks.end(), task))
131 needRasterize = true;
134 if(keepAnimation && success)
136 if(mCompletedTasks.end() == std::find(mCompletedTasks.begin(), mCompletedTasks.end(), task))
138 mCompletedTasks.push_back(task);
139 needRasterize = true;
145 mNeedToSleep = false;
146 // wake up the animation thread
147 mConditionalWait.Notify(lock);
152 void VectorAnimationThread::OnAwakeFromSleep()
156 mNeedToSleep = false;
157 // wake up the animation thread
158 mConditionalWait.Notify();
162 void VectorAnimationThread::Run()
164 SetThreadName("VectorAnimationThread");
165 mLogFactory.InstallLogFunction();
167 while(!mDestroyThread)
173 void VectorAnimationThread::Rasterize()
175 // Lock while popping task out from the queue
176 ConditionalWait::ScopedLock lock(mConditionalWait);
181 mConditionalWait.Wait(lock);
186 // Process completed tasks
187 for(auto&& task : mCompletedTasks)
189 if(mAnimationTasks.end() == std::find(mAnimationTasks.begin(), mAnimationTasks.end(), task))
191 // Should use the frame rate of the animation file
192 auto nextFrameTime = task->CalculateNextFrameTime(false);
194 bool inserted = false;
195 for(auto iter = mAnimationTasks.begin(); iter != mAnimationTasks.end(); ++iter)
197 auto time = (*iter)->GetNextFrameTime();
198 if(time > nextFrameTime)
200 mAnimationTasks.insert(iter, task);
208 mAnimationTasks.push_back(task);
212 mCompletedTasks.clear();
214 // pop out the next task from the queue
215 for(auto it = mAnimationTasks.begin(); it != mAnimationTasks.end();)
217 VectorAnimationTaskPtr nextTask = *it;
219 auto currentTime = std::chrono::steady_clock::now();
220 auto nextFrameTime = nextTask->GetNextFrameTime();
222 #if defined(DEBUG_ENABLED)
223 // auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(nextFrameTime - currentTime);
225 // DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationThread::Rasterize: [next time = %lld]\n", duration.count());
228 if(nextFrameTime <= currentTime)
230 // If the task is not in the working list
231 if(std::find(mWorkingTasks.begin(), mWorkingTasks.end(), nextTask) == mWorkingTasks.end())
233 it = mAnimationTasks.erase(it);
235 // Add it to the working list
236 mWorkingTasks.push_back(nextTask);
238 auto rasterizerHelperIt = mRasterizers.GetNext();
239 DALI_ASSERT_ALWAYS(rasterizerHelperIt != mRasterizers.End());
241 rasterizerHelperIt->Rasterize(nextTask);
250 mSleepThread.SleepUntil(nextFrameTime);
256 VectorAnimationThread::RasterizeHelper::RasterizeHelper(VectorAnimationThread& animationThread)
257 : RasterizeHelper(std::unique_ptr<VectorRasterizeThread>(new VectorRasterizeThread()), animationThread)
261 VectorAnimationThread::RasterizeHelper::RasterizeHelper(RasterizeHelper&& rhs)
262 : RasterizeHelper(std::move(rhs.mRasterizer), rhs.mAnimationThread)
266 VectorAnimationThread::RasterizeHelper::RasterizeHelper(std::unique_ptr<VectorRasterizeThread> rasterizer, VectorAnimationThread& animationThread)
267 : mRasterizer(std::move(rasterizer)),
268 mAnimationThread(animationThread)
270 mRasterizer->SetCompletedCallback(MakeCallback(&mAnimationThread, &VectorAnimationThread::OnTaskCompleted));
273 void VectorAnimationThread::RasterizeHelper::Rasterize(VectorAnimationTaskPtr task)
277 mRasterizer->AddTask(task);
281 VectorAnimationThread::SleepThread::SleepThread(CallbackBase* callback)
282 : mConditionalWait(),
283 mAwakeCallback(std::unique_ptr<CallbackBase>(callback)),
285 mLogFactory(Dali::Adaptor::Get().GetLogFactory()),
287 mDestroyThread(false)
291 VectorAnimationThread::SleepThread::~SleepThread()
295 ConditionalWait::ScopedLock lock(mConditionalWait);
296 mDestroyThread = true;
297 mConditionalWait.Notify(lock);
303 void VectorAnimationThread::SleepThread::SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil)
305 ConditionalWait::ScopedLock lock(mConditionalWait);
306 mSleepTimePoint = timeToSleepUntil;
308 mConditionalWait.Notify(lock);
311 void VectorAnimationThread::SleepThread::Run()
313 SetThreadName("VectorSleepThread");
314 mLogFactory.InstallLogFunction();
316 while(!mDestroyThread)
319 std::chrono::time_point<std::chrono::steady_clock> sleepTimePoint;
322 ConditionalWait::ScopedLock lock(mConditionalWait);
324 needToSleep = mNeedToSleep;
325 sleepTimePoint = mSleepTimePoint;
327 mNeedToSleep = false;
332 #if defined(DEBUG_ENABLED)
333 // auto sleepDuration = std::chrono::duration_cast<std::chrono::milliseconds>(mSleepTimePoint - std::chrono::steady_clock::now());
335 // DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorAnimationThread::SleepThread::Run: [sleep duration = %lld]\n", sleepDuration.count());
338 std::this_thread::sleep_until(sleepTimePoint);
342 CallbackBase::Execute(*mAwakeCallback);
347 ConditionalWait::ScopedLock lock(mConditionalWait);
348 if(!mDestroyThread && !mNeedToSleep)
350 mConditionalWait.Wait(lock);
356 } // namespace Internal
358 } // namespace Toolkit