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 mRedrawInScalingDown(true),
105 mEnableFrameCache(false),
106 mUseNativeImage(false),
107 mNotifyAfterRasterization(false)
109 // the rasterized image is with pre-multiplied alpha format
110 mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
112 // By default, load a file synchronously
113 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
116 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
118 if(Dali::Adaptor::IsAvailable())
120 if(mImageUrl.IsBufferResource())
122 TextureManager& textureManager = mFactoryCache.GetTextureManager();
123 textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
128 mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback(mEventCallback);
129 mEventCallback = nullptr;
132 // Finalize animation task and disconnect the signal in the main thread
133 mVectorAnimationTask->ResourceReadySignal().Disconnect(this, &AnimatedVectorImageVisual::OnResourceReady);
134 mVectorAnimationTask->Finalize();
138 void AnimatedVectorImageVisual::GetNaturalSize(Vector2& naturalSize)
140 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
142 naturalSize.x = mDesiredSize.GetWidth();
143 naturalSize.y = mDesiredSize.GetHeight();
145 else if(mVisualSize != Vector2::ZERO)
147 naturalSize = mVisualSize;
151 if(mLoadFailed && mImpl->mRenderer)
153 // Load failed, use broken image size
154 auto textureSet = mImpl->mRenderer.GetTextures();
155 if(textureSet && textureSet.GetTextureCount())
157 auto texture = textureSet.GetTexture(0);
160 naturalSize.x = texture.GetWidth();
161 naturalSize.y = texture.GetHeight();
168 uint32_t width, height;
169 mVectorAnimationTask->GetDefaultSize(width, height);
170 naturalSize.x = width;
171 naturalSize.y = height;
175 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this);
178 void AnimatedVectorImageVisual::DoCreatePropertyMap(Property::Map& map) const
181 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE);
182 if(mImageUrl.IsValid())
184 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
186 map.Insert(Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount);
188 uint32_t startFrame, endFrame;
189 mVectorAnimationTask->GetPlayRange(startFrame, endFrame);
191 Property::Array playRange;
192 playRange.PushBack(static_cast<int32_t>(startFrame));
193 playRange.PushBack(static_cast<int32_t>(endFrame));
194 map.Insert(Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange);
196 map.Insert(Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast<int32_t>(mPlayState));
197 map.Insert(Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetCurrentFrameNumber()));
198 map.Insert(Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetTotalFrameNumber()));
200 map.Insert(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior);
201 map.Insert(Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode);
202 map.Insert(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, mRedrawInScalingDown);
204 Property::Map layerInfo;
205 mVectorAnimationTask->GetLayerInfo(layerInfo);
206 map.Insert(Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo);
208 Property::Map markerInfo;
209 mVectorAnimationTask->GetMarkerInfo(markerInfo);
210 map.Insert(Toolkit::DevelImageVisual::Property::MARKER_INFO, markerInfo);
212 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
213 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
214 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
215 map.Insert(Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE, mEnableFrameCache);
216 map.Insert(Toolkit::DevelImageVisual::Property::NOTIFY_AFTER_RASTERIZATION, mNotifyAfterRasterization);
219 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
224 void AnimatedVectorImageVisual::EnablePreMultipliedAlpha(bool preMultiplied)
226 // Make always enable pre multiplied alpha whether preMultiplied value is false.
229 DALI_LOG_WARNING("Note : AnimatedVectorVisual cannot disable PreMultipliedAlpha\n");
233 void AnimatedVectorImageVisual::DoSetProperties(const Property::Map& propertyMap)
235 // url already passed in from constructor
236 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
238 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
239 if(keyValue.first.type == Property::Key::INDEX)
241 DoSetProperty(keyValue.first.indexKey, keyValue.second);
245 if(keyValue.first == LOOP_COUNT_NAME)
247 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second);
249 else if(keyValue.first == PLAY_RANGE_NAME)
251 DoSetProperty(Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second);
253 else if(keyValue.first == STOP_BEHAVIOR_NAME)
255 DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second);
257 else if(keyValue.first == LOOPING_MODE_NAME)
259 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second);
261 else if(keyValue.first == REDRAW_IN_SCALING_DOWN_NAME)
263 DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
265 else if(keyValue.first == SYNCHRONOUS_LOADING)
267 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
269 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
271 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
273 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
275 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
277 else if(keyValue.first == ENABLE_FRAME_CACHE)
279 DoSetProperty(Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE, keyValue.second);
281 else if(keyValue.first == NOTIFY_AFTER_RASTERIZATION)
283 DoSetProperty(Toolkit::DevelImageVisual::Property::NOTIFY_AFTER_RASTERIZATION, keyValue.second);
288 TriggerVectorRasterization();
291 void AnimatedVectorImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
295 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
298 if(value.Get(loopCount))
300 mAnimationData.loopCount = loopCount;
301 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
305 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
307 const Property::Array* array = value.GetArray();
310 mAnimationData.playRange = *array;
311 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
313 else if(value.GetType() == Property::STRING)
315 std::string markerName;
316 if(value.Get(markerName))
318 Property::Array array;
319 array.Add(markerName);
320 mAnimationData.playRange = std::move(array);
321 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
326 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
328 int32_t stopBehavior = mAnimationData.stopBehavior;
329 if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior))
331 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior);
332 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
336 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
338 int32_t loopingMode = mAnimationData.loopingMode;
339 if(Scripting::GetEnumerationProperty(value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode))
341 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type(loopingMode);
342 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
346 case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
349 if(value.Get(redraw))
351 mRedrawInScalingDown = redraw;
355 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
362 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
366 mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
371 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
373 int32_t desiredWidth = 0;
374 if(value.Get(desiredWidth))
376 mDesiredSize.SetWidth(desiredWidth);
381 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
383 int32_t desiredHeight = 0;
384 if(value.Get(desiredHeight))
386 mDesiredSize.SetHeight(desiredHeight);
391 case Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE:
393 bool enableFrameCache = false;
394 if(value.Get(enableFrameCache))
396 mEnableFrameCache = enableFrameCache;
397 if(mVectorAnimationTask)
399 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
405 case Toolkit::DevelImageVisual::Property::NOTIFY_AFTER_RASTERIZATION:
407 bool notifyAfterRasterization = false;
408 if(value.Get(notifyAfterRasterization))
410 if(mNotifyAfterRasterization != notifyAfterRasterization)
412 mNotifyAfterRasterization = notifyAfterRasterization;
414 mAnimationData.notifyAfterRasterization = mNotifyAfterRasterization;
415 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NOTIFY_AFTER_RASTERIZATION;
423 void AnimatedVectorImageVisual::OnInitialize(void)
425 mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
426 mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
428 EncodedImageBuffer encodedImageBuffer;
430 if(mImageUrl.IsBufferResource())
432 // Increase reference count of External Resources :
433 // EncodedImageBuffer.
434 // Reference count will be decreased at destructor of the visual.
435 TextureManager& textureManager = mFactoryCache.GetTextureManager();
436 textureManager.UseExternalResource(mImageUrl.GetUrl());
438 encodedImageBuffer = textureManager.GetEncodedImageBuffer(mImageUrl.GetUrl());
441 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
442 mVectorAnimationTask->RequestLoad(mImageUrl, encodedImageBuffer, IsSynchronousLoadingRequired());
444 Shader shader = GenerateShader();
446 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
448 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
449 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
451 TextureSet textureSet = TextureSet::New();
452 mImpl->mRenderer.SetTextures(textureSet);
454 // Register transform properties
455 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
457 mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
460 void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
462 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
464 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
465 mPlacementActor = actor;
469 Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
470 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
471 actor.AddRenderer(mImpl->mRenderer);
472 mRendererAdded = true;
473 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
477 // Add property notification for scaling & size
478 mScaleNotification = actor.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
479 mScaleNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnScaleNotification);
481 mSizeNotification = actor.AddPropertyNotification(Actor::Property::SIZE, StepCondition(3.0f));
482 mSizeNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnSizeNotification);
484 DevelActor::VisibilityChangedSignal(actor).Connect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
486 Window window = DevelWindow::Get(actor);
489 mPlacementWindow = window;
490 DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
493 if(mImpl->mEventObserver)
495 // The visual needs it's size set before it can be rasterized hence request relayout once on stage
496 mImpl->mEventObserver->RelayoutRequest(*this);
499 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
500 TriggerVectorRasterization();
503 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
506 void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
509 TriggerVectorRasterization();
513 actor.RemoveRenderer(mImpl->mRenderer);
514 mRendererAdded = false;
517 // Remove property notification
518 actor.RemovePropertyNotification(mScaleNotification);
519 actor.RemovePropertyNotification(mSizeNotification);
521 DevelActor::VisibilityChangedSignal(actor).Disconnect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
523 Window window = mPlacementWindow.GetHandle();
526 DevelWindow::VisibilityChangedSignal(window).Disconnect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
527 mPlacementWindow.Reset();
530 mPlacementActor.Reset();
532 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
533 mVisualSize = Vector2::ZERO;
534 mVisualScale = Vector2::ONE;
535 mAnimationData.width = 0;
536 mAnimationData.height = 0;
538 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
541 void AnimatedVectorImageVisual::OnSetTransform()
543 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
545 if(IsOnScene() && visualSize != mVisualSize)
547 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this);
549 mVisualSize = visualSize;
551 SetVectorImageSize();
553 if(mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
555 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
556 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
559 TriggerVectorRasterization();
563 void AnimatedVectorImageVisual::UpdateShader()
567 Shader shader = GenerateShader();
568 mImpl->mRenderer.SetShader(shader);
572 void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
574 // Check if action is valid for this visual type and perform action if possible
577 case DevelAnimatedVectorImageVisual::Action::PLAY:
579 if(IsOnScene() && mVisualSize != Vector2::ZERO)
581 // Always resend Playing state. If task is already playing, it will be ignored at Rasterize time.
582 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
583 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
585 mPlayState = DevelImageVisual::PlayState::PLAYING;
588 case DevelAnimatedVectorImageVisual::Action::PAUSE:
590 if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
592 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
593 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
595 mPlayState = DevelImageVisual::PlayState::PAUSED;
598 case DevelAnimatedVectorImageVisual::Action::STOP:
600 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
602 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
603 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
605 mPlayState = DevelImageVisual::PlayState::STOPPED;
608 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
611 if(attributes.Get(frameNumber))
613 mAnimationData.currentFrame = frameNumber;
614 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
618 case DevelAnimatedVectorImageVisual::Action::FLUSH:
620 if(DALI_LIKELY(Dali::Adaptor::IsAvailable()))
628 TriggerVectorRasterization();
631 void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
635 case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
637 DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
638 mAnimationData.dynamicProperties.push_back(info);
639 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
644 TriggerVectorRasterization();
647 void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
649 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
651 if(status == VectorAnimationTask::ResourceStatus::LOADED)
653 if(mImpl->mEventObserver)
655 mImpl->mEventObserver->RelayoutRequest(*this);
660 mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
661 if(status == VectorAnimationTask::ResourceStatus::READY)
663 // Texture was ready. Change the shader if we need.
664 bool useNativeImage = false;
667 auto textureSet = mImpl->mRenderer.GetTextures();
668 if(textureSet && textureSet.GetTextureCount() > 0)
670 auto texture = textureSet.GetTexture(0u);
673 useNativeImage = DevelTexture::IsNative(texture);
675 if(mUseNativeImage != useNativeImage)
677 mUseNativeImage = useNativeImage;
685 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
686 Actor actor = mPlacementActor.GetHandle();
687 if(actor && !mRendererAdded)
691 actor.AddRenderer(mImpl->mRenderer);
692 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
696 Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
697 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
698 actor.AddRenderer(mImpl->mRenderer);
699 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
702 mRendererAdded = true;
706 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
709 void AnimatedVectorImageVisual::OnAnimationFinished(uint32_t playStateId)
711 // Only send event when animation is finished by the last Play/Pause/Stop request.
712 if(mLastSentPlayStateId != playStateId)
717 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
719 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
721 if(mPlayState != DevelImageVisual::PlayState::STOPPED)
723 mPlayState = DevelImageVisual::PlayState::STOPPED;
725 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
727 if(mImpl->mEventObserver)
729 mImpl->mEventObserver->NotifyVisualEvent(*this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED);
733 if(!mNotifyAfterRasterization && mImpl->mRenderer)
735 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
739 void AnimatedVectorImageVisual::SendAnimationData()
741 if(mAnimationData.resendFlag)
743 if(mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
745 // Keep last sent playId. It will be used when we try to emit AnimationFinished signal.
746 // The OnAnimationFinished signal what before Play/Pause/Stop action send could be come after action sent.
747 // To ensure the OnAnimationFinished signal comes belong to what we sent, we need to keep last sent playId.
748 mAnimationData.playStateId = ++mLastSentPlayStateId;
750 mVectorAnimationTask->SetAnimationData(mAnimationData);
752 if(mImpl->mRenderer &&
753 ((mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE) ||
754 (mAnimationData.resendFlag & VectorAnimationTask::RESEND_NOTIFY_AFTER_RASTERIZATION)))
756 if(!mNotifyAfterRasterization && mPlayState == DevelImageVisual::PlayState::PLAYING)
758 // Make rendering behaviour if we don't notify after rasterization, but animation playing.
759 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY);
763 // Otherwise, notify will be sended after rasterization. Make behaviour as required.
764 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
768 mAnimationData.resendFlag = 0;
772 void AnimatedVectorImageVisual::SetVectorImageSize()
774 uint32_t width, height;
775 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
777 width = mDesiredSize.GetWidth();
778 height = mDesiredSize.GetHeight();
782 width = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
783 height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
786 if(mAnimationData.width != width || mAnimationData.height != height)
788 mAnimationData.width = width;
789 mAnimationData.height = height;
790 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
794 void AnimatedVectorImageVisual::StopAnimation()
796 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
798 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
799 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
801 mPlayState = DevelImageVisual::PlayState::STOPPED;
805 void AnimatedVectorImageVisual::TriggerVectorRasterization()
807 if(!mEventCallback && Dali::Adaptor::IsAvailable())
809 mEventCallback = MakeCallback(this, &AnimatedVectorImageVisual::OnProcessEvents);
810 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
811 vectorAnimationManager.RegisterEventCallback(mEventCallback);
812 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
816 void AnimatedVectorImageVisual::OnScaleNotification(PropertyNotification& source)
818 Actor actor = mPlacementActor.GetHandle();
821 Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
823 if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
825 mVisualScale.width = scale.width;
826 mVisualScale.height = scale.height;
828 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this);
830 SetVectorImageSize();
833 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
838 void AnimatedVectorImageVisual::OnSizeNotification(PropertyNotification& source)
840 Actor actor = mPlacementActor.GetHandle();
843 Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
845 if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
847 mVisualSize.width = size.width;
848 mVisualSize.height = size.height;
850 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
852 SetVectorImageSize();
855 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
860 void AnimatedVectorImageVisual::OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type)
865 TriggerVectorRasterization();
867 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this);
871 void AnimatedVectorImageVisual::OnWindowVisibilityChanged(Window window, bool visible)
876 TriggerVectorRasterization();
878 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this);
882 void AnimatedVectorImageVisual::OnProcessEvents()
886 mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
889 Shader AnimatedVectorImageVisual::GenerateShader() const
892 if(mImpl->mCustomShader)
894 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
895 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
896 mImpl->mCustomShader->mHints);
898 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
902 shader = mImageVisualShaderFactory.GetShader(
904 ImageVisualShaderFeatureBuilder()
905 .EnableRoundedCorner(IsRoundedCornerRequired())
906 .EnableBorderline(IsBorderlineRequired())
907 .SetTextureForFragmentShaderCheck(mUseNativeImage ? mImpl->mRenderer.GetTextures().GetTexture(0) : Dali::Texture()));
912 } // namespace Internal
914 } // namespace Toolkit