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