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