e0070ed64fbae1d6ce5402ec32c05237814eac87
[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) 2021 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 /**
34  * ImageVisualShaderFeature contains feature lists what image visual shader need to know.
35  */
36 namespace ImageVisualShaderFeature
37 {
38 namespace TextureAtlas
39 {
40 /**
41  * @brief Whether use texture with atlas, or not
42  */
43 enum Type
44 {
45   DISABLED = 0, ///< Image visual doesn't use ATLAS
46   ENABLED       ///< Image visual uses ATLAS
47 };
48 } // namespace TextureAtlas
49
50 namespace DefaultTextureWrapMode
51 {
52 /**
53  * @brief Whether apply to texture wraping in default, or not
54  */
55 enum Type
56 {
57   APPLY = 0,   ///< Image visual applies to wraping texture in default
58   DO_NOT_APPLY ///< Image visual doesn't apply to wraping texture in default
59 };
60 } // namespace DefaultTextureWrapMode
61
62 namespace RoundedCorner
63 {
64 /**
65  * @brief Whether use rounded corner, or not
66  */
67 enum Type
68 {
69   DISABLED = 0, ///< Image visual doesn't use rounded corner
70   ENABLED       ///< Image visual uses rounded corner
71 };
72 } // namespace RoundedCorner
73
74 namespace Borderline
75 {
76 /**
77  * @brief Whether use borderline, or not
78  */
79 enum Type
80 {
81   DISABLED = 0, ///< Image visual doesn't use borderline
82   ENABLED       ///< Image visual uses borderline
83 };
84 } // namespace Borderline
85
86 namespace ChangeFragmentShader
87 {
88 /**
89  * @brief Whether native image change the default fragment shader, or not
90  */
91 enum Type
92 {
93   DONT_CHANGE = 0, ///< Native image doesn't change default fragment shader.
94   NEED_CHANGE,     ///< Native image changes default fragment shader. We need another shader cache.
95   UNDECIDED,       ///< Undecided.
96 };
97 } // namespace ChangeFragmentShader
98
99 namespace AlphaMaskingOnRendering
100 {
101 /**
102  * @brief Whether use runtime alpha masking in shader, or not
103  */
104 enum Type
105 {
106   DISABLED = 0, ///< Image visual doesn't use runtime alpha masking
107   ENABLED       ///< Image visual uses runtime alpha masking
108 };
109 } // namespace AlphaMaskingOnRendering
110
111 /**
112  * @brief Collection of current image visual feature. Only use for ImageVisualShaderFactory::GetShader()
113  */
114 struct FeatureBuilder
115 {
116   FeatureBuilder()
117   : mTextureAtlas(TextureAtlas::DISABLED),
118     mDefaultTextureWrapMode(DefaultTextureWrapMode::APPLY),
119     mRoundedCorner(RoundedCorner::DISABLED),
120     mBorderline(Borderline::DISABLED),
121     mAlphaMaskingOnRendering(AlphaMaskingOnRendering::DISABLED),
122     mTexture()
123   {
124   }
125
126   FeatureBuilder& EnableTextureAtlas(bool enableTextureAtlas);
127   FeatureBuilder& ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode);
128   FeatureBuilder& EnableRoundedCorner(bool enableRoundedCorner);
129   FeatureBuilder& EnableBorderline(bool enableBorderline);
130   FeatureBuilder& SetTextureForFragmentShaderCheck(const Dali::Texture& texture);
131   FeatureBuilder& EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering);
132
133   TextureAtlas::Type            mTextureAtlas : 2;            ///< Whether use texture with atlas, or not. default as TextureAtlas::DISABLED
134   DefaultTextureWrapMode::Type  mDefaultTextureWrapMode : 2;  ///< Whether apply to texture wraping in default, or not. default as DefaultTextureWrapMode::APPLY
135   RoundedCorner::Type           mRoundedCorner : 2;           ///< Whether use rounded corner, or not. default as RoundedCorner::DISABLED
136   Borderline::Type              mBorderline : 2;              ///< Whether use borderline, or not. default as Borderline::DISABLED
137   AlphaMaskingOnRendering::Type mAlphaMaskingOnRendering : 2; ///< Whether use runtime alpha masking, or not. default as AlphaMaskingOnRendering::DISABLED
138   Dali::Texture                 mTexture;                     ///< Texture to check whether we need to change fragment shader or not
139 };
140
141 } // namespace ImageVisualShaderFeature
142
143 /**
144  * ImageVisualShaderFactory is an object that provides and shares shaders between image visuals
145  */
146 class ImageVisualShaderFactory
147 {
148 public:
149
150   /**
151    * @brief Constructor
152    */
153   ImageVisualShaderFactory();
154
155   /**
156    * @brief Destructor
157    */
158   ~ImageVisualShaderFactory();
159
160   /**
161    * @brief Get the standard image rendering shader.
162    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
163    * @param[in] featureBuilder Collection of current image shader's features
164    * @return The standard image rendering shader with features.
165    */
166   Shader GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder);
167
168   /**
169    * @brief Request the default vertex shader source.
170    * @return The default vertex shader source.
171    */
172   std::string_view GetVertexShaderSource();
173
174   /**
175    * @brief Request the default fragment shader source.
176    * @return The default fragment shader source.
177    */
178   std::string_view GetFragmentShaderSource();
179
180 protected:
181   /**
182    * Undefined copy constructor.
183    */
184   ImageVisualShaderFactory(const ImageVisualShaderFactory&);
185
186   /**
187    * Undefined assignment operator.
188    */
189   ImageVisualShaderFactory& operator=(const ImageVisualShaderFactory& rhs);
190
191 private:
192
193   /**
194    * @brief Cached information whether native image should change fragment shader.
195    * Default it is ChangeFragmentShader::UNDECIDED.
196    * If we have any chance to check native image source apply fragment shader,
197    * this vaule will be changed one of these : ChangeFragmentShader::DONT_CHANGE or ChangeFragmentShader::NEED_CHANGE
198    *
199    * After result cached, this value will not be changed.
200    *
201    * If value is DONT_CHANGE, ImageVisualShaderFactory::GetShader never call ApplyNativeFragmentShader.
202    * Else, ImageVisualShaderFactory::GetShader will call ApplyNativeFragmentShader if native image source texture come.
203    */
204   ImageVisualShaderFeature::ChangeFragmentShader::Type mFragmentShaderNeedChange : 3;
205 };
206
207 } // namespace Internal
208
209 } // namespace Toolkit
210
211 } // namespace Dali
212
213 #endif // DALI_TOOLKIT_IMAGE_VISUAL_SHADER_FACTORY_H