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;
246 Scripting::GetEnumerationProperty( value, STOP_BEHAVIOR_TABLE, STOP_BEHAVIOR_TABLE_COUNT, stopBehavior );
247 mStopBehavior = DevelImageVisual::StopBehavior::Type( stopBehavior );
248 mResendFlag |= RESEND_STOP_BEHAVIOR;
251 case Toolkit::DevelImageVisual::Property::LOOPING_MODE:
254 Scripting::GetEnumerationProperty( value, LOOPING_MODE_TABLE, LOOPING_MODE_TABLE_COUNT, loopingMode );
255 mLoopingMode = DevelImageVisual::LoopingMode::Type( loopingMode );
256 mResendFlag |= RESEND_LOOPING_MODE;
262 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
266 if( mImpl->mCustomShader )
268 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
269 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
270 mImpl->mCustomShader->mHints );
272 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
276 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
279 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
281 mImpl->mRenderer = Renderer::New( geometry, shader );
283 TextureSet textureSet = TextureSet::New();
284 mImpl->mRenderer.SetTextures( textureSet );
286 // Register transform properties
287 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
289 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
291 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
292 mPlacementActor = actor;
294 mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
296 // Add property notification for scaling & size
297 mScaleNotification = actor.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
298 mScaleNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnScaleNotification );
300 mSizeNotification = actor.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 3.0f ) );
301 mSizeNotification.NotifySignal().Connect( this, &AnimatedVectorImageVisual::OnSizeNotification );
303 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOnStage [%p]\n", this );
306 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
308 mVectorRasterizeThread.PauseAnimation();
310 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
312 if( mImpl->mRenderer )
314 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
316 actor.RemoveRenderer( mImpl->mRenderer );
317 mImpl->mRenderer.Reset();
319 mRendererAdded = false;
322 // Remove property notification
323 actor.RemovePropertyNotification( mScaleNotification );
324 actor.RemovePropertyNotification( mSizeNotification );
326 mPlacementActor.Reset();
328 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
329 mVisualSize = Vector2::ZERO;
330 mVisualScale = Vector2::ONE;
332 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::DoSetOffStage [%p]\n", this );
335 void AnimatedVectorImageVisual::OnSetTransform()
337 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
339 if( IsOnStage() && visualSize != mVisualSize )
341 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f [%p]\n", visualSize.width, visualSize.height, this );
343 mVisualSize = visualSize;
345 SetVectorImageSize();
349 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
351 mVectorRasterizeThread.PlayAnimation();
353 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
358 mVectorRasterizeThread.RenderFrame();
363 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
365 // Check if action is valid for this visual type and perform action if possible
368 case DevelAnimatedVectorImageVisual::Action::PLAY:
370 if( IsOnStage() && mVisualSize != Vector2::ZERO )
372 mVectorRasterizeThread.PlayAnimation();
374 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
376 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
379 case DevelAnimatedVectorImageVisual::Action::PAUSE:
381 mVectorRasterizeThread.PauseAnimation();
383 if( mImpl->mRenderer )
385 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
388 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
391 case DevelAnimatedVectorImageVisual::Action::STOP:
393 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
395 mVectorRasterizeThread.StopAnimation();
397 OnAnimationFinished();
399 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
402 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
405 if( attributes.Get( frameNumber ) )
407 mVectorRasterizeThread.SetCurrentFrameNumber( frameNumber );
409 if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
411 mVectorRasterizeThread.RenderFrame();
412 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
417 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
419 Property::Map* map = attributes.GetMap();
422 DoSetProperties( *map );
431 void AnimatedVectorImageVisual::OnUploadCompleted()
433 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
434 Actor actor = mPlacementActor.GetHandle();
435 if( actor && !mRendererAdded )
437 actor.AddRenderer( mImpl->mRenderer );
438 mRendererAdded = true;
441 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
443 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnUploadCompleted: Renderer is added [%p]\n", this );
446 void AnimatedVectorImageVisual::OnAnimationFinished()
448 if( mImpl->mEventObserver )
450 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
453 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
455 if( mImpl->mRenderer )
457 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
461 void AnimatedVectorImageVisual::SendAnimationData()
465 bool isPlaying = false;
466 if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
468 mVectorRasterizeThread.PauseAnimation();
472 if( mResendFlag & RESEND_LOOP_COUNT )
474 mVectorRasterizeThread.SetLoopCount( mLoopCount );
477 if( mResendFlag & RESEND_PLAY_RANGE )
479 mVectorRasterizeThread.SetPlayRange( mStartFrame, mEndFrame );
482 if( mResendFlag & RESEND_STOP_BEHAVIOR )
484 mVectorRasterizeThread.SetStopBehavior( mStopBehavior );
487 if( mResendFlag & RESEND_LOOPING_MODE )
489 mVectorRasterizeThread.SetLoopingMode( mLoopingMode );
496 mVectorRasterizeThread.PlayAnimation();
500 mVectorRasterizeThread.RenderFrame();
501 Stage::GetCurrent().KeepRendering( 0.0f );
509 void AnimatedVectorImageVisual::SetVectorImageSize()
511 uint32_t width = static_cast< uint32_t >( mVisualSize.width * mVisualScale.width );
512 uint32_t height = static_cast< uint32_t >( mVisualSize.height * mVisualScale.height );
514 mVectorRasterizeThread.SetSize( width, height );
516 if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
518 mVectorRasterizeThread.RenderFrame();
519 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
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();
538 void AnimatedVectorImageVisual::OnSizeNotification( PropertyNotification& source )
540 Actor actor = mPlacementActor.GetHandle();
543 Vector3 size = actor.GetCurrentSize();
544 mVisualSize.width = size.width;
545 mVisualSize.height = size.height;
547 DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSizeNotification: size = %f, %f [%p]\n", mVisualSize.width, mVisualSize.height, this );
549 SetVectorImageSize();
553 } // namespace Internal
555 } // namespace Toolkit