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