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