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