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 );
703 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
708 std::string fragmentShader;
709 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
710 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
714 fragmentShader = fragmentPreFix;
715 fragmentShader += "\n";
718 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
720 fragmentShader += mImpl->mCustomShader->mFragmentShader;
724 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
727 if( customSamplerTypename )
729 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
732 if( !mImpl->mCustomShader )
734 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
736 shader = Shader::New( VERTEX_SHADER, fragmentShader );
737 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
741 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
742 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
744 mImpl->mCustomShader->mHints );
745 if( mImpl->mCustomShader->mVertexShader.empty() )
747 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
751 mImpl->mRenderer = Renderer::New( geometry, shader );
753 //Register transform properties
754 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
758 bool ImageVisual::IsSynchronousResourceLoading() const
760 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
763 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
764 TextureManager::ReloadPolicy forceReload )
766 TextureManager& textureManager = mFactoryCache.GetTextureManager();
768 ImageAtlasManagerPtr atlasManager = nullptr;
769 AtlasUploadObserver* atlasUploadObserver = nullptr;
770 auto textureObserver = this;
774 atlasManager = mFactoryCache.GetAtlasManager();
775 atlasUploadObserver = this;
778 textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
779 mMaskingData, IsSynchronousResourceLoading(), mTextureId,
780 atlasRect, atlasing, mLoading, mWrapModeU,
781 mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
782 mOrientationCorrection,
785 if( atlasing ) // Flag needs to be set before creating renderer
787 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
791 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
795 bool ImageVisual::AttemptAtlasing()
797 return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
800 void ImageVisual::InitializeRenderer()
802 auto attemptAtlasing = AttemptAtlasing();
803 // texture set has to be created first as we need to know if atlasing succeeded or not
804 // when selecting the shader
806 if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
808 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
809 TextureManager::ReloadPolicy::CACHED );
812 CreateRenderer( mTextures );
813 mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
815 if( attemptAtlasing ) // the texture is packed inside atlas
817 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
819 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
821 if( !defaultWrapMode ) // custom wrap mode
823 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
824 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
825 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
830 void ImageVisual::InitializeRenderer( const Image& image )
832 TextureSet textures = TextureSet::New();
834 NativeImage nativeImage = NativeImage::DownCast( image );
837 CreateNativeImageRenderer( nativeImage );
838 DALI_ASSERT_DEBUG( textures );
839 mImpl->mRenderer.SetTextures( textures );
843 // reuse existing code for regular images
844 CreateRenderer( textures ); // Textures will be retreived from Image
846 ApplyImageToSampler( image );
849 void ImageVisual::DoSetOnStage( Actor& actor )
851 if( mImageUrl.IsValid() )
853 InitializeRenderer();
857 InitializeRenderer( mImage );
860 if( !mImpl->mRenderer )
865 mPlacementActor = actor;
866 // Search the Actor tree to find if Layer UI behaviour set.
867 Layer layer = actor.GetLayer();
868 if( layer && layer.GetBehavior() == Layer::LAYER_3D )
870 // Layer 3D set, do not align pixels
871 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
874 if( mPixelArea != FULL_TEXTURE_RECT )
876 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
879 if( mLoading == false )
881 actor.AddRenderer( mImpl->mRenderer );
882 mPlacementActor.Reset();
884 // Image loaded and ready to display
885 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
889 void ImageVisual::DoSetOffStage( Actor& actor )
891 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
893 // Image release is dependent on the ReleasePolicy, renderer is destroyed.
894 actor.RemoveRenderer( mImpl->mRenderer);
895 if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
897 RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
900 if( mImageUrl.IsValid() )
902 // Legacy support for deprecated Dali::Image
906 mImpl->mRenderer.Reset();
907 mPlacementActor.Reset();
910 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
913 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
915 bool sync = IsSynchronousResourceLoading();
916 map.Insert( SYNCHRONOUS_LOADING, sync );
917 if( mImageUrl.IsValid() )
919 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
920 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
921 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
925 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
926 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
928 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
931 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
935 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
936 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
938 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
939 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
940 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
942 map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
944 if( mMaskingData != NULL )
946 map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
947 map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
948 map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
951 map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
952 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
953 map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
956 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
959 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
960 if( mImageUrl.IsValid() )
962 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
963 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
967 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
968 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
972 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
974 // Check if action is valid for this visual type and perform action if possible
976 switch ( actionName )
978 case DevelImageVisual::Action::RELOAD:
980 auto attemptAtlasing = AttemptAtlasing();
981 LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
982 TextureManager::ReloadPolicy::FORCED );
988 void ImageVisual::OnSetTransform()
990 if( mImpl->mRenderer )
992 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
996 bool ImageVisual::IsResourceReady() const
998 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
999 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
1002 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
1007 if( defaultTextureWrapping )
1009 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
1012 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
1013 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1014 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
1019 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
1022 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
1023 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1024 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
1030 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
1033 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
1034 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1035 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
1042 const char* ImageVisual::GetStandardVertexShader()
1044 return VERTEX_SHADER;
1047 const char* ImageVisual::GetStandardFrgamentShader()
1049 return FRAGMENT_SHADER_NO_ATLAS;
1052 void ImageVisual::ApplyImageToSampler( const Image& image )
1056 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1057 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
1059 TextureSetImage( textureSet, 0u, image );
1060 Sampler sampler = Sampler::New();
1061 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1062 textureSet.SetSampler( 0u, sampler );
1066 // From existing atlas manager
1067 void ImageVisual::UploadCompleted()
1069 // Texture has been uploaded. If weak handle is holding a placement actor,
1070 // it is the time to add the renderer to actor.
1071 Actor actor = mPlacementActor.GetHandle();
1074 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1075 actor.AddRenderer( mImpl->mRenderer );
1076 // reset the weak handle so that the renderer only get added to actor once
1077 mPlacementActor.Reset();
1080 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
1084 // From Texture Manager
1085 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
1086 const Vector4& atlasRectangle )
1088 Toolkit::Visual::ResourceStatus resourceStatus;
1089 Actor actor = mPlacementActor.GetHandle();
1092 if( mImpl->mRenderer )
1096 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1099 actor.AddRenderer( mImpl->mRenderer );
1100 // reset the weak handle so that the renderer only get added to actor once
1101 mPlacementActor.Reset();
1102 if( loadingSuccess )
1104 Sampler sampler = Sampler::New();
1105 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1106 textureSet.SetSampler( 0u, sampler );
1107 mImpl->mRenderer.SetTextures(textureSet);
1111 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
1113 textureSet = TextureSet::New();
1114 mImpl->mRenderer.SetTextures( textureSet );
1116 ApplyImageToSampler( brokenImage );
1120 // Storing TextureSet needed when renderer staged.
1121 if( ! mImpl->mRenderer )
1123 mTextures = textureSet;
1126 // Image loaded, set status regardless of staged status.
1127 if( loadingSuccess )
1129 resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1133 resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1135 // Signal to observers ( control ) that resources are ready. Must be all resources.
1136 ResourceReady( resourceStatus );
1140 void ImageVisual::RemoveTexture()
1142 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1144 mFactoryCache.GetTextureManager().Remove( mTextureId );
1145 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1149 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1150 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1151 if( index != Property::INVALID_INDEX )
1153 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1154 atlasRectValue.Get( atlasRect );
1157 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1158 mImpl->mRenderer.Reset();
1160 if( index != Property::INVALID_INDEX )
1162 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1167 } // namespace Internal
1169 } // namespace Toolkit