2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
21 #include <dali/devel-api/rendering/texture-devel.h>
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>
36 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
38 // global string variable to caching complate vertex shader
39 static std::string gVertexShader;
41 // global string variable to caching complate fragment shader (no atlas)
42 static std::string gFragmentShaderNoAtlas;
44 const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER;
46 // enum of required list when we select shader
47 enum class ImageVisualRequireFlag : uint32_t
50 ROUNDED_CORNER = 1 << 0,
52 ALPHA_MASKING = 1 << 2,
53 COLOR_CONVERSION = 1 << 3,
56 static constexpr auto SHADER_TYPE_COUNT = 12u;
57 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
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
73 namespace ImageVisualShaderFeature
75 FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
77 mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
81 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
83 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
87 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
89 mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
93 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
95 mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
99 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
105 FeatureBuilder& FeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
107 mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? AlphaMaskingOnRendering::ENABLED : AlphaMaskingOnRendering::DISABLED);
111 FeatureBuilder& FeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb)
113 mColorConversion = (enableYuvToRgb ? ColorConversion::YUV_TO_RGB : ColorConversion::DONT_NEED);
116 } // namespace ImageVisualShaderFeature
118 ImageVisualShaderFactory::ImageVisualShaderFactory()
119 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
123 ImageVisualShaderFactory::~ImageVisualShaderFactory()
127 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
130 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
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;
142 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
144 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
146 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
150 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
155 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
156 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
158 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
160 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
162 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
164 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
166 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
168 else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB) // Not support gpu masking and color conversion at the same time now
170 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
172 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
175 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
176 (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
177 mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
179 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
182 shader = factoryCache.GetShader(shaderType);
185 std::string vertexShaderPrefixList;
186 std::string fragmentShaderPrefixList;
187 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
189 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
191 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
195 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
200 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
202 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
203 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
205 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
207 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
208 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
210 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
212 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
213 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
215 else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
217 fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB\n";
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());
224 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
226 if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
228 // NOTE : This routine will run exist one times.
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.
239 // Try to apply fragmentShader
240 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
243 // Now we know that fragment shader need to change.
244 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
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;
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);
256 // If we already compiled this type already, just use that cached shader.
257 // Else, just go forward.
258 shader = factoryCache.GetShader(shaderType);
265 else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
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!");
272 shader = Shader::New(vertexShader, fragmentShader);
273 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
274 factoryCache.SaveShader(shaderType, shader);
280 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
282 if(gVertexShader.empty())
284 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
287 return gVertexShader;
290 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
292 if(gFragmentShaderNoAtlas.empty())
294 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
296 return gFragmentShaderNoAtlas;
299 } // namespace Internal
301 } // namespace Toolkit