2 * Copyright (c) 2018 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 const char * const IMAGE_FITTING_MODE( "fittingMode" );
61 const char * const IMAGE_SAMPLING_MODE( "samplingMode" );
62 const char * const IMAGE_DESIRED_WIDTH( "desiredWidth" );
63 const char * const IMAGE_DESIRED_HEIGHT( "desiredHeight" );
64 const char * const SYNCHRONOUS_LOADING( "synchronousLoading" );
65 const char * const IMAGE_ATLASING("atlasing");
66 const char * const ALPHA_MASK_URL("alphaMaskUrl");
69 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SHRINK_TO_FIT )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SCALE_TO_FILL )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_WIDTH )
73 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_HEIGHT )
74 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, DEFAULT )
75 DALI_ENUM_TO_STRING_TABLE_END( FITTING_MODE )
78 DALI_ENUM_TO_STRING_TABLE_BEGIN( SAMPLING_MODE )
79 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX )
80 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NEAREST )
81 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, LINEAR )
82 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_NEAREST )
83 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_LINEAR )
84 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NO_FILTER )
85 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, DONT_CARE )
86 DALI_ENUM_TO_STRING_TABLE_END( SAMPLING_MODE )
89 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
90 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
91 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
92 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
93 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
94 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
97 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOAD_POLICY )
98 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE )
99 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED )
100 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
103 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
104 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED )
105 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED )
106 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER )
107 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
109 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
111 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
113 const float PIXEL_ALIGN_ON = 1.0f;
114 const float PIXEL_ALIGN_OFF = 0.0f;
116 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
120 if( gridSize == ImageDimensions( 1, 1 ) )
122 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
126 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
132 } // unnamed namespace
134 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
135 ImageVisualShaderFactory& shaderFactory,
136 const VisualUrl& imageUrl,
137 const Property::Map& properties,
138 ImageDimensions size,
139 FittingMode::Type fittingMode,
140 Dali::SamplingMode::Type samplingMode )
142 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode ) );
143 imageVisualPtr->SetProperties( properties );
144 return imageVisualPtr;
147 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
148 ImageVisualShaderFactory& shaderFactory,
149 const VisualUrl& imageUrl,
150 ImageDimensions size,
151 FittingMode::Type fittingMode,
152 Dali::SamplingMode::Type samplingMode )
154 return new ImageVisual( factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode );
157 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
159 return new ImageVisual( factoryCache, shaderFactory, image );
162 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
163 ImageVisualShaderFactory& shaderFactory,
164 const VisualUrl& imageUrl,
165 ImageDimensions size,
166 FittingMode::Type fittingMode,
167 Dali::SamplingMode::Type samplingMode )
168 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
170 mPixelArea( FULL_TEXTURE_RECT ),
172 mImageUrl( imageUrl ),
174 mDesiredSize( size ),
175 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
177 mImageVisualShaderFactory( shaderFactory ),
178 mFittingMode( fittingMode ),
179 mSamplingMode( samplingMode ),
180 mWrapModeU( WrapMode::DEFAULT ),
181 mWrapModeV( WrapMode::DEFAULT ),
182 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
183 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
184 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
185 mAtlasRectSize( 0, 0 ),
186 mAttemptAtlasing( false ),
188 mOrientationCorrection( true )
190 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
193 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
194 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
196 mPixelArea( FULL_TEXTURE_RECT ),
201 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
203 mImageVisualShaderFactory( shaderFactory ),
204 mFittingMode( FittingMode::DEFAULT ),
205 mSamplingMode( SamplingMode::DEFAULT ),
206 mWrapModeU( WrapMode::DEFAULT ),
207 mWrapModeV( WrapMode::DEFAULT ),
208 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
209 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
210 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
211 mAttemptAtlasing( false ),
213 mOrientationCorrection( true )
215 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
218 ImageVisual::~ImageVisual()
220 if( Stage::IsInstalled() )
224 // TextureManager could have been deleted before the actor that contains this
225 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
226 // is still valid before accessing texture manager.
227 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
229 TextureManager& textureManager = mFactoryCache.GetTextureManager();
230 textureManager.Remove( mMaskingData->mAlphaMaskId );
234 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
235 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
242 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
244 // Url is already received in constructor
245 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
247 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
248 if( keyValue.first.type == Property::Key::INDEX )
250 DoSetProperty( keyValue.first.indexKey, keyValue.second );
254 if( keyValue.first == IMAGE_FITTING_MODE )
256 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
258 else if( keyValue.first == IMAGE_SAMPLING_MODE )
260 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
262 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
264 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
266 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
268 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
270 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
272 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
274 else if( keyValue.first == IMAGE_WRAP_MODE_U )
276 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
278 else if( keyValue.first == IMAGE_WRAP_MODE_V )
280 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
282 else if( keyValue.first == SYNCHRONOUS_LOADING )
284 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
286 else if( keyValue.first == IMAGE_ATLASING )
288 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
290 else if( keyValue.first == ALPHA_MASK_URL )
292 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
294 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
296 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
298 else if( keyValue.first == CROP_TO_MASK_NAME )
300 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
302 else if ( keyValue.first == LOAD_POLICY_NAME )
304 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
306 else if( keyValue.first == RELEASE_POLICY_NAME )
308 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
310 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
312 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
316 // Load image immediately if LOAD_POLICY requires it
317 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
319 auto attemptAtlasing = AttemptAtlasing();
320 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
321 TextureManager::ReloadPolicy::CACHED );
325 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
329 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
332 if( value.Get( sync ) )
336 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
340 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
345 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
350 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
352 float desiredWidth = 0.0f;
353 if( value.Get( desiredWidth ) )
355 mDesiredSize.SetWidth( desiredWidth );
359 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
364 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
366 float desiredHeight = 0.0f;
367 if( value.Get( desiredHeight ) )
369 mDesiredSize.SetHeight( desiredHeight );
373 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
378 case Toolkit::ImageVisual::Property::FITTING_MODE:
381 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
382 mFittingMode = Dali::FittingMode::Type( fittingMode );
386 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
388 int samplingMode = 0;
389 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
390 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
394 case Toolkit::ImageVisual::Property::PIXEL_AREA:
396 value.Get( mPixelArea );
400 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
403 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
404 mWrapModeU = Dali::WrapMode::Type( wrapMode );
408 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
411 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
412 mWrapModeV = Dali::WrapMode::Type( wrapMode );
416 case Toolkit::ImageVisual::Property::ATLASING:
418 value.Get( mAttemptAtlasing );
422 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
424 std::string alphaUrl = "";
425 if( value.Get( alphaUrl ) )
428 // Immediately trigger the alpha mask loading (it may just get a cached value)
429 mMaskingData->mAlphaMaskUrl = alphaUrl;
430 TextureManager& textureManager = mFactoryCache.GetTextureManager();
431 mMaskingData->mAlphaMaskId = textureManager.RequestMaskLoad( alphaUrl );
436 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
439 if( value.Get( scale ) )
442 mMaskingData->mContentScaleFactor = scale;
447 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
450 if( value.Get( crop ) )
453 mMaskingData->mCropToMask = crop;
458 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
460 int releasePolicy = 0;
461 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
462 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
466 case Toolkit::ImageVisual::Property::LOAD_POLICY:
469 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
470 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
473 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
475 bool orientationCorrection( mOrientationCorrection );
476 if( value.Get( orientationCorrection ) )
478 mOrientationCorrection = orientationCorrection;
485 void ImageVisual::AllocateMaskData()
489 mMaskingData.reset(new TextureManager::MaskingData());
493 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
497 naturalSize.x = mImage.GetWidth();
498 naturalSize.y = mImage.GetHeight();
501 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
503 naturalSize.x = mDesiredSize.GetWidth();
504 naturalSize.y = mDesiredSize.GetHeight();
507 else if( mImpl->mRenderer ) // Check if we have a loaded image
509 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
511 naturalSize.x = mAtlasRectSize.GetWidth();
512 naturalSize.y = mAtlasRectSize.GetHeight();
516 auto textureSet = mImpl->mRenderer.GetTextures();
519 auto texture = textureSet.GetTexture(0);
520 naturalSize.x = texture.GetWidth();
521 naturalSize.y = texture.GetHeight();
526 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
527 mMaskingData->mCropToMask )
529 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
530 if( dimensions != ImageDimensions( 0, 0 ) )
532 naturalSize.x = dimensions.GetWidth();
533 naturalSize.y = dimensions.GetHeight();
537 else if( mImageUrl.IsValid() )
539 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
541 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
543 if( dimensions != ImageDimensions( 0, 0 ) )
545 naturalSize.x = dimensions.GetWidth();
546 naturalSize.y = dimensions.GetHeight();
550 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
552 naturalSize.x = brokenImage.GetWidth();
553 naturalSize.y = brokenImage.GetWidth();
559 naturalSize = Vector2::ZERO;
562 void ImageVisual::CreateRenderer( TextureSet& textureSet )
567 if( !mImpl->mCustomShader )
569 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
571 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
572 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
573 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
577 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
578 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
581 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
582 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
586 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
587 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
588 mImpl->mCustomShader->mHints );
589 if( mImpl->mCustomShader->mVertexShader.empty() )
591 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
596 // Set pixel align off as default.
597 // ToDo: Pixel align causes issues such as rattling image animation.
598 // We should trun it off until issues are resolved
599 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
601 mImpl->mRenderer = Renderer::New( geometry, shader );
604 mImpl->mRenderer.SetTextures( textureSet );
606 // else still waiting for texture load to finish.
608 //Register transform properties
609 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
611 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
614 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
619 std::string fragmentShader;
620 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
621 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
625 fragmentShader = fragmentPreFix;
626 fragmentShader += "\n";
629 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
631 fragmentShader += mImpl->mCustomShader->mFragmentShader;
635 fragmentShader += mImageVisualShaderFactory.GetFragmentShaderSource();
638 if( customSamplerTypename )
640 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
643 if( !mImpl->mCustomShader )
645 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
647 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), fragmentShader );
648 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
652 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
653 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
655 mImpl->mCustomShader->mHints );
656 if( mImpl->mCustomShader->mVertexShader.empty() )
658 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
662 mImpl->mRenderer = Renderer::New( geometry, shader );
664 //Register transform properties
665 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
668 bool ImageVisual::IsSynchronousResourceLoading() const
670 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
673 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
674 TextureManager::ReloadPolicy forceReload )
676 TextureManager& textureManager = mFactoryCache.GetTextureManager();
678 ImageAtlasManagerPtr atlasManager = nullptr;
679 AtlasUploadObserver* atlasUploadObserver = nullptr;
680 auto textureObserver = this;
684 atlasManager = mFactoryCache.GetAtlasManager();
685 atlasUploadObserver = this;
688 auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
689 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
690 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
692 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
693 mMaskingData, IsSynchronousResourceLoading(), mTextureId,
694 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
695 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
696 mOrientationCorrection, forceReload, preMultiplyOnLoad);
700 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
703 if( atlasing ) // Flag needs to be set before creating renderer
705 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
709 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
713 bool ImageVisual::AttemptAtlasing()
715 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
718 void ImageVisual::InitializeRenderer()
720 auto attemptAtlasing = AttemptAtlasing();
721 // texture set has to be created first as we need to know if atlasing succeeded or not
722 // when selecting the shader
724 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
726 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
727 TextureManager::ReloadPolicy::CACHED );
730 CreateRenderer( mTextures );
731 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
733 if( attemptAtlasing ) // the texture is packed inside atlas
735 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
737 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
739 if( !defaultWrapMode ) // custom wrap mode
741 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
742 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
743 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
748 void ImageVisual::InitializeRenderer( const Image& image )
750 TextureSet textures = TextureSet::New();
752 NativeImage nativeImage = NativeImage::DownCast( image );
755 CreateNativeImageRenderer( nativeImage );
756 DALI_ASSERT_DEBUG( textures );
757 mImpl->mRenderer.SetTextures( textures );
761 // reuse existing code for regular images
762 CreateRenderer( textures ); // Textures will be retreived from Image
764 ApplyImageToSampler( image );
767 void ImageVisual::DoSetOnStage( Actor& actor )
769 if( mImageUrl.IsValid() )
771 InitializeRenderer();
775 InitializeRenderer( mImage );
778 if( !mImpl->mRenderer )
783 mPlacementActor = actor;
784 // Search the Actor tree to find if Layer UI behaviour set.
785 Layer layer = actor.GetLayer();
786 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
788 // Layer 3D set, do not align pixels
789 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
792 if( mPixelArea != FULL_TEXTURE_RECT )
794 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
797 if( mLoading == false )
799 actor.AddRenderer( mImpl->mRenderer );
800 mPlacementActor.Reset();
802 // Image loaded and ready to display
803 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
807 void ImageVisual::DoSetOffStage( Actor& actor )
809 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
811 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
812 actor.RemoveRenderer( mImpl->mRenderer);
813 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
815 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
816 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
819 if( mImageUrl.IsValid() )
821 // Legacy support for deprecated Dali::Image
825 mImpl->mRenderer.Reset();
826 mPlacementActor.Reset();
829 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
832 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
834 bool sync = IsSynchronousResourceLoading();
835 map.Insert( SYNCHRONOUS_LOADING, sync );
836 if( mImageUrl.IsValid() )
838 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
839 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
840 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
844 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
845 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
847 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
850 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
854 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
855 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
857 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
858 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
859 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
861 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
863 if( mMaskingData != NULL )
865 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
866 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
867 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
870 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
871 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
872 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
875 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
878 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
879 if( mImageUrl.IsValid() )
881 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
882 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
886 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
887 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
891 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
893 // Check if action is valid for this visual type and perform action if possible
895 switch ( actionName )
897 case DevelImageVisual::Action::RELOAD:
899 auto attemptAtlasing = AttemptAtlasing();
900 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
901 TextureManager::ReloadPolicy::FORCED );
907 void ImageVisual::OnSetTransform()
909 if( mImpl->mRenderer )
911 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
915 bool ImageVisual::IsResourceReady() const
917 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
918 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
921 void ImageVisual::ApplyImageToSampler( const Image& image )
925 TextureSet textureSet = mImpl->mRenderer.GetTextures();
926 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
928 TextureSetImage( textureSet, 0u, image );
929 Sampler sampler = Sampler::New();
930 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
931 textureSet.SetSampler( 0u, sampler );
935 // From existing atlas manager
936 void ImageVisual::UploadCompleted()
938 // Texture has been uploaded. If weak handle is holding a placement actor,
939 // it is the time to add the renderer to actor.
940 Actor actor = mPlacementActor.GetHandle();
943 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
944 actor.AddRenderer( mImpl->mRenderer );
945 // reset the weak handle so that the renderer only get added to actor once
946 mPlacementActor.Reset();
949 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
953 // From Texture Manager
954 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
955 const Vector4& atlasRectangle, bool preMultiplied )
957 Toolkit::Visual::ResourceStatus resourceStatus;
958 if( mImpl->mRenderer )
962 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
965 EnablePreMultipliedAlpha( preMultiplied );
967 Actor actor = mPlacementActor.GetHandle();
970 actor.AddRenderer( mImpl->mRenderer );
971 // reset the weak handle so that the renderer only get added to actor once
972 mPlacementActor.Reset();
977 Sampler sampler = Sampler::New();
978 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
979 textureSet.SetSampler( 0u, sampler );
980 mImpl->mRenderer.SetTextures(textureSet);
984 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
986 textureSet = TextureSet::New();
987 mImpl->mRenderer.SetTextures( textureSet );
989 ApplyImageToSampler( brokenImage );
993 // Storing TextureSet needed when renderer staged.
994 if( ! mImpl->mRenderer )
996 mTextures = textureSet;
999 // Image loaded, set status regardless of staged status.
1000 if( loadingSuccess )
1002 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1006 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1008 // Signal to observers ( control ) that resources are ready. Must be all resources.
1009 ResourceReady( resourceStatus );
1013 void ImageVisual::RemoveTexture()
1015 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1017 mFactoryCache.GetTextureManager().Remove( mTextureId );
1018 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1022 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1023 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1024 if( index != Property::INVALID_INDEX )
1026 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1027 atlasRectValue.Get( atlasRect );
1030 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1031 mImpl->mRenderer.Reset();
1033 if( index != Property::INVALID_INDEX )
1035 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1040 } // namespace Internal
1042 } // namespace Toolkit