[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-vector-animation-renderer.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 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
19 #include <dali/public-api/object/base-object.h>
20 #include <toolkit-application.h>
21 #include <toolkit-vector-animation-renderer.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <memory>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 class VectorAnimationRenderer: public Dali::BaseObject
35 {
36 public:
37
38   VectorAnimationRenderer( const std::string& url )
39   : mUrl( url ),
40     mRenderer(),
41     mWidth( 0 ),
42     mHeight( 0 ),
43     mPreviousFrame( 0 ),
44     mFrameRate( 60.0f ),
45     mEventThreadCallback( new EventThreadCallback( MakeCallback( this, &VectorAnimationRenderer::OnTriggered ) ) )
46   {
47     mCount++;
48
49     if( mCount == 2 )
50     {
51       mFrameRate = 0.1f;
52     }
53   }
54
55   ~VectorAnimationRenderer()
56   {
57     mCount--;
58   }
59
60   void SetRenderer( Dali::Renderer renderer )
61   {
62     mRenderer = renderer;
63
64     if( mWidth != 0 && mHeight != 0 )
65     {
66       Dali::TextureSet textureSet = mRenderer.GetTextures();
67       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
68       textureSet.SetTexture( 0, texture );
69       mUploadCompletedSignal.Emit();
70     }
71   }
72
73   void SetSize( uint32_t width, uint32_t height )
74   {
75     mWidth = width;
76     mHeight = height;
77
78     if( mRenderer )
79     {
80       Dali::TextureSet textureSet = mRenderer.GetTextures();
81       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
82       textureSet.SetTexture( 0, texture );
83       mUploadCompletedSignal.Emit();
84     }
85   }
86
87   bool Render( uint32_t frameNumber )
88   {
89     if( mNeedTrigger )
90     {
91       mEventThreadCallback->Trigger();
92       mNeedTrigger = false;
93     }
94
95     if( frameNumber == 1 && mPreviousFrame != frameNumber )
96     {
97       mPreviousFrame = frameNumber;
98       // For test corverage
99       return false;
100     }
101     mPreviousFrame = frameNumber;
102     return true;
103   }
104
105   uint32_t GetTotalFrameNumber() const
106   {
107     return VECTOR_ANIMATION_TOTAL_FRAME_NUMBER;
108   }
109
110   float GetFrameRate() const
111   {
112     return mFrameRate;
113   }
114
115   void GetDefaultSize( uint32_t& width, uint32_t& height ) const
116   {
117     width = 100;
118     height = 100;
119   }
120
121   bool GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
122   {
123     if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_1 ) == 0 )
124     {
125       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
126       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
127     }
128     else if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_2 ) == 0 )
129     {
130       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
131       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
132     }
133     else
134     {
135       return false;
136     }
137     return true;
138   }
139
140   Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
141   {
142     return mUploadCompletedSignal;
143   }
144
145   void OnTriggered()
146   {
147   }
148
149 public:
150
151   static uint32_t mCount;
152   static bool mNeedTrigger;
153
154   std::string mUrl;
155   Dali::Renderer mRenderer;
156   uint32_t mWidth;
157   uint32_t mHeight;
158   uint32_t mPreviousFrame;
159   float mFrameRate;
160   Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
161   std::unique_ptr< EventThreadCallback > mEventThreadCallback;
162 };
163
164 uint32_t VectorAnimationRenderer::mCount = 0;
165 bool VectorAnimationRenderer::mNeedTrigger = true;
166
167 inline VectorAnimationRenderer& GetImplementation( Dali::VectorAnimationRenderer& renderer )
168 {
169   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
170   BaseObject& handle = renderer.GetBaseObject();
171   return static_cast< Internal::Adaptor::VectorAnimationRenderer& >( handle );
172 }
173
174 inline const VectorAnimationRenderer& GetImplementation( const Dali::VectorAnimationRenderer& renderer )
175 {
176   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
177   const BaseObject& handle = renderer.GetBaseObject();
178   return static_cast< const Internal::Adaptor::VectorAnimationRenderer& >( handle );
179 }
180
181 } // namespace Adaptor
182
183 } // namespace Internal
184
185
186 /********************************************************************************/
187 /*********************************  PUBLIC CLASS  *******************************/
188 /********************************************************************************/
189
190 VectorAnimationRenderer VectorAnimationRenderer::New( const std::string& url )
191 {
192   Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer( url );
193
194   return VectorAnimationRenderer( animationRenderer );
195 }
196
197 VectorAnimationRenderer::VectorAnimationRenderer()
198 {
199 }
200
201 VectorAnimationRenderer::~VectorAnimationRenderer()
202 {
203 }
204
205 VectorAnimationRenderer::VectorAnimationRenderer( Internal::Adaptor::VectorAnimationRenderer* internal )
206 : BaseHandle( internal )
207 {
208 }
209
210 VectorAnimationRenderer::VectorAnimationRenderer( const VectorAnimationRenderer& handle )
211 : BaseHandle( handle )
212 {
213 }
214
215 VectorAnimationRenderer& VectorAnimationRenderer::operator=( const VectorAnimationRenderer& rhs )
216 {
217   BaseHandle::operator=( rhs );
218   return *this;
219 }
220
221 void VectorAnimationRenderer::Finalize()
222 {
223 }
224
225 void VectorAnimationRenderer::SetRenderer( Renderer renderer )
226 {
227   Internal::Adaptor::GetImplementation( *this ).SetRenderer( renderer );
228 }
229
230 void VectorAnimationRenderer::SetSize( uint32_t width, uint32_t height )
231 {
232   Internal::Adaptor::GetImplementation( *this ).SetSize( width, height );
233 }
234
235 bool VectorAnimationRenderer::Render( uint32_t frameNumber )
236 {
237   return Internal::Adaptor::GetImplementation( *this ).Render( frameNumber );
238 }
239
240 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
241 {
242   return Internal::Adaptor::GetImplementation( *this ).GetTotalFrameNumber();
243 }
244
245 float VectorAnimationRenderer::GetFrameRate() const
246 {
247   return Internal::Adaptor::GetImplementation( *this ).GetFrameRate();
248 }
249
250 void VectorAnimationRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
251 {
252   Internal::Adaptor::GetImplementation( *this ).GetDefaultSize( width, height );
253 }
254
255 void VectorAnimationRenderer::GetLayerInfo( Property::Map& map ) const
256 {
257 }
258
259 bool VectorAnimationRenderer::GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
260 {
261   return Internal::Adaptor::GetImplementation( *this ).GetMarkerInfo( marker, startFrame, endFrame );
262 }
263
264 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
265 {
266   return Internal::Adaptor::GetImplementation( *this ).UploadCompletedSignal();
267 }
268
269 } // namespace Dali
270
271 namespace Test
272 {
273 namespace VectorAnimationRenderer
274 {
275
276 void RequestTrigger()
277 {
278   Dali::Internal::Adaptor::VectorAnimationRenderer::mNeedTrigger = true;
279 }
280
281 } // VectorAnimationRenderer
282 } // Test
283