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 ),
99 mRedrawInScalingDown(true)
101 // the rasterized image is with pre-multiplied alpha format
102 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
104 mVectorAnimationTask->UploadCompletedSignal().Connect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
105 mVectorAnimationTask->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
107 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
108 vectorAnimationManager.AddObserver(*this);
111 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
113 if( ! mCoreShutdown )
115 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
116 vectorAnimationManager.RemoveObserver(*this);
120 mFactoryCache.GetVectorAnimationManager().UnregisterEventCallback( mEventCallback );
123 // Finalize animation task and disconnect the signal in the main thread
124 mVectorAnimationTask->UploadCompletedSignal().Disconnect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
125 mVectorAnimationTask->Finalize();
129 void AnimatedVectorImageVisual::VectorAnimationManagerDestroyed()
131 // Core is shutting down. Don't talk to the plugin any more.
132 mCoreShutdown = true;
135 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
137 if( mVisualSize != Vector2::ZERO )
139 naturalSize = mVisualSize;
143 uint32_t width, height;
144 mVectorAnimationTask->GetDefaultSize( width, height );
145 naturalSize.x = width;
146 naturalSize.y = height;
149 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
152 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
155 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
158 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
160 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount );
162 uint32_t startFrame, endFrame;
163 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
165 Property::Array playRange;
166 playRange.PushBack( static_cast< int32_t >( startFrame ) );
167 playRange.PushBack( static_cast< int32_t >( endFrame ) );
168 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
170 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mPlayState ) );
171 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
172 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
174 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior );
175 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode );
176 map.Insert( Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, mRedrawInScalingDown );
178 Property::Map layerInfo;
179 mVectorAnimationTask->GetLayerInfo( layerInfo );
180 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
183 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
188 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
190 // url already passed in from constructor
191 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
193 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
194 if( keyValue.first.type == Property::Key::INDEX )
196 DoSetProperty( keyValue.first.indexKey, keyValue.second );
200 if( keyValue.first == LOOP_COUNT_NAME )
202 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
204 else if( keyValue.first == PLAY_RANGE_NAME )
206 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
208 else if( keyValue.first == STOP_BEHAVIOR_NAME )
210 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
212 else if( keyValue.first == LOOPING_MODE_NAME )
214 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
216 else if( keyValue.first == REDRAW_IN_SCALING_DOWN_NAME )
218 DoSetProperty( Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN, keyValue.second );
223 TriggerVectorRasterization();
226 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
230 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
233 if( value.Get( loopCount ) )
235 mAnimationData.loopCount = loopCount;
236 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
240 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
242 const Property::Array* array = value.GetArray();
245 mAnimationData.playRange = *array;
246 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
250 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
252 int32_t stopBehavior = mAnimationData.stopBehavior;
253 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
255 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
256 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
260 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
262 int32_t loopingMode = mAnimationData.loopingMode;
263 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
265 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
266 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
270 case Toolkit::DevelImageVisual::Property::REDRAW_IN_SCALING_DOWN:
273 if( value.Get( redraw ) )
275 mRedrawInScalingDown = redraw;
282 void AnimatedVectorImageVisual::DoSetOnScene( Actor& actor )
286 if( mImpl->mCustomShader )
288 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource().data() : mImpl->mCustomShader->mVertexShader,
289 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource().data() : mImpl->mCustomShader->mFragmentShader,
290 mImpl->mCustomShader->mHints );
292 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
296 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true, false );
299 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
301 mImpl->mRenderer = Renderer::New( geometry, shader );
303 TextureSet textureSet = TextureSet::New();
304 mImpl->mRenderer.SetTextures( textureSet );
306 // Register transform properties
307 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
309 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
311 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
312 mPlacementActor = actor;
314 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
316 // Add property notification for scaling & size
317 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
318 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
320 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
321 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
323 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
325 Window window = DevelWindow::Get( actor );
328 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
331 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this );
334 void AnimatedVectorImageVisual::DoSetOffScene( Actor& actor )
339 if( mImpl->mRenderer )
341 actor.RemoveRenderer( mImpl->mRenderer );
342 mImpl->mRenderer.Reset();
344 mRendererAdded = false;
347 // Remove property notification
348 actor.RemovePropertyNotification( mScaleNotification );
349 actor.RemovePropertyNotification( mSizeNotification );
351 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
353 Window window = DevelWindow::Get( actor );
356 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
359 mPlacementActor.Reset();
361 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
362 mVisualSize = Vector2::ZERO;
363 mVisualScale = Vector2::ONE;
365 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this );
368 void AnimatedVectorImageVisual::OnSetTransform()
370 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
372 if( IsOnScene() && visualSize != mVisualSize )
374 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
376 mVisualSize = visualSize;
378 SetVectorImageSize();
380 if( mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
382 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
383 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
390 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
392 // Check if action is valid for this visual type and perform action if possible
395 case DevelAnimatedVectorImageVisual::Action::PLAY:
397 if( IsOnScene() && mVisualSize != Vector2::ZERO )
399 if( mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
401 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
402 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
405 mPlayState = DevelImageVisual::PlayState::PLAYING;
408 case DevelAnimatedVectorImageVisual::Action::PAUSE:
410 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
412 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
413 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
415 mPlayState = DevelImageVisual::PlayState::PAUSED;
418 case DevelAnimatedVectorImageVisual::Action::STOP:
420 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
422 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
423 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
425 mPlayState = DevelImageVisual::PlayState::STOPPED;
428 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
431 if( attributes.Get( frameNumber ) )
433 mAnimationData.currentFrame = frameNumber;
434 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
438 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
440 const Property::Map* map = attributes.GetMap();
443 DoSetProperties( *map );
449 TriggerVectorRasterization();
452 void AnimatedVectorImageVisual::OnUploadCompleted()
454 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
455 Actor actor = mPlacementActor.GetHandle();
456 if( actor && !mRendererAdded )
458 actor.AddRenderer( mImpl->mRenderer );
459 mRendererAdded = true;
461 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
463 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
467 void AnimatedVectorImageVisual::OnAnimationFinished()
469 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this );
471 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
473 mPlayState = DevelImageVisual::PlayState::STOPPED;
475 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
477 if( mImpl->mEventObserver )
479 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
483 if( mImpl->mRenderer )
485 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
489 void AnimatedVectorImageVisual::SendAnimationData()
491 if( mAnimationData.resendFlag )
493 mVectorAnimationTask->SetAnimationData( mAnimationData );
495 if( mImpl->mRenderer )
497 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
499 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
503 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
507 mAnimationData.resendFlag = 0;
511 void AnimatedVectorImageVisual::SetVectorImageSize()
513 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
514 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
516 mAnimationData.width = width;
517 mAnimationData.height = height;
518 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
521 void AnimatedVectorImageVisual::StopAnimation()
523 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
525 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
526 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
528 mPlayState = DevelImageVisual::PlayState::STOPPED;
532 void AnimatedVectorImageVisual::TriggerVectorRasterization()
534 if( !mEventCallback && !mCoreShutdown )
536 mEventCallback = MakeCallback( this, &AnimatedVectorImageVisual::OnProcessEvents );
537 auto& vectorAnimationManager = mFactoryCache.GetVectorAnimationManager();
538 vectorAnimationManager.RegisterEventCallback( mEventCallback );
539 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
543 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
545 Actor actor = mPlacementActor.GetHandle();
548 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
550 if(mRedrawInScalingDown || scale.width >= 1.0f || scale.height >= 1.0f)
552 mVisualScale.width = scale.width;
553 mVisualScale.height = scale.height;
555 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
557 SetVectorImageSize();
560 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
565 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
567 Actor actor = mPlacementActor.GetHandle();
570 Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
571 mVisualSize.width = size.width;
572 mVisualSize.height = size.height;
574 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
576 SetVectorImageSize();
579 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
583 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
588 TriggerVectorRasterization();
590 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
594 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
599 TriggerVectorRasterization();
601 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
605 void AnimatedVectorImageVisual::OnProcessEvents()
609 mEventCallback = nullptr; // The callback will be deleted in the VectorAnimationManager
612 } // namespace Internal
614 } // namespace Toolkit