[dali_1.4.33] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / animated-vector-image-visual.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/animated-vector-image-visual.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/stage.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
31 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 constexpr auto LOOP_FOREVER = -1;
49
50 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
51
52 // Flags for re-sending data to the rasterize thread
53 enum Flags
54 {
55   RESEND_PLAY_RANGE    = 1 << 0,
56   RESEND_LOOP_COUNT    = 1 << 1,
57   RESEND_STOP_BEHAVIOR = 1 << 2,
58   RESEND_LOOPING_MODE  = 1 << 3
59 };
60
61 // stop behavior
62 DALI_ENUM_TO_STRING_TABLE_BEGIN( STOP_BEHAVIOR )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME )
66 DALI_ENUM_TO_STRING_TABLE_END( STOP_BEHAVIOR )
67
68 // looping mode
69 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOOPING_MODE )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, RESTART )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, AUTO_REVERSE )
72 DALI_ENUM_TO_STRING_TABLE_END( LOOPING_MODE )
73
74 #if defined(DEBUG_ENABLED)
75 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
76 #endif
77
78 } // unnamed namespace
79
80 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
81 {
82   AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
83   visual->SetProperties( properties );
84
85   return visual;
86 }
87
88 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
89 {
90   AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
91
92   return visual;
93 }
94
95 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
96 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
97   mImageVisualShaderFactory( shaderFactory ),
98   mUrl( imageUrl ),
99   mVectorRasterizeThread( imageUrl.GetUrl() ),
100   mVisualSize(),
101   mPlacementActor(),
102   mLoopCount( LOOP_FOREVER ),
103   mStartFrame( 0 ),
104   mEndFrame( 0 ),
105   mResendFlag( 0 ),
106   mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP ),
107   mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
108   mLoopingMode( DevelImageVisual::LoopingMode::RESTART )
109 {
110   // the rasterized image is with pre-multiplied alpha format
111   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
112
113   mVectorRasterizeThread.SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
114   mVectorRasterizeThread.SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
115
116   mVectorRasterizeThread.Start();
117 }
118
119 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
120 {
121 }
122
123 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
124 {
125   if( mImpl->mRenderer ) // Check if we have a rendered image
126   {
127     auto textureSet = mImpl->mRenderer.GetTextures();
128     if( textureSet )
129     {
130       if( textureSet.GetTextureCount() > 0 )
131       {
132         auto texture = textureSet.GetTexture( 0 );
133         naturalSize.x = texture.GetWidth();
134         naturalSize.y = texture.GetHeight();
135         return;
136       }
137     }
138   }
139
140   uint32_t width, height;
141   mVectorRasterizeThread.GetDefaultSize( width, height );
142   naturalSize.x = width;
143   naturalSize.y = height;
144 }
145
146 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
147 {
148   map.Clear();
149   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
150   if( mUrl.IsValid() )
151   {
152     map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
153   }
154   map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
155
156   Property::Array playRange;
157   playRange.PushBack( mStartFrame );
158   playRange.PushBack( mEndFrame );
159   map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
160
161   map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread.GetPlayState() ) );
162   map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorRasterizeThread.GetCurrentFrameNumber() ) );
163   map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorRasterizeThread.GetTotalFrameNumber() ) );
164
165   map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mStopBehavior );
166   map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mLoopingMode );
167 }
168
169 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
170 {
171   // Do nothing
172 }
173
174 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
175 {
176   // url already passed in from constructor
177   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
178   {
179     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
180     if( keyValue.first.type == Property::Key::INDEX )
181     {
182       DoSetProperty( keyValue.first.indexKey, keyValue.second );
183     }
184     else
185     {
186        if( keyValue.first == LOOP_COUNT_NAME )
187        {
188           DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
189        }
190        else if( keyValue.first == PLAY_RANGE_NAME )
191        {
192           DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
193        }
194        else if( keyValue.first == STOP_BEHAVIOR_NAME )
195        {
196           DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
197        }
198        else if( keyValue.first == LOOPING_MODE_NAME )
199        {
200           DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
201        }
202     }
203   }
204 }
205
206 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
207 {
208   switch(index)
209   {
210     case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
211     {
212       int32_t loopCount;
213       if( value.Get( loopCount ) )
214       {
215         mLoopCount = loopCount;
216         mResendFlag |= RESEND_LOOP_COUNT;
217       }
218       break;
219     }
220     case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
221     {
222       Property::Array* array = value.GetArray();
223       if( array )
224       {
225         size_t count = array->Count();
226         if( count >= 2 )
227         {
228           int startFrame, endFrame;
229           int totalFrame = mVectorRasterizeThread.GetTotalFrameNumber();
230           array->GetElementAt( 0 ).Get( startFrame );
231           array->GetElementAt( 1 ).Get( endFrame );
232
233           if( startFrame >= 0 && startFrame < totalFrame && endFrame >= 0 && endFrame < totalFrame )
234           {
235             mStartFrame = startFrame;
236             mEndFrame = endFrame;
237             mResendFlag |= RESEND_PLAY_RANGE;
238           }
239           else
240           {
241             DALI_LOG_ERROR( "Invalid play range [%d, %d / %d]\n", startFrame, endFrame, totalFrame );
242           }
243         }
244       }
245       break;
246     }
247     case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
248     {
249       int32_t stopBehavior;
250       Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior );
251       mStopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
252       mResendFlag |= RESEND_STOP_BEHAVIOR;
253       break;
254     }
255     case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
256     {
257       int32_t loopingMode;
258       Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode );
259       mLoopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
260       mResendFlag |= RESEND_LOOPING_MODE;
261       break;
262     }
263   }
264 }
265
266 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
267 {
268   Shader shader;
269
270   if( mImpl->mCustomShader )
271   {
272     shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
273                           mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
274                           mImpl->mCustomShader->mHints );
275
276     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
277   }
278   else
279   {
280     shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
281   }
282
283   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
284
285   mImpl->mRenderer = Renderer::New( geometry, shader );
286
287   TextureSet textureSet = TextureSet::New();
288   mImpl->mRenderer.SetTextures( textureSet );
289
290   // Register transform properties
291   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
292
293   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
294
295   // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
296   mPlacementActor = actor;
297
298   mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
299
300   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
301 }
302
303 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
304 {
305   mVectorRasterizeThread.PauseAnimation();
306
307   mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
308
309   if( mImpl->mRenderer )
310   {
311     mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
312
313     actor.RemoveRenderer( mImpl->mRenderer );
314     mImpl->mRenderer.Reset();
315   }
316
317   mPlacementActor.Reset();
318
319   // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
320   mVisualSize = Vector2::ZERO;
321
322   DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffStage [%p]\n", this );
323 }
324
325 void AnimatedVectorImageVisual::OnSetTransform()
326 {
327   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
328
329   if( IsOnStage() )
330   {
331     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
332
333     if( visualSize != mVisualSize )
334     {
335       mVisualSize = visualSize;
336
337       uint32_t width = static_cast< uint32_t >( visualSize.width );
338       uint32_t height = static_cast< uint32_t >( visualSize.height );
339
340       mVectorRasterizeThread.SetSize( width, height );
341     }
342
343     SendAnimationData();
344
345     if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
346     {
347       mVectorRasterizeThread.PlayAnimation();
348
349       mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
350     }
351     else
352     {
353       // Render one frame
354       mVectorRasterizeThread.RenderFrame();
355     }
356
357     if( mVectorRasterizeThread.IsResourceReady() )
358     {
359       Actor actor = mPlacementActor.GetHandle();
360       if( actor )
361       {
362         actor.AddRenderer( mImpl->mRenderer );
363         mPlacementActor.Reset();
364
365         DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: Renderer is added [%p]\n", this );
366       }
367
368       ResourceReady( Toolkit::Visual::ResourceStatus::READY );
369     }
370   }
371 }
372
373 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
374 {
375   // Check if action is valid for this visual type and perform action if possible
376   switch( actionId )
377   {
378     case DevelAnimatedVectorImageVisual::Action::PLAY:
379     {
380       if( IsOnStage() && mVisualSize != Vector2::ZERO )
381       {
382         mVectorRasterizeThread.PlayAnimation();
383
384         mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
385       }
386       mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
387       break;
388     }
389     case DevelAnimatedVectorImageVisual::Action::PAUSE:
390     {
391       mVectorRasterizeThread.PauseAnimation();
392
393       if( mImpl->mRenderer )
394       {
395         mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
396       }
397
398       mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
399       break;
400     }
401     case DevelAnimatedVectorImageVisual::Action::STOP:
402     {
403       if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
404       {
405         mVectorRasterizeThread.StopAnimation();
406
407         OnAnimationFinished();
408       }
409       mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
410       break;
411     }
412     case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
413     {
414       int32_t frameNumber;
415       if( attributes.Get( frameNumber ) )
416       {
417         mVectorRasterizeThread.SetCurrentFrameNumber( frameNumber );
418
419         if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
420         {
421           mVectorRasterizeThread.RenderFrame();
422           Stage::GetCurrent().KeepRendering( 0.0f );    // Trigger rendering
423         }
424       }
425       break;
426     }
427     case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
428     {
429       Property::Map* map = attributes.GetMap();
430       if( map )
431       {
432         DoSetProperties( *map );
433
434         SendAnimationData();
435       }
436       break;
437     }
438   }
439 }
440
441 void AnimatedVectorImageVisual::OnResourceReady()
442 {
443   // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
444   Actor actor = mPlacementActor.GetHandle();
445   if( actor )
446   {
447     actor.AddRenderer( mImpl->mRenderer );
448     // reset the weak handle so that the renderer only get added to actor once
449     mPlacementActor.Reset();
450
451     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
452
453     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnResourceReady: Renderer is added [%p]\n", this );
454   }
455 }
456
457 void AnimatedVectorImageVisual::OnAnimationFinished()
458 {
459   if( mImpl->mEventObserver )
460   {
461     mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
462   }
463
464   mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
465
466   if( mImpl->mRenderer )
467   {
468     mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
469   }
470 }
471
472 void AnimatedVectorImageVisual::SendAnimationData()
473 {
474   if( mResendFlag )
475   {
476     bool isPlaying = false;
477     if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
478     {
479       mVectorRasterizeThread.PauseAnimation();
480       isPlaying = true;
481     }
482
483     if( mResendFlag & RESEND_LOOP_COUNT )
484     {
485       mVectorRasterizeThread.SetLoopCount( mLoopCount );
486     }
487
488     if( mResendFlag & RESEND_PLAY_RANGE )
489     {
490       mVectorRasterizeThread.SetPlayRange( mStartFrame, mEndFrame );
491     }
492
493     if( mResendFlag & RESEND_STOP_BEHAVIOR )
494     {
495       mVectorRasterizeThread.SetStopBehavior( mStopBehavior );
496     }
497
498     if( mResendFlag & RESEND_LOOPING_MODE )
499     {
500       mVectorRasterizeThread.SetLoopingMode( mLoopingMode );
501     }
502
503     if( IsOnStage() )
504     {
505       if( isPlaying )
506       {
507         mVectorRasterizeThread.PlayAnimation();
508       }
509       else
510       {
511         mVectorRasterizeThread.RenderFrame();
512         Stage::GetCurrent().KeepRendering( 0.0f );
513       }
514     }
515
516     mResendFlag = 0;
517   }
518 }
519
520 } // namespace Internal
521
522 } // namespace Toolkit
523
524 } // namespace Dali