/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <dali/devel-api/common/stage.h>
#include <dali/devel-api/rendering/renderer-devel.h>
#include <dali/integration-api/debug.h>
+#include <dali/public-api/math/math-utils.h>
+#include <dali/public-api/rendering/decorated-visual-renderer.h>
// INTERNAL INCLUDES
#include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
{
namespace
{
-const int CUSTOM_PROPERTY_COUNT(6); // 5 transform properties + pixel area,
+const int CUSTOM_PROPERTY_COUNT(1); // pixel area,
const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
{
- AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl));
+ AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, ImageDimensions{}));
visual->SetProperties(properties);
visual->Initialize();
return visual;
}
-AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
+AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
{
- AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl));
+ AnimatedVectorImageVisualPtr visual(new AnimatedVectorImageVisual(factoryCache, shaderFactory, imageUrl, size));
visual->Initialize();
return visual;
}
-AnimatedVectorImageVisual::AnimatedVectorImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
+AnimatedVectorImageVisual::AnimatedVectorImageVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, ImageDimensions size)
: Visual::Base(factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>(Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE)),
mUrl(imageUrl),
mAnimationData(),
mImageVisualShaderFactory(shaderFactory),
mVisualSize(),
mVisualScale(Vector2::ONE),
+ mDesiredSize(size),
mPlacementActor(),
mPlayState(DevelImageVisual::PlayState::STOPPED),
mEventCallback(nullptr),
mRedrawInScalingDown(true)
{
// the rasterized image is with pre-multiplied alpha format
- mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
+ mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
- if(!mVectorAnimationTask->Load(mUrl.GetUrl()))
- {
- mLoadFailed = true;
- }
-
- mVectorAnimationTask->UploadCompletedSignal().Connect(this, &AnimatedVectorImageVisual::OnUploadCompleted);
- mVectorAnimationTask->SetAnimationFinishedCallback(new EventThreadCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished)));
-
- auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
- vectorAnimationManager.AddObserver(*this);
+ // By default, load a file synchronously
+ mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
}
AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
if(mEventCallback)
{
mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback(mEventCallback);
+ mEventCallback = nullptr;
}
// Finalize animation task and disconnect the signal in the main thread
- mVectorAnimationTask->UploadCompletedSignal().Disconnect(this, &AnimatedVectorImageVisual::OnUploadCompleted);
+ mVectorAnimationTask->ResourceReadySignal().Disconnect(this, &AnimatedVectorImageVisual::OnResourceReady);
mVectorAnimationTask->Finalize();
}
}
void AnimatedVectorImageVisual::GetNaturalSize(Vector2& naturalSize)
{
- if(mVisualSize != Vector2::ZERO)
+ if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
+ {
+ naturalSize.x = mDesiredSize.GetWidth();
+ naturalSize.y = mDesiredSize.GetHeight();
+ }
+ else if(mVisualSize != Vector2::ZERO)
{
naturalSize = mVisualSize;
}
else
{
- uint32_t width, height;
- mVectorAnimationTask->GetDefaultSize(width, height);
- naturalSize.x = width;
- naturalSize.y = height;
+ if(mLoadFailed && mImpl->mRenderer)
+ {
+ // Load failed, use broken image size
+ auto textureSet = mImpl->mRenderer.GetTextures();
+ if(textureSet && textureSet.GetTextureCount())
+ {
+ auto texture = textureSet.GetTexture(0);
+ if(texture)
+ {
+ naturalSize.x = texture.GetWidth();
+ naturalSize.y = texture.GetHeight();
+ return;
+ }
+ }
+ }
+ else
+ {
+ uint32_t width, height;
+ mVectorAnimationTask->GetDefaultSize(width, height);
+ naturalSize.x = width;
+ naturalSize.y = height;
+ }
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this);
Property::Map layerInfo;
mVectorAnimationTask->GetLayerInfo(layerInfo);
map.Insert(Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo);
+
+ map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired());
+ map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
+ map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
}
void AnimatedVectorImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
{
DoSetProperty(Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second);
}
+ else if(keyValue.first == SYNCHRONOUS_LOADING)
+ {
+ DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
+ }
+ else if(keyValue.first == IMAGE_DESIRED_WIDTH)
+ {
+ DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
+ }
+ else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
+ {
+ DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
+ }
}
}
mAnimationData.playRange = *array;
mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
}
+ else if(value.GetType() == Property::STRING)
+ {
+ std::string markerName;
+ if(value.Get(markerName))
+ {
+ Property::Array array;
+ array.Add(markerName);
+ mAnimationData.playRange = std::move(array);
+ mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
+ }
+ }
break;
}
case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
}
break;
}
+ case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
+ {
+ bool sync = false;
+ if(value.Get(sync))
+ {
+ if(sync)
+ {
+ mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ }
+ else
+ {
+ mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
+ }
+ }
+ break;
+ }
+ case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
+ {
+ int32_t desiredWidth = 0;
+ if(value.Get(desiredWidth))
+ {
+ mDesiredSize.SetWidth(desiredWidth);
+ }
+ break;
+ }
+
+ case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
+ {
+ int32_t desiredHeight = 0;
+ if(value.Get(desiredHeight))
+ {
+ mDesiredSize.SetHeight(desiredHeight);
+ }
+ break;
+ }
}
}
void AnimatedVectorImageVisual::OnInitialize(void)
{
+ mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
+ mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
+
+ mVectorAnimationTask->RequestLoad(mUrl.GetUrl(), IsSynchronousLoadingRequired());
+
+ auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
+ vectorAnimationManager.AddObserver(*this);
+
Shader shader = GenerateShader();
Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
- mImpl->mRenderer = VisualRenderer::New(geometry, shader);
+ mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
TextureSet textureSet = TextureSet::New();
// Register transform properties
mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
+
+ mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
}
void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
if(mLoadFailed)
{
- Vector2 imageSize = Vector2::ZERO;
- imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
- mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
+ Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
actor.AddRenderer(mImpl->mRenderer);
ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
}
else
{
- mVectorAnimationTask->SetRenderer(mImpl->mRenderer);
-
// Add property notification for scaling & size
mScaleNotification = actor.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
mScaleNotification.NotifySignal().Connect(this, &AnimatedVectorImageVisual::OnScaleNotification);
{
DevelWindow::VisibilityChangedSignal(window).Connect(this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged);
}
+
+ if(mImpl->mEventObserver)
+ {
+ // The visual needs it's size set before it can be rasterized hence request relayout once on stage
+ mImpl->mEventObserver->RelayoutRequest(*this);
+ }
+
+ mAnimationData.resendFlag |= VectorAnimationTask::RESEND_NEED_RESOURCE_READY;
+ TriggerVectorRasterization();
}
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this);
void AnimatedVectorImageVisual::DoSetOffScene(Actor& actor)
{
StopAnimation();
- SendAnimationData();
+ TriggerVectorRasterization();
if(mImpl->mRenderer)
{
mPlacementActor.Reset();
// Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
- mVisualSize = Vector2::ZERO;
- mVisualScale = Vector2::ONE;
+ mVisualSize = Vector2::ZERO;
+ mVisualScale = Vector2::ONE;
+ mAnimationData.width = 0;
+ mAnimationData.height = 0;
DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this);
}
mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
}
- SendAnimationData();
+ TriggerVectorRasterization();
}
}
TriggerVectorRasterization();
}
-void AnimatedVectorImageVisual::OnUploadCompleted()
+void AnimatedVectorImageVisual::OnDoActionExtension(const Property::Index actionId, Dali::Any attributes)
{
- // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
- Actor actor = mPlacementActor.GetHandle();
- if(actor && !mRendererAdded)
+ switch(actionId)
{
- actor.AddRenderer(mImpl->mRenderer);
- mRendererAdded = true;
+ case DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY:
+ {
+ DevelAnimatedVectorImageVisual::DynamicPropertyInfo info = AnyCast<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>(attributes);
+ mAnimationData.dynamicProperties.push_back(info);
+ mAnimationData.resendFlag |= VectorAnimationTask::RESEND_DYNAMIC_PROPERTY;
+ break;
+ }
+ }
- ResourceReady(Toolkit::Visual::ResourceStatus::READY);
+ TriggerVectorRasterization();
+}
- DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this);
+void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceStatus status)
+{
+ if(status == VectorAnimationTask::ResourceStatus::LOADED)
+ {
+ if(mImpl->mEventObserver)
+ {
+ mImpl->mEventObserver->RelayoutRequest(*this);
+ }
}
+ else
+ {
+ mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
+
+ // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
+ Actor actor = mPlacementActor.GetHandle();
+ if(actor && !mRendererAdded)
+ {
+ if(!mLoadFailed)
+ {
+ actor.AddRenderer(mImpl->mRenderer);
+ ResourceReady(Toolkit::Visual::ResourceStatus::READY);
+ }
+ else
+ {
+ Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
+ mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
+ actor.AddRenderer(mImpl->mRenderer);
+ ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
+ }
+
+ mRendererAdded = true;
+ }
+ }
+
+ DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
}
void AnimatedVectorImageVisual::OnAnimationFinished()
void AnimatedVectorImageVisual::SetVectorImageSize()
{
- uint32_t width = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
- uint32_t height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
+ uint32_t width, height;
+ if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
+ {
+ width = mDesiredSize.GetWidth();
+ height = mDesiredSize.GetHeight();
+ }
+ else
+ {
+ width = static_cast<uint32_t>(mVisualSize.width * mVisualScale.width);
+ height = static_cast<uint32_t>(mVisualSize.height * mVisualScale.height);
+ }
- mAnimationData.width = width;
- mAnimationData.height = height;
- mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
+ if(mAnimationData.width != width || mAnimationData.height != height)
+ {
+ mAnimationData.width = width;
+ mAnimationData.height = height;
+ mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
+ }
}
void AnimatedVectorImageVisual::StopAnimation()
{
Vector3 scale = actor.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
- if(mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f)
+ if((!Dali::Equals(mVisualScale.width, scale.width) || !Dali::Equals(mVisualScale.height, scale.height)) && (mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f))
{
mVisualScale.width = scale.width;
mVisualScale.height = scale.height;
Actor actor = mPlacementActor.GetHandle();
if(actor)
{
- Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
- mVisualSize.width = size.width;
- mVisualSize.height = size.height;
+ Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
- DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
+ if(!Dali::Equals(mVisualSize.width, size.width) || !Dali::Equals(mVisualSize.height, size.height))
+ {
+ mVisualSize.width = size.width;
+ mVisualSize.height = size.height;
- SetVectorImageSize();
- SendAnimationData();
+ DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this);
- Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
+ SetVectorImageSize();
+ SendAnimationData();
+
+ Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
+ }
}
}