2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "color-visual.h"
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>
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>
45 const int CUSTOM_PROPERTY_COUNT(0);
47 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[6] =
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,
57 // enum of required list when we select shader
58 enum ColorVisualRequireFlag
61 ROUNDED_CORNER = 1 << 0,
66 constexpr uint32_t MINIMUM_SHADER_VERSION_SUPPORT_ROUNDED_BLUR = 300;
67 } // unnamed namespace
68 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
70 ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
71 colorVisualPtr->SetProperties(properties);
72 colorVisualPtr->Initialize();
73 return colorVisualPtr;
76 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
77 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
79 mAlwaysUsingBlurRadius(false)
83 ColorVisual::~ColorVisual()
87 void ColorVisual::DoSetProperties(const Property::Map& propertyMap)
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);
95 if(colorValue->Get(color))
97 Property::Type type = colorValue->GetType();
98 if(type == Property::VECTOR4)
102 else if(type == Property::VECTOR3)
104 Vector3 color3(color);
110 DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
114 Property::Value* blurRadiusValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME);
117 if(!blurRadiusValue->Get(mBlurRadius))
119 DALI_LOG_ERROR("ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType());
122 if(DALI_UNLIKELY(mImpl->mRenderer))
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);
129 // Check whether we must update shader.
130 if(!mAlwaysUsingBlurRadius && IsBlurRequired())
132 // Change the shader must not be occured many times. we always have to use blur feature.
133 mAlwaysUsingBlurRadius = true;
135 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
137 if(!mImpl->mCustomShader)
146 void ColorVisual::DoSetOnScene(Actor& actor)
148 actor.AddRenderer(mImpl->mRenderer);
150 // Color Visual generated and ready to display
151 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
154 void ColorVisual::DoSetOffScene(Actor& actor)
156 actor.RemoveRenderer(mImpl->mRenderer);
159 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
162 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
163 map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
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);
173 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
177 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
182 void ColorVisual::EnablePreMultipliedAlpha(bool preMultiplied)
184 // Make always disable pre multiplied alpha whether preMultiplied value is true.
187 DALI_LOG_WARNING("Note : ColorVisual cannot enable PreMultipliedAlpha\n");
191 void ColorVisual::OnSetTransform()
195 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
199 void ColorVisual::UpdateShader()
203 Shader shader = GenerateShader();
204 mImpl->mRenderer.SetShader(shader);
208 void ColorVisual::OnInitialize()
210 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
212 Shader shader = GenerateShader();
214 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
215 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
217 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
219 if(mAlwaysUsingBlurRadius || !EqualsZero(mBlurRadius))
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);
226 // Register transform properties
227 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
230 Shader ColorVisual::GenerateShader() const
233 VisualFactoryCache::ShaderType shaderType;
235 bool roundedCorner = IsRoundedCornerRequired();
236 bool borderline = IsBorderlineRequired();
237 bool blur = IsBlurRequired();
238 int shaderTypeFlag = ColorVisualRequireFlag::DEFAULT;
242 // If we use blur, just ignore borderline
244 shaderTypeFlag |= ColorVisualRequireFlag::BLUR;
248 shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER;
252 shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE;
255 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
256 shader = mFactoryCache.GetShader(shaderType);
259 std::string vertexShaderPrefixList;
260 std::string fragmentShaderPrefixList;
263 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
264 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
268 vertexShaderPrefixList += "#define IS_REQUIRED_BLUR\n";
269 fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR\n";
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))
274 fragmentShaderPrefixList += "#define SL_VERSION_LOW\n";
279 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
280 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
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());
291 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
293 if(!mImpl->mRenderer)
296 return Dali::Property(handle, Property::INVALID_INDEX);
299 if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
301 const bool updateShader = !mImpl->mCustomShader && !IsBlurRequired();
303 // Blur is animated now. we always have to use blur feature.
304 mAlwaysUsingBlurRadius = true;
308 // Update each values to renderer
309 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBlurRadiusUniform();
310 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BLUR_RADIUS, mBlurRadius);
315 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
316 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::BLUR_RADIUS);
320 return Dali::Property(handle, Property::INVALID_INDEX);
323 bool ColorVisual::IsBlurRequired() const
325 float blurRadius = mBlurRadius;
328 // Update values from Renderer
329 blurRadius = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BLUR_RADIUS);
331 return mAlwaysUsingBlurRadius || !EqualsZero(blurRadius);
334 } // namespace Internal
336 } // namespace Toolkit