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
125 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
126 ImageVisualShaderFactory& shaderFactory,
127 const VisualUrl& imageUrl,
128 const Property::Map& properties,
129 ImageDimensions size,
130 FittingMode::Type fittingMode,
131 Dali::SamplingMode::Type samplingMode )
133 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode ) );
134 imageVisualPtr->SetProperties( properties );
135 return imageVisualPtr;
138 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
139 ImageVisualShaderFactory& shaderFactory,
140 const VisualUrl& imageUrl,
141 ImageDimensions size,
142 FittingMode::Type fittingMode,
143 Dali::SamplingMode::Type samplingMode )
145 return new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode );
148 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
150 return new ImageVisual( factoryCache, shaderFactory, image );
153 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
154 ImageVisualShaderFactory& shaderFactory,
155 const VisualUrl& imageUrl,
156 ImageDimensions size,
157 FittingMode::Type fittingMode,
158 Dali::SamplingMode::Type samplingMode )
159 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
161 mPixelArea( FULL_TEXTURE_RECT ),
163 mImageUrl( imageUrl ),
165 mDesiredSize( size ),
166 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
168 mImageVisualShaderFactory( shaderFactory ),
169 mFittingMode( fittingMode ),
170 mSamplingMode( samplingMode ),
171 mWrapModeU( WrapMode::DEFAULT ),
172 mWrapModeV( WrapMode::DEFAULT ),
173 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
174 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
175 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
176 mAtlasRectSize( 0, 0 ),
177 mAttemptAtlasing( false ),
179 mOrientationCorrection( true )
181 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
184 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
185 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
187 mPixelArea( FULL_TEXTURE_RECT ),
192 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
194 mImageVisualShaderFactory( shaderFactory ),
195 mFittingMode( FittingMode::DEFAULT ),
196 mSamplingMode( SamplingMode::DEFAULT ),
197 mWrapModeU( WrapMode::DEFAULT ),
198 mWrapModeV( WrapMode::DEFAULT ),
199 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
200 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
201 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
202 mAttemptAtlasing( false ),
204 mOrientationCorrection( true )
206 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
209 ImageVisual::~ImageVisual()
211 if( Stage::IsInstalled() )
215 // TextureManager could have been deleted before the actor that contains this
216 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
217 // is still valid before accessing texture manager.
218 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
220 TextureManager& textureManager = mFactoryCache.GetTextureManager();
221 textureManager.Remove( mMaskingData->mAlphaMaskId, this );
225 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
226 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
233 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
235 // Url is already received in constructor
236 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
238 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
239 if( keyValue.first.type == Property::Key::INDEX )
241 DoSetProperty( keyValue.first.indexKey, keyValue.second );
245 if( keyValue.first == IMAGE_FITTING_MODE )
247 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
249 else if( keyValue.first == IMAGE_SAMPLING_MODE )
251 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
253 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
255 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
257 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
259 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
261 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
263 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
265 else if( keyValue.first == IMAGE_WRAP_MODE_U )
267 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
269 else if( keyValue.first == IMAGE_WRAP_MODE_V )
271 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
273 else if( keyValue.first == SYNCHRONOUS_LOADING )
275 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
277 else if( keyValue.first == IMAGE_ATLASING )
279 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
281 else if( keyValue.first == ALPHA_MASK_URL )
283 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
285 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
287 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
289 else if( keyValue.first == CROP_TO_MASK_NAME )
291 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
293 else if ( keyValue.first == LOAD_POLICY_NAME )
295 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
297 else if( keyValue.first == RELEASE_POLICY_NAME )
299 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
301 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
303 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
307 // Load image immediately if LOAD_POLICY requires it
308 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
310 auto attemptAtlasing = AttemptAtlasing();
311 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
312 TextureManager::ReloadPolicy::CACHED );
316 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
320 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
323 if( value.Get( sync ) )
327 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
331 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
336 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
341 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
343 float desiredWidth = 0.0f;
344 if( value.Get( desiredWidth ) )
346 mDesiredSize.SetWidth( desiredWidth );
350 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
355 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
357 float desiredHeight = 0.0f;
358 if( value.Get( desiredHeight ) )
360 mDesiredSize.SetHeight( desiredHeight );
364 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
369 case Toolkit::ImageVisual::Property::FITTING_MODE:
372 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
373 mFittingMode = Dali::FittingMode::Type( fittingMode );
377 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
379 int samplingMode = 0;
380 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
381 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
385 case Toolkit::ImageVisual::Property::PIXEL_AREA:
387 value.Get( mPixelArea );
391 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
394 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
395 mWrapModeU = Dali::WrapMode::Type( wrapMode );
399 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
402 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
403 mWrapModeV = Dali::WrapMode::Type( wrapMode );
407 case Toolkit::ImageVisual::Property::ATLASING:
409 value.Get( mAttemptAtlasing );
413 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
415 std::string alphaUrl = "";
416 if( value.Get( alphaUrl ) )
419 mMaskingData->mAlphaMaskUrl = alphaUrl;
424 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
427 if( value.Get( scale ) )
430 mMaskingData->mContentScaleFactor = scale;
435 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
438 if( value.Get( crop ) )
441 mMaskingData->mCropToMask = crop;
446 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
448 int releasePolicy = 0;
449 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
450 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
454 case Toolkit::ImageVisual::Property::LOAD_POLICY:
457 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
458 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
461 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
463 bool orientationCorrection( mOrientationCorrection );
464 if( value.Get( orientationCorrection ) )
466 mOrientationCorrection = orientationCorrection;
473 void ImageVisual::AllocateMaskData()
477 mMaskingData.reset(new TextureManager::MaskingData());
481 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
485 naturalSize.x = mImage.GetWidth();
486 naturalSize.y = mImage.GetHeight();
489 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
491 naturalSize.x = mDesiredSize.GetWidth();
492 naturalSize.y = mDesiredSize.GetHeight();
495 else if( mImpl->mRenderer ) // Check if we have a loaded image
497 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
499 naturalSize.x = mAtlasRectSize.GetWidth();
500 naturalSize.y = mAtlasRectSize.GetHeight();
504 auto textureSet = mImpl->mRenderer.GetTextures();
507 auto texture = textureSet.GetTexture(0);
508 naturalSize.x = texture.GetWidth();
509 naturalSize.y = texture.GetHeight();
514 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
515 mMaskingData->mCropToMask )
517 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
518 if( dimensions != ImageDimensions( 0, 0 ) )
520 naturalSize.x = dimensions.GetWidth();
521 naturalSize.y = dimensions.GetHeight();
525 else if( mImageUrl.IsValid() )
527 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
529 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
531 if( dimensions != ImageDimensions( 0, 0 ) )
533 naturalSize.x = dimensions.GetWidth();
534 naturalSize.y = dimensions.GetHeight();
538 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
540 naturalSize.x = brokenImage.GetWidth();
541 naturalSize.y = brokenImage.GetWidth();
547 naturalSize = Vector2::ZERO;
550 void ImageVisual::CreateRenderer( TextureSet& textureSet )
555 if( !mImpl->mCustomShader )
557 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
559 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
560 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
561 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
562 IsRoundedCornerRequired() );
566 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
567 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
570 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
571 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
575 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
576 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
577 mImpl->mCustomShader->mHints );
578 if( mImpl->mCustomShader->mVertexShader.empty() )
580 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
585 // Set pixel align off as default.
586 // ToDo: Pixel align causes issues such as rattling image animation.
587 // We should trun it off until issues are resolved
588 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
590 mImpl->mRenderer = Renderer::New( geometry, shader );
593 mImpl->mRenderer.SetTextures( textureSet );
595 // else still waiting for texture load to finish.
597 //Register transform properties
598 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
600 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
603 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
608 std::string fragmentShader;
609 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
610 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
614 fragmentShader = fragmentPreFix;
615 fragmentShader += "\n";
618 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
620 fragmentShader += mImpl->mCustomShader->mFragmentShader;
624 fragmentShader += mImageVisualShaderFactory.GetFragmentShaderSource();
627 if( customSamplerTypename )
629 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
632 if( !mImpl->mCustomShader )
634 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
636 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), fragmentShader );
637 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
641 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
642 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
644 mImpl->mCustomShader->mHints );
645 if( mImpl->mCustomShader->mVertexShader.empty() )
647 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
651 mImpl->mRenderer = Renderer::New( geometry, shader );
653 //Register transform properties
654 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
657 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
658 TextureManager::ReloadPolicy forceReload )
660 TextureManager& textureManager = mFactoryCache.GetTextureManager();
662 ImageAtlasManagerPtr atlasManager = nullptr;
663 AtlasUploadObserver* atlasUploadObserver = nullptr;
664 auto textureObserver = this;
668 atlasManager = mFactoryCache.GetAtlasManager();
669 atlasUploadObserver = this;
672 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
673 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
674 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
676 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
677 mMaskingData, IsSynchronousLoadingRequired(), mTextureId,
678 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
679 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
680 mOrientationCorrection, forceReload, preMultiplyOnLoad);
684 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
687 if( atlasing ) // Flag needs to be set before creating renderer
689 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
693 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
697 bool ImageVisual::AttemptAtlasing()
699 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
702 void ImageVisual::InitializeRenderer()
704 auto attemptAtlasing = AttemptAtlasing();
705 // texture set has to be created first as we need to know if atlasing succeeded or not
706 // when selecting the shader
708 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
710 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
711 TextureManager::ReloadPolicy::CACHED );
714 CreateRenderer( mTextures );
715 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
717 if( attemptAtlasing ) // the texture is packed inside atlas
719 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
721 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
723 if( !defaultWrapMode ) // custom wrap mode
725 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
726 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
727 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
732 void ImageVisual::InitializeRenderer( const Image& image )
734 TextureSet textures = TextureSet::New();
736 NativeImage nativeImage = NativeImage::DownCast( image );
739 CreateNativeImageRenderer( nativeImage );
740 DALI_ASSERT_DEBUG( textures );
741 mImpl->mRenderer.SetTextures( textures );
745 // reuse existing code for regular images
746 CreateRenderer( textures ); // Textures will be retreived from Image
748 ApplyImageToSampler( image );
751 void ImageVisual::DoSetOnStage( Actor& actor )
753 if( mImageUrl.IsValid() )
755 InitializeRenderer();
759 InitializeRenderer( mImage );
762 if( !mImpl->mRenderer )
767 mPlacementActor = actor;
768 // Search the Actor tree to find if Layer UI behaviour set.
769 Layer layer = actor.GetLayer();
770 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
772 // Layer 3D set, do not align pixels
773 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
776 if( mPixelArea != FULL_TEXTURE_RECT )
778 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
781 if( mLoading == false )
783 actor.AddRenderer( mImpl->mRenderer );
784 mPlacementActor.Reset();
786 // Image loaded and ready to display
787 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
791 void ImageVisual::DoSetOffStage( Actor& actor )
793 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
795 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
796 actor.RemoveRenderer( mImpl->mRenderer);
797 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
799 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
800 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
803 if( mImageUrl.IsValid() )
805 // Legacy support for deprecated Dali::Image
809 mImpl->mRenderer.Reset();
810 mPlacementActor.Reset();
813 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
816 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
818 bool sync = IsSynchronousLoadingRequired();
819 map.Insert( SYNCHRONOUS_LOADING, sync );
820 if( mImageUrl.IsValid() )
822 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
823 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
824 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
828 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
829 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
831 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
834 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
838 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
839 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
841 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
842 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
843 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
845 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
847 if( mMaskingData != NULL )
849 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
850 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
851 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
854 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
855 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
856 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
859 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
862 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
863 if( mImageUrl.IsValid() )
865 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
866 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
870 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
871 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
875 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
877 // Check if action is valid for this visual type and perform action if possible
879 switch ( actionName )
881 case DevelImageVisual::Action::RELOAD:
883 auto attemptAtlasing = AttemptAtlasing();
884 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
885 TextureManager::ReloadPolicy::FORCED );
891 void ImageVisual::OnSetTransform()
893 if( mImpl->mRenderer )
895 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
899 bool ImageVisual::IsResourceReady() const
901 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
902 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
905 void ImageVisual::ApplyImageToSampler( const Image& image )
909 TextureSet textureSet = mImpl->mRenderer.GetTextures();
910 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
912 TextureSetImage( textureSet, 0u, image );
913 Sampler sampler = Sampler::New();
914 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
915 textureSet.SetSampler( 0u, sampler );
919 // From existing atlas manager
920 void ImageVisual::UploadCompleted()
922 // Texture has been uploaded. If weak handle is holding a placement actor,
923 // it is the time to add the renderer to actor.
924 Actor actor = mPlacementActor.GetHandle();
927 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
928 actor.AddRenderer( mImpl->mRenderer );
929 // reset the weak handle so that the renderer only get added to actor once
930 mPlacementActor.Reset();
933 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
937 // From Texture Manager
938 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
939 const Vector4& atlasRectangle, bool preMultiplied )
941 Toolkit::Visual::ResourceStatus resourceStatus;
942 if( mImpl->mRenderer )
946 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
949 EnablePreMultipliedAlpha( preMultiplied );
951 Actor actor = mPlacementActor.GetHandle();
954 actor.AddRenderer( mImpl->mRenderer );
955 // reset the weak handle so that the renderer only get added to actor once
956 mPlacementActor.Reset();
961 Sampler sampler = Sampler::New();
962 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
963 textureSet.SetSampler( 0u, sampler );
964 mImpl->mRenderer.SetTextures(textureSet);
968 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
970 textureSet = TextureSet::New();
971 mImpl->mRenderer.SetTextures( textureSet );
973 ApplyImageToSampler( brokenImage );
977 // Storing TextureSet needed when renderer staged.
978 if( ! mImpl->mRenderer )
980 mTextures = textureSet;
983 // Image loaded, set status regardless of staged status.
986 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
990 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
992 // Signal to observers ( control ) that resources are ready. Must be all resources.
993 ResourceReady( resourceStatus );
997 void ImageVisual::RemoveTexture()
999 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1001 mFactoryCache.GetTextureManager().Remove( mTextureId, this );
1002 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1006 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1007 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1008 if( index != Property::INVALID_INDEX )
1010 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1011 atlasRectValue.Get( atlasRect );
1014 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1015 mImpl->mRenderer.Reset();
1017 if( index != Property::INVALID_INDEX )
1019 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1024 } // namespace Internal
1026 } // namespace Toolkit