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.
18 #include <dali-toolkit/internal/visuals/text-visual-shader-factory.h>
21 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
22 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
23 #include <dali/integration-api/debug.h>
33 // enum of required list when we select shader
34 enum class TextVisualRequireFlag : uint32_t
43 const VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] =
45 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT,
46 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE,
47 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY,
48 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
49 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI,
50 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI,
51 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY_AND_EMOJI,
52 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY_AND_EMOJI,
53 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT,
54 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE,
55 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_OVERLAY,
56 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
59 } // unnamed namespace
61 namespace TextVisualShaderFeature
63 FeatureBuilder& FeatureBuilder::EnableMultiColor(bool enableMultiColor)
65 mTextMultiColor = enableMultiColor ? TextMultiColor::MULTI_COLOR_TEXT : TextMultiColor::SINGLE_COLOR_TEXT;
68 FeatureBuilder& FeatureBuilder::EnableEmoji(bool enableEmoji)
70 mTextEmoji = enableEmoji ? TextEmoji::HAS_EMOJI : TextEmoji::NO_EMOJI;
73 FeatureBuilder& FeatureBuilder::EnableStyle(bool enableStyle)
75 mTextStyle = enableStyle ? TextStyle::HAS_STYLES : TextStyle::NO_STYLES;
78 FeatureBuilder& FeatureBuilder::EnableOverlay(bool enableOverlay)
80 mTextOverlay = enableOverlay ? TextOverlay::HAS_OVERLAY : TextOverlay::NO_OVERLAY;
83 } // namespace TextVisualShaderFeature
85 TextVisualShaderFactory::TextVisualShaderFactory()
89 TextVisualShaderFactory::~TextVisualShaderFactory()
93 Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
96 uint32_t shaderTypeFlag = static_cast<uint32_t>(TextVisualRequireFlag::DEFAULT);
97 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT;
99 const auto& multiColor = featureBuilder.mTextMultiColor;
100 const auto& emoji = featureBuilder.mTextEmoji;
101 const auto& style = featureBuilder.mTextStyle;
102 const auto& overlay = featureBuilder.mTextOverlay;
104 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
106 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::STYLES);
108 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
110 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::OVERLAY);
112 // multi color can also render emoji. If multi color text, dont consider emoji
113 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
115 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::EMOJI);
117 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
119 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::MULTI_COLOR);
122 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
123 shader = factoryCache.GetShader(shaderType);
127 std::string vertexShaderPrefixList;
128 std::string fragmentShaderPrefixList;
130 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
132 fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n";
134 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
136 fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n";
138 // multi color can also render emoji. If multi color text, dont consider emoji
139 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
141 fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n";
143 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
145 fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n";
148 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_VERT.data());
149 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_FRAG.data());
151 shader = Shader::New(vertexShader, fragmentShader);
152 factoryCache.SaveShader(shaderType, shader);
157 } // namespace Internal
159 } // namespace Toolkit