[Tize] Change svg visual to load file using nanosvg
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
1 /*
2  * Copyright (c) 2018 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 // INTERNAL INCLUDES
22 #include <dali-toolkit/third-party/nanosvg/nanosvg.h>
23 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
24 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
25 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
26 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
27 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
28 #include <dali/devel-api/adaptor-framework/file-loader.h>
29
30 // EXTERNAL INCLUDES
31 #include <dali/public-api/common/stage.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 // property name
45 const char * const UNITS("px");
46
47 const char * const IMAGE_ATLASING( "atlasing" );
48
49 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
50 }
51
52 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
53 {
54   SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
55   svgVisual->ParseFromUrl( imageUrl );
56   svgVisual->SetProperties( properties );
57
58   return svgVisual;
59 }
60
61 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
62 {
63   SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
64   svgVisual->ParseFromUrl( imageUrl );
65
66   return svgVisual;
67 }
68
69 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
70 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
71   mImageVisualShaderFactory( shaderFactory ),
72   mAtlasRect( FULL_TEXTURE_RECT ),
73   mImageUrl( imageUrl ),
74   mParsedImage( NULL ),
75   mPlacementActor(),
76   mVisualSize(Vector2::ZERO),
77   mAttemptAtlasing( false )
78 {
79   // the rasterized image is with pre-multiplied alpha format
80   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
81 }
82
83 SvgVisual::~SvgVisual()
84 {
85   if( mParsedImage )
86   {
87     nsvgDelete( mParsedImage );
88   }
89 }
90
91 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
92 {
93   // url already passed in from constructor
94   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
95   {
96     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
97     if( keyValue.first.type == Property::Key::INDEX )
98     {
99       DoSetProperty( keyValue.first.indexKey, keyValue.second );
100     }
101     else if( keyValue.first == IMAGE_ATLASING )
102     {
103       DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
104     }
105   }
106 }
107
108 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
109 {
110   switch( index )
111   {
112     case Toolkit::ImageVisual::Property::ATLASING:
113     {
114       value.Get( mAttemptAtlasing );
115       break;
116     }
117   }
118 }
119
120 void SvgVisual::DoSetOnStage( Actor& actor )
121 {
122   Shader shader;
123   if( !mImpl->mCustomShader )
124   {
125     shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true );
126   }
127   else
128   {
129     shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
130                           mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
131                           mImpl->mCustomShader->mHints );
132
133     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
134   }
135
136   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
137   TextureSet textureSet = TextureSet::New();
138   mImpl->mRenderer = Renderer::New( geometry, shader );
139   mImpl->mRenderer.SetTextures( textureSet );
140
141   // Register transform properties
142   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
143
144   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
145
146   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
147   mPlacementActor = actor;
148
149   // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
150   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
151 }
152
153 void SvgVisual::DoSetOffStage( Actor& actor )
154 {
155   mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
156
157   actor.RemoveRenderer( mImpl->mRenderer );
158   mImpl->mRenderer.Reset();
159   mPlacementActor.Reset();
160
161   // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
162   mVisualSize = Vector2::ZERO;
163 }
164
165 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
166 {
167   if( mParsedImage )
168   {
169     naturalSize.x = mParsedImage->width;
170     naturalSize.y = mParsedImage->height;
171   }
172   else
173   {
174     naturalSize = Vector2::ZERO;
175   }
176 }
177
178 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
179 {
180   map.Clear();
181   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
182   if( mImageUrl.IsValid() )
183   {
184     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
185     map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
186   }
187 }
188
189 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
190 {
191   mImageUrl = imageUrl;
192   if( mImageUrl.IsLocalResource() )
193   {
194     Vector2 dpi = Stage::GetCurrent().GetDpi();
195     float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
196     Dali::Vector<char> buffer;
197
198     mParsedImage = nsvgParseFromFile( mImageUrl.GetUrl().c_str(), UNITS, meanDpi );
199   }
200 }
201
202 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
203 {
204   // Do nothing
205 }
206
207 void SvgVisual::AddRasterizationTask( const Vector2& size )
208 {
209   if( mImpl->mRenderer )
210   {
211     unsigned int width = static_cast<unsigned int>(size.width);
212     unsigned int height = static_cast<unsigned int>( size.height );
213
214     Vector2 dpi = Stage::GetCurrent().GetDpi();
215     float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
216
217     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height );
218     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
219   }
220 }
221
222 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
223 {
224   if( mParsedImage == NULL)
225   {
226     mParsedImage = parsedSvg;
227   }
228
229   if( mParsedImage && IsOnStage() )
230   {
231     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
232     if( mImpl->mFlags |= Impl::IS_ATLASING_APPLIED )
233     {
234       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
235     }
236
237     TextureSet textureSet;
238
239     if( mAttemptAtlasing && !mImpl->mCustomShader )
240     {
241       Vector4 atlasRect;
242       textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
243       if( textureSet ) // atlasing
244       {
245         if( textureSet != currentTextureSet )
246         {
247           mImpl->mRenderer.SetTextures( textureSet );
248         }
249         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
250         mAtlasRect = atlasRect;
251         mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
252       }
253     }
254
255     if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
256     {
257       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
258                                       rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
259       texture.Upload( rasterizedPixelData );
260       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
261
262       if( mAtlasRect == FULL_TEXTURE_RECT )
263       {
264         textureSet = currentTextureSet;
265       }
266       else
267       {
268         textureSet = TextureSet::New();
269         mImpl->mRenderer.SetTextures( textureSet );
270
271         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
272         mAtlasRect = FULL_TEXTURE_RECT;
273       }
274
275       if( textureSet )
276       {
277         textureSet.SetTexture( 0, texture );
278       }
279     }
280
281     // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
282     Actor actor = mPlacementActor.GetHandle();
283     if( actor )
284     {
285       actor.AddRenderer( mImpl->mRenderer );
286       // reset the weak handle so that the renderer only get added to actor once
287       mPlacementActor.Reset();
288     }
289
290    // Svg loaded and ready to display
291    ResourceReady( Toolkit::Visual::ResourceStatus::READY );
292   }
293 }
294
295 void SvgVisual::OnSetTransform()
296 {
297   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
298
299   if( IsOnStage() )
300   {
301     if( visualSize != mVisualSize )
302     {
303       AddRasterizationTask( visualSize );
304       mVisualSize = visualSize;
305     }
306   }
307
308   if(mImpl->mRenderer)
309   {
310     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
311   }
312 }
313
314 } // namespace Internal
315
316 } // namespace Toolkit
317
318 } // namespace Dali