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/visual-properties-devel.h>
30 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
32 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
36 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
50 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
51 constexpr auto LOOP_FOREVER = -1;
53 } // unnamed namespace
55 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
57 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
58 visual->SetProperties( properties );
63 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
65 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
70 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
71 : Visual::Base( factoryCache ),
72 mImageVisualShaderFactory( shaderFactory ),
75 mPlayRange( 0.0f, 1.0f ),
77 mVectorRasterizeThread(),
78 mLoopCount( LOOP_FOREVER ),
79 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
81 // the rasterized image is with pre-multiplied alpha format
82 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
85 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
89 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
91 naturalSize = mVisualSize;
94 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
97 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
100 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
102 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, static_cast< int >( mLoopCount ) );
103 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, static_cast< Vector2 >( mPlayRange ) );
105 if( mVectorRasterizeThread )
107 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread->GetPlayState() ) );
111 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( DevelImageVisual::PlayState::STOPPED ) );
115 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
120 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
122 // url already passed in from constructor
123 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
125 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
126 if( keyValue.first.type == Property::Key::INDEX )
128 DoSetProperty( keyValue.first.indexKey, keyValue.second );
132 if( keyValue.first == LOOP_COUNT_NAME )
134 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
136 else if( keyValue.first == PLAY_RANGE_NAME )
138 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
144 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
148 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
151 if( value.Get( loopCount ) )
153 mLoopCount = loopCount;
154 if( mVectorRasterizeThread )
156 mVectorRasterizeThread->SetLoopCount( loopCount );
161 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
164 if( value.Get( range ) )
166 // Make sure the range specified is between 0.0 and 1.0
167 if( range.x >= 0.0f && range.x <= 1.0f && range.y >= 0.0f && range.y <= 1.0f )
169 Vector2 orderedRange( range );
170 // If the range is not in order swap values
171 if( range.x > range.y )
173 orderedRange = Vector2( range.y, range.x );
176 mPlayRange = orderedRange;
178 if( mVectorRasterizeThread )
180 mVectorRasterizeThread->SetPlayRange( mPlayRange );
189 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
193 if( mImpl->mCustomShader )
195 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
196 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
197 mImpl->mCustomShader->mHints );
199 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
203 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
206 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
208 mImpl->mRenderer = Renderer::New( geometry, shader );
210 TextureSet textureSet = TextureSet::New();
211 mImpl->mRenderer.SetTextures( textureSet );
213 // Register transform properties
214 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
216 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
218 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
219 mPlacementActor = actor;
221 // This visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
222 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
225 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
227 if( mVectorRasterizeThread )
229 mVectorRasterizeThread->PauseAnimation();
230 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
231 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
234 if( mImpl->mRenderer )
236 actor.RemoveRenderer( mImpl->mRenderer );
237 mImpl->mRenderer.Reset();
240 mPlacementActor.Reset();
242 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
243 mVisualSize = Vector2::ZERO;
246 void AnimatedVectorImageVisual::OnSetTransform()
248 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
252 if( visualSize != mVisualSize )
254 mVisualSize = visualSize;
256 if( !mVectorRasterizeThread )
258 uint32_t width = static_cast< uint32_t >( visualSize.width );
259 uint32_t height = static_cast< uint32_t >( visualSize.height );
261 mVectorRasterizeThread = std::unique_ptr< VectorRasterizeThread >( new VectorRasterizeThread( mUrl.GetUrl(), mImpl->mRenderer, width, height ) );
263 mVectorRasterizeThread->SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
264 mVectorRasterizeThread->SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
265 mVectorRasterizeThread->SetLoopCount( mLoopCount );
266 mVectorRasterizeThread->SetPlayRange( mPlayRange );
268 mVectorRasterizeThread->Start();
270 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
272 mVectorRasterizeThread->StartAnimation();
273 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY );
278 mVectorRasterizeThread->RenderFrame();
283 uint32_t width = static_cast< uint32_t >( visualSize.width );
284 uint32_t height = static_cast< uint32_t >( visualSize.height );
286 mVectorRasterizeThread->SetSize( width, height );
292 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
294 // Check if action is valid for this visual type and perform action if possible
297 case DevelAnimatedVectorImageVisual::Action::PLAY:
301 if( mVectorRasterizeThread )
303 mVectorRasterizeThread->StartAnimation();
304 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::CONTINUOUSLY ); //TODO: Should manage this globally
307 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
310 case DevelAnimatedVectorImageVisual::Action::PAUSE:
312 if( mVectorRasterizeThread )
314 mVectorRasterizeThread->PauseAnimation();
315 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
317 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
320 case DevelAnimatedVectorImageVisual::Action::STOP:
322 if( mVectorRasterizeThread )
324 bool emitSignal = false;
325 if( mVectorRasterizeThread->GetPlayState() != DevelImageVisual::PlayState::STOPPED )
330 mVectorRasterizeThread->StopAnimation();
331 DevelStage::SetRenderingBehavior( Stage::GetCurrent(), DevelStage::Rendering::IF_REQUIRED );
335 OnAnimationFinished();
338 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
344 void AnimatedVectorImageVisual::OnResourceReady()
346 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
347 Actor actor = mPlacementActor.GetHandle();
350 actor.AddRenderer( mImpl->mRenderer );
351 // reset the weak handle so that the renderer only get added to actor once
352 mPlacementActor.Reset();
354 Stage::GetCurrent().KeepRendering( 0.0f );
358 void AnimatedVectorImageVisual::OnAnimationFinished()
360 if( mImpl->mEventObserver )
362 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
366 } // namespace Internal
368 } // namespace Toolkit