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/internal/visuals/image-visual-shader-factory.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
48 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
49 constexpr auto LOOP_FOREVER = -1;
51 } // unnamed namespace
53 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
55 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
56 visual->SetProperties( properties );
61 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
63 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
68 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
69 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
70 mImageVisualShaderFactory( shaderFactory ),
73 mPlayRange( 0.0f, 1.0f ),
75 mVectorRasterizeThread(),
76 mLoopCount( LOOP_FOREVER ),
77 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
79 // the rasterized image is with pre-multiplied alpha format
80 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
83 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
87 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
89 naturalSize = mVisualSize;
92 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
95 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
98 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
100 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast< int >( mLoopCount ) );
101 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, static_cast< Vector2 >( mPlayRange ) );
104 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
109 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
111 // url already passed in from constructor
112 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
114 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
115 if( keyValue.first.type == Property::Key::INDEX )
117 DoSetProperty( keyValue.first.indexKey, keyValue.second );
121 if( keyValue.first == LOOP_COUNT_NAME )
123 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
125 else if( keyValue.first == PLAY_RANGE_NAME )
127 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
133 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
137 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
140 if( value.Get( loopCount ) )
142 mLoopCount = loopCount;
143 if( mVectorRasterizeThread )
145 mVectorRasterizeThread->SetLoopCount( loopCount );
150 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
153 if( value.Get( range ) )
155 // Make sure the range specified is between 0.0 and 1.0
156 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
158 Vector2 orderedRange( range );
159 // If the range is not in order swap values
160 if( range.x > range.y )
162 orderedRange = Vector2( range.y, range.x );
165 mPlayRange = orderedRange;
167 if( mVectorRasterizeThread )
169 mVectorRasterizeThread->SetPlayRange( mPlayRange );
178 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
182 if( mImpl->mCustomShader )
184 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
185 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
186 mImpl->mCustomShader->mHints );
188 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
192 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
195 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
197 mImpl->mRenderer = Renderer::New( geometry, shader );
199 TextureSet textureSet = TextureSet::New();
200 mImpl->mRenderer.SetTextures( textureSet );
202 // Register transform properties
203 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
205 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
207 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
208 mPlacementActor = actor;
210 // This visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
211 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
214 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
216 if( mVectorRasterizeThread )
218 mVectorRasterizeThread->PauseAnimation();
219 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
220 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
223 if( mImpl->mRenderer )
225 actor.RemoveRenderer( mImpl->mRenderer );
226 mImpl->mRenderer.Reset();
229 mPlacementActor.Reset();
231 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
232 mVisualSize = Vector2::ZERO;
235 void AnimatedVectorImageVisual::OnSetTransform()
237 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
241 if( visualSize != mVisualSize )
243 mVisualSize = visualSize;
245 if( !mVectorRasterizeThread )
247 uint32_t width = static_cast< uint32_t >( visualSize.width );
248 uint32_t height = static_cast< uint32_t >( visualSize.height );
250 mVectorRasterizeThread = std::unique_ptr< VectorRasterizeThread >( new VectorRasterizeThread( mUrl.GetUrl(), mImpl->mRenderer, width, height ) );
252 mVectorRasterizeThread->SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
253 mVectorRasterizeThread->SetLoopCount( mLoopCount );
254 mVectorRasterizeThread->SetPlayRange( mPlayRange );
256 mVectorRasterizeThread->Start();
258 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
260 mVectorRasterizeThread->StartAnimation();
261 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY );
266 mVectorRasterizeThread->RenderFrame();
271 uint32_t width = static_cast< uint32_t >( visualSize.width );
272 uint32_t height = static_cast< uint32_t >( visualSize.height );
274 mVectorRasterizeThread->SetSize( width, height );
280 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
282 if( actionId == mActionStatus )
287 // Check if action is valid for this visual type and perform action if possible
290 case DevelAnimatedVectorImageVisual::Action::PLAY:
294 if( mVectorRasterizeThread )
296 mVectorRasterizeThread->StartAnimation();
297 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY ); //TODO: Should manage this globally
300 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
303 case DevelAnimatedVectorImageVisual::Action::PAUSE:
305 if( mVectorRasterizeThread )
307 mVectorRasterizeThread->PauseAnimation();
308 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
310 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
313 case DevelAnimatedVectorImageVisual::Action::RESUME:
315 if( mVectorRasterizeThread )
317 mVectorRasterizeThread->ResumeAnimation();
318 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY );
320 mActionStatus = DevelAnimatedVectorImageVisual::Action::RESUME;
323 case DevelAnimatedVectorImageVisual::Action::STOP:
325 if( mVectorRasterizeThread )
327 mVectorRasterizeThread->StopAnimation();
328 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
330 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
336 void AnimatedVectorImageVisual::OnResourceReady()
338 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
339 Actor actor = mPlacementActor.GetHandle();
342 actor.AddRenderer( mImpl->mRenderer );
343 // reset the weak handle so that the renderer only get added to actor once
344 mPlacementActor.Reset();
346 Stage::GetCurrent().KeepRendering( 0.0f );
350 } // namespace Internal
352 } // namespace Toolkit