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