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/debug.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/public-api/rendering/decorated-visual-renderer.h>
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>
50 const int CUSTOM_PROPERTY_COUNT(1); // pixel area,
52 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
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)
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)
67 #if defined(DEBUG_ENABLED)
68 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VECTOR_ANIMATION");
71 } // unnamed namespace
73 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
75 AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, ImageDimensions{}));
76 visual->SetProperties(properties);
81 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
83 AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, size));
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)),
92 mVectorAnimationTask(new VectorAnimationTask(factoryCache)),
93 mImageVisualShaderFactory(shaderFactory),
95 mVisualScale(Vector2::ONE),
98 mPlayState(DevelImageVisual::PlayState::STOPPED),
99 mEventCallback(nullptr),
100 mLastSentPlayStateId(0u),
102 mRendererAdded(false),
103 mCoreShutdown(false),
104 mRedrawInScalingDown(true),
105 mEnableFrameCache(false),
106 mUseNativeImage(false)
108 // the rasterized image is with pre-multiplied alpha format
109 mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
111 // By default, load a file synchronously
112 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
115 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
119 if(mImageUrl.IsBufferResource())
121 TextureManager& textureManager = mFactoryCache.GetTextureManager();
122 textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
125 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
126 vectorAnimationManager.RemoveObserver(*this);
130 mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback(mEventCallback);
131 mEventCallback = nullptr;
134 // Finalize animation task and disconnect the signal in the main thread
135 mVectorAnimationTask->ResourceReadySignal().Disconnect(this, &AnimatedVectorImageVisual::OnResourceReady);
136 mVectorAnimationTask->Finalize();
140 void AnimatedVectorImageVisual::VectorAnimationManagerDestroyed()
142 // Core is shutting down. Don't talk to the plugin any more.
143 mCoreShutdown = true;
146 void AnimatedVectorImageVisual::GetNaturalSize(Vector2& naturalSize)
148 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
150 naturalSize.x = mDesiredSize.GetWidth();
151 naturalSize.y = mDesiredSize.GetHeight();
153 else if(mVisualSize != Vector2::ZERO)
155 naturalSize = mVisualSize;
159 if(mLoadFailed && mImpl->mRenderer)
161 // Load failed, use broken image size
162 auto textureSet = mImpl->mRenderer.GetTextures();
163 if(textureSet && textureSet.GetTextureCount())
165 auto texture = textureSet.GetTexture(0);
168 naturalSize.x = texture.GetWidth();
169 naturalSize.y = texture.GetHeight();
176 uint32_t width, height;
177 mVectorAnimationTask->GetDefaultSize(width, height);
178 naturalSize.x = width;
179 naturalSize.y = height;
183 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this);
186 void AnimatedVectorImageVisual::DoCreatePropertyMap(Property::Map& map) const
189 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE);
190 if(mImageUrl.IsValid())
192 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
194 map.Insert(Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount);
196 uint32_t startFrame, endFrame;
197 mVectorAnimationTask->GetPlayRange(startFrame, endFrame);
199 Property::Array playRange;
200 playRange.PushBack(static_cast<int32_t>(startFrame));
201 playRange.PushBack(static_cast<int32_t>(endFrame));
202 map.Insert(Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange);
204 map.Insert(Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast<int32_t>(mPlayState));
205 map.Insert(Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetCurrentFrameNumber()));
206 map.Insert(Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast<int32_t>(mVectorAnimationTask->GetTotalFrameNumber()));
208 map.Insert(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior);
209 map.Insert(Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode);
210 map.Insert(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, mRedrawInScalingDown);
212 Property::Map layerInfo;
213 mVectorAnimationTask->GetLayerInfo(layerInfo);
214 map.Insert(Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo);
216 Property::Map markerInfo;
217 mVectorAnimationTask->GetMarkerInfo(markerInfo);
218 map.Insert(Toolkit::DevelImageVisual::Property::MARKER_INFO, markerInfo);
220 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
221 map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
222 map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
223 map.Insert(Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE, mEnableFrameCache);
226 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
231 void AnimatedVectorImageVisual::EnablePreMultipliedAlpha(bool preMultiplied)
233 // Make always enable pre multiplied alpha whether preMultiplied value is false.
236 DALI_LOG_WARNING("Note : AnimatedVectorVisual cannot disable PreMultipliedAlpha\n");
240 void AnimatedVectorImageVisual::DoSetProperties(const Property::Map& propertyMap)
242 // url already passed in from constructor
243 for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
245 KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
246 if(keyValue.first.type == Property::Key::INDEX)
248 DoSetProperty(keyValue.first.indexKey, keyValue.second);
252 if(keyValue.first == LOOP_COUNT_NAME)
254 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second);
256 else if(keyValue.first == PLAY_RANGE_NAME)
258 DoSetProperty(Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second);
260 else if(keyValue.first == STOP_BEHAVIOR_NAME)
262 DoSetProperty(Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second);
264 else if(keyValue.first == LOOPING_MODE_NAME)
266 DoSetProperty(Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second);
268 else if(keyValue.first == REDRAW_IN_SCALING_DOWN_NAME)
270 DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
272 else if(keyValue.first == SYNCHRONOUS_LOADING)
274 DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
276 else if(keyValue.first == IMAGE_DESIRED_WIDTH)
278 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
280 else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
282 DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
284 else if(keyValue.first == ENABLE_FRAME_CACHE)
286 DoSetProperty(Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE, keyValue.second);
291 TriggerVectorRasterization();
294 void AnimatedVectorImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
298 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
301 if(value.Get(loopCount))
303 mAnimationData.loopCount = loopCount;
304 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
308 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
310 const Property::Array* array = value.GetArray();
313 mAnimationData.playRange = *array;
314 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
316 else if(value.GetType() == Property::STRING)
318 std::string markerName;
319 if(value.Get(markerName))
321 Property::Array array;
322 array.Add(markerName);
323 mAnimationData.playRange = std::move(array);
324 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
329 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
331 int32_t stopBehavior = mAnimationData.stopBehavior;
332 if(Scripting::GetEnumerationProperty(value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior))
334 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type(stopBehavior);
335 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
339 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
341 int32_t loopingMode = mAnimationData.loopingMode;
342 if(Scripting::GetEnumerationProperty(value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode))
344 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type(loopingMode);
345 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
349 case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
352 if(value.Get(redraw))
354 mRedrawInScalingDown = redraw;
358 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
365 mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
369 mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
374 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
376 int32_t desiredWidth = 0;
377 if(value.Get(desiredWidth))
379 mDesiredSize.SetWidth(desiredWidth);
384 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
386 int32_t desiredHeight = 0;
387 if(value.Get(desiredHeight))
389 mDesiredSize.SetHeight(desiredHeight);
394 case Toolkit::DevelImageVisual::Property::ENABLE_FRAME_CACHE:
396 bool enableFrameCache = false;
397 if(value.Get(enableFrameCache))
399 mEnableFrameCache = enableFrameCache;
400 if(mVectorAnimationTask)
402 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
410 void AnimatedVectorImageVisual::OnInitialize(void)
412 mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
413 mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
415 EncodedImageBuffer encodedImageBuffer;
417 if(mImageUrl.IsBufferResource())
419 // Increase reference count of External Resources :
420 // EncodedImageBuffer.
421 // Reference count will be decreased at destructor of the visual.
422 TextureManager& textureManager = mFactoryCache.GetTextureManager();
423 textureManager.UseExternalResource(mImageUrl.GetUrl());
425 encodedImageBuffer = textureManager.GetEncodedImageBuffer(mImageUrl.GetUrl());
428 mVectorAnimationTask->KeepRasterizedBuffer(mEnableFrameCache);
429 mVectorAnimationTask->RequestLoad(mImageUrl, encodedImageBuffer, IsSynchronousLoadingRequired());
431 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
432 vectorAnimationManager.AddObserver(*this);
434 Shader shader = GenerateShader();
436 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
438 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
439 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
441 TextureSet textureSet = TextureSet::New();
442 mImpl->mRenderer.SetTextures(textureSet);
444 // Register transform properties
445 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
447 mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
450 void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
452 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
454 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
455 mPlacementActor = actor;
459 Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
460 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
461 actor.AddRenderer(mImpl->mRenderer);
462 mRendererAdded = true;
463 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
467 // Add property notification for scaling & size
468 mScaleNotification = actor.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
469 mScaleNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnScaleNotification);
471 mSizeNotification = actor.AddPropertyNotification(Actor::Property::SIZE, StepCondition(3.0f));
472 mSizeNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnSizeNotification);
474 DevelActor::VisibilityChangedSignal(actor).Connect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
476 Window window = DevelWindow::Get(actor);
479 DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
482 if(mImpl->mEventObserver)
484 // The visual needs it's size set before it can be rasterized hence request relayout once on stage
485 mImpl->mEventObserver->RelayoutRequest(*this);
488 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
489 TriggerVectorRasterization();
492 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
495 void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
498 TriggerVectorRasterization();
502 actor.RemoveRenderer(mImpl->mRenderer);
503 mRendererAdded = false;
506 // Remove property notification
507 actor.RemovePropertyNotification(mScaleNotification);
508 actor.RemovePropertyNotification(mSizeNotification);
510 DevelActor::VisibilityChangedSignal(actor).Disconnect(this, &AnimatedVectorImageVisual::OnControlVisibilityChanged);
512 Window window = DevelWindow::Get(actor);
515 DevelWindow::VisibilityChangedSignal(window).Disconnect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
518 mPlacementActor.Reset();
520 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
521 mVisualSize = Vector2::ZERO;
522 mVisualScale = Vector2::ONE;
523 mAnimationData.width = 0;
524 mAnimationData.height = 0;
526 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
529 void AnimatedVectorImageVisual::OnSetTransform()
531 Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
533 if(IsOnScene() && visualSize != mVisualSize)
535 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this);
537 mVisualSize = visualSize;
539 SetVectorImageSize();
541 if(mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
543 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
544 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
547 TriggerVectorRasterization();
551 void AnimatedVectorImageVisual::UpdateShader()
555 Shader shader = GenerateShader();
556 mImpl->mRenderer.SetShader(shader);
560 void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
562 // Check if action is valid for this visual type and perform action if possible
565 case DevelAnimatedVectorImageVisual::Action::PLAY:
567 if(IsOnScene() && mVisualSize != Vector2::ZERO)
569 // Always resend Playing state. If task is already playing, it will be ignored at Rasterize time.
570 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
571 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
573 mPlayState = DevelImageVisual::PlayState::PLAYING;
576 case DevelAnimatedVectorImageVisual::Action::PAUSE:
578 if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
580 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
581 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
583 mPlayState = DevelImageVisual::PlayState::PAUSED;
586 case DevelAnimatedVectorImageVisual::Action::STOP:
588 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
590 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
591 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
593 mPlayState = DevelImageVisual::PlayState::STOPPED;
596 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
599 if(attributes.Get(frameNumber))
601 mAnimationData.currentFrame = frameNumber;
602 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
606 case DevelAnimatedVectorImageVisual::Action::FLUSH:
608 if(DALI_LIKELY(!mCoreShutdown))
616 TriggerVectorRasterization();
619 void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
623 case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
625 DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
626 mAnimationData.dynamicProperties.push_back(info);
627 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
632 TriggerVectorRasterization();
635 void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
637 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
639 if(status == VectorAnimationTask::ResourceStatus::LOADED)
641 if(mImpl->mEventObserver)
643 mImpl->mEventObserver->RelayoutRequest(*this);
648 mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
649 if(status == VectorAnimationTask::ResourceStatus::READY)
651 // Texture was ready. Change the shader if we need.
652 bool useNativeImage = false;
655 auto textureSet = mImpl->mRenderer.GetTextures();
656 if(textureSet && textureSet.GetTextureCount() > 0)
658 auto texture = textureSet.GetTexture(0u);
661 useNativeImage = DevelTexture::IsNative(texture);
663 if(mUseNativeImage != useNativeImage)
665 mUseNativeImage = useNativeImage;
673 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
674 Actor actor = mPlacementActor.GetHandle();
675 if(actor && !mRendererAdded)
679 actor.AddRenderer(mImpl->mRenderer);
680 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
684 Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
685 mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
686 actor.AddRenderer(mImpl->mRenderer);
687 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
690 mRendererAdded = true;
694 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
697 void AnimatedVectorImageVisual::OnAnimationFinished(uint32_t playStateId)
699 // Only send event when animation is finished by the last Play/Pause/Stop request.
700 if(mLastSentPlayStateId != playStateId)
705 AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
707 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
709 if(mPlayState != DevelImageVisual::PlayState::STOPPED)
711 mPlayState = DevelImageVisual::PlayState::STOPPED;
713 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
715 if(mImpl->mEventObserver)
717 mImpl->mEventObserver->NotifyVisualEvent(*this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED);
723 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
727 void AnimatedVectorImageVisual::SendAnimationData()
729 if(mAnimationData.resendFlag)
731 if(mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
733 // Keep last sent playId. It will be used when we try to emit AnimationFinished signal.
734 // The OnAnimationFinished signal what before Play/Pause/Stop action send could be come after action sent.
735 // To ensure the OnAnimationFinished signal comes belong to what we sent, we need to keep last sent playId.
736 mAnimationData.playStateId = ++mLastSentPlayStateId;
738 mVectorAnimationTask->SetAnimationData(mAnimationData);
742 if(mAnimationData.playState == DevelImageVisual::PlayState::PLAYING)
744 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY);
748 mImpl->mRenderer.SetProperty(DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED);
752 mAnimationData.resendFlag = 0;
756 void AnimatedVectorImageVisual::SetVectorImageSize()
758 uint32_t width, height;
759 if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
761 width = mDesiredSize.GetWidth();
762 height = mDesiredSize.GetHeight();
766 width = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
767 height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
770 if(mAnimationData.width != width || mAnimationData.height != height)
772 mAnimationData.width = width;
773 mAnimationData.height = height;
774 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
778 void AnimatedVectorImageVisual::StopAnimation()
780 if(mAnimationData.playState != DevelImageVisual::PlayState::STOPPED)
782 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
783 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
785 mPlayState = DevelImageVisual::PlayState::STOPPED;
789 void AnimatedVectorImageVisual::TriggerVectorRasterization()
791 if(!mEventCallback && !mCoreShutdown)
793 mEventCallback = MakeCallback(this, &AnimatedVectorImageVisual::OnProcessEvents);
794 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
795 vectorAnimationManager.RegisterEventCallback(mEventCallback);
796 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
800 void AnimatedVectorImageVisual::OnScaleNotification(PropertyNotification& source)
802 Actor actor = mPlacementActor.GetHandle();
805 Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
807 if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
809 mVisualScale.width = scale.width;
810 mVisualScale.height = scale.height;
812 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this);
814 SetVectorImageSize();
817 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
822 void AnimatedVectorImageVisual::OnSizeNotification(PropertyNotification& source)
824 Actor actor = mPlacementActor.GetHandle();
827 Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
829 if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
831 mVisualSize.width = size.width;
832 mVisualSize.height = size.height;
834 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
836 SetVectorImageSize();
839 Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
844 void AnimatedVectorImageVisual::OnControlVisibilityChanged(Actor actor, bool visible, DevelActor::VisibilityChange::Type type)
849 TriggerVectorRasterization();
851 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this);
855 void AnimatedVectorImageVisual::OnWindowVisibilityChanged(Window window, bool visible)
860 TriggerVectorRasterization();
862 DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this);
866 void AnimatedVectorImageVisual::OnProcessEvents()
870 mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
873 Shader AnimatedVectorImageVisual::GenerateShader() const
876 if(mImpl->mCustomShader)
878 shader = Shader::New(mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
879 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
880 mImpl->mCustomShader->mHints);
882 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
886 shader = mImageVisualShaderFactory.GetShader(
888 ImageVisualShaderFeatureBuilder()
889 .EnableRoundedCorner(IsRoundedCornerRequired())
890 .EnableBorderline(IsBorderlineRequired())
891 .SetTextureForFragmentShaderCheck(mUseNativeImage ? mImpl->mRenderer.GetTextures().GetTexture(0) : Dali::Texture()));
896 } // namespace Internal
898 } // namespace Toolkit