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