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