999a0c40daf7913ab39b41db1c6e2a28482b4f33
[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) 2017 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/visual-factory/visual-factory.h>
25 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
26 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 class VisualFactoryCache;
38
39 /**
40  * @copydoc Toolkit::VisualFactory
41  */
42 class VisualFactory : public BaseObject
43 {
44 public:
45
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    * @copydoc Toolkit::VisualFactory::CreateVisual( const Property::Map& )
55    */
56   Toolkit::Visual::Base CreateVisual( const Property::Map& propertyMap );
57
58   /**
59    * @copydoc Toolkit::VisualFactory::CreateVisual( const Image& )
60    */
61   Toolkit::Visual::Base CreateVisual( const Image& image );
62
63   /**
64    * @copydoc Toolkit::VisualFactory::CreateVisual( const std::string&, ImageDimensions )
65    */
66   Toolkit::Visual::Base CreateVisual( const std::string& image, ImageDimensions size );
67
68   /**
69    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
70    */
71   void SetPreMultiplyOnLoad( bool preMultiply );
72
73   /**
74    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
75    */
76   bool GetPreMultiplyOnLoad() const;
77
78   /**
79    * @return the reference to texture manager
80    */
81   Internal::TextureManager& GetTextureManager();
82
83 protected:
84
85   /**
86    * A reference counted object may only be deleted by calling Unreference()
87    */
88   virtual ~VisualFactory();
89
90 private:
91   /**
92    * Get the factory cache, creating it if necessary.
93    */
94   Internal::VisualFactoryCache& GetFactoryCache();
95
96   VisualFactory(const VisualFactory&) = delete;
97
98   VisualFactory& operator=(const VisualFactory& rhs) = delete;
99
100 private:
101   std::unique_ptr<VisualFactoryCache> mFactoryCache;
102   bool                                mDebugEnabled:1;
103   bool                                mPreMultiplyOnLoad:1; ///< Local store for this flag
104 };
105
106 /**
107  * @brief Template to allow discard old visual, get new one and set it on stage if possible
108  *
109  * @tparam ParameterType0 The type of first argument passed to the CreateVisual()
110  * @tparam ParameterType1 The type of second argument passed to the CreateVisual()
111  * @SINCE_1_0.39
112  * @param[in] actor Actor for which the visual will be replaced
113  * @param[in,out] visual The visual to be replaced
114  * @param[in] param0 First template based argument passed to the visual factory
115  * @param[in] param1 Second template based argument passed to the visual factory
116  */
117 template< class ParameterType0, class ParameterType1 >
118 void InitializeVisual( Actor& actor, Toolkit::Visual::Base& visual, ParameterType0& param0, ParameterType1& param1 )
119 {
120   if( actor )
121   {
122     Toolkit::GetImplementation(visual).SetOffStage( actor );
123   }
124   visual = Toolkit::VisualFactory::Get().CreateVisual( param0, param1 );
125   if( visual && actor && actor.OnStage() )
126   {
127     Toolkit::GetImplementation(visual).SetOnStage(actor);
128   }
129 }
130
131 /**
132  * @brief Template to allow discard old visual, get new one and set it on stage if possible
133  *
134  * @tparam ParameterType The type of argument passed to the CreateVisual()
135  * @SINCE_1_0.39
136  * @param[in] actor Actor for which the visual will be replaced
137  * @param[in,out] visual The visual to be replaced
138  * @param[in] param Template based argument passed to the visual factory
139  */
140 template< class ParameterType >
141 void InitializeVisual( Actor& actor, Toolkit::Visual::Base& visual, ParameterType& param )
142 {
143   if( actor && visual )
144   {
145     Toolkit::GetImplementation(visual).SetOffStage( actor );
146   }
147   visual =  Toolkit::VisualFactory::Get().CreateVisual( param );
148   if( visual && actor && actor.OnStage() )
149   {
150     Toolkit::GetImplementation(visual).SetOnStage(actor);
151   }
152 }
153
154 } // namespace Internal
155
156 inline const Internal::VisualFactory& GetImplementation(const Toolkit::VisualFactory& factory)
157 {
158   DALI_ASSERT_ALWAYS( factory && "VisualFactory handle is empty" );
159
160   const BaseObject& handle = factory.GetBaseObject();
161
162   return static_cast<const Internal::VisualFactory&>(handle);
163 }
164
165 inline Internal::VisualFactory& GetImplementation(Toolkit::VisualFactory& factory)
166 {
167   DALI_ASSERT_ALWAYS( factory && "VisualFactory handle is empty" );
168
169   BaseObject& handle = factory.GetBaseObject();
170
171   return static_cast<Internal::VisualFactory&>(handle);
172 }
173
174 } // namespace Toolkit
175
176 } // namespace Dali
177
178 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_IMPL_H */