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/devel-api/visuals/image-visual-properties-devel.h>
36 #include <dali-toolkit/devel-api/visuals/visual-properties-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 )
95 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
97 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
99 const float PIXEL_ALIGN_ON = 1.0f;
100 const float PIXEL_ALIGN_OFF = 0.0f;
102 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
103 attribute mediump vec2 aPosition;\n
104 uniform mediump mat4 uModelMatrix;\n
105 uniform mediump mat4 uViewMatrix;\n
106 uniform mediump mat4 uProjection;\n
107 uniform mediump vec3 uSize;\n
108 uniform mediump vec4 pixelArea;
109 varying mediump vec2 vTexCoord;\n
110 uniform lowp float uPixelAligned;\n
112 //Visual size and offset
113 uniform mediump vec2 offset;\n
114 uniform mediump vec2 size;\n
115 uniform mediump vec4 offsetSizeMode;\n
116 uniform mediump vec2 origin;\n
117 uniform mediump vec2 anchorPoint;\n
119 vec4 ComputeVertexPosition()\n
121 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
122 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
123 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
128 mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n
129 vec4 alignedVertexPosition = vertexPosition;\n
130 alignedVertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment
131 vertexPosition = uProjection * mix( vertexPosition, alignedVertexPosition, uPixelAligned );\n
132 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
133 gl_Position = vertexPosition;\n
137 const char* FRAGMENT_SHADER_NO_ATLAS = DALI_COMPOSE_SHADER(
138 varying mediump vec2 vTexCoord;\n
139 uniform sampler2D sTexture;\n
140 uniform lowp vec4 uColor;\n
141 uniform lowp vec3 mixColor;\n
142 uniform lowp float opacity;\n
143 uniform lowp float preMultipliedAlpha;\n
145 lowp vec4 visualMixColor()\n
147 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
151 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
155 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
156 varying mediump vec2 vTexCoord;\n
157 uniform sampler2D sTexture;\n
158 uniform mediump vec4 uAtlasRect;\n
159 uniform lowp vec4 uColor;\n
160 uniform lowp vec3 mixColor;\n
161 uniform lowp float opacity;\n
162 uniform lowp float preMultipliedAlpha;\n
164 lowp vec4 visualMixColor()\n
166 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
171 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
172 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
176 const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER(
177 varying mediump vec2 vTexCoord;\n
178 uniform sampler2D sTexture;\n
179 uniform mediump vec4 uAtlasRect;\n
180 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
181 uniform lowp vec2 wrapMode;\n
182 uniform lowp vec4 uColor;\n
183 uniform lowp vec3 mixColor;\n
184 uniform lowp float opacity;\n
185 uniform lowp float preMultipliedAlpha;\n
187 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n
189 mediump float coord;\n
190 if( wrap > 1.5 )\n // REFLECT
191 coord = 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n
192 else \n// warp == 0 or 1
193 coord = mix(coordinate, fract( coordinate ), wrap);\n
194 return clamp( mix(range.x, range.y, coord), range.x, range.y );
197 lowp vec4 visualMixColor()\n
199 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
204 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
205 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );\n
206 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
210 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
214 if( gridSize == ImageDimensions( 1, 1 ) )
216 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
220 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
226 } // unnamed namespace
228 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
229 const VisualUrl& imageUrl,
230 const Property::Map& properties,
231 ImageDimensions size,
232 FittingMode::Type fittingMode,
233 Dali::SamplingMode::Type samplingMode )
235 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ) );
236 imageVisualPtr->SetProperties( properties );
237 return imageVisualPtr;
240 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
241 const VisualUrl& imageUrl,
242 ImageDimensions size,
243 FittingMode::Type fittingMode,
244 Dali::SamplingMode::Type samplingMode )
246 return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode );
249 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image )
251 return new ImageVisual( factoryCache, image );
254 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
255 const VisualUrl& imageUrl,
256 ImageDimensions size,
257 FittingMode::Type fittingMode,
258 Dali::SamplingMode::Type samplingMode )
259 : Visual::Base( factoryCache ),
261 mPixelArea( FULL_TEXTURE_RECT ),
263 mImageUrl( imageUrl ),
264 mMaskingData( NULL ),
265 mDesiredSize( size ),
266 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
267 mFittingMode( fittingMode ),
268 mSamplingMode( samplingMode ),
269 mWrapModeU( WrapMode::DEFAULT ),
270 mWrapModeV( WrapMode::DEFAULT ),
271 mAttemptAtlasing( false ),
272 mTextureLoading( false )
276 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
277 : Visual::Base( factoryCache ),
279 mPixelArea( FULL_TEXTURE_RECT ),
282 mMaskingData( NULL ),
284 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
285 mFittingMode( FittingMode::DEFAULT ),
286 mSamplingMode( SamplingMode::DEFAULT ),
287 mWrapModeU( WrapMode::DEFAULT ),
288 mWrapModeV( WrapMode::DEFAULT ),
289 mAttemptAtlasing( false ),
290 mTextureLoading( false )
294 ImageVisual::~ImageVisual()
299 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
301 // Url is already received in constructor
302 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
304 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
305 if( keyValue.first.type == Property::Key::INDEX )
307 DoSetProperty( keyValue.first.indexKey, keyValue.second );
311 if( keyValue.first == IMAGE_FITTING_MODE )
313 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
315 else if( keyValue.first == IMAGE_SAMPLING_MODE )
317 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
319 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
321 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
323 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
325 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
327 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
329 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
331 else if( keyValue.first == IMAGE_WRAP_MODE_U )
333 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
335 else if( keyValue.first == IMAGE_WRAP_MODE_V )
337 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
339 else if( keyValue.first == SYNCHRONOUS_LOADING )
341 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
343 else if ( keyValue.first == IMAGE_ATLASING )
345 DoSetProperty( Toolkit::DevelImageVisual::Property::ATLASING, keyValue.second );
347 else if ( keyValue.first == ALPHA_MASK_URL )
349 DoSetProperty( Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
351 else if ( keyValue.first == MASK_CONTENT_SCALE_NAME )
353 DoSetProperty( Toolkit::DevelImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
355 else if ( keyValue.first == CROP_TO_MASK_NAME )
357 DoSetProperty( Toolkit::DevelImageVisual::Property::CROP_TO_MASK, keyValue.second );
364 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
368 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
371 if( value.Get( sync ) )
375 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
379 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
384 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
389 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
392 if( value.Get( desiredWidth ) )
394 mDesiredSize.SetWidth( desiredWidth );
398 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
403 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
406 if( value.Get( desiredHeight ) )
408 mDesiredSize.SetHeight( desiredHeight );
412 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
417 case Toolkit::ImageVisual::Property::FITTING_MODE:
420 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
421 mFittingMode = Dali::FittingMode::Type( fittingMode );
425 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
428 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
429 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
433 case Toolkit::ImageVisual::Property::PIXEL_AREA:
435 value.Get( mPixelArea );
439 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
442 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
443 mWrapModeU = Dali::WrapMode::Type( wrapMode );
447 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
450 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
451 mWrapModeV = Dali::WrapMode::Type( wrapMode );
455 case Toolkit::DevelImageVisual::Property::ATLASING:
457 bool atlasing = false;
458 mAttemptAtlasing = value.Get( atlasing );
462 case Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL:
464 std::string alphaUrl;
465 if( value.Get( alphaUrl ) )
468 // Immediately trigger the alpha mask loading (it may just get a cached value)
469 mMaskingData->SetImage( alphaUrl );
474 case Toolkit::DevelImageVisual::Property::MASK_CONTENT_SCALE:
477 if( value.Get( scale ) )
480 mMaskingData->mContentScaleFactor = scale;
485 case Toolkit::DevelImageVisual::Property::CROP_TO_MASK:
488 if( value.Get( crop ) )
491 mMaskingData->mCropToMask = crop;
498 void ImageVisual::AllocateMaskData()
500 if( mMaskingData == NULL )
502 TextureManager& textureManager = mFactoryCache.GetTextureManager();
503 mMaskingData = new MaskingData(textureManager);
507 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
511 naturalSize.x = mImage.GetWidth();
512 naturalSize.y = mImage.GetHeight();
515 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
517 naturalSize.x = mDesiredSize.GetWidth();
518 naturalSize.y = mDesiredSize.GetHeight();
521 else if( mImpl->mRenderer ) // Check if we have a loaded image
523 auto textureSet = mImpl->mRenderer.GetTextures();
527 auto texture = textureSet.GetTexture(0);
528 naturalSize.x = texture.GetWidth();
529 naturalSize.y = texture.GetHeight();
534 if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
535 mMaskingData->mCropToMask )
537 ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
538 if( dimensions != ImageDimensions( 0, 0 ) )
540 naturalSize.x = dimensions.GetWidth();
541 naturalSize.y = dimensions.GetHeight();
545 else if( mImageUrl.IsValid() )
547 if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
549 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
551 if( dimensions != ImageDimensions( 0, 0 ) )
553 naturalSize.x = dimensions.GetWidth();
554 naturalSize.y = dimensions.GetHeight();
558 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
560 naturalSize.x = brokenImage.GetWidth();
561 naturalSize.y = brokenImage.GetWidth();
567 naturalSize = Vector2::ZERO;
570 void ImageVisual::CreateRenderer( TextureSet& textureSet )
575 if( !mImpl->mCustomShader )
577 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
579 shader = GetImageShader( mFactoryCache,
580 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
581 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
585 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
586 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
588 shader = GetImageShader( mFactoryCache, false, true );
592 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
593 mImpl->mCustomShader->mFragmentShader.empty() ? FRAGMENT_SHADER_NO_ATLAS : mImpl->mCustomShader->mFragmentShader,
594 mImpl->mCustomShader->mHints );
595 if( mImpl->mCustomShader->mVertexShader.empty() )
597 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
602 // Set pixel align off as default.
603 // ToDo: Pixel align causes issues such as rattling image animation.
604 // We should trun it off until issues are resolved
605 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
607 mImpl->mRenderer = Renderer::New( geometry, shader );
610 mImpl->mRenderer.SetTextures( textureSet );
612 // else still waiting for texture load to finish.
614 //Register transform properties
615 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
618 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
623 std::string fragmentShader;
624 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
625 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
629 fragmentShader = fragmentPreFix;
630 fragmentShader += "\n";
633 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
635 fragmentShader += mImpl->mCustomShader->mFragmentShader;
639 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
642 if( customSamplerTypename )
644 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
647 if( !mImpl->mCustomShader )
649 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
651 shader = Shader::New( VERTEX_SHADER, fragmentShader );
652 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
656 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
657 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
659 mImpl->mCustomShader->mHints );
660 if( mImpl->mCustomShader->mVertexShader.empty() )
662 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
666 mImpl->mRenderer = Renderer::New( geometry, shader );
668 //Register transform properties
669 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
673 bool ImageVisual::IsSynchronousResourceLoading() const
675 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
678 TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing )
680 TextureSet textureSet;
682 mTextureLoading = false;
683 textureRect = FULL_TEXTURE_RECT;
685 if( synchronousLoading )
688 if( mImageUrl.IsValid() )
690 // if sync loading is required, the loading should immediately when actor is on stage
691 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode );
695 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
701 textureSet = TextureSet::New();
702 TextureSetImage( textureSet, 0u, VisualFactoryCache::GetBrokenVisualImage() );
706 if( attemptAtlasing )
708 textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, data );
709 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
711 if( !textureSet ) // big image, no atlasing or atlasing failed
713 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
714 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
715 data.GetWidth(), data.GetHeight() );
716 texture.Upload( data );
717 textureSet = TextureSet::New();
718 textureSet.SetTexture( 0u, texture );
724 mTextureLoading = true;
725 if( attemptAtlasing )
727 textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, mImageUrl.GetUrl(), mDesiredSize, mFittingMode, true, this );
728 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
730 if( !textureSet ) // big image, no atlasing or atlasing failed
732 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
733 TextureManager& textureManager = mFactoryCache.GetTextureManager();
734 if( mMaskingData == NULL )
736 mTextureId = textureManager.RequestLoad( mImageUrl, mDesiredSize, mFittingMode,
737 mSamplingMode, TextureManager::NO_ATLAS, this );
741 mTextureId = textureManager.RequestLoad( mImageUrl,
742 mMaskingData->mAlphaMaskId,
743 mMaskingData->mContentScaleFactor,
745 mFittingMode, mSamplingMode,
746 TextureManager::NO_ATLAS,
747 mMaskingData->mCropToMask,
751 TextureManager::LoadState loadState = textureManager.GetTextureState( mTextureId );
752 mTextureLoading = ( loadState == TextureManager::LOADING );
754 if( loadState == TextureManager::UPLOADED )
756 // UploadComplete has already been called - keep the same texture set
757 textureSet = textureManager.GetTextureSet(mTextureId);
762 if( ! (mImpl->mFlags & Impl::IS_ATLASING_APPLIED) && textureSet )
764 Sampler sampler = Sampler::New();
765 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
766 textureSet.SetSampler( 0u, sampler );
772 void ImageVisual::InitializeRenderer()
774 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
776 if( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
778 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
781 // texture set has to be created first as we need to know if atlasing succeeded or not
782 // when selecting the shader
783 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), mAttemptAtlasing );
784 CreateRenderer( textures );
786 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas
788 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
789 if( !defaultWrapMode ) // custom wrap mode, renderer is not cached.
791 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
792 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
793 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
799 // for custom shader or remote image, atlas is not applied
800 Vector4 atlasRect; // ignored in this case
801 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), false );
802 CreateRenderer( textures );
806 void ImageVisual::InitializeRenderer( const Image& image )
808 // don't reuse CreateTextureSet
809 TextureSet textures = TextureSet::New();
811 NativeImage nativeImage = NativeImage::DownCast( image );
814 CreateNativeImageRenderer( nativeImage );
815 DALI_ASSERT_DEBUG( textures );
816 mImpl->mRenderer.SetTextures( textures );
820 // reuse existing code for regular images
821 CreateRenderer( textures );
823 ApplyImageToSampler( image );
826 void ImageVisual::DoSetOnStage( Actor& actor )
828 if( mImageUrl.IsValid() )
830 InitializeRenderer();
834 InitializeRenderer( mImage );
837 if ( !mImpl->mRenderer )
842 mPlacementActor = actor;
843 // Search the Actor tree to find if Layer UI behaviour set.
844 Layer layer = actor.GetLayer();
845 if ( layer && layer.GetBehavior() == Layer::LAYER_3D )
847 // Layer 3D set, do not align pixels
848 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
851 if( mPixelArea != FULL_TEXTURE_RECT )
853 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
856 if( mTextureLoading == false )
858 actor.AddRenderer( mImpl->mRenderer );
859 mPlacementActor.Reset();
861 // Image loaded and ready to display
866 void ImageVisual::DoSetOffStage( Actor& actor )
868 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
870 //If we own the image then make sure we release it when we go off stage
871 actor.RemoveRenderer( mImpl->mRenderer);
872 if( mImageUrl.IsValid() )
874 RemoveTexture( mImageUrl.GetUrl() );
877 mTextureLoading = false;
878 mImpl->mRenderer.Reset();
879 mPlacementActor.Reset();
882 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
885 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
887 bool sync = IsSynchronousResourceLoading();
888 map.Insert( SYNCHRONOUS_LOADING, sync );
889 if( mImageUrl.IsValid() )
891 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
892 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
893 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
897 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
898 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
900 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
903 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
907 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
908 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
910 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
911 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
912 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
914 map.Insert( Toolkit::DevelImageVisual::Property::ATLASING, mAttemptAtlasing );
915 if( mMaskingData != NULL )
917 map.Insert( Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
918 map.Insert( Toolkit::DevelImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
919 map.Insert( Toolkit::DevelImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
923 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
926 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
927 if( mImageUrl.IsValid() )
929 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
930 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
934 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
935 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
939 void ImageVisual::OnSetTransform()
941 if( mImpl->mRenderer )
943 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
947 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
952 if( defaultTextureWrapping )
954 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
957 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
958 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
959 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
964 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
967 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
968 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
969 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
975 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
978 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
979 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
980 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
987 void ImageVisual::ApplyImageToSampler( const Image& image )
991 TextureSet textureSet = mImpl->mRenderer.GetTextures();
992 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
994 TextureSetImage( textureSet, 0u, image );
995 Sampler sampler = Sampler::New();
996 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
997 textureSet.SetSampler( 0u, sampler );
1001 // From existing atlas manager
1002 void ImageVisual::UploadCompleted()
1004 // Texture has been uploaded. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
1005 Actor actor = mPlacementActor.GetHandle();
1008 actor.AddRenderer( mImpl->mRenderer );
1010 // reset the weak handle so that the renderer only get added to actor once
1011 mPlacementActor.Reset();
1013 mTextureLoading = false;
1016 // From Texture Manager
1017 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle )
1019 Actor actor = mPlacementActor.GetHandle();
1022 if( mImpl->mRenderer )
1024 actor.AddRenderer( mImpl->mRenderer );
1025 // reset the weak handle so that the renderer only get added to actor once
1026 mPlacementActor.Reset();
1028 if( loadingSuccess )
1030 Sampler sampler = Sampler::New();
1031 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
1032 textureSet.SetSampler( 0u, sampler );
1033 mImpl->mRenderer.SetTextures(textureSet);
1037 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
1039 textureSet = TextureSet::New();
1040 mImpl->mRenderer.SetTextures( textureSet );
1042 ApplyImageToSampler( brokenImage );
1044 // Image loaded and ready to display
1048 mTextureLoading = false;
1051 void ImageVisual::RemoveTexture(const std::string& url)
1053 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1055 mFactoryCache.GetTextureManager().Remove( mTextureId );
1056 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1060 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1061 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1062 if( index != Property::INVALID_INDEX )
1064 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1065 atlasRectValue.Get( atlasRect );
1068 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1069 mImpl->mRenderer.Reset();
1071 if( index != Property::INVALID_INDEX )
1073 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1079 ImageVisual::MaskingData::MaskingData( TextureManager& textureManager )
1080 : mTextureManager( textureManager ),
1082 mAlphaMaskId( TextureManager::INVALID_TEXTURE_ID ),
1083 mContentScaleFactor( 1.0f ),
1088 ImageVisual::MaskingData::~MaskingData()
1090 if( Stage::IsInstalled() )
1092 // TextureManager could have been deleted before the actor that contains this
1093 // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
1094 // is still valid before accessing texture manager.
1095 if( mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
1097 mTextureManager.Remove( mAlphaMaskId );
1102 void ImageVisual::MaskingData::SetImage( const std::string& maskUrl )
1104 mAlphaMaskUrl = maskUrl;
1105 mAlphaMaskId = mTextureManager.RequestMaskLoad( mAlphaMaskUrl );
1108 } // namespace Internal
1110 } // namespace Toolkit