[Tizen] Support to use NPatchImage in brokenImage
[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) 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 #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
39 /**
40  * @copydoc Toolkit::VisualFactory
41  */
42 class VisualFactory : public BaseObject
43 {
44 public:
45   /**
46    * @brief Constructor
47    *
48    * @param[in] debugEnabled If true, use debug renderer to replace all the concrete renderer.
49    */
50   VisualFactory(bool debugEnabled);
51
52   /**
53    * @brief StyleChanged callback
54    *
55    * @param[in] styleManager Handle for style manager.
56    * @param[in] type Style change type.
57    */
58   void OnStyleChangedSignal(Toolkit::StyleManager styleManager, StyleChange::Type type);
59
60   /**
61    * @copydoc Toolkit::VisualFactory::CreateVisual( const Property::Map& )
62    */
63   Toolkit::Visual::Base CreateVisual(const Property::Map& propertyMap);
64
65   /**
66    * @copydoc Toolkit::VisualFactory::CreateVisual( const std::string&, ImageDimensions )
67    */
68   Toolkit::Visual::Base CreateVisual(const std::string& image, ImageDimensions size);
69
70   /**
71    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
72    */
73   void SetPreMultiplyOnLoad(bool preMultiply);
74
75   /**
76    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
77    */
78   bool GetPreMultiplyOnLoad() const;
79
80   /**
81    * @return the reference to texture manager
82    */
83   Internal::TextureManager& GetTextureManager();
84
85 protected:
86   /**
87    * A reference counted object may only be deleted by calling Unreference()
88    */
89   ~VisualFactory() override;
90
91 private:
92   /**
93    * @brief Set the Broken Image url
94    *
95    */
96   void SetBrokenImageUrl(Toolkit::StyleManager& styleManager);
97
98   /**
99    * Get the factory cache, creating it if necessary.
100    */
101   Internal::VisualFactoryCache& GetFactoryCache();
102
103   /**
104    * Get the image visual shader factory, creating it if necessary.
105    */
106   ImageVisualShaderFactory& GetImageVisualShaderFactory();
107
108   VisualFactory(const VisualFactory&) = delete;
109
110   VisualFactory& operator=(const VisualFactory& rhs) = delete;
111
112 private:
113   std::unique_ptr<VisualFactoryCache>       mFactoryCache;
114   std::unique_ptr<ImageVisualShaderFactory> mImageVisualShaderFactory;
115   SlotDelegate<VisualFactory>               mSlotDelegate;
116   bool                                      mDebugEnabled : 1;
117   bool                                      mPreMultiplyOnLoad : 1; ///< Local store for this flag
118 };
119
120 /**
121  * @brief Template to allow discard old visual, get new one and set it on stage if possible
122  *
123  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
124  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
125  * @SINCE_1_0.39
126  * @param[in] actor Actor for which the visual will be replaced
127  * @param[in,out] visual The visual to be replaced
128  * @param[in] param0 First template based argument passed to the visual factory
129  * @param[in] param1 Second template based argument passed to the visual factory
130  */
131 template<class ParameterType0, class ParameterType1>
132 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1)
133 {
134   if(actor)
135   {
136     Toolkit::GetImplementation(visual).SetOffScene(actor);
137   }
138   visual = Toolkit::VisualFactory::Get().CreateVisual(param0, param1);
139   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
140   {
141     Toolkit::GetImplementation(visual).SetOnScene(actor);
142   }
143 }
144
145 /**
146  * @brief Template to allow discard old visual, get new one and set it on stage if possible
147  *
148  * @tparam ParameterType The type of argument passed to the CreateVisual()
149  * @SINCE_1_0.39
150  * @param[in] actor Actor for which the visual will be replaced
151  * @param[in,out] visual The visual to be replaced
152  * @param[in] param Template based argument passed to the visual factory
153  */
154 template<class ParameterType>
155 void InitializeVisual(Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param)
156 {
157   if(actor && visual)
158   {
159     Toolkit::GetImplementation(visual).SetOffScene(actor);
160   }
161   visual = Toolkit::VisualFactory::Get().CreateVisual(param);
162   if(visual && actor && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
163   {
164     Toolkit::GetImplementation(visual).SetOnScene(actor);
165   }
166 }
167
168 } // namespace Internal
169
170 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
171 {
172   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
173
174   const BaseObject& handle = factory.GetBaseObject();
175
176   return static_cast<const Internal::VisualFactory&>(handle);
177 }
178
179 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
180 {
181   DALI_ASSERT_ALWAYS(factory && "VisualFactory handle is empty");
182
183   BaseObject& handle = factory.GetBaseObject();
184
185   return static_cast<Internal::VisualFactory&>(handle);
186 }
187
188 } // namespace Toolkit
189
190 } // namespace Dali
191
192 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */