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/common/stage-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>
35 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
49 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
50 constexpr auto LOOP_FOREVER = -1;
52 } // unnamed namespace
54 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
56 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
57 visual->SetProperties( properties );
62 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
64 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
69 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
70 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
71 mImageVisualShaderFactory( shaderFactory ),
74 mPlayRange( 0.0f, 1.0f ),
76 mVectorRasterizeThread(),
77 mLoopCount( LOOP_FOREVER ),
78 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
80 // the rasterized image is with pre-multiplied alpha format
81 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
84 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
88 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
90 naturalSize = mVisualSize;
93 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
96 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
99 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
101 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast< int >( mLoopCount ) );
102 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, static_cast< Vector2 >( mPlayRange ) );
104 if( mVectorRasterizeThread )
106 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread->GetPlayState() ) );
110 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( DevelImageVisual::PlayState::STOPPED ) );
114 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
119 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
121 // url already passed in from constructor
122 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
124 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
125 if( keyValue.first.type == Property::Key::INDEX )
127 DoSetProperty( keyValue.first.indexKey, keyValue.second );
131 if( keyValue.first == LOOP_COUNT_NAME )
133 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
135 else if( keyValue.first == PLAY_RANGE_NAME )
137 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
143 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
147 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
150 if( value.Get( loopCount ) )
152 mLoopCount = loopCount;
153 if( mVectorRasterizeThread )
155 mVectorRasterizeThread->SetLoopCount( loopCount );
160 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
163 if( value.Get( range ) )
165 // Make sure the range specified is between 0.0 and 1.0
166 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
168 Vector2 orderedRange( range );
169 // If the range is not in order swap values
170 if( range.x > range.y )
172 orderedRange = Vector2( range.y, range.x );
175 mPlayRange = orderedRange;
177 if( mVectorRasterizeThread )
179 mVectorRasterizeThread->SetPlayRange( mPlayRange );
188 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
192 if( mImpl->mCustomShader )
194 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
195 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
196 mImpl->mCustomShader->mHints );
198 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
202 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
205 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
207 mImpl->mRenderer = Renderer::New( geometry, shader );
209 TextureSet textureSet = TextureSet::New();
210 mImpl->mRenderer.SetTextures( textureSet );
212 // Register transform properties
213 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
215 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
217 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
218 mPlacementActor = actor;
220 // This visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
221 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
224 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
226 if( mVectorRasterizeThread )
228 mVectorRasterizeThread->PauseAnimation();
229 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
230 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
233 if( mImpl->mRenderer )
235 actor.RemoveRenderer( mImpl->mRenderer );
236 mImpl->mRenderer.Reset();
239 mPlacementActor.Reset();
241 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
242 mVisualSize = Vector2::ZERO;
245 void AnimatedVectorImageVisual::OnSetTransform()
247 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
251 if( visualSize != mVisualSize )
253 mVisualSize = visualSize;
255 if( !mVectorRasterizeThread )
257 uint32_t width = static_cast< uint32_t >( visualSize.width );
258 uint32_t height = static_cast< uint32_t >( visualSize.height );
260 mVectorRasterizeThread = std::unique_ptr< VectorRasterizeThread >( new VectorRasterizeThread( mUrl.GetUrl(), mImpl->mRenderer, width, height ) );
262 mVectorRasterizeThread->SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
263 mVectorRasterizeThread->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
264 mVectorRasterizeThread->SetLoopCount( mLoopCount );
265 mVectorRasterizeThread->SetPlayRange( mPlayRange );
267 mVectorRasterizeThread->Start();
269 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
271 mVectorRasterizeThread->StartAnimation();
272 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY );
277 mVectorRasterizeThread->RenderFrame();
282 uint32_t width = static_cast< uint32_t >( visualSize.width );
283 uint32_t height = static_cast< uint32_t >( visualSize.height );
285 mVectorRasterizeThread->SetSize( width, height );
291 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
293 // Check if action is valid for this visual type and perform action if possible
296 case DevelAnimatedVectorImageVisual::Action::PLAY:
300 if( mVectorRasterizeThread )
302 mVectorRasterizeThread->StartAnimation();
303 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY ); //TODO: Should manage this globally
306 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
309 case DevelAnimatedVectorImageVisual::Action::PAUSE:
311 if( mVectorRasterizeThread )
313 mVectorRasterizeThread->PauseAnimation();
314 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
316 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
319 case DevelAnimatedVectorImageVisual::Action::STOP:
321 if( mVectorRasterizeThread )
323 bool emitSignal = false;
324 if( mVectorRasterizeThread->GetPlayState() != DevelImageVisual::PlayState::STOPPED )
329 mVectorRasterizeThread->StopAnimation();
330 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
334 OnAnimationFinished();
337 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
343 void AnimatedVectorImageVisual::OnResourceReady()
345 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
346 Actor actor = mPlacementActor.GetHandle();
349 actor.AddRenderer( mImpl->mRenderer );
350 // reset the weak handle so that the renderer only get added to actor once
351 mPlacementActor.Reset();
353 Stage::GetCurrent().KeepRendering( 0.0f );
357 void AnimatedVectorImageVisual::OnAnimationFinished()
359 if( mImpl->mEventObserver )
361 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
365 } // namespace Internal
367 } // namespace Toolkit