Merge "[AT-SPI] Set ImageView non-highlightable by default" into 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) 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 #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 #include <thread>
25 #include <chrono>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 namespace
37 {
38 Dali::Internal::Adaptor::VectorAnimationRenderer* gVectorAnimationRenderer = nullptr;
39 }
40
41 class VectorAnimationRenderer: public Dali::BaseObject
42 {
43 public:
44
45   VectorAnimationRenderer()
46   : mUrl(),
47     mRenderer(),
48     mWidth( 0 ),
49     mHeight( 0 ),
50     mTotalFrameNumber(VECTOR_ANIMATION_TOTAL_FRAME_NUMBER),
51     mPreviousFrame( 0 ),
52     mDelayTime(0),
53     mDroppedFrames(0),
54     mFrameRate( 60.0f ),
55     mNeedDroppedFrames(false),
56     mEventThreadCallback( new EventThreadCallback( MakeCallback( this, &VectorAnimationRenderer::OnTriggered ) ) )
57   {
58     mCount++;
59
60     if( mCount == 2 )
61     {
62       mFrameRate = 0.1f;
63     }
64   }
65
66   ~VectorAnimationRenderer()
67   {
68     mCount--;
69   }
70
71   bool Load(const std::string& url)
72   {
73     mUrl = url;
74     if(mUrl == "invalid.json")
75     {
76       return false;
77     }
78     else if(mUrl == "framedrop.json")
79     {
80       // Change total frame number for test
81       mTotalFrameNumber = 200;
82     }
83     return true;
84   }
85
86   void SetRenderer( Dali::Renderer renderer )
87   {
88     mRenderer = renderer;
89
90     if( mWidth != 0 && mHeight != 0 )
91     {
92       Dali::TextureSet textureSet = mRenderer.GetTextures();
93       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
94       textureSet.SetTexture( 0, texture );
95       mUploadCompletedSignal.Emit();
96     }
97   }
98
99   void SetSize( uint32_t width, uint32_t height )
100   {
101     mWidth = width;
102     mHeight = height;
103
104     if( mRenderer )
105     {
106       Dali::TextureSet textureSet = mRenderer.GetTextures();
107       Dali::Texture texture = Dali::Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, mWidth, mHeight );
108       textureSet.SetTexture( 0, texture );
109       mUploadCompletedSignal.Emit();
110     }
111   }
112
113   bool Render( uint32_t frameNumber )
114   {
115     if(mDelayTime != 0)
116     {
117       std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int32_t>(mDelayTime)));
118       mDelayTime = 0;
119       mNeedDroppedFrames = true;
120     }
121     else if(mNeedDroppedFrames)
122     {
123       mDroppedFrames = (frameNumber > mPreviousFrame) ? frameNumber - mPreviousFrame - 1: frameNumber + (mTotalFrameNumber - mPreviousFrame) - 1;
124       mNeedTrigger = true;
125       mNeedDroppedFrames = false;
126     }
127
128     if( mNeedTrigger )
129     {
130       mEventThreadCallback->Trigger();
131       mNeedTrigger = false;
132     }
133
134     if( frameNumber == 1 && mPreviousFrame != frameNumber )
135     {
136       mPreviousFrame = frameNumber;
137       // For test corverage
138       return false;
139     }
140     mPreviousFrame = frameNumber;
141     return true;
142   }
143
144   uint32_t GetTotalFrameNumber() const
145   {
146     return mTotalFrameNumber;
147   }
148
149   float GetFrameRate() const
150   {
151     return mFrameRate;
152   }
153
154   void GetDefaultSize( uint32_t& width, uint32_t& height ) const
155   {
156     width = 100;
157     height = 100;
158   }
159
160   bool GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
161   {
162     if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_1 ) == 0 )
163     {
164       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_1;
165       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_1;
166     }
167     else if( marker.compare( VECTOR_ANIMATION_MARKER_NAME_2 ) == 0 )
168     {
169       startFrame = VECTOR_ANIMATION_MARKER_START_FRAME_2;
170       endFrame = VECTOR_ANIMATION_MARKER_END_FRAME_2;
171     }
172     else
173     {
174       return false;
175     }
176     return true;
177   }
178
179   Dali::VectorAnimationRenderer::UploadCompletedSignalType& UploadCompletedSignal()
180   {
181     return mUploadCompletedSignal;
182   }
183
184   void OnTriggered()
185   {
186   }
187
188 public:
189
190   static uint32_t mCount;
191   static bool mNeedTrigger;
192
193   std::string mUrl;
194   Dali::Renderer mRenderer;
195   uint32_t mWidth;
196   uint32_t mHeight;
197   uint32_t mTotalFrameNumber;
198   uint32_t mPreviousFrame;
199   uint32_t mDelayTime;
200   uint32_t mDroppedFrames;
201   float mFrameRate;
202   bool mNeedDroppedFrames;
203   Dali::VectorAnimationRenderer::UploadCompletedSignalType mUploadCompletedSignal;
204   std::unique_ptr< EventThreadCallback > mEventThreadCallback;
205 };
206
207 uint32_t VectorAnimationRenderer::mCount = 0;
208 bool VectorAnimationRenderer::mNeedTrigger = true;
209
210 inline VectorAnimationRenderer& GetImplementation( Dali::VectorAnimationRenderer& renderer )
211 {
212   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
213   BaseObject& handle = renderer.GetBaseObject();
214   return static_cast< Internal::Adaptor::VectorAnimationRenderer& >( handle );
215 }
216
217 inline const VectorAnimationRenderer& GetImplementation( const Dali::VectorAnimationRenderer& renderer )
218 {
219   DALI_ASSERT_ALWAYS( renderer && "VectorAnimationRenderer handle is empty." );
220   const BaseObject& handle = renderer.GetBaseObject();
221   return static_cast< const Internal::Adaptor::VectorAnimationRenderer& >( handle );
222 }
223
224 } // namespace Adaptor
225
226 } // namespace Internal
227
228
229 /********************************************************************************/
230 /*********************************  PUBLIC CLASS  *******************************/
231 /********************************************************************************/
232
233 VectorAnimationRenderer VectorAnimationRenderer::New()
234 {
235   Internal::Adaptor::VectorAnimationRenderer* animationRenderer = new Internal::Adaptor::VectorAnimationRenderer();
236
237   Internal::Adaptor::gVectorAnimationRenderer = animationRenderer;
238
239   return VectorAnimationRenderer( animationRenderer );
240 }
241
242 VectorAnimationRenderer::VectorAnimationRenderer()
243 {
244 }
245
246 VectorAnimationRenderer::~VectorAnimationRenderer()
247 {
248 }
249
250 VectorAnimationRenderer::VectorAnimationRenderer( Internal::Adaptor::VectorAnimationRenderer* internal )
251 : BaseHandle( internal )
252 {
253 }
254
255 VectorAnimationRenderer::VectorAnimationRenderer( const VectorAnimationRenderer& handle )
256 : BaseHandle( handle )
257 {
258 }
259
260 VectorAnimationRenderer& VectorAnimationRenderer::operator=( const VectorAnimationRenderer& rhs )
261 {
262   BaseHandle::operator=( rhs );
263   return *this;
264 }
265
266 void VectorAnimationRenderer::Finalize()
267 {
268 }
269
270 bool VectorAnimationRenderer::Load(const std::string& url)
271 {
272   return Internal::Adaptor::GetImplementation( *this ).Load(url);
273 }
274
275 void VectorAnimationRenderer::SetRenderer( Renderer renderer )
276 {
277   Internal::Adaptor::GetImplementation( *this ).SetRenderer( renderer );
278 }
279
280 void VectorAnimationRenderer::SetSize( uint32_t width, uint32_t height )
281 {
282   Internal::Adaptor::GetImplementation( *this ).SetSize( width, height );
283 }
284
285 bool VectorAnimationRenderer::Render( uint32_t frameNumber )
286 {
287   return Internal::Adaptor::GetImplementation( *this ).Render( frameNumber );
288 }
289
290 uint32_t VectorAnimationRenderer::GetTotalFrameNumber() const
291 {
292   return Internal::Adaptor::GetImplementation( *this ).GetTotalFrameNumber();
293 }
294
295 float VectorAnimationRenderer::GetFrameRate() const
296 {
297   return Internal::Adaptor::GetImplementation( *this ).GetFrameRate();
298 }
299
300 void VectorAnimationRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
301 {
302   Internal::Adaptor::GetImplementation( *this ).GetDefaultSize( width, height );
303 }
304
305 void VectorAnimationRenderer::GetLayerInfo( Property::Map& map ) const
306 {
307 }
308
309 bool VectorAnimationRenderer::GetMarkerInfo( const std::string& marker, uint32_t& startFrame, uint32_t& endFrame ) const
310 {
311   return Internal::Adaptor::GetImplementation( *this ).GetMarkerInfo( marker, startFrame, endFrame );
312 }
313
314 VectorAnimationRenderer::UploadCompletedSignalType& VectorAnimationRenderer::UploadCompletedSignal()
315 {
316   return Internal::Adaptor::GetImplementation( *this ).UploadCompletedSignal();
317 }
318
319 } // namespace Dali
320
321 namespace Test
322 {
323 namespace VectorAnimationRenderer
324 {
325
326 void RequestTrigger()
327 {
328   Dali::Internal::Adaptor::VectorAnimationRenderer::mNeedTrigger = true;
329 }
330
331 void DelayRendering(uint32_t delay)
332 {
333   Dali::Internal::Adaptor::gVectorAnimationRenderer->mDelayTime = delay;
334 }
335
336 uint32_t GetDroppedFrames()
337 {
338   return Dali::Internal::Adaptor::gVectorAnimationRenderer->mDroppedFrames;
339 }
340
341 } // VectorAnimationRenderer
342 } // Test
343