[Tizen] Destroy removal visuals after idler
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / animated-vector-image-visual.cpp
1 /*
2  * Copyright (c) 2023 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/devel-api/adaptor-framework/window-devel.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/public-api/rendering/decorated-visual-renderer.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
33 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
34 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
35 #include <dali-toolkit/internal/visuals/image-visual-shader-feature-builder.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
38 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
40 #include <dali-toolkit/public-api/visuals/visual-properties.h>
41
42 namespace Dali
43 {
44 namespace Toolkit
45 {
46 namespace Internal
47 {
48 namespace
49 {
50 const int CUSTOM_PROPERTY_COUNT(1); // pixel area,
51
52 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
53
54 // stop behavior
55 DALI_ENUM_TO_STRING_TABLE_BEGIN(STOP_BEHAVIOR)
56   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME)
57   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME)
58   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME)
59 DALI_ENUM_TO_STRING_TABLE_END(STOP_BEHAVIOR)
60
61 // looping mode
62 DALI_ENUM_TO_STRING_TABLE_BEGIN(LOOPING_MODE)
63   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::LoopingMode, RESTART)
64   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelImageVisual::LoopingMode, AUTO_REVERSE)
65 DALI_ENUM_TO_STRING_TABLE_END(LOOPING_MODE)
66
67 #if defined(DEBUG_ENABLED)
68 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
69 #endif
70
71 } // unnamed namespace
72
73 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
74 {
75   AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, ImageDimensions{}));
76   visual->SetProperties(properties);
77   visual->Initialize();
78   return visual;
79 }
80
81 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
82 {
83   AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, size));
84   visual->Initialize();
85   return visual;
86 }
87
88 AnimatedVectorImageVisual::AnimatedVectorImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
89 : Visual::Base(factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>(Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE)),
90   mUrl(imageUrl),
91   mAnimationData(),
92   mVectorAnimationTask(new VectorAnimationTask(factoryCache)),
93   mImageVisualShaderFactory(shaderFactory),
94   mVisualSize(),
95   mVisualScale(Vector2::ONE),
96   mDesiredSize(size),
97   mPlacementActor(),
98   mPlayState(DevelImageVisual::PlayState::STOPPED),
99   mEventCallback(nullptr),
100   mLoadFailed(false),
101   mRendererAdded(false),
102   mCoreShutdown(false),
103   mRedrawInScalingDown(true)
104 {
105   // the rasterized image is with pre-multiplied alpha format
106   mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
107
108   // By default, load a file synchronously
109   mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
110 }
111
112 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
113 {
114   if(!mCoreShutdown)
115   {
116     auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
117     vectorAnimationManager.RemoveObserver(*this);
118
119     if(mEventCallback)
120     {
121       mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback(mEventCallback);
122       mEventCallback = nullptr;
123     }
124
125     // Finalize animation task and disconnect the signal in the main thread
126     mVectorAnimationTask->ResourceReadySignal().Disconnect(this, &AnimatedVectorImageVisual::OnResourceReady);
127     mVectorAnimationTask->Finalize();
128   }
129 }
130
131 void AnimatedVectorImageVisual::VectorAnimationManagerDestroyed()
132 {
133   // Core is shutting down. Don't talk to the plugin any more.
134   mCoreShutdown = true;
135 }
136
137 void AnimatedVectorImageVisual::GetNaturalSize(Vector2& naturalSize)
138 {
139   if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
140   {
141     naturalSize.x = mDesiredSize.GetWidth();
142     naturalSize.y = mDesiredSize.GetHeight();
143   }
144   else if(mVisualSize != Vector2::ZERO)
145   {
146     naturalSize = mVisualSize;
147   }
148   else
149   {
150     if(mLoadFailed && mImpl->mRenderer)
151     {
152       // Load failed, use broken image size
153       auto textureSet = mImpl->mRenderer.GetTextures();
154       if(textureSet && textureSet.GetTextureCount())
155       {
156         auto texture = textureSet.GetTexture(0);
157         if(texture)
158         {
159           naturalSize.x = texture.GetWidth();
160           naturalSize.y = texture.GetHeight();
161           return;
162         }
163       }
164     }
165     else
166     {
167       uint32_t width, height;
168       mVectorAnimationTask->GetDefaultSize(width, height);
169       naturalSize.x = width;
170       naturalSize.y = height;
171     }
172   }
173
174   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this);
175 }
176
177 void AnimatedVectorImageVisual::DoCreatePropertyMap(Property::Map& map) const
178 {
179   map.Clear();
180   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE);
181   if(mUrl.IsValid())
182   {
183     map.Insert(Toolkit::ImageVisual::Property::URL, mUrl.GetUrl());
184   }
185   map.Insert(Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount);
186
187   uint32_t startFrame, endFrame;
188   mVectorAnimationTask->GetPlayRange(startFrame, endFrame);
189
190   Property::Array playRange;
191   playRange.PushBack(static_cast<int32_t>(startFrame));
192   playRange.PushBack(static_cast<int32_t>(endFrame));
193   map.Insert(Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange);
194
195   map.Insert(Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast<int32_t>(mPlayState));
196   map.Insert(Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetCurrentFrameNumber()));
197   map.Insert(Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetTotalFrameNumber()));
198
199   map.Insert(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior);
200   map.Insert(Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode);
201   map.Insert(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, mRedrawInScalingDown);
202
203   Property::Map layerInfo;
204   mVectorAnimationTask->GetLayerInfo(layerInfo);
205   map.Insert(Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo);
206
207   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
208   map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
209   map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
210 }
211
212 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
213 {
214   // Do nothing
215 }
216
217 void AnimatedVectorImageVisual::EnablePreMultipliedAlpha(bool preMultiplied)
218 {
219   // Make always enable pre multiplied alpha whether preMultiplied value is false.
220   if(!preMultiplied)
221   {
222     DALI_LOG_WARNING("Note : AnimatedVectorVisual cannot disable PreMultipliedAlpha\n");
223   }
224 }
225
226 void AnimatedVectorImageVisual::DoSetProperties(const Property::Map& propertyMap)
227 {
228   // url already passed in from constructor
229   for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
230   {
231     KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
232     if(keyValue.first.type == Property::Key::INDEX)
233     {
234       DoSetProperty(keyValue.first.indexKey, keyValue.second);
235     }
236     else
237     {
238       if(keyValue.first == LOOP_COUNT_NAME)
239       {
240         DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second);
241       }
242       else if(keyValue.first == PLAY_RANGE_NAME)
243       {
244         DoSetProperty(Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second);
245       }
246       else if(keyValue.first == STOP_BEHAVIOR_NAME)
247       {
248         DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second);
249       }
250       else if(keyValue.first == LOOPING_MODE_NAME)
251       {
252         DoSetProperty(Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second);
253       }
254       else if(keyValue.first == REDRAW_IN_SCALING_DOWN_NAME)
255       {
256         DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
257       }
258       else if(keyValue.first == SYNCHRONOUS_LOADING)
259       {
260         DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
261       }
262       else if(keyValue.first == IMAGE_DESIRED_WIDTH)
263       {
264         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
265       }
266       else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
267       {
268         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
269       }
270     }
271   }
272
273   TriggerVectorRasterization();
274 }
275
276 void AnimatedVectorImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
277 {
278   switch(index)
279   {
280     case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
281     {
282       int32_t loopCount;
283       if(value.Get(loopCount))
284       {
285         mAnimationData.loopCount = loopCount;
286         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
287       }
288       break;
289     }
290     case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
291     {
292       const Property::Array* array = value.GetArray();
293       if(array)
294       {
295         mAnimationData.playRange = *array;
296         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
297       }
298       else if(value.GetType() == Property::STRING)
299       {
300         std::string markerName;
301         if(value.Get(markerName))
302         {
303           Property::Array array;
304           array.Add(markerName);
305           mAnimationData.playRange = std::move(array);
306           mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
307         }
308       }
309       break;
310     }
311     case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
312     {
313       int32_t stopBehavior = mAnimationData.stopBehavior;
314       if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior))
315       {
316         mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior);
317         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
318       }
319       break;
320     }
321     case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
322     {
323       int32_t loopingMode = mAnimationData.loopingMode;
324       if(Scripting::GetEnumerationProperty(value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode))
325       {
326         mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type(loopingMode);
327         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
328       }
329       break;
330     }
331     case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
332     {
333       bool redraw;
334       if(value.Get(redraw))
335       {
336         mRedrawInScalingDown = redraw;
337       }
338       break;
339     }
340     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
341     {
342       bool sync = false;
343       if(value.Get(sync))
344       {
345         if(sync)
346         {
347           mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
348         }
349         else
350         {
351           mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
352         }
353       }
354       break;
355     }
356     case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
357     {
358       int32_t desiredWidth = 0;
359       if(value.Get(desiredWidth))
360       {
361         mDesiredSize.SetWidth(desiredWidth);
362       }
363       break;
364     }
365
366     case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
367     {
368       int32_t desiredHeight = 0;
369       if(value.Get(desiredHeight))
370       {
371         mDesiredSize.SetHeight(desiredHeight);
372       }
373       break;
374     }
375   }
376 }
377
378 void AnimatedVectorImageVisual::OnInitialize(void)
379 {
380   mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
381   mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
382
383   mVectorAnimationTask->RequestLoad(mUrl.GetUrl(), IsSynchronousLoadingRequired());
384
385   auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
386   vectorAnimationManager.AddObserver(*this);
387
388   Shader shader = GenerateShader();
389
390   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
391
392   mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
393   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
394
395   TextureSet textureSet = TextureSet::New();
396   mImpl->mRenderer.SetTextures(textureSet);
397
398   // Register transform properties
399   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
400
401   mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
402 }
403
404 void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
405 {
406   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
407
408   // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
409   mPlacementActor = actor;
410
411   if(mLoadFailed)
412   {
413     Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
414     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
415     actor.AddRenderer(mImpl->mRenderer);
416     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
417   }
418   else
419   {
420     // Add property notification for scaling & size
421     mScaleNotification = actor.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
422     mScaleNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnScaleNotification);
423
424     mSizeNotification = actor.AddPropertyNotification(Actor::Property::SIZE, StepCondition(3.0f));
425     mSizeNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnSizeNotification);
426
427     DevelActor::VisibilityChangedSignal(actor).Connect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
428
429     Window window = DevelWindow::Get(actor);
430     if(window)
431     {
432       DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
433     }
434
435     if(mImpl->mEventObserver)
436     {
437       // The visual needs it's size set before it can be rasterized hence request relayout once on stage
438       mImpl->mEventObserver->RelayoutRequest(*this);
439     }
440
441     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
442     TriggerVectorRasterization();
443   }
444
445   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
446 }
447
448 void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
449 {
450   StopAnimation();
451   TriggerVectorRasterization();
452
453   if(mImpl->mRenderer)
454   {
455     actor.RemoveRenderer(mImpl->mRenderer);
456     mRendererAdded = false;
457   }
458
459   // Remove property notification
460   actor.RemovePropertyNotification(mScaleNotification);
461   actor.RemovePropertyNotification(mSizeNotification);
462
463   DevelActor::VisibilityChangedSignal(actor).Disconnect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
464
465   Window window = DevelWindow::Get(actor);
466   if(window)
467   {
468     DevelWindow::VisibilityChangedSignal(window).Disconnect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
469   }
470
471   mPlacementActor.Reset();
472
473   // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
474   mVisualSize           = Vector2::ZERO;
475   mVisualScale          = Vector2::ONE;
476   mAnimationData.width  = 0;
477   mAnimationData.height = 0;
478
479   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
480 }
481
482 void AnimatedVectorImageVisual::OnSetTransform()
483 {
484   Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
485
486   if(IsOnScene() && visualSize != mVisualSize)
487   {
488     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this);
489
490     mVisualSize = visualSize;
491
492     SetVectorImageSize();
493
494     if(mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
495     {
496       mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
497       mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
498     }
499
500     TriggerVectorRasterization();
501   }
502 }
503
504 void AnimatedVectorImageVisual::UpdateShader()
505 {
506   if(mImpl->mRenderer)
507   {
508     Shader shader = GenerateShader();
509     mImpl->mRenderer.SetShader(shader);
510   }
511 }
512
513 void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
514 {
515   // Check if action is valid for this visual type and perform action if possible
516   switch(actionId)
517   {
518     case DevelAnimatedVectorImageVisual::Action::PLAY:
519     {
520       if(IsOnScene() && mVisualSize != Vector2::ZERO)
521       {
522         if(mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
523         {
524           mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
525           mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
526         }
527       }
528       mPlayState = DevelImageVisual::PlayState::PLAYING;
529       break;
530     }
531     case DevelAnimatedVectorImageVisual::Action::PAUSE:
532     {
533       if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
534       {
535         mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
536         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
537       }
538       mPlayState = DevelImageVisual::PlayState::PAUSED;
539       break;
540     }
541     case DevelAnimatedVectorImageVisual::Action::STOP:
542     {
543       if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
544       {
545         mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
546         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
547       }
548       mPlayState = DevelImageVisual::PlayState::STOPPED;
549       break;
550     }
551     case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
552     {
553       int32_t frameNumber;
554       if(attributes.Get(frameNumber))
555       {
556         mAnimationData.currentFrame = frameNumber;
557         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
558       }
559       break;
560     }
561     case DevelAnimatedVectorImageVisual::Action::FLUSH:
562     {
563       if(DALI_LIKELY(!mCoreShutdown))
564       {
565         SendAnimationData();
566       }
567       break;
568     }
569   }
570
571   TriggerVectorRasterization();
572 }
573
574 void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
575 {
576   switch(actionId)
577   {
578     case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
579     {
580       DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
581       mAnimationData.dynamicProperties.push_back(info);
582       mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
583       break;
584     }
585   }
586
587   TriggerVectorRasterization();
588 }
589
590 void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
591 {
592   AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
593
594   if(status == VectorAnimationTask::ResourceStatus::LOADED)
595   {
596     if(mImpl->mEventObserver)
597     {
598       mImpl->mEventObserver->RelayoutRequest(*this);
599     }
600   }
601   else
602   {
603     mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
604
605     // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
606     Actor actor = mPlacementActor.GetHandle();
607     if(actor && !mRendererAdded)
608     {
609       if(!mLoadFailed)
610       {
611         actor.AddRenderer(mImpl->mRenderer);
612         ResourceReady(Toolkit::Visual::ResourceStatus::READY);
613       }
614       else
615       {
616         Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
617         mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
618         actor.AddRenderer(mImpl->mRenderer);
619         ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
620       }
621
622       mRendererAdded = true;
623     }
624   }
625
626   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
627 }
628
629 void AnimatedVectorImageVisual::OnAnimationFinished()
630 {
631   if(DALI_UNLIKELY(mImpl == nullptr))
632   {
633     DALI_LOG_ERROR("Fatal error!! already destroyed object callback called! AnimatedVectorImageVisual : %p, url : %s\n", this, mUrl.GetUrl().c_str());
634     return;
635   }
636
637   AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
638
639   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
640
641   if(mPlayState != DevelImageVisual::PlayState::STOPPED)
642   {
643     mPlayState = DevelImageVisual::PlayState::STOPPED;
644
645     mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
646
647     if(mImpl->mEventObserver)
648     {
649       mImpl->mEventObserver->NotifyVisualEvent(*this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED);
650     }
651   }
652
653   if(mImpl->mRenderer)
654   {
655     mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
656   }
657 }
658
659 void AnimatedVectorImageVisual::SendAnimationData()
660 {
661   if(DALI_UNLIKELY(mImpl == nullptr))
662   {
663     DALI_LOG_ERROR("Fatal error!! already destroyed object callback called! AnimatedVectorImageVisual : %p, url : %s\n", this, mUrl.GetUrl().c_str());
664     return;
665   }
666
667   if(mAnimationData.resendFlag)
668   {
669     mVectorAnimationTask->SetAnimationData(mAnimationData);
670
671     if(mImpl->mRenderer)
672     {
673       if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
674       {
675         mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY);
676       }
677       else
678       {
679         mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
680       }
681     }
682
683     mAnimationData.resendFlag = 0;
684   }
685 }
686
687 void AnimatedVectorImageVisual::SetVectorImageSize()
688 {
689   uint32_t width, height;
690   if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
691   {
692     width  = mDesiredSize.GetWidth();
693     height = mDesiredSize.GetHeight();
694   }
695   else
696   {
697     width  = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
698     height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
699   }
700
701   if(mAnimationData.width != width || mAnimationData.height != height)
702   {
703     mAnimationData.width  = width;
704     mAnimationData.height = height;
705     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
706   }
707 }
708
709 void AnimatedVectorImageVisual::StopAnimation()
710 {
711   if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
712   {
713     mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
714     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
715
716     mPlayState = DevelImageVisual::PlayState::STOPPED;
717   }
718 }
719
720 void AnimatedVectorImageVisual::TriggerVectorRasterization()
721 {
722   if(!mEventCallback && !mCoreShutdown)
723   {
724     mEventCallback               = MakeCallback(this, &AnimatedVectorImageVisual::OnProcessEvents);
725     auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
726     vectorAnimationManager.RegisterEventCallback(mEventCallback);
727     Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
728   }
729 }
730
731 void AnimatedVectorImageVisual::OnScaleNotification(PropertyNotification& source)
732 {
733   Actor actor = mPlacementActor.GetHandle();
734   if(actor)
735   {
736     Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
737
738     if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
739     {
740       mVisualScale.width  = scale.width;
741       mVisualScale.height = scale.height;
742
743       DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this);
744
745       SetVectorImageSize();
746       SendAnimationData();
747
748       Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
749     }
750   }
751 }
752
753 void AnimatedVectorImageVisual::OnSizeNotification(PropertyNotification& source)
754 {
755   Actor actor = mPlacementActor.GetHandle();
756   if(actor)
757   {
758     Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
759
760     if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
761     {
762       mVisualSize.width  = size.width;
763       mVisualSize.height = size.height;
764
765       DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
766
767       SetVectorImageSize();
768       SendAnimationData();
769
770       Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
771     }
772   }
773 }
774
775 void AnimatedVectorImageVisual::OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type)
776 {
777   if(!visible)
778   {
779     StopAnimation();
780     TriggerVectorRasterization();
781
782     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this);
783   }
784 }
785
786 void AnimatedVectorImageVisual::OnWindowVisibilityChanged(Window window, bool visible)
787 {
788   if(!visible)
789   {
790     StopAnimation();
791     TriggerVectorRasterization();
792
793     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this);
794   }
795 }
796
797 void AnimatedVectorImageVisual::OnProcessEvents()
798 {
799   SendAnimationData();
800
801   mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
802 }
803
804 Shader AnimatedVectorImageVisual::GenerateShader() const
805 {
806   Shader shader;
807   if(mImpl->mCustomShader)
808   {
809     shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
810                          mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
811                          mImpl->mCustomShader->mHints);
812
813     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
814   }
815   else
816   {
817     shader = mImageVisualShaderFactory.GetShader(
818       mFactoryCache,
819       ImageVisualShaderFeatureBuilder()
820         .EnableRoundedCorner(IsRoundedCornerRequired())
821         .EnableBorderline(IsBorderlineRequired()));
822   }
823   return shader;
824 }
825
826 } // namespace Internal
827
828 } // namespace Toolkit
829
830 } // namespace Dali