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