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