Set mParsedImage of svg-visual
[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 // 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::DoCreateInstancePropertyMap( Property::Map& map ) const
190 {
191   // Do nothing
192 }
193
194 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
195 {
196   mImageUrl = imageUrl;
197   if( mImageUrl.IsLocalResource() )
198   {
199     Vector2 dpi = Stage::GetCurrent().GetDpi();
200     float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
201     Dali::Vector<char> buffer;
202     if ( Dali::FileLoader::ReadFile( mImageUrl.GetUrl(), buffer ) )
203     {
204       buffer.PushBack( '\0' );
205       mParsedImage = nsvgParse( buffer.Begin(), UNITS, meanDpi );
206     }
207   }
208 }
209
210 void SvgVisual::AddRasterizationTask( const Vector2& size )
211 {
212   if( mImpl->mRenderer )
213   {
214     unsigned int width = static_cast<unsigned int>(size.width);
215     unsigned int height = static_cast<unsigned int>( size.height );
216
217     Vector2 dpi = Stage::GetCurrent().GetDpi();
218     float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
219
220     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, mImageUrl, meanDpi, width, height );
221     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
222   }
223 }
224
225 void SvgVisual::ApplyRasterizedImage( NSVGimage* parsedSvg, PixelData rasterizedPixelData )
226 {
227   if( mParsedImage == NULL)
228   {
229     mParsedImage = parsedSvg;
230   }
231
232   if( mParsedImage && IsOnStage() )
233   {
234     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
235     if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
236     {
237       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
238     }
239
240     TextureSet textureSet;
241
242     if( mAttemptAtlasing && !mImpl->mCustomShader )
243     {
244       Vector4 atlasRect;
245       textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
246       if( textureSet ) // atlasing
247       {
248         if( textureSet != currentTextureSet )
249         {
250           mImpl->mRenderer.SetTextures( textureSet );
251         }
252         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
253         mAtlasRect = atlasRect;
254         mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
255       }
256     }
257
258     if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
259     {
260       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
261                                       rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
262       texture.Upload( rasterizedPixelData );
263       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
264
265       if( mAtlasRect == FULL_TEXTURE_RECT )
266       {
267         textureSet = currentTextureSet;
268       }
269       else
270       {
271         textureSet = TextureSet::New();
272         mImpl->mRenderer.SetTextures( textureSet );
273
274         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
275         mAtlasRect = FULL_TEXTURE_RECT;
276       }
277
278       if( textureSet )
279       {
280         textureSet.SetTexture( 0, texture );
281       }
282     }
283
284     // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
285     Actor actor = mPlacementActor.GetHandle();
286     if( actor )
287     {
288       actor.AddRenderer( mImpl->mRenderer );
289       // reset the weak handle so that the renderer only get added to actor once
290       mPlacementActor.Reset();
291     }
292
293    // Svg loaded and ready to display
294    ResourceReady( Toolkit::Visual::ResourceStatus::READY );
295   }
296 }
297
298 void SvgVisual::OnSetTransform()
299 {
300   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
301
302   if( IsOnStage() )
303   {
304     if( visualSize != mVisualSize )
305     {
306       AddRasterizationTask( visualSize );
307       mVisualSize = visualSize;
308     }
309   }
310
311   if(mImpl->mRenderer)
312   {
313     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
314   }
315 }
316
317 } // namespace Internal
318
319 } // namespace Toolkit
320
321 } // namespace Dali