2 * Copyright (c) 2023 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-feature-builder.h>
28 // enum of required list when we select shader
29 enum class ImageVisualRequireFlag : uint32_t
32 ROUNDED_CORNER = 1 << 0,
34 ALPHA_MASKING = 1 << 2,
35 COLOR_CONVERSION = 1 << 3,
37 UNIFIED_YUV_AND_RGB = 1 << 2, // Special enum to trick unified YUV and RGB.
40 static constexpr auto SHADER_TYPE_COUNT = 16u;
41 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
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
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),
72 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableTextureAtlas(bool enableTextureAtlas)
74 mTextureAtlas = (enableTextureAtlas ? ImageVisualShaderFeature::TextureAtlas::ENABLED : ImageVisualShaderFeature::TextureAtlas::DISABLED);
78 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
80 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY : ImageVisualShaderFeature::DefaultTextureWrapMode::DO_NOT_APPLY);
84 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
86 mRoundedCorner = (enableRoundedCorner ? ImageVisualShaderFeature::RoundedCorner::ENABLED : ImageVisualShaderFeature::RoundedCorner::DISABLED);
90 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableBorderline(bool enableBorderline)
92 mBorderline = (enableBorderline ? ImageVisualShaderFeature::Borderline::ENABLED : ImageVisualShaderFeature::Borderline::DISABLED);
96 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
102 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
104 mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED : ImageVisualShaderFeature::AlphaMaskingOnRendering::DISABLED);
108 ImageVisualShaderFeatureBuilder& ImageVisualShaderFeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb, bool enableUnifiedYuvAndRgb)
110 mColorConversion = (enableUnifiedYuvAndRgb ? ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB : (enableYuvToRgb ? ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB : ImageVisualShaderFeature::ColorConversion::DONT_NEED));
114 VisualFactoryCache::ShaderType ImageVisualShaderFeatureBuilder::GetShaderType()
116 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
117 if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED)
119 if(mDefaultTextureWrapMode == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
121 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
125 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
130 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
131 if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
133 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
135 if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
137 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
139 if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
141 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
143 else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB) // Not support gpu masking and color conversion at the same time now
145 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
147 else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB)
149 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
150 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::UNIFIED_YUV_AND_RGB);
152 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
158 ImageVisualShaderFeature::ChangeFragmentShader::Type ImageVisualShaderFeatureBuilder::NeedToChangeFragmentShader()
160 return (mTexture && DevelTexture::IsNative(mTexture))
161 ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
162 : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
165 void ImageVisualShaderFeatureBuilder::GetVertexShaderPrefixList(std::string& vertexShaderPrefixList)
167 if(mTextureAtlas != ImageVisualShaderFeature::TextureAtlas::ENABLED)
169 if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
171 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
173 if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
175 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
177 if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
179 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
184 void ImageVisualShaderFeatureBuilder::GetFragmentShaderPrefixList(std::string& fragmentShaderPrefixList)
186 if(mTextureAtlas == ImageVisualShaderFeature::TextureAtlas::ENABLED)
188 if(mDefaultTextureWrapMode == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
190 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
194 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
199 if(mRoundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
201 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
203 if(mBorderline == ImageVisualShaderFeature::Borderline::ENABLED)
205 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
207 if(mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
209 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
211 else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
213 fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB\n";
215 else if(mColorConversion == ImageVisualShaderFeature::ColorConversion::UNIFIED_YUV_AND_RGB)
217 fragmentShaderPrefixList += "#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n";
222 Dali::Texture ImageVisualShaderFeatureBuilder::GetTexture()
227 bool ImageVisualShaderFeatureBuilder::IsEnabledAlphaMaskingOnRendering() const
229 return mAlphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED;
232 } // namespace Internal
234 } // namespace Toolkit