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/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
33 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
34 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.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 mRendererAdded( false ),
97 mRasterizationTriggered( false )
99 // the rasterized image is with pre-multiplied alpha format
100 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
102 mVectorAnimationTask->UploadCompletedSignal().Connect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
103 mVectorAnimationTask->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
106 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
108 if( mRasterizationTriggered && Adaptor::IsAvailable() )
110 Adaptor::Get().UnregisterProcessor( *this );
113 // Finalize animation task and disconnect the signal in the main thread
114 mVectorAnimationTask->UploadCompletedSignal().Disconnect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
115 mVectorAnimationTask->Finalize();
118 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
120 if( mVisualSize != Vector2::ZERO )
122 naturalSize = mVisualSize;
126 uint32_t width, height;
127 mVectorAnimationTask->GetDefaultSize( width, height );
128 naturalSize.x = width;
129 naturalSize.y = height;
132 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
135 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
138 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
141 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
143 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount );
145 uint32_t startFrame, endFrame;
146 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
148 Property::Array playRange;
149 playRange.PushBack( static_cast< int32_t >( startFrame ) );
150 playRange.PushBack( static_cast< int32_t >( endFrame ) );
151 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
153 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mPlayState ) );
154 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
155 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
157 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior );
158 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode );
160 Property::Map layerInfo;
161 mVectorAnimationTask->GetLayerInfo( layerInfo );
162 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
165 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
170 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
172 // url already passed in from constructor
173 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
175 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
176 if( keyValue.first.type == Property::Key::INDEX )
178 DoSetProperty( keyValue.first.indexKey, keyValue.second );
182 if( keyValue.first == LOOP_COUNT_NAME )
184 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
186 else if( keyValue.first == PLAY_RANGE_NAME )
188 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
190 else if( keyValue.first == STOP_BEHAVIOR_NAME )
192 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
194 else if( keyValue.first == LOOPING_MODE_NAME )
196 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
201 TriggerVectorRasterization();
204 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
208 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
211 if( value.Get( loopCount ) )
213 mAnimationData.loopCount = loopCount;
214 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
218 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
220 Property::Array* array = value.GetArray();
223 mAnimationData.playRange = *array;
224 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
228 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
230 int32_t stopBehavior = mAnimationData.stopBehavior;
231 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
233 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
234 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
238 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
240 int32_t loopingMode = mAnimationData.loopingMode;
241 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
243 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
244 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
251 void AnimatedVectorImageVisual::DoSetOnScene( Actor& actor )
255 if( mImpl->mCustomShader )
257 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
258 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
259 mImpl->mCustomShader->mHints );
261 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
265 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true, false );
268 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
270 mImpl->mRenderer = Renderer::New( geometry, shader );
272 TextureSet textureSet = TextureSet::New();
273 mImpl->mRenderer.SetTextures( textureSet );
275 // Register transform properties
276 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
278 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
280 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
281 mPlacementActor = actor;
283 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
285 // Add property notification for scaling & size
286 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
287 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
289 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
290 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
292 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
294 Window window = DevelWindow::Get( actor );
297 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
300 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnScene [%p]\n", this );
303 void AnimatedVectorImageVisual::DoSetOffScene( Actor& actor )
308 if( mImpl->mRenderer )
310 actor.RemoveRenderer( mImpl->mRenderer );
311 mImpl->mRenderer.Reset();
313 mRendererAdded = false;
316 // Remove property notification
317 actor.RemovePropertyNotification( mScaleNotification );
318 actor.RemovePropertyNotification( mSizeNotification );
320 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
322 Window window = DevelWindow::Get( actor );
325 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
328 mPlacementActor.Reset();
330 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
331 mVisualSize = Vector2::ZERO;
332 mVisualScale = Vector2::ONE;
334 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffScene [%p]\n", this );
337 void AnimatedVectorImageVisual::OnSetTransform()
339 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
341 if( IsOnScene() && visualSize != mVisualSize )
343 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
345 mVisualSize = visualSize;
347 SetVectorImageSize();
349 if( mPlayState == DevelImageVisual::PlayState::PLAYING && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
351 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
352 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
359 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
361 // Check if action is valid for this visual type and perform action if possible
364 case DevelAnimatedVectorImageVisual::Action::PLAY:
366 if( IsOnScene() && mVisualSize != Vector2::ZERO )
368 if( mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
370 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
371 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
374 mPlayState = DevelImageVisual::PlayState::PLAYING;
377 case DevelAnimatedVectorImageVisual::Action::PAUSE:
379 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
381 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
382 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
384 mPlayState = DevelImageVisual::PlayState::PAUSED;
387 case DevelAnimatedVectorImageVisual::Action::STOP:
389 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
391 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
392 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
394 mPlayState = DevelImageVisual::PlayState::STOPPED;
397 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
400 if( attributes.Get( frameNumber ) )
402 mAnimationData.currentFrame = frameNumber;
403 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
407 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
409 Property::Map* map = attributes.GetMap();
412 DoSetProperties( *map );
418 TriggerVectorRasterization();
421 void AnimatedVectorImageVisual::Process()
425 mRasterizationTriggered = false;
427 Adaptor::Get().UnregisterProcessor( *this );
430 void AnimatedVectorImageVisual::OnUploadCompleted()
432 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
433 Actor actor = mPlacementActor.GetHandle();
434 if( actor && !mRendererAdded )
436 actor.AddRenderer( mImpl->mRenderer );
437 mRendererAdded = true;
439 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
441 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
445 void AnimatedVectorImageVisual::OnAnimationFinished()
447 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this );
449 if( mPlayState != DevelImageVisual::PlayState::STOPPED )
451 mPlayState = DevelImageVisual::PlayState::STOPPED;
453 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
455 if( mImpl->mEventObserver )
457 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
461 if( mImpl->mRenderer )
463 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
467 void AnimatedVectorImageVisual::SendAnimationData()
469 if( mAnimationData.resendFlag )
471 mVectorAnimationTask->SetAnimationData( mAnimationData );
473 if( mImpl->mRenderer )
475 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
477 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
481 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
485 mAnimationData.resendFlag = 0;
489 void AnimatedVectorImageVisual::SetVectorImageSize()
491 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
492 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
494 mAnimationData.width = width;
495 mAnimationData.height = height;
496 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
499 void AnimatedVectorImageVisual::StopAnimation()
501 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
503 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
504 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
506 mPlayState = DevelImageVisual::PlayState::STOPPED;
510 void AnimatedVectorImageVisual::TriggerVectorRasterization()
512 if( !mRasterizationTriggered )
514 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
516 Adaptor::Get().RegisterProcessor( *this );
517 mRasterizationTriggered = true;
521 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
523 Actor actor = mPlacementActor.GetHandle();
526 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
527 mVisualScale.width = scale.width;
528 mVisualScale.height = scale.height;
530 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
532 SetVectorImageSize();
535 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
539 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
541 Actor actor = mPlacementActor.GetHandle();
544 Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
545 mVisualSize.width = size.width;
546 mVisualSize.height = size.height;
548 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
550 SetVectorImageSize();
553 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger event processing
557 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
562 TriggerVectorRasterization();
564 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
568 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
573 TriggerVectorRasterization();
575 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
579 } // namespace Internal
581 } // namespace Toolkit