2 * Copyright (c) 2022 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,
65 } // unnamed namespace
66 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
68 ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
69 colorVisualPtr->SetProperties(properties);
70 colorVisualPtr->Initialize();
71 return colorVisualPtr;
74 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
75 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
77 mAlwaysUsingBlurRadius(false)
81 ColorVisual::~ColorVisual()
85 void ColorVisual::DoSetProperties(const Property::Map& propertyMap)
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);
93 if(colorValue->Get(color))
95 Property::Type type = colorValue->GetType();
96 if(type == Property::VECTOR4)
100 else if(type == Property::VECTOR3)
102 Vector3 color3(color);
108 DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
112 Property::Value* blurRadiusValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME);
115 if(!blurRadiusValue->Get(mBlurRadius))
117 DALI_LOG_ERROR("ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType());
120 if(DALI_UNLIKELY(mImpl->mRenderer))
122 // Unusual case. SetProperty called after OnInitialize().
123 // Assume that DoAction call UPDATE_PROPERTY.
124 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBlurRadiusUniform();
125 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BLUR_RADIUS, mBlurRadius);
127 // Check whether we must update shader.
128 if(!mAlwaysUsingBlurRadius && IsBlurRequired())
130 // Change the shader must not be occured many times. we always have to use blur feature.
131 mAlwaysUsingBlurRadius = true;
133 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
135 if(!mImpl->mCustomShader)
144 void ColorVisual::DoSetOnScene(Actor& actor)
146 actor.AddRenderer(mImpl->mRenderer);
148 // Color Visual generated and ready to display
149 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
152 void ColorVisual::DoSetOffScene(Actor& actor)
154 actor.RemoveRenderer(mImpl->mRenderer);
157 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
160 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
161 map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
165 // Update values from Renderer
166 float blurRadius = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BLUR_RADIUS);
167 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
171 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
175 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
180 void ColorVisual::EnablePreMultipliedAlpha(bool preMultiplied)
182 // Make always disable pre multiplied alpha whether preMultiplied value is true.
185 DALI_LOG_WARNING("Note : ColorVisual cannot enable PreMultipliedAlpha\n");
189 void ColorVisual::OnSetTransform()
193 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
197 void ColorVisual::UpdateShader()
201 Shader shader = GenerateShader();
202 mImpl->mRenderer.SetShader(shader);
206 void ColorVisual::OnInitialize()
208 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
210 Shader shader = GenerateShader();
212 mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
213 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
215 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
217 if(mAlwaysUsingBlurRadius || !EqualsZero(mBlurRadius))
219 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBlurRadiusUniform();
220 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BLUR_RADIUS, mBlurRadius);
221 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
224 // Register transform properties
225 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
228 Shader ColorVisual::GenerateShader() const
231 VisualFactoryCache::ShaderType shaderType;
233 bool roundedCorner = IsRoundedCornerRequired();
234 bool borderline = IsBorderlineRequired();
235 bool blur = IsBlurRequired();
236 int shaderTypeFlag = ColorVisualRequireFlag::DEFAULT;
240 shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER;
244 // If we use blur, just ignore borderline
246 shaderTypeFlag |= ColorVisualRequireFlag::BLUR;
250 shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE;
253 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
254 shader = mFactoryCache.GetShader(shaderType);
257 std::string vertexShaderPrefixList;
258 std::string fragmentShaderPrefixList;
261 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
262 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
266 vertexShaderPrefixList += "#define IS_REQUIRED_BLUR\n";
267 fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR\n";
271 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
272 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
274 shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_VERT.data(),
275 Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_FRAG.data());
276 mFactoryCache.SaveShader(shaderType, shader);
282 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
284 if(!mImpl->mRenderer)
287 return Dali::Property(handle, Property::INVALID_INDEX);
290 if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
292 const bool updateShader = !mImpl->mCustomShader && !IsBlurRequired();
294 // Blur is animated now. we always have to use blur feature.
295 mAlwaysUsingBlurRadius = true;
299 // Update each values to renderer
300 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBlurRadiusUniform();
301 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BLUR_RADIUS, mBlurRadius);
306 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
307 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::BLUR_RADIUS);
311 return Dali::Property(handle, Property::INVALID_INDEX);
314 bool ColorVisual::IsBlurRequired() const
316 float blurRadius = mBlurRadius;
319 // Update values from Renderer
320 blurRadius = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BLUR_RADIUS);
322 return mAlwaysUsingBlurRadius || !EqualsZero(blurRadius);
325 } // namespace Internal
327 } // namespace Toolkit