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