[dali_2.3.20] Merge branch 'devel/master'
[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/public-api/common/vector-wrapper.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/integration-api/adaptor-framework/shader-precompiler.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    * @brief Get the default shader source.
119    * @param[in] shaders shaderList for precompile
120    */
121   void GetPreCompiledShader(RawShaderData& shaders);
122
123   /**
124    * Get the factory cache, creating it if necessary.
125    */
126   Internal::VisualFactoryCache& GetFactoryCache();
127
128   /**
129    * Get the image visual shader factory, creating it if necessary.
130    */
131   ImageVisualShaderFactory& GetImageVisualShaderFactory();
132
133   /**
134    * Get the text visual shader factory, creating it if necessary.
135    */
136   TextVisualShaderFactory& GetTextVisualShaderFactory();
137
138   /**
139    * @brief Callbacks called for clear discarded visuals.
140    */
141   void OnDiscardCallback();
142
143   /**
144    * @brief Register idle callback for discard visuals if need.
145    */
146   void RegisterDiscardCallback();
147
148   VisualFactory(const VisualFactory&) = delete;
149
150   VisualFactory& operator=(const VisualFactory& rhs) = delete;
151
152 private:
153   std::unique_ptr<VisualFactoryCache>       mFactoryCache;
154   std::unique_ptr<ImageVisualShaderFactory> mImageVisualShaderFactory;
155   std::unique_ptr<TextVisualShaderFactory>  mTextVisualShaderFactory;
156   SlotDelegate<VisualFactory>               mSlotDelegate;
157   CallbackBase*                             mIdleCallback;
158   using DiscardedVisualContainer = std::vector<Toolkit::Visual::Base>;
159   DiscardedVisualContainer mDiscardedVisuals{};
160   bool                                      mDebugEnabled : 1;
161   bool                                      mPreMultiplyOnLoad : 1; ///< Local store for this flag
162   bool                                      mPrecompiledShaderRequested : 1;
163 };
164
165 /**
166  * @brief Template to allow discard old visual, get new one and set it on stage if possible
167  *
168  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
169  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
170  * @SINCE_1_0.39
171  * @param[in] actor Actor for which the visual will be replaced
172  * @param[in,out] visual The visual to be replaced
173  * @param[in] param0 First template based argument passed to the visual factory
174  * @param[in] param1 Second template based argument passed to the visual factory
175  */
176 template<class ParameterType0, class ParameterType1>
177 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1)
178 {
179   if(actor)
180   {
181     Toolkit::GetImplementation(visual).SetOffScene(actor);
182   }
183   visual = Toolkit::VisualFactory::Get().CreateVisual(param0, param1);
184   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
185   {
186     Toolkit::GetImplementation(visual).SetOnScene(actor);
187   }
188 }
189
190 /**
191  * @brief Template to allow discard old visual, get new one and set it on stage if possible
192  *
193  * @tparam ParameterType The type of argument passed to the CreateVisual()
194  * @SINCE_1_0.39
195  * @param[in] actor Actor for which the visual will be replaced
196  * @param[in,out] visual The visual to be replaced
197  * @param[in] param Template based argument passed to the visual factory
198  */
199 template<class ParameterType>
200 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param)
201 {
202   if(actor && visual)
203   {
204     Toolkit::GetImplementation(visual).SetOffScene(actor);
205   }
206   visual = Toolkit::VisualFactory::Get().CreateVisual(param);
207   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
208   {
209     Toolkit::GetImplementation(visual).SetOnScene(actor);
210   }
211 }
212
213 } // namespace Internal
214
215 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
216 {
217   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
218
219   const BaseObject& handle = factory.GetBaseObject();
220
221   return static_cast<const Internal::VisualFactory&>(handle);
222 }
223
224 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
225 {
226   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
227
228   BaseObject& handle = factory.GetBaseObject();
229
230   return static_cast<Internal::VisualFactory&>(handle);
231 }
232
233 } // namespace Toolkit
234
235 } // namespace Dali
236
237 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */