(AnimatedVector) Get marker informations
[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   mImageUrl(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(mImageUrl.IsValid())
182   {
183     map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.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   Property::Map markerInfo;
208   mVectorAnimationTask->GetMarkerInfo(markerInfo);
209   map.Insert(Toolkit::DevelImageVisual::Property::MARKER_INFO, markerInfo);
210
211   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
212   map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
213   map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
214 }
215
216 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
217 {
218   // Do nothing
219 }
220
221 void AnimatedVectorImageVisual::EnablePreMultipliedAlpha(bool preMultiplied)
222 {
223   // Make always enable pre multiplied alpha whether preMultiplied value is false.
224   if(!preMultiplied)
225   {
226     DALI_LOG_WARNING("Note : AnimatedVectorVisual cannot disable PreMultipliedAlpha\n");
227   }
228 }
229
230 void AnimatedVectorImageVisual::DoSetProperties(const Property::Map& propertyMap)
231 {
232   // url already passed in from constructor
233   for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
234   {
235     KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
236     if(keyValue.first.type == Property::Key::INDEX)
237     {
238       DoSetProperty(keyValue.first.indexKey, keyValue.second);
239     }
240     else
241     {
242       if(keyValue.first == LOOP_COUNT_NAME)
243       {
244         DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second);
245       }
246       else if(keyValue.first == PLAY_RANGE_NAME)
247       {
248         DoSetProperty(Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second);
249       }
250       else if(keyValue.first == STOP_BEHAVIOR_NAME)
251       {
252         DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second);
253       }
254       else if(keyValue.first == LOOPING_MODE_NAME)
255       {
256         DoSetProperty(Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second);
257       }
258       else if(keyValue.first == REDRAW_IN_SCALING_DOWN_NAME)
259       {
260         DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
261       }
262       else if(keyValue.first == SYNCHRONOUS_LOADING)
263       {
264         DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
265       }
266       else if(keyValue.first == IMAGE_DESIRED_WIDTH)
267       {
268         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
269       }
270       else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
271       {
272         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
273       }
274     }
275   }
276
277   TriggerVectorRasterization();
278 }
279
280 void AnimatedVectorImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
281 {
282   switch(index)
283   {
284     case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
285     {
286       int32_t loopCount;
287       if(value.Get(loopCount))
288       {
289         mAnimationData.loopCount = loopCount;
290         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
291       }
292       break;
293     }
294     case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
295     {
296       const Property::Array* array = value.GetArray();
297       if(array)
298       {
299         mAnimationData.playRange = *array;
300         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
301       }
302       else if(value.GetType() == Property::STRING)
303       {
304         std::string markerName;
305         if(value.Get(markerName))
306         {
307           Property::Array array;
308           array.Add(markerName);
309           mAnimationData.playRange = std::move(array);
310           mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
311         }
312       }
313       break;
314     }
315     case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
316     {
317       int32_t stopBehavior = mAnimationData.stopBehavior;
318       if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior))
319       {
320         mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior);
321         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
322       }
323       break;
324     }
325     case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
326     {
327       int32_t loopingMode = mAnimationData.loopingMode;
328       if(Scripting::GetEnumerationProperty(value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode))
329       {
330         mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type(loopingMode);
331         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
332       }
333       break;
334     }
335     case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
336     {
337       bool redraw;
338       if(value.Get(redraw))
339       {
340         mRedrawInScalingDown = redraw;
341       }
342       break;
343     }
344     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
345     {
346       bool sync = false;
347       if(value.Get(sync))
348       {
349         if(sync)
350         {
351           mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
352         }
353         else
354         {
355           mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
356         }
357       }
358       break;
359     }
360     case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
361     {
362       int32_t desiredWidth = 0;
363       if(value.Get(desiredWidth))
364       {
365         mDesiredSize.SetWidth(desiredWidth);
366       }
367       break;
368     }
369
370     case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
371     {
372       int32_t desiredHeight = 0;
373       if(value.Get(desiredHeight))
374       {
375         mDesiredSize.SetHeight(desiredHeight);
376       }
377       break;
378     }
379   }
380 }
381
382 void AnimatedVectorImageVisual::OnInitialize(void)
383 {
384   mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
385   mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
386
387   mVectorAnimationTask->RequestLoad(mImageUrl, IsSynchronousLoadingRequired());
388
389   auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
390   vectorAnimationManager.AddObserver(*this);
391
392   Shader shader = GenerateShader();
393
394   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
395
396   mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
397   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
398
399   TextureSet textureSet = TextureSet::New();
400   mImpl->mRenderer.SetTextures(textureSet);
401
402   // Register transform properties
403   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
404
405   mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
406 }
407
408 void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
409 {
410   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
411
412   // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
413   mPlacementActor = actor;
414
415   if(mLoadFailed)
416   {
417     Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
418     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
419     actor.AddRenderer(mImpl->mRenderer);
420     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
421   }
422   else
423   {
424     // Add property notification for scaling & size
425     mScaleNotification = actor.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
426     mScaleNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnScaleNotification);
427
428     mSizeNotification = actor.AddPropertyNotification(Actor::Property::SIZE, StepCondition(3.0f));
429     mSizeNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnSizeNotification);
430
431     DevelActor::VisibilityChangedSignal(actor).Connect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
432
433     Window window = DevelWindow::Get(actor);
434     if(window)
435     {
436       DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
437     }
438
439     if(mImpl->mEventObserver)
440     {
441       // The visual needs it's size set before it can be rasterized hence request relayout once on stage
442       mImpl->mEventObserver->RelayoutRequest(*this);
443     }
444
445     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
446     TriggerVectorRasterization();
447   }
448
449   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
450 }
451
452 void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
453 {
454   StopAnimation();
455   TriggerVectorRasterization();
456
457   if(mImpl->mRenderer)
458   {
459     actor.RemoveRenderer(mImpl->mRenderer);
460     mRendererAdded = false;
461   }
462
463   // Remove property notification
464   actor.RemovePropertyNotification(mScaleNotification);
465   actor.RemovePropertyNotification(mSizeNotification);
466
467   DevelActor::VisibilityChangedSignal(actor).Disconnect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
468
469   Window window = DevelWindow::Get(actor);
470   if(window)
471   {
472     DevelWindow::VisibilityChangedSignal(window).Disconnect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
473   }
474
475   mPlacementActor.Reset();
476
477   // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
478   mVisualSize           = Vector2::ZERO;
479   mVisualScale          = Vector2::ONE;
480   mAnimationData.width  = 0;
481   mAnimationData.height = 0;
482
483   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
484 }
485
486 void AnimatedVectorImageVisual::OnSetTransform()
487 {
488   Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
489
490   if(IsOnScene() && visualSize != mVisualSize)
491   {
492     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this);
493
494     mVisualSize = visualSize;
495
496     SetVectorImageSize();
497
498     if(mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
499     {
500       mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
501       mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
502     }
503
504     TriggerVectorRasterization();
505   }
506 }
507
508 void AnimatedVectorImageVisual::UpdateShader()
509 {
510   if(mImpl->mRenderer)
511   {
512     Shader shader = GenerateShader();
513     mImpl->mRenderer.SetShader(shader);
514   }
515 }
516
517 void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
518 {
519   // Check if action is valid for this visual type and perform action if possible
520   switch(actionId)
521   {
522     case DevelAnimatedVectorImageVisual::Action::PLAY:
523     {
524       if(IsOnScene() && mVisualSize != Vector2::ZERO)
525       {
526         if(mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
527         {
528           mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
529           mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
530         }
531       }
532       mPlayState = DevelImageVisual::PlayState::PLAYING;
533       break;
534     }
535     case DevelAnimatedVectorImageVisual::Action::PAUSE:
536     {
537       if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
538       {
539         mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
540         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
541       }
542       mPlayState = DevelImageVisual::PlayState::PAUSED;
543       break;
544     }
545     case DevelAnimatedVectorImageVisual::Action::STOP:
546     {
547       if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
548       {
549         mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
550         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
551       }
552       mPlayState = DevelImageVisual::PlayState::STOPPED;
553       break;
554     }
555     case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
556     {
557       int32_t frameNumber;
558       if(attributes.Get(frameNumber))
559       {
560         mAnimationData.currentFrame = frameNumber;
561         mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
562       }
563       break;
564     }
565     case DevelAnimatedVectorImageVisual::Action::FLUSH:
566     {
567       if(DALI_LIKELY(!mCoreShutdown))
568       {
569         SendAnimationData();
570       }
571       break;
572     }
573   }
574
575   TriggerVectorRasterization();
576 }
577
578 void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
579 {
580   switch(actionId)
581   {
582     case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
583     {
584       DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
585       mAnimationData.dynamicProperties.push_back(info);
586       mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
587       break;
588     }
589   }
590
591   TriggerVectorRasterization();
592 }
593
594 void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
595 {
596   AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
597
598   if(status == VectorAnimationTask::ResourceStatus::LOADED)
599   {
600     if(mImpl->mEventObserver)
601     {
602       mImpl->mEventObserver->RelayoutRequest(*this);
603     }
604   }
605   else
606   {
607     mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
608
609     // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
610     Actor actor = mPlacementActor.GetHandle();
611     if(actor && !mRendererAdded)
612     {
613       if(!mLoadFailed)
614       {
615         actor.AddRenderer(mImpl->mRenderer);
616         ResourceReady(Toolkit::Visual::ResourceStatus::READY);
617       }
618       else
619       {
620         Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
621         mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
622         actor.AddRenderer(mImpl->mRenderer);
623         ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
624       }
625
626       mRendererAdded = true;
627     }
628   }
629
630   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
631 }
632
633 void AnimatedVectorImageVisual::OnAnimationFinished()
634 {
635   AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
636
637   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
638
639   if(mPlayState != DevelImageVisual::PlayState::STOPPED)
640   {
641     mPlayState = DevelImageVisual::PlayState::STOPPED;
642
643     mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
644
645     if(mImpl->mEventObserver)
646     {
647       mImpl->mEventObserver->NotifyVisualEvent(*this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED);
648     }
649   }
650
651   if(mImpl->mRenderer)
652   {
653     mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
654   }
655 }
656
657 void AnimatedVectorImageVisual::SendAnimationData()
658 {
659   if(mAnimationData.resendFlag)
660   {
661     mVectorAnimationTask->SetAnimationData(mAnimationData);
662
663     if(mImpl->mRenderer)
664     {
665       if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
666       {
667         mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY);
668       }
669       else
670       {
671         mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
672       }
673     }
674
675     mAnimationData.resendFlag = 0;
676   }
677 }
678
679 void AnimatedVectorImageVisual::SetVectorImageSize()
680 {
681   uint32_t width, height;
682   if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
683   {
684     width  = mDesiredSize.GetWidth();
685     height = mDesiredSize.GetHeight();
686   }
687   else
688   {
689     width  = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
690     height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
691   }
692
693   if(mAnimationData.width != width || mAnimationData.height != height)
694   {
695     mAnimationData.width  = width;
696     mAnimationData.height = height;
697     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
698   }
699 }
700
701 void AnimatedVectorImageVisual::StopAnimation()
702 {
703   if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
704   {
705     mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
706     mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
707
708     mPlayState = DevelImageVisual::PlayState::STOPPED;
709   }
710 }
711
712 void AnimatedVectorImageVisual::TriggerVectorRasterization()
713 {
714   if(!mEventCallback && !mCoreShutdown)
715   {
716     mEventCallback               = MakeCallback(this, &AnimatedVectorImageVisual::OnProcessEvents);
717     auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
718     vectorAnimationManager.RegisterEventCallback(mEventCallback);
719     Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
720   }
721 }
722
723 void AnimatedVectorImageVisual::OnScaleNotification(PropertyNotification& source)
724 {
725   Actor actor = mPlacementActor.GetHandle();
726   if(actor)
727   {
728     Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
729
730     if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
731     {
732       mVisualScale.width  = scale.width;
733       mVisualScale.height = scale.height;
734
735       DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this);
736
737       SetVectorImageSize();
738       SendAnimationData();
739
740       Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
741     }
742   }
743 }
744
745 void AnimatedVectorImageVisual::OnSizeNotification(PropertyNotification& source)
746 {
747   Actor actor = mPlacementActor.GetHandle();
748   if(actor)
749   {
750     Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
751
752     if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
753     {
754       mVisualSize.width  = size.width;
755       mVisualSize.height = size.height;
756
757       DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
758
759       SetVectorImageSize();
760       SendAnimationData();
761
762       Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
763     }
764   }
765 }
766
767 void AnimatedVectorImageVisual::OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type)
768 {
769   if(!visible)
770   {
771     StopAnimation();
772     TriggerVectorRasterization();
773
774     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this);
775   }
776 }
777
778 void AnimatedVectorImageVisual::OnWindowVisibilityChanged(Window window, bool visible)
779 {
780   if(!visible)
781   {
782     StopAnimation();
783     TriggerVectorRasterization();
784
785     DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this);
786   }
787 }
788
789 void AnimatedVectorImageVisual::OnProcessEvents()
790 {
791   SendAnimationData();
792
793   mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
794 }
795
796 Shader AnimatedVectorImageVisual::GenerateShader() const
797 {
798   Shader shader;
799   if(mImpl->mCustomShader)
800   {
801     shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
802                          mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
803                          mImpl->mCustomShader->mHints);
804
805     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
806   }
807   else
808   {
809     shader = mImageVisualShaderFactory.GetShader(
810       mFactoryCache,
811       ImageVisualShaderFeatureBuilder()
812         .EnableRoundedCorner(IsRoundedCornerRequired())
813         .EnableBorderline(IsBorderlineRequired()));
814   }
815   return shader;
816 }
817
818 } // namespace Internal
819
820 } // namespace Toolkit
821
822 } // namespace Dali