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>
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>
44 const int CUSTOM_PROPERTY_COUNT(12); // 5 transform properties + Blur Radius + Mix Color + border/corner
46 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[6] =
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,
56 // enum of required list when we select shader
57 enum ColorVisualRequireFlag
60 ROUNDED_CORNER = 1 << 0,
64 } // unnamed namespace
65 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
67 ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
68 colorVisualPtr->SetProperties(properties);
69 colorVisualPtr->Initialize();
70 return colorVisualPtr;
73 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
74 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
76 mBlurRadiusIndex(Property::INVALID_INDEX),
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(mBlurRadiusIndex != Property::INVALID_INDEX)
122 mImpl->mRenderer.SetProperty(mBlurRadiusIndex, mBlurRadius);
124 else if(DALI_UNLIKELY(mImpl->mRenderer && (!EqualsZero(mBlurRadius) || mAlwaysUsingBlurRadius)))
126 // Unusual case. SetProperty called after OnInitialize().
127 // Assume that DoAction call UPDATE_PROPERTY.
128 // We must regist properies into renderer, and update shader.
130 // BlurRadius added by this action. Regist property to renderer.
131 mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
132 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
134 // Change the shader must not be occured many times. we always have to use blur feature.
135 mAlwaysUsingBlurRadius = true;
143 void ColorVisual::DoSetOnScene(Actor& actor)
145 actor.AddRenderer(mImpl->mRenderer);
147 // Color Visual generated and ready to display
148 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
151 void ColorVisual::DoSetOffScene(Actor& actor)
153 actor.RemoveRenderer(mImpl->mRenderer);
156 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
159 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
160 map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
162 if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
164 // Update values from Renderer
165 float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
166 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
170 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
174 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
179 void ColorVisual::OnSetTransform()
183 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
187 void ColorVisual::UpdateShader()
191 Shader shader = GenerateShader();
192 mImpl->mRenderer.SetShader(shader);
196 void ColorVisual::OnInitialize()
198 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
200 Shader shader = GenerateShader();
202 mImpl->mRenderer = Renderer::New(geometry, shader);
203 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
205 // ColorVisual has it's own index key for mix color - use this instead
206 // of using the new base index to avoid changing existing applications
207 // String keys will get to this property.
208 mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
210 if(!EqualsZero(mBlurRadius))
212 mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
213 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
216 // Register transform properties
217 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
220 Shader ColorVisual::GenerateShader() const
223 VisualFactoryCache::ShaderType shaderType;
225 bool roundedCorner = IsRoundedCornerRequired();
226 bool borderline = IsBorderlineRequired();
227 bool blur = !EqualsZero(mBlurRadius) || mAlwaysUsingBlurRadius;
228 int shaderTypeFlag = ColorVisualRequireFlag::DEFAULT;
232 shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER;
236 // If we use blur, just ignore borderline
238 shaderTypeFlag |= ColorVisualRequireFlag::BLUR;
242 shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE;
245 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
246 shader = mFactoryCache.GetShader(shaderType);
249 std::string vertexShaderPrefixList;
250 std::string fragmentShaderPrefixList;
253 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
254 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
258 vertexShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
259 fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
263 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
264 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
266 shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_VERT.data(),
267 Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_FRAG.data());
268 mFactoryCache.SaveShader(shaderType, shader);
274 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
276 if(!mImpl->mRenderer)
279 return Dali::Property(handle, Property::INVALID_INDEX);
282 if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
284 mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
286 // Blur is animated now. we always have to use blur feature.
287 mAlwaysUsingBlurRadius = true;
289 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
294 return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
298 return Dali::Property(handle, Property::INVALID_INDEX);
301 } // namespace Internal
303 } // namespace Toolkit