Merge branch 'devel/master' into devel/graphics
[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 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
43 {
44   ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
45   colorVisualPtr->SetProperties(properties);
46   colorVisualPtr->Initialize();
47   return colorVisualPtr;
48 }
49
50 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
51 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
52   mBlurRadius(0.0f),
53   mBlurRadiusIndex(Property::INVALID_INDEX),
54   mRenderIfTransparent(false),
55   mNeedBlurRadius(false)
56 {
57 }
58
59 ColorVisual::~ColorVisual()
60 {
61 }
62
63 void ColorVisual::DoSetProperties(const Property::Map& propertyMap)
64 {
65   // By virtue of DoSetProperties being called last, this will override
66   // anything set by Toolkit::Visual::Property::MIX_COLOR
67   Property::Value* colorValue = propertyMap.Find(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR);
68   if(colorValue)
69   {
70     Vector4 color;
71     if(colorValue->Get(color))
72     {
73       Property::Type type = colorValue->GetType();
74       if(type == Property::VECTOR4)
75       {
76         SetMixColor(color);
77       }
78       else if(type == Property::VECTOR3)
79       {
80         Vector3 color3(color);
81         SetMixColor(color3);
82       }
83     }
84     else
85     {
86       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
87     }
88   }
89
90   Property::Value* renderIfTransparentValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME);
91   if(renderIfTransparentValue)
92   {
93     if(!renderIfTransparentValue->Get(mRenderIfTransparent))
94     {
95       DALI_LOG_ERROR("ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType());
96     }
97   }
98
99   Property::Value* blurRadiusValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME);
100   if(blurRadiusValue)
101   {
102     if(!blurRadiusValue->Get(mBlurRadius))
103     {
104       DALI_LOG_ERROR("ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType());
105     }
106   }
107 }
108
109 void ColorVisual::DoSetOnScene(Actor& actor)
110 {
111   // Only add the renderer if it's not fully transparent
112   // We cannot avoid creating a renderer as it's used in the base class
113   if(mRenderIfTransparent || mImpl->mMixColor.a > 0.0f)
114   {
115     actor.AddRenderer(mImpl->mRenderer);
116   }
117
118   // Color Visual generated and ready to display
119   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
120 }
121
122 void ColorVisual::DoSetOffScene(Actor& actor)
123 {
124   actor.RemoveRenderer(mImpl->mRenderer);
125 }
126
127 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
128 {
129   map.Clear();
130   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
131   map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
132   map.Insert(Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent);
133
134   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
135   {
136     // Update values from Renderer
137     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
138     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
139   }
140   else
141   {
142     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
143   }
144 }
145
146 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
147 {
148   // Do nothing
149 }
150
151 void ColorVisual::OnSetTransform()
152 {
153   if(mImpl->mRenderer)
154   {
155     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
156   }
157 }
158
159 void ColorVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
160 {
161   // Check if action is valid for this visual type and perform action if possible
162   switch(actionId)
163   {
164     case DevelColorVisual::Action::UPDATE_PROPERTY:
165     {
166       const Property::Map* map = attributes.GetMap();
167       if(map)
168       {
169         DoSetProperties(*map);
170       }
171       break;
172     }
173   }
174 }
175
176 void ColorVisual::UpdateShader()
177 {
178   if(mImpl->mRenderer)
179   {
180     Shader shader = GetShader();
181     mImpl->mRenderer.SetShader(shader);
182   }
183 }
184
185 void ColorVisual::OnInitialize()
186 {
187   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
188
189   Shader shader = GetShader();
190
191   mImpl->mRenderer = Renderer::New(geometry, shader);
192
193   // ColorVisual has it's own index key for mix color - use this instead
194   // of using the new base index to avoid changing existing applications
195   // String keys will get to this property.
196   mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
197
198   if(!EqualsZero(mBlurRadius))
199   {
200     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
201     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
202   }
203
204   // Register transform properties
205   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
206 }
207
208 Shader ColorVisual::GetShader()
209 {
210   Shader shader;
211   if(!EqualsZero(mBlurRadius) || mNeedBlurRadius)
212   {
213     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER_BLUR_EDGE);
214     if(!shader)
215     {
216       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_FRAG.data());
217       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader);
218     }
219   }
220   else if(!IsRoundedCornerRequired())
221   {
222     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER);
223     if(!shader)
224     {
225       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_FRAG.data());
226       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER, shader);
227     }
228   }
229   else
230   {
231     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER);
232     if(!shader)
233     {
234       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_FRAG.data());
235       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader);
236     }
237   }
238
239   return shader;
240 }
241
242 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
243 {
244   if(!mImpl->mRenderer)
245   {
246     Handle handle;
247     return Dali::Property(handle, Property::INVALID_INDEX);
248   }
249
250   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
251   {
252     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
253
254     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
255
256     mNeedBlurRadius = true;
257
258     // Change shader
259     UpdateShader();
260
261     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
262   }
263
264   Handle handle;
265   return Dali::Property(handle, Property::INVALID_INDEX);
266 }
267
268 } // namespace Internal
269
270 } // namespace Toolkit
271
272 } // namespace Dali