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