2 * Copyright (c) 2016 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/images/resource-image.h>
25 #include <dali/public-api/images/native-image.h>
26 #include <dali/devel-api/images/texture-set-image.h>
27 #include <dali/devel-api/adaptor-framework/image-loading.h>
28 #include <dali/devel-api/scripting/enum-helper.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
34 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
35 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
36 #include <dali-toolkit/internal/visuals/texture-manager.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
40 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
41 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
42 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
43 #include <dali-toolkit/internal/visuals/visual-url.h>
58 const char * const IMAGE_FITTING_MODE( "fittingMode" );
59 const char * const IMAGE_SAMPLING_MODE( "samplingMode" );
60 const char * const IMAGE_DESIRED_WIDTH( "desiredWidth" );
61 const char * const IMAGE_DESIRED_HEIGHT( "desiredHeight" );
62 const char * const SYNCHRONOUS_LOADING( "synchronousLoading" );
63 const char * const IMAGE_ATLASING("atlasing");
64 const char * const ALPHA_MASK_URL("alphaMaskUrl");
67 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
68 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SHRINK_TO_FIT )
69 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, SCALE_TO_FILL )
70 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_WIDTH )
71 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, FIT_HEIGHT )
72 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::FittingMode, DEFAULT )
73 DALI_ENUM_TO_STRING_TABLE_END( FITTING_MODE )
76 DALI_ENUM_TO_STRING_TABLE_BEGIN( SAMPLING_MODE )
77 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX )
78 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NEAREST )
79 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, LINEAR )
80 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_NEAREST )
81 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, BOX_THEN_LINEAR )
82 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, NO_FILTER )
83 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::SamplingMode, DONT_CARE )
84 DALI_ENUM_TO_STRING_TABLE_END( SAMPLING_MODE )
87 DALI_ENUM_TO_STRING_TABLE_BEGIN( WRAP_MODE )
88 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, DEFAULT )
89 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, CLAMP_TO_EDGE )
90 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, REPEAT )
91 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::WrapMode, MIRRORED_REPEAT )
92 DALI_ENUM_TO_STRING_TABLE_END( WRAP_MODE )
94 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
96 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
98 const float PIXEL_ALIGN_ON = 1.0f;
99 const float PIXEL_ALIGN_OFF = 0.0f;
101 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
102 attribute mediump vec2 aPosition;\n
103 uniform mediump mat4 uModelMatrix;\n
104 uniform mediump mat4 uViewMatrix;\n
105 uniform mediump mat4 uProjection;\n
106 uniform mediump vec3 uSize;\n
107 uniform mediump vec4 pixelArea;
108 varying mediump vec2 vTexCoord;\n
109 uniform lowp float uPixelAligned;\n
111 //Visual size and offset
112 uniform mediump vec2 offset;\n
113 uniform mediump vec2 size;\n
114 uniform mediump vec4 offsetSizeMode;\n
115 uniform mediump vec2 origin;\n
116 uniform mediump vec2 anchorPoint;\n
118 vec4 ComputeVertexPosition()\n
120 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
121 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
122 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
127 mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n
128 vec4 alignedVertexPosition = vertexPosition;\n
129 alignedVertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment
130 vertexPosition = uProjection * mix( vertexPosition, alignedVertexPosition, uPixelAligned );\n
131 vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
132 gl_Position = vertexPosition;\n
136 const char* FRAGMENT_SHADER_NO_ATLAS = DALI_COMPOSE_SHADER(
137 varying mediump vec2 vTexCoord;\n
138 uniform sampler2D sTexture;\n
139 uniform lowp vec4 uColor;\n
140 uniform lowp vec3 mixColor;\n
141 uniform lowp float opacity;\n
142 uniform lowp float preMultipliedAlpha;\n
144 lowp vec4 visualMixColor()\n
146 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
150 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
154 const char* FRAGMENT_SHADER_ATLAS_CLAMP = DALI_COMPOSE_SHADER(
155 varying mediump vec2 vTexCoord;\n
156 uniform sampler2D sTexture;\n
157 uniform mediump vec4 uAtlasRect;\n
158 uniform lowp vec4 uColor;\n
159 uniform lowp vec3 mixColor;\n
160 uniform lowp float opacity;\n
161 uniform lowp float preMultipliedAlpha;\n
163 lowp vec4 visualMixColor()\n
165 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
170 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );\n
171 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
175 const char* FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP = DALI_COMPOSE_SHADER(
176 varying mediump vec2 vTexCoord;\n
177 uniform sampler2D sTexture;\n
178 uniform mediump vec4 uAtlasRect;\n
179 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
180 uniform lowp vec2 wrapMode;\n
181 uniform lowp vec4 uColor;\n
182 uniform lowp vec3 mixColor;\n
183 uniform lowp float opacity;\n
184 uniform lowp float preMultipliedAlpha;\n
186 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n
188 mediump float coord;\n
189 if( wrap > 1.5 )\n // REFLECT
190 coord = 1.0-abs(fract(coordinate*0.5)*2.0 - 1.0);\n
191 else \n// warp == 0 or 1
192 coord = mix(coordinate, fract( coordinate ), wrap);\n
193 return clamp( mix(range.x, range.y, coord), range.x, range.y );
196 lowp vec4 visualMixColor()\n
198 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
203 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
204 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );\n
205 gl_FragColor = texture2D( sTexture, texCoord ) * uColor * visualMixColor();\n
209 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
213 if( gridSize == ImageDimensions( 1, 1 ) )
215 geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
219 geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
225 } // unnamed namespace
227 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
228 const VisualUrl& imageUrl,
229 const Property::Map& properties,
230 ImageDimensions size,
231 FittingMode::Type fittingMode,
232 Dali::SamplingMode::Type samplingMode )
234 ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ) );
235 imageVisualPtr->SetProperties( properties );
236 return imageVisualPtr;
239 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
240 const VisualUrl& imageUrl,
241 ImageDimensions size,
242 FittingMode::Type fittingMode,
243 Dali::SamplingMode::Type samplingMode )
245 return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode );
248 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image )
250 return new ImageVisual( factoryCache, image );
253 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
254 const VisualUrl& imageUrl,
255 ImageDimensions size,
256 FittingMode::Type fittingMode,
257 Dali::SamplingMode::Type samplingMode )
258 : Visual::Base( factoryCache ),
261 mPixelArea( FULL_TEXTURE_RECT ),
263 mImageUrl( imageUrl ),
265 mDesiredSize( size ),
266 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
267 mAlphaMaskId( TextureManager::INVALID_TEXTURE_ID ),
268 mFittingMode( fittingMode ),
269 mSamplingMode( samplingMode ),
270 mWrapModeU( WrapMode::DEFAULT ),
271 mWrapModeV( WrapMode::DEFAULT ),
272 mAttemptAtlasing( false ),
273 mTextureLoading( false )
277 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
278 : Visual::Base( factoryCache ),
281 mPixelArea( FULL_TEXTURE_RECT ),
286 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
287 mAlphaMaskId( TextureManager::INVALID_TEXTURE_ID ),
288 mFittingMode( FittingMode::DEFAULT ),
289 mSamplingMode( SamplingMode::DEFAULT ),
290 mWrapModeU( WrapMode::DEFAULT ),
291 mWrapModeV( WrapMode::DEFAULT ),
292 mAttemptAtlasing( false ),
293 mTextureLoading( false )
297 ImageVisual::~ImageVisual()
299 if( mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID )
301 TextureManager& textureManager = mFactoryCache.GetTextureManager();
302 textureManager.Remove( mAlphaMaskId );
306 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
308 // Url is already received in constructor
309 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
311 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
312 if( keyValue.first.type == Property::Key::INDEX )
314 DoSetProperty( keyValue.first.indexKey, keyValue.second );
318 if( keyValue.first == IMAGE_FITTING_MODE )
320 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
322 else if( keyValue.first == IMAGE_SAMPLING_MODE )
324 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
326 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
328 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
330 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
332 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
334 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
336 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
338 else if( keyValue.first == IMAGE_WRAP_MODE_U )
340 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
342 else if( keyValue.first == IMAGE_WRAP_MODE_V )
344 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
346 else if( keyValue.first == SYNCHRONOUS_LOADING )
348 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
350 else if ( keyValue.first == IMAGE_ATLASING )
352 DoSetProperty( Toolkit::DevelImageVisual::Property::ATLASING, keyValue.second );
354 else if ( keyValue.first == ALPHA_MASK_URL )
356 DoSetProperty( Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
361 if( mAlphaMaskUrl.IsValid() )
363 // Immediately trigger the alpha mask loading (it may just get a cached value)
364 TextureManager& textureManager = mFactoryCache.GetTextureManager();
365 mAlphaMaskId = textureManager.RequestMaskLoad( mAlphaMaskUrl );
368 if( ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING ) && mImageUrl.IsValid() )
370 // if sync loading is required, the loading should start
371 // immediately when new image url is set or the actor is off stage
372 // ( for on-stage actor with image url unchanged, resource loading
373 // is already finished )
374 LoadResourceSynchronously();
378 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
382 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
385 if( value.Get( sync ) )
389 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
393 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
398 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
403 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
406 if( value.Get( desiredWidth ) )
408 mDesiredSize.SetWidth( desiredWidth );
412 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
417 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
420 if( value.Get( desiredHeight ) )
422 mDesiredSize.SetHeight( desiredHeight );
426 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
431 case Toolkit::ImageVisual::Property::FITTING_MODE:
434 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
435 mFittingMode = Dali::FittingMode::Type( fittingMode );
439 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
442 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
443 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
447 case Toolkit::ImageVisual::Property::PIXEL_AREA:
449 value.Get( mPixelArea );
453 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
456 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
457 mWrapModeU = Dali::WrapMode::Type( wrapMode );
461 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
464 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
465 mWrapModeV = Dali::WrapMode::Type( wrapMode );
469 case Toolkit::DevelImageVisual::Property::ATLASING:
471 bool atlasing = false;
472 mAttemptAtlasing = value.Get( atlasing );
476 case Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL:
478 std::string alphaUrl;
479 if( value.Get( alphaUrl ) )
481 mAlphaMaskUrl = VisualUrl( alphaUrl );
488 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
492 naturalSize.x = mImage.GetWidth();
493 naturalSize.y = mImage.GetHeight();
496 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
498 naturalSize.x = mDesiredSize.GetWidth();
499 naturalSize.y = mDesiredSize.GetHeight();
502 else if( mImageUrl.IsValid() && mImageUrl.GetLocation() == VisualUrl::LOCAL )
504 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
506 if( dimensions != ImageDimensions( 0, 0 ) )
508 naturalSize.x = dimensions.GetWidth();
509 naturalSize.y = dimensions.GetHeight();
513 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
515 naturalSize.x = brokenImage.GetWidth();
516 naturalSize.y = brokenImage.GetWidth();
521 naturalSize = Vector2::ZERO;
524 void ImageVisual::CreateRenderer( TextureSet& textureSet )
529 if( !mImpl->mCustomShader )
531 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
533 shader = GetImageShader( mFactoryCache,
534 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
535 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
539 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
540 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
542 shader = GetImageShader( mFactoryCache, false, true );
546 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
547 mImpl->mCustomShader->mFragmentShader.empty() ? FRAGMENT_SHADER_NO_ATLAS : mImpl->mCustomShader->mFragmentShader,
548 mImpl->mCustomShader->mHints );
549 if( mImpl->mCustomShader->mVertexShader.empty() )
551 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
556 // Set pixel align off as default.
557 // ToDo: Pixel align causes issues such as rattling image animation.
558 // We should trun it off until issues are resolved
559 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
561 mImpl->mRenderer = Renderer::New( geometry, shader );
564 mImpl->mRenderer.SetTextures( textureSet );
566 // else still waiting for texture load to finish.
568 //Register transform properties
569 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
572 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
577 std::string fragmentShader;
578 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
579 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
583 fragmentShader = fragmentPreFix;
584 fragmentShader += "\n";
587 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
589 fragmentShader += mImpl->mCustomShader->mFragmentShader;
593 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
596 if( customSamplerTypename )
598 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
601 if( !mImpl->mCustomShader )
603 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
605 shader = Shader::New( VERTEX_SHADER, fragmentShader );
606 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
610 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
611 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
613 mImpl->mCustomShader->mHints );
614 if( mImpl->mCustomShader->mVertexShader.empty() )
616 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
620 mImpl->mRenderer = Renderer::New( geometry, shader );
622 //Register transform properties
623 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
627 bool ImageVisual::IsSynchronousResourceLoading() const
629 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
632 void ImageVisual::LoadResourceSynchronously()
634 if( mImageUrl.IsValid() )
636 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode );
640 mPixels = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
642 mTextureLoading = false;
646 TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing )
648 TextureSet textureSet;
650 mTextureLoading = false;
652 textureRect = FULL_TEXTURE_RECT;
653 if( synchronousLoading )
658 textureSet = TextureSet::New();
659 TextureSetImage( textureSet, 0u, VisualFactoryCache::GetBrokenVisualImage() );
663 if( attemptAtlasing )
665 textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, mPixels );
666 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
668 if( !textureSet ) // big image, no atlasing or atlasing failed
670 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
671 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, mPixels.GetPixelFormat(),
672 mPixels.GetWidth(), mPixels.GetHeight() );
673 texture.Upload( mPixels );
674 textureSet = TextureSet::New();
675 textureSet.SetTexture( 0u, texture );
681 if( attemptAtlasing )
683 textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, mImageUrl.GetUrl(), mDesiredSize, mFittingMode, true, this );
684 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
685 mTextureLoading = true;
687 if( !textureSet ) // big image, no atlasing or atlasing failed
689 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
690 TextureManager& textureManager = mFactoryCache.GetTextureManager();
691 if( mAlphaMaskId == TextureManager::INVALID_TEXTURE_ID )
693 mTextureId = textureManager.RequestLoad( mImageUrl, mDesiredSize, mFittingMode,
694 mSamplingMode, TextureManager::NO_ATLAS, this );
698 mTextureId = textureManager.RequestLoad( mImageUrl, mAlphaMaskId, mDesiredSize,
699 mFittingMode, mSamplingMode,
700 TextureManager::NO_ATLAS, this );
703 TextureManager::LoadState loadState = textureManager.GetTextureState( mTextureId );
705 mTextureLoading = ( loadState == TextureManager::LOADING );
707 if( loadState == TextureManager::UPLOADED )
709 // UploadComplete has already been called - keep the same texture set
710 textureSet = textureManager.GetTextureSet(mTextureId);
715 if( ! (mImpl->mFlags & Impl::IS_ATLASING_APPLIED) && textureSet )
717 Sampler sampler = Sampler::New();
718 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
719 textureSet.SetSampler( 0u, sampler );
725 void ImageVisual::InitializeRenderer()
727 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
729 if( ! mImpl->mCustomShader && mImageUrl.GetLocation() == VisualUrl::LOCAL )
731 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
734 // texture set has to be created first as we need to know if atlasing succeeded or not
735 // when selecting the shader
736 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), mAttemptAtlasing );
737 CreateRenderer( textures );
739 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas
741 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
742 if( !defaultWrapMode ) // custom wrap mode, renderer is not cached.
744 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
745 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
746 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
752 // for custom shader or remote image, atlas is not applied
753 Vector4 atlasRect; // ignored in this case
754 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), false );
755 CreateRenderer( textures );
759 void ImageVisual::InitializeRenderer( const Image& image )
761 // don't reuse CreateTextureSet
762 TextureSet textures = TextureSet::New();
764 NativeImage nativeImage = NativeImage::DownCast( image );
767 CreateNativeImageRenderer( nativeImage );
768 DALI_ASSERT_DEBUG( textures );
769 mImpl->mRenderer.SetTextures( textures );
773 // reuse existing code for regular images
774 CreateRenderer( textures );
776 ApplyImageToSampler( image );
779 void ImageVisual::DoSetOnStage( Actor& actor )
781 if( mImageUrl.IsValid() )
783 InitializeRenderer();
787 InitializeRenderer( mImage );
790 if ( !mImpl->mRenderer )
795 mPlacementActor = actor;
796 // Search the Actor tree to find if Layer UI behaviour set.
797 Layer layer = actor.GetLayer();
798 if ( layer && layer.GetBehavior() == Layer::LAYER_3D )
800 // Layer 3D set, do not align pixels
801 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
804 if( mPixelArea != FULL_TEXTURE_RECT )
806 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
809 if( mTextureLoading == false )
811 actor.AddRenderer( mImpl->mRenderer );
812 mPlacementActor.Reset();
814 // Image loaded and ready to display
819 void ImageVisual::DoSetOffStage( Actor& actor )
821 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
823 //If we own the image then make sure we release it when we go off stage
824 actor.RemoveRenderer( mImpl->mRenderer);
825 if( mImageUrl.IsValid() )
827 RemoveTexture( mImageUrl.GetUrl() );
830 mTextureLoading = false;
831 mImpl->mRenderer.Reset();
832 mPlacementActor.Reset();
835 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
838 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
840 bool sync = IsSynchronousResourceLoading();
841 map.Insert( SYNCHRONOUS_LOADING, sync );
842 if( mImageUrl.IsValid() )
844 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
845 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
846 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
850 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
851 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
853 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
856 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
860 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
861 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
863 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
864 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
865 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
867 map.Insert( Toolkit::DevelImageVisual::Property::ATLASING, mAttemptAtlasing );
868 map.Insert( Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL, mAlphaMaskUrl.GetUrl() );
871 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
874 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
875 if( mImageUrl.IsValid() )
877 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
878 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
882 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
883 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
887 void ImageVisual::OnSetTransform()
889 if( mImpl->mRenderer )
891 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
895 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
900 if( defaultTextureWrapping )
902 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
905 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
906 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
907 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
912 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
915 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
916 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
917 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
923 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
926 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
927 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
928 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
935 void ImageVisual::ApplyImageToSampler( const Image& image )
939 TextureSet textureSet = mImpl->mRenderer.GetTextures();
940 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
942 TextureSetImage( textureSet, 0u, image );
943 Sampler sampler = Sampler::New();
944 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
945 textureSet.SetSampler( 0u, sampler );
949 // From existing atlas manager
950 void ImageVisual::UploadCompleted()
952 // Texture has been uploaded. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
953 Actor actor = mPlacementActor.GetHandle();
956 actor.AddRenderer( mImpl->mRenderer );
958 // reset the weak handle so that the renderer only get added to actor once
959 mPlacementActor.Reset();
961 mTextureLoading = false;
964 // From Texture Manager
965 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle )
967 Actor actor = mPlacementActor.GetHandle();
970 if( mImpl->mRenderer )
972 actor.AddRenderer( mImpl->mRenderer );
973 // reset the weak handle so that the renderer only get added to actor once
974 mPlacementActor.Reset();
978 Sampler sampler = Sampler::New();
979 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
980 textureSet.SetSampler( 0u, sampler );
981 mImpl->mRenderer.SetTextures(textureSet);
985 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
987 textureSet = TextureSet::New();
988 mImpl->mRenderer.SetTextures( textureSet );
990 ApplyImageToSampler( brokenImage );
992 // Image loaded and ready to display
996 mTextureLoading = false;
999 void ImageVisual::RemoveTexture(const std::string& url)
1001 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1003 mFactoryCache.GetTextureManager().Remove( mTextureId );
1004 mTextureId = TextureManager::INVALID_TEXTURE_ID;
1008 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1009 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1010 if( index != Property::INVALID_INDEX )
1012 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1013 atlasRectValue.Get( atlasRect );
1016 TextureSet textureSet = mImpl->mRenderer.GetTextures();
1017 mImpl->mRenderer.Reset();
1019 if( index != Property::INVALID_INDEX )
1021 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1026 } // namespace Internal
1028 } // namespace Toolkit