Support YUV decoding for JPEG
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-visual-shader-factory.h
1 #ifndef DALI_TOOLKIT_IMAGE_VISUAL_SHADER_FACTORY_H
2 #define DALI_TOOLKIT_IMAGE_VISUAL_SHADER_FACTORY_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
24 #include <string_view>
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30 namespace Internal
31 {
32 /**
33  * ImageVisualShaderFeature contains feature lists what image visual shader need to know.
34  */
35 namespace ImageVisualShaderFeature
36 {
37 namespace TextureAtlas
38 {
39 /**
40  * @brief Whether use texture with atlas, or not
41  */
42 enum Type
43 {
44   DISABLED = 0, ///< Image visual doesn't use ATLAS
45   ENABLED       ///< Image visual uses ATLAS
46 };
47 } // namespace TextureAtlas
48
49 namespace DefaultTextureWrapMode
50 {
51 /**
52  * @brief Whether apply to texture wraping in default, or not
53  */
54 enum Type
55 {
56   APPLY = 0,   ///< Image visual applies to wraping texture in default
57   DO_NOT_APPLY ///< Image visual doesn't apply to wraping texture in default
58 };
59 } // namespace DefaultTextureWrapMode
60
61 namespace RoundedCorner
62 {
63 /**
64  * @brief Whether use rounded corner, or not
65  */
66 enum Type
67 {
68   DISABLED = 0, ///< Image visual doesn't use rounded corner
69   ENABLED       ///< Image visual uses rounded corner
70 };
71 } // namespace RoundedCorner
72
73 namespace Borderline
74 {
75 /**
76  * @brief Whether use borderline, or not
77  */
78 enum Type
79 {
80   DISABLED = 0, ///< Image visual doesn't use borderline
81   ENABLED       ///< Image visual uses borderline
82 };
83 } // namespace Borderline
84
85 namespace ChangeFragmentShader
86 {
87 /**
88  * @brief Whether native image change the default fragment shader, or not
89  */
90 enum Type
91 {
92   DONT_CHANGE = 0, ///< Native image doesn't change default fragment shader.
93   NEED_CHANGE,     ///< Native image changes default fragment shader. We need another shader cache.
94   UNDECIDED,       ///< Undecided.
95 };
96 } // namespace ChangeFragmentShader
97
98 namespace AlphaMaskingOnRendering
99 {
100 /**
101  * @brief Whether use runtime alpha masking in shader, or not
102  */
103 enum Type
104 {
105   DISABLED = 0, ///< Image visual doesn't use runtime alpha masking
106   ENABLED       ///< Image visual uses runtime alpha masking
107 };
108 } // namespace AlphaMaskingOnRendering
109
110 namespace ColorConversion
111 {
112 /**
113  * @brief Whether the color format conversion is needed or not
114  */
115 enum Type
116 {
117   DONT_NEED = 0, ///< Not need to convert
118   YUV_TO_RGB     ///< Need yuv to rgb conversion
119 };
120 } // namespace ColorConversion
121
122 /**
123  * @brief Collection of current image visual feature. Only use for ImageVisualShaderFactory::GetShader()
124  */
125 struct FeatureBuilder
126 {
127   FeatureBuilder()
128   : mTextureAtlas(TextureAtlas::DISABLED),
129     mDefaultTextureWrapMode(DefaultTextureWrapMode::APPLY),
130     mRoundedCorner(RoundedCorner::DISABLED),
131     mBorderline(Borderline::DISABLED),
132     mAlphaMaskingOnRendering(AlphaMaskingOnRendering::DISABLED),
133     mColorConversion(ColorConversion::DONT_NEED),
134     mTexture()
135   {
136   }
137
138   FeatureBuilder& EnableTextureAtlas(bool enableTextureAtlas);
139   FeatureBuilder& ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode);
140   FeatureBuilder& EnableRoundedCorner(bool enableRoundedCorner);
141   FeatureBuilder& EnableBorderline(bool enableBorderline);
142   FeatureBuilder& SetTextureForFragmentShaderCheck(const Dali::Texture& texture);
143   FeatureBuilder& EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering);
144   FeatureBuilder& EnableYuvToRgb(bool enableYuvToRgb);
145
146   TextureAtlas::Type            mTextureAtlas : 2;            ///< Whether use texture with atlas, or not. default as TextureAtlas::DISABLED
147   DefaultTextureWrapMode::Type  mDefaultTextureWrapMode : 2;  ///< Whether apply to texture wraping in default, or not. default as DefaultTextureWrapMode::APPLY
148   RoundedCorner::Type           mRoundedCorner : 2;           ///< Whether use rounded corner, or not. default as RoundedCorner::DISABLED
149   Borderline::Type              mBorderline : 2;              ///< Whether use borderline, or not. default as Borderline::DISABLED
150   AlphaMaskingOnRendering::Type mAlphaMaskingOnRendering : 2; ///< Whether use runtime alpha masking, or not. default as AlphaMaskingOnRendering::DISABLED
151   ColorConversion::Type         mColorConversion : 2;         ///< Whether the color format conversion is needed or not
152   Dali::Texture                 mTexture;                     ///< Texture to check whether we need to change fragment shader or not
153 };
154
155 } // namespace ImageVisualShaderFeature
156
157 /**
158  * ImageVisualShaderFactory is an object that provides and shares shaders between image visuals
159  */
160 class ImageVisualShaderFactory
161 {
162 public:
163   /**
164    * @brief Constructor
165    */
166   ImageVisualShaderFactory();
167
168   /**
169    * @brief Destructor
170    */
171   ~ImageVisualShaderFactory();
172
173   /**
174    * @brief Get the standard image rendering shader.
175    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
176    * @param[in] featureBuilder Collection of current image shader's features
177    * @return The standard image rendering shader with features.
178    */
179   Shader GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder);
180
181   /**
182    * @brief Request the default vertex shader source.
183    * @return The default vertex shader source.
184    */
185   std::string_view GetVertexShaderSource();
186
187   /**
188    * @brief Request the default fragment shader source.
189    * @return The default fragment shader source.
190    */
191   std::string_view GetFragmentShaderSource();
192
193 protected:
194   /**
195    * Undefined copy constructor.
196    */
197   ImageVisualShaderFactory(const ImageVisualShaderFactory&);
198
199   /**
200    * Undefined assignment operator.
201    */
202   ImageVisualShaderFactory& operator=(const ImageVisualShaderFactory& rhs);
203
204 private:
205   /**
206    * @brief Cached information whether native image should change fragment shader.
207    * Default it is ChangeFragmentShader::UNDECIDED.
208    * If we have any chance to check native image source apply fragment shader,
209    * this vaule will be changed one of these : ChangeFragmentShader::DONT_CHANGE or ChangeFragmentShader::NEED_CHANGE
210    *
211    * After result cached, this value will not be changed.
212    *
213    * If value is DONT_CHANGE, ImageVisualShaderFactory::GetShader never call ApplyNativeFragmentShader.
214    * Else, ImageVisualShaderFactory::GetShader will call ApplyNativeFragmentShader if native image source texture come.
215    */
216   ImageVisualShaderFeature::ChangeFragmentShader::Type mFragmentShaderNeedChange : 3;
217 };
218
219 } // namespace Internal
220
221 } // namespace Toolkit
222
223 } // namespace Dali
224
225 #endif // DALI_TOOLKIT_IMAGE_VISUAL_SHADER_FACTORY_H