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/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>
49 constexpr auto LOOP_FOREVER = -1;
51 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
53 // Flags for re-sending data to the rasterize thread
56 RESEND_PLAY_RANGE = 1 << 0,
57 RESEND_LOOP_COUNT = 1 << 1
60 } // unnamed namespace
62 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
64 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
65 visual->SetProperties( properties );
70 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
72 AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
77 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
78 : Visual::Base( factoryCache ),
79 mImageVisualShaderFactory( shaderFactory ),
81 mVectorRasterizeThread( imageUrl.GetUrl() ),
83 mPlayRange( 0.0f, 1.0f ),
85 mLoopCount( LOOP_FOREVER ),
87 mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
89 // the rasterized image is with pre-multiplied alpha format
90 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
92 mVectorRasterizeThread.SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
93 mVectorRasterizeThread.SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
95 mVectorRasterizeThread.Start();
98 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
102 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
104 if( mImpl->mRenderer ) // Check if we have a rendered image
106 auto textureSet = mImpl->mRenderer.GetTextures();
109 if( textureSet.GetTextureCount() > 0 )
111 auto texture = textureSet.GetTexture( 0 );
112 naturalSize.x = texture.GetWidth();
113 naturalSize.y = texture.GetHeight();
119 uint32_t width, height;
120 mVectorRasterizeThread.GetDefaultSize( width, height );
121 naturalSize.x = width;
122 naturalSize.y = height;
125 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
128 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
131 map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
133 map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
134 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, mPlayRange );
135 map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread.GetPlayState() ) );
136 map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_PROGRESS, mVectorRasterizeThread.GetCurrentProgress() );
139 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
144 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
146 // url already passed in from constructor
147 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
149 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
150 if( keyValue.first.type == Property::Key::INDEX )
152 DoSetProperty( keyValue.first.indexKey, keyValue.second );
156 if( keyValue.first == LOOP_COUNT_NAME )
158 DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
160 else if( keyValue.first == PLAY_RANGE_NAME )
162 DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
168 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
172 case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
175 if( value.Get( loopCount ) )
177 mLoopCount = loopCount;
178 mResendFlag |= RESEND_LOOP_COUNT;
182 case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
185 if( value.Get( range ) )
188 mResendFlag |= RESEND_PLAY_RANGE;
195 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
199 if( mImpl->mCustomShader )
201 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
202 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
203 mImpl->mCustomShader->mHints );
205 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
209 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
212 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
214 mImpl->mRenderer = Renderer::New( geometry, shader );
216 TextureSet textureSet = TextureSet::New();
217 mImpl->mRenderer.SetTextures( textureSet );
219 // Register transform properties
220 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
222 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
224 // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
225 mPlacementActor = actor;
227 mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
230 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
232 mVectorRasterizeThread.PauseAnimation();
234 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
236 if( mImpl->mRenderer )
238 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
240 actor.RemoveRenderer( mImpl->mRenderer );
241 mImpl->mRenderer.Reset();
244 mPlacementActor.Reset();
246 // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
247 mVisualSize = Vector2::ZERO;
250 void AnimatedVectorImageVisual::OnSetTransform()
252 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
256 if( visualSize != mVisualSize )
258 mVisualSize = visualSize;
260 uint32_t width = static_cast< uint32_t >( visualSize.width );
261 uint32_t height = static_cast< uint32_t >( visualSize.height );
263 mVectorRasterizeThread.SetSize( width, height );
268 if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
270 mVectorRasterizeThread.PlayAnimation();
272 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
277 mVectorRasterizeThread.RenderFrame();
280 if( mVectorRasterizeThread.IsResourceReady() )
282 Actor actor = mPlacementActor.GetHandle();
285 actor.AddRenderer( mImpl->mRenderer );
286 mPlacementActor.Reset();
289 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
294 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
296 // Check if action is valid for this visual type and perform action if possible
299 case DevelAnimatedVectorImageVisual::Action::PLAY:
301 if( IsOnStage() && mVisualSize != Vector2::ZERO )
303 mVectorRasterizeThread.PlayAnimation();
305 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
307 mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
310 case DevelAnimatedVectorImageVisual::Action::PAUSE:
312 mVectorRasterizeThread.PauseAnimation();
314 if( mImpl->mRenderer )
316 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
319 mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
322 case DevelAnimatedVectorImageVisual::Action::STOP:
324 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
326 mVectorRasterizeThread.StopAnimation();
328 OnAnimationFinished();
330 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
333 case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
336 if( attributes.Get( progress ) )
338 mVectorRasterizeThread.SetCurrentProgress( progress );
340 if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
342 mVectorRasterizeThread.RenderFrame();
343 Stage::GetCurrent().KeepRendering( 0.0f ); // Trigger rendering
348 case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
350 Property::Map* map = attributes.GetMap();
353 DoSetProperties( *map );
362 void AnimatedVectorImageVisual::OnResourceReady()
364 // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
365 Actor actor = mPlacementActor.GetHandle();
368 actor.AddRenderer( mImpl->mRenderer );
369 // reset the weak handle so that the renderer only get added to actor once
370 mPlacementActor.Reset();
372 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
376 void AnimatedVectorImageVisual::OnAnimationFinished()
378 if( mImpl->mEventObserver )
380 mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
383 mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
385 if( mImpl->mRenderer )
387 mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
391 void AnimatedVectorImageVisual::SendAnimationData()
395 bool isPlaying = false;
396 if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
398 mVectorRasterizeThread.PauseAnimation();
402 if( mResendFlag & RESEND_LOOP_COUNT )
404 mVectorRasterizeThread.SetLoopCount( mLoopCount );
407 if( mResendFlag & RESEND_PLAY_RANGE )
409 mVectorRasterizeThread.SetPlayRange( mPlayRange );
414 mVectorRasterizeThread.PlayAnimation();
418 mVectorRasterizeThread.RenderFrame();
419 Stage::GetCurrent().KeepRendering( 0.0f );
426 } // namespace Internal
428 } // namespace Toolkit