Merge "(Automated Tests) Sync after stencil/depth buffer changes" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-impl.cpp
1  /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/images/image.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33 #include <dali-toolkit/internal/visuals/border/border-visual.h>
34 #include <dali-toolkit/internal/visuals/color/color-visual.h>
35 #include <dali-toolkit/internal/visuals/gradient/gradient-visual.h>
36 #include <dali-toolkit/internal/visuals/animated-gradient/animated-gradient-visual.h>
37 #include <dali-toolkit/internal/visuals/image/image-visual.h>
38 #include <dali-toolkit/internal/visuals/mesh/mesh-visual.h>
39 #include <dali-toolkit/internal/visuals/npatch/npatch-visual.h>
40 #include <dali-toolkit/internal/visuals/primitive/primitive-visual.h>
41 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
42 #include <dali-toolkit/internal/visuals/text/text-visual.h>
43 #include <dali-toolkit/internal/visuals/animated-image/animated-image-visual.h>
44 #include <dali-toolkit/internal/visuals/wireframe/wireframe-visual.h>
45 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
46 #include <dali-toolkit/internal/visuals/visual-url.h>
47 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
48
49 namespace Dali
50 {
51
52 namespace Toolkit
53 {
54
55 namespace Internal
56 {
57
58 namespace
59 {
60
61 BaseHandle Create()
62 {
63   BaseHandle handle = Toolkit::VisualFactory::Get();
64
65   return handle;
66 }
67
68 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
69 DALI_TYPE_REGISTRATION_END()
70
71 } // namespace
72
73 VisualFactory::VisualFactory( bool debugEnabled )
74 :mDebugEnabled( debugEnabled )
75 {
76 }
77
78 VisualFactory::~VisualFactory()
79 {
80 }
81
82 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
83 {
84   // Create factory cache if it hasn't already been
85   if( !mFactoryCache )
86   {
87     mFactoryCache = new VisualFactoryCache();
88   }
89
90   Visual::BasePtr visualPtr;
91
92   Property::Value* typeValue = propertyMap.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE );
93   Toolkit::DevelVisual::Type visualType = Toolkit::DevelVisual::IMAGE; // Default to IMAGE type.
94   if( typeValue )
95   {
96     Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
97   }
98
99   switch( visualType )
100   {
101     case Toolkit::Visual::BORDER:
102     {
103       visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
104       break;
105     }
106
107     case Toolkit::Visual::COLOR:
108     {
109       visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
110       break;
111     }
112
113     case Toolkit::Visual::GRADIENT:
114     {
115       visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
116       break;
117     }
118
119     case Toolkit::Visual::IMAGE:
120     {
121       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
122       std::string imageUrl;
123       if( imageURLValue )
124       {
125         if( imageURLValue->Get( imageUrl ) )
126         {
127           if( !imageUrl.empty() )
128           {
129             VisualUrl visualUrl( imageUrl );
130
131             switch( visualUrl.GetType() )
132             {
133               case VisualUrl::N_PATCH:
134               {
135                 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
136                 break;
137               }
138               case VisualUrl::SVG:
139               {
140                 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
141                 break;
142               }
143               case VisualUrl::GIF:
144               {
145                 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
146                 break;
147               }
148               case VisualUrl::REGULAR_IMAGE:
149               {
150                 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
151                 break;
152               }
153             }
154           }
155         }
156         else
157         {
158           Property::Array* array = imageURLValue->GetArray();
159           if( array )
160           {
161             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
162           }
163         }
164       }
165       break;
166     }
167
168     case Toolkit::Visual::MESH:
169     {
170       visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
171       break;
172     }
173
174     case Toolkit::Visual::PRIMITIVE:
175     {
176       visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
177       break;
178     }
179
180     case Toolkit::Visual::WIREFRAME:
181     {
182       visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
183       break;
184     }
185
186     case Toolkit::Visual::TEXT:
187     {
188       visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
189       break;
190     }
191
192     case Toolkit::Visual::N_PATCH:
193     {
194       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
195       std::string imageUrl;
196       if( imageURLValue && imageURLValue->Get( imageUrl ) )
197       {
198         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
199       }
200       break;
201     }
202
203     case Toolkit::Visual::SVG:
204     {
205       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
206       std::string imageUrl;
207       if( imageURLValue && imageURLValue->Get( imageUrl ) )
208       {
209         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
210       }
211       break;
212     }
213
214     case Toolkit::Visual::ANIMATED_IMAGE:
215     {
216       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
217       std::string imageUrl;
218       if( imageURLValue )
219       {
220         if( imageURLValue->Get( imageUrl ) )
221         {
222           visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
223         }
224         else
225         {
226           Property::Array* array = imageURLValue->GetArray();
227           if( array )
228           {
229             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
230           }
231         }
232       }
233       break;
234     }
235
236     case Toolkit::DevelVisual::ANIMATED_GRADIENT:
237     {
238       visualPtr = AnimatedGradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
239       break;
240     }
241   }
242
243   if( !visualPtr )
244   {
245     DALI_LOG_ERROR( "Renderer type unknown\n" );
246   }
247
248   if( mDebugEnabled && visualType !=  Toolkit::DevelVisual::WIREFRAME )
249   {
250     //Create a WireframeVisual if we have debug enabled
251     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
252   }
253
254   return Toolkit::Visual::Base( visualPtr.Get() );
255 }
256
257 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
258 {
259   if( !mFactoryCache )
260   {
261     mFactoryCache = new VisualFactoryCache();
262   }
263
264   Visual::BasePtr visualPtr;
265
266   if( image )
267   {
268     NinePatchImage npatchImage = NinePatchImage::DownCast( image );
269     if( npatchImage )
270     {
271       visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
272     }
273     else
274     {
275       visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
276     }
277   }
278
279   if( mDebugEnabled )
280   {
281     //Create a WireframeVisual if we have debug enabled
282     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
283   }
284
285   return Toolkit::Visual::Base( visualPtr.Get() );
286 }
287
288 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
289 {
290   if( !mFactoryCache )
291   {
292     mFactoryCache = new VisualFactoryCache();
293   }
294
295   Visual::BasePtr visualPtr;
296
297   if( !url.empty() )
298   {
299     // first resolve url type to know which visual to create
300     VisualUrl visualUrl( url );
301     switch( visualUrl.GetType() )
302     {
303       case VisualUrl::N_PATCH:
304       {
305         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl );
306         break;
307       }
308       case VisualUrl::SVG:
309       {
310         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl );
311         break;
312       }
313       case VisualUrl::GIF:
314       {
315         visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl );
316         break;
317       }
318       case VisualUrl::REGULAR_IMAGE:
319       {
320         visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, size );
321         break;
322       }
323     }
324   }
325
326   if( mDebugEnabled )
327   {
328     //Create a WireframeVisual if we have debug enabled
329     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
330   }
331
332   return Toolkit::Visual::Base( visualPtr.Get() );
333 }
334
335 Internal::TextureManager& VisualFactory::GetTextureManager()
336 {
337   if( !mFactoryCache )
338   {
339     mFactoryCache = new VisualFactoryCache();
340   }
341   return mFactoryCache->GetTextureManager();
342 }
343
344 } // namespace Internal
345
346 } // namespace Toolkit
347
348 } // namespace Dali