Merge "Unparents internalRoot actor when gaussian-blur-view is deactived." into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-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 "color-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
27 #include <dali-toolkit/public-api/visuals/visual-properties.h>
28 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
47   attribute mediump vec2 aPosition;\n
48   uniform highp   mat4 uMvpMatrix;\n
49   uniform mediump vec3 uSize;\n
50   \n
51
52   //Visual size and offset
53   uniform mediump vec2 offset;\n
54   uniform mediump vec2 size;\n
55   uniform mediump vec4 offsetSizeMode;\n
56   uniform mediump vec2 origin;\n
57   uniform mediump vec2 anchorPoint;\n
58
59   vec4 ComputeVertexPosition()\n
60   {\n
61     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
62     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
63     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
64   }\n
65
66   void main()\n
67   {\n
68     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
69   }\n
70 );
71
72 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
73   uniform lowp vec4 uColor;\n
74   uniform lowp vec3 mixColor;\n
75   \n
76   void main()\n
77   {\n
78     gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
79   }\n
80 );
81
82 const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER(
83   attribute mediump vec2 aPosition;\n
84   uniform highp   mat4 uMvpMatrix;\n
85   uniform mediump vec3 uSize;\n
86   varying mediump vec2 vPosition;\n
87   varying mediump vec2 vRectSize;\n
88   \n
89   //Visual size and offset
90   uniform mediump vec2 offset;\n
91   uniform mediump vec2 size;\n
92   uniform mediump vec4 offsetSizeMode;\n
93   uniform mediump vec2 origin;\n
94   uniform mediump vec2 anchorPoint;\n
95   uniform mediump float cornerRadius;\n
96   \n
97   vec4 ComputeVertexPosition()\n
98   {\n
99     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
100     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
101     vRectSize = visualSize / 2.0 - cornerRadius;\n
102     vPosition = aPosition* visualSize;\n
103     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
104   }\n
105   \n
106   void main()\n
107   {\n
108     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
109   }\n
110 );
111
112 //float distance = length( max( abs( position - center ), size ) - size ) - radius;
113 const char* FRAGMENT_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER(
114   varying mediump vec2 vPosition;\n
115   varying mediump vec2 vRectSize;\n
116   uniform lowp vec4 uColor;\n
117   uniform lowp vec3 mixColor;\n
118   uniform mediump float cornerRadius;\n
119   \n
120   void main()\n
121   {\n
122       mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n
123       gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
124       gl_FragColor.a *= smoothstep( 1.0, -1.0, dist );\n
125   }\n
126 );
127
128 }
129
130 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
131 {
132   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
133   colorVisualPtr->SetProperties( properties );
134   return colorVisualPtr;
135 }
136
137 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
138 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
139   mRenderIfTransparent( false )
140 {
141 }
142
143 ColorVisual::~ColorVisual()
144 {
145 }
146
147 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
148 {
149   // By virtue of DoSetProperties being called last, this will override
150   // anything set by Toolkit::Visual::Property::MIX_COLOR
151   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
152   if( colorValue )
153   {
154     Vector4 color;
155     if( colorValue->Get( color ) )
156     {
157       Property::Type type = colorValue->GetType();
158       if( type == Property::VECTOR4 )
159       {
160         SetMixColor( color );
161       }
162       else if( type == Property::VECTOR3 )
163       {
164         Vector3 color3(color);
165         SetMixColor( color3 );
166       }
167     }
168     else
169     {
170       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
171     }
172   }
173
174   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
175   if( renderIfTransparentValue )
176   {
177     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
178     {
179       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
180     }
181   }
182 }
183
184 void ColorVisual::DoSetOnStage( Actor& actor )
185 {
186   InitializeRenderer();
187
188   // Only add the renderer if it's not fully transparent
189   // We cannot avoid creating a renderer as it's used in the base class
190   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
191   {
192     actor.AddRenderer( mImpl->mRenderer );
193   }
194
195   // Color Visual generated and ready to display
196   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
197 }
198
199 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
200 {
201   map.Clear();
202   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
203   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
204   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
205 }
206
207 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
208 {
209   // Do nothing
210 }
211
212
213 void ColorVisual::OnSetTransform()
214 {
215   if( mImpl->mRenderer )
216   {
217     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
218   }
219 }
220
221 void ColorVisual::InitializeRenderer()
222 {
223   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
224
225   Shader shader;
226   if( !IsRoundedCornerRequired() )
227   {
228     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
229     if( !shader )
230     {
231       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
232       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
233     }
234   }
235   else
236   {
237     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
238     if( !shader )
239     {
240       shader = Shader::New( VERTEX_SHADER_ROUNDED_CORNER, FRAGMENT_SHADER_ROUNDED_CORNER );
241       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
242     }
243   }
244
245   mImpl->mRenderer = Renderer::New( geometry, shader );
246
247   // ColorVisual has it's own index key for mix color - use this instead
248   // of using the new base index to avoid changing existing applications
249   // String keys will get to this property.
250   mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
251
252   if( mImpl->mMixColor.a < 1.f )
253   {
254     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
255   }
256
257   // Register transform properties
258   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
259 }
260
261 } // namespace Internal
262
263 } // namespace Toolkit
264
265 } // namespace Dali