2 * Copyright (c) 2020 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/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>
36 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
50 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
53 DALI_ENUM_TO_STRING_TABLE_BEGIN( STOP_BEHAVIOR )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME )
56 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME )
57 DALI_ENUM_TO_STRING_TABLE_END( STOP_BEHAVIOR )
60 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOOPING_MODE )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, RESTART )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, AUTO_REVERSE )
63 DALI_ENUM_TO_STRING_TABLE_END( LOOPING_MODE )
65 #if defined(DEBUG_ENABLED)
66 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
69 } // unnamed namespace
71 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
73 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
74 visual->SetProperties( properties );
79 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
81 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
86 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
87 : Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>( Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE ) ),
90 mVectorAnimationTask( new VectorAnimationTask( factoryCache, imageUrl.GetUrl() ) ),
91 mImageVisualShaderFactory( shaderFactory ),
93 mVisualScale( Vector2::ONE ),
95 mPlayState( DevelImageVisual::PlayState::STOPPED ),
96 mEventCallback( nullptr ),
97 mRendererAdded( false ),
100 // the rasterized image is with pre-multiplied alpha format
101 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
103 mVectorAnimationTask->UploadCompletedSignal().Connect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
104 mVectorAnimationTask->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
106 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
107 vectorAnimationManager.AddObserver(*this);
110 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
112 if( ! mCoreShutdown )
114 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
115 vectorAnimationManager.RemoveObserver(*this);
119 mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback( mEventCallback );
122 // Finalize animation task and disconnect the signal in the main thread
123 mVectorAnimationTask->UploadCompletedSignal().Disconnect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
124 mVectorAnimationTask->Finalize();
128 void AnimatedVectorImageVisual::VectorAnimationManagerDestroyed()
130 // Core is shutting down. Don't talk to the plugin any more.
131 mCoreShutdown = true;
134 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
136 if( mVisualSize != Vector2::ZERO )
138 naturalSize = mVisualSize;
142 uint32_t width, height;
143 mVectorAnimationTask->GetDefaultSize( width, height );
144 naturalSize.x = width;
145 naturalSize.y = height;
148 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
151 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
154 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
157 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
159 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount );
161 uint32_t startFrame, endFrame;
162 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
164 Property::Array playRange;
165 playRange.PushBack( static_cast< int32_t >( startFrame ) );
166 playRange.PushBack( static_cast< int32_t >( endFrame ) );
167 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
169 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mPlayState ) );
170 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
171 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
173 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior );
174 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode );
176 Property::Map layerInfo;
177 mVectorAnimationTask->GetLayerInfo( layerInfo );
178 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
181 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
186 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
188 // url already passed in from constructor
189 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
191 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
192 if( keyValue.first.type == Property::Key::INDEX )
194 DoSetProperty( keyValue.first.indexKey, keyValue.second );
198 if( keyValue.first == LOOP_COUNT_NAME )
200 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
202 else if( keyValue.first == PLAY_RANGE_NAME )
204 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
206 else if( keyValue.first == STOP_BEHAVIOR_NAME )
208 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
210 else if( keyValue.first == LOOPING_MODE_NAME )
212 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
217 TriggerVectorRasterization();
220 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
224 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
227 if( value.Get( loopCount ) )
229 mAnimationData.loopCount = loopCount;
230 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
234 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
236 const Property::Array* array = value.GetArray();
239 mAnimationData.playRange = *array;
240 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
244 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
246 int32_t stopBehavior = mAnimationData.stopBehavior;
247 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
249 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
250 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
254 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
256 int32_t loopingMode = mAnimationData.loopingMode;
257 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
259 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
260 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
267 void AnimatedVectorImageVisual::DoSetOnScene( Actor& actor )
271 if( mImpl->mCustomShader )
273 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
274 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
275 mImpl->mCustomShader->mHints );
277 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
281 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true, false );
284 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
286 mImpl->mRenderer = Renderer::New( geometry, shader );
288 TextureSet textureSet = TextureSet::New();
289 mImpl->mRenderer.SetTextures( textureSet );
291 // Register transform properties
292 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
294 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
296 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
297 mPlacementActor = actor;
299 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
301 // Add property notification for scaling & size
302 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
303 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
305 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
306 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
308 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
310 Window window = DevelWindow::Get( actor );
313 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
316 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this );
319 void AnimatedVectorImageVisual::DoSetOffScene( Actor& actor )
324 if( mImpl->mRenderer )
326 actor.RemoveRenderer( mImpl->mRenderer );
327 mImpl->mRenderer.Reset();
329 mRendererAdded = false;
332 // Remove property notification
333 actor.RemovePropertyNotification( mScaleNotification );
334 actor.RemovePropertyNotification( mSizeNotification );
336 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
338 Window window = DevelWindow::Get( actor );
341 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
344 mPlacementActor.Reset();
346 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
347 mVisualSize = Vector2::ZERO;
348 mVisualScale = Vector2::ONE;
350 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this );
353 void AnimatedVectorImageVisual::OnSetTransform()
355 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
357 if( IsOnScene() && visualSize != mVisualSize )
359 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
361 mVisualSize = visualSize;
363 SetVectorImageSize();
365 if( mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
367 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
368 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
375 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
377 // Check if action is valid for this visual type and perform action if possible
380 case DevelAnimatedVectorImageVisual::Action::PLAY:
382 if( IsOnScene() && mVisualSize != Vector2::ZERO )
384 if( mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
386 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
387 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
390 mPlayState = DevelImageVisual::PlayState::PLAYING;
393 case DevelAnimatedVectorImageVisual::Action::PAUSE:
395 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
397 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
398 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
400 mPlayState = DevelImageVisual::PlayState::PAUSED;
403 case DevelAnimatedVectorImageVisual::Action::STOP:
405 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
407 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
408 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
410 mPlayState = DevelImageVisual::PlayState::STOPPED;
413 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
416 if( attributes.Get( frameNumber ) )
418 mAnimationData.currentFrame = frameNumber;
419 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
423 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
425 const Property::Map* map = attributes.GetMap();
428 DoSetProperties( *map );
434 TriggerVectorRasterization();
437 void AnimatedVectorImageVisual::OnUploadCompleted()
439 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
440 Actor actor = mPlacementActor.GetHandle();
441 if( actor && !mRendererAdded )
443 actor.AddRenderer( mImpl->mRenderer );
444 mRendererAdded = true;
446 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
448 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
452 void AnimatedVectorImageVisual::OnAnimationFinished()
454 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this );
456 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
458 mPlayState = DevelImageVisual::PlayState::STOPPED;
460 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
462 if( mImpl->mEventObserver )
464 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
468 if( mImpl->mRenderer )
470 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
474 void AnimatedVectorImageVisual::SendAnimationData()
476 if( mAnimationData.resendFlag )
478 mVectorAnimationTask->SetAnimationData( mAnimationData );
480 if( mImpl->mRenderer )
482 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
484 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
488 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
492 mAnimationData.resendFlag = 0;
496 void AnimatedVectorImageVisual::SetVectorImageSize()
498 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
499 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
501 mAnimationData.width = width;
502 mAnimationData.height = height;
503 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
506 void AnimatedVectorImageVisual::StopAnimation()
508 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
510 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
511 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
513 mPlayState = DevelImageVisual::PlayState::STOPPED;
517 void AnimatedVectorImageVisual::TriggerVectorRasterization()
519 if( !mEventCallback && !mCoreShutdown )
521 mEventCallback = MakeCallback( this, &AnimatedVectorImageVisual::OnProcessEvents );
522 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
523 vectorAnimationManager.RegisterEventCallback( mEventCallback );
524 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
528 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
530 Actor actor = mPlacementActor.GetHandle();
533 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
534 mVisualScale.width = scale.width;
535 mVisualScale.height = scale.height;
537 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
539 SetVectorImageSize();
542 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
546 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
548 Actor actor = mPlacementActor.GetHandle();
551 Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
552 mVisualSize.width = size.width;
553 mVisualSize.height = size.height;
555 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
557 SetVectorImageSize();
560 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
564 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
569 TriggerVectorRasterization();
571 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
575 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
580 TriggerVectorRasterization();
582 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
586 void AnimatedVectorImageVisual::OnProcessEvents()
590 mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
593 } // namespace Internal
595 } // namespace Toolkit