Add synchronous loading of svg-visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.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 "visual-base-impl.h"
20
21 // EXTERNAL HEADER
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/devel-api/object/handle-devel.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali/devel-api/rendering/renderer-devel.h>
26 #include <dali/integration-api/debug.h>
27
28 //INTERNAL HEARDER
29 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/visual-properties.h>
32 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
33 #include <dali-toolkit/internal/helpers/property-helper.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36
37 namespace
38 {
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
41 #endif
42
43 const char * const PRE_MULTIPLIED_ALPHA_PROPERTY( "preMultipliedAlpha" );
44
45 } // namespace
46
47 namespace Dali
48 {
49
50 namespace Toolkit
51 {
52
53 namespace Internal
54 {
55
56 namespace
57 {
58
59 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_FITTING_MODE )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_KEEP_ASPECT_RATIO  )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FILL  )
62 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_FITTING_MODE )
63
64 } // namespace
65
66 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode )
67 : mImpl( new Impl(fittingMode) ),
68   mFactoryCache( factoryCache )
69 {
70 }
71
72 Visual::Base::~Base()
73 {
74   delete mImpl;
75 }
76
77 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
78 {
79   if( mImpl->mCustomShader )
80   {
81     mImpl->mCustomShader->SetPropertyMap( shaderMap );
82   }
83   else
84   {
85     mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
86   }
87 }
88
89 void Visual::Base::SetProperties( const Property::Map& propertyMap )
90 {
91   for( size_t i = 0; i < propertyMap.Count(); ++i )
92   {
93     const KeyValuePair& pair = propertyMap.GetKeyValue( i );
94     const Property::Key& key = pair.first;
95     const Property::Value& value = pair.second;
96
97     Property::Key matchKey = key;
98     if( matchKey.type == Property::Key::STRING )
99     {
100       if( matchKey == CUSTOM_SHADER )
101       {
102         matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
103       }
104       else if( matchKey == TRANSFORM )
105       {
106         matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
107       }
108       else if( matchKey == PREMULTIPLIED_ALPHA )
109       {
110         matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
111       }
112       else if( matchKey == MIX_COLOR )
113       {
114         matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
115       }
116       else if( matchKey == OPACITY )
117       {
118         matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
119       }
120       else if( matchKey == VISUAL_FITTING_MODE )
121       {
122         matchKey = Property::Key( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE );
123       }
124       else if( matchKey == CORNER_RADIUS )
125       {
126         matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS );
127       }
128     }
129
130     switch( matchKey.indexKey )
131     {
132       case Toolkit::Visual::Property::SHADER:
133       {
134         Property::Map shaderMap;
135         if( value.Get( shaderMap ) )
136         {
137           SetCustomShader( shaderMap );
138         }
139         break;
140       }
141
142       case Toolkit::Visual::Property::TRANSFORM:
143       {
144         Property::Map map;
145         if( value.Get( map ) )
146         {
147           mImpl->mTransform.SetPropertyMap( map );
148         }
149         break;
150       }
151
152       case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
153       {
154         bool premultipliedAlpha = false;
155         if( value.Get( premultipliedAlpha ) )
156         {
157           EnablePreMultipliedAlpha( premultipliedAlpha );
158         }
159         break;
160       }
161
162       case Toolkit::Visual::Property::MIX_COLOR:
163       {
164         Vector4 mixColor;
165         if( value.Get( mixColor ) )
166         {
167           if( value.GetType() == Property::VECTOR4 )
168           {
169             SetMixColor( mixColor );
170           }
171           else
172           {
173             Vector3 mixColor3(mixColor);
174             SetMixColor( mixColor3 );
175           }
176         }
177         break;
178       }
179       case Toolkit::Visual::Property::OPACITY:
180       {
181         float opacity;
182         if( value.Get( opacity ) )
183         {
184           mImpl->mMixColor.a = opacity;
185           SetMixColor( mImpl->mMixColor );
186         }
187         break;
188       }
189       case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
190       {
191         Scripting::GetEnumerationProperty< Visual::FittingMode >(
192           value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
193         break;
194       }
195       case Toolkit::DevelVisual::Property::CORNER_RADIUS:
196       {
197         float radius;
198         if( value.Get( radius ) )
199         {
200           mImpl->mCornerRadius = radius;
201         }
202         break;
203       }
204     }
205   }
206
207   DoSetProperties( propertyMap );
208 }
209
210 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
211 {
212   mImpl->mControlSize = controlSize;
213   mImpl->mTransform.UpdatePropertyMap( transform );
214
215 #if defined(DEBUG_ENABLED)
216   std::ostringstream oss;
217   oss << transform;
218   DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s  controlSize: (%3.1f, %3.1f)]\e[0m\n",
219                  GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
220 #endif
221
222   OnSetTransform();
223 }
224
225 void Visual::Base::SetName( const std::string& name )
226 {
227   mImpl->mName = name;
228 }
229
230 const std::string& Visual::Base::GetName() const
231 {
232   return mImpl->mName;
233 }
234
235 float Visual::Base::GetHeightForWidth( float width )
236 {
237   float aspectCorrectedHeight = 0.f;
238   Vector2 naturalSize;
239   GetNaturalSize( naturalSize );
240   if( naturalSize.width )
241   {
242     aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
243   }
244   return aspectCorrectedHeight;
245 }
246
247 float Visual::Base::GetWidthForHeight( float height )
248 {
249   float aspectCorrectedWidth = 0.f;
250   Vector2 naturalSize;
251   GetNaturalSize( naturalSize );
252   if( naturalSize.height > 0.0f )
253   {
254     aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
255   }
256   return aspectCorrectedWidth;
257 }
258
259 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
260 {
261   naturalSize = Vector2::ZERO;
262 }
263
264 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
265 {
266   OnDoAction( actionId, attributes );
267 }
268
269 void Visual::Base::SetDepthIndex( int index )
270 {
271   mImpl->mDepthIndex = index;
272   if( mImpl->mRenderer )
273   {
274     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
275   }
276 }
277
278 int Visual::Base::GetDepthIndex() const
279 {
280   return mImpl->mDepthIndex;
281 }
282
283 void Visual::Base::SetOnStage( Actor& actor )
284 {
285   if( !IsOnStage() )
286   {
287     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
288     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
289     DoSetOnStage( actor );
290
291     if( mImpl->mRenderer )
292     {
293       RegisterMixColor();
294
295       if( IsRoundedCornerRequired() )
296       {
297         mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
298
299         mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
300       }
301
302       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
303       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
304       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
305     }
306   }
307 }
308
309 void Visual::Base::SetOffStage( Actor& actor )
310 {
311   if( IsOnStage() )
312   {
313     DoSetOffStage( actor );
314     mImpl->mMixColorIndex = Property::INVALID_INDEX;
315     mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
316     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
317   }
318 }
319
320 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
321 {
322   DoCreatePropertyMap( map );
323
324   if( mImpl->mCustomShader )
325   {
326     mImpl->mCustomShader->CreatePropertyMap( map );
327   }
328
329   Property::Map transform;
330   mImpl->mTransform.GetPropertyMap( transform );
331   map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
332
333   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
334   map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
335
336   // Note, Color and Primitive will also insert their own mix color into the map
337   // which is ok, because they have a different key value range.
338   map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
339   map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
340   map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
341
342   auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
343     mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
344   map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
345
346   map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
347 }
348
349 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
350 {
351   DoCreateInstancePropertyMap( map );
352
353   if( mImpl->mCustomShader )
354   {
355     mImpl->mCustomShader->CreatePropertyMap( map );
356   }
357
358   //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
359   //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
360 }
361
362
363 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
364 {
365   if( preMultiplied )
366   {
367     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
368   }
369   else
370   {
371     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
372   }
373
374   if( mImpl->mRenderer )
375   {
376     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
377     mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
378   }
379 }
380
381 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
382 {
383   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
384 }
385
386 void Visual::Base::DoSetOffStage( Actor& actor )
387 {
388   actor.RemoveRenderer( mImpl->mRenderer );
389   mImpl->mRenderer.Reset();
390 }
391
392 bool Visual::Base::IsOnStage() const
393 {
394   return mImpl->mFlags & Impl::IS_ON_STAGE;
395 }
396
397 bool Visual::Base::IsRoundedCornerRequired() const
398 {
399   return !EqualsZero( mImpl->mCornerRadius );
400 }
401
402 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
403 {
404   // May be overriden by derived class
405 }
406
407 void Visual::Base::RegisterMixColor()
408 {
409   // Only register if not already registered.
410   // (Color and Primitive visuals will register their own and save to this index)
411   if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
412   {
413     mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
414       mImpl->mRenderer,
415       Toolkit::Visual::Property::MIX_COLOR,
416       MIX_COLOR,
417       Vector3(mImpl->mMixColor) );
418   }
419
420   if( mImpl->mMixColor.a < 1.f )
421   {
422     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
423   }
424
425   mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
426
427   float preMultipliedAlpha = 0.0f;
428   if( IsPreMultipliedAlphaEnabled() )
429   {
430     preMultipliedAlpha = 1.0f;
431   }
432   mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
433 }
434
435 void Visual::Base::SetMixColor( const Vector4& color )
436 {
437   mImpl->mMixColor = color;
438
439   if( mImpl->mRenderer )
440   {
441     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
442     mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
443     if( color.a < 1.f )
444     {
445       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
446     }
447   }
448 }
449
450 void Visual::Base::SetMixColor( const Vector3& color )
451 {
452   mImpl->mMixColor.r = color.r;
453   mImpl->mMixColor.g = color.g;
454   mImpl->mMixColor.b = color.b;
455
456   if( mImpl->mRenderer )
457   {
458     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
459   }
460 }
461
462 const Vector4& Visual::Base::GetMixColor() const
463 {
464   return mImpl->mMixColor;
465 }
466
467 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
468 {
469   mImpl->mEventObserver = &observer;
470 }
471
472 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
473 {
474   mImpl->mEventObserver = NULL;
475 }
476
477 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
478 {
479   if( mImpl->mResourceStatus != resourceStatus )
480   {
481     mImpl->mResourceStatus = resourceStatus;
482
483     if( mImpl->mEventObserver )
484     {
485       // observer is currently a control impl
486       mImpl->mEventObserver->ResourceReady( *this );
487     }
488   }
489 }
490
491 bool Visual::Base::IsResourceReady() const
492 {
493   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
494 }
495
496 bool Visual::Base::IsSynchronousLoadingRequired() const
497 {
498   return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
499 }
500
501 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
502 {
503   return mImpl->mResourceStatus;
504 }
505
506 Visual::FittingMode Visual::Base::GetFittingMode() const
507 {
508   return mImpl->mFittingMode;
509 }
510
511 Visual::Base& Visual::Base::GetVisualObject()
512 {
513   return *this;
514 }
515
516 Renderer Visual::Base::GetRenderer()
517 {
518   return mImpl->mRenderer;
519 }
520
521 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
522 {
523   Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
524
525   if( index == Property::INVALID_INDEX )
526   {
527     // Is it a shader property?
528     Shader shader = mImpl->mRenderer.GetShader();
529     index = DevelHandle::GetPropertyIndex( shader, key );
530     if( index != Property::INVALID_INDEX )
531     {
532       // Yes - we should register it in the Renderer so it can be set / animated
533       // independently, as shaders are shared across multiple renderers.
534       std::string keyName;
535       Property::Index keyIndex( Property::INVALID_KEY );
536       if( key.type == Property::Key::INDEX )
537       {
538         keyName = shader.GetPropertyName( index );
539         keyIndex = key.indexKey;
540       }
541       else
542       {
543         keyName = key.stringKey;
544         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
545       }
546       Property::Value value = shader.GetProperty( index );
547       index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
548     }
549   }
550   return index;
551 }
552
553 void Visual::Base::SetupTransition(
554   Dali::Animation& transition,
555   Internal::TransitionData::Animator& animator,
556   Property::Index index,
557   Property::Value& initialValue,
558   Property::Value& targetValue )
559 {
560   if( index != Property::INVALID_INDEX )
561   {
562     if( mImpl->mRenderer )
563     {
564       if( animator.animate == false )
565       {
566         mImpl->mRenderer.SetProperty( index, targetValue );
567       }
568       else
569       {
570         if( animator.initialValue.GetType() != Property::NONE )
571         {
572           mImpl->mRenderer.SetProperty( index, initialValue );
573         }
574
575         if( ! transition )
576         {
577           transition = Dali::Animation::New( 0.1f );
578         }
579
580         transition.AnimateTo( Property( mImpl->mRenderer, index ),
581                               targetValue,
582                               animator.alphaFunction,
583                               TimePeriod( animator.timePeriodDelay,
584                                           animator.timePeriodDuration ) );
585       }
586     }
587   }
588 }
589
590 void Visual::Base::AnimateProperty(
591   Dali::Animation& transition,
592   Internal::TransitionData::Animator& animator )
593 {
594 #if defined(DEBUG_ENABLED)
595   {
596     std::ostringstream oss;
597     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
598     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
599   }
600 #endif
601
602   Property::Map map;
603   DoCreatePropertyMap( map );
604   Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
605   int visualType = -1;
606   if( valuePtr )
607   {
608     valuePtr->Get( visualType );
609   }
610
611   if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
612       animator.propertyKey == MIX_COLOR ||
613       ( visualType == Toolkit::Visual::COLOR &&
614         animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
615       ( visualType == Toolkit::Visual::PRIMITIVE &&
616         animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
617   {
618     AnimateMixColorProperty( transition, animator );
619   }
620   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
621           animator.propertyKey == OPACITY )
622   {
623     AnimateOpacityProperty( transition, animator );
624   }
625   else if( mImpl->mRenderer )
626   {
627     AnimateRendererProperty( transition, animator );
628   }
629 }
630
631 void Visual::Base::AnimateOpacityProperty(
632   Dali::Animation& transition,
633   Internal::TransitionData::Animator& animator )
634 {
635   bool isOpaque = mImpl->mMixColor.a >= 1.0f;
636
637   float initialOpacity;
638   if( animator.initialValue.Get( initialOpacity ) )
639   {
640     isOpaque = (initialOpacity >= 1.0f);
641   }
642
643   float targetOpacity;
644   if( animator.targetValue.Get( targetOpacity ) )
645   {
646     mImpl->mMixColor.a = targetOpacity;
647   }
648
649   SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
650   SetupBlendMode( transition, isOpaque, animator.animate );
651 }
652
653 void Visual::Base::AnimateRendererProperty(
654   Dali::Animation& transition,
655   Internal::TransitionData::Animator& animator )
656 {
657   Property::Index index = GetPropertyIndex( animator.propertyKey );
658   if( index != Property::INVALID_INDEX )
659   {
660     if( animator.targetValue.GetType() != Property::NONE )
661     {
662       // Try writing target value into transform property map
663       // if it's not a valid key, then it won't alter mTransform
664       Property::Map map;
665       if( animator.propertyKey.type == Property::Key::INDEX )
666       {
667         map.Add( animator.propertyKey.indexKey, animator.targetValue );
668       }
669       else
670       {
671         map.Add( animator.propertyKey.stringKey, animator.targetValue );
672       }
673
674       mImpl->mTransform.UpdatePropertyMap( map );
675     }
676
677     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
678   }
679 }
680
681 void Visual::Base::AnimateMixColorProperty(
682   Dali::Animation& transition,
683   Internal::TransitionData::Animator& animator )
684 {
685   Property::Index index = mImpl->mMixColorIndex;
686   bool animateOpacity = false;
687   bool isOpaque = true;
688
689   Property::Value initialOpacity;
690   Property::Value targetOpacity;
691   Property::Value initialMixColor;
692   Property::Value targetMixColor;
693
694   if( index != Property::INVALID_INDEX )
695   {
696     Vector4 initialColor;
697     if( animator.initialValue.Get(initialColor) )
698     {
699       if( animator.initialValue.GetType() == Property::VECTOR4 )
700       {
701         // if there is an initial color specifying alpha, test it
702         isOpaque = initialColor.a >= 1.0f;
703         initialOpacity = initialColor.a;
704       }
705       initialMixColor = Vector3( initialColor );
706     }
707
708     // Set target value into data store
709     if( animator.targetValue.GetType() != Property::NONE )
710     {
711       Vector4 mixColor;
712       animator.targetValue.Get(mixColor);
713       if( animator.targetValue.GetType() == Property::VECTOR4 )
714       {
715         mImpl->mMixColor.a = mixColor.a;
716         targetOpacity = mixColor.a;
717         animateOpacity = true;
718       }
719
720       mImpl->mMixColor.r = mixColor.r;
721       mImpl->mMixColor.g = mixColor.g;
722       mImpl->mMixColor.b = mixColor.b;
723       targetMixColor = Vector3(mixColor);
724     }
725
726     SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
727     if( animateOpacity )
728     {
729       SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
730       SetupBlendMode( transition, isOpaque, animator.animate );
731     }
732   }
733 }
734
735 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
736 {
737   // Ensure the blend mode is turned on if we are animating opacity, and
738   // turned off after the animation ends if the final value is opaque
739   if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
740   {
741     if( mImpl->mRenderer )
742     {
743       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
744
745       if( animating == true && mImpl->mMixColor.a >= 1.0f )
746       {
747         // When it becomes opaque, set the blend mode back to automatically
748         if( ! mImpl->mBlendSlotDelegate )
749         {
750           mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
751         }
752         transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
753                                              &Visual::Base::OnMixColorFinished );
754       }
755     }
756   }
757 }
758
759 void Visual::Base::OnMixColorFinished( Animation& animation )
760 {
761   if( mImpl->mRenderer )
762   {
763     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
764     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
765                                   ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
766   }
767   delete mImpl->mBlendSlotDelegate;
768   mImpl->mBlendSlotDelegate = NULL;
769 }
770
771 } // namespace Internal
772
773 } // namespace Toolkit
774
775 } // namespace Dali