2 * Copyright (c) 2018 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
59 } // unnamed namespace
61 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
63 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
64 visual->SetProperties( properties );
69 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
71 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
76 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
77 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
78 mImageVisualShaderFactory( shaderFactory ),
80 mVectorRasterizeThread( imageUrl.GetUrl() ),
82 mPlayRange( 0.0f, 1.0f ),
84 mLoopCount( LOOP_FOREVER ),
86 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
88 // the rasterized image is with pre-multiplied alpha format
89 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
91 mVectorRasterizeThread.SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
92 mVectorRasterizeThread.SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
94 mVectorRasterizeThread.Start();
97 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
101 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
103 if( mImpl->mRenderer ) // Check if we have a rendered image
105 auto textureSet = mImpl->mRenderer.GetTextures();
108 if( textureSet.GetTextureCount() > 0 )
110 auto texture = textureSet.GetTexture( 0 );
111 naturalSize.x = texture.GetWidth();
112 naturalSize.y = texture.GetHeight();
118 uint32_t width, height;
119 mVectorRasterizeThread.GetDefaultSize( width, height );
120 naturalSize.x = width;
121 naturalSize.y = height;
124 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
127 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
130 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
132 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
133 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, mPlayRange );
134 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread.GetPlayState() ) );
135 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_PROGRESS, mVectorRasterizeThread.GetCurrentProgress() );
138 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
143 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
145 // url already passed in from constructor
146 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
148 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
149 if( keyValue.first.type == Property::Key::INDEX )
151 DoSetProperty( keyValue.first.indexKey, keyValue.second );
155 if( keyValue.first == LOOP_COUNT_NAME )
157 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
159 else if( keyValue.first == PLAY_RANGE_NAME )
161 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
167 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
171 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
174 if( value.Get( loopCount ) )
176 mLoopCount = loopCount;
177 mResendFlag |= RESEND_LOOP_COUNT;
181 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
184 if( value.Get( range ) )
187 mResendFlag |= RESEND_PLAY_RANGE;
194 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
198 if( mImpl->mCustomShader )
200 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
201 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
202 mImpl->mCustomShader->mHints );
204 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
208 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
211 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
213 mImpl->mRenderer = Renderer::New( geometry, shader );
215 TextureSet textureSet = TextureSet::New();
216 mImpl->mRenderer.SetTextures( textureSet );
218 // Register transform properties
219 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
221 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
223 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
224 mPlacementActor = actor;
226 mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
229 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
231 mVectorRasterizeThread.PauseAnimation();
233 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
235 if( mImpl->mRenderer )
237 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
239 actor.RemoveRenderer( mImpl->mRenderer );
240 mImpl->mRenderer.Reset();
243 mPlacementActor.Reset();
245 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
246 mVisualSize = Vector2::ZERO;
249 void AnimatedVectorImageVisual::OnSetTransform()
251 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
255 if( visualSize != mVisualSize )
257 mVisualSize = visualSize;
259 uint32_t width = static_cast< uint32_t >( visualSize.width );
260 uint32_t height = static_cast< uint32_t >( visualSize.height );
262 mVectorRasterizeThread.SetSize( width, height );
267 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
269 mVectorRasterizeThread.PlayAnimation();
271 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
276 mVectorRasterizeThread.RenderFrame();
279 if( mVectorRasterizeThread.IsResourceReady() )
281 Actor actor = mPlacementActor.GetHandle();
284 actor.AddRenderer( mImpl->mRenderer );
285 mPlacementActor.Reset();
288 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
293 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
295 // Check if action is valid for this visual type and perform action if possible
298 case DevelAnimatedVectorImageVisual::Action::PLAY:
300 if( IsOnStage() && mVisualSize != Vector2::ZERO )
302 mVectorRasterizeThread.PlayAnimation();
304 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
306 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
309 case DevelAnimatedVectorImageVisual::Action::PAUSE:
311 mVectorRasterizeThread.PauseAnimation();
313 if( mImpl->mRenderer )
315 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
318 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
321 case DevelAnimatedVectorImageVisual::Action::STOP:
323 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
325 mVectorRasterizeThread.StopAnimation();
327 OnAnimationFinished();
329 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
332 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
335 if( attributes.Get( progress ) )
337 mVectorRasterizeThread.SetCurrentProgress( progress );
339 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
341 mVectorRasterizeThread.RenderFrame();
342 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
347 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
349 Property::Map* map = attributes.GetMap();
352 DoSetProperties( *map );
361 void AnimatedVectorImageVisual::OnResourceReady()
363 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
364 Actor actor = mPlacementActor.GetHandle();
367 actor.AddRenderer( mImpl->mRenderer );
368 // reset the weak handle so that the renderer only get added to actor once
369 mPlacementActor.Reset();
371 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
375 void AnimatedVectorImageVisual::OnAnimationFinished()
377 if( mImpl->mEventObserver )
379 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
382 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
384 if( mImpl->mRenderer )
386 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
390 void AnimatedVectorImageVisual::SendAnimationData()
394 bool isPlaying = false;
395 if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
397 mVectorRasterizeThread.PauseAnimation();
401 if( mResendFlag & RESEND_LOOP_COUNT )
403 mVectorRasterizeThread.SetLoopCount( mLoopCount );
406 if( mResendFlag & RESEND_PLAY_RANGE )
408 mVectorRasterizeThread.SetPlayRange( mPlayRange );
413 mVectorRasterizeThread.PlayAnimation();
417 mVectorRasterizeThread.RenderFrame();
418 Stage::GetCurrent().KeepRendering( 0.0f );
425 } // namespace Internal
427 } // namespace Toolkit