Added devel-api mirroring of public enumeration.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
1 /*
2  * Copyright (c) 2016 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
18 // CLASS HEADER
19 #include "svg-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/buffer-image.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/devel-api/images/atlas.h>
26 #include <dali/devel-api/images/texture-set-image.h>
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
32 #include <dali-toolkit/third-party/nanosvg/nanosvg.h>
33 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
34 #include <dali-toolkit/internal/visuals/image/image-visual.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
39
40
41 namespace
42 {
43 const char * const UNITS("px");
44
45 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
46 }
47
48 namespace Dali
49 {
50
51 namespace Toolkit
52 {
53
54 namespace Internal
55 {
56
57 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache )
58 {
59   return new SvgVisual( factoryCache );
60 }
61
62 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size )
63 {
64   SvgVisual* svgVisual = new SvgVisual( factoryCache );
65   svgVisual->ParseFromUrl( imageUrl, size );
66   return svgVisual;
67 }
68
69 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
70 : Visual::Base( factoryCache ),
71   mAtlasRect( FULL_TEXTURE_RECT ),
72   mImageUrl(),
73   mParsedImage( NULL ),
74   mPlacementActor()
75 {
76   // the rasterized image is with pre-multiplied alpha format
77   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
78 }
79
80 SvgVisual::~SvgVisual()
81 {
82   if( mParsedImage )
83   {
84     nsvgDelete( mParsedImage );
85   }
86 }
87
88 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
89 {
90   Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
91   if( imageURLValue )
92   {
93     std::string imageUrl;
94     if( imageURLValue->Get( imageUrl ) )
95     {
96       ParseFromUrl( imageUrl );
97     }
98     else
99     {
100       DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
101     }
102   }
103 }
104
105 void SvgVisual::DoSetOnStage( Actor& actor )
106 {
107   Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
108   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
109   if( !geometry )
110   {
111     geometry =  mFactoryCache.CreateQuadGeometry();
112     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
113   }
114   TextureSet textureSet = TextureSet::New();
115   mImpl->mRenderer = Renderer::New( geometry, shader );
116   mImpl->mRenderer.SetTextures( textureSet );
117
118   if( mImpl->mSize != Vector2::ZERO && mParsedImage )
119   {
120     AddRasterizationTask( mImpl->mSize );
121   }
122
123   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
124   mPlacementActor = actor;
125 }
126
127 void SvgVisual::DoSetOffStage( Actor& actor )
128 {
129   mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
130
131   actor.RemoveRenderer( mImpl->mRenderer );
132   mImpl->mRenderer.Reset();
133   mPlacementActor.Reset();
134 }
135
136 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
137 {
138   if( mParsedImage )
139   {
140     naturalSize.x = mParsedImage->width;
141     naturalSize.y = mParsedImage->height;
142   }
143   else
144   {
145     naturalSize = Vector2::ZERO;
146   }
147 }
148
149 void SvgVisual::SetSize( const Vector2& size )
150 {
151   if(mImpl->mSize != size && mParsedImage && IsOnStage() )
152   {
153     AddRasterizationTask( size );
154   }
155   mImpl->mSize = size;
156 }
157
158 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
159 {
160   map.Clear();
161   map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
162   if( !mImageUrl.empty() )
163   {
164     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
165   }
166 }
167
168 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
169 {
170   // TODO
171 }
172
173 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
174 {
175   // TODO
176   return Dali::Property::Value();
177 }
178
179 void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size )
180 {
181   mImageUrl = imageUrl;
182
183   Vector2 dpi = Stage::GetCurrent().GetDpi();
184   float meanDpi = (dpi.height + dpi.width) * 0.5f;
185   mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi );
186
187   if( size.GetWidth() != 0u && size.GetHeight() != 0u)
188   {
189     mImpl->mSize.x = size.GetWidth();
190     mImpl->mSize.y = size.GetHeight();
191   }
192 }
193
194 void SvgVisual::AddRasterizationTask( const Vector2& size )
195 {
196   if( mImpl->mRenderer && mParsedImage )
197   {
198     unsigned int width = static_cast<unsigned int>(size.width);
199     unsigned int height = static_cast<unsigned int>( size.height );
200     BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
201
202     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
203     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
204   }
205 }
206
207 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
208 {
209   if( IsOnStage()  )
210   {
211     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
212     if( mAtlasRect != FULL_TEXTURE_RECT )
213     {
214       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
215     }
216
217     Vector4 atlasRect;
218     TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
219     if( textureSet ) // atlasing
220     {
221       if( textureSet != currentTextureSet )
222       {
223         mImpl->mRenderer.SetTextures( textureSet );
224       }
225       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
226       mAtlasRect = atlasRect;
227       mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
228     }
229     else // no atlasing
230     {
231       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
232                                       rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
233       texture.Upload( rasterizedPixelData );
234       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
235
236       if( mAtlasRect == FULL_TEXTURE_RECT )
237       {
238         textureSet = currentTextureSet;
239       }
240       else
241       {
242         textureSet = TextureSet::New();
243         mImpl->mRenderer.SetTextures( textureSet );
244
245         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
246         mAtlasRect = FULL_TEXTURE_RECT;
247       }
248
249       if( textureSet )
250       {
251         textureSet.SetTexture( 0, texture );
252       }
253     }
254
255     // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
256     Actor actor = mPlacementActor.GetHandle();
257     if( actor )
258     {
259       actor.AddRenderer( mImpl->mRenderer );
260       // reset the weak handle so that the renderer only get added to actor once
261       mPlacementActor.Reset();
262     }
263   }
264 }
265
266
267 } // namespace Internal
268
269 } // namespace Toolkit
270
271 } // namespace Dali