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/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 ),
90 mVectorAnimationTask( new VectorAnimationTask( factoryCache, imageUrl.GetUrl() ) ),
91 mImageVisualShaderFactory( shaderFactory ),
93 mVisualScale( Vector2::ONE ),
95 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP ),
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()
110 if( mRasterizationTriggered && Adaptor::IsAvailable() )
112 Adaptor::Get().UnregisterProcessor( *this );
115 // Finalize animation task and disconnect the signal in the main thread
116 mVectorAnimationTask->UploadCompletedSignal().Disconnect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
117 mVectorAnimationTask->Finalize();
120 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
122 if( mVisualSize != Vector2::ZERO )
124 naturalSize = mVisualSize;
128 uint32_t width, height;
129 mVectorAnimationTask->GetDefaultSize( width, height );
130 naturalSize.x = width;
131 naturalSize.y = height;
134 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::GetNaturalSize: w = %f, h = %f [%p]\n", naturalSize.width, naturalSize.height, this );
137 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
140 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
143 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
145 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mAnimationData.loopCount );
147 uint32_t startFrame, endFrame;
148 mVectorAnimationTask->GetPlayRange( startFrame, endFrame );
150 Property::Array playRange;
151 playRange.PushBack( static_cast< int32_t >( startFrame ) );
152 playRange.PushBack( static_cast< int32_t >( endFrame ) );
153 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
155 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int32_t >( mVectorAnimationTask->GetPlayState() ) );
156 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetCurrentFrameNumber() ) );
157 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorAnimationTask->GetTotalFrameNumber() ) );
159 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mAnimationData.stopBehavior );
160 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mAnimationData.loopingMode );
162 Property::Map layerInfo;
163 mVectorAnimationTask->GetLayerInfo( layerInfo );
164 map.Insert( Toolkit::DevelImageVisual::Property::CONTENT_INFO, layerInfo );
167 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
172 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
174 // url already passed in from constructor
175 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
177 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
178 if( keyValue.first.type == Property::Key::INDEX )
180 DoSetProperty( keyValue.first.indexKey, keyValue.second );
184 if( keyValue.first == LOOP_COUNT_NAME )
186 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
188 else if( keyValue.first == PLAY_RANGE_NAME )
190 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
192 else if( keyValue.first == STOP_BEHAVIOR_NAME )
194 DoSetProperty( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, keyValue.second );
196 else if( keyValue.first == LOOPING_MODE_NAME )
198 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOPING_MODE, keyValue.second );
203 TriggerVectorRasterization();
206 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
210 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
213 if( value.Get( loopCount ) )
215 mAnimationData.loopCount = loopCount;
216 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOP_COUNT;
220 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
222 Property::Array* array = value.GetArray();
225 mAnimationData.playRange = *array;
226 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_RANGE;
230 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
232 int32_t stopBehavior = mAnimationData.stopBehavior;
233 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
235 mAnimationData.stopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
236 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_STOP_BEHAVIOR;
240 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
242 int32_t loopingMode = mAnimationData.loopingMode;
243 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
245 mAnimationData.loopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
246 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_LOOPING_MODE;
253 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
257 if( mImpl->mCustomShader )
259 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
260 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
261 mImpl->mCustomShader->mHints );
263 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
267 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
270 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
272 mImpl->mRenderer = Renderer::New( geometry, shader );
274 TextureSet textureSet = TextureSet::New();
275 mImpl->mRenderer.SetTextures( textureSet );
277 // Register transform properties
278 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
280 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
282 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
283 mPlacementActor = actor;
285 mVectorAnimationTask->SetRenderer( mImpl->mRenderer );
287 // Add property notification for scaling & size
288 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
289 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
291 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
292 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
294 DevelActor::VisibilityChangedSignal( actor ).Connect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
296 Window window = DevelWindow::Get( actor );
299 DevelWindow::VisibilityChangedSignal( window ).Connect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
302 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
305 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
310 if( mImpl->mRenderer )
312 actor.RemoveRenderer( mImpl->mRenderer );
313 mImpl->mRenderer.Reset();
315 mRendererAdded = false;
318 // Remove property notification
319 actor.RemovePropertyNotification( mScaleNotification );
320 actor.RemovePropertyNotification( mSizeNotification );
322 DevelActor::VisibilityChangedSignal( actor ).Disconnect( this, &AnimatedVectorImageVisual::OnControlVisibilityChanged );
324 Window window = DevelWindow::Get( actor );
327 DevelWindow::VisibilityChangedSignal( window ).Disconnect( this, &AnimatedVectorImageVisual::OnWindowVisibilityChanged );
330 mPlacementActor.Reset();
332 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
333 mVisualSize = Vector2::ZERO;
334 mVisualScale = Vector2::ONE;
336 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffStage [%p]\n", this );
339 void AnimatedVectorImageVisual::OnSetTransform()
341 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
343 if( IsOnStage() && visualSize != mVisualSize )
345 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
347 mVisualSize = visualSize;
349 SetVectorImageSize();
351 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY && mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
353 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
354 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
361 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
363 // Check if action is valid for this visual type and perform action if possible
366 case DevelAnimatedVectorImageVisual::Action::PLAY:
368 if( IsOnStage() && mVisualSize != Vector2::ZERO )
370 if( mAnimationData.playState != DevelImageVisual::PlayState::PLAYING )
372 mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
373 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
376 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
379 case DevelAnimatedVectorImageVisual::Action::PAUSE:
381 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
383 mAnimationData.playState = DevelImageVisual::PlayState::PAUSED;
384 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
386 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
389 case DevelAnimatedVectorImageVisual::Action::STOP:
391 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
393 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
394 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
396 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
399 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
402 if( attributes.Get( frameNumber ) )
404 mAnimationData.currentFrame = frameNumber;
405 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_CURRENT_FRAME;
409 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
411 Property::Map* map = attributes.GetMap();
414 DoSetProperties( *map );
420 TriggerVectorRasterization();
423 void AnimatedVectorImageVisual::Process()
427 mRasterizationTriggered = false;
429 Adaptor::Get().UnregisterProcessor( *this );
432 void AnimatedVectorImageVisual::OnUploadCompleted()
434 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
435 Actor actor = mPlacementActor.GetHandle();
436 if( actor && !mRendererAdded )
438 actor.AddRenderer( mImpl->mRenderer );
439 mRendererAdded = true;
441 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
443 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
447 void AnimatedVectorImageVisual::OnAnimationFinished()
449 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mActionStatus, this );
451 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
453 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
455 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
457 if( mImpl->mEventObserver )
459 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
463 if( mImpl->mRenderer )
465 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
469 void AnimatedVectorImageVisual::SendAnimationData()
471 if( mAnimationData.resendFlag )
473 mVectorAnimationTask->SetAnimationData( mAnimationData );
475 if( mImpl->mRenderer )
477 if( mAnimationData.playState == DevelImageVisual::PlayState::PLAYING )
479 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
483 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
487 mAnimationData.resendFlag = 0;
491 void AnimatedVectorImageVisual::SetVectorImageSize()
493 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
494 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
496 mAnimationData.width = width;
497 mAnimationData.height = height;
498 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_SIZE;
501 void AnimatedVectorImageVisual::StopAnimation()
503 if( mAnimationData.playState != DevelImageVisual::PlayState::STOPPED )
505 mAnimationData.playState = DevelImageVisual::PlayState::STOPPED;
506 mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
508 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
512 void AnimatedVectorImageVisual::TriggerVectorRasterization()
514 if( !mRasterizationTriggered )
516 Stage::GetCurrent().KeepRendering( 1.0f ); // Trigger event processing
518 Adaptor::Get().RegisterProcessor( *this );
519 mRasterizationTriggered = true;
523 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
525 Actor actor = mPlacementActor.GetHandle();
528 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
529 mVisualScale.width = scale.width;
530 mVisualScale.height = scale.height;
532 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
534 SetVectorImageSize();
537 Stage::GetCurrent().KeepRendering( 1.0f ); // Trigger event processing
541 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
543 Actor actor = mPlacementActor.GetHandle();
546 Vector3 size = actor.GetCurrentSize();
547 mVisualSize.width = size.width;
548 mVisualSize.height = size.height;
550 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
552 SetVectorImageSize();
555 Stage::GetCurrent().KeepRendering( 1.0f ); // Trigger event processing
559 void AnimatedVectorImageVisual::OnControlVisibilityChanged( Actor actor, bool visible, DevelActor::VisibilityChange::Type type )
564 TriggerVectorRasterization();
566 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnControlVisibilityChanged: invisibile. Pause animation [%p]\n", this );
570 void AnimatedVectorImageVisual::OnWindowVisibilityChanged( Window window, bool visible )
575 TriggerVectorRasterization();
577 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnWindowVisibilityChanged: invisibile. Pause animation [%p]\n", this );
581 } // namespace Internal
583 } // namespace Toolkit