[Tizen] Apply precompile shader
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-impl.h
1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H
3
4 /*
5  * Copyright (c) 2023 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 #include <dali/integration-api/shader-precompiler.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/object/base-object.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
27 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
28 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
29 #include <dali-toolkit/public-api/styling/style-manager.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class VisualFactoryCache;
38 class ImageVisualShaderFactory;
39 class TextVisualShaderFactory;
40
41 /**
42  * @copydoc Toolkit::VisualFactory
43  */
44 class VisualFactory : public BaseObject
45 {
46 public:
47   /**
48    * @brief Constructor
49    *
50    * @param[in] debugEnabled If true, use debug renderer to replace all the concrete renderer.
51    */
52   VisualFactory(bool debugEnabled);
53
54   /**
55    * @brief StyleChanged callback
56    *
57    * @param[in] styleManager Handle for style manager.
58    * @param[in] type Style change type.
59    */
60   void OnStyleChangedSignal(Toolkit::StyleManager styleManager, StyleChange::Type type);
61
62   /**
63    * @brief BrokenImageChanged callback
64    *
65    * @param[in] styleManager Handle for style manager.
66    */
67   void OnBrokenImageChangedSignal(Toolkit::StyleManager styleManager);
68
69   /**
70    * @copydoc Toolkit::VisualFactory::CreateVisual( const Property::Map& )
71    */
72   Toolkit::Visual::Base CreateVisual(const Property::Map& propertyMap);
73
74   /**
75    * @copydoc Toolkit::VisualFactory::CreateVisual( const std::string&, ImageDimensions )
76    */
77   Toolkit::Visual::Base CreateVisual(const std::string& image, ImageDimensions size);
78
79   /**
80    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
81    */
82   void SetPreMultiplyOnLoad(bool preMultiply);
83
84   /**
85    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
86    */
87   bool GetPreMultiplyOnLoad() const;
88
89   /**
90    * @copydoc Toolkit::VisualFactory::DiscardVisual()
91    */
92   void DiscardVisual(Toolkit::Visual::Base visual);
93
94   /**
95    * @copydoc Toolkit::VisualFactory::UsePreCompiledShader()
96    */
97   void UsePreCompiledShader();
98
99   /**
100    * @return the reference to texture manager
101    */
102   Internal::TextureManager& GetTextureManager();
103
104 protected:
105   /**
106    * A reference counted object may only be deleted by calling Unreference()
107    */
108   ~VisualFactory() override;
109
110 private:
111   /**
112    * @brief Set the Broken Image url
113    * @param[in] styleManager The instance of StyleManager
114    */
115   void SetBrokenImageUrl(Toolkit::StyleManager& styleManager);
116
117   /**
118    * Get the factory cache, creating it if necessary.
119    */
120   Internal::VisualFactoryCache& GetFactoryCache();
121
122   /**
123    * Get the image visual shader factory, creating it if necessary.
124    */
125   ImageVisualShaderFactory& GetImageVisualShaderFactory();
126
127   /**
128    * Get the text visual shader factory, creating it if necessary.
129    */
130   TextVisualShaderFactory& GetTextVisualShaderFactory();
131
132   /**
133    * @brief Callbacks called for clear discarded visuals.
134    */
135   void OnDiscardCallback();
136
137   /**
138    * @brief Register idle callback for discard visuals if need.
139    */
140   void RegisterDiscardCallback();
141
142   VisualFactory(const VisualFactory&) = delete;
143
144   VisualFactory& operator=(const VisualFactory& rhs) = delete;
145
146 private:
147   std::unique_ptr<VisualFactoryCache>       mFactoryCache;
148   std::unique_ptr<ImageVisualShaderFactory> mImageVisualShaderFactory;
149   std::unique_ptr<TextVisualShaderFactory>  mTextVisualShaderFactory;
150   SlotDelegate<VisualFactory>               mSlotDelegate;
151   CallbackBase*                             mIdleCallback;
152
153   using DiscardedVisualContainer = std::vector<Toolkit::Visual::Base>;
154   DiscardedVisualContainer mDiscardedVisuals{};
155
156   bool mDebugEnabled : 1;
157   bool mPreMultiplyOnLoad : 1; ///< Local store for this flag
158   bool mPrecompiledShaderRequested{false};
159 };
160
161 /**
162  * @brief Template to allow discard old visual, get new one and set it on stage if possible
163  *
164  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
165  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
166  * @SINCE_1_0.39
167  * @param[in] actor Actor for which the visual will be replaced
168  * @param[in,out] visual The visual to be replaced
169  * @param[in] param0 First template based argument passed to the visual factory
170  * @param[in] param1 Second template based argument passed to the visual factory
171  */
172 template<class ParameterType0, class ParameterType1>
173 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1)
174 {
175   if(actor)
176   {
177     Toolkit::GetImplementation(visual).SetOffScene(actor);
178   }
179   visual = Toolkit::VisualFactory::Get().CreateVisual(param0, param1);
180   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
181   {
182     Toolkit::GetImplementation(visual).SetOnScene(actor);
183   }
184 }
185
186 /**
187  * @brief Template to allow discard old visual, get new one and set it on stage if possible
188  *
189  * @tparam ParameterType The type of argument passed to the CreateVisual()
190  * @SINCE_1_0.39
191  * @param[in] actor Actor for which the visual will be replaced
192  * @param[in,out] visual The visual to be replaced
193  * @param[in] param Template based argument passed to the visual factory
194  */
195 template<class ParameterType>
196 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param)
197 {
198   if(actor && visual)
199   {
200     Toolkit::GetImplementation(visual).SetOffScene(actor);
201   }
202   visual = Toolkit::VisualFactory::Get().CreateVisual(param);
203   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
204   {
205     Toolkit::GetImplementation(visual).SetOnScene(actor);
206   }
207 }
208
209 } // namespace Internal
210
211 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
212 {
213   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
214
215   const BaseObject& handle = factory.GetBaseObject();
216
217   return static_cast<const Internal::VisualFactory&>(handle);
218 }
219
220 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
221 {
222   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
223
224   BaseObject& handle = factory.GetBaseObject();
225
226   return static_cast<Internal::VisualFactory&>(handle);
227 }
228
229 } // namespace Toolkit
230
231 } // namespace Dali
232
233 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */