[Tizen] Change precompile shader list for performance
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text-visual-shader-factory.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/text-visual-shader-factory.h>
19
20 // INTERNAL INCLUDES
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>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29 namespace Internal
30 {
31 namespace
32 {
33 // enum of required list when we select shader
34 enum class TextVisualRequireFlag : uint32_t
35 {
36   DEFAULT     = 0,
37   STYLES      = 1 << 0,
38   OVERLAY     = 1 << 1,
39   EMOJI       = 1 << 2,
40   MULTI_COLOR = 1 << 3,
41 };
42
43 const VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] =
44   {
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,
57 };
58
59 static constexpr auto          SHADER_TYPE_COUNT = 1u;
60 const std::string_view VertexPredefines[SHADER_TYPE_COUNT]
61 {
62   "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT
63 };
64 const std::string_view FragmentPredefines[SHADER_TYPE_COUNT]
65 {
66   "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT
67 };
68
69 } // unnamed namespace
70
71 namespace TextVisualShaderFeature
72 {
73 FeatureBuilder& FeatureBuilder::EnableMultiColor(bool enableMultiColor)
74 {
75   mTextMultiColor = enableMultiColor ? TextMultiColor::MULTI_COLOR_TEXT : TextMultiColor::SINGLE_COLOR_TEXT;
76   return *this;
77 }
78 FeatureBuilder& FeatureBuilder::EnableEmoji(bool enableEmoji)
79 {
80   mTextEmoji = enableEmoji ? TextEmoji::HAS_EMOJI : TextEmoji::NO_EMOJI;
81   return *this;
82 }
83 FeatureBuilder& FeatureBuilder::EnableStyle(bool enableStyle)
84 {
85   mTextStyle = enableStyle ? TextStyle::HAS_STYLES : TextStyle::NO_STYLES;
86   return *this;
87 }
88 FeatureBuilder& FeatureBuilder::EnableOverlay(bool enableOverlay)
89 {
90   mTextOverlay = enableOverlay ? TextOverlay::HAS_OVERLAY : TextOverlay::NO_OVERLAY;
91   return *this;
92 }
93 } // namespace TextVisualShaderFeature
94
95 TextVisualShaderFactory::TextVisualShaderFactory()
96 {
97 }
98
99 TextVisualShaderFactory::~TextVisualShaderFactory()
100 {
101 }
102
103 Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder)
104 {
105   Shader                         shader;
106   uint32_t                       shaderTypeFlag = static_cast<uint32_t>(TextVisualRequireFlag::DEFAULT);
107   VisualFactoryCache::ShaderType shaderType     = VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT;
108
109   const auto& multiColor = featureBuilder.mTextMultiColor;
110   const auto& emoji      = featureBuilder.mTextEmoji;
111   const auto& style      = featureBuilder.mTextStyle;
112   const auto& overlay    = featureBuilder.mTextOverlay;
113
114   if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
115   {
116     shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::STYLES);
117   }
118   if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
119   {
120     shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::OVERLAY);
121   }
122   // multi color can also render emoji. If multi color text, dont consider emoji
123   if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
124   {
125     shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::EMOJI);
126   }
127   if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
128   {
129     shaderTypeFlag |= static_cast<uint32_t>(TextVisualRequireFlag::MULTI_COLOR);
130   }
131
132   shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
133   shader     = factoryCache.GetShader(shaderType);
134
135   if(!shader)
136   {
137     std::string vertexShaderPrefixList;
138     std::string fragmentShaderPrefixList;
139
140     if(style == TextVisualShaderFeature::TextStyle::HAS_STYLES)
141     {
142       fragmentShaderPrefixList += "#define IS_REQUIRED_STYLE\n";
143     }
144     if(overlay == TextVisualShaderFeature::TextOverlay::HAS_OVERLAY)
145     {
146       fragmentShaderPrefixList += "#define IS_REQUIRED_OVERLAY\n";
147     }
148     // multi color can also render emoji. If multi color text, dont consider emoji
149     if(multiColor != TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT && emoji == TextVisualShaderFeature::TextEmoji::HAS_EMOJI)
150     {
151       fragmentShaderPrefixList += "#define IS_REQUIRED_EMOJI\n";
152     }
153     if(multiColor == TextVisualShaderFeature::TextMultiColor::MULTI_COLOR_TEXT)
154     {
155       fragmentShaderPrefixList += "#define IS_REQUIRED_MULTI_COLOR\n";
156     }
157
158     std::string vertexShader   = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_VERT.data());
159     std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_TEXT_VISUAL_SHADER_FRAG.data());
160
161     shader = Shader::New(vertexShader, fragmentShader);
162     factoryCache.SaveShader(shaderType, shader);
163   }
164   return shader;
165 }
166
167 void TextVisualShaderFactory::GetPrecompiledShader(RawShaderData& shaders)
168 {
169   std::vector<std::string_view> vertexPrefix;
170   std::vector<std::string_view> fragmentPrefix;
171   int shaderCount = 0;
172   for(int i=0; i< SHADER_TYPE_COUNT; ++i)
173   {
174     vertexPrefix.push_back(VertexPredefines[i]);
175     fragmentPrefix.push_back(FragmentPredefines[i]);
176     shaderCount++;
177   }
178
179   shaders.vertexPrefix= vertexPrefix;
180   shaders.fragmentPrefix = fragmentPrefix;
181   shaders.vertexShader = SHADER_TEXT_VISUAL_SHADER_VERT;
182   shaders.fragmentShader = SHADER_TEXT_VISUAL_SHADER_FRAG;
183   shaders.shaderCount = shaderCount;
184 }
185
186 } // namespace Internal
187
188 } // namespace Toolkit
189
190 } // namespace Dali