[dali_2.1.0] Merge branch 'devel/master'
[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 /**
100  * @brief Collection of current image visual feature. Only use for ImageVisualShaderFactory::GetShader()
101  */
102 struct FeatureBuilder
103 {
104   FeatureBuilder()
105   : mTextureAtlas(TextureAtlas::DISABLED),
106     mDefaultTextureWrapMode(DefaultTextureWrapMode::APPLY),
107     mRoundedCorner(RoundedCorner::DISABLED),
108     mBorderline(Borderline::DISABLED),
109     mTexture()
110   {
111   }
112
113   FeatureBuilder& EnableTextureAtlas(bool enableTextureAtlas);
114   FeatureBuilder& ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode);
115   FeatureBuilder& EnableRoundedCorner(bool enableRoundedCorner);
116   FeatureBuilder& EnableBorderline(bool enableBorderline);
117   FeatureBuilder& SetTextureForFragmentShaderCheck(const Dali::Texture& texture);
118
119   TextureAtlas::Type           mTextureAtlas : 2;           ///< Whether use texture with atlas, or not. default as TextureAtlas::DISABLED
120   DefaultTextureWrapMode::Type mDefaultTextureWrapMode : 2; ///< Whether apply to texture wraping in default, or not. default as DefaultTextureWrapMode::APPLY
121   RoundedCorner::Type          mRoundedCorner : 2;          ///< Whether use rounded corner, or not. default as RoundedCorner::DISABLED
122   Borderline::Type             mBorderline : 2;             ///< Whether use borderline, or not. default as Borderline::DISABLED
123   Dali::Texture                mTexture;                    ///< Texture to check whether we need to change fragment shader or not
124 };
125
126 } // namespace ImageVisualShaderFactoryFeature
127
128 /**
129  * ImageVisualShaderFactory is an object that provides and shares shaders between image visuals
130  */
131 class ImageVisualShaderFactory
132 {
133 public:
134
135   /**
136    * @brief Constructor
137    */
138   ImageVisualShaderFactory();
139
140   /**
141    * @brief Destructor
142    */
143   ~ImageVisualShaderFactory();
144
145   /**
146    * @brief Get the standard image rendering shader.
147    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
148    * @param[in] featureBuilder Collection of current image shader's features
149    * @return The standard image rendering shader with features.
150    */
151   Shader GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder);
152
153   /**
154    * @brief Request the default vertex shader source.
155    * @return The default vertex shader source.
156    */
157   std::string_view GetVertexShaderSource();
158
159   /**
160    * @brief Request the default fragment shader source.
161    * @return The default fragment shader source.
162    */
163   std::string_view GetFragmentShaderSource();
164
165 protected:
166   /**
167    * Undefined copy constructor.
168    */
169   ImageVisualShaderFactory(const ImageVisualShaderFactory&);
170
171   /**
172    * Undefined assignment operator.
173    */
174   ImageVisualShaderFactory& operator=(const ImageVisualShaderFactory& rhs);
175
176 private:
177
178   /**
179    * @brief Cached information whether native image should change fragment shader.
180    * Default it is ChangeFragmentShader::UNDECIDED.
181    * If we have any chance to check native image source apply fragment shader,
182    * this vaule will be changed one of these : ChangeFragmentShader::DONT_CHANGE or ChangeFragmentShader::NEED_CHANGE
183    *
184    * After result cached, this value will not be changed.
185    *
186    * If value is DONT_CHANGE, ImageVisualShaderFactory::GetShader never call ApplyNativeFragmentShader.
187    * Else, ImageVisualShaderFactory::GetShader will call ApplyNativeFragmentShader if native image source texture come.
188    */
189   ImageVisualShaderFeature::ChangeFragmentShader::Type mFragmentShaderNeedChange : 3;
190 };
191
192 } // namespace Internal
193
194 } // namespace Toolkit
195
196 } // namespace Dali
197
198 #endif // DALI_TOOLKIT_IMAGE_VISUAL_SHADER_FACTORY_H