Merge "Garbage Collection using Dispose Queue method" into devel/master
[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, const std::string& imageUrl )
58 {
59   SvgVisual* svgVisual = new SvgVisual( factoryCache );
60   svgVisual->ParseFromUrl( imageUrl );
61   return svgVisual;
62 }
63
64 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
65 : Visual::Base( factoryCache ),
66   mAtlasRect( FULL_TEXTURE_RECT ),
67   mImageUrl(),
68   mParsedImage( NULL ),
69   mPlacementActor(),
70   mVisualSize(Vector2::ZERO)
71 {
72   // the rasterized image is with pre-multiplied alpha format
73   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
74 }
75
76 SvgVisual::~SvgVisual()
77 {
78   if( mParsedImage )
79   {
80     nsvgDelete( mParsedImage );
81   }
82 }
83
84 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
85 {
86   // url already passed in from constructor
87 }
88
89 void SvgVisual::DoSetOnStage( Actor& actor )
90 {
91   Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
92   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
93   if( !geometry )
94   {
95     geometry =  mFactoryCache.CreateQuadGeometry();
96     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
97   }
98   TextureSet textureSet = TextureSet::New();
99   mImpl->mRenderer = Renderer::New( geometry, shader );
100   mImpl->mRenderer.SetTextures( textureSet );
101
102   // Register transform properties
103   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
104
105   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
106
107   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
108   mPlacementActor = actor;
109 }
110
111 void SvgVisual::DoSetOffStage( Actor& actor )
112 {
113   mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
114
115   actor.RemoveRenderer( mImpl->mRenderer );
116   mImpl->mRenderer.Reset();
117   mPlacementActor.Reset();
118 }
119
120 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
121 {
122   if( mParsedImage )
123   {
124     naturalSize.x = mParsedImage->width;
125     naturalSize.y = mParsedImage->height;
126   }
127   else
128   {
129     naturalSize = Vector2::ZERO;
130   }
131 }
132
133 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
134 {
135   map.Clear();
136   map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
137   if( !mImageUrl.empty() )
138   {
139     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
140   }
141 }
142
143 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
144 {
145   // TODO
146 }
147
148 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
149 {
150   // TODO
151   return Dali::Property::Value();
152 }
153
154 void SvgVisual::ParseFromUrl( const std::string& imageUrl )
155 {
156   mImageUrl = imageUrl;
157
158   Vector2 dpi = Stage::GetCurrent().GetDpi();
159   float meanDpi = (dpi.height + dpi.width) * 0.5f;
160   mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi );
161 }
162
163 void SvgVisual::AddRasterizationTask( const Vector2& size )
164 {
165   if( mImpl->mRenderer && mParsedImage )
166   {
167     unsigned int width = static_cast<unsigned int>(size.width);
168     unsigned int height = static_cast<unsigned int>( size.height );
169
170     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
171     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
172   }
173 }
174
175 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
176 {
177   if( IsOnStage()  )
178   {
179     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
180     if( mAtlasRect != FULL_TEXTURE_RECT )
181     {
182       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
183     }
184
185     Vector4 atlasRect;
186     TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
187     if( textureSet ) // atlasing
188     {
189       if( textureSet != currentTextureSet )
190       {
191         mImpl->mRenderer.SetTextures( textureSet );
192       }
193       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
194       mAtlasRect = atlasRect;
195       mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
196     }
197     else // no atlasing
198     {
199       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
200                                       rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
201       texture.Upload( rasterizedPixelData );
202       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
203
204       if( mAtlasRect == FULL_TEXTURE_RECT )
205       {
206         textureSet = currentTextureSet;
207       }
208       else
209       {
210         textureSet = TextureSet::New();
211         mImpl->mRenderer.SetTextures( textureSet );
212
213         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
214         mAtlasRect = FULL_TEXTURE_RECT;
215       }
216
217       if( textureSet )
218       {
219         textureSet.SetTexture( 0, texture );
220       }
221     }
222
223     // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
224     Actor actor = mPlacementActor.GetHandle();
225     if( actor )
226     {
227       actor.AddRenderer( mImpl->mRenderer );
228       // reset the weak handle so that the renderer only get added to actor once
229       mPlacementActor.Reset();
230     }
231   }
232 }
233
234 void SvgVisual::OnSetTransform()
235 {
236   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
237
238   if( mParsedImage && IsOnStage() )
239   {
240     if( visualSize != mVisualSize )
241     {
242       AddRasterizationTask( visualSize );
243       mVisualSize = visualSize;
244     }
245   }
246 }
247
248 } // namespace Internal
249
250 } // namespace Toolkit
251
252 } // namespace Dali