From e7ad51802b51bc6951080e95e8c009264f0dee1b Mon Sep 17 00:00:00 2001 From: sunghyun kim Date: Wed, 31 Jul 2024 17:41:37 +0900 Subject: [PATCH] Refactoring visual-shader-factory Change-Id: I3b1d9c4ba1aa45cb9c11192121b2fb286b002b6c --- .../utc-Dali-Visuals-internal.cpp | 2 +- dali-toolkit/devel-api/file.list | 1 + .../visual-shader-factory-interface.h | 51 ++++ dali-toolkit/internal/file.list | 1 + .../color/color-visual-shader-factory.cpp | 236 ++++++++++++++++++ .../color/color-visual-shader-factory.h | 172 +++++++++++++ .../internal/visuals/color/color-visual.cpp | 105 +------- .../internal/visuals/color/color-visual.h | 8 +- .../image/image-visual-shader-factory.cpp | 18 +- .../image/image-visual-shader-factory.h | 11 +- .../image-visual-shader-feature-builder.cpp | 10 +- .../image-visual-shader-feature-builder.h | 10 +- .../text/text-visual-shader-factory.cpp | 112 +++++---- .../visuals/text/text-visual-shader-factory.h | 26 +- .../internal/visuals/text/text-visual.cpp | 5 +- .../internal/visuals/text/text-visual.h | 2 +- .../internal/visuals/visual-factory-impl.cpp | 55 ++-- .../internal/visuals/visual-factory-impl.h | 13 +- 18 files changed, 610 insertions(+), 228 deletions(-) create mode 100644 dali-toolkit/devel-api/visual-factory/visual-shader-factory-interface.h create mode 100644 dali-toolkit/internal/visuals/color/color-visual-shader-factory.cpp create mode 100644 dali-toolkit/internal/visuals/color/color-visual-shader-factory.h diff --git a/automated-tests/src/dali-toolkit-internal/utc-Dali-Visuals-internal.cpp b/automated-tests/src/dali-toolkit-internal/utc-Dali-Visuals-internal.cpp index 84d1358e0b..40ae0b80f2 100644 --- a/automated-tests/src/dali-toolkit-internal/utc-Dali-Visuals-internal.cpp +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-Visuals-internal.cpp @@ -205,7 +205,7 @@ int UtcDaliVisualSetProperties(void) Property::Map propertyMap1; propertyMap1.Insert(Visual::Property::TYPE, Visual::COLOR); propertyMap1.Insert(ColorVisual::Property::MIX_COLOR, Color::RED); - Toolkit::Internal::ColorVisualPtr colorVisualPtr = Toolkit::Internal::ColorVisual::New(*factoryCache, propertyMap1); + Toolkit::Internal::DummyVisualPtr colorVisualPtr = Toolkit::Internal::DummyVisual::New(propertyMap1); DummyControl dummyControl = DummyControl::New(true); Impl::DummyControl& dummyImpl = static_cast(dummyControl.GetImplementation()); diff --git a/dali-toolkit/devel-api/file.list b/dali-toolkit/devel-api/file.list index f8a9d42a31..b37f8a8f81 100755 --- a/dali-toolkit/devel-api/file.list +++ b/dali-toolkit/devel-api/file.list @@ -183,6 +183,7 @@ SET( devel_api_visual_factory_header_files ${devel_api_src_dir}/visual-factory/transition-data.h ${devel_api_src_dir}/visual-factory/visual-factory.h ${devel_api_src_dir}/visual-factory/visual-base.h + ${devel_api_src_dir}/visual-factory/visual-shader-factory-interface.h ) SET( devel_api_visuals_header_files diff --git a/dali-toolkit/devel-api/visual-factory/visual-shader-factory-interface.h b/dali-toolkit/devel-api/visual-factory/visual-shader-factory-interface.h new file mode 100644 index 0000000000..875d26a4af --- /dev/null +++ b/dali-toolkit/devel-api/visual-factory/visual-shader-factory-interface.h @@ -0,0 +1,51 @@ +#ifndef DALI_TOOLKIT_VISUAL_SHADER_FACTORY_INTERFACE_H +#define DALI_TOOLKIT_VISUAL_SHADER_FACTORY_INTERFACE_H + +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ +namespace Toolkit +{ + +/** + * @brief The VisualShaderFactoryInterface class provides a factory interface for creating visual shader + */ +class DALI_TOOLKIT_API VisualShaderFactoryInterface +{ +public: + VisualShaderFactoryInterface() = default; + virtual ~VisualShaderFactoryInterface() = default; + + /** + * @brief Get precompiled shader for precompile + * @param[out] shaders shaderList for precompile + */ + virtual void GetPreCompiledShader(RawShaderData& shaders) = 0; +}; + +} // namespace Toolkit +} // namespace Dali + +#endif // DALI_TOOLKIT_VISUAL_SHADER_FACTORY_INTERFACE_H diff --git a/dali-toolkit/internal/file.list b/dali-toolkit/internal/file.list index 8048a5dedc..735b3a4d9c 100644 --- a/dali-toolkit/internal/file.list +++ b/dali-toolkit/internal/file.list @@ -35,6 +35,7 @@ SET( toolkit_src_files ${toolkit_src_dir}/visuals/animated-vector-image/vector-animation-thread.cpp ${toolkit_src_dir}/visuals/arc/arc-visual.cpp ${toolkit_src_dir}/visuals/border/border-visual.cpp + ${toolkit_src_dir}/visuals/color/color-visual-shader-factory.cpp ${toolkit_src_dir}/visuals/color/color-visual.cpp ${toolkit_src_dir}/visuals/gradient/gradient-visual.cpp ${toolkit_src_dir}/visuals/gradient/gradient.cpp diff --git a/dali-toolkit/internal/visuals/color/color-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/color/color-visual-shader-factory.cpp new file mode 100644 index 0000000000..a79def5ca1 --- /dev/null +++ b/dali-toolkit/internal/visuals/color/color-visual-shader-factory.cpp @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// CLASS HEADER +#include + +// INTERNAL INCLUDES +#include +#include +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace Internal +{ + +namespace +{ + +constexpr VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] = { + VisualFactoryCache::COLOR_SHADER, + VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, + VisualFactoryCache::COLOR_SHADER_BORDERLINE, + VisualFactoryCache::COLOR_SHADER_ROUNDED_BORDERLINE, + VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, + VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER_BLUR_EDGE, +}; + +constexpr VisualFactoryCache::ShaderType SHADER_TYPE_WITH_CUTOUT_TABLE[] = { + VisualFactoryCache::COLOR_SHADER_CUTOUT, + VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_CORNER, + VisualFactoryCache::COLOR_SHADER_CUTOUT_BORDERLINE, + VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_BORDERLINE, + VisualFactoryCache::COLOR_SHADER_CUTOUT_BLUR_EDGE, + VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_CORNER_BLUR_EDGE, +}; + +// enum of required list when we select shader +enum ColorVisualRequireFlag +{ + DEFAULT = 0, + ROUNDED_CORNER = 1 << 0, + BORDERLINE = 1 << 1, + BLUR = 1 << 2, +}; + +constexpr uint32_t MINIMUM_SHADER_VERSION_SUPPORT_ROUNDED_BLUR = 300; + +static constexpr auto PREDEFINED_SHADER_TYPE_COUNT = 2u; + +constexpr std::string_view VertexPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ + "", //VisualFactoryCache::COLOR_SHADER + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER +}; +constexpr std::string_view FragmentPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ + "", //VisualFactoryCache::COLOR_SHADER + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER +}; +constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[PREDEFINED_SHADER_TYPE_COUNT]{ + VisualFactoryCache::ShaderType::COLOR_SHADER, + VisualFactoryCache::ShaderType::COLOR_SHADER_ROUNDED_CORNER, +}; +} + +ColorVisualShaderFeatureBuilder::ColorVisualShaderFeatureBuilder() +: mColorRoundCorner(ColorVisualShaderFeature::RoundedCorner::DISABLED), + mColorBorderline(ColorVisualShaderFeature::Borderline::DISABLED), + mColorBlur(ColorVisualShaderFeature::Blur::DISABLED), + mColorCutout(ColorVisualShaderFeature::Cutout::DISABLED) +{ +} + +ColorVisualShaderFeatureBuilder& ColorVisualShaderFeatureBuilder::EnableRoundCorner(bool enableRoundedCorner) +{ + mColorRoundCorner = (enableRoundedCorner ? ColorVisualShaderFeature::RoundedCorner::ENABLED : ColorVisualShaderFeature::RoundedCorner::DISABLED); + return *this; +} + +ColorVisualShaderFeatureBuilder& ColorVisualShaderFeatureBuilder::EnableBorderLine(bool enableBorderLine) +{ + mColorBorderline = (enableBorderLine ? ColorVisualShaderFeature::Borderline::ENABLED : ColorVisualShaderFeature::Borderline::DISABLED); + return *this; +} + +ColorVisualShaderFeatureBuilder& ColorVisualShaderFeatureBuilder::EnableBlur(bool enableBlur) +{ + mColorBlur = (enableBlur ? ColorVisualShaderFeature::Blur::ENABLED : ColorVisualShaderFeature::Blur::DISABLED); + return *this; +} + +ColorVisualShaderFeatureBuilder& ColorVisualShaderFeatureBuilder::EnableCutout(bool enableCutout) +{ + mColorCutout = (enableCutout ? ColorVisualShaderFeature::Cutout::ENABLED : ColorVisualShaderFeature::Cutout::DISABLED); + return *this; +} + +VisualFactoryCache::ShaderType ColorVisualShaderFeatureBuilder::GetShaderType() const +{ + VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::COLOR_SHADER; + uint32_t shaderTypeFlag = ColorVisualRequireFlag::DEFAULT; + if(mColorBlur) + { + shaderTypeFlag |= ColorVisualRequireFlag::BLUR; + } + if(mColorRoundCorner) + { + shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER; + } + if(mColorBorderline && !mColorBlur) + { + shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE; + } + + shaderType = mColorCutout ? SHADER_TYPE_WITH_CUTOUT_TABLE[shaderTypeFlag] : SHADER_TYPE_TABLE[shaderTypeFlag]; + + return shaderType; +} + +void ColorVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const +{ + if(mColorRoundCorner == ColorVisualShaderFeature::RoundedCorner::ENABLED) + { + vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n"; + } + if(mColorBlur == ColorVisualShaderFeature::Blur::ENABLED) + { + vertexShaderPrefixList += "#define IS_REQUIRED_BLUR\n"; + } + if(mColorBorderline == ColorVisualShaderFeature::Borderline::ENABLED && mColorBlur == ColorVisualShaderFeature::RoundedCorner::DISABLED) + { + vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n"; + } + if(mColorCutout == ColorVisualShaderFeature::Cutout::ENABLED) + { + vertexShaderPrefixList += "#define IS_REQUIRED_CUTOUT\n"; + } +} + +void ColorVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const +{ + if(mColorRoundCorner == ColorVisualShaderFeature::RoundedCorner::ENABLED) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n"; + } + if(mColorBlur == ColorVisualShaderFeature::Blur::ENABLED) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR\n"; + // If shader version doesn't support latest blur with corner radius, Let we use legacy code. + if(DALI_UNLIKELY(Dali::Shader::GetShaderLanguageVersion() < MINIMUM_SHADER_VERSION_SUPPORT_ROUNDED_BLUR)) + { + fragmentShaderPrefixList += "#define SL_VERSION_LOW\n"; + } + } + if(mColorBorderline == ColorVisualShaderFeature::Borderline::ENABLED && mColorBlur == ColorVisualShaderFeature::RoundedCorner::DISABLED) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n"; + } + if(mColorCutout == ColorVisualShaderFeature::Cutout::ENABLED) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_CUTOUT\n"; + } +} + +ColorVisualShaderFactory::ColorVisualShaderFactory() +{ +} + +ColorVisualShaderFactory::~ColorVisualShaderFactory() +{ +} + +Shader ColorVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ColorVisualShaderFeatureBuilder& featureBuilder) +{ + Shader shader; + VisualFactoryCache::ShaderType shaderType = featureBuilder.GetShaderType(); + shader = factoryCache.GetShader(shaderType); + + if(!shader) + { + std::string vertexShaderPrefixList; + std::string fragmentShaderPrefixList; + featureBuilder.GetVertexShaderPrefixList(vertexShaderPrefixList); + featureBuilder.GetFragmentShaderPrefixList(fragmentShaderPrefixList); + + std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_VERT.data()); + std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_FRAG.data()); + + shader = factoryCache.GenerateAndSaveShader(shaderType, vertexShader, fragmentShader); + } + return shader; + +} + +void ColorVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) +{ + std::vector vertexPrefix; + std::vector fragmentPrefix; + std::vector shaderName; + int shaderCount = 0; + shaders.shaderCount = 0; + for(uint32_t i = 0u; i < PREDEFINED_SHADER_TYPE_COUNT; ++i) + { + vertexPrefix.push_back(VertexPredefines[i]); + fragmentPrefix.push_back(FragmentPredefines[i]); + shaderName.push_back(Scripting::GetLinearEnumerationName(ShaderTypePredefines[i], VISUAL_SHADER_TYPE_TABLE, VISUAL_SHADER_TYPE_TABLE_COUNT)); + shaderCount++; + } + + shaders.vertexPrefix = std::move(vertexPrefix); + shaders.fragmentPrefix = std::move(fragmentPrefix); + shaders.shaderName = std::move(shaderName); + shaders.vertexShader = SHADER_COLOR_VISUAL_SHADER_VERT; + shaders.fragmentShader = SHADER_COLOR_VISUAL_SHADER_FRAG; + shaders.shaderCount = shaderCount; +} + +} // namespace Internal + +} // namespace Toolkit + +} // namespace Dali diff --git a/dali-toolkit/internal/visuals/color/color-visual-shader-factory.h b/dali-toolkit/internal/visuals/color/color-visual-shader-factory.h new file mode 100644 index 0000000000..f55ea452e7 --- /dev/null +++ b/dali-toolkit/internal/visuals/color/color-visual-shader-factory.h @@ -0,0 +1,172 @@ +#ifndef DALI_TOOLKIT_COLOR_VISUAL_SHADER_FACTORY_H +#define DALI_TOOLKIT_COLOR_VISUAL_SHADER_FACTORY_H + +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace Internal +{ + +namespace ColorVisualShaderFeature +{ +namespace RoundedCorner +{ +/** + * @brief Whether use RoundedCorner, or not + */ +enum Type +{ + DISABLED = 0, ///< Color visual doesn't use rounded corner + ENABLED ///< Color visual uses rounded corner +}; +} // namespace RoundedCorner + +namespace Blur +{ +/** + * @brief Whether use Blur, or not + */ +enum Type +{ + DISABLED = 0, ///< Color visual doesn't use blur + ENABLED ///< Color visual uses blur +}; +} // namespace Blur + +namespace Borderline +{ +/** + * @brief Whether use Borderline + */ +enum Type +{ + DISABLED = 0, /// Color visual doesn't use Borderline + ENABLED /// Color visual uses Borderline +}; +} // namespace Borderline + +namespace Cutout +{ +/** + * @brief Whether use Cutout, or not + */ +enum Type +{ + DISABLED = 0, ///< Color visual doesn't use Cutout + ENABLED ///< Color visual uses Cutout +}; +} // namespace Cutout +} // namespace ColorVisualShaderFeature + +class ColorVisualShaderFeatureBuilder +{ +public: + ColorVisualShaderFeatureBuilder(); + ColorVisualShaderFeatureBuilder& EnableRoundCorner(bool enableRoundCorner); + ColorVisualShaderFeatureBuilder& EnableBorderLine(bool enableBorderLine); + ColorVisualShaderFeatureBuilder& EnableBlur(bool enableBlur); + ColorVisualShaderFeatureBuilder& EnableCutout(bool enableCutout); + + VisualFactoryCache::ShaderType GetShaderType() const; + void GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const; + void GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const; + + bool IsEnabledRoundCorner() const + { + return mColorRoundCorner == ColorVisualShaderFeature::RoundedCorner::ENABLED; + } + bool IsEnabledBorderLine() const + { + return mColorBorderline == ColorVisualShaderFeature::Borderline::ENABLED; + } + bool IsEnabledBlur() const + { + return mColorBlur == ColorVisualShaderFeature::Blur::ENABLED; + } + bool IsEnabledCutout() const + { + return mColorCutout == ColorVisualShaderFeature::Cutout::ENABLED; + } + +private: + ColorVisualShaderFeature::RoundedCorner::Type mColorRoundCorner : 2; ///< Whether use rounded corner, or not. default as RoundedCorner::DISABLED + ColorVisualShaderFeature::Borderline::Type mColorBorderline : 2; ///< Whether use border line, or not. default as Borderline::DISABLED + ColorVisualShaderFeature::Blur::Type mColorBlur : 2; ///< Whether use blur, or not. default as Blur::DISABLED + ColorVisualShaderFeature::Cutout::Type mColorCutout : 2; ///< Whether use cutout, or not. default as Cutout::DISABLED +}; + +/** + * ColorVisualShaderFactory is an object that provides and shares shaders between color visuals + */ +class ColorVisualShaderFactory : public VisualShaderFactoryInterface +{ +public: + /** + * @brief Constructor + */ + ColorVisualShaderFactory(); + + /** + * @brief Destructor + */ + ~ColorVisualShaderFactory() override; + +public: + /** + * @brief Get the standard color rendering shader. + * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object + * @param[in] featureBuilder Collection of current text shader's features + * @return The standard text rendering shader with features. + */ + Shader GetShader(VisualFactoryCache& factoryCache, const ColorVisualShaderFeatureBuilder& featureBuilder); + +public: // Implementation of VisualShaderFactoryInterface + /** + * @copydoc Dali::Toolkit::VisualShaderFactoryInterface::GetPreCompiledShader + */ + void GetPreCompiledShader(RawShaderData& shaders) override; + +protected: + /** + * Undefined copy constructor. + */ + ColorVisualShaderFactory(const ColorVisualShaderFactory&) = delete; + + /** + * Undefined assignment operator. + */ + ColorVisualShaderFactory& operator=(const ColorVisualShaderFactory& rhs) = delete; +}; + +} // namespace Internal + +} // namespace Toolkit + +} // namespace Dali + +#endif // DALI_TOOLKIT_COLOR_VISUAL_SHADER_FACTORY_H diff --git a/dali-toolkit/internal/visuals/color/color-visual.cpp b/dali-toolkit/internal/visuals/color/color-visual.cpp index 435eac05e5..9434672330 100644 --- a/dali-toolkit/internal/visuals/color/color-visual.cpp +++ b/dali-toolkit/internal/visuals/color/color-visual.cpp @@ -51,49 +51,22 @@ DALI_ENUM_TO_STRING_TABLE_BEGIN(CUTOUT_POLICY) DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::DevelColorVisual::CutoutPolicy, CUTOUT_VIEW_WITH_CORNER_RADIUS) DALI_ENUM_TO_STRING_TABLE_END(CUTOUT_POLICY) -constexpr VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] = { - VisualFactoryCache::COLOR_SHADER, - VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, - VisualFactoryCache::COLOR_SHADER_BORDERLINE, - VisualFactoryCache::COLOR_SHADER_ROUNDED_BORDERLINE, - VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, - VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER_BLUR_EDGE, -}; - -constexpr VisualFactoryCache::ShaderType SHADER_TYPE_WITH_CUTOUT_TABLE[] = { - VisualFactoryCache::COLOR_SHADER_CUTOUT, - VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_CORNER, - VisualFactoryCache::COLOR_SHADER_CUTOUT_BORDERLINE, - VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_BORDERLINE, - VisualFactoryCache::COLOR_SHADER_CUTOUT_BLUR_EDGE, - VisualFactoryCache::COLOR_SHADER_CUTOUT_ROUNDED_CORNER_BLUR_EDGE, -}; - -// enum of required list when we select shader -enum ColorVisualRequireFlag -{ - DEFAULT = 0, - ROUNDED_CORNER = 1 << 0, - BORDERLINE = 1 << 1, - BLUR = 1 << 2, -}; - -constexpr uint32_t MINIMUM_SHADER_VERSION_SUPPORT_ROUNDED_BLUR = 300; } // unnamed namespace -ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties) +ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, ColorVisualShaderFactory& shaderFactory, const Property::Map& properties) { - ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache)); + ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache, shaderFactory)); colorVisualPtr->SetProperties(properties); colorVisualPtr->Initialize(); return colorVisualPtr; } -ColorVisual::ColorVisual(VisualFactoryCache& factoryCache) +ColorVisual::ColorVisual(VisualFactoryCache& factoryCache, ColorVisualShaderFactory& shaderFactory) : Visual::Base(factoryCache, Visual::FittingMode::DONT_CARE, Toolkit::Visual::COLOR), mBlurRadius(0.0f), mCutoutPolicy(DevelColorVisual::CutoutPolicy::NONE), - mAlwaysUsingBlurRadius(false) + mAlwaysUsingBlurRadius(false), + mColorVisualShaderFactory(shaderFactory) { } @@ -269,67 +242,13 @@ void ColorVisual::OnInitialize() Shader ColorVisual::GenerateShader() const { - Shader shader; - VisualFactoryCache::ShaderType shaderType; - - bool roundedCorner = IsRoundedCornerRequired(); - bool borderline = IsBorderlineRequired(); - bool blur = IsBlurRequired(); - bool cutout = IsCutoutRequired(); - int shaderTypeFlag = ColorVisualRequireFlag::DEFAULT; - - if(blur) - { - // If we use blur, just ignore borderline - borderline = false; - shaderTypeFlag |= ColorVisualRequireFlag::BLUR; - } - if(roundedCorner) - { - shaderTypeFlag |= ColorVisualRequireFlag::ROUNDED_CORNER; - } - if(borderline) - { - shaderTypeFlag |= ColorVisualRequireFlag::BORDERLINE; - } - - shaderType = cutout ? SHADER_TYPE_WITH_CUTOUT_TABLE[shaderTypeFlag] : SHADER_TYPE_TABLE[shaderTypeFlag]; - shader = mFactoryCache.GetShader(shaderType); - if(!shader) - { - std::string vertexShaderPrefixList; - std::string fragmentShaderPrefixList; - if(roundedCorner) - { - vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n"; - fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n"; - } - if(blur) - { - vertexShaderPrefixList += "#define IS_REQUIRED_BLUR\n"; - fragmentShaderPrefixList += "#define IS_REQUIRED_BLUR\n"; - - // If shader version doesn't support latest blur with corner radius, Let we use legacy code. - if(DALI_UNLIKELY(Dali::Shader::GetShaderLanguageVersion() < MINIMUM_SHADER_VERSION_SUPPORT_ROUNDED_BLUR)) - { - fragmentShaderPrefixList += "#define SL_VERSION_LOW\n"; - } - } - if(borderline) - { - vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n"; - fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n"; - } - if(cutout) - { - vertexShaderPrefixList += "#define IS_REQUIRED_CUTOUT\n"; - fragmentShaderPrefixList += "#define IS_REQUIRED_CUTOUT\n"; - } - - shader = mFactoryCache.GenerateAndSaveShader(shaderType, - Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_VERT.data(), - Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_COLOR_VISUAL_SHADER_FRAG.data()); - } + Shader shader = mColorVisualShaderFactory.GetShader( + mFactoryCache, + ColorVisualShaderFeatureBuilder() + .EnableBlur(IsBlurRequired()) + .EnableBorderLine(IsBorderlineRequired()) + .EnableRoundCorner(IsRoundedCornerRequired()) + .EnableCutout(IsCutoutRequired())); return shader; } diff --git a/dali-toolkit/internal/visuals/color/color-visual.h b/dali-toolkit/internal/visuals/color/color-visual.h index ae9388582a..44f1180525 100644 --- a/dali-toolkit/internal/visuals/color/color-visual.h +++ b/dali-toolkit/internal/visuals/color/color-visual.h @@ -24,6 +24,7 @@ // INTERNAL INCLUDES #include #include +#include namespace Dali { @@ -50,10 +51,11 @@ public: * @brief Create a new color visual. * * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object + * @param[in] shaderFactory A reference to the ColorVisualShaderFactory object * @param[in] properties A Property::Map containing settings for this visual * @return A smart-pointer to the newly allocated visual. */ - static ColorVisualPtr New(VisualFactoryCache& factoryCache, const Property::Map& properties); + static ColorVisualPtr New(VisualFactoryCache& factoryCache, ColorVisualShaderFactory& shaderFactory, const Property::Map& properties); public: // from Visual /** @@ -76,8 +78,9 @@ protected: * @brief Constructor. * * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object + * @param[in] shaderFactory A reference to the ColorVisualShaderFactory object */ - ColorVisual(VisualFactoryCache& factoryCache); + ColorVisual(VisualFactoryCache& factoryCache, ColorVisualShaderFactory& shaderFactory); /** * @brief A reference counted object may only be deleted by calling Unreference(). @@ -151,6 +154,7 @@ private: DevelColorVisual::CutoutPolicy::Type mCutoutPolicy : 3; ///< The policy of cutout bool mAlwaysUsingBlurRadius : 1; ///< Whether we need the blur radius in shader always. + ColorVisualShaderFactory& mColorVisualShaderFactory; ///< The shader factory for color visual. }; } // namespace Internal diff --git a/dali-toolkit/internal/visuals/image/image-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/image/image-visual-shader-factory.cpp index 77ea3c086f..b254426ea9 100644 --- a/dali-toolkit/internal/visuals/image/image-visual-shader-factory.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual-shader-factory.cpp @@ -40,9 +40,9 @@ constexpr int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::Shade constexpr std::string_view Y_FLIP_MASK_TEXTURE = "uYFlipMaskTexture"; constexpr float NOT_FLIP_MASK_TEXTURE = 0.0f; -constexpr auto SHADER_TYPE_COUNT = 6u; +constexpr auto PREDEFINED_SHADER_TYPE_COUNT = 6u; -constexpr std::string_view VertexPredefines[SHADER_TYPE_COUNT]{ +constexpr std::string_view VertexPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ "", // VisualFactoryCache::IMAGE_SHADER, "#define IS_REQUIRED_ROUNDED_CORNER\n", // VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER, "", // VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB, @@ -50,7 +50,7 @@ constexpr std::string_view VertexPredefines[SHADER_TYPE_COUNT]{ "", // VisualFactoryCache::IMAGE_SHADER_YUV_AND_RGB, "#define IS_REQUIRED_ROUNDED_CORNER\n", // VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB, }; -constexpr std::string_view FragmentPredefines[SHADER_TYPE_COUNT]{ +constexpr std::string_view FragmentPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ "", // VisualFactoryCache::IMAGE_SHADER, "#define IS_REQUIRED_ROUNDED_CORNER\n", // VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER, "#define IS_REQUIRED_YUV_TO_RGB\n", // VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB, @@ -58,7 +58,7 @@ constexpr std::string_view FragmentPredefines[SHADER_TYPE_COUNT]{ "#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n", // VisualFactoryCache::IMAGE_SHADER_YUV_AND_RGB, "#define IS_REQUIRED_ROUNDED_CORNER\n#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n", // VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB, }; -constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[SHADER_TYPE_COUNT]{ +constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[PREDEFINED_SHADER_TYPE_COUNT]{ VisualFactoryCache::ShaderType::IMAGE_SHADER, VisualFactoryCache::ShaderType::IMAGE_SHADER_ROUNDED_CORNER, VisualFactoryCache::ShaderType::IMAGE_SHADER_YUV_TO_RGB, @@ -77,7 +77,7 @@ ImageVisualShaderFactory::~ImageVisualShaderFactory() { } -Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, ImageVisualShaderFeatureBuilder& featureBuilder) +Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeatureBuilder& featureBuilder) { Shader shader; VisualFactoryCache::ShaderType shaderType = featureBuilder.GetShaderType(); @@ -181,7 +181,7 @@ void ImageVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) std::vector shaderName; shaders.shaderCount = 0; int shaderCount = 0; - for(uint32_t i = 0; i < SHADER_TYPE_COUNT; ++i) + for(uint32_t i = 0; i < PREDEFINED_SHADER_TYPE_COUNT; ++i) { vertexPrefix.push_back(VertexPredefines[i]); fragmentPrefix.push_back(FragmentPredefines[i]); @@ -189,9 +189,9 @@ void ImageVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) shaderCount++; } - shaders.vertexPrefix = vertexPrefix; - shaders.fragmentPrefix = fragmentPrefix; - shaders.shaderName = shaderName; + shaders.vertexPrefix = std::move(vertexPrefix); + shaders.fragmentPrefix = std::move(fragmentPrefix); + shaders.shaderName = std::move(shaderName); shaders.vertexShader = SHADER_IMAGE_VISUAL_SHADER_VERT; shaders.fragmentShader = SHADER_IMAGE_VISUAL_SHADER_FRAG; shaders.shaderCount = shaderCount; diff --git a/dali-toolkit/internal/visuals/image/image-visual-shader-factory.h b/dali-toolkit/internal/visuals/image/image-visual-shader-factory.h index 21f6ec3911..d79d258466 100644 --- a/dali-toolkit/internal/visuals/image/image-visual-shader-factory.h +++ b/dali-toolkit/internal/visuals/image/image-visual-shader-factory.h @@ -23,6 +23,7 @@ // INTERNAL INCLUDES #include #include +#include #include namespace Dali @@ -34,7 +35,7 @@ namespace Internal /** * ImageVisualShaderFactory is an object that provides and shares shaders between image visuals */ -class ImageVisualShaderFactory +class ImageVisualShaderFactory : public VisualShaderFactoryInterface { public: /** @@ -53,7 +54,7 @@ public: * @param[in] featureBuilder Collection of current image shader's features * @return The standard image rendering shader with features. */ - Shader GetShader(VisualFactoryCache& factoryCache, ImageVisualShaderFeatureBuilder& featureBuilder); + Shader GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeatureBuilder& featureBuilder); /** * @brief Request the default vertex shader source. @@ -67,11 +68,11 @@ public: */ std::string_view GetFragmentShaderSource(); +public: // Implementation of VisualShaderFactoryInterface /** - * @brief Get the default shader source. - * @param[in] shaders shaderList for precompile + * @copydoc Dali::Toolkit::VisualShaderFactoryInterface::GetPreCompiledShader */ - void GetPreCompiledShader(RawShaderData& shaders); + void GetPreCompiledShader(RawShaderData& shaders) override; protected: /** diff --git a/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.cpp b/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.cpp index 7945470b08..34aab0c5e7 100644 --- a/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.cpp @@ -111,7 +111,7 @@ ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableYuvToRgb return *this; } -VisualFactoryCache::ShaderType ImageVisualShaderFeatureBuilder::GetShaderType() +VisualFactoryCache::ShaderType ImageVisualShaderFeatureBuilder::GetShaderType() const { VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER; if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED) @@ -155,14 +155,14 @@ VisualFactoryCache::ShaderType ImageVisualShaderFeatureBuilder::GetShaderType() return shaderType; } -ImageVisualShaderFeature::ChangeFragmentShader::Type ImageVisualShaderFeatureBuilder::NeedToChangeFragmentShader() +ImageVisualShaderFeature::ChangeFragmentShader::Type ImageVisualShaderFeatureBuilder::NeedToChangeFragmentShader() const { return (mTexture && DevelTexture::IsNative(mTexture)) ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE; } -void ImageVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) +void ImageVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const { if(mTextureAtlas != ImageVisualShaderFeature::TextureAtlas::ENABLED) { @@ -181,7 +181,7 @@ void ImageVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& ver } } -void ImageVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) +void ImageVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const { if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED) { @@ -219,7 +219,7 @@ void ImageVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& f } } -Dali::Texture ImageVisualShaderFeatureBuilder::GetTexture() +Dali::Texture ImageVisualShaderFeatureBuilder::GetTexture() const { return mTexture; } diff --git a/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.h b/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.h index 635a06c110..bef845f1ae 100644 --- a/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.h +++ b/dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.h @@ -145,14 +145,14 @@ public: ImageVisualShaderFeatureBuilder& EnableYuvToRgb(bool enableYuvToRgb, bool enableUnifiedYuvAndRgb = false); - VisualFactoryCache::ShaderType GetShaderType(); + VisualFactoryCache::ShaderType GetShaderType() const; - ImageVisualShaderFeature::ChangeFragmentShader::Type NeedToChangeFragmentShader(); + ImageVisualShaderFeature::ChangeFragmentShader::Type NeedToChangeFragmentShader() const; - void GetVertexShaderPrefixList(std::string& vertexShaderPrefixList); - void GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList); + void GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const; + void GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const; - Dali::Texture GetTexture(); + Dali::Texture GetTexture() const; bool IsEnabledAlphaMaskingOnRendering() const; diff --git a/dali-toolkit/internal/visuals/text/text-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/text/text-visual-shader-factory.cpp index 693c39eb54..62928b4393 100644 --- a/dali-toolkit/internal/visuals/text/text-visual-shader-factory.cpp +++ b/dali-toolkit/internal/visuals/text/text-visual-shader-factory.cpp @@ -56,14 +56,14 @@ const VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] = VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE_AND_OVERLAY, }; -static constexpr auto SHADER_TYPE_COUNT = 1u; -constexpr std::string_view VertexPredefines[SHADER_TYPE_COUNT]{ +static constexpr auto PREDEFINED_SHADER_TYPE_COUNT = 1u; +constexpr std::string_view VertexPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT }; -constexpr std::string_view FragmentPredefines[SHADER_TYPE_COUNT]{ +constexpr std::string_view FragmentPredefines[PREDEFINED_SHADER_TYPE_COUNT]{ "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT }; -constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[SHADER_TYPE_COUNT]{ +constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[PREDEFINED_SHADER_TYPE_COUNT]{ VisualFactoryCache::ShaderType::TEXT_SHADER_SINGLE_COLOR_TEXT, }; @@ -71,6 +71,14 @@ constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[SHADER_TYPE_COUNT] namespace TextVisualShaderFeature { +FeatureBuilder::FeatureBuilder() +: mTextMultiColor(TextMultiColor::SINGLE_COLOR_TEXT), + mTextEmoji(TextEmoji::NO_EMOJI), + mTextStyle(TextStyle::NO_STYLES), + mTextOverlay(TextOverlay::NO_OVERLAY) +{ +} + FeatureBuilder& FeatureBuilder::EnableMultiColor(bool enableMultiColor) { mTextMultiColor = enableMultiColor ? TextMultiColor::MULTI_COLOR_TEXT : TextMultiColor::SINGLE_COLOR_TEXT; @@ -91,70 +99,82 @@ FeatureBuilder& FeatureBuilder::EnableOverlay(bool enableOverlay) mTextOverlay = enableOverlay ? TextOverlay::HAS_OVERLAY : TextOverlay::NO_OVERLAY; return *this; } -} // namespace TextVisualShaderFeature - -TextVisualShaderFactory::TextVisualShaderFactory() -{ -} -TextVisualShaderFactory::~TextVisualShaderFactory() +VisualFactoryCache::ShaderType FeatureBuilder::GetShaderType() const { -} - -Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder) -{ - Shader shader; uint32_t shaderTypeFlag = static_cast(TextVisualRequireFlag::DEFAULT); VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT; - const auto& multiColor = featureBuilder.mTextMultiColor; - const auto& emoji = featureBuilder.mTextEmoji; - const auto& style = featureBuilder.mTextStyle; - const auto& overlay = featureBuilder.mTextOverlay; - - if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES) + if(mTextStyle == TextVisualShaderFeature::TextStyle::HAS_STYLES) { shaderTypeFlag |= static_cast(TextVisualRequireFlag::STYLES); } - if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY) + if(mTextOverlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY) { shaderTypeFlag |= static_cast(TextVisualRequireFlag::OVERLAY); } // multi color can also render emoji. If multi color text, dont consider emoji - if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI) + if(mTextMultiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && mTextEmoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI) { shaderTypeFlag |= static_cast(TextVisualRequireFlag::EMOJI); } - if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT) + if(mTextMultiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT) { shaderTypeFlag |= static_cast(TextVisualRequireFlag::MULTI_COLOR); } shaderType = SHADER_TYPE_TABLE[shaderTypeFlag]; + return shaderType; +} + +void FeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const +{ + // Do nothing +} + +void FeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const +{ + if(mTextStyle == TextVisualShaderFeature::TextStyle::HAS_STYLES) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n"; + } + if(mTextOverlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n"; + } + // multi color can also render emoji. If multi color text, dont consider emoji + if(mTextMultiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && mTextEmoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n"; + } + if(mTextMultiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT) + { + fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n"; + } +} + +} // namespace TextVisualShaderFeature + +TextVisualShaderFactory::TextVisualShaderFactory() +{ +} + +TextVisualShaderFactory::~TextVisualShaderFactory() +{ +} + +Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder) +{ + Shader shader; + VisualFactoryCache::ShaderType shaderType = featureBuilder.GetShaderType(); shader = factoryCache.GetShader(shaderType); if(!shader) { std::string vertexShaderPrefixList; std::string fragmentShaderPrefixList; - - if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES) - { - fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n"; - } - if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY) - { - fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n"; - } - // multi color can also render emoji. If multi color text, dont consider emoji - if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI) - { - fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n"; - } - if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT) - { - fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n"; - } + featureBuilder.GetVertexShaderPrefixList(vertexShaderPrefixList); + featureBuilder.GetFragmentShaderPrefixList(fragmentShaderPrefixList); std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_VERT.data()); std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_FRAG.data()); @@ -170,7 +190,7 @@ void TextVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) std::vector fragmentPrefix; std::vector shaderName; int shaderCount = 0; - for(uint32_t i = 0; i < SHADER_TYPE_COUNT; ++i) + for(uint32_t i = 0; i < PREDEFINED_SHADER_TYPE_COUNT; ++i) { vertexPrefix.push_back(VertexPredefines[i]); fragmentPrefix.push_back(FragmentPredefines[i]); @@ -178,9 +198,9 @@ void TextVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) shaderCount++; } - shaders.vertexPrefix = vertexPrefix; - shaders.fragmentPrefix = fragmentPrefix; - shaders.shaderName = shaderName; + shaders.vertexPrefix = std::move(vertexPrefix); + shaders.fragmentPrefix = std::move(fragmentPrefix); + shaders.shaderName = std::move(shaderName); shaders.vertexShader = SHADER_TEXT_VISUAL_SHADER_VERT; shaders.fragmentShader = SHADER_TEXT_VISUAL_SHADER_FRAG; shaders.shaderCount = shaderCount; diff --git a/dali-toolkit/internal/visuals/text/text-visual-shader-factory.h b/dali-toolkit/internal/visuals/text/text-visual-shader-factory.h index 237edc7242..f354f6449e 100644 --- a/dali-toolkit/internal/visuals/text/text-visual-shader-factory.h +++ b/dali-toolkit/internal/visuals/text/text-visual-shader-factory.h @@ -22,6 +22,7 @@ // INTERNAL INCLUDES #include +#include #include namespace Dali @@ -86,21 +87,19 @@ enum Type /** * @brief Collection of current text visual feature. */ -struct FeatureBuilder +class FeatureBuilder { - FeatureBuilder() - : mTextMultiColor(TextMultiColor::SINGLE_COLOR_TEXT), - mTextEmoji(TextEmoji::NO_EMOJI), - mTextStyle(TextStyle::NO_STYLES), - mTextOverlay(TextOverlay::NO_OVERLAY) - { - } - +public: + FeatureBuilder(); FeatureBuilder& EnableMultiColor(bool enableMultiColor); FeatureBuilder& EnableEmoji(bool enableEmoji); FeatureBuilder& EnableStyle(bool enableStyle); FeatureBuilder& EnableOverlay(bool enableOverlay); + VisualFactoryCache::ShaderType GetShaderType() const; + void GetVertexShaderPrefixList(std::string& vertexShaderPrefixList) const; + void GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList) const; + bool IsEnabledMultiColor() const { return mTextMultiColor == TextMultiColor::MULTI_COLOR_TEXT; @@ -118,6 +117,7 @@ struct FeatureBuilder return mTextOverlay == TextOverlay::HAS_OVERLAY; } +private: TextMultiColor::Type mTextMultiColor : 2; ///< Whether text has multiple color, or not. default as TextMultiColor::SINGLE_COLOR_TEXT TextEmoji::Type mTextEmoji : 2; ///< Whether text has emoji, or not. default as TextEmoji::NO_EMOJI TextStyle::Type mTextStyle : 2; ///< Whether text has style, or not. default as TextStyle::NO_STYLES @@ -129,7 +129,7 @@ struct FeatureBuilder /** * TextVisualShaderFactory is an object that provides and shares shaders for text visuals */ -class TextVisualShaderFactory +class TextVisualShaderFactory : public VisualShaderFactoryInterface { public: /** @@ -150,11 +150,11 @@ public: */ Shader GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder); +public: // Implementation of VisualShaderFactoryInterface /** - * @brief Get the default shader source. - * @param[in] shaders shaderList for precompile + * @copydoc Dali::Toolkit::VisualShaderFactoryInterface::GetPreCompiledShader */ - void GetPreCompiledShader(RawShaderData& shaders); + void GetPreCompiledShader(RawShaderData& shaders) override; protected: /** diff --git a/dali-toolkit/internal/visuals/text/text-visual.cpp b/dali-toolkit/internal/visuals/text/text-visual.cpp index 5b9e3440d2..da1874cab8 100644 --- a/dali-toolkit/internal/visuals/text/text-visual.cpp +++ b/dali-toolkit/internal/visuals/text/text-visual.cpp @@ -301,7 +301,8 @@ TextVisual::~TextVisual() void TextVisual::OnInitialize() { Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY); - Shader shader = GetTextShader(mFactoryCache, TextVisualShaderFeature::FeatureBuilder()); + auto featureBuilder = TextVisualShaderFeature::FeatureBuilder(); + Shader shader = GetTextShader(mFactoryCache, featureBuilder); mImpl->mRenderer = VisualRenderer::New(geometry, shader); mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT); @@ -1364,7 +1365,7 @@ TextureSet TextVisual::GetTextTexture(const Vector2& size) return textureSet; } -Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder) +Shader TextVisual::GetTextShader(VisualFactoryCache& factoryCache, TextVisualShaderFeature::FeatureBuilder& featureBuilder) { // Cache feature builder informations. mTextShaderFeatureCache = featureBuilder; diff --git a/dali-toolkit/internal/visuals/text/text-visual.h b/dali-toolkit/internal/visuals/text/text-visual.h index 0bc00d4b7f..69611484d1 100644 --- a/dali-toolkit/internal/visuals/text/text-visual.h +++ b/dali-toolkit/internal/visuals/text/text-visual.h @@ -350,7 +350,7 @@ private: * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object * @param[in] featureBuilder Collection of current text shader's features. It will be cached as text visual. */ - Shader GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder); + Shader GetTextShader(VisualFactoryCache& factoryCache, TextVisualShaderFeature::FeatureBuilder& featureBuilder); /** * @brief Set the text to be always rendered diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.cpp b/dali-toolkit/internal/visuals/visual-factory-impl.cpp index 70a994b10e..4c4c73613e 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-impl.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -77,27 +78,13 @@ DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::VisualFactory, Dali::BaseHandle, Cr DALI_TYPE_REGISTRATION_END() const char* const BROKEN_IMAGE_FILE_NAME = "broken.png"; ///< The file name of the broken image. -static constexpr auto SHADER_TYPE_COUNT = 2u; - -constexpr std::string_view VertexPredefines[SHADER_TYPE_COUNT]{ - "", //VisualFactoryCache::COLOR_SHADER - "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER -}; -constexpr std::string_view FragmentPredefines[SHADER_TYPE_COUNT]{ - "", //VisualFactoryCache::COLOR_SHADER - "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER -}; -constexpr VisualFactoryCache::ShaderType ShaderTypePredefines[SHADER_TYPE_COUNT]{ - VisualFactoryCache::ShaderType::COLOR_SHADER, - VisualFactoryCache::ShaderType::COLOR_SHADER_ROUNDED_CORNER, -}; - } // namespace VisualFactory::VisualFactory(bool debugEnabled) : mFactoryCache(), mImageVisualShaderFactory(), mTextVisualShaderFactory(), + mColorVisualShaderFactory(), mSlotDelegate(this), mIdleCallback(nullptr), mDefaultCreationOptions(Toolkit::VisualFactory::CreationOptions::NONE), @@ -170,7 +157,7 @@ Toolkit::Visual::Base VisualFactory::CreateVisual(const Property::Map& propertyM case Toolkit::Visual::COLOR: { - visualPtr = ColorVisual::New(GetFactoryCache(), propertyMap); + visualPtr = ColorVisual::New(GetFactoryCache(), GetColorVisualShaderFactory(), propertyMap); break; } @@ -440,6 +427,8 @@ void VisualFactory::UsePreCompiledShader() ShaderPreCompiler::Get().Enable(); + // To DO : Create all visual shader factory here. + // Get image shader std::vector rawShaderList; RawShaderData imageShaderData; @@ -453,7 +442,7 @@ void VisualFactory::UsePreCompiledShader() // Get color shader RawShaderData colorShaderData; - GetPreCompiledShader(colorShaderData); + GetColorVisualShaderFactory().GetPreCompiledShader(colorShaderData); rawShaderList.push_back(colorShaderData); // Save all shader @@ -490,29 +479,6 @@ void VisualFactory::SetBrokenImageUrl(Toolkit::StyleManager& styleManager) mFactoryCache->SetBrokenImageUrl(brokenImageUrl, customBrokenImageUrlList); } -void VisualFactory::GetPreCompiledShader(RawShaderData& shaders) -{ - std::vector vertexPrefix; - std::vector fragmentPrefix; - std::vector shaderName; - int shaderCount = 0; - shaders.shaderCount = 0; - for(uint32_t i = 0u; i < SHADER_TYPE_COUNT; ++i) - { - vertexPrefix.push_back(VertexPredefines[i]); - fragmentPrefix.push_back(FragmentPredefines[i]); - shaderName.push_back(Scripting::GetLinearEnumerationName(ShaderTypePredefines[i], VISUAL_SHADER_TYPE_TABLE, VISUAL_SHADER_TYPE_TABLE_COUNT)); - shaderCount++; - } - - shaders.vertexPrefix = vertexPrefix; - shaders.fragmentPrefix = fragmentPrefix; - shaders.shaderName = shaderName; - shaders.vertexShader = SHADER_COLOR_VISUAL_SHADER_VERT; - shaders.fragmentShader = SHADER_COLOR_VISUAL_SHADER_FRAG; - shaders.shaderCount = shaderCount; -} - Internal::VisualFactoryCache& VisualFactory::GetFactoryCache() { if(!mFactoryCache) @@ -548,6 +514,15 @@ TextVisualShaderFactory& VisualFactory::GetTextVisualShaderFactory() return *mTextVisualShaderFactory; } +ColorVisualShaderFactory& VisualFactory::GetColorVisualShaderFactory() +{ + if(!mColorVisualShaderFactory) + { + mColorVisualShaderFactory = std::unique_ptr(new ColorVisualShaderFactory()); + } + return *mColorVisualShaderFactory; +} + void VisualFactory::OnDiscardCallback() { mIdleCallback = nullptr; diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.h b/dali-toolkit/internal/visuals/visual-factory-impl.h index 418237a270..913ded2c31 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.h +++ b/dali-toolkit/internal/visuals/visual-factory-impl.h @@ -37,6 +37,7 @@ namespace Internal class VisualFactoryCache; class ImageVisualShaderFactory; class TextVisualShaderFactory; +class ColorVisualShaderFactory; /** * @copydoc Toolkit::VisualFactory @@ -139,12 +140,6 @@ private: */ void SetBrokenImageUrl(Toolkit::StyleManager& styleManager); - /** - * @brief Get the default shader source. - * @param[in] shaders shaderList for precompile - */ - void GetPreCompiledShader(RawShaderData& shaders); - /** * Get the factory cache, creating it if necessary. */ @@ -160,6 +155,11 @@ private: */ TextVisualShaderFactory& GetTextVisualShaderFactory(); + /** + * Get the color visual shader factory, creating it if necessary. + */ + ColorVisualShaderFactory& GetColorVisualShaderFactory(); + /** * @brief Callbacks called for clear discarded visuals. */ @@ -183,6 +183,7 @@ private: std::unique_ptr mFactoryCache; std::unique_ptr mImageVisualShaderFactory; std::unique_ptr mTextVisualShaderFactory; + std::unique_ptr mColorVisualShaderFactory; SlotDelegate mSlotDelegate; CallbackBase* mIdleCallback; using DiscardedVisualContainer = std::vector; -- 2.34.1