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/bitmap-loader.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.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");
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 ),
264 mDesiredSize( size ),
265 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
266 mFittingMode( fittingMode ),
267 mSamplingMode( samplingMode ),
268 mWrapModeU( WrapMode::DEFAULT ),
269 mWrapModeV( WrapMode::DEFAULT ),
270 mAttemptAtlasing( false ),
271 mTextureLoading( false )
275 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
276 : Visual::Base( factoryCache ),
279 mPixelArea( FULL_TEXTURE_RECT ),
283 mTextureId( TextureManager::INVALID_TEXTURE_ID ),
284 mFittingMode( FittingMode::DEFAULT ),
285 mSamplingMode( SamplingMode::DEFAULT ),
286 mWrapModeU( WrapMode::DEFAULT ),
287 mWrapModeV( WrapMode::DEFAULT ),
288 mAttemptAtlasing( false ),
289 mTextureLoading( false )
293 ImageVisual::~ImageVisual()
297 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
299 // Url is already received in constructor
300 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
302 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
303 if( keyValue.first.type == Property::Key::INDEX )
305 DoSetProperty( keyValue.first.indexKey, keyValue.second );
309 if( keyValue.first == IMAGE_FITTING_MODE )
311 DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
313 else if( keyValue.first == IMAGE_SAMPLING_MODE )
315 DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
317 else if( keyValue.first == IMAGE_DESIRED_WIDTH )
319 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
321 else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
323 DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
325 else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
327 DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
329 else if( keyValue.first == IMAGE_WRAP_MODE_U )
331 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
333 else if( keyValue.first == IMAGE_WRAP_MODE_V )
335 DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
337 else if( keyValue.first == SYNCHRONOUS_LOADING )
339 DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
341 else if ( keyValue.first == IMAGE_ATLASING )
343 DoSetProperty( Toolkit::DevelImageVisual::Property::ATLASING, keyValue.second );
348 if( ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING ) && mImageUrl.IsValid() )
350 // if sync loading is required, the loading should start
351 // immediately when new image url is set or the actor is off stage
352 // ( for on-stage actor with image url unchanged, resource loading
353 // is already finished )
354 LoadResourceSynchronously();
358 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
362 case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
365 if( value.Get( sync ) )
369 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
373 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
378 DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
383 case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
386 if( value.Get( desiredWidth ) )
388 mDesiredSize.SetWidth( desiredWidth );
392 DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
397 case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
400 if( value.Get( desiredHeight ) )
402 mDesiredSize.SetHeight( desiredHeight );
406 DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
411 case Toolkit::ImageVisual::Property::FITTING_MODE:
414 Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
415 mFittingMode = Dali::FittingMode::Type( fittingMode );
419 case Toolkit::ImageVisual::Property::SAMPLING_MODE:
422 Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
423 mSamplingMode = Dali::SamplingMode::Type( samplingMode );
427 case Toolkit::ImageVisual::Property::PIXEL_AREA:
429 value.Get( mPixelArea );
433 case Toolkit::ImageVisual::Property::WRAP_MODE_U:
436 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
437 mWrapModeU = Dali::WrapMode::Type( wrapMode );
441 case Toolkit::ImageVisual::Property::WRAP_MODE_V:
444 Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
445 mWrapModeV = Dali::WrapMode::Type( wrapMode );
449 case Toolkit::DevelImageVisual::Property::ATLASING:
451 bool atlasing = false;
452 mAttemptAtlasing = value.Get( atlasing );
457 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
461 naturalSize.x = mImage.GetWidth();
462 naturalSize.y = mImage.GetHeight();
465 else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
467 naturalSize.x = mDesiredSize.GetWidth();
468 naturalSize.y = mDesiredSize.GetHeight();
471 else if( mImageUrl.IsValid() && mImageUrl.GetLocation() == VisualUrl::LOCAL )
473 ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
474 naturalSize.x = dimensions.GetWidth();
475 naturalSize.y = dimensions.GetHeight();
479 naturalSize = Vector2::ZERO;
482 void ImageVisual::CreateRenderer( TextureSet& textureSet )
487 if( !mImpl->mCustomShader )
489 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
491 shader = GetImageShader( mFactoryCache,
492 mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
493 mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
497 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
498 if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
500 shader = GetImageShader( mFactoryCache, false, true );
504 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
505 mImpl->mCustomShader->mFragmentShader.empty() ? FRAGMENT_SHADER_NO_ATLAS : mImpl->mCustomShader->mFragmentShader,
506 mImpl->mCustomShader->mHints );
507 if( mImpl->mCustomShader->mVertexShader.empty() )
509 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
514 shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_ON ); // Set default to align
516 mImpl->mRenderer = Renderer::New( geometry, shader );
519 mImpl->mRenderer.SetTextures( textureSet );
521 // else still waiting for texture load to finish.
523 //Register transform properties
524 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
527 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
532 std::string fragmentShader;
533 const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
534 const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
538 fragmentShader = fragmentPreFix;
539 fragmentShader += "\n";
542 if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
544 fragmentShader += mImpl->mCustomShader->mFragmentShader;
548 fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
551 if( customSamplerTypename )
553 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
556 if( !mImpl->mCustomShader )
558 geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
560 shader = Shader::New( VERTEX_SHADER, fragmentShader );
561 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
565 geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
566 shader = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
568 mImpl->mCustomShader->mHints );
569 if( mImpl->mCustomShader->mVertexShader.empty() )
571 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
575 mImpl->mRenderer = Renderer::New( geometry, shader );
577 //Register transform properties
578 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
582 bool ImageVisual::IsSynchronousResourceLoading() const
584 return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
587 void ImageVisual::LoadResourceSynchronously()
589 if( mImageUrl.IsValid() )
591 BitmapLoader loader = BitmapLoader::New( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode );
593 mPixels = loader.GetPixelData();
594 mTextureLoading = false;
598 TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing )
600 TextureSet textureSet;
602 mTextureLoading = false;
604 textureRect = FULL_TEXTURE_RECT;
605 if( synchronousLoading )
610 textureSet = TextureSet::New();
611 TextureSetImage( textureSet, 0u, VisualFactoryCache::GetBrokenVisualImage() );
615 if( attemptAtlasing )
617 textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, mPixels );
618 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
620 if( !textureSet ) // big image, no atlasing or atlasing failed
622 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
623 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, mPixels.GetPixelFormat(),
624 mPixels.GetWidth(), mPixels.GetHeight() );
625 texture.Upload( mPixels );
626 textureSet = TextureSet::New();
627 textureSet.SetTexture( 0u, texture );
633 if( attemptAtlasing )
635 textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, mImageUrl.GetUrl(), mDesiredSize, mFittingMode, true, this );
636 mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
637 mTextureLoading = true;
639 if( !textureSet ) // big image, no atlasing or atlasing failed
641 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
642 TextureManager& textureManager = mFactoryCache.GetTextureManager();
643 mTextureId = textureManager.RequestLoad( mImageUrl, mDesiredSize, mFittingMode,
644 mSamplingMode, TextureManager::NO_ATLAS, this );
646 TextureManager::LoadState loadState = textureManager.GetTextureState( mTextureId );
648 mTextureLoading = ( loadState == TextureManager::LOADING );
650 if( loadState == TextureManager::UPLOADED )
652 // UploadComplete has already been called - keep the same texture set
653 textureSet = textureManager.GetTextureSet(mTextureId);
658 if( ! (mImpl->mFlags & Impl::IS_ATLASING_APPLIED) && textureSet )
660 Sampler sampler = Sampler::New();
661 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
662 textureSet.SetSampler( 0u, sampler );
668 void ImageVisual::InitializeRenderer()
670 mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
672 if( ! mImpl->mCustomShader && mImageUrl.GetLocation() == VisualUrl::LOCAL )
674 bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
677 // texture set has to be created first as we need to know if atlasing succeeded or not
678 // when selecting the shader
679 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), mAttemptAtlasing );
680 CreateRenderer( textures );
682 if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas
684 mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
685 if( !defaultWrapMode ) // custom wrap mode, renderer is not cached.
687 Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
688 wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
689 mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
695 // for custom shader or remote image, atlas is not applied
696 Vector4 atlasRect; // ignored in this case
697 TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), false );
698 CreateRenderer( textures );
702 void ImageVisual::InitializeRenderer( const Image& image )
704 // don't reuse CreateTextureSet
705 TextureSet textures = TextureSet::New();
707 NativeImage nativeImage = NativeImage::DownCast( image );
710 CreateNativeImageRenderer( nativeImage );
711 DALI_ASSERT_DEBUG( textures );
712 mImpl->mRenderer.SetTextures( textures );
716 // reuse existing code for regular images
717 CreateRenderer( textures );
719 ApplyImageToSampler( image );
722 void ImageVisual::DoSetOnStage( Actor& actor )
724 if( mImageUrl.IsValid() )
726 InitializeRenderer();
730 InitializeRenderer( mImage );
733 if ( !mImpl->mRenderer )
738 mPlacementActor = actor;
739 // Search the Actor tree to find if Layer UI behaviour set.
740 Layer layer = actor.GetLayer();
741 if ( layer && layer.GetBehavior() == Layer::LAYER_3D )
743 // Layer 3D set, do not align pixels
744 mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
747 if( mPixelArea != FULL_TEXTURE_RECT )
749 mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
752 if( mTextureLoading == false )
754 actor.AddRenderer( mImpl->mRenderer );
755 mPlacementActor.Reset();
757 // Image loaded and ready to display
762 void ImageVisual::DoSetOffStage( Actor& actor )
764 // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
766 //If we own the image then make sure we release it when we go off stage
767 actor.RemoveRenderer( mImpl->mRenderer);
768 if( mImageUrl.IsValid() )
770 RemoveTexture( mImageUrl.GetUrl() );
773 mTextureLoading = false;
774 mImpl->mRenderer.Reset();
775 mPlacementActor.Reset();
778 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
781 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
783 bool sync = IsSynchronousResourceLoading();
784 map.Insert( SYNCHRONOUS_LOADING, sync );
785 if( mImageUrl.IsValid() )
787 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
788 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
789 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
793 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
794 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
796 ResourceImage resourceImage = ResourceImage::DownCast(mImage);
799 map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
803 map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
804 map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
806 map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
807 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
808 map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
810 map.Insert( Toolkit::DevelImageVisual::Property::ATLASING, mAttemptAtlasing );
813 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
816 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
817 if( mImageUrl.IsValid() )
819 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
820 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
824 map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
825 map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
829 void ImageVisual::OnSetTransform()
831 if( mImpl->mRenderer )
833 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
837 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
842 if( defaultTextureWrapping )
844 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
847 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
848 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
849 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
854 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
857 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
858 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
859 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
865 shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
868 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
869 shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
870 factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
877 void ImageVisual::ApplyImageToSampler( const Image& image )
881 TextureSet textureSet = mImpl->mRenderer.GetTextures();
882 DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
884 TextureSetImage( textureSet, 0u, image );
885 Sampler sampler = Sampler::New();
886 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
887 textureSet.SetSampler( 0u, sampler );
891 // From existing atlas manager
892 void ImageVisual::UploadCompleted()
894 // Texture has been uploaded. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
895 Actor actor = mPlacementActor.GetHandle();
898 actor.AddRenderer( mImpl->mRenderer );
900 // reset the weak handle so that the renderer only get added to actor once
901 mPlacementActor.Reset();
903 mTextureLoading = false;
906 // From Texture Manager
907 void ImageVisual::UploadComplete( bool loadingSuccess, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle )
909 Actor actor = mPlacementActor.GetHandle();
912 if( mImpl->mRenderer )
914 actor.AddRenderer( mImpl->mRenderer );
915 // reset the weak handle so that the renderer only get added to actor once
916 mPlacementActor.Reset();
920 Sampler sampler = Sampler::New();
921 sampler.SetWrapMode( mWrapModeU, mWrapModeV );
922 textureSet.SetSampler( 0u, sampler );
923 mImpl->mRenderer.SetTextures(textureSet);
927 Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
928 ApplyImageToSampler( brokenImage );
930 // Image loaded and ready to display
934 mTextureLoading = false;
937 void ImageVisual::RemoveTexture(const std::string& url)
939 if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
941 mFactoryCache.GetTextureManager().Remove( mTextureId );
942 mTextureId = TextureManager::INVALID_TEXTURE_ID;
946 Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
947 Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
948 if( index != Property::INVALID_INDEX )
950 Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
951 atlasRectValue.Get( atlasRect );
954 TextureSet textureSet = mImpl->mRenderer.GetTextures();
955 mImpl->mRenderer.Reset();
957 if( index != Property::INVALID_INDEX )
959 mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
964 } // namespace Internal
966 } // namespace Toolkit