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