2 * Copyright (c) 2021 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/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>
43 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[6] =
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,
53 // enum of required list when we select shader
54 enum ColorVisualRequireFlag
57 ROUNDED_CORNER = 1 << 0,
61 } // unnamed namespace
62 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
64 ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
65 colorVisualPtr->SetProperties(properties);
66 colorVisualPtr->Initialize();
67 return colorVisualPtr;
70 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
71 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
73 mBlurRadiusIndex(Property::INVALID_INDEX),
74 mAlwaysUsingBlurRadius(false)
78 ColorVisual::~ColorVisual()
82 void ColorVisual::DoSetProperties(const Property::Map& propertyMap)
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);
90 if(colorValue->Get(color))
92 Property::Type type = colorValue->GetType();
93 if(type == Property::VECTOR4)
97 else if(type == Property::VECTOR3)
99 Vector3 color3(color);
105 DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
109 Property::Value* blurRadiusValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME);
112 if(!blurRadiusValue->Get(mBlurRadius))
114 DALI_LOG_ERROR("ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType());
119 void ColorVisual::DoSetOnScene(Actor& actor)
121 actor.AddRenderer(mImpl->mRenderer);
123 // Color Visual generated and ready to display
124 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
127 void ColorVisual::DoSetOffScene(Actor& actor)
129 actor.RemoveRenderer(mImpl->mRenderer);
132 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
135 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
136 map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
138 if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
140 // Update values from Renderer
141 float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
142 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
146 map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
150 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
155 void ColorVisual::OnSetTransform()
159 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
163 void ColorVisual::UpdateShader()
167 Shader shader = GenerateShader();
168 mImpl->mRenderer.SetShader(shader);
172 void ColorVisual::OnInitialize()
174 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
176 Shader shader = GenerateShader();
178 mImpl->mRenderer = Renderer::New(geometry, shader);
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));
185 if(!EqualsZero(mBlurRadius))
187 mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
188 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
191 // Register transform properties
192 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
195 Shader ColorVisual::GenerateShader() const
198 VisualFactoryCache::ShaderType shaderType;
200 bool roundedCorner = IsRoundedCornerRequired();
201 bool borderline = IsBorderlineRequired();
202 bool blur = !EqualsZero(mBlurRadius) || mAlwaysUsingBlurRadius;
203 int shaderTypeFlag = ColorVisualRequireFlag::DEFAULT;
207 shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER;
211 // If we use blur, just ignore borderline
213 shaderTypeFlag |= ColorVisualRequireFlag::BLUR;
217 shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE;
220 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
221 shader = mFactoryCache.GetShader(shaderType);
224 std::string vertexShaderPrefixList;
225 std::string fragmentShaderPrefixList;
228 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
229 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
233 vertexShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
234 fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR 1\n";
238 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
239 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
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);
249 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
251 if(!mImpl->mRenderer)
254 return Dali::Property(handle, Property::INVALID_INDEX);
257 if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
259 mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
261 // Blur is animated now. we always have to use blur feature.
262 mAlwaysUsingBlurRadius = true;
264 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
269 return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
273 return Dali::Property(handle, Property::INVALID_INDEX);
276 } // namespace Internal
278 } // namespace Toolkit