Combine textvisual shader by TextShaderFactory
[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) 2022 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/object/base-object.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
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    * @return the reference to texture manager
90    */
91   Internal::TextureManager& GetTextureManager();
92
93 protected:
94   /**
95    * A reference counted object may only be deleted by calling Unreference()
96    */
97   ~VisualFactory() override;
98
99 private:
100   /**
101    * @brief Set the Broken Image url
102    * @param[in] styleManager The instance of StyleManager
103    */
104   void SetBrokenImageUrl(Toolkit::StyleManager& styleManager);
105
106   /**
107    * Get the factory cache, creating it if necessary.
108    */
109   Internal::VisualFactoryCache& GetFactoryCache();
110
111   /**
112    * Get the image visual shader factory, creating it if necessary.
113    */
114   ImageVisualShaderFactory& GetImageVisualShaderFactory();
115
116   /**
117    * Get the text visual shader factory, creating it if necessary.
118    */
119   TextVisualShaderFactory& GetTextVisualShaderFactory();
120
121   VisualFactory(const VisualFactory&) = delete;
122
123   VisualFactory& operator=(const VisualFactory& rhs) = delete;
124
125 private:
126   std::unique_ptr<VisualFactoryCache>       mFactoryCache;
127   std::unique_ptr<ImageVisualShaderFactory> mImageVisualShaderFactory;
128   std::unique_ptr<TextVisualShaderFactory>  mTextVisualShaderFactory;
129   SlotDelegate<VisualFactory>               mSlotDelegate;
130   bool                                      mDebugEnabled : 1;
131   bool                                      mPreMultiplyOnLoad : 1; ///< Local store for this flag
132 };
133
134 /**
135  * @brief Template to allow discard old visual, get new one and set it on stage if possible
136  *
137  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
138  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
139  * @SINCE_1_0.39
140  * @param[in] actor Actor for which the visual will be replaced
141  * @param[in,out] visual The visual to be replaced
142  * @param[in] param0 First template based argument passed to the visual factory
143  * @param[in] param1 Second template based argument passed to the visual factory
144  */
145 template<class ParameterType0, class ParameterType1>
146 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1)
147 {
148   if(actor)
149   {
150     Toolkit::GetImplementation(visual).SetOffScene(actor);
151   }
152   visual = Toolkit::VisualFactory::Get().CreateVisual(param0, param1);
153   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
154   {
155     Toolkit::GetImplementation(visual).SetOnScene(actor);
156   }
157 }
158
159 /**
160  * @brief Template to allow discard old visual, get new one and set it on stage if possible
161  *
162  * @tparam ParameterType The type of argument passed to the CreateVisual()
163  * @SINCE_1_0.39
164  * @param[in] actor Actor for which the visual will be replaced
165  * @param[in,out] visual The visual to be replaced
166  * @param[in] param Template based argument passed to the visual factory
167  */
168 template<class ParameterType>
169 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param)
170 {
171   if(actor && visual)
172   {
173     Toolkit::GetImplementation(visual).SetOffScene(actor);
174   }
175   visual = Toolkit::VisualFactory::Get().CreateVisual(param);
176   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
177   {
178     Toolkit::GetImplementation(visual).SetOnScene(actor);
179   }
180 }
181
182 } // namespace Internal
183
184 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
185 {
186   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
187
188   const BaseObject& handle = factory.GetBaseObject();
189
190   return static_cast<const Internal::VisualFactory&>(handle);
191 }
192
193 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
194 {
195   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
196
197   BaseObject& handle = factory.GetBaseObject();
198
199   return static_cast<Internal::VisualFactory&>(handle);
200 }
201
202 } // namespace Toolkit
203
204 } // namespace Dali
205
206 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */