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 // global string variable to caching complate vertex shader
34 static std::string gVertexShader;
36 // global string variable to caching complate fragment shader (no atlas)
37 static std::string gFragmentShaderNoAtlas;
39 // enum of required list when we select shader
40 enum class TextVisualRequireFlag : uint32_t
49 const VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] =
51 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT,
52 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE,
53 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY,
54 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
55 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI,
56 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI,
57 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY_AND_EMOJI,
58 VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY_AND_EMOJI,
59 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT,
60 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE,
61 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_OVERLAY,
62 VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
65 } // unnamed namespace
67 namespace TextVisualShaderFeature
69 FeatureBuilder& FeatureBuilder::EnableMultiColor(bool enableMultiColor)
71 mTextMultiColor = enableMultiColor ? TextMultiColor::MULTI_COLOR_TEXT : TextMultiColor::SINGLE_COLOR_TEXT;
74 FeatureBuilder& FeatureBuilder::EnableEmoji(bool enableEmoji)
76 mTextEmoji = enableEmoji ? TextEmoji::HAS_EMOJI : TextEmoji::NO_EMOJI;
79 FeatureBuilder& FeatureBuilder::EnableStyle(bool enableStyle)
81 mTextStyle = enableStyle ? TextStyle::HAS_STYLES : TextStyle::NO_STYLES;
84 FeatureBuilder& FeatureBuilder::EnableOverlay(bool enableOverlay)
86 mTextOverlay = enableOverlay ? TextOverlay::HAS_OVERLAY : TextOverlay::NO_OVERLAY;
89 } // namespace TextVisualShaderFeature
91 TextVisualShaderFactory::TextVisualShaderFactory()
95 TextVisualShaderFactory::~TextVisualShaderFactory()
99 Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
102 uint32_t shaderTypeFlag = static_cast<uint32_t>(TextVisualRequireFlag::DEFAULT);
103 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT;
105 const auto& multiColor = featureBuilder.mTextMultiColor;
106 const auto& emoji = featureBuilder.mTextEmoji;
107 const auto& style = featureBuilder.mTextStyle;
108 const auto& overlay = featureBuilder.mTextOverlay;
110 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
112 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::STYLES);
114 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
116 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::OVERLAY);
118 // multi color can also render emoji. If multi color text, dont consider emoji
119 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
121 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::EMOJI);
123 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
125 shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::MULTI_COLOR);
128 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
129 shader = factoryCache.GetShader(shaderType);
133 std::string vertexShaderPrefixList;
134 std::string fragmentShaderPrefixList;
136 if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
138 fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n";
140 if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
142 fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n";
144 // multi color can also render emoji. If multi color text, dont consider emoji
145 if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
147 fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n";
149 if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
151 fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n";
154 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_VERT.data());
155 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_FRAG.data());
157 shader = Shader::New(vertexShader, fragmentShader);
158 factoryCache.SaveShader(shaderType, shader);
163 } // namespace Internal
165 } // namespace Toolkit