Convert more shaders in dali-toolkit and dali-scene-loader to use shader compilation...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2020 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/rendering/renderer-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/devel-api/visuals/color-visual-actions-devel.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
46 {
47   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
48   colorVisualPtr->SetProperties( properties );
49   return colorVisualPtr;
50 }
51
52 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
53 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
54   mBlurRadius(0.0f),
55   mBlurRadiusIndex(Property::INVALID_INDEX),
56   mRenderIfTransparent(false),
57   mNeedBlurRadius(false)
58 {
59 }
60
61 ColorVisual::~ColorVisual()
62 {
63 }
64
65 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
66 {
67   // By virtue of DoSetProperties being called last, this will override
68   // anything set by Toolkit::Visual::Property::MIX_COLOR
69   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
70   if( colorValue )
71   {
72     Vector4 color;
73     if( colorValue->Get( color ) )
74     {
75       Property::Type type = colorValue->GetType();
76       if( type == Property::VECTOR4 )
77       {
78         SetMixColor( color );
79       }
80       else if( type == Property::VECTOR3 )
81       {
82         Vector3 color3(color);
83         SetMixColor( color3 );
84       }
85     }
86     else
87     {
88       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
89     }
90   }
91
92   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
93   if( renderIfTransparentValue )
94   {
95     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
96     {
97       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
98     }
99   }
100
101   Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
102   if( blurRadiusValue )
103   {
104     if( !blurRadiusValue->Get( mBlurRadius ) )
105     {
106       DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
107     }
108   }
109 }
110
111 void ColorVisual::DoSetOnScene( Actor& actor )
112 {
113   InitializeRenderer();
114
115   // Only add the renderer if it's not fully transparent
116   // We cannot avoid creating a renderer as it's used in the base class
117   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
118   {
119     actor.AddRenderer( mImpl->mRenderer );
120   }
121
122   // Color Visual generated and ready to display
123   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
124 }
125
126 void ColorVisual::DoSetOffScene(Actor& actor)
127 {
128   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
129   {
130     // Update values from Renderer
131     mBlurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
132   }
133
134   actor.RemoveRenderer(mImpl->mRenderer);
135   mImpl->mRenderer.Reset();
136   mBlurRadiusIndex = Property::INVALID_INDEX;
137 }
138
139 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
140 {
141   map.Clear();
142   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
143   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
144   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
145
146   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
147   {
148     // Update values from Renderer
149     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
150     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
151   }
152   else
153   {
154     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
155   }
156 }
157
158 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
159 {
160   // Do nothing
161 }
162
163 void ColorVisual::OnSetTransform()
164 {
165   if( mImpl->mRenderer )
166   {
167     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
168   }
169 }
170
171 void ColorVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
172 {
173   // Check if action is valid for this visual type and perform action if possible
174   switch( actionId )
175   {
176     case DevelColorVisual::Action::UPDATE_PROPERTY:
177     {
178       const Property::Map* map = attributes.GetMap();
179       if( map )
180       {
181         DoSetProperties( *map );
182       }
183       break;
184     }
185   }
186 }
187
188 void ColorVisual::UpdateShader()
189 {
190   if(mImpl->mRenderer)
191   {
192     Shader shader = GetShader();
193     mImpl->mRenderer.SetShader(shader);
194   }
195 }
196
197 void ColorVisual::InitializeRenderer()
198 {
199   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
200
201   Shader shader = GetShader();
202
203   mImpl->mRenderer = Renderer::New(geometry, shader);
204
205   // ColorVisual has it's own index key for mix color - use this instead
206   // of using the new base index to avoid changing existing applications
207   // String keys will get to this property.
208   mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
209
210   if(!EqualsZero(mBlurRadius))
211   {
212     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
213     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
214   }
215
216   // Register transform properties
217   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
218 }
219
220 Shader ColorVisual::GetShader()
221 {
222   Shader shader;
223   if(!EqualsZero(mBlurRadius) || mNeedBlurRadius)
224   {
225     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
226     if( !shader )
227     {
228       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_FRAG.data() );
229       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
230     }
231   }
232   else if( !IsRoundedCornerRequired() )
233   {
234     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
235     if( !shader )
236     {
237       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_FRAG.data() );
238       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
239     }
240   }
241   else
242   {
243     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
244     if( !shader )
245     {
246       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_FRAG.data() );
247       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
248     }
249   }
250
251   return shader;
252 }
253
254 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
255 {
256   if(!mImpl->mRenderer)
257   {
258     Handle handle;
259     return Dali::Property(handle, Property::INVALID_INDEX);
260   }
261
262   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
263   {
264     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
265
266     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
267
268     mNeedBlurRadius = true;
269
270     // Change shader
271     UpdateShader();
272
273     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
274   }
275
276   Handle handle;
277   return Dali::Property(handle, Property::INVALID_INDEX);
278 }
279
280 } // namespace Internal
281
282 } // namespace Toolkit
283
284 } // namespace Dali