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 )
192 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Image& image )
193 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
195 mPixelArea( FULL_TEXTURE_RECT ),
200 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
202 mImageVisualShaderFactory( shaderFactory ),
203 mFittingMode( FittingMode::DEFAULT ),
204 mSamplingMode( SamplingMode::DEFAULT ),
205 mWrapModeU( WrapMode::DEFAULT ),
206 mWrapModeV( WrapMode::DEFAULT ),
207 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
208 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
209 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
210 mAttemptAtlasing( false ),
212 mOrientationCorrection( true )
216 ImageVisual::~ImageVisual()
218 if( Stage::IsInstalled() )
222 // TextureManager could have been deleted before the actor that contains this
223 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
224 // is still valid before accessing texture manager.
225 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
227 TextureManager& textureManager = mFactoryCache.GetTextureManager();
228 textureManager.Remove( mMaskingData->mAlphaMaskId );
232 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
233 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
240 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
242 // Url is already received in constructor
243 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
245 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
246 if( keyValue.first.type == Property::Key::INDEX )
248 DoSetProperty( keyValue.first.indexKey, keyValue.second );
252 if( keyValue.first == IMAGE_FITTING_MODE )
254 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
256 else if( keyValue.first == IMAGE_SAMPLING_MODE )
258 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
260 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
262 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
264 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
266 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
268 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
270 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
272 else if( keyValue.first == IMAGE_WRAP_MODE_U )
274 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
276 else if( keyValue.first == IMAGE_WRAP_MODE_V )
278 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
280 else if( keyValue.first == SYNCHRONOUS_LOADING )
282 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
284 else if( keyValue.first == IMAGE_ATLASING )
286 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
288 else if( keyValue.first == ALPHA_MASK_URL )
290 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
292 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
294 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
296 else if( keyValue.first == CROP_TO_MASK_NAME )
298 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
300 else if ( keyValue.first == LOAD_POLICY_NAME )
302 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
304 else if( keyValue.first == RELEASE_POLICY_NAME )
306 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
308 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
310 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
314 // Load image immediately if LOAD_POLICY requires it
315 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
317 auto attemptAtlasing = AttemptAtlasing();
318 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
319 TextureManager::ReloadPolicy::CACHED );
323 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
327 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
330 if( value.Get( sync ) )
334 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
338 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
343 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
348 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
350 float desiredWidth = 0.0f;
351 if( value.Get( desiredWidth ) )
353 mDesiredSize.SetWidth( desiredWidth );
357 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
362 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
364 float desiredHeight = 0.0f;
365 if( value.Get( desiredHeight ) )
367 mDesiredSize.SetHeight( desiredHeight );
371 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
376 case Toolkit::ImageVisual::Property::FITTING_MODE:
379 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
380 mFittingMode = Dali::FittingMode::Type( fittingMode );
384 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
386 int samplingMode = 0;
387 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
388 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
392 case Toolkit::ImageVisual::Property::PIXEL_AREA:
394 value.Get( mPixelArea );
398 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
401 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
402 mWrapModeU = Dali::WrapMode::Type( wrapMode );
406 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
409 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
410 mWrapModeV = Dali::WrapMode::Type( wrapMode );
414 case Toolkit::ImageVisual::Property::ATLASING:
416 value.Get( mAttemptAtlasing );
420 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
422 std::string alphaUrl = "";
423 if( value.Get( alphaUrl ) )
426 // Immediately trigger the alpha mask loading (it may just get a cached value)
427 mMaskingData->mAlphaMaskUrl = alphaUrl;
428 TextureManager& textureManager = mFactoryCache.GetTextureManager();
429 mMaskingData->mAlphaMaskId = textureManager.RequestMaskLoad( alphaUrl );
434 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
437 if( value.Get( scale ) )
440 mMaskingData->mContentScaleFactor = scale;
445 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
448 if( value.Get( crop ) )
451 mMaskingData->mCropToMask = crop;
456 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
458 int releasePolicy = 0;
459 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
460 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
464 case Toolkit::ImageVisual::Property::LOAD_POLICY:
467 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
468 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
471 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
473 bool orientationCorrection( mOrientationCorrection );
474 if( value.Get( orientationCorrection ) )
476 mOrientationCorrection = orientationCorrection;
483 void ImageVisual::AllocateMaskData()
487 mMaskingData.reset(new TextureManager::MaskingData());
491 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
495 naturalSize.x = mImage.GetWidth();
496 naturalSize.y = mImage.GetHeight();
499 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
501 naturalSize.x = mDesiredSize.GetWidth();
502 naturalSize.y = mDesiredSize.GetHeight();
505 else if( mImpl->mRenderer ) // Check if we have a loaded image
507 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
509 naturalSize.x = mAtlasRectSize.GetWidth();
510 naturalSize.y = mAtlasRectSize.GetHeight();
514 auto textureSet = mImpl->mRenderer.GetTextures();
517 auto texture = textureSet.GetTexture(0);
518 naturalSize.x = texture.GetWidth();
519 naturalSize.y = texture.GetHeight();
524 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
525 mMaskingData->mCropToMask )
527 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
528 if( dimensions != ImageDimensions( 0, 0 ) )
530 naturalSize.x = dimensions.GetWidth();
531 naturalSize.y = dimensions.GetHeight();
535 else if( mImageUrl.IsValid() )
537 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
539 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
541 if( dimensions != ImageDimensions( 0, 0 ) )
543 naturalSize.x = dimensions.GetWidth();
544 naturalSize.y = dimensions.GetHeight();
548 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
550 naturalSize.x = brokenImage.GetWidth();
551 naturalSize.y = brokenImage.GetWidth();
557 naturalSize = Vector2::ZERO;
560 void ImageVisual::CreateRenderer( TextureSet& textureSet )
565 if( !mImpl->mCustomShader )
567 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
569 shader = mImageVisualShaderFactory.GetShader( mFactoryCache,
570 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
571 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
575 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
576 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
579 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), mImageVisualShaderFactory.GetFragmentShaderSource(), mImpl->mCustomShader->mHints );
580 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
584 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
585 mImpl->mCustomShader->mFragmentShader.empty() ? mImageVisualShaderFactory.GetFragmentShaderSource() : mImpl->mCustomShader->mFragmentShader,
586 mImpl->mCustomShader->mHints );
587 if( mImpl->mCustomShader->mVertexShader.empty() )
589 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
594 // Set pixel align off as default.
595 // ToDo: Pixel align causes issues such as rattling image animation.
596 // We should trun it off until issues are resolved
597 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
599 mImpl->mRenderer = Renderer::New( geometry, shader );
602 mImpl->mRenderer.SetTextures( textureSet );
604 // else still waiting for texture load to finish.
606 //Register transform properties
607 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
609 EnablePreMultipliedAlpha( IsPreMultipliedAlphaEnabled() );
612 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
617 std::string fragmentShader;
618 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
619 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
623 fragmentShader = fragmentPreFix;
624 fragmentShader += "\n";
627 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
629 fragmentShader += mImpl->mCustomShader->mFragmentShader;
633 fragmentShader += mImageVisualShaderFactory.GetFragmentShaderSource();
636 if( customSamplerTypename )
638 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
641 if( !mImpl->mCustomShader )
643 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
645 shader = Shader::New( mImageVisualShaderFactory.GetVertexShaderSource(), fragmentShader );
646 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
650 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
651 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? mImageVisualShaderFactory.GetVertexShaderSource() : mImpl->mCustomShader->mVertexShader,
653 mImpl->mCustomShader->mHints );
654 if( mImpl->mCustomShader->mVertexShader.empty() )
656 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
660 mImpl->mRenderer = Renderer::New( geometry, shader );
662 //Register transform properties
663 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
666 bool ImageVisual::IsSynchronousResourceLoading() const
668 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
671 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
672 TextureManager::ReloadPolicy forceReload )
674 TextureManager& textureManager = mFactoryCache.GetTextureManager();
676 ImageAtlasManagerPtr atlasManager = nullptr;
677 AtlasUploadObserver* atlasUploadObserver = nullptr;
678 auto textureObserver = this;
682 atlasManager = mFactoryCache.GetAtlasManager();
683 atlasUploadObserver = this;
686 auto preMultiplyOnLoad = mFactoryCache.GetPreMultiplyOnLoad() && !mImpl->mCustomShader
687 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
688 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
690 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
691 mMaskingData, IsSynchronousResourceLoading(), mTextureId,
692 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
693 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
694 mOrientationCorrection, forceReload, preMultiplyOnLoad);
698 EnablePreMultipliedAlpha( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
701 if( atlasing ) // Flag needs to be set before creating renderer
703 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
707 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
711 bool ImageVisual::AttemptAtlasing()
713 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
716 void ImageVisual::InitializeRenderer()
718 auto attemptAtlasing = AttemptAtlasing();
719 // texture set has to be created first as we need to know if atlasing succeeded or not
720 // when selecting the shader
722 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
724 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
725 TextureManager::ReloadPolicy::CACHED );
728 CreateRenderer( mTextures );
729 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
731 if( attemptAtlasing ) // the texture is packed inside atlas
733 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
735 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
737 if( !defaultWrapMode ) // custom wrap mode
739 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
740 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
741 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
746 void ImageVisual::InitializeRenderer( const Image& image )
748 TextureSet textures = TextureSet::New();
750 NativeImage nativeImage = NativeImage::DownCast( image );
753 CreateNativeImageRenderer( nativeImage );
754 DALI_ASSERT_DEBUG( textures );
755 mImpl->mRenderer.SetTextures( textures );
759 // reuse existing code for regular images
760 CreateRenderer( textures ); // Textures will be retreived from Image
762 ApplyImageToSampler( image );
765 void ImageVisual::DoSetOnStage( Actor& actor )
767 if( mImageUrl.IsValid() )
769 InitializeRenderer();
773 InitializeRenderer( mImage );
776 if( !mImpl->mRenderer )
781 mPlacementActor = actor;
782 // Search the Actor tree to find if Layer UI behaviour set.
783 Layer layer = actor.GetLayer();
784 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
786 // Layer 3D set, do not align pixels
787 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
790 if( mPixelArea != FULL_TEXTURE_RECT )
792 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
795 if( mLoading == false )
797 actor.AddRenderer( mImpl->mRenderer );
798 mPlacementActor.Reset();
800 // Image loaded and ready to display
801 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
805 void ImageVisual::DoSetOffStage( Actor& actor )
807 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
809 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
810 actor.RemoveRenderer( mImpl->mRenderer);
811 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
813 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
814 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
817 if( mImageUrl.IsValid() )
819 // Legacy support for deprecated Dali::Image
823 mImpl->mRenderer.Reset();
824 mPlacementActor.Reset();
827 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
830 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
832 bool sync = IsSynchronousResourceLoading();
833 map.Insert( SYNCHRONOUS_LOADING, sync );
834 if( mImageUrl.IsValid() )
836 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
837 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
838 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
842 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
843 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
845 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
848 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
852 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
853 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
855 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
856 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
857 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
859 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
861 if( mMaskingData != NULL )
863 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
864 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
865 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
868 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
869 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
870 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
873 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
876 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
877 if( mImageUrl.IsValid() )
879 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
880 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
884 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
885 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
889 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
891 // Check if action is valid for this visual type and perform action if possible
893 switch ( actionName )
895 case DevelImageVisual::Action::RELOAD:
897 auto attemptAtlasing = AttemptAtlasing();
898 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
899 TextureManager::ReloadPolicy::FORCED );
905 void ImageVisual::OnSetTransform()
907 if( mImpl->mRenderer )
909 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
913 bool ImageVisual::IsResourceReady() const
915 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
916 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
919 void ImageVisual::ApplyImageToSampler( const Image& image )
923 TextureSet textureSet = mImpl->mRenderer.GetTextures();
924 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
926 TextureSetImage( textureSet, 0u, image );
927 Sampler sampler = Sampler::New();
928 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
929 textureSet.SetSampler( 0u, sampler );
933 // From existing atlas manager
934 void ImageVisual::UploadCompleted()
936 // Texture has been uploaded. If weak handle is holding a placement actor,
937 // it is the time to add the renderer to actor.
938 Actor actor = mPlacementActor.GetHandle();
941 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
942 actor.AddRenderer( mImpl->mRenderer );
943 // reset the weak handle so that the renderer only get added to actor once
944 mPlacementActor.Reset();
947 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
951 // From Texture Manager
952 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
953 const Vector4& atlasRectangle, bool preMultiplied )
955 Toolkit::Visual::ResourceStatus resourceStatus;
956 Actor actor = mPlacementActor.GetHandle();
959 if( mImpl->mRenderer )
963 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
966 EnablePreMultipliedAlpha( preMultiplied );
968 actor.AddRenderer( mImpl->mRenderer );
969 // reset the weak handle so that the renderer only get added to actor once
970 mPlacementActor.Reset();
973 Sampler sampler = Sampler::New();
974 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
975 textureSet.SetSampler( 0u, sampler );
976 mImpl->mRenderer.SetTextures(textureSet);
980 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
982 textureSet = TextureSet::New();
983 mImpl->mRenderer.SetTextures( textureSet );
985 ApplyImageToSampler( brokenImage );
989 // Storing TextureSet needed when renderer staged.
990 if( ! mImpl->mRenderer )
992 mTextures = textureSet;
995 // Image loaded, set status regardless of staged status.
998 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1002 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1004 // Signal to observers ( control ) that resources are ready. Must be all resources.
1005 ResourceReady( resourceStatus );
1009 void ImageVisual::RemoveTexture()
1011 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1013 mFactoryCache.GetTextureManager().Remove( mTextureId );
1014 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1018 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1019 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1020 if( index != Property::INVALID_INDEX )
1022 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1023 atlasRectValue.Get( atlasRect );
1026 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1027 mImpl->mRenderer.Reset();
1029 if( index != Property::INVALID_INDEX )
1031 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1036 } // namespace Internal
1038 } // namespace Toolkit