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