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