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 <dali-toolkit/internal/visuals/image/image-visual.h>
22 #include <cstring> // for strlen()
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/images/native-image.h>
27 #include <dali/devel-api/images/texture-set-image.h>
28 #include <dali/devel-api/adaptor-framework/image-loading.h>
29 #include <dali/devel-api/scripting/enum-helper.h>
30 #include <dali/devel-api/scripting/scripting.h>
31 #include <dali/integration-api/debug.h>
34 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
37 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
41 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
42 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
43 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
44 #include <dali-toolkit/internal/visuals/visual-url.h>
45 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
60 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SHRINK_TO_FIT )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SCALE_TO_FILL )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_WIDTH )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_HEIGHT )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, DEFAULT )
66 DALI_ENUM_TO_STRING_TABLE_END( FITTING_MODE )
69 DALI_ENUM_TO_STRING_TABLE_BEGIN( SAMPLING_MODE )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NEAREST )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, LINEAR )
73 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_NEAREST )
74 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_LINEAR )
75 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NO_FILTER )
76 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, DONT_CARE )
77 DALI_ENUM_TO_STRING_TABLE_END( SAMPLING_MODE )
80 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
81 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
82 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
83 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
84 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
85 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
88 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOAD_POLICY )
89 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE )
90 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED )
91 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
94 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
95 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED )
96 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED )
97 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER )
98 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
100 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
102 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
104 const float PIXEL_ALIGN_ON = 1.0f;
105 const float PIXEL_ALIGN_OFF = 0.0f;
107 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
111 if( gridSize == ImageDimensions( 1, 1 ) )
113 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
117 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
123 } // unnamed namespace
126 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
127 ImageVisualShaderFactory& shaderFactory,
128 const VisualUrl& imageUrl,
129 const Property::Map& properties,
130 ImageDimensions size,
131 FittingMode::Type fittingMode,
132 Dali::SamplingMode::Type samplingMode )
134 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode ) );
135 imageVisualPtr->SetProperties( properties );
136 return imageVisualPtr;
139 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
140 ImageVisualShaderFactory& shaderFactory,
141 const VisualUrl& imageUrl,
142 ImageDimensions size,
143 FittingMode::Type fittingMode,
144 Dali::SamplingMode::Type samplingMode )
146 return new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode );
149 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
151 return new ImageVisual( factoryCache, shaderFactory, image );
154 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
155 ImageVisualShaderFactory& shaderFactory,
156 const VisualUrl& imageUrl,
157 ImageDimensions size,
158 FittingMode::Type fittingMode,
159 Dali::SamplingMode::Type samplingMode )
160 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
162 mPixelArea( FULL_TEXTURE_RECT ),
164 mImageUrl( imageUrl ),
166 mDesiredSize( size ),
167 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
169 mImageVisualShaderFactory( shaderFactory ),
170 mFittingMode( fittingMode ),
171 mSamplingMode( samplingMode ),
172 mWrapModeU( WrapMode::DEFAULT ),
173 mWrapModeV( WrapMode::DEFAULT ),
174 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
175 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
176 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
177 mAtlasRectSize( 0, 0 ),
178 mAttemptAtlasing( false ),
180 mOrientationCorrection( true )
182 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
185 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
186 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
188 mPixelArea( FULL_TEXTURE_RECT ),
193 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
195 mImageVisualShaderFactory( shaderFactory ),
196 mFittingMode( FittingMode::DEFAULT ),
197 mSamplingMode( SamplingMode::DEFAULT ),
198 mWrapModeU( WrapMode::DEFAULT ),
199 mWrapModeV( WrapMode::DEFAULT ),
200 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
201 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
202 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
203 mAttemptAtlasing( false ),
205 mOrientationCorrection( true )
207 // PreMultiplied alpha should be disabled when the Image is used.
208 EnablePreMultipliedAlpha( false );
211 ImageVisual::~ImageVisual()
213 if( Stage::IsInstalled() )
217 // TextureManager could have been deleted before the actor that contains this
218 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
219 // is still valid before accessing texture manager.
220 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
222 TextureManager& textureManager = mFactoryCache.GetTextureManager();
223 textureManager.Remove( mMaskingData->mAlphaMaskId, this );
227 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
228 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
235 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
237 // Url is already received in constructor
238 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
240 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
241 if( keyValue.first.type == Property::Key::INDEX )
243 DoSetProperty( keyValue.first.indexKey, keyValue.second );
247 if( keyValue.first == IMAGE_FITTING_MODE )
249 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
251 else if( keyValue.first == IMAGE_SAMPLING_MODE )
253 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
255 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
257 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
259 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
261 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
263 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
265 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
267 else if( keyValue.first == IMAGE_WRAP_MODE_U )
269 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
271 else if( keyValue.first == IMAGE_WRAP_MODE_V )
273 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
275 else if( keyValue.first == SYNCHRONOUS_LOADING )
277 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
279 else if( keyValue.first == IMAGE_ATLASING )
281 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
283 else if( keyValue.first == ALPHA_MASK_URL )
285 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
287 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
289 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
291 else if( keyValue.first == CROP_TO_MASK_NAME )
293 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
295 else if ( keyValue.first == LOAD_POLICY_NAME )
297 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
299 else if( keyValue.first == RELEASE_POLICY_NAME )
301 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
303 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
305 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
309 // Load image immediately if LOAD_POLICY requires it
310 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
312 auto attemptAtlasing = AttemptAtlasing();
313 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
314 TextureManager::ReloadPolicy::CACHED );
318 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
322 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
325 if( value.Get( sync ) )
329 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
333 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
338 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
343 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
345 float desiredWidth = 0.0f;
346 if( value.Get( desiredWidth ) )
348 mDesiredSize.SetWidth( desiredWidth );
352 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
357 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
359 float desiredHeight = 0.0f;
360 if( value.Get( desiredHeight ) )
362 mDesiredSize.SetHeight( desiredHeight );
366 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
371 case Toolkit::ImageVisual::Property::FITTING_MODE:
374 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
375 mFittingMode = Dali::FittingMode::Type( fittingMode );
379 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
381 int samplingMode = 0;
382 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
383 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
387 case Toolkit::ImageVisual::Property::PIXEL_AREA:
389 value.Get( mPixelArea );
393 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
396 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
397 mWrapModeU = Dali::WrapMode::Type( wrapMode );
401 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
404 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
405 mWrapModeV = Dali::WrapMode::Type( wrapMode );
409 case Toolkit::ImageVisual::Property::ATLASING:
411 value.Get( mAttemptAtlasing );
415 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
417 std::string alphaUrl = "";
418 if( value.Get( alphaUrl ) )
421 mMaskingData->mAlphaMaskUrl = alphaUrl;
426 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
429 if( value.Get( scale ) )
432 mMaskingData->mContentScaleFactor = scale;
437 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
440 if( value.Get( crop ) )
443 mMaskingData->mCropToMask = crop;
448 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
450 int releasePolicy = 0;
451 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
452 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
456 case Toolkit::ImageVisual::Property::LOAD_POLICY:
459 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
460 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
463 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
465 bool orientationCorrection( mOrientationCorrection );
466 if( value.Get( orientationCorrection ) )
468 mOrientationCorrection = orientationCorrection;
475 void ImageVisual::AllocateMaskData()
479 mMaskingData.reset(new TextureManager::MaskingData());
483 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
487 naturalSize.x = mImage.GetWidth();
488 naturalSize.y = mImage.GetHeight();
491 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
493 naturalSize.x = mDesiredSize.GetWidth();
494 naturalSize.y = mDesiredSize.GetHeight();
497 else if( mImpl->mRenderer ) // Check if we have a loaded image
499 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
501 naturalSize.x = mAtlasRectSize.GetWidth();
502 naturalSize.y = mAtlasRectSize.GetHeight();
506 auto textureSet = mImpl->mRenderer.GetTextures();
509 auto texture = textureSet.GetTexture(0);
510 naturalSize.x = texture.GetWidth();
511 naturalSize.y = texture.GetHeight();
516 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
517 mMaskingData->mCropToMask )
519 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
520 if( dimensions != ImageDimensions( 0, 0 ) )
522 naturalSize.x = dimensions.GetWidth();
523 naturalSize.y = dimensions.GetHeight();
527 else if( mImageUrl.IsValid() )
529 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
531 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
533 if( dimensions != ImageDimensions( 0, 0 ) )
535 naturalSize.x = dimensions.GetWidth();
536 naturalSize.y = dimensions.GetHeight();
540 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
542 naturalSize.x = brokenImage.GetWidth();
543 naturalSize.y = brokenImage.GetWidth();
548 naturalSize = Vector2::ZERO;
551 void ImageVisual::CreateRenderer( TextureSet& textureSet )
556 if( !mImpl->mCustomShader )
558 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
560 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
561 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
562 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
563 IsRoundedCornerRequired() );
567 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
568 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
571 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
572 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
576 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
577 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
578 mImpl->mCustomShader->mHints );
579 if( mImpl->mCustomShader->mVertexShader.empty() )
581 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
586 // Set pixel align off as default.
587 // ToDo: Pixel align causes issues such as rattling image animation.
588 // We should trun it off until issues are resolved
589 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
591 mImpl->mRenderer = Renderer::New( geometry, shader );
594 mImpl->mRenderer.SetTextures( textureSet );
596 // else still waiting for texture load to finish.
598 //Register transform properties
599 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
601 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
604 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
609 std::string fragmentShader;
610 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
611 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
615 fragmentShader = fragmentPreFix;
616 fragmentShader += "\n";
619 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
621 fragmentShader += mImpl->mCustomShader->mFragmentShader;
625 fragmentShader += mImageVisualShaderFactory.GetFragmentShaderSource();
628 if( customSamplerTypename )
630 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
633 if( !mImpl->mCustomShader )
635 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
637 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), fragmentShader );
638 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
642 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
643 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
645 mImpl->mCustomShader->mHints );
646 if( mImpl->mCustomShader->mVertexShader.empty() )
648 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
652 mImpl->mRenderer = Renderer::New( geometry, shader );
654 //Register transform properties
655 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
658 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
659 TextureManager::ReloadPolicy forceReload )
661 TextureManager& textureManager = mFactoryCache.GetTextureManager();
663 ImageAtlasManagerPtr atlasManager = nullptr;
664 AtlasUploadObserver* atlasUploadObserver = nullptr;
665 auto textureObserver = this;
669 atlasManager = mFactoryCache.GetAtlasManager();
670 atlasUploadObserver = this;
673 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
674 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
675 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
677 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
678 mMaskingData, IsSynchronousLoadingRequired(), mTextureId,
679 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
680 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
681 mOrientationCorrection, forceReload, preMultiplyOnLoad);
685 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
688 if( atlasing ) // Flag needs to be set before creating renderer
690 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
694 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
698 bool ImageVisual::AttemptAtlasing()
700 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
703 void ImageVisual::InitializeRenderer()
705 auto attemptAtlasing = AttemptAtlasing();
706 // texture set has to be created first as we need to know if atlasing succeeded or not
707 // when selecting the shader
709 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
711 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
712 TextureManager::ReloadPolicy::CACHED );
715 CreateRenderer( mTextures );
716 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
718 if( attemptAtlasing ) // the texture is packed inside atlas
720 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
722 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
724 if( !defaultWrapMode ) // custom wrap mode
726 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
727 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
728 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
733 void ImageVisual::InitializeRenderer( const Image& image )
735 TextureSet textures = TextureSet::New();
737 NativeImage nativeImage = NativeImage::DownCast( image );
740 CreateNativeImageRenderer( nativeImage );
741 DALI_ASSERT_DEBUG( textures );
742 mImpl->mRenderer.SetTextures( textures );
746 // reuse existing code for regular images
747 CreateRenderer( textures ); // Textures will be retreived from Image
749 ApplyImageToSampler( image );
752 void ImageVisual::DoSetOnStage( Actor& actor )
754 if( mImageUrl.IsValid() )
756 InitializeRenderer();
760 InitializeRenderer( mImage );
763 if( !mImpl->mRenderer )
768 mPlacementActor = actor;
769 // Search the Actor tree to find if Layer UI behaviour set.
770 Layer layer = actor.GetLayer();
771 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
773 // Layer 3D set, do not align pixels
774 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
777 if( mPixelArea != FULL_TEXTURE_RECT )
779 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
782 if( mLoading == false )
784 actor.AddRenderer( mImpl->mRenderer );
785 mPlacementActor.Reset();
787 // Image loaded and ready to display
788 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
792 void ImageVisual::DoSetOffStage( Actor& actor )
794 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
796 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
797 actor.RemoveRenderer( mImpl->mRenderer);
798 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
800 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
801 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
804 if( mImageUrl.IsValid() )
806 // Legacy support for deprecated Dali::Image
810 mImpl->mRenderer.Reset();
811 mPlacementActor.Reset();
814 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
817 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
819 bool sync = IsSynchronousLoadingRequired();
820 map.Insert( SYNCHRONOUS_LOADING, sync );
821 if( mImageUrl.IsValid() )
823 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
824 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
825 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
829 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
830 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
832 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
835 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
839 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
840 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
842 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
843 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
844 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
846 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
848 if( mMaskingData != NULL )
850 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
851 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
852 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
855 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
856 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
857 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
860 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
863 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
864 if( mImageUrl.IsValid() )
866 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
867 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
871 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
872 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
876 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
878 // Check if action is valid for this visual type and perform action if possible
880 switch ( actionName )
882 case DevelImageVisual::Action::RELOAD:
884 auto attemptAtlasing = AttemptAtlasing();
885 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
886 TextureManager::ReloadPolicy::FORCED );
892 void ImageVisual::OnSetTransform()
894 if( mImpl->mRenderer )
896 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
900 bool ImageVisual::IsResourceReady() const
902 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
903 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
906 void ImageVisual::ApplyImageToSampler( const Image& image )
910 TextureSet textureSet = mImpl->mRenderer.GetTextures();
911 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
913 TextureSetImage( textureSet, 0u, image );
914 Sampler sampler = Sampler::New();
915 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
916 textureSet.SetSampler( 0u, sampler );
920 // From existing atlas manager
921 void ImageVisual::UploadCompleted()
923 // Texture has been uploaded. If weak handle is holding a placement actor,
924 // it is the time to add the renderer to actor.
925 Actor actor = mPlacementActor.GetHandle();
928 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
929 actor.AddRenderer( mImpl->mRenderer );
930 // reset the weak handle so that the renderer only get added to actor once
931 mPlacementActor.Reset();
934 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
938 // From Texture Manager
939 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
940 const Vector4& atlasRectangle, bool preMultiplied )
942 Toolkit::Visual::ResourceStatus resourceStatus;
943 if( mImpl->mRenderer )
947 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
950 EnablePreMultipliedAlpha( preMultiplied );
952 Actor actor = mPlacementActor.GetHandle();
955 actor.AddRenderer( mImpl->mRenderer );
956 // reset the weak handle so that the renderer only get added to actor once
957 mPlacementActor.Reset();
962 Sampler sampler = Sampler::New();
963 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
964 textureSet.SetSampler( 0u, sampler );
965 mImpl->mRenderer.SetTextures(textureSet);
969 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
971 textureSet = TextureSet::New();
972 mImpl->mRenderer.SetTextures( textureSet );
974 ApplyImageToSampler( brokenImage );
978 // Storing TextureSet needed when renderer staged.
979 if( ! mImpl->mRenderer )
981 mTextures = textureSet;
984 // Image loaded, set status regardless of staged status.
987 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
991 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
993 // Signal to observers ( control ) that resources are ready. Must be all resources.
994 ResourceReady( resourceStatus );
998 void ImageVisual::RemoveTexture()
1000 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1002 mFactoryCache.GetTextureManager().Remove( mTextureId, this );
1003 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1007 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1008 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1009 if( index != Property::INVALID_INDEX )
1011 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1012 atlasRectValue.Get( atlasRect );
1015 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1016 mImpl->mRenderer.Reset();
1018 if( index != Property::INVALID_INDEX )
1020 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1025 } // namespace Internal
1027 } // namespace Toolkit