Merge "Enable Property::LABEL visual of a button when CreateVisuals" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
1 /*
2  * Copyright (c) 2018 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/public-api/visuals/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-atlas-manager.h>
34 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
37 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
38
39 namespace
40 {
41 const char * const UNITS("px");
42
43 // property name
44 const char * const IMAGE_ATLASING( "atlasing" );
45
46 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
47 }
48
49 namespace Dali
50 {
51
52 namespace Toolkit
53 {
54
55 namespace Internal
56 {
57
58 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
59 {
60   SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory ) );
61   svgVisual->ParseFromUrl( imageUrl );
62   svgVisual->SetProperties( properties );
63
64   return svgVisual;
65 }
66
67 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
68 {
69   SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory ) );
70   svgVisual->ParseFromUrl( imageUrl );
71
72   return svgVisual;
73 }
74
75 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory )
76 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
77   mImageVisualShaderFactory( shaderFactory ),
78   mAtlasRect( FULL_TEXTURE_RECT ),
79   mImageUrl( ),
80   mParsedImage( NULL ),
81   mPlacementActor(),
82   mVisualSize(Vector2::ZERO),
83   mAttemptAtlasing( false )
84 {
85   // the rasterized image is with pre-multiplied alpha format
86   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
87 }
88
89 SvgVisual::~SvgVisual()
90 {
91   if( mParsedImage )
92   {
93     nsvgDelete( mParsedImage );
94   }
95 }
96
97 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
98 {
99   // url already passed in from constructor
100   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
101   {
102     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
103     if( keyValue.first.type == Property::Key::INDEX )
104     {
105       DoSetProperty( keyValue.first.indexKey, keyValue.second );
106     }
107     else if( keyValue.first == IMAGE_ATLASING )
108     {
109       DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
110     }
111   }
112 }
113
114 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
115 {
116   switch( index )
117   {
118     case Toolkit::ImageVisual::Property::ATLASING:
119     {
120       value.Get( mAttemptAtlasing );
121       break;
122     }
123   }
124 }
125
126 void SvgVisual::DoSetOnStage( Actor& actor )
127 {
128   Shader shader;
129   if( !mImpl->mCustomShader )
130   {
131     shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true );
132   }
133   else
134   {
135     shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
136                           mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
137                           mImpl->mCustomShader->mHints );
138
139     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
140   }
141
142   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
143   TextureSet textureSet = TextureSet::New();
144   mImpl->mRenderer = Renderer::New( geometry, shader );
145   mImpl->mRenderer.SetTextures( textureSet );
146
147   // Register transform properties
148   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
149
150   // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
151
152   // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
153   mPlacementActor = actor;
154
155   // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
156   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
157 }
158
159 void SvgVisual::DoSetOffStage( Actor& actor )
160 {
161   mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
162
163   actor.RemoveRenderer( mImpl->mRenderer );
164   mImpl->mRenderer.Reset();
165   mPlacementActor.Reset();
166
167   // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
168   mVisualSize = Vector2::ZERO;
169 }
170
171 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
172 {
173   if( mParsedImage )
174   {
175     naturalSize.x = mParsedImage->width;
176     naturalSize.y = mParsedImage->height;
177   }
178   else
179   {
180     naturalSize = Vector2::ZERO;
181   }
182 }
183
184 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
185 {
186   map.Clear();
187   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
188   if( mImageUrl.IsValid() )
189   {
190     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
191     map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
192   }
193 }
194
195 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
196 {
197   // Do nothing
198 }
199
200 void SvgVisual::ParseFromUrl( const VisualUrl& imageUrl )
201 {
202   mImageUrl = imageUrl;
203   if( mImageUrl.IsLocalResource() )
204   {
205     Vector2 dpi = Stage::GetCurrent().GetDpi();
206     float meanDpi = (dpi.height + dpi.width) * 0.5f;
207     mParsedImage = nsvgParseFromFile( mImageUrl.GetUrl().c_str(), UNITS, meanDpi );
208   }
209 }
210
211 void SvgVisual::AddRasterizationTask( const Vector2& size )
212 {
213   if( mImpl->mRenderer && mParsedImage )
214   {
215     unsigned int width = static_cast<unsigned int>(size.width);
216     unsigned int height = static_cast<unsigned int>( size.height );
217
218     RasterizingTaskPtr newTask = new RasterizingTask( this, mParsedImage, width, height );
219     mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
220   }
221 }
222
223 void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
224 {
225   if( IsOnStage()  )
226   {
227     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
228     if( mImpl->mFlags |= Impl::IS_ATLASING_APPLIED )
229     {
230       mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
231     }
232
233     TextureSet textureSet;
234
235     if( mAttemptAtlasing && !mImpl->mCustomShader )
236     {
237       Vector4 atlasRect;
238       textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
239       if( textureSet ) // atlasing
240       {
241         if( textureSet != currentTextureSet )
242         {
243           mImpl->mRenderer.SetTextures( textureSet );
244         }
245         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
246         mAtlasRect = atlasRect;
247         mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
248       }
249     }
250
251     if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
252     {
253       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
254                                       rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
255       texture.Upload( rasterizedPixelData );
256       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
257
258       if( mAtlasRect == FULL_TEXTURE_RECT )
259       {
260         textureSet = currentTextureSet;
261       }
262       else
263       {
264         textureSet = TextureSet::New();
265         mImpl->mRenderer.SetTextures( textureSet );
266
267         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
268         mAtlasRect = FULL_TEXTURE_RECT;
269       }
270
271       if( textureSet )
272       {
273         textureSet.SetTexture( 0, texture );
274       }
275     }
276
277     // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
278     Actor actor = mPlacementActor.GetHandle();
279     if( actor )
280     {
281       actor.AddRenderer( mImpl->mRenderer );
282       // reset the weak handle so that the renderer only get added to actor once
283       mPlacementActor.Reset();
284     }
285
286    // Svg loaded and ready to display
287    ResourceReady( Toolkit::Visual::ResourceStatus::READY );
288   }
289 }
290
291 void SvgVisual::OnSetTransform()
292 {
293   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
294
295   if( mParsedImage && IsOnStage() )
296   {
297     if( visualSize != mVisualSize )
298     {
299       AddRasterizationTask( visualSize );
300       mVisualSize = visualSize;
301     }
302   }
303
304   if(mImpl->mRenderer)
305   {
306     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
307   }
308 }
309
310 } // namespace Internal
311
312 } // namespace Toolkit
313
314 } // namespace Dali