Add post processor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-rasterize-thread.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/thread-settings.h>
23 #include <dali/integration-api/adaptor-framework/adaptor.h>
24 #include <dali/integration-api/debug.h>
25 #include <chrono>
26 #include <thread>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
38 #endif
39
40 } // unnamed namespace
41
42 VectorRasterizeThread::VectorRasterizeThread()
43 : mRasterizeTasks(),
44   mConditionalWait(),
45   mCompletedCallback(),
46   mDestroyThread(false),
47   mIsThreadStarted(false),
48   mLogFactory(Dali::Adaptor::Get().GetLogFactory())
49 {
50 }
51
52 VectorRasterizeThread::~VectorRasterizeThread()
53 {
54   // Stop the thread
55   {
56     ConditionalWait::ScopedLock lock(mConditionalWait);
57     mDestroyThread = true;
58     mConditionalWait.Notify(lock);
59   }
60
61   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "VectorRasterizeThread::~VectorRasterizeThread: Join [%p]\n", this);
62
63   Join();
64 }
65
66 void VectorRasterizeThread::SetCompletedCallback(CallbackBase* callback)
67 {
68   ConditionalWait::ScopedLock lock(mConditionalWait);
69
70   mCompletedCallback = std::unique_ptr<CallbackBase>(callback);
71 }
72
73 void VectorRasterizeThread::AddTask(VectorAnimationTaskPtr task)
74 {
75   // Lock while adding task to the queue
76   ConditionalWait::ScopedLock lock(mConditionalWait);
77
78   if(!mIsThreadStarted)
79   {
80     Start();
81     mIsThreadStarted = true;
82   }
83
84   if(mRasterizeTasks.end() == std::find(mRasterizeTasks.begin(), mRasterizeTasks.end(), task))
85   {
86     mRasterizeTasks.push_back(task);
87
88     // wake up the animation thread
89     mConditionalWait.Notify(lock);
90   }
91 }
92
93 void VectorRasterizeThread::Run()
94 {
95   SetThreadName("VectorRasterizeThread");
96   mLogFactory.InstallLogFunction();
97
98   while(!mDestroyThread)
99   {
100     Rasterize();
101   }
102 }
103
104 void VectorRasterizeThread::Rasterize()
105 {
106   VectorAnimationTaskPtr nextTask;
107   {
108     // Lock while popping task out from the queue
109     ConditionalWait::ScopedLock lock(mConditionalWait);
110
111     // conditional wait
112     if(mRasterizeTasks.empty())
113     {
114       mConditionalWait.Wait(lock);
115     }
116
117     // pop out the next task from the queue
118     if(!mRasterizeTasks.empty())
119     {
120       std::vector<VectorAnimationTaskPtr>::iterator next = mRasterizeTasks.begin();
121       nextTask                                           = *next;
122       mRasterizeTasks.erase(next);
123     }
124   }
125
126   if(nextTask)
127   {
128     bool keepAnimation = nextTask->Rasterize();
129
130     if(mCompletedCallback)
131     {
132       CallbackBase::Execute(*mCompletedCallback, nextTask, keepAnimation);
133     }
134   }
135 }
136
137 } // namespace Internal
138
139 } // namespace Toolkit
140
141 } // namespace Dali