2 * Copyright (c) 2019 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/public-api/common/stage.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/devel-api/adaptor-framework/window-devel.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
29 #include <dali-toolkit/public-api/visuals/visual-properties.h>
30 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
32 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
49 constexpr auto LOOP_FOREVER = -1;
51 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
53 // Flags for re-sending data to the rasterize thread
56 RESEND_PLAY_RANGE = 1 << 0,
57 RESEND_LOOP_COUNT = 1 << 1,
58 RESEND_STOP_BEHAVIOR = 1 << 2,
59 RESEND_LOOPING_MODE = 1 << 3
63 DALI_ENUM_TO_STRING_TABLE_BEGIN( STOP_BEHAVIOR )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME )
66 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME )
67 DALI_ENUM_TO_STRING_TABLE_END( STOP_BEHAVIOR )
70 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOOPING_MODE )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, RESTART )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, AUTO_REVERSE )
73 DALI_ENUM_TO_STRING_TABLE_END( LOOPING_MODE )
75 #if defined(DEBUG_ENABLED)
76 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
79 } // unnamed namespace
81 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
83 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
84 visual->SetProperties( properties );
89 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
91 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
96 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
97 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
99 mVectorAnimationTask( new VectorAnimationTask( factoryCache, imageUrl.GetUrl() ) ),
100 mImageVisualShaderFactory( shaderFactory ),
102 mVisualScale( Vector2::ONE ),
105 mLoopCount( LOOP_FOREVER ),
107 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP ),
108 mStopBehavior( DevelImageVisual::StopBehavior::CURRENT_FRAME ),
109 mLoopingMode( DevelImageVisual::LoopingMode::RESTART ),
110 mRendererAdded( false )
112 // the rasterized image is with pre-multiplied alpha format
113 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
115 mVectorAnimationTask->UploadCompletedSignal().Connect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
116 mVectorAnimationTask->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
119 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
121 // Finalize animation task and disconnect the signal in the main thread
122 mVectorAnimationTask->UploadCompletedSignal().Disconnect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
123 mVectorAnimationTask->Finalize();
126 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
128 if( mVisualSize != Vector2::ZERO )
130 naturalSize = mVisualSize;
134 uint32_t width, height;
135 mVectorAnimationTask->GetDefaultSize( width, height );
136 naturalSize.x = width;
137 naturalSize.y = height;
140 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
143 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
146 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
149 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
151 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
153 uint32_t startFrame, endFrame;
154 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
156 Property::Array playRange;
157 playRange.PushBack( static_cast< int32_t >( startFrame ) );
158 playRange.PushBack( static_cast< int32_t >( endFrame ) );
159 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
161 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mVectorAnimationTask->GetPlayState() ) );
162 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
163 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
165 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mStopBehavior );
166 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mLoopingMode );
168 Property::Map layerInfo;
169 mVectorAnimationTask->GetLayerInfo( layerInfo );
170 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
173 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
178 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
180 // url already passed in from constructor
181 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
183 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
184 if( keyValue.first.type == Property::Key::INDEX )
186 DoSetProperty( keyValue.first.indexKey, keyValue.second );
190 if( keyValue.first == LOOP_COUNT_NAME )
192 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
194 else if( keyValue.first == PLAY_RANGE_NAME )
196 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
198 else if( keyValue.first == STOP_BEHAVIOR_NAME )
200 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
202 else if( keyValue.first == LOOPING_MODE_NAME )
204 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
210 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
214 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
217 if( value.Get( loopCount ) )
219 mLoopCount = loopCount;
220 mResendFlag |= RESEND_LOOP_COUNT;
224 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
226 Property::Array* array = value.GetArray();
230 mResendFlag |= RESEND_PLAY_RANGE;
234 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
236 int32_t stopBehavior = mStopBehavior;
237 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
239 mStopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
240 mResendFlag |= RESEND_STOP_BEHAVIOR;
244 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
246 int32_t loopingMode = mLoopingMode;
247 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
249 mLoopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
250 mResendFlag |= RESEND_LOOPING_MODE;
257 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
261 if( mImpl->mCustomShader )
263 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
264 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
265 mImpl->mCustomShader->mHints );
267 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
271 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true, false );
274 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
276 mImpl->mRenderer = Renderer::New( geometry, shader );
278 TextureSet textureSet = TextureSet::New();
279 mImpl->mRenderer.SetTextures( textureSet );
281 // Register transform properties
282 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
284 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
286 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
287 mPlacementActor = actor;
289 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
291 // Add property notification for scaling & size
292 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
293 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
295 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
296 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
298 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
300 Window window = DevelWindow::Get( actor );
303 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
306 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
309 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
313 if( mImpl->mRenderer )
315 actor.RemoveRenderer( mImpl->mRenderer );
316 mImpl->mRenderer.Reset();
318 mRendererAdded = false;
321 // Remove property notification
322 actor.RemovePropertyNotification( mScaleNotification );
323 actor.RemovePropertyNotification( mSizeNotification );
325 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
327 Window window = DevelWindow::Get( actor );
330 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
333 mPlacementActor.Reset();
335 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
336 mVisualSize = Vector2::ZERO;
337 mVisualScale = Vector2::ONE;
339 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffStage [%p]\n", this );
342 void AnimatedVectorImageVisual::OnSetTransform()
344 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
346 if( IsOnStage() && visualSize != mVisualSize )
348 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
350 mVisualSize = visualSize;
352 SetVectorImageSize();
356 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
358 mVectorAnimationTask->PlayAnimation();
360 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
365 mVectorAnimationTask->RenderFrame();
370 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
372 // Check if action is valid for this visual type and perform action if possible
375 case DevelAnimatedVectorImageVisual::Action::PLAY:
377 if( IsOnStage() && mVisualSize != Vector2::ZERO )
379 mVectorAnimationTask->PlayAnimation();
381 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
383 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
386 case DevelAnimatedVectorImageVisual::Action::PAUSE:
388 mVectorAnimationTask->PauseAnimation();
390 if( mImpl->mRenderer )
392 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
395 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
398 case DevelAnimatedVectorImageVisual::Action::STOP:
400 if( mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::STOPPED )
402 mVectorAnimationTask->StopAnimation();
405 if( mImpl->mRenderer )
407 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
410 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
413 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
416 if( attributes.Get( frameNumber ) )
418 mVectorAnimationTask->SetCurrentFrameNumber( frameNumber );
420 if( IsOnStage() && mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::PLAYING )
422 mVectorAnimationTask->RenderFrame();
423 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
428 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
430 Property::Map* map = attributes.GetMap();
433 DoSetProperties( *map );
442 void AnimatedVectorImageVisual::OnUploadCompleted()
444 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
445 Actor actor = mPlacementActor.GetHandle();
446 if( actor && !mRendererAdded )
448 actor.AddRenderer( mImpl->mRenderer );
449 mRendererAdded = true;
451 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
453 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
457 void AnimatedVectorImageVisual::OnAnimationFinished()
459 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mActionStatus, this );
461 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
463 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
465 if( mImpl->mEventObserver )
467 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
471 if( mImpl->mRenderer )
473 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
477 void AnimatedVectorImageVisual::SendAnimationData()
481 bool isPlaying = false;
482 if( mVectorAnimationTask->GetPlayState() == DevelImageVisual::PlayState::PLAYING )
484 mVectorAnimationTask->PauseAnimation();
488 if( mResendFlag & RESEND_LOOP_COUNT )
490 mVectorAnimationTask->SetLoopCount( mLoopCount );
493 if( mResendFlag & RESEND_PLAY_RANGE )
495 mVectorAnimationTask->SetPlayRange( mPlayRange );
498 if( mResendFlag & RESEND_STOP_BEHAVIOR )
500 mVectorAnimationTask->SetStopBehavior( mStopBehavior );
503 if( mResendFlag & RESEND_LOOPING_MODE )
505 mVectorAnimationTask->SetLoopingMode( mLoopingMode );
512 mVectorAnimationTask->PlayAnimation();
516 mVectorAnimationTask->RenderFrame();
517 Stage::GetCurrent().KeepRendering( 0.0f );
525 void AnimatedVectorImageVisual::SetVectorImageSize()
527 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
528 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
530 mVectorAnimationTask->SetSize( width, height );
532 if( IsOnStage() && mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::PLAYING )
534 mVectorAnimationTask->RenderFrame();
535 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
539 void AnimatedVectorImageVisual::StopAnimation()
541 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
543 mVectorAnimationTask->StopAnimation();
545 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
547 if( mImpl->mRenderer )
549 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
554 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
556 Actor actor = mPlacementActor.GetHandle();
559 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
560 mVisualScale.width = scale.width;
561 mVisualScale.height = scale.height;
563 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
565 SetVectorImageSize();
569 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
571 Actor actor = mPlacementActor.GetHandle();
574 Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
575 mVisualSize.width = size.width;
576 mVisualSize.height = size.height;
578 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
580 SetVectorImageSize();
584 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
590 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
594 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
600 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
604 } // namespace Internal
606 } // namespace Toolkit