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