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