(Visuals) Remove dead code & redundant Get prefix to check if on stage or from the...
[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 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
57 : Visual::Base( factoryCache ),
58   mAtlasRect( FULL_TEXTURE_RECT ),
59   mImageUrl(),
60   mParsedImage( NULL ),
61   mPlacementActor()
62 {
63   // the rasterized image is with pre-multiplied alpha format
64   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
65 }
66
67 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, const std::string& imageUrl, ImageDimensions size )
68 : Visual::Base( factoryCache ),
69   mAtlasRect( FULL_TEXTURE_RECT ),
70   mImageUrl(),
71   mParsedImage( NULL ),
72   mPlacementActor()
73 {
74   // the rasterized image is with pre-multiplied alpha format
75   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
76
77   ParseFromUrl( imageUrl, size );
78 }
79
80 SvgVisual::~SvgVisual()
81 {
82   if( mParsedImage )
83   {
84     nsvgDelete( mParsedImage );
85   }
86 }
87
88 void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
89 {
90   Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
91   if( imageURLValue )
92   {
93     std::string imageUrl;
94     if( imageURLValue->Get( imageUrl ) )
95     {
96       ParseFromUrl( imageUrl );
97     }
98     else
99     {
100       DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
101     }
102   }
103 }
104
105 void SvgVisual::DoSetOnStage( Actor& actor )
106 {
107   Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
108   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
109   if( !geometry )
110   {
111     geometry =  mFactoryCache.CreateQuadGeometry();
112     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
113   }
114   TextureSet textureSet = TextureSet::New();
115   mImpl->mRenderer = Renderer::New( geometry, shader );
116   mImpl->mRenderer.SetTextures( textureSet );
117
118   if( mImpl->mSize != Vector2::ZERO && mParsedImage )
119   {
120     AddRasterizationTask( mImpl->mSize );
121   }
122
123   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
124   mPlacementActor = actor;
125 }
126
127 void SvgVisual::DoSetOffStage( Actor& actor )
128 {
129   mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
130
131   actor.RemoveRenderer( mImpl->mRenderer );
132   mImpl->mRenderer.Reset();
133   mPlacementActor.Reset();
134 }
135
136 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
137 {
138   if( mParsedImage )
139   {
140     naturalSize.x = mParsedImage->width;
141     naturalSize.y = mParsedImage->height;
142   }
143   else
144   {
145     naturalSize = Vector2::ZERO;
146   }
147 }
148
149 void SvgVisual::SetSize( const Vector2& size )
150 {
151   if(mImpl->mSize != size && mParsedImage && IsOnStage() )
152   {
153     AddRasterizationTask( size );
154   }
155   mImpl->mSize = size;
156 }
157
158 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
159 {
160   map.Clear();
161   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
162   if( !mImageUrl.empty() )
163   {
164     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
165   }
166 }
167
168 void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
169 {
170   // TODO
171 }
172
173 Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
174 {
175   // TODO
176   return Dali::Property::Value();
177 }
178
179 void SvgVisual::ParseFromUrl( const std::string& imageUrl, ImageDimensions size )
180 {
181   mImageUrl = imageUrl;
182
183   Vector2 dpi = Stage::GetCurrent().GetDpi();
184   float meanDpi = (dpi.height + dpi.width) * 0.5f;
185   mParsedImage = nsvgParseFromFile( imageUrl.c_str(), UNITS, meanDpi );
186
187   if( size.GetWidth() != 0u && size.GetHeight() != 0u)
188   {
189     mImpl->mSize.x = size.GetWidth();
190     mImpl->mSize.y = size.GetHeight();
191   }
192 }
193
194 void SvgVisual::AddRasterizationTask( const Vector2& size )
195 {
196   if( mImpl->mRenderer && mParsedImage )
197   {
198     unsigned int width = static_cast<unsigned int>(size.width);
199     unsigned int height = static_cast<unsigned int>( size.height );
200     BufferImage image = BufferImage::New( width, height, Pixel::RGBA8888);
201
202     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
203     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
204   }
205 }
206
207 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
208 {
209   if( IsOnStage()  )
210   {
211     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
212     if( mAtlasRect != FULL_TEXTURE_RECT )
213     {
214       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
215     }
216
217     Vector4 atlasRect;
218     TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
219     if( textureSet ) // atlasing
220     {
221       if( textureSet != currentTextureSet )
222       {
223         mImpl->mRenderer.SetTextures( textureSet );
224       }
225       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
226       mAtlasRect = atlasRect;
227       mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
228     }
229     else // no atlasing
230     {
231       Atlas texture = Atlas::New( rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
232       texture.Upload( rasterizedPixelData, 0, 0 );
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         TextureSetImage( textureSet, 0u, 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