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);
512 naturalSize.x = texture.GetWidth();
513 naturalSize.y = texture.GetHeight();
519 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
520 mMaskingData->mCropToMask )
522 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
523 if( dimensions != ImageDimensions( 0, 0 ) )
525 naturalSize.x = dimensions.GetWidth();
526 naturalSize.y = dimensions.GetHeight();
530 else if( mImageUrl.IsValid() )
532 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
534 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
536 if( dimensions != ImageDimensions( 0, 0 ) )
538 naturalSize.x = dimensions.GetWidth();
539 naturalSize.y = dimensions.GetHeight();
543 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
545 naturalSize.x = brokenImage.GetWidth();
546 naturalSize.y = brokenImage.GetWidth();
551 naturalSize = Vector2::ZERO;
554 void ImageVisual::CreateRenderer( TextureSet& textureSet )
559 if( !mImpl->mCustomShader )
561 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
563 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
564 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
565 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
566 IsRoundedCornerRequired() );
570 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
571 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
574 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
575 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
579 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
580 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
581 mImpl->mCustomShader->mHints );
582 if( mImpl->mCustomShader->mVertexShader.empty() )
584 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
589 // Set pixel align off as default.
590 // ToDo: Pixel align causes issues such as rattling image animation.
591 // We should trun it off until issues are resolved
592 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
594 mImpl->mRenderer = Renderer::New( geometry, shader );
597 mImpl->mRenderer.SetTextures( textureSet );
599 // else still waiting for texture load to finish.
601 //Register transform properties
602 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
604 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
607 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
612 std::string fragmentShader;
613 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
614 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
618 fragmentShader = fragmentPreFix;
619 fragmentShader += "\n";
622 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
624 fragmentShader += mImpl->mCustomShader->mFragmentShader;
628 fragmentShader += mImageVisualShaderFactory.GetFragmentShaderSource();
631 if( customSamplerTypename )
633 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
636 if( !mImpl->mCustomShader )
638 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
640 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), fragmentShader );
641 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
645 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
646 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
648 mImpl->mCustomShader->mHints );
649 if( mImpl->mCustomShader->mVertexShader.empty() )
651 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
655 mImpl->mRenderer = Renderer::New( geometry, shader );
657 //Register transform properties
658 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
661 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
662 TextureManager::ReloadPolicy forceReload )
664 TextureManager& textureManager = mFactoryCache.GetTextureManager();
666 ImageAtlasManagerPtr atlasManager = nullptr;
667 AtlasUploadObserver* atlasUploadObserver = nullptr;
668 auto textureObserver = this;
672 atlasManager = mFactoryCache.GetAtlasManager();
673 atlasUploadObserver = this;
676 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
677 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
678 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
680 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
681 mMaskingData, IsSynchronousLoadingRequired(), mTextureId,
682 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
683 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
684 mOrientationCorrection, forceReload, preMultiplyOnLoad);
688 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
691 if( atlasing ) // Flag needs to be set before creating renderer
693 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
697 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
701 bool ImageVisual::AttemptAtlasing()
703 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
706 void ImageVisual::InitializeRenderer()
708 auto attemptAtlasing = AttemptAtlasing();
709 // texture set has to be created first as we need to know if atlasing succeeded or not
710 // when selecting the shader
712 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
714 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
715 TextureManager::ReloadPolicy::CACHED );
718 CreateRenderer( mTextures );
719 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
721 if( attemptAtlasing ) // the texture is packed inside atlas
723 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
725 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
727 if( !defaultWrapMode ) // custom wrap mode
729 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
730 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
731 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
736 void ImageVisual::InitializeRenderer( const Image& image )
738 TextureSet textures = TextureSet::New();
740 NativeImage nativeImage = NativeImage::DownCast( image );
743 CreateNativeImageRenderer( nativeImage );
744 DALI_ASSERT_DEBUG( textures );
745 mImpl->mRenderer.SetTextures( textures );
749 // reuse existing code for regular images
750 CreateRenderer( textures ); // Textures will be retreived from Image
752 ApplyImageToSampler( image );
755 void ImageVisual::DoSetOnStage( Actor& actor )
757 if( mImageUrl.IsValid() )
759 InitializeRenderer();
763 InitializeRenderer( mImage );
766 if( !mImpl->mRenderer )
771 mPlacementActor = actor;
772 // Search the Actor tree to find if Layer UI behaviour set.
773 Layer layer = actor.GetLayer();
774 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
776 // Layer 3D set, do not align pixels
777 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
780 if( mPixelArea != FULL_TEXTURE_RECT )
782 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
785 if( mLoading == false )
787 actor.AddRenderer( mImpl->mRenderer );
788 mPlacementActor.Reset();
790 // Image loaded and ready to display
791 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
795 void ImageVisual::DoSetOffStage( Actor& actor )
797 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
799 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
800 actor.RemoveRenderer( mImpl->mRenderer);
801 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
803 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
804 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
807 if( mImageUrl.IsValid() )
809 // Legacy support for deprecated Dali::Image
813 mImpl->mRenderer.Reset();
814 mPlacementActor.Reset();
817 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
820 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
822 bool sync = IsSynchronousLoadingRequired();
823 map.Insert( SYNCHRONOUS_LOADING, sync );
824 if( mImageUrl.IsValid() )
826 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
827 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
828 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
832 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
833 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
835 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
838 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
842 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
843 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
845 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
846 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
847 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
849 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
851 if( mMaskingData != NULL )
853 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
854 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
855 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
858 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
859 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
860 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
863 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
866 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
867 if( mImageUrl.IsValid() )
869 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
870 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
874 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
875 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
879 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
881 // Check if action is valid for this visual type and perform action if possible
883 switch ( actionName )
885 case DevelImageVisual::Action::RELOAD:
887 auto attemptAtlasing = AttemptAtlasing();
888 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
889 TextureManager::ReloadPolicy::FORCED );
895 void ImageVisual::OnSetTransform()
897 if( mImpl->mRenderer )
899 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
903 bool ImageVisual::IsResourceReady() const
905 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
906 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
909 void ImageVisual::ApplyImageToSampler( const Image& image )
913 TextureSet textureSet = mImpl->mRenderer.GetTextures();
914 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
916 TextureSetImage( textureSet, 0u, image );
917 Sampler sampler = Sampler::New();
918 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
919 textureSet.SetSampler( 0u, sampler );
923 // From existing atlas manager
924 void ImageVisual::UploadCompleted()
926 // Texture has been uploaded. If weak handle is holding a placement actor,
927 // it is the time to add the renderer to actor.
928 Actor actor = mPlacementActor.GetHandle();
931 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
932 actor.AddRenderer( mImpl->mRenderer );
933 // reset the weak handle so that the renderer only get added to actor once
934 mPlacementActor.Reset();
937 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
941 // From Texture Manager
942 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
943 const Vector4& atlasRectangle, bool preMultiplied )
945 Toolkit::Visual::ResourceStatus resourceStatus;
946 if( mImpl->mRenderer )
950 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
953 EnablePreMultipliedAlpha( preMultiplied );
955 Actor actor = mPlacementActor.GetHandle();
958 actor.AddRenderer( mImpl->mRenderer );
959 // reset the weak handle so that the renderer only get added to actor once
960 mPlacementActor.Reset();
965 Sampler sampler = Sampler::New();
966 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
967 textureSet.SetSampler( 0u, sampler );
968 mImpl->mRenderer.SetTextures(textureSet);
972 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
974 textureSet = TextureSet::New();
975 mImpl->mRenderer.SetTextures( textureSet );
977 ApplyImageToSampler( brokenImage );
981 // Storing TextureSet needed when renderer staged.
982 if( ! mImpl->mRenderer )
984 mTextures = textureSet;
987 // Image loaded, set status regardless of staged status.
990 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
994 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
996 // Signal to observers ( control ) that resources are ready. Must be all resources.
997 ResourceReady( resourceStatus );
1001 void ImageVisual::RemoveTexture()
1003 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1005 mFactoryCache.GetTextureManager().Remove( mTextureId, this );
1006 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1010 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1011 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1012 if( index != Property::INVALID_INDEX )
1014 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1015 atlasRectValue.Get( atlasRect );
1018 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1019 mImpl->mRenderer.Reset();
1021 if( index != Property::INVALID_INDEX )
1023 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1028 } // namespace Internal
1030 } // namespace Toolkit