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