[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-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/image-visual-shader-factory.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/rendering/texture-devel.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
25 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
26 #include <dali/integration-api/debug.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
37
38 // global string variable to caching complate vertex shader
39 static std::string gVertexShader;
40
41 // global string variable to caching complate fragment shader (no atlas)
42 static std::string gFragmentShaderNoAtlas;
43
44 const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER;
45
46 // enum of required list when we select shader
47 enum class ImageVisualRequireFlag : uint32_t
48 {
49   DEFAULT          = 0,
50   ROUNDED_CORNER   = 1 << 0,
51   BORDERLINE       = 1 << 1,
52   ALPHA_MASKING    = 1 << 2,
53   COLOR_CONVERSION = 1 << 3,
54 };
55
56 static constexpr auto          SHADER_TYPE_COUNT = 12u;
57 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
58   {
59     VisualFactoryCache::IMAGE_SHADER,
60     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER,
61     VisualFactoryCache::IMAGE_SHADER_BORDERLINE,
62     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE,
63     VisualFactoryCache::IMAGE_SHADER_MASKING,
64     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_MASKING,
65     VisualFactoryCache::IMAGE_SHADER_BORDERLINE_MASKING,
66     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING,
67     VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB,
68     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB,
69     VisualFactoryCache::IMAGE_SHADER_BORDERLINE_YUV_TO_RGB,
70     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_TO_RGB};
71 } // unnamed namespace
72
73 namespace ImageVisualShaderFeature
74 {
75 FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
76 {
77   mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
78   return *this;
79 }
80
81 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
82 {
83   mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
84   return *this;
85 }
86
87 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
88 {
89   mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
90   return *this;
91 }
92
93 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
94 {
95   mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
96   return *this;
97 }
98
99 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
100 {
101   mTexture = texture;
102   return *this;
103 }
104
105 FeatureBuilder& FeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
106 {
107   mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? AlphaMaskingOnRendering::ENABLED : AlphaMaskingOnRendering::DISABLED);
108   return *this;
109 }
110
111 FeatureBuilder& FeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb)
112 {
113   mColorConversion = (enableYuvToRgb ? ColorConversion::YUV_TO_RGB : ColorConversion::DONT_NEED);
114   return *this;
115 }
116 } // namespace ImageVisualShaderFeature
117
118 ImageVisualShaderFactory::ImageVisualShaderFactory()
119 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
120 {
121 }
122
123 ImageVisualShaderFactory::~ImageVisualShaderFactory()
124 {
125 }
126
127 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
128 {
129   Shader                         shader;
130   VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
131
132   const auto& atlasing                = featureBuilder.mTextureAtlas;
133   const auto& defaultTextureWrapping  = featureBuilder.mDefaultTextureWrapMode;
134   const auto& roundedCorner           = featureBuilder.mRoundedCorner;
135   const auto& borderline              = featureBuilder.mBorderline;
136   const auto& alphaMaskingOnRendering = featureBuilder.mAlphaMaskingOnRendering;
137   const auto& colorConversion         = featureBuilder.mColorConversion;
138   const auto& changeFragmentShader    = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
139                                           ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
140                                           : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
141
142   if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
143   {
144     if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
145     {
146       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
147     }
148     else
149     {
150       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
151     }
152   }
153   else
154   {
155     uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
156     if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
157     {
158       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
159     }
160     if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
161     {
162       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
163     }
164     if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
165     {
166       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
167     }
168     else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB) // Not support gpu masking and color conversion at the same time now
169     {
170       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
171     }
172     shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
173   }
174
175   if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
176      (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
177       mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
178   {
179     shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
180   }
181
182   shader = factoryCache.GetShader(shaderType);
183   if(!shader)
184   {
185     std::string vertexShaderPrefixList;
186     std::string fragmentShaderPrefixList;
187     if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
188     {
189       if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
190       {
191         fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
192       }
193       else
194       {
195         fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
196       }
197     }
198     else
199     {
200       if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
201       {
202         vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
203         fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
204       }
205       if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
206       {
207         vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
208         fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
209       }
210       if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
211       {
212         vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
213         fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
214       }
215       else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
216       {
217         fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB\n";
218       }
219     }
220
221     std::string vertexShader   = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
222     std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
223
224     if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
225     {
226       if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
227       {
228         // NOTE : This routine will run exist one times.
229         //
230         // First, we will run ApplyNativeFragmentShader
231         //  - If fragment shader is modified, then current platform allow to change fragment shader.
232         //    We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
233         //  - If fragment shader is not modified, then current platform will always don't change fragment shader.
234         //    We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
235         //    And change current shaderType into normal image range.
236         //    After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
237         // Second, save shader result.
238
239         // Try to apply fragmentShader
240         bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
241         if(modified)
242         {
243           // Now we know that fragment shader need to change.
244           mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
245         }
246         else
247         {
248           // Now we know that fragment shader even don't need to change.
249           // We can skip ApplyNativeFragmentShader routine after now.
250           mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
251
252           // Now we need normal shader type
253           // So decrease NATIVE_SHADER_TYPE_OFFSET.
254           shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
255
256           // If we already compiled this type already, just use that cached shader.
257           // Else, just go forward.
258           shader = factoryCache.GetShader(shaderType);
259           if(shader)
260           {
261             return shader;
262           }
263         }
264       }
265       else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
266       {
267         // Always need to apply fragmentShader
268         bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
269         DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
270       }
271     }
272     shader = Shader::New(vertexShader, fragmentShader);
273     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
274     factoryCache.SaveShader(shaderType, shader);
275   }
276
277   return shader;
278 }
279
280 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
281 {
282   if(gVertexShader.empty())
283   {
284     gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
285   }
286
287   return gVertexShader;
288 }
289
290 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
291 {
292   if(gFragmentShaderNoAtlas.empty())
293   {
294     gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
295   }
296   return gFragmentShaderNoAtlas;
297 }
298
299 } // namespace Internal
300
301 } // namespace Toolkit
302
303 } // namespace Dali