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