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/integration-api/debug.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
31 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
48 constexpr auto LOOP_FOREVER = -1;
50 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
52 // Flags for re-sending data to the rasterize thread
55 RESEND_PLAY_RANGE = 1 << 0,
56 RESEND_LOOP_COUNT = 1 << 1,
57 RESEND_STOP_BEHAVIOR = 1 << 2,
58 RESEND_LOOPING_MODE = 1 << 3
62 DALI_ENUM_TO_STRING_TABLE_BEGIN( STOP_BEHAVIOR )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, CURRENT_FRAME )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, FIRST_FRAME )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::StopBehavior, LAST_FRAME )
66 DALI_ENUM_TO_STRING_TABLE_END( STOP_BEHAVIOR )
69 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOOPING_MODE )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, RESTART )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::DevelImageVisual::LoopingMode, AUTO_REVERSE )
72 DALI_ENUM_TO_STRING_TABLE_END( LOOPING_MODE )
74 #if defined(DEBUG_ENABLED)
75 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
78 } // unnamed namespace
80 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
82 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
83 visual->SetProperties( properties );
88 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
90 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
95 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
96 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
97 mImageVisualShaderFactory( shaderFactory ),
99 mVectorRasterizeThread( imageUrl.GetUrl() ),
101 mVisualScale( Vector2::ONE ),
103 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 mVectorRasterizeThread.UploadCompletedSignal().Connect( this, &AnimatedVectorImageVisual::OnUploadCompleted );
116 mVectorRasterizeThread.SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
118 mVectorRasterizeThread.Start();
121 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
125 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
127 if( mVisualSize != Vector2::ZERO )
129 naturalSize = mVisualSize;
133 uint32_t width, height;
134 mVectorRasterizeThread.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 Property::Array playRange;
153 playRange.PushBack( mStartFrame );
154 playRange.PushBack( mEndFrame );
155 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, playRange );
157 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread.GetPlayState() ) );
158 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_FRAME_NUMBER, static_cast< int32_t >( mVectorRasterizeThread.GetCurrentFrameNumber() ) );
159 map.Insert( Toolkit::DevelImageVisual::Property::TOTAL_FRAME_NUMBER, static_cast< int32_t >( mVectorRasterizeThread.GetTotalFrameNumber() ) );
161 map.Insert( Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR, mStopBehavior );
162 map.Insert( Toolkit::DevelImageVisual::Property::LOOPING_MODE, mLoopingMode );
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 );
202 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
206 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
209 if( value.Get( loopCount ) )
211 mLoopCount = loopCount;
212 mResendFlag |= RESEND_LOOP_COUNT;
216 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
218 Property::Array* array = value.GetArray();
221 size_t count = array->Count();
224 int startFrame, endFrame;
225 int totalFrame = mVectorRasterizeThread.GetTotalFrameNumber();
226 array->GetElementAt( 0 ).Get( startFrame );
227 array->GetElementAt( 1 ).Get( endFrame );
229 if( startFrame >= 0 && startFrame < totalFrame && endFrame >= 0 && endFrame < totalFrame )
231 mStartFrame = startFrame;
232 mEndFrame = endFrame;
233 mResendFlag |= RESEND_PLAY_RANGE;
237 DALI_LOG_ERROR( "Invalid play range [%d, %d / %d]\n", startFrame, endFrame, totalFrame );
243 case Toolkit::DevelImageVisual::Property::STOP_BEHAVIOR:
245 int32_t stopBehavior = mStopBehavior;
246 if( Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior ) )
248 mStopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
249 mResendFlag |= RESEND_STOP_BEHAVIOR;
253 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
255 int32_t loopingMode = mLoopingMode;
256 if( Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode ) )
258 mLoopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
259 mResendFlag |= RESEND_LOOPING_MODE;
266 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
270 if( mImpl->mCustomShader )
272 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
273 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
274 mImpl->mCustomShader->mHints );
276 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
280 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
283 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
285 mImpl->mRenderer = Renderer::New( geometry, shader );
287 TextureSet textureSet = TextureSet::New();
288 mImpl->mRenderer.SetTextures( textureSet );
290 // Register transform properties
291 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
293 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
295 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
296 mPlacementActor = actor;
298 mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
300 // Add property notification for scaling & size
301 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
302 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
304 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
305 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
307 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
310 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
312 mVectorRasterizeThread.PauseAnimation();
314 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
316 if( mImpl->mRenderer )
318 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
320 actor.RemoveRenderer( mImpl->mRenderer );
321 mImpl->mRenderer.Reset();
323 mRendererAdded = false;
326 // Remove property notification
327 actor.RemovePropertyNotification( mScaleNotification );
328 actor.RemovePropertyNotification( mSizeNotification );
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();
353 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
355 mVectorRasterizeThread.PlayAnimation();
357 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
362 mVectorRasterizeThread.RenderFrame();
367 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
369 // Check if action is valid for this visual type and perform action if possible
372 case DevelAnimatedVectorImageVisual::Action::PLAY:
374 if( IsOnStage() && mVisualSize != Vector2::ZERO )
376 mVectorRasterizeThread.PlayAnimation();
378 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
380 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
383 case DevelAnimatedVectorImageVisual::Action::PAUSE:
385 mVectorRasterizeThread.PauseAnimation();
387 if( mImpl->mRenderer )
389 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
392 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
395 case DevelAnimatedVectorImageVisual::Action::STOP:
397 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
399 mVectorRasterizeThread.StopAnimation();
402 if( mImpl->mRenderer )
404 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
407 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
410 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
413 if( attributes.Get( frameNumber ) )
415 mVectorRasterizeThread.SetCurrentFrameNumber( frameNumber );
417 if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
419 mVectorRasterizeThread.RenderFrame();
420 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
425 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
427 Property::Map* map = attributes.GetMap();
430 DoSetProperties( *map );
439 void AnimatedVectorImageVisual::OnUploadCompleted()
441 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
442 Actor actor = mPlacementActor.GetHandle();
443 if( actor && !mRendererAdded )
445 actor.AddRenderer( mImpl->mRenderer );
446 mRendererAdded = true;
449 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
451 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
454 void AnimatedVectorImageVisual::OnAnimationFinished()
456 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mActionStatus, this );
458 if( mActionStatus != DevelAnimatedVectorImageVisual::Action::STOP )
460 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
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()
478 bool isPlaying = false;
479 if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
481 mVectorRasterizeThread.PauseAnimation();
485 if( mResendFlag & RESEND_LOOP_COUNT )
487 mVectorRasterizeThread.SetLoopCount( mLoopCount );
490 if( mResendFlag & RESEND_PLAY_RANGE )
492 mVectorRasterizeThread.SetPlayRange( mStartFrame, mEndFrame );
495 if( mResendFlag & RESEND_STOP_BEHAVIOR )
497 mVectorRasterizeThread.SetStopBehavior( mStopBehavior );
500 if( mResendFlag & RESEND_LOOPING_MODE )
502 mVectorRasterizeThread.SetLoopingMode( mLoopingMode );
509 mVectorRasterizeThread.PlayAnimation();
513 mVectorRasterizeThread.RenderFrame();
514 Stage::GetCurrent().KeepRendering( 0.0f );
522 void AnimatedVectorImageVisual::SetVectorImageSize()
524 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
525 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
527 mVectorRasterizeThread.SetSize( width, height );
529 if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
531 mVectorRasterizeThread.RenderFrame();
532 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
536 void AnimatedVectorImageVisual::OnScaleNotification( PropertyNotification& source )
538 Actor actor = mPlacementActor.GetHandle();
541 Vector3 scale = actor.GetProperty< Vector3 >( Actor::Property::WORLD_SCALE );
542 mVisualScale.width = scale.width;
543 mVisualScale.height = scale.height;
545 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnScaleNotification: scale = %f, %f [%p]\n", mVisualScale.width, mVisualScale.height, this );
547 SetVectorImageSize();
551 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
553 Actor actor = mPlacementActor.GetHandle();
556 Vector3 size = actor.GetCurrentSize();
557 mVisualSize.width = size.width;
558 mVisualSize.height = size.height;
560 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
562 SetVectorImageSize();
566 } // namespace Internal
568 } // namespace Toolkit