[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-visual-shader-feature-builder.cpp
1 /*
2  * Copyright (c) 2023 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-feature-builder.h>
19
20 namespace Dali
21 {
22 namespace Toolkit
23 {
24 namespace Internal
25 {
26 namespace
27 {
28 // enum of required list when we select shader
29 enum class ImageVisualRequireFlag : uint32_t
30 {
31   DEFAULT          = 0,
32   ROUNDED_CORNER   = 1 << 0,
33   BORDERLINE       = 1 << 1,
34   ALPHA_MASKING    = 1 << 2,
35   COLOR_CONVERSION = 1 << 3,
36
37   UNIFIED_YUV_AND_RGB = 1 << 2, // Special enum to trick unified YUV and RGB.
38 };
39
40 static constexpr auto          SHADER_TYPE_COUNT = 16u;
41 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
42   {
43     VisualFactoryCache::IMAGE_SHADER,
44     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER,
45     VisualFactoryCache::IMAGE_SHADER_BORDERLINE,
46     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE,
47     VisualFactoryCache::IMAGE_SHADER_MASKING,
48     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_MASKING,
49     VisualFactoryCache::IMAGE_SHADER_BORDERLINE_MASKING,
50     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING,
51     VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB,
52     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB,
53     VisualFactoryCache::IMAGE_SHADER_BORDERLINE_YUV_TO_RGB,
54     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_TO_RGB,
55     VisualFactoryCache::IMAGE_SHADER_YUV_AND_RGB,
56     VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB,
57     VisualFactoryCache::IMAGE_SHADER_BORDERLINE_YUV_AND_RGB,
58     VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_AND_RGB};
59 } // unnamed namespace
60
61 ImageVisualShaderFeatureBuilder::ImageVisualShaderFeatureBuilder()
62 : mTextureAtlas(ImageVisualShaderFeature::TextureAtlas::DISABLED),
63   mDefaultTextureWrapMode(ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY),
64   mRoundedCorner(ImageVisualShaderFeature::RoundedCorner::DISABLED),
65   mBorderline(ImageVisualShaderFeature::Borderline::DISABLED),
66   mAlphaMaskingOnRendering(ImageVisualShaderFeature::AlphaMaskingOnRendering::DISABLED),
67   mColorConversion(ImageVisualShaderFeature::ColorConversion::DONT_NEED),
68   mTexture()
69 {
70 }
71
72 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableTextureAtlas(bool enableTextureAtlas)
73 {
74   mTextureAtlas = (enableTextureAtlas ? ImageVisualShaderFeature::TextureAtlas::ENABLED : ImageVisualShaderFeature::TextureAtlas::DISABLED);
75   return *this;
76 }
77
78 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
79 {
80   mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY : ImageVisualShaderFeature::DefaultTextureWrapMode::DO_NOT_APPLY);
81   return *this;
82 }
83
84 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
85 {
86   mRoundedCorner = (enableRoundedCorner ? ImageVisualShaderFeature::RoundedCorner::ENABLED : ImageVisualShaderFeature::RoundedCorner::DISABLED);
87   return *this;
88 }
89
90 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableBorderline(bool enableBorderline)
91 {
92   mBorderline = (enableBorderline ? ImageVisualShaderFeature::Borderline::ENABLED : ImageVisualShaderFeature::Borderline::DISABLED);
93   return *this;
94 }
95
96 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
97 {
98   mTexture = texture;
99   return *this;
100 }
101
102 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
103 {
104   mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED : ImageVisualShaderFeature::AlphaMaskingOnRendering::DISABLED);
105   return *this;
106 }
107
108 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb, bool enableUnifiedYuvAndRgb)
109 {
110   mColorConversion = (enableUnifiedYuvAndRgb ? ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB : (enableYuvToRgb ? ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB : ImageVisualShaderFeature::ColorConversion::DONT_NEED));
111   return *this;
112 }
113
114 VisualFactoryCache::ShaderType ImageVisualShaderFeatureBuilder::GetShaderType()
115 {
116   VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
117   if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED)
118   {
119     if(mDefaultTextureWrapMode == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
120     {
121       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
122     }
123     else
124     {
125       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
126     }
127   }
128   else
129   {
130     uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
131     if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
132     {
133       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
134     }
135     if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
136     {
137       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
138     }
139     if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
140     {
141       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
142     }
143     else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB) // Not support gpu masking and color conversion at the same time now
144     {
145       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
146     }
147     else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB)
148     {
149       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
150       shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::UNIFIED_YUV_AND_RGB);
151     }
152     shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
153   }
154
155   return shaderType;
156 }
157
158 ImageVisualShaderFeature::ChangeFragmentShader::Type ImageVisualShaderFeatureBuilder::NeedToChangeFragmentShader()
159 {
160   return (mTexture && DevelTexture::IsNative(mTexture))
161            ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
162            : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
163 }
164
165 void ImageVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList)
166 {
167   if(mTextureAtlas != ImageVisualShaderFeature::TextureAtlas::ENABLED)
168   {
169     if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
170     {
171       vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
172     }
173     if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
174     {
175       vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
176     }
177     if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
178     {
179       vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
180     }
181   }
182 }
183
184 void ImageVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList)
185 {
186   if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED)
187   {
188     if(mDefaultTextureWrapMode == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
189     {
190       fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
191     }
192     else
193     {
194       fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
195     }
196   }
197   else
198   {
199     if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
200     {
201       fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
202     }
203     if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
204     {
205       fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
206     }
207     if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
208     {
209       fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
210     }
211     else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
212     {
213       fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB\n";
214     }
215     else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB)
216     {
217       fragmentShaderPrefixList += "#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n";
218     }
219   }
220 }
221
222 Dali::Texture ImageVisualShaderFeatureBuilder::GetTexture()
223 {
224   return mTexture;
225 }
226
227 bool ImageVisualShaderFeatureBuilder::IsEnabledAlphaMaskingOnRendering() const
228 {
229   return mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED;
230 }
231
232 } // namespace Internal
233
234 } // namespace Toolkit
235
236 } // namespace Dali