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>
59 const char * const IMAGE_FITTING_MODE( "fittingMode" );
60 const char * const IMAGE_SAMPLING_MODE( "samplingMode" );
61 const char * const IMAGE_DESIRED_WIDTH( "desiredWidth" );
62 const char * const IMAGE_DESIRED_HEIGHT( "desiredHeight" );
63 const char * const SYNCHRONOUS_LOADING( "synchronousLoading" );
64 const char * const IMAGE_ATLASING("atlasing");
65 const char * const ALPHA_MASK_URL("alphaMaskUrl");
68 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SHRINK_TO_FIT )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SCALE_TO_FILL )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_WIDTH )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_HEIGHT )
73 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, DEFAULT )
74 DALI_ENUM_TO_STRING_TABLE_END( FITTING_MODE )
77 DALI_ENUM_TO_STRING_TABLE_BEGIN( SAMPLING_MODE )
78 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX )
79 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NEAREST )
80 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, LINEAR )
81 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_NEAREST )
82 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_LINEAR )
83 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NO_FILTER )
84 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, DONT_CARE )
85 DALI_ENUM_TO_STRING_TABLE_END( SAMPLING_MODE )
88 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
89 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
90 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
91 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
92 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
93 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
96 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOAD_POLICY )
97 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE )
98 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED )
99 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
102 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
103 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED )
104 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED )
105 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER )
106 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
108 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
110 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
112 const float PIXEL_ALIGN_ON = 1.0f;
113 const float PIXEL_ALIGN_OFF = 0.0f;
115 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
116 attribute mediump vec2 aPosition;\n
117 uniform mediump mat4 uModelMatrix;\n
118 uniform mediump mat4 uViewMatrix;\n
119 uniform mediump mat4 uProjection;\n
120 uniform mediump vec3 uSize;\n
121 uniform mediump vec4 pixelArea;
122 varying mediump vec2 vTexCoord;\n
123 uniform lowp float uPixelAligned;\n
125 //Visual size and offset
126 uniform mediump vec2 offset;\n
127 uniform mediump vec2 size;\n
128 uniform mediump vec4 offsetSizeMode;\n
129 uniform mediump vec2 origin;\n
130 uniform mediump vec2 anchorPoint;\n
132 vec4 ComputeVertexPosition()\n
134 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
135 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
136 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
141 mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n
142 vec4 alignedVertexPosition = vertexPosition;\n
143 alignedVertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment
144 vertexPosition = uProjection * mix( vertexPosition, alignedVertexPosition, uPixelAligned );\n
145 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
146 gl_Position = vertexPosition;\n
150 const char* FRAGMENT_SHADER_NO_ATLAS = DALI_COMPOSE_SHADER(
151 varying mediump vec2 vTexCoord;\n
152 uniform sampler2D sTexture;\n
153 uniform lowp vec4 uColor;\n
154 uniform lowp vec3 mixColor;\n
155 uniform lowp float preMultipliedAlpha;\n
159 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
163 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
164 varying mediump vec2 vTexCoord;\n
165 uniform sampler2D sTexture;\n
166 uniform mediump vec4 uAtlasRect;\n
167 uniform lowp vec4 uColor;\n
168 uniform lowp vec3 mixColor;\n
169 uniform lowp float preMultipliedAlpha;\n
173 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
174 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * vec4( mixColor, 1.0 );\n
178 const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER(
179 varying mediump vec2 vTexCoord;\n
180 uniform sampler2D sTexture;\n
181 uniform mediump vec4 uAtlasRect;\n
182 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
183 uniform lowp vec2 wrapMode;\n
184 uniform lowp vec4 uColor;\n
185 uniform lowp vec3 mixColor;\n
186 uniform lowp float preMultipliedAlpha;\n
188 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n
190 mediump float coord;\n
191 if( wrap > 1.5 )\n // REFLECT
192 coord = 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n
193 else \n// warp == 0 or 1
194 coord = mix(coordinate, fract( coordinate ), wrap);\n
195 return clamp( mix(range.x, range.y, coord), range.x, range.y );
200 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
201 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );\n
202 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * vec4( mixColor, 1.0 );\n
206 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
210 if( gridSize == ImageDimensions( 1, 1 ) )
212 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
216 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
222 } // unnamed namespace
224 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
225 const VisualUrl& imageUrl,
226 const Property::Map& properties,
227 ImageDimensions size,
228 FittingMode::Type fittingMode,
229 Dali::SamplingMode::Type samplingMode )
231 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ) );
232 imageVisualPtr->SetProperties( properties );
233 return imageVisualPtr;
236 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
237 const VisualUrl& imageUrl,
238 ImageDimensions size,
239 FittingMode::Type fittingMode,
240 Dali::SamplingMode::Type samplingMode )
242 return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode );
245 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image )
247 return new ImageVisual( factoryCache, image );
250 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
251 const VisualUrl& imageUrl,
252 ImageDimensions size,
253 FittingMode::Type fittingMode,
254 Dali::SamplingMode::Type samplingMode )
255 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
257 mPixelArea( FULL_TEXTURE_RECT ),
259 mImageUrl( imageUrl ),
261 mDesiredSize( size ),
262 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
264 mFittingMode( fittingMode ),
265 mSamplingMode( samplingMode ),
266 mWrapModeU( WrapMode::DEFAULT ),
267 mWrapModeV( WrapMode::DEFAULT ),
268 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
269 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
270 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
271 mAtlasRectSize( 0, 0 ),
272 mAttemptAtlasing( false ),
274 mOrientationCorrection( true )
278 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
279 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
281 mPixelArea( FULL_TEXTURE_RECT ),
286 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
288 mFittingMode( FittingMode::DEFAULT ),
289 mSamplingMode( SamplingMode::DEFAULT ),
290 mWrapModeU( WrapMode::DEFAULT ),
291 mWrapModeV( WrapMode::DEFAULT ),
292 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
293 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
294 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
295 mAttemptAtlasing( false ),
297 mOrientationCorrection( true )
301 ImageVisual::~ImageVisual()
303 if( Stage::IsInstalled() )
307 // TextureManager could have been deleted before the actor that contains this
308 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
309 // is still valid before accessing texture manager.
310 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
312 TextureManager& textureManager = mFactoryCache.GetTextureManager();
313 textureManager.Remove( mMaskingData->mAlphaMaskId );
317 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
318 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
325 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
327 // Url is already received in constructor
328 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
330 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
331 if( keyValue.first.type == Property::Key::INDEX )
333 DoSetProperty( keyValue.first.indexKey, keyValue.second );
337 if( keyValue.first == IMAGE_FITTING_MODE )
339 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
341 else if( keyValue.first == IMAGE_SAMPLING_MODE )
343 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
345 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
347 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
349 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
351 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
353 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
355 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
357 else if( keyValue.first == IMAGE_WRAP_MODE_U )
359 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
361 else if( keyValue.first == IMAGE_WRAP_MODE_V )
363 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
365 else if( keyValue.first == SYNCHRONOUS_LOADING )
367 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
369 else if( keyValue.first == IMAGE_ATLASING )
371 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
373 else if( keyValue.first == ALPHA_MASK_URL )
375 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
377 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
379 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
381 else if( keyValue.first == CROP_TO_MASK_NAME )
383 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
385 else if ( keyValue.first == LOAD_POLICY_NAME )
387 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
389 else if( keyValue.first == RELEASE_POLICY_NAME )
391 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
393 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
395 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
399 // Load image immediately if LOAD_POLICY requires it
400 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
402 auto attemptAtlasing = AttemptAtlasing();
403 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
404 TextureManager::ReloadPolicy::CACHED );
408 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
412 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
415 if( value.Get( sync ) )
419 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
423 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
428 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
433 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
435 float desiredWidth = 0.0f;
436 if( value.Get( desiredWidth ) )
438 mDesiredSize.SetWidth( desiredWidth );
442 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
447 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
449 float desiredHeight = 0.0f;
450 if( value.Get( desiredHeight ) )
452 mDesiredSize.SetHeight( desiredHeight );
456 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
461 case Toolkit::ImageVisual::Property::FITTING_MODE:
464 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
465 mFittingMode = Dali::FittingMode::Type( fittingMode );
469 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
471 int samplingMode = 0;
472 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
473 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
477 case Toolkit::ImageVisual::Property::PIXEL_AREA:
479 value.Get( mPixelArea );
483 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
486 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
487 mWrapModeU = Dali::WrapMode::Type( wrapMode );
491 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
494 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
495 mWrapModeV = Dali::WrapMode::Type( wrapMode );
499 case Toolkit::ImageVisual::Property::ATLASING:
501 value.Get( mAttemptAtlasing );
505 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
507 std::string alphaUrl = "";
508 if( value.Get( alphaUrl ) )
511 // Immediately trigger the alpha mask loading (it may just get a cached value)
512 mMaskingData->mAlphaMaskUrl = alphaUrl;
513 TextureManager& textureManager = mFactoryCache.GetTextureManager();
514 mMaskingData->mAlphaMaskId = textureManager.RequestMaskLoad( alphaUrl );
519 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
522 if( value.Get( scale ) )
525 mMaskingData->mContentScaleFactor = scale;
530 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
533 if( value.Get( crop ) )
536 mMaskingData->mCropToMask = crop;
541 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
543 int releasePolicy = 0;
544 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
545 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
549 case Toolkit::ImageVisual::Property::LOAD_POLICY:
552 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
553 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
556 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
558 bool orientationCorrection( mOrientationCorrection );
559 if( value.Get( orientationCorrection ) )
561 mOrientationCorrection = orientationCorrection;
568 void ImageVisual::AllocateMaskData()
572 mMaskingData.reset(new TextureManager::MaskingData());
576 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
580 naturalSize.x = mImage.GetWidth();
581 naturalSize.y = mImage.GetHeight();
584 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
586 naturalSize.x = mDesiredSize.GetWidth();
587 naturalSize.y = mDesiredSize.GetHeight();
590 else if( mImpl->mRenderer ) // Check if we have a loaded image
592 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED )
594 naturalSize.x = mAtlasRectSize.GetWidth();
595 naturalSize.y = mAtlasRectSize.GetHeight();
599 auto textureSet = mImpl->mRenderer.GetTextures();
602 auto texture = textureSet.GetTexture(0);
603 naturalSize.x = texture.GetWidth();
604 naturalSize.y = texture.GetHeight();
609 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
610 mMaskingData->mCropToMask )
612 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
613 if( dimensions != ImageDimensions( 0, 0 ) )
615 naturalSize.x = dimensions.GetWidth();
616 naturalSize.y = dimensions.GetHeight();
620 else if( mImageUrl.IsValid() )
622 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
624 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
626 if( dimensions != ImageDimensions( 0, 0 ) )
628 naturalSize.x = dimensions.GetWidth();
629 naturalSize.y = dimensions.GetHeight();
633 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
635 naturalSize.x = brokenImage.GetWidth();
636 naturalSize.y = brokenImage.GetWidth();
642 naturalSize = Vector2::ZERO;
645 void ImageVisual::CreateRenderer( TextureSet& textureSet )
650 if( !mImpl->mCustomShader )
652 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
654 shader = GetImageShader( mFactoryCache,
655 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
656 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
660 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
661 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
663 shader = GetImageShader( mFactoryCache, false, true );
667 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
668 mImpl->mCustomShader->mFragmentShader.empty() ? FRAGMENT_SHADER_NO_ATLAS : mImpl->mCustomShader->mFragmentShader,
669 mImpl->mCustomShader->mHints );
670 if( mImpl->mCustomShader->mVertexShader.empty() )
672 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
677 // Set pixel align off as default.
678 // ToDo: Pixel align causes issues such as rattling image animation.
679 // We should trun it off until issues are resolved
680 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
682 mImpl->mRenderer = Renderer::New( geometry, shader );
685 mImpl->mRenderer.SetTextures( textureSet );
687 // else still waiting for texture load to finish.
689 //Register transform properties
690 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
692 if( IsPreMultipliedAlphaEnabled() )
694 EnablePreMultipliedAlpha( true );
698 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
703 std::string fragmentShader;
704 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
705 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
709 fragmentShader = fragmentPreFix;
710 fragmentShader += "\n";
713 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
715 fragmentShader += mImpl->mCustomShader->mFragmentShader;
719 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
722 if( customSamplerTypename )
724 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
727 if( !mImpl->mCustomShader )
729 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
731 shader = Shader::New( VERTEX_SHADER, fragmentShader );
732 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
736 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
737 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
739 mImpl->mCustomShader->mHints );
740 if( mImpl->mCustomShader->mVertexShader.empty() )
742 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
746 mImpl->mRenderer = Renderer::New( geometry, shader );
748 //Register transform properties
749 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
752 bool ImageVisual::IsSynchronousResourceLoading() const
754 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
757 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
758 TextureManager::ReloadPolicy forceReload )
760 TextureManager& textureManager = mFactoryCache.GetTextureManager();
762 ImageAtlasManagerPtr atlasManager = nullptr;
763 AtlasUploadObserver* atlasUploadObserver = nullptr;
764 auto textureObserver = this;
768 atlasManager = mFactoryCache.GetAtlasManager();
769 atlasUploadObserver = this;
772 auto preMultiplyOnLoad = mFactoryCache.GetPreMultiplyOnLoad() && !mImpl->mCustomShader
773 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
774 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
776 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
777 mMaskingData, IsSynchronousResourceLoading(), mTextureId,
778 atlasRect, mAtlasRectSize, atlasing, mLoading, mWrapModeU,
779 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
780 mOrientationCorrection, forceReload, preMultiplyOnLoad);
782 if( textures && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
784 EnablePreMultipliedAlpha( true );
787 if( atlasing ) // Flag needs to be set before creating renderer
789 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
793 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
797 bool ImageVisual::AttemptAtlasing()
799 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
802 void ImageVisual::InitializeRenderer()
804 auto attemptAtlasing = AttemptAtlasing();
805 // texture set has to be created first as we need to know if atlasing succeeded or not
806 // when selecting the shader
808 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
810 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
811 TextureManager::ReloadPolicy::CACHED );
814 CreateRenderer( mTextures );
815 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
817 if( attemptAtlasing ) // the texture is packed inside atlas
819 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
821 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
823 if( !defaultWrapMode ) // custom wrap mode
825 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
826 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
827 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
832 void ImageVisual::InitializeRenderer( const Image& image )
834 TextureSet textures = TextureSet::New();
836 NativeImage nativeImage = NativeImage::DownCast( image );
839 CreateNativeImageRenderer( nativeImage );
840 DALI_ASSERT_DEBUG( textures );
841 mImpl->mRenderer.SetTextures( textures );
845 // reuse existing code for regular images
846 CreateRenderer( textures ); // Textures will be retreived from Image
848 ApplyImageToSampler( image );
851 void ImageVisual::DoSetOnStage( Actor& actor )
853 if( mImageUrl.IsValid() )
855 InitializeRenderer();
859 InitializeRenderer( mImage );
862 if( !mImpl->mRenderer )
867 mPlacementActor = actor;
868 // Search the Actor tree to find if Layer UI behaviour set.
869 Layer layer = actor.GetLayer();
870 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
872 // Layer 3D set, do not align pixels
873 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
876 if( mPixelArea != FULL_TEXTURE_RECT )
878 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
881 if( mLoading == false )
883 actor.AddRenderer( mImpl->mRenderer );
884 mPlacementActor.Reset();
886 // Image loaded and ready to display
887 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
891 void ImageVisual::DoSetOffStage( Actor& actor )
893 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
895 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
896 actor.RemoveRenderer( mImpl->mRenderer);
897 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
899 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
902 if( mImageUrl.IsValid() )
904 // Legacy support for deprecated Dali::Image
908 mImpl->mRenderer.Reset();
909 mPlacementActor.Reset();
912 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
915 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
917 bool sync = IsSynchronousResourceLoading();
918 map.Insert( SYNCHRONOUS_LOADING, sync );
919 if( mImageUrl.IsValid() )
921 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
922 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
923 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
927 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
928 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
930 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
933 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
937 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
938 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
940 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
941 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
942 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
944 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
946 if( mMaskingData != NULL )
948 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
949 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
950 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
953 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
954 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
955 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
958 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
961 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
962 if( mImageUrl.IsValid() )
964 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
965 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
969 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
970 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
974 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
976 // Check if action is valid for this visual type and perform action if possible
978 switch ( actionName )
980 case DevelImageVisual::Action::RELOAD:
982 auto attemptAtlasing = AttemptAtlasing();
983 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
984 TextureManager::ReloadPolicy::FORCED );
990 void ImageVisual::OnSetTransform()
992 if( mImpl->mRenderer )
994 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
998 bool ImageVisual::IsResourceReady() const
1000 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
1001 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
1004 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
1009 if( defaultTextureWrapping )
1011 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
1014 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
1015 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1016 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
1021 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
1024 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
1025 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1026 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
1032 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
1035 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
1036 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1037 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
1044 void ImageVisual::ApplyImageToSampler( const Image& image )
1048 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1049 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
1051 TextureSetImage( textureSet, 0u, image );
1052 Sampler sampler = Sampler::New();
1053 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1054 textureSet.SetSampler( 0u, sampler );
1058 // From existing atlas manager
1059 void ImageVisual::UploadCompleted()
1061 // Texture has been uploaded. If weak handle is holding a placement actor,
1062 // it is the time to add the renderer to actor.
1063 Actor actor = mPlacementActor.GetHandle();
1066 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1067 actor.AddRenderer( mImpl->mRenderer );
1068 // reset the weak handle so that the renderer only get added to actor once
1069 mPlacementActor.Reset();
1072 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
1076 // From Texture Manager
1077 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
1078 const Vector4& atlasRectangle, bool preMultiplied )
1080 Toolkit::Visual::ResourceStatus resourceStatus;
1081 Actor actor = mPlacementActor.GetHandle();
1084 if( mImpl->mRenderer )
1088 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1090 else if( preMultiplied )
1092 EnablePreMultipliedAlpha( true );
1095 actor.AddRenderer( mImpl->mRenderer );
1096 // reset the weak handle so that the renderer only get added to actor once
1097 mPlacementActor.Reset();
1098 if( loadingSuccess )
1100 Sampler sampler = Sampler::New();
1101 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1102 textureSet.SetSampler( 0u, sampler );
1103 mImpl->mRenderer.SetTextures(textureSet);
1107 Image brokenImage = mFactoryCache.GetBrokenVisualImage();
1109 textureSet = TextureSet::New();
1110 mImpl->mRenderer.SetTextures( textureSet );
1112 ApplyImageToSampler( brokenImage );
1116 // Storing TextureSet needed when renderer staged.
1117 if( ! mImpl->mRenderer )
1119 mTextures = textureSet;
1122 // Image loaded, set status regardless of staged status.
1123 if( loadingSuccess )
1125 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1129 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1131 // Signal to observers ( control ) that resources are ready. Must be all resources.
1132 ResourceReady( resourceStatus );
1136 void ImageVisual::RemoveTexture()
1138 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1140 mFactoryCache.GetTextureManager().Remove( mTextureId );
1141 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1145 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1146 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1147 if( index != Property::INVALID_INDEX )
1149 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1150 atlasRectValue.Get( atlasRect );
1153 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1154 mImpl->mRenderer.Reset();
1156 if( index != Property::INVALID_INDEX )
1158 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1163 } // namespace Internal
1165 } // namespace Toolkit