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