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