(GradientVisual) Turn BlendMode on renderer to OFF if using fully opaque colors 15/96315/3
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 8 Nov 2016 11:56:57 +0000 (11:56 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 8 Nov 2016 13:57:03 +0000 (13:57 +0000)
Change-Id: Ib5c0da3df30b42940254561315854f8f8ab12d87

automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp
dali-toolkit/internal/visuals/gradient/gradient-visual.cpp
dali-toolkit/internal/visuals/gradient/gradient-visual.h

index 7cda6e5..ff64e24 100644 (file)
@@ -1615,4 +1615,41 @@ int UtcDaliNPatchVisualCustomShader(void)
   END_TEST;
 }
 
+int UtcDaliGradientVisualBlendMode(void)
+{
+  ToolkitTestApplication application;
+
+  VisualFactory factory = VisualFactory::Get();
+
+  Visual::Base opaqueGradientVisual = factory.CreateVisual(
+      Property::Map().Add( Visual::Property::TYPE, Visual::GRADIENT )
+                     .Add( GradientVisual::Property::START_POSITION, Vector2( -0.5f, -0.5f ) )
+                     .Add( GradientVisual::Property::END_POSITION, Vector2( 0.5f, 0.5f ) )
+                     .Add( GradientVisual::Property::STOP_COLOR, Property::Array().Add( Color::RED )
+                                                                                  .Add( Color::GREEN ) ) );
+
+  Visual::Base alphaGradientVisual = factory.CreateVisual(
+      Property::Map().Add( Visual::Property::TYPE, Visual::GRADIENT )
+                     .Add( GradientVisual::Property::START_POSITION, Vector2( -0.5f, -0.5f ) )
+                     .Add( GradientVisual::Property::END_POSITION, Vector2( 0.5f, 0.5f ) )
+                     .Add( GradientVisual::Property::STOP_COLOR, Property::Array().Add( Color::RED )
+                                                                                  .Add( Vector4( 1.0f, 1.0f, 1.0f, 0.5f ) ) ) );
+
+  DummyControl control = DummyControl::New();
+  control.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
+  Stage::GetCurrent().Add( control );
 
+  DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>( control.GetImplementation() );
+  dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, opaqueGradientVisual );
+  dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 2, alphaGradientVisual );
+
+  application.SendNotification();
+  application.Render();
+
+  // Control should have two renderers, the first one is opaque so our blending mode should be off, the second one has some alpha so should be set to automatic
+  DALI_TEST_EQUALS( 2u, control.GetRendererCount(), TEST_LOCATION );
+  DALI_TEST_EQUALS( control.GetRendererAt( 0 ).GetProperty( Renderer::Property::BLEND_MODE ).Get<int>(), (int)BlendMode::OFF, TEST_LOCATION );
+  DALI_TEST_EQUALS( control.GetRendererAt( 1 ).GetProperty( Renderer::Property::BLEND_MODE ).Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
+
+  END_TEST;
+}
index e0ae085..6f50a9a 100644 (file)
@@ -222,7 +222,8 @@ GradientVisualPtr GradientVisual::New( VisualFactoryCache& factoryCache )
 
 GradientVisual::GradientVisual( VisualFactoryCache& factoryCache )
 : Visual::Base( factoryCache ),
-  mGradientType( LINEAR )
+  mGradientType( LINEAR ),
+  mIsOpaque( true )
 {
   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
 }
@@ -362,6 +363,12 @@ void GradientVisual::InitializeRenderer()
   mImpl->mRenderer = Renderer::New( geometry, shader );
   mImpl->mRenderer.SetTextures( textureSet );
 
+  // If opaque then no need to have blending
+  if( mIsOpaque )
+  {
+    mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
+  }
+
   mImpl->mRenderer.RegisterProperty( UNIFORM_ALIGNMENT_MATRIX_NAME, mGradientTransform );
 
   //Register transform properties
@@ -423,6 +430,10 @@ bool GradientVisual::NewGradient(Type gradientType, const Property::Map& propert
         {
           mGradient->AddStop( offsetArray[i], Vector4(color.r*color.a, color.g*color.a, color.b*color.a, color.a));
           numValidStop++;
+          if( ! Equals( color.a, 1.0f, Math::MACHINE_EPSILON_1 ) )
+          {
+            mIsOpaque = false;
+          }
         }
       }
     }
index e4920d1..bc131ae 100644 (file)
@@ -177,6 +177,7 @@ private:
   Matrix3 mGradientTransform;
   IntrusivePtr<Gradient> mGradient;
   Type mGradientType;
+  bool mIsOpaque; ///< Set to false if any of the stop colors are not opaque
 };
 
 } // namespace Internal