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