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 in the main thread
122 mVectorAnimationTask->Finalize();
125 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
127 if( mVisualSize != Vector2::ZERO )
129 naturalSize = mVisualSize;
133 uint32_t width, height;
134 mVectorAnimationTask->GetDefaultSize( width, height );
135 naturalSize.x = width;
136 naturalSize.y = height;
139 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
142 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
145 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
148 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
150 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
152 uint32_t startFrame, endFrame;
153 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
155 Property::Array playRange;
156 playRange.PushBack( static_cast< int32_t >( startFrame ) );
157 playRange.PushBack( static_cast< int32_t >( endFrame ) );
158 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
160 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mVectorAnimationTask->GetPlayState() ) );
161 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
162 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
164 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mStopBehavior );
165 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mLoopingMode );
167 Property::Map layerInfo;
168 mVectorAnimationTask->GetLayerInfo( layerInfo );
169 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
172 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
177 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
179 // url already passed in from constructor
180 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
182 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
183 if( keyValue.first.type == Property::Key::INDEX )
185 DoSetProperty( keyValue.first.indexKey, keyValue.second );
189 if( keyValue.first == LOOP_COUNT_NAME )
191 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
193 else if( keyValue.first == PLAY_RANGE_NAME )
195 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
197 else if( keyValue.first == STOP_BEHAVIOR_NAME )
199 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
201 else if( keyValue.first == LOOPING_MODE_NAME )
203 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
209 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
213 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
216 if( value.Get( loopCount ) )
218 mLoopCount = loopCount;
219 mResendFlag |= RESEND_LOOP_COUNT;
223 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
225 Property::Array* array = value.GetArray();
229 mResendFlag |= RESEND_PLAY_RANGE;
233 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
235 int32_t stopBehavior = mStopBehavior;
236 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
238 mStopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
239 mResendFlag |= RESEND_STOP_BEHAVIOR;
243 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
245 int32_t loopingMode = mLoopingMode;
246 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
248 mLoopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
249 mResendFlag |= RESEND_LOOPING_MODE;
256 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
260 if( mImpl->mCustomShader )
262 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
263 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
264 mImpl->mCustomShader->mHints );
266 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
270 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true, false );
273 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
275 mImpl->mRenderer = Renderer::New( geometry, shader );
277 TextureSet textureSet = TextureSet::New();
278 mImpl->mRenderer.SetTextures( textureSet );
280 // Register transform properties
281 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
283 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
285 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
286 mPlacementActor = actor;
288 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
290 // Add property notification for scaling & size
291 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
292 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
294 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
295 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
297 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
299 Window window = DevelWindow::Get( actor );
302 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
305 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
308 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
312 if( mImpl->mRenderer )
314 actor.RemoveRenderer( mImpl->mRenderer );
315 mImpl->mRenderer.Reset();
317 mRendererAdded = false;
320 // Remove property notification
321 actor.RemovePropertyNotification( mScaleNotification );
322 actor.RemovePropertyNotification( mSizeNotification );
324 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
326 Window window = DevelWindow::Get( actor );
329 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
332 mPlacementActor.Reset();
334 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
335 mVisualSize = Vector2::ZERO;
336 mVisualScale = Vector2::ONE;
338 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffStage [%p]\n", this );
341 void AnimatedVectorImageVisual::OnSetTransform()
343 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
345 if( IsOnStage() && visualSize != mVisualSize )
347 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
349 mVisualSize = visualSize;
351 SetVectorImageSize();
355 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
357 mVectorAnimationTask->PlayAnimation();
359 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
364 mVectorAnimationTask->RenderFrame();
369 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
371 // Check if action is valid for this visual type and perform action if possible
374 case DevelAnimatedVectorImageVisual::Action::PLAY:
376 if( IsOnStage() && mVisualSize != Vector2::ZERO )
378 mVectorAnimationTask->PlayAnimation();
380 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
382 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
385 case DevelAnimatedVectorImageVisual::Action::PAUSE:
387 mVectorAnimationTask->PauseAnimation();
389 if( mImpl->mRenderer )
391 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
394 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
397 case DevelAnimatedVectorImageVisual::Action::STOP:
399 if( mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::STOPPED )
401 mVectorAnimationTask->StopAnimation();
404 if( mImpl->mRenderer )
406 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
409 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
412 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
415 if( attributes.Get( frameNumber ) )
417 mVectorAnimationTask->SetCurrentFrameNumber( frameNumber );
419 if( IsOnStage() && mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::PLAYING )
421 mVectorAnimationTask->RenderFrame();
422 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
427 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
429 Property::Map* map = attributes.GetMap();
432 DoSetProperties( *map );
441 void AnimatedVectorImageVisual::OnUploadCompleted()
443 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
444 Actor actor = mPlacementActor.GetHandle();
445 if( actor && !mRendererAdded )
447 actor.AddRenderer( mImpl->mRenderer );
448 mRendererAdded = true;
450 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
452 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
456 void AnimatedVectorImageVisual::OnAnimationFinished()
458 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mActionStatus, this );
460 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
462 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
464 if( mImpl->mEventObserver )
466 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
470 if( mImpl->mRenderer )
472 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
476 void AnimatedVectorImageVisual::SendAnimationData()
480 bool isPlaying = false;
481 if( mVectorAnimationTask->GetPlayState() == DevelImageVisual::PlayState::PLAYING )
483 mVectorAnimationTask->PauseAnimation();
487 if( mResendFlag & RESEND_LOOP_COUNT )
489 mVectorAnimationTask->SetLoopCount( mLoopCount );
492 if( mResendFlag & RESEND_PLAY_RANGE )
494 mVectorAnimationTask->SetPlayRange( mPlayRange );
497 if( mResendFlag & RESEND_STOP_BEHAVIOR )
499 mVectorAnimationTask->SetStopBehavior( mStopBehavior );
502 if( mResendFlag & RESEND_LOOPING_MODE )
504 mVectorAnimationTask->SetLoopingMode( mLoopingMode );
511 mVectorAnimationTask->PlayAnimation();
515 mVectorAnimationTask->RenderFrame();
516 Stage::GetCurrent().KeepRendering( 0.0f );
524 void AnimatedVectorImageVisual::SetVectorImageSize()
526 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
527 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
529 mVectorAnimationTask->SetSize( width, height );
531 if( IsOnStage() && mVectorAnimationTask->GetPlayState() != DevelImageVisual::PlayState::PLAYING )
533 mVectorAnimationTask->RenderFrame();
534 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
538 void AnimatedVectorImageVisual::StopAnimation()
540 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
542 mVectorAnimationTask->StopAnimation();
544 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
546 if( mImpl->mRenderer )
548 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
553 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
555 Actor actor = mPlacementActor.GetHandle();
558 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
559 mVisualScale.width = scale.width;
560 mVisualScale.height = scale.height;
562 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
564 SetVectorImageSize();
568 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
570 Actor actor = mPlacementActor.GetHandle();
573 Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE );
574 mVisualSize.width = size.width;
575 mVisualSize.height = size.height;
577 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
579 SetVectorImageSize();
583 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
589 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
593 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
599 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
603 } // namespace Internal
605 } // namespace Toolkit