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