[dali_2.1.25] Merge branch 'devel/master'
[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
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
27 #include <dali-toolkit/devel-api/visuals/visual-actions-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 const int CUSTOM_PROPERTY_COUNT(6); // Blur Radius + border/corner
45
46 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[6] =
47   {
48     VisualFactoryCache::COLOR_SHADER,
49     VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER,
50     VisualFactoryCache::COLOR_SHADER_BORDERLINE,
51     VisualFactoryCache::COLOR_SHADER_ROUNDED_BORDERLINE,
52     VisualFactoryCache::COLOR_SHADER_BLUR_EDGE,
53     VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER_BLUR_EDGE,
54 };
55
56 // enum of required list when we select shader
57 enum ColorVisualRequireFlag
58 {
59   DEFAULT        = 0,
60   ROUNDED_CORNER = 1 << 0,
61   BORDERLINE     = 1 << 1,
62   BLUR           = 1 << 2,
63 };
64 } // unnamed namespace
65 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
66 {
67   ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
68   colorVisualPtr->SetProperties(properties);
69   colorVisualPtr->Initialize();
70   return colorVisualPtr;
71 }
72
73 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
74 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
75   mBlurRadius(0.0f),
76   mBlurRadiusIndex(Property::INVALID_INDEX),
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(mBlurRadiusIndex != Property::INVALID_INDEX)
121     {
122       mImpl->mRenderer.SetProperty(mBlurRadiusIndex, mBlurRadius);
123     }
124     else if(DALI_UNLIKELY(mImpl->mRenderer && (!EqualsZero(mBlurRadius) || mAlwaysUsingBlurRadius)))
125     {
126       // Unusual case. SetProperty called after OnInitialize().
127       // Assume that DoAction call UPDATE_PROPERTY.
128       // We must regist properies into renderer, and update shader.
129
130       // BlurRadius added by this action. Regist property to renderer.
131       mBlurRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
132       mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
133
134       // Change the shader must not be occured many times. we always have to use blur feature.
135       mAlwaysUsingBlurRadius = true;
136
137       // Change shader
138       UpdateShader();
139     }
140   }
141 }
142
143 void ColorVisual::DoSetOnScene(Actor& actor)
144 {
145   actor.AddRenderer(mImpl->mRenderer);
146
147   // Color Visual generated and ready to display
148   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
149 }
150
151 void ColorVisual::DoSetOffScene(Actor& actor)
152 {
153   actor.RemoveRenderer(mImpl->mRenderer);
154 }
155
156 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
157 {
158   map.Clear();
159   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
160   map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
161
162   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
163   {
164     // Update values from Renderer
165     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
166     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
167   }
168   else
169   {
170     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
171   }
172 }
173
174 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
175 {
176   // Do nothing
177 }
178
179 void ColorVisual::EnablePreMultipliedAlpha(bool preMultiplied)
180 {
181   // Make always disable pre multiplied alpha whether preMultiplied value is true.
182   if(preMultiplied)
183   {
184     DALI_LOG_WARNING("Note : ColorVisual cannot enable PreMultipliedAlpha\n");
185   }
186 }
187
188 void ColorVisual::OnSetTransform()
189 {
190   if(mImpl->mRenderer)
191   {
192     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
193   }
194 }
195
196 void ColorVisual::UpdateShader()
197 {
198   if(mImpl->mRenderer)
199   {
200     Shader shader = GenerateShader();
201     mImpl->mRenderer.SetShader(shader);
202   }
203 }
204
205 void ColorVisual::OnInitialize()
206 {
207   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
208
209   Shader shader = GenerateShader();
210
211   mImpl->mRenderer = VisualRenderer::New(geometry, shader);
212   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
213
214   mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
215
216   if(!EqualsZero(mBlurRadius))
217   {
218     mBlurRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
219     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
220   }
221
222   // Register transform properties
223   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
224 }
225
226 Shader ColorVisual::GenerateShader() const
227 {
228   Shader                         shader;
229   VisualFactoryCache::ShaderType shaderType;
230
231   bool roundedCorner  = IsRoundedCornerRequired();
232   bool borderline     = IsBorderlineRequired();
233   bool blur           = !EqualsZero(mBlurRadius) || mAlwaysUsingBlurRadius;
234   int  shaderTypeFlag = ColorVisualRequireFlag::DEFAULT;
235
236   if(roundedCorner)
237   {
238     shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER;
239   }
240   if(blur)
241   {
242     // If we use blur, just ignore borderline
243     borderline = false;
244     shaderTypeFlag |= ColorVisualRequireFlag::BLUR;
245   }
246   if(borderline)
247   {
248     shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE;
249   }
250
251   shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
252   shader     = mFactoryCache.GetShader(shaderType);
253   if(!shader)
254   {
255     std::string vertexShaderPrefixList;
256     std::string fragmentShaderPrefixList;
257     if(roundedCorner)
258     {
259       vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
260       fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
261     }
262     if(blur)
263     {
264       vertexShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
265       fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
266     }
267     if(borderline)
268     {
269       vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
270       fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
271     }
272     shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_VERT.data(),
273                          Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_FRAG.data());
274     mFactoryCache.SaveShader(shaderType, shader);
275   }
276
277   return shader;
278 }
279
280 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
281 {
282   if(!mImpl->mRenderer)
283   {
284     Handle handle;
285     return Dali::Property(handle, Property::INVALID_INDEX);
286   }
287
288   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
289   {
290     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
291
292     // Blur is animated now. we always have to use blur feature.
293     mAlwaysUsingBlurRadius = true;
294
295     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
296
297     // Change shader
298     UpdateShader();
299
300     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
301   }
302
303   Handle handle;
304   return Dali::Property(handle, Property::INVALID_INDEX);
305 }
306
307 } // namespace Internal
308
309 } // namespace Toolkit
310
311 } // namespace Dali