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