2 * Copyright (c) 2017 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 opacity;\n
156 uniform lowp float preMultipliedAlpha;\n
158 lowp vec4 visualMixColor()\n
160 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
164 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
168 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
169 varying mediump vec2 vTexCoord;\n
170 uniform sampler2D sTexture;\n
171 uniform mediump vec4 uAtlasRect;\n
172 uniform lowp vec4 uColor;\n
173 uniform lowp vec3 mixColor;\n
174 uniform lowp float opacity;\n
175 uniform lowp float preMultipliedAlpha;\n
177 lowp vec4 visualMixColor()\n
179 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
184 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
185 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
189 const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER(
190 varying mediump vec2 vTexCoord;\n
191 uniform sampler2D sTexture;\n
192 uniform mediump vec4 uAtlasRect;\n
193 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
194 uniform lowp vec2 wrapMode;\n
195 uniform lowp vec4 uColor;\n
196 uniform lowp vec3 mixColor;\n
197 uniform lowp float opacity;\n
198 uniform lowp float preMultipliedAlpha;\n
200 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n
202 mediump float coord;\n
203 if( wrap > 1.5 )\n // REFLECT
204 coord = 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n
205 else \n// warp == 0 or 1
206 coord = mix(coordinate, fract( coordinate ), wrap);\n
207 return clamp( mix(range.x, range.y, coord), range.x, range.y );
210 lowp vec4 visualMixColor()\n
212 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
217 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
218 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );\n
219 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
223 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
227 if( gridSize == ImageDimensions( 1, 1 ) )
229 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
233 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
239 } // unnamed namespace
241 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
242 const VisualUrl& imageUrl,
243 const Property::Map& properties,
244 ImageDimensions size,
245 FittingMode::Type fittingMode,
246 Dali::SamplingMode::Type samplingMode )
248 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ) );
249 imageVisualPtr->SetProperties( properties );
250 return imageVisualPtr;
253 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
254 const VisualUrl& imageUrl,
255 ImageDimensions size,
256 FittingMode::Type fittingMode,
257 Dali::SamplingMode::Type samplingMode )
259 return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode );
262 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image )
264 return new ImageVisual( factoryCache, image );
267 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
268 const VisualUrl& imageUrl,
269 ImageDimensions size,
270 FittingMode::Type fittingMode,
271 Dali::SamplingMode::Type samplingMode )
272 : Visual::Base( factoryCache ),
274 mPixelArea( FULL_TEXTURE_RECT ),
276 mImageUrl( imageUrl ),
278 mDesiredSize( size ),
279 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
281 mFittingMode( fittingMode ),
282 mSamplingMode( samplingMode ),
283 mWrapModeU( WrapMode::DEFAULT ),
284 mWrapModeV( WrapMode::DEFAULT ),
285 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
286 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED ),
287 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
288 mAttemptAtlasing( false ),
290 mOrientationCorrection( true )
294 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
295 : Visual::Base( factoryCache ),
297 mPixelArea( FULL_TEXTURE_RECT ),
302 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
304 mFittingMode( FittingMode::DEFAULT ),
305 mSamplingMode( SamplingMode::DEFAULT ),
306 mWrapModeU( WrapMode::DEFAULT ),
307 mWrapModeV( WrapMode::DEFAULT ),
308 mLoadPolicy( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
309 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DESTROYED ),
310 mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
311 mAttemptAtlasing( false ),
313 mOrientationCorrection( true )
317 ImageVisual::~ImageVisual()
319 if( Stage::IsInstalled() )
323 // TextureManager could have been deleted before the actor that contains this
324 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
325 // is still valid before accessing texture manager.
326 if( mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
328 TextureManager& textureManager = mFactoryCache.GetTextureManager();
329 textureManager.Remove( mMaskingData->mAlphaMaskId );
333 // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
334 if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID ) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ) )
341 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
343 // Url is already received in constructor
344 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
346 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
347 if( keyValue.first.type == Property::Key::INDEX )
349 DoSetProperty( keyValue.first.indexKey, keyValue.second );
353 if( keyValue.first == IMAGE_FITTING_MODE )
355 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
357 else if( keyValue.first == IMAGE_SAMPLING_MODE )
359 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
361 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
363 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
365 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
367 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
369 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
371 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
373 else if( keyValue.first == IMAGE_WRAP_MODE_U )
375 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
377 else if( keyValue.first == IMAGE_WRAP_MODE_V )
379 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
381 else if( keyValue.first == SYNCHRONOUS_LOADING )
383 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
385 else if( keyValue.first == IMAGE_ATLASING )
387 DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
389 else if( keyValue.first == ALPHA_MASK_URL )
391 DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
393 else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
395 DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
397 else if( keyValue.first == CROP_TO_MASK_NAME )
399 DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
401 else if ( keyValue.first == LOAD_POLICY_NAME )
403 DoSetProperty( Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second );
405 else if( keyValue.first == RELEASE_POLICY_NAME )
407 DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
409 else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
411 DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
415 // Load image immediately if LOAD_POLICY requires it
416 if ( mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE )
418 auto attemptAtlasing = AttemptAtlasing();
419 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
420 TextureManager::ReloadPolicy::CACHED );
424 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
428 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
431 if( value.Get( sync ) )
435 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
439 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
444 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
449 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
452 if( value.Get( desiredWidth ) )
454 mDesiredSize.SetWidth( desiredWidth );
458 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
463 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
466 if( value.Get( desiredHeight ) )
468 mDesiredSize.SetHeight( desiredHeight );
472 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
477 case Toolkit::ImageVisual::Property::FITTING_MODE:
480 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
481 mFittingMode = Dali::FittingMode::Type( fittingMode );
485 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
488 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
489 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
493 case Toolkit::ImageVisual::Property::PIXEL_AREA:
495 value.Get( mPixelArea );
499 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
502 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
503 mWrapModeU = Dali::WrapMode::Type( wrapMode );
507 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
510 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
511 mWrapModeV = Dali::WrapMode::Type( wrapMode );
515 case Toolkit::ImageVisual::Property::ATLASING:
517 value.Get( mAttemptAtlasing );
521 case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
523 std::string alphaUrl;
524 if( value.Get( alphaUrl ) )
527 // Immediately trigger the alpha mask loading (it may just get a cached value)
528 mMaskingData->mAlphaMaskUrl = alphaUrl;
529 TextureManager& textureManager = mFactoryCache.GetTextureManager();
530 mMaskingData->mAlphaMaskId = textureManager.RequestMaskLoad( alphaUrl );
535 case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
538 if( value.Get( scale ) )
541 mMaskingData->mContentScaleFactor = scale;
546 case Toolkit::ImageVisual::Property::CROP_TO_MASK:
549 if( value.Get( crop ) )
552 mMaskingData->mCropToMask = crop;
557 case Toolkit::ImageVisual::Property::RELEASE_POLICY:
560 Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
561 mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
565 case Toolkit::ImageVisual::Property::LOAD_POLICY:
568 Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
569 mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
572 case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
574 bool orientationCorrection( mOrientationCorrection );
575 if( value.Get( orientationCorrection ) )
577 mOrientationCorrection = orientationCorrection;
584 void ImageVisual::AllocateMaskData()
588 mMaskingData.reset(new TextureManager::MaskingData());
592 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
596 naturalSize.x = mImage.GetWidth();
597 naturalSize.y = mImage.GetHeight();
600 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
602 naturalSize.x = mDesiredSize.GetWidth();
603 naturalSize.y = mDesiredSize.GetHeight();
606 else if( mImpl->mRenderer ) // Check if we have a loaded image
608 auto textureSet = mImpl->mRenderer.GetTextures();
612 auto texture = textureSet.GetTexture(0);
613 naturalSize.x = texture.GetWidth();
614 naturalSize.y = texture.GetHeight();
619 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
620 mMaskingData->mCropToMask )
622 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
623 if( dimensions != ImageDimensions( 0, 0 ) )
625 naturalSize.x = dimensions.GetWidth();
626 naturalSize.y = dimensions.GetHeight();
630 else if( mImageUrl.IsValid() )
632 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
634 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
636 if( dimensions != ImageDimensions( 0, 0 ) )
638 naturalSize.x = dimensions.GetWidth();
639 naturalSize.y = dimensions.GetHeight();
643 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
645 naturalSize.x = brokenImage.GetWidth();
646 naturalSize.y = brokenImage.GetWidth();
652 naturalSize = Vector2::ZERO;
655 void ImageVisual::CreateRenderer( TextureSet& textureSet )
660 if( !mImpl->mCustomShader )
662 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
664 shader = GetImageShader( mFactoryCache,
665 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
666 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
670 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
671 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
673 shader = GetImageShader( mFactoryCache, false, true );
677 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
678 mImpl->mCustomShader->mFragmentShader.empty() ? FRAGMENT_SHADER_NO_ATLAS : mImpl->mCustomShader->mFragmentShader,
679 mImpl->mCustomShader->mHints );
680 if( mImpl->mCustomShader->mVertexShader.empty() )
682 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
687 // Set pixel align off as default.
688 // ToDo: Pixel align causes issues such as rattling image animation.
689 // We should trun it off until issues are resolved
690 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
692 mImpl->mRenderer = Renderer::New( geometry, shader );
695 mImpl->mRenderer.SetTextures( textureSet );
697 // else still waiting for texture load to finish.
699 //Register transform properties
700 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
702 if( IsPreMultipliedAlphaEnabled() )
704 EnablePreMultipliedAlpha( true );
708 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
713 std::string fragmentShader;
714 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
715 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
719 fragmentShader = fragmentPreFix;
720 fragmentShader += "\n";
723 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
725 fragmentShader += mImpl->mCustomShader->mFragmentShader;
729 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
732 if( customSamplerTypename )
734 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
737 if( !mImpl->mCustomShader )
739 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
741 shader = Shader::New( VERTEX_SHADER, fragmentShader );
742 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
746 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
747 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
749 mImpl->mCustomShader->mHints );
750 if( mImpl->mCustomShader->mVertexShader.empty() )
752 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
756 mImpl->mRenderer = Renderer::New( geometry, shader );
758 //Register transform properties
759 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
762 bool ImageVisual::IsSynchronousResourceLoading() const
764 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
767 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
768 TextureManager::ReloadPolicy forceReload )
770 TextureManager& textureManager = mFactoryCache.GetTextureManager();
772 ImageAtlasManagerPtr atlasManager = nullptr;
773 AtlasUploadObserver* atlasUploadObserver = nullptr;
774 auto textureObserver = this;
778 atlasManager = mFactoryCache.GetAtlasManager();
779 atlasUploadObserver = this;
782 auto preMultiplyOnLoad = mFactoryCache.GetPreMultiplyOnLoad()
783 ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
784 : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
786 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
787 mMaskingData, IsSynchronousResourceLoading(), mTextureId,
788 atlasRect, atlasing, mLoading, mWrapModeU,
789 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
790 mOrientationCorrection, forceReload, preMultiplyOnLoad);
792 if( textures && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
794 EnablePreMultipliedAlpha( true );
797 if( atlasing ) // Flag needs to be set before creating renderer
799 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
803 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
807 bool ImageVisual::AttemptAtlasing()
809 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
812 void ImageVisual::InitializeRenderer()
814 auto attemptAtlasing = AttemptAtlasing();
815 // texture set has to be created first as we need to know if atlasing succeeded or not
816 // when selecting the shader
818 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
820 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
821 TextureManager::ReloadPolicy::CACHED );
824 CreateRenderer( mTextures );
825 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
827 if( attemptAtlasing ) // the texture is packed inside atlas
829 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
831 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
833 if( !defaultWrapMode ) // custom wrap mode
835 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
836 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
837 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
842 void ImageVisual::InitializeRenderer( const Image& image )
844 TextureSet textures = TextureSet::New();
846 NativeImage nativeImage = NativeImage::DownCast( image );
849 CreateNativeImageRenderer( nativeImage );
850 DALI_ASSERT_DEBUG( textures );
851 mImpl->mRenderer.SetTextures( textures );
855 // reuse existing code for regular images
856 CreateRenderer( textures ); // Textures will be retreived from Image
858 ApplyImageToSampler( image );
861 void ImageVisual::DoSetOnStage( Actor& actor )
863 if( mImageUrl.IsValid() )
865 InitializeRenderer();
869 InitializeRenderer( mImage );
872 if( !mImpl->mRenderer )
877 mPlacementActor = actor;
878 // Search the Actor tree to find if Layer UI behaviour set.
879 Layer layer = actor.GetLayer();
880 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
882 // Layer 3D set, do not align pixels
883 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
886 if( mPixelArea != FULL_TEXTURE_RECT )
888 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
891 if( mLoading == false )
893 actor.AddRenderer( mImpl->mRenderer );
894 mPlacementActor.Reset();
896 // Image loaded and ready to display
897 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
901 void ImageVisual::DoSetOffStage( Actor& actor )
903 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
905 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
906 actor.RemoveRenderer( mImpl->mRenderer);
907 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
909 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
912 if( mImageUrl.IsValid() )
914 // Legacy support for deprecated Dali::Image
918 mImpl->mRenderer.Reset();
919 mPlacementActor.Reset();
922 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
925 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
927 bool sync = IsSynchronousResourceLoading();
928 map.Insert( SYNCHRONOUS_LOADING, sync );
929 if( mImageUrl.IsValid() )
931 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
932 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
933 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
937 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
938 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
940 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
943 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
947 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
948 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
950 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
951 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
952 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
954 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
956 if( mMaskingData != NULL )
958 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
959 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
960 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
963 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
964 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
965 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
968 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
971 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
972 if( mImageUrl.IsValid() )
974 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
975 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
979 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
980 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
984 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
986 // Check if action is valid for this visual type and perform action if possible
988 switch ( actionName )
990 case DevelImageVisual::Action::RELOAD:
992 auto attemptAtlasing = AttemptAtlasing();
993 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
994 TextureManager::ReloadPolicy::FORCED );
1000 void ImageVisual::OnSetTransform()
1002 if( mImpl->mRenderer )
1004 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
1008 bool ImageVisual::IsResourceReady() const
1010 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
1011 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
1014 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
1019 if( defaultTextureWrapping )
1021 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
1024 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
1025 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1026 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
1031 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
1034 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
1035 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1036 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
1042 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
1045 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
1046 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1047 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
1054 void ImageVisual::ApplyImageToSampler( const Image& image )
1058 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1059 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
1061 TextureSetImage( textureSet, 0u, image );
1062 Sampler sampler = Sampler::New();
1063 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1064 textureSet.SetSampler( 0u, sampler );
1068 // From existing atlas manager
1069 void ImageVisual::UploadCompleted()
1071 // Texture has been uploaded. If weak handle is holding a placement actor,
1072 // it is the time to add the renderer to actor.
1073 Actor actor = mPlacementActor.GetHandle();
1076 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1077 actor.AddRenderer( mImpl->mRenderer );
1078 // reset the weak handle so that the renderer only get added to actor once
1079 mPlacementActor.Reset();
1082 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
1086 // From Texture Manager
1087 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
1088 const Vector4& atlasRectangle, bool preMultiplied )
1090 Toolkit::Visual::ResourceStatus resourceStatus;
1091 Actor actor = mPlacementActor.GetHandle();
1094 if( mImpl->mRenderer )
1098 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1100 else if( preMultiplied )
1102 EnablePreMultipliedAlpha( true );
1105 actor.AddRenderer( mImpl->mRenderer );
1106 // reset the weak handle so that the renderer only get added to actor once
1107 mPlacementActor.Reset();
1108 if( loadingSuccess )
1110 Sampler sampler = Sampler::New();
1111 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1112 textureSet.SetSampler( 0u, sampler );
1113 mImpl->mRenderer.SetTextures(textureSet);
1117 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
1119 textureSet = TextureSet::New();
1120 mImpl->mRenderer.SetTextures( textureSet );
1122 ApplyImageToSampler( brokenImage );
1126 // Storing TextureSet needed when renderer staged.
1127 if( ! mImpl->mRenderer )
1129 mTextures = textureSet;
1132 // Image loaded, set status regardless of staged status.
1133 if( loadingSuccess )
1135 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1139 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1141 // Signal to observers ( control ) that resources are ready. Must be all resources.
1142 ResourceReady( resourceStatus );
1146 void ImageVisual::RemoveTexture()
1148 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1150 mFactoryCache.GetTextureManager().Remove( mTextureId );
1151 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1155 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1156 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1157 if( index != Property::INVALID_INDEX )
1159 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1160 atlasRectValue.Get( atlasRect );
1163 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1164 mImpl->mRenderer.Reset();
1166 if( index != Property::INVALID_INDEX )
1168 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1173 } // namespace Internal
1175 } // namespace Toolkit