2 * Copyright (c) 2023 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 static constexpr auto SHADER_TYPE_COUNT = 1u;
60 const std::string_view VertexPredefines[SHADER_TYPE_COUNT]{
61 "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT
63 const std::string_view FragmentPredefines[SHADER_TYPE_COUNT]{
64 "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT
67 } // unnamed namespace
69 namespace TextVisualShaderFeature
71 FeatureBuilder& FeatureBuilder::EnableMultiColor(bool enableMultiColor)
73 mTextMultiColor = enableMultiColor ? TextMultiColor::MULTI_COLOR_TEXT : TextMultiColor::SINGLE_COLOR_TEXT;
76 FeatureBuilder& FeatureBuilder::EnableEmoji(bool enableEmoji)
78 mTextEmoji = enableEmoji ? TextEmoji::HAS_EMOJI : TextEmoji::NO_EMOJI;
81 FeatureBuilder& FeatureBuilder::EnableStyle(bool enableStyle)
83 mTextStyle = enableStyle ? TextStyle::HAS_STYLES : TextStyle::NO_STYLES;
86 FeatureBuilder& FeatureBuilder::EnableOverlay(bool enableOverlay)
88 mTextOverlay = enableOverlay ? TextOverlay::HAS_OVERLAY : TextOverlay::NO_OVERLAY;
91 } // namespace TextVisualShaderFeature
93 TextVisualShaderFactory::TextVisualShaderFactory()
97 TextVisualShaderFactory::~TextVisualShaderFactory()
101 Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
104 uint32_t shaderTypeFlag = static_cast<uint32_t>(TextVisualRequireFlag::DEFAULT);
105 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT;
107 const auto& multiColor = featureBuilder.mTextMultiColor;
108 const auto& emoji = featureBuilder.mTextEmoji;
109 const auto& style = featureBuilder.mTextStyle;
110 const auto& overlay = featureBuilder.mTextOverlay;
112 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
114 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::STYLES);
116 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
118 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::OVERLAY);
120 // multi color can also render emoji. If multi color text, dont consider emoji
121 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
123 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::EMOJI);
125 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
127 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::MULTI_COLOR);
130 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
131 shader = factoryCache.GetShader(shaderType);
135 std::string vertexShaderPrefixList;
136 std::string fragmentShaderPrefixList;
138 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
140 fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n";
142 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
144 fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n";
146 // multi color can also render emoji. If multi color text, dont consider emoji
147 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
149 fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n";
151 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
153 fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n";
156 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_VERT.data());
157 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_FRAG.data());
159 shader = Shader::New(vertexShader, fragmentShader);
160 factoryCache.SaveShader(shaderType, shader);
165 void TextVisualShaderFactory::GetPrecompiledShader(RawShaderData& shaders)
167 std::vector<std::string_view> vertexPrefix;
168 std::vector<std::string_view> fragmentPrefix;
170 for(int i = 0; i < SHADER_TYPE_COUNT; ++i)
172 vertexPrefix.push_back(VertexPredefines[i]);
173 fragmentPrefix.push_back(FragmentPredefines[i]);
177 shaders.vertexPrefix = vertexPrefix;
178 shaders.fragmentPrefix = fragmentPrefix;
179 shaders.vertexShader = SHADER_TEXT_VISUAL_SHADER_VERT;
180 shaders.fragmentShader = SHADER_TEXT_VISUAL_SHADER_FRAG;
181 shaders.shaderCount = shaderCount;
184 } // namespace Internal
186 } // namespace Toolkit