2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "svg-visual.h"
22 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
23 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
24 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
25 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
26 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/devel-api/adaptor-framework/file-loader.h>
32 #include <dali/integration-api/debug.h>
46 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
50 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
52 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
54 svgVisual->SetProperties(properties);
58 SvgVisualPtr SvgVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
60 SvgVisualPtr svgVisual( new SvgVisual( factoryCache, shaderFactory, imageUrl ) );
65 SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
66 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::SVG ),
67 mImageVisualShaderFactory( shaderFactory ),
68 mAtlasRect( FULL_TEXTURE_RECT ),
69 mImageUrl( imageUrl ),
70 mVectorRenderer( VectorImageRenderer::New() ),
75 mVisualSize(Vector2::ZERO),
76 mAttemptAtlasing( false )
78 // the rasterized image is with pre-multiplied alpha format
79 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
82 SvgVisual::~SvgVisual()
86 void SvgVisual::DoSetProperties( const Property::Map& propertyMap )
88 // url already passed in from constructor
89 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
91 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
92 if( keyValue.first.type == Property::Key::INDEX )
94 DoSetProperty( keyValue.first.indexKey, keyValue.second );
96 else if( keyValue.first == IMAGE_ATLASING )
98 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
100 else if( keyValue.first == SYNCHRONOUS_LOADING )
102 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
107 void SvgVisual::DoSetProperty( Property::Index index, const Property::Value& value )
111 case Toolkit::ImageVisual::Property::ATLASING:
113 value.Get( mAttemptAtlasing );
116 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
119 if( value.Get( sync ) )
123 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
127 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
132 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
139 void SvgVisual::DoSetOnScene( Actor& actor )
142 if( !mImpl->mCustomShader )
144 shader = mImageVisualShaderFactory.GetShader( mFactoryCache, mAttemptAtlasing, true, false );
148 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
149 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
150 mImpl->mCustomShader->mHints );
152 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
155 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
156 TextureSet textureSet = TextureSet::New();
157 mImpl->mRenderer = Renderer::New( geometry, shader );
158 mImpl->mRenderer.SetTextures( textureSet );
160 // Register transform properties
161 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
163 // Defer the rasterisation task until we get given a size (by Size Negotiation algorithm)
165 // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
166 mPlacementActor = actor;
168 // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
169 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
172 void SvgVisual::DoSetOffScene( Actor& actor )
174 mFactoryCache.GetSVGRasterizationThread()->RemoveTask( this );
176 actor.RemoveRenderer( mImpl->mRenderer );
177 mImpl->mRenderer.Reset();
178 mPlacementActor.Reset();
180 // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
181 mVisualSize = Vector2::ZERO;
184 void SvgVisual::GetNaturalSize( Vector2& naturalSize )
188 naturalSize.x = mDefaultWidth;
189 naturalSize.y = mDefaultHeight;
193 naturalSize = Vector2::ZERO;
197 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
200 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::SVG );
201 if( mImageUrl.IsValid() )
203 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
204 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
206 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
209 void SvgVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
214 void SvgVisual::Load()
216 // load remote resource on svg rasterize thread.
217 if(!mLoaded && mImageUrl.IsLocalResource())
219 Dali::Vector<uint8_t> buffer;
220 if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
222 buffer.PushBack('\0');
224 Vector2 dpi = Stage::GetCurrent().GetDpi();
225 float meanDpi = (dpi.height + dpi.width) * 0.5f;
226 if(!mVectorRenderer.Load(buffer, meanDpi))
228 DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
231 mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
236 DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
241 void SvgVisual::AddRasterizationTask( const Vector2& size )
243 if( mImpl->mRenderer )
245 unsigned int width = static_cast<unsigned int>(size.width);
246 unsigned int height = static_cast<unsigned int>( size.height );
248 Vector2 dpi = Stage::GetCurrent().GetDpi();
249 float meanDpi = ( dpi.height + dpi.width ) * 0.5f;
251 RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height, mLoaded);
252 if(IsSynchronousLoadingRequired())
255 newTask->Rasterize();
256 ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
260 mFactoryCache.GetSVGRasterizationThread()->AddTask( newTask );
265 void SvgVisual::ApplyRasterizedImage( VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded )
269 if(isLoaded && rasterizedPixelData && IsOnScene())
271 TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
272 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
274 mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
277 TextureSet textureSet;
279 if( mAttemptAtlasing && !mImpl->mCustomShader )
282 textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
283 if( textureSet ) // atlasing
285 if( textureSet != currentTextureSet )
287 mImpl->mRenderer.SetTextures( textureSet );
289 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
290 mAtlasRect = atlasRect;
291 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
295 if( !textureSet ) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
297 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888,
298 rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
299 texture.Upload( rasterizedPixelData );
300 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
302 if( mAtlasRect == FULL_TEXTURE_RECT )
304 textureSet = currentTextureSet;
308 textureSet = TextureSet::New();
309 mImpl->mRenderer.SetTextures( textureSet );
311 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT );
312 mAtlasRect = FULL_TEXTURE_RECT;
317 textureSet.SetTexture( 0, texture );
321 // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
322 Actor actor = mPlacementActor.GetHandle();
325 actor.AddRenderer( mImpl->mRenderer );
326 // reset the weak handle so that the renderer only get added to actor once
327 mPlacementActor.Reset();
330 // Svg loaded and ready to display
331 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
333 else if(!isLoaded || !rasterizedPixelData)
335 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
339 void SvgVisual::OnSetTransform()
341 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
345 if( visualSize != mVisualSize )
347 AddRasterizationTask( visualSize );
348 mVisualSize = visualSize;
354 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
358 bool SvgVisual::IsResourceReady() const
360 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
361 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
364 } // namespace Internal
366 } // namespace Toolkit