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