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 const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER;
40 // enum of required list when we select shader
41 enum class ImageVisualRequireFlag : uint32_t
44 ROUNDED_CORNER = 1 << 0,
46 ALPHA_MASKING = 1 << 2,
47 COLOR_CONVERSION = 1 << 3,
50 static constexpr auto SHADER_TYPE_COUNT = 12u;
51 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
53 VisualFactoryCache::IMAGE_SHADER,
54 VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER,
55 VisualFactoryCache::IMAGE_SHADER_BORDERLINE,
56 VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE,
57 VisualFactoryCache::IMAGE_SHADER_MASKING,
58 VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_MASKING,
59 VisualFactoryCache::IMAGE_SHADER_BORDERLINE_MASKING,
60 VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING,
61 VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB,
62 VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB,
63 VisualFactoryCache::IMAGE_SHADER_BORDERLINE_YUV_TO_RGB,
64 VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_TO_RGB};
65 } // unnamed namespace
67 namespace ImageVisualShaderFeature
69 FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
71 mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
75 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
77 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
81 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
83 mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
87 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
89 mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
93 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
99 FeatureBuilder& FeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
101 mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? AlphaMaskingOnRendering::ENABLED : AlphaMaskingOnRendering::DISABLED);
105 FeatureBuilder& FeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb)
107 mColorConversion = (enableYuvToRgb ? ColorConversion::YUV_TO_RGB : ColorConversion::DONT_NEED);
110 } // namespace ImageVisualShaderFeature
112 ImageVisualShaderFactory::ImageVisualShaderFactory()
113 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
117 ImageVisualShaderFactory::~ImageVisualShaderFactory()
121 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
124 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
126 const auto& atlasing = featureBuilder.mTextureAtlas;
127 const auto& defaultTextureWrapping = featureBuilder.mDefaultTextureWrapMode;
128 const auto& roundedCorner = featureBuilder.mRoundedCorner;
129 const auto& borderline = featureBuilder.mBorderline;
130 const auto& alphaMaskingOnRendering = featureBuilder.mAlphaMaskingOnRendering;
131 const auto& colorConversion = featureBuilder.mColorConversion;
132 const auto& changeFragmentShader = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
133 ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
134 : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
136 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
138 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
140 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
144 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
149 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
150 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
152 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
154 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
156 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
158 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
160 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
162 else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB) // Not support gpu masking and color conversion at the same time now
164 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::COLOR_CONVERSION);
166 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
169 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
170 (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
171 mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
173 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
176 shader = factoryCache.GetShader(shaderType);
179 std::string vertexShaderPrefixList;
180 std::string fragmentShaderPrefixList;
181 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
183 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
185 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
189 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
194 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
196 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
197 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
199 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
201 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
202 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
204 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
206 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
207 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
209 else if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
211 fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB\n";
215 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
216 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
218 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
220 if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
222 // NOTE : This routine will run exist one times.
224 // First, we will run ApplyNativeFragmentShader
225 // - If fragment shader is modified, then current platform allow to change fragment shader.
226 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
227 // - If fragment shader is not modified, then current platform will always don't change fragment shader.
228 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
229 // And change current shaderType into normal image range.
230 // After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
231 // Second, save shader result.
233 // Try to apply fragmentShader
234 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
237 // Now we know that fragment shader need to change.
238 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
242 // Now we know that fragment shader even don't need to change.
243 // We can skip ApplyNativeFragmentShader routine after now.
244 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
246 // Now we need normal shader type
247 // So decrease NATIVE_SHADER_TYPE_OFFSET.
248 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
250 // If we already compiled this type already, just use that cached shader.
251 // Else, just go forward.
252 shader = factoryCache.GetShader(shaderType);
259 else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
261 // Always need to apply fragmentShader
262 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
263 DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
266 shader = Shader::New(vertexShader, fragmentShader);
267 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
268 factoryCache.SaveShader(shaderType, shader);
274 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
276 // static string variable to cache complete vertex shader
277 static std::string gVertexShader;
278 if(gVertexShader.empty())
280 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
283 return gVertexShader;
286 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
288 // static string variable to cache complete fragment shader (no atlas)
289 static std::string gFragmentShaderNoAtlas;
290 if(gFragmentShaderNoAtlas.empty())
292 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
294 return gFragmentShaderNoAtlas;
297 } // namespace Internal
299 } // namespace Toolkit