2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/visuals/animated-vector-image/animated-vector-image-visual.h>
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>
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>
51 const int CUSTOM_PROPERTY_COUNT(1); // pixel area,
53 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
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)
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)
68 #if defined(DEBUG_ENABLED)
69 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
72 } // unnamed namespace
74 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
76 AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, ImageDimensions{}));
77 visual->SetProperties(properties);
82 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
84 AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, size));
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)),
93 mVectorAnimationTask(new VectorAnimationTask(factoryCache)),
94 mImageVisualShaderFactory(shaderFactory),
96 mVisualScale(Vector2::ONE),
99 mPlayState(DevelImageVisual::PlayState::STOPPED),
100 mEventCallback(nullptr),
101 mLastSentPlayStateId(0u),
103 mRendererAdded(false),
104 mCoreShutdown(false),
105 mRedrawInScalingDown(true),
106 mEnableFrameCache(false),
107 mUseNativeImage(false),
108 mNotifyAfterRasterization(false)
110 // the rasterized image is with pre-multiplied alpha format
111 mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
113 // By default, load a file synchronously
114 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
117 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
121 if(mImageUrl.IsBufferResource())
123 TextureManager& textureManager = mFactoryCache.GetTextureManager();
124 textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
127 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
128 vectorAnimationManager.RemoveObserver(*this);
132 mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback(mEventCallback);
133 mEventCallback = nullptr;
136 // Finalize animation task and disconnect the signal in the main thread
137 mVectorAnimationTask->ResourceReadySignal().Disconnect(this, &AnimatedVectorImageVisual::OnResourceReady);
138 mVectorAnimationTask->Finalize();
142 void AnimatedVectorImageVisual::VectorAnimationManagerDestroyed()
144 // Core is shutting down. Don't talk to the plugin any more.
145 mCoreShutdown = true;
148 void AnimatedVectorImageVisual::GetNaturalSize(Vector2& naturalSize)
150 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
152 naturalSize.x = mDesiredSize.GetWidth();
153 naturalSize.y = mDesiredSize.GetHeight();
155 else if(mVisualSize != Vector2::ZERO)
157 naturalSize = mVisualSize;
161 if(mLoadFailed && mImpl->mRenderer)
163 // Load failed, use broken image size
164 auto textureSet = mImpl->mRenderer.GetTextures();
165 if(textureSet && textureSet.GetTextureCount())
167 auto texture = textureSet.GetTexture(0);
170 naturalSize.x = texture.GetWidth();
171 naturalSize.y = texture.GetHeight();
178 uint32_t width, height;
179 mVectorAnimationTask->GetDefaultSize(width, height);
180 naturalSize.x = width;
181 naturalSize.y = height;
185 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this);
188 void AnimatedVectorImageVisual::DoCreatePropertyMap(Property::Map& map) const
191 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE);
192 if(mImageUrl.IsValid())
194 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
196 map.Insert(Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount);
198 uint32_t startFrame, endFrame;
199 mVectorAnimationTask->GetPlayRange(startFrame, endFrame);
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);
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()));
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);
214 Property::Map layerInfo;
215 mVectorAnimationTask->GetLayerInfo(layerInfo);
216 map.Insert(Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo);
218 Property::Map markerInfo;
219 mVectorAnimationTask->GetMarkerInfo(markerInfo);
220 map.Insert(Toolkit::DevelImageVisual::Property::MARKER_INFO, markerInfo);
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);
229 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
234 void AnimatedVectorImageVisual::EnablePreMultipliedAlpha(bool preMultiplied)
236 // Make always enable pre multiplied alpha whether preMultiplied value is false.
239 DALI_LOG_WARNING("Note : AnimatedVectorVisual cannot disable PreMultipliedAlpha\n");
243 void AnimatedVectorImageVisual::DoSetProperties(const Property::Map& propertyMap)
245 // url already passed in from constructor
246 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
248 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
249 if(keyValue.first.type == Property::Key::INDEX)
251 DoSetProperty(keyValue.first.indexKey, keyValue.second);
255 if(keyValue.first == LOOP_COUNT_NAME)
257 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second);
259 else if(keyValue.first == PLAY_RANGE_NAME)
261 DoSetProperty(Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second);
263 else if(keyValue.first == STOP_BEHAVIOR_NAME)
265 DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second);
267 else if(keyValue.first == LOOPING_MODE_NAME)
269 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second);
271 else if(keyValue.first == REDRAW_IN_SCALING_DOWN_NAME)
273 DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
275 else if(keyValue.first == SYNCHRONOUS_LOADING)
277 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
279 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
281 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
283 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
285 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
287 else if(keyValue.first == ENABLE_FRAME_CACHE)
289 DoSetProperty(Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE, keyValue.second);
291 else if(keyValue.first == NOTIFY_AFTER_RASTERIZATION)
293 DoSetProperty(Toolkit::DevelImageVisual::Property::NOTIFY_AFTER_RASTERIZATION, keyValue.second);
298 TriggerVectorRasterization();
301 void AnimatedVectorImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
305 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
308 if(value.Get(loopCount))
310 mAnimationData.loopCount = loopCount;
311 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
315 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
317 const Property::Array* array = value.GetArray();
320 mAnimationData.playRange = *array;
321 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
323 else if(value.GetType() == Property::STRING)
325 std::string markerName;
326 if(value.Get(markerName))
328 Property::Array array;
329 array.Add(markerName);
330 mAnimationData.playRange = std::move(array);
331 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
336 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
338 int32_t stopBehavior = mAnimationData.stopBehavior;
339 if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior))
341 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior);
342 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
346 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
348 int32_t loopingMode = mAnimationData.loopingMode;
349 if(Scripting::GetEnumerationProperty(value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode))
351 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type(loopingMode);
352 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
356 case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
359 if(value.Get(redraw))
361 mRedrawInScalingDown = redraw;
365 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
372 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
376 mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
381 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
383 int32_t desiredWidth = 0;
384 if(value.Get(desiredWidth))
386 mDesiredSize.SetWidth(desiredWidth);
391 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
393 int32_t desiredHeight = 0;
394 if(value.Get(desiredHeight))
396 mDesiredSize.SetHeight(desiredHeight);
401 case Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE:
403 bool enableFrameCache = false;
404 if(value.Get(enableFrameCache))
406 mEnableFrameCache = enableFrameCache;
407 if(mVectorAnimationTask)
409 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
415 case Toolkit::DevelImageVisual::Property::NOTIFY_AFTER_RASTERIZATION:
417 bool notifyAfterRasterization = false;
418 if(value.Get(notifyAfterRasterization))
420 if(mNotifyAfterRasterization != notifyAfterRasterization)
422 mNotifyAfterRasterization = notifyAfterRasterization;
424 mAnimationData.notifyAfterRasterization = mNotifyAfterRasterization;
425 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NOTIFY_AFTER_RASTERIZATION;
433 void AnimatedVectorImageVisual::OnInitialize(void)
435 mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
436 mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
437 mVectorAnimationTask->SetForceRenderOnceCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnForceRendering));
439 EncodedImageBuffer encodedImageBuffer;
441 if(mImageUrl.IsBufferResource())
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());
449 encodedImageBuffer = textureManager.GetEncodedImageBuffer(mImageUrl.GetUrl());
452 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
453 mVectorAnimationTask->RequestLoad(mImageUrl, encodedImageBuffer, IsSynchronousLoadingRequired());
455 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
456 vectorAnimationManager.AddObserver(*this);
458 Shader shader = GenerateShader();
460 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
462 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
463 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
465 TextureSet textureSet = TextureSet::New();
466 mImpl->mRenderer.SetTextures(textureSet);
468 // Register transform properties
469 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
471 mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
474 void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
476 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
478 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
479 mPlacementActor = actor;
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);
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);
495 mSizeNotification = actor.AddPropertyNotification(Actor::Property::SIZE, StepCondition(3.0f));
496 mSizeNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnSizeNotification);
498 DevelActor::VisibilityChangedSignal(actor).Connect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
500 Window window = DevelWindow::Get(actor);
503 DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
506 if(mImpl->mEventObserver)
508 // The visual needs it's size set before it can be rasterized hence request relayout once on stage
509 mImpl->mEventObserver->RelayoutRequest(*this);
512 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
513 TriggerVectorRasterization();
516 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
519 void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
522 TriggerVectorRasterization();
526 actor.RemoveRenderer(mImpl->mRenderer);
527 mRendererAdded = false;
530 // Remove property notification
531 actor.RemovePropertyNotification(mScaleNotification);
532 actor.RemovePropertyNotification(mSizeNotification);
534 DevelActor::VisibilityChangedSignal(actor).Disconnect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
536 Window window = DevelWindow::Get(actor);
539 DevelWindow::VisibilityChangedSignal(window).Disconnect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
542 mPlacementActor.Reset();
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;
550 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
553 void AnimatedVectorImageVisual::OnSetTransform()
555 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
557 if(IsOnScene() && visualSize != mVisualSize)
559 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this);
561 mVisualSize = visualSize;
563 SetVectorImageSize();
565 if(mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
567 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
568 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
571 TriggerVectorRasterization();
575 void AnimatedVectorImageVisual::UpdateShader()
579 Shader shader = GenerateShader();
580 mImpl->mRenderer.SetShader(shader);
584 void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
586 // Check if action is valid for this visual type and perform action if possible
589 case DevelAnimatedVectorImageVisual::Action::PLAY:
591 if(IsOnScene() && mVisualSize != Vector2::ZERO)
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;
597 mPlayState = DevelImageVisual::PlayState::PLAYING;
600 case DevelAnimatedVectorImageVisual::Action::PAUSE:
602 if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
604 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
605 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
607 mPlayState = DevelImageVisual::PlayState::PAUSED;
610 case DevelAnimatedVectorImageVisual::Action::STOP:
612 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
614 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
615 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
617 mPlayState = DevelImageVisual::PlayState::STOPPED;
620 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
623 if(attributes.Get(frameNumber))
625 mAnimationData.currentFrame = frameNumber;
626 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
630 case DevelAnimatedVectorImageVisual::Action::FLUSH:
632 if(DALI_LIKELY(!mCoreShutdown))
640 TriggerVectorRasterization();
643 void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
647 case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
649 DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
650 mAnimationData.dynamicProperties.push_back(info);
651 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
656 TriggerVectorRasterization();
659 void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
661 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
663 if(status == VectorAnimationTask::ResourceStatus::LOADED)
665 if(mImpl->mEventObserver)
667 mImpl->mEventObserver->RelayoutRequest(*this);
672 mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
673 if(status == VectorAnimationTask::ResourceStatus::READY)
675 // Texture was ready. Change the shader if we need.
676 bool useNativeImage = false;
679 auto textureSet = mImpl->mRenderer.GetTextures();
680 if(textureSet && textureSet.GetTextureCount() > 0)
682 auto texture = textureSet.GetTexture(0u);
685 useNativeImage = DevelTexture::IsNative(texture);
687 if(mUseNativeImage != useNativeImage)
689 mUseNativeImage = useNativeImage;
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)
703 actor.AddRenderer(mImpl->mRenderer);
704 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
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);
714 mRendererAdded = true;
718 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
721 void AnimatedVectorImageVisual::OnAnimationFinished(uint32_t playStateId)
723 // Only send event when animation is finished by the last Play/Pause/Stop request.
724 if(mLastSentPlayStateId != playStateId)
729 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
731 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
733 if(mPlayState != DevelImageVisual::PlayState::STOPPED)
735 mPlayState = DevelImageVisual::PlayState::STOPPED;
737 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
739 if(mImpl->mEventObserver)
741 mImpl->mEventObserver->NotifyVisualEvent(*this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED);
745 if(!mNotifyAfterRasterization && mImpl->mRenderer)
747 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
751 void AnimatedVectorImageVisual::OnForceRendering(uint32_t playStateId)
753 if(!mCoreShutdown && Dali::Adaptor::IsAvailable())
755 Dali::Adaptor::Get().UpdateOnce();
759 void AnimatedVectorImageVisual::SendAnimationData()
761 if(mAnimationData.resendFlag)
763 if(mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
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;
770 mVectorAnimationTask->SetAnimationData(mAnimationData);
772 if(mImpl->mRenderer &&
773 ((mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE) ||
774 (mAnimationData.resendFlag & VectorAnimationTask::RESEND_NOTIFY_AFTER_RASTERIZATION)))
776 if(!mNotifyAfterRasterization && mPlayState == DevelImageVisual::PlayState::PLAYING)
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);
783 // Otherwise, notify will be sended after rasterization. Make behaviour as required.
784 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
788 mAnimationData.resendFlag = 0;
792 void AnimatedVectorImageVisual::SetVectorImageSize()
794 uint32_t width, height;
795 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
797 width = mDesiredSize.GetWidth();
798 height = mDesiredSize.GetHeight();
802 width = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
803 height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
806 if(mAnimationData.width != width || mAnimationData.height != height)
808 mAnimationData.width = width;
809 mAnimationData.height = height;
810 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
814 void AnimatedVectorImageVisual::StopAnimation()
816 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
818 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
819 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
821 mPlayState = DevelImageVisual::PlayState::STOPPED;
825 void AnimatedVectorImageVisual::TriggerVectorRasterization()
827 if(!mEventCallback && !mCoreShutdown)
829 mEventCallback = MakeCallback(this, &AnimatedVectorImageVisual::OnProcessEvents);
830 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
831 vectorAnimationManager.RegisterEventCallback(mEventCallback);
832 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
836 void AnimatedVectorImageVisual::OnScaleNotification(PropertyNotification& source)
838 Actor actor = mPlacementActor.GetHandle();
841 Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
843 if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
845 mVisualScale.width = scale.width;
846 mVisualScale.height = scale.height;
848 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this);
850 SetVectorImageSize();
853 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
858 void AnimatedVectorImageVisual::OnSizeNotification(PropertyNotification& source)
860 Actor actor = mPlacementActor.GetHandle();
863 Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
865 if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
867 mVisualSize.width = size.width;
868 mVisualSize.height = size.height;
870 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
872 SetVectorImageSize();
875 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
880 void AnimatedVectorImageVisual::OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type)
885 TriggerVectorRasterization();
887 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this);
891 void AnimatedVectorImageVisual::OnWindowVisibilityChanged(Window window, bool visible)
896 TriggerVectorRasterization();
898 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this);
902 void AnimatedVectorImageVisual::OnProcessEvents()
906 mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
909 Shader AnimatedVectorImageVisual::GenerateShader() const
912 if(mImpl->mCustomShader)
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);
918 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
922 shader = mImageVisualShaderFactory.GetShader(
924 ImageVisualShaderFeatureBuilder()
925 .EnableRoundedCorner(IsRoundedCornerRequired())
926 .EnableBorderline(IsBorderlineRequired())
927 .SetTextureForFragmentShaderCheck(mUseNativeImage ? mImpl->mRenderer.GetTextures().GetTexture(0) : Dali::Texture()));
932 } // namespace Internal
934 } // namespace Toolkit