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