DALi Version 1.4.25
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / animated-vector-image-visual.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/animated-vector-image/animated-vector-image-visual.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/stage.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
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
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 constexpr auto LOOP_FOREVER = -1;
49
50 const Dali::Vector4 FULL_TEXTURE_RECT( 0.f, 0.f, 1.f, 1.f );
51
52 // Flags for re-sending data to the rasterize thread
53 enum Flags
54 {
55   RESEND_PLAY_RANGE  = 1 << 0,
56   RESEND_LOOP_COUNT  = 1 << 1
57 };
58
59 #if defined(DEBUG_ENABLED)
60 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
61 #endif
62
63 } // unnamed namespace
64
65 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
66 {
67   AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
68   visual->SetProperties( properties );
69
70   return visual;
71 }
72
73 AnimatedVectorImageVisualPtr AnimatedVectorImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
74 {
75   AnimatedVectorImageVisualPtr visual( new AnimatedVectorImageVisual( factoryCache, shaderFactory, imageUrl ) );
76
77   return visual;
78 }
79
80 AnimatedVectorImageVisual::AnimatedVectorImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
81 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
82   mImageVisualShaderFactory( shaderFactory ),
83   mUrl( imageUrl ),
84   mVectorRasterizeThread( imageUrl.GetUrl() ),
85   mVisualSize(),
86   mPlayRange( 0.0f, 1.0f ),
87   mPlacementActor(),
88   mLoopCount( LOOP_FOREVER ),
89   mResendFlag( 0 ),
90   mActionStatus( DevelAnimatedVectorImageVisual::Action::STOP )
91 {
92   // the rasterized image is with pre-multiplied alpha format
93   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
94
95   mVectorRasterizeThread.SetResourceReadyCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnResourceReady ) ) );
96   mVectorRasterizeThread.SetAnimationFinishedCallback( new EventThreadCallback( MakeCallback( this, &AnimatedVectorImageVisual::OnAnimationFinished ) ) );
97
98   mVectorRasterizeThread.Start();
99 }
100
101 AnimatedVectorImageVisual::~AnimatedVectorImageVisual()
102 {
103 }
104
105 void AnimatedVectorImageVisual::GetNaturalSize( Vector2& naturalSize )
106 {
107   if( mImpl->mRenderer ) // Check if we have a rendered image
108   {
109     auto textureSet = mImpl->mRenderer.GetTextures();
110     if( textureSet )
111     {
112       if( textureSet.GetTextureCount() > 0 )
113       {
114         auto texture = textureSet.GetTexture( 0 );
115         naturalSize.x = texture.GetWidth();
116         naturalSize.y = texture.GetHeight();
117         return;
118       }
119     }
120   }
121
122   uint32_t width, height;
123   mVectorRasterizeThread.GetDefaultSize( width, height );
124   naturalSize.x = width;
125   naturalSize.y = height;
126 }
127
128 void AnimatedVectorImageVisual::DoCreatePropertyMap( Property::Map& map ) const
129 {
130   map.Clear();
131   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ANIMATED_VECTOR_IMAGE );
132   if( mUrl.IsValid() )
133   {
134     map.Insert( Toolkit::ImageVisual::Property::URL, mUrl.GetUrl() );
135   }
136   map.Insert( Toolkit::DevelImageVisual::Property::LOOP_COUNT, mLoopCount );
137   map.Insert( Toolkit::DevelImageVisual::Property::PLAY_RANGE, mPlayRange );
138   map.Insert( Toolkit::DevelImageVisual::Property::PLAY_STATE, static_cast< int >( mVectorRasterizeThread.GetPlayState() ) );
139   map.Insert( Toolkit::DevelImageVisual::Property::CURRENT_PROGRESS, mVectorRasterizeThread.GetCurrentProgress() );
140 }
141
142 void AnimatedVectorImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
143 {
144   // Do nothing
145 }
146
147 void AnimatedVectorImageVisual::DoSetProperties( const Property::Map& propertyMap )
148 {
149   // url already passed in from constructor
150   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
151   {
152     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
153     if( keyValue.first.type == Property::Key::INDEX )
154     {
155       DoSetProperty( keyValue.first.indexKey, keyValue.second );
156     }
157     else
158     {
159        if( keyValue.first == LOOP_COUNT_NAME )
160        {
161           DoSetProperty( Toolkit::DevelImageVisual::Property::LOOP_COUNT, keyValue.second );
162        }
163        else if( keyValue.first == PLAY_RANGE_NAME )
164        {
165           DoSetProperty( Toolkit::DevelImageVisual::Property::PLAY_RANGE, keyValue.second );
166        }
167     }
168   }
169 }
170
171 void AnimatedVectorImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
172 {
173   switch(index)
174   {
175     case Toolkit::DevelImageVisual::Property::LOOP_COUNT:
176     {
177       int32_t loopCount;
178       if( value.Get( loopCount ) )
179       {
180         mLoopCount = loopCount;
181         mResendFlag |= RESEND_LOOP_COUNT;
182       }
183       break;
184     }
185     case Toolkit::DevelImageVisual::Property::PLAY_RANGE:
186     {
187       Vector2 range;
188       if( value.Get( range ) )
189       {
190         mPlayRange = range;
191         mResendFlag |= RESEND_PLAY_RANGE;
192       }
193       break;
194     }
195   }
196 }
197
198 void AnimatedVectorImageVisual::DoSetOnStage( Actor& actor )
199 {
200   Shader shader;
201
202   if( mImpl->mCustomShader )
203   {
204     shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
205                           mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
206                           mImpl->mCustomShader->mHints );
207
208     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
209   }
210   else
211   {
212     shader = mImageVisualShaderFactory.GetShader( mFactoryCache, false, true );
213   }
214
215   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
216
217   mImpl->mRenderer = Renderer::New( geometry, shader );
218
219   TextureSet textureSet = TextureSet::New();
220   mImpl->mRenderer.SetTextures( textureSet );
221
222   // Register transform properties
223   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
224
225   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
226
227   // Hold the weak handle of the placement actor and delay the adding of renderer until the rasterization is finished.
228   mPlacementActor = actor;
229
230   mVectorRasterizeThread.SetRenderer( mImpl->mRenderer );
231 }
232
233 void AnimatedVectorImageVisual::DoSetOffStage( Actor& actor )
234 {
235   mVectorRasterizeThread.PauseAnimation();
236
237   mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
238
239   if( mImpl->mRenderer )
240   {
241     mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
242
243     actor.RemoveRenderer( mImpl->mRenderer );
244     mImpl->mRenderer.Reset();
245   }
246
247   mPlacementActor.Reset();
248
249   // Reset the visual size to zero so that when adding the actor back to stage the rasterization is forced
250   mVisualSize = Vector2::ZERO;
251 }
252
253 void AnimatedVectorImageVisual::OnSetTransform()
254 {
255   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
256
257   if( IsOnStage() )
258   {
259     DALI_LOG_INFO( gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnSetTransform: width = %f, height = %f\n", visualSize.width, visualSize.height );
260
261     if( visualSize != mVisualSize )
262     {
263       mVisualSize = visualSize;
264
265       uint32_t width = static_cast< uint32_t >( visualSize.width );
266       uint32_t height = static_cast< uint32_t >( visualSize.height );
267
268       mVectorRasterizeThread.SetSize( width, height );
269     }
270
271     SendAnimationData();
272
273     if( mActionStatus == DevelAnimatedVectorImageVisual::Action::PLAY )
274     {
275       mVectorRasterizeThread.PlayAnimation();
276
277       mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
278     }
279     else
280     {
281       // Render one frame
282       mVectorRasterizeThread.RenderFrame();
283     }
284
285     if( mVectorRasterizeThread.IsResourceReady() )
286     {
287       Actor actor = mPlacementActor.GetHandle();
288       if( actor )
289       {
290         actor.AddRenderer( mImpl->mRenderer );
291         mPlacementActor.Reset();
292       }
293
294       ResourceReady( Toolkit::Visual::ResourceStatus::READY );
295     }
296   }
297 }
298
299 void AnimatedVectorImageVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
300 {
301   // Check if action is valid for this visual type and perform action if possible
302   switch( actionId )
303   {
304     case DevelAnimatedVectorImageVisual::Action::PLAY:
305     {
306       if( IsOnStage() && mVisualSize != Vector2::ZERO )
307       {
308         mVectorRasterizeThread.PlayAnimation();
309
310         mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::CONTINUOUSLY );
311       }
312       mActionStatus = DevelAnimatedVectorImageVisual::Action::PLAY;
313       break;
314     }
315     case DevelAnimatedVectorImageVisual::Action::PAUSE:
316     {
317       mVectorRasterizeThread.PauseAnimation();
318
319       if( mImpl->mRenderer )
320       {
321         mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
322       }
323
324       mActionStatus = DevelAnimatedVectorImageVisual::Action::PAUSE;
325       break;
326     }
327     case DevelAnimatedVectorImageVisual::Action::STOP:
328     {
329       if( mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::STOPPED )
330       {
331         mVectorRasterizeThread.StopAnimation();
332
333         OnAnimationFinished();
334       }
335       mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
336       break;
337     }
338     case DevelAnimatedVectorImageVisual::Action::JUMP_TO:
339     {
340       float progress;
341       if( attributes.Get( progress ) )
342       {
343         mVectorRasterizeThread.SetCurrentProgress( progress );
344
345         if( IsOnStage() && mVectorRasterizeThread.GetPlayState() != DevelImageVisual::PlayState::PLAYING )
346         {
347           mVectorRasterizeThread.RenderFrame();
348           Stage::GetCurrent().KeepRendering( 0.0f );    // Trigger rendering
349         }
350       }
351       break;
352     }
353     case DevelAnimatedVectorImageVisual::Action::UPDATE_PROPERTY:
354     {
355       Property::Map* map = attributes.GetMap();
356       if( map )
357       {
358         DoSetProperties( *map );
359
360         SendAnimationData();
361       }
362       break;
363     }
364   }
365 }
366
367 void AnimatedVectorImageVisual::OnResourceReady()
368 {
369   // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
370   Actor actor = mPlacementActor.GetHandle();
371   if( actor )
372   {
373     actor.AddRenderer( mImpl->mRenderer );
374     // reset the weak handle so that the renderer only get added to actor once
375     mPlacementActor.Reset();
376
377     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
378   }
379 }
380
381 void AnimatedVectorImageVisual::OnAnimationFinished()
382 {
383   if( mImpl->mEventObserver )
384   {
385     mImpl->mEventObserver->NotifyVisualEvent( *this, DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED );
386   }
387
388   mActionStatus = DevelAnimatedVectorImageVisual::Action::STOP;
389
390   if( mImpl->mRenderer )
391   {
392     mImpl->mRenderer.SetProperty( DevelRenderer::Property::RENDERING_BEHAVIOR, DevelRenderer::Rendering::IF_REQUIRED );
393   }
394 }
395
396 void AnimatedVectorImageVisual::SendAnimationData()
397 {
398   if( mResendFlag )
399   {
400     bool isPlaying = false;
401     if( mVectorRasterizeThread.GetPlayState() == DevelImageVisual::PlayState::PLAYING )
402     {
403       mVectorRasterizeThread.PauseAnimation();
404       isPlaying = true;
405     }
406
407     if( mResendFlag & RESEND_LOOP_COUNT )
408     {
409       mVectorRasterizeThread.SetLoopCount( mLoopCount );
410     }
411
412     if( mResendFlag & RESEND_PLAY_RANGE )
413     {
414       mVectorRasterizeThread.SetPlayRange( mPlayRange );
415     }
416
417     if( IsOnStage() )
418     {
419       if( isPlaying )
420       {
421         mVectorRasterizeThread.PlayAnimation();
422       }
423       else
424       {
425         mVectorRasterizeThread.RenderFrame();
426         Stage::GetCurrent().KeepRendering( 0.0f );
427       }
428     }
429
430     mResendFlag = 0;
431   }
432 }
433
434 } // namespace Internal
435
436 } // namespace Toolkit
437
438 } // namespace Dali