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/devel-api/adaptor-framework/image-loading.h>
26 #include <dali/devel-api/scripting/enum-helper.h>
27 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali/integration-api/debug.h>
31 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
34 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
38 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
40 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-url.h>
42 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
57 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
58 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SHRINK_TO_FIT )
59 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SCALE_TO_FILL )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_WIDTH )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_HEIGHT )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, DEFAULT )
63 DALI_ENUM_TO_STRING_TABLE_END( FITTING_MODE )
66 DALI_ENUM_TO_STRING_TABLE_BEGIN( SAMPLING_MODE )
67 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX )
68 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NEAREST )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, LINEAR )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_NEAREST )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_LINEAR )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NO_FILTER )
73 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, DONT_CARE )
74 DALI_ENUM_TO_STRING_TABLE_END( SAMPLING_MODE )
77 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
78 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
79 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
80 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
81 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
82 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
85 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOAD_POLICY )
86 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE )
87 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED )
88 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
91 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
92 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED )
93 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED )
94 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER )
95 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
97 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
99 const float PIXEL_ALIGN_ON = 1.0f;
100 const float PIXEL_ALIGN_OFF = 0.0f;
102 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
106 if( gridSize == ImageDimensions( 1, 1 ) )
108 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
112 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
118 } // unnamed namespace
121 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
122 ImageVisualShaderFactory& shaderFactory,
123 const VisualUrl& imageUrl,
124 const Property::Map& properties,
125 ImageDimensions size,
126 FittingMode::Type fittingMode,
127 Dali::SamplingMode::Type samplingMode )
129 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode ) );
130 imageVisualPtr->SetProperties( properties );
131 return imageVisualPtr;
134 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
135 ImageVisualShaderFactory& shaderFactory,
136 const VisualUrl& imageUrl,
137 ImageDimensions size,
138 FittingMode::Type fittingMode,
139 Dali::SamplingMode::Type samplingMode )
141 return new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode );
144 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
145 ImageVisualShaderFactory& shaderFactory,
146 const VisualUrl& imageUrl,
147 ImageDimensions size,
148 FittingMode::Type fittingMode,
149 Dali::SamplingMode::Type samplingMode )
150 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::IMAGE ),
151 mPixelArea( FULL_TEXTURE_RECT ),
153 mImageUrl( imageUrl ),
155 mDesiredSize( size ),
156 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
158 mImageVisualShaderFactory( shaderFactory ),
159 mFittingMode( fittingMode ),
160 mSamplingMode( samplingMode ),
161 mWrapModeU( WrapMode::DEFAULT ),
162 mWrapModeV( WrapMode::DEFAULT ),
163 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
164 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
165 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
166 mAtlasRectSize( 0, 0 ),
167 mAttemptAtlasing( false ),
169 mOrientationCorrection( true )
171 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
174 ImageVisual::~ImageVisual()
176 if( Stage::IsInstalled() )
180 // TextureManager could have been deleted before the actor that contains this
181 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
182 // is still valid before accessing texture manager.
183 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
185 TextureManager& textureManager = mFactoryCache.GetTextureManager();
186 textureManager.Remove( mMaskingData->mAlphaMaskId, this );
190 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
191 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
198 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
200 // Url is already received in constructor
201 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
203 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
204 if( keyValue.first.type == Property::Key::INDEX )
206 DoSetProperty( keyValue.first.indexKey, keyValue.second );
210 if( keyValue.first == IMAGE_FITTING_MODE )
212 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
214 else if( keyValue.first == IMAGE_SAMPLING_MODE )
216 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
218 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
220 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
222 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
224 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
226 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
228 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
230 else if( keyValue.first == IMAGE_WRAP_MODE_U )
232 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
234 else if( keyValue.first == IMAGE_WRAP_MODE_V )
236 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
238 else if( keyValue.first == SYNCHRONOUS_LOADING )
240 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
242 else if( keyValue.first == IMAGE_ATLASING )
244 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
246 else if( keyValue.first == ALPHA_MASK_URL )
248 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
250 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
252 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
254 else if( keyValue.first == CROP_TO_MASK_NAME )
256 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
258 else if ( keyValue.first == LOAD_POLICY_NAME )
260 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
262 else if( keyValue.first == RELEASE_POLICY_NAME )
264 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
266 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
268 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
272 // Load image immediately if LOAD_POLICY requires it
273 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
275 auto attemptAtlasing = AttemptAtlasing();
276 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
277 TextureManager::ReloadPolicy::CACHED );
281 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
285 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
288 if( value.Get( sync ) )
292 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
296 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
301 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
306 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
308 float desiredWidth = 0.0f;
309 if( value.Get( desiredWidth ) )
311 mDesiredSize.SetWidth( desiredWidth );
315 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
320 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
322 float desiredHeight = 0.0f;
323 if( value.Get( desiredHeight ) )
325 mDesiredSize.SetHeight( desiredHeight );
329 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
334 case Toolkit::ImageVisual::Property::FITTING_MODE:
337 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
338 mFittingMode = Dali::FittingMode::Type( fittingMode );
342 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
344 int samplingMode = 0;
345 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
346 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
350 case Toolkit::ImageVisual::Property::PIXEL_AREA:
352 value.Get( mPixelArea );
356 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
359 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
360 mWrapModeU = Dali::WrapMode::Type( wrapMode );
364 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
367 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
368 mWrapModeV = Dali::WrapMode::Type( wrapMode );
372 case Toolkit::ImageVisual::Property::ATLASING:
374 value.Get( mAttemptAtlasing );
378 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
380 std::string alphaUrl = "";
381 if( value.Get( alphaUrl ) )
384 mMaskingData->mAlphaMaskUrl = alphaUrl;
389 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
392 if( value.Get( scale ) )
395 mMaskingData->mContentScaleFactor = scale;
400 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
403 if( value.Get( crop ) )
406 mMaskingData->mCropToMask = crop;
411 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
413 int releasePolicy = 0;
414 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
415 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
419 case Toolkit::ImageVisual::Property::LOAD_POLICY:
422 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
423 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
426 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
428 bool orientationCorrection( mOrientationCorrection );
429 if( value.Get( orientationCorrection ) )
431 mOrientationCorrection = orientationCorrection;
438 void ImageVisual::AllocateMaskData()
442 mMaskingData.reset(new TextureManager::MaskingData());
446 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
448 if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
450 naturalSize.x = mDesiredSize.GetWidth();
451 naturalSize.y = mDesiredSize.GetHeight();
454 else if( mImpl->mRenderer ) // Check if we have a loaded image
456 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
458 naturalSize.x = mAtlasRectSize.GetWidth();
459 naturalSize.y = mAtlasRectSize.GetHeight();
463 auto textureSet = mImpl->mRenderer.GetTextures();
466 auto texture = textureSet.GetTexture(0);
469 naturalSize.x = texture.GetWidth();
470 naturalSize.y = texture.GetHeight();
476 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
477 mMaskingData->mCropToMask )
479 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
480 if( dimensions != ImageDimensions( 0, 0 ) )
482 naturalSize.x = dimensions.GetWidth();
483 naturalSize.y = dimensions.GetHeight();
487 else if( mImageUrl.IsValid() )
489 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
491 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
493 if( dimensions != ImageDimensions( 0, 0 ) )
495 naturalSize.x = dimensions.GetWidth();
496 naturalSize.y = dimensions.GetHeight();
500 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
502 naturalSize.x = brokenImage.GetWidth();
503 naturalSize.y = brokenImage.GetWidth();
508 naturalSize = Vector2::ZERO;
511 void ImageVisual::CreateRenderer( TextureSet& textureSet )
516 if( !mImpl->mCustomShader )
518 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
520 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
521 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
522 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
523 IsRoundedCornerRequired() );
527 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
528 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
531 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
532 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
536 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
537 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
538 mImpl->mCustomShader->mHints );
539 if( mImpl->mCustomShader->mVertexShader.empty() )
541 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
546 // Set pixel align off as default.
547 // ToDo: Pixel align causes issues such as rattling image animation.
548 // We should trun it off until issues are resolved
549 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
551 mImpl->mRenderer = Renderer::New( geometry, shader );
554 mImpl->mRenderer.SetTextures( textureSet );
556 // else still waiting for texture load to finish.
558 //Register transform properties
559 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
561 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
564 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
565 TextureManager::ReloadPolicy forceReload )
567 TextureManager& textureManager = mFactoryCache.GetTextureManager();
569 ImageAtlasManagerPtr atlasManager = nullptr;
570 AtlasUploadObserver* atlasUploadObserver = nullptr;
571 auto textureObserver = this;
575 atlasManager = mFactoryCache.GetAtlasManager();
576 atlasUploadObserver = this;
579 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
580 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
581 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
583 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
584 mMaskingData, IsSynchronousLoadingRequired(), mTextureId,
585 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
586 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
587 mOrientationCorrection, forceReload, preMultiplyOnLoad);
591 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
594 if( atlasing ) // Flag needs to be set before creating renderer
596 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
600 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
604 bool ImageVisual::AttemptAtlasing()
606 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
609 void ImageVisual::InitializeRenderer()
611 auto attemptAtlasing = AttemptAtlasing();
613 // Load Texture if mTextures is empty.
614 // mTextures is already set, the mTexture can be used to create Renderer.
615 // There are two cases mTextures is empty.
616 // 1. mTextureId == TextureManager::INVALID_TEXTURE_ID
617 // - Visual is on stage with LoadPolicy::ATTACHED
618 // 2. mTextureId != TextureManager::INVALID_TEXTURE_ID
619 // - If ReleasePolicy is DESTROYED, InitializeRenderer called every on stage called.
620 // - Then every resources those contained in Visual are Reset but mTextureId is remained when the Off stage time,
621 // - So, mTextures needed to be get from texture manager to created resources like mImpl->mRenderer.
624 if( mTextureId == TextureManager::INVALID_TEXTURE_ID )
626 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
627 TextureManager::ReloadPolicy::CACHED );
631 mTextures = mFactoryCache.GetTextureManager().GetTextureSet( mTextureId );
635 CreateRenderer( mTextures );
636 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
638 if( attemptAtlasing ) // the texture is packed inside atlas
640 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
642 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
644 if( !defaultWrapMode ) // custom wrap mode
646 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
647 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
648 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
653 void ImageVisual::DoSetOnStage( Actor& actor )
655 if( mImageUrl.IsValid() )
657 InitializeRenderer();
660 if( !mImpl->mRenderer )
665 mPlacementActor = actor;
666 // Search the Actor tree to find if Layer UI behaviour set.
667 Layer layer = actor.GetLayer();
668 if( layer && layer.GetProperty<Layer::Behavior>( Layer::Property::BEHAVIOR ) == Layer::LAYER_3D )
670 // Layer 3D set, do not align pixels
671 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
674 if( mPixelArea != FULL_TEXTURE_RECT )
676 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
679 if( mLoading == false )
681 actor.AddRenderer( mImpl->mRenderer );
682 mPlacementActor.Reset();
684 // Image loaded and ready to display
685 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
689 void ImageVisual::DoSetOffStage( Actor& actor )
691 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
693 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
694 actor.RemoveRenderer( mImpl->mRenderer);
695 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
697 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
698 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
702 mImpl->mRenderer.Reset();
703 mPlacementActor.Reset();
706 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
709 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
711 bool sync = IsSynchronousLoadingRequired();
712 map.Insert( SYNCHRONOUS_LOADING, sync );
713 if( mImageUrl.IsValid() )
715 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
716 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
717 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
720 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
721 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
723 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
724 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
725 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
727 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
729 if( mMaskingData != NULL )
731 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
732 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
733 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
736 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
737 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
738 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
741 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
744 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
745 if( mImageUrl.IsValid() )
747 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
748 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
752 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
754 // Check if action is valid for this visual type and perform action if possible
756 switch ( actionName )
758 case DevelImageVisual::Action::RELOAD:
760 auto attemptAtlasing = AttemptAtlasing();
761 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
762 TextureManager::ReloadPolicy::FORCED );
768 void ImageVisual::OnSetTransform()
770 if( mImpl->mRenderer )
772 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
776 bool ImageVisual::IsResourceReady() const
778 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
779 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
782 // From existing atlas manager
783 void ImageVisual::UploadCompleted()
785 // Texture has been uploaded. If weak handle is holding a placement actor,
786 // it is the time to add the renderer to actor.
787 Actor actor = mPlacementActor.GetHandle();
790 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
791 actor.AddRenderer( mImpl->mRenderer );
792 // reset the weak handle so that the renderer only get added to actor once
793 mPlacementActor.Reset();
796 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
800 // From Texture Manager
801 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
802 const Vector4& atlasRectangle, bool preMultiplied )
804 Toolkit::Visual::ResourceStatus resourceStatus;
805 if( mImpl->mRenderer )
809 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
812 EnablePreMultipliedAlpha( preMultiplied );
814 Actor actor = mPlacementActor.GetHandle();
817 actor.AddRenderer( mImpl->mRenderer );
818 // reset the weak handle so that the renderer only get added to actor once
819 mPlacementActor.Reset();
822 if( !loadingSuccess )
824 Texture brokenImage = mFactoryCache.GetBrokenVisualImage();
826 textureSet = TextureSet::New();
827 textureSet.SetTexture(0u, brokenImage);
828 mImpl->mRenderer.SetTextures( textureSet );
831 Sampler sampler = Sampler::New();
832 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
833 textureSet.SetSampler( 0u, sampler );
834 mImpl->mRenderer.SetTextures(textureSet);
838 // Storing TextureSet needed when renderer staged.
839 if( ! mImpl->mRenderer )
841 mTextures = textureSet;
844 // Image loaded, set status regardless of staged status.
847 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
851 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
853 // Signal to observers ( control ) that resources are ready. Must be all resources.
854 ResourceReady( resourceStatus );
858 void ImageVisual::RemoveTexture()
860 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
862 mFactoryCache.GetTextureManager().Remove( mTextureId, this );
863 mTextureId = TextureManager::INVALID_TEXTURE_ID;
867 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
868 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
869 if( index != Property::INVALID_INDEX )
871 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
872 atlasRectValue.Get( atlasRect );
875 TextureSet textureSet = mImpl->mRenderer.GetTextures();
876 mImpl->mRenderer.Reset();
878 if( index != Property::INVALID_INDEX )
880 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
885 } // namespace Internal
887 } // namespace Toolkit