Merge "Added text direction property" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/image/image-visual.h>
20
21 // EXTERNAL HEADERS
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>
32
33 // INTERNAL HEADERS
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>
45
46 namespace Dali
47 {
48
49 namespace Toolkit
50 {
51
52 namespace Internal
53 {
54
55 namespace
56 {
57
58 // property names
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");
66
67 // fitting modes
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 )
75
76 // sampling modes
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 )
86
87 // wrap modes
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 )
94
95 // load policies
96 DALI_ENUM_TO_STRING_TABLE_BEGIN( LOAD_POLICY )
97 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelImageVisual::LoadPolicy, IMMEDIATE )
98 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelImageVisual::LoadPolicy, ATTACHED )
99 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
100
101 // release policies
102 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
103 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelImageVisual::ReleasePolicy, DETACHED )
104 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelImageVisual::ReleasePolicy, DESTROYED )
105 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelImageVisual::ReleasePolicy, NEVER )
106 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
107
108 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
109
110 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
111
112 const float PIXEL_ALIGN_ON = 1.0f;
113 const float PIXEL_ALIGN_OFF = 0.0f;
114
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
124   \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
131 \n
132   vec4 ComputeVertexPosition()\n
133   {\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
137   }\n
138 \n
139   void main()\n
140   {\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
147   }\n
148 );
149
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
157   \n
158   lowp vec4 visualMixColor()\n
159   {\n
160     return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
161   }\n
162   void main()\n
163   {\n
164       gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
165   }\n
166 );
167
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
176     \n
177     lowp vec4 visualMixColor()\n
178     {\n
179         return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
180     }\n
181     \n
182     void main()\n
183     {\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
186      }\n
187 );
188
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
199     \n
200     mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )\n
201     {\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 );
208     }\n
209     \n
210     lowp vec4 visualMixColor()\n
211     {\n
212       return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
213     }\n
214     \n
215     void main()\n
216     {\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
220     }\n
221 );
222
223 Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridSize )
224 {
225   Geometry geometry;
226
227   if( gridSize == ImageDimensions( 1, 1 ) )
228   {
229     geometry = factoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
230   }
231   else
232   {
233     geometry = VisualFactoryCache::CreateGridGeometry( gridSize );
234   }
235
236   return geometry;
237 }
238
239 } // unnamed namespace
240
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 )
247 {
248   ImageVisualPtr imageVisualPtr( new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode ) );
249   imageVisualPtr->SetProperties( properties );
250   return imageVisualPtr;
251 }
252
253 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
254                                  const VisualUrl& imageUrl,
255                                  ImageDimensions size,
256                                  FittingMode::Type fittingMode,
257                                  Dali::SamplingMode::Type samplingMode )
258 {
259   return new ImageVisual( factoryCache, imageUrl, size, fittingMode, samplingMode );
260 }
261
262 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image& image )
263 {
264   return new ImageVisual( factoryCache, image );
265 }
266
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 ),
273   mImage(),
274   mPixelArea( FULL_TEXTURE_RECT ),
275   mPlacementActor(),
276   mImageUrl( imageUrl ),
277   mMaskingData( ),
278   mDesiredSize( size ),
279   mTextureId( TextureManager::INVALID_TEXTURE_ID ),
280   mTextures(),
281   mFittingMode( fittingMode ),
282   mSamplingMode( samplingMode ),
283   mWrapModeU( WrapMode::DEFAULT ),
284   mWrapModeV( WrapMode::DEFAULT ),
285   mLoadPolicy( DevelImageVisual::LoadPolicy::ATTACHED ),
286   mReleasePolicy( DevelImageVisual::ReleasePolicy::DETACHED ),
287   mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
288   mAttemptAtlasing( false ),
289   mLoading( false ),
290   mOrientationCorrection( true )
291 {
292 }
293
294 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
295 : Visual::Base( factoryCache ),
296   mImage( image ),
297   mPixelArea( FULL_TEXTURE_RECT ),
298   mPlacementActor(),
299   mImageUrl(),
300   mMaskingData( ),
301   mDesiredSize(),
302   mTextureId( TextureManager::INVALID_TEXTURE_ID ),
303   mTextures(),
304   mFittingMode( FittingMode::DEFAULT ),
305   mSamplingMode( SamplingMode::DEFAULT ),
306   mWrapModeU( WrapMode::DEFAULT ),
307   mWrapModeV( WrapMode::DEFAULT ),
308   mLoadPolicy( DevelImageVisual::LoadPolicy::ATTACHED ),
309   mReleasePolicy( DevelImageVisual::ReleasePolicy::DESTROYED ),
310   mAtlasRect( 0.0f, 0.0f, 0.0f, 0.0f ),
311   mAttemptAtlasing( false ),
312   mLoading( false ),
313   mOrientationCorrection( true )
314 {
315 }
316
317 ImageVisual::~ImageVisual()
318 {
319   if( Stage::IsInstalled() )
320   {
321     if( mMaskingData )
322     {
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 )
327       {
328         TextureManager& textureManager = mFactoryCache.GetTextureManager();
329         textureManager.Remove( mMaskingData->mAlphaMaskId );
330       }
331     }
332
333     // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
334     if( ( mTextureId != TextureManager::INVALID_TEXTURE_ID  ) && ( mReleasePolicy != DevelImageVisual::ReleasePolicy::NEVER ) )
335     {
336       RemoveTexture();
337     }
338   }
339 }
340
341 void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
342 {
343   // Url is already received in constructor
344   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
345   {
346     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
347     if( keyValue.first.type == Property::Key::INDEX )
348     {
349       DoSetProperty( keyValue.first.indexKey, keyValue.second );
350     }
351     else
352     {
353       if( keyValue.first == IMAGE_FITTING_MODE )
354       {
355         DoSetProperty( Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second );
356       }
357       else if( keyValue.first == IMAGE_SAMPLING_MODE )
358       {
359         DoSetProperty( Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second );
360       }
361       else if( keyValue.first == IMAGE_DESIRED_WIDTH )
362       {
363         DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second );
364       }
365       else if( keyValue.first == IMAGE_DESIRED_HEIGHT )
366       {
367         DoSetProperty( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second );
368       }
369       else if( keyValue.first == PIXEL_AREA_UNIFORM_NAME )
370       {
371         DoSetProperty( Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second );
372       }
373       else if( keyValue.first == IMAGE_WRAP_MODE_U )
374       {
375         DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second );
376       }
377       else if( keyValue.first == IMAGE_WRAP_MODE_V )
378       {
379         DoSetProperty( Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second );
380       }
381       else if( keyValue.first == SYNCHRONOUS_LOADING )
382       {
383         DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
384       }
385       else if( keyValue.first == IMAGE_ATLASING )
386       {
387         DoSetProperty( Toolkit::ImageVisual::Property::ATLASING, keyValue.second );
388       }
389       else if( keyValue.first == ALPHA_MASK_URL )
390       {
391         DoSetProperty( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second );
392       }
393       else if( keyValue.first == MASK_CONTENT_SCALE_NAME )
394       {
395         DoSetProperty( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second );
396       }
397       else if( keyValue.first == CROP_TO_MASK_NAME )
398       {
399         DoSetProperty( Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second );
400       }
401       else if ( keyValue.first == LOAD_POLICY_NAME )
402       {
403         DoSetProperty( Toolkit::DevelImageVisual::Property::LOAD_POLICY, keyValue.second );
404       }
405       else if( keyValue.first == RELEASE_POLICY_NAME )
406       {
407         DoSetProperty( Toolkit::DevelImageVisual::Property::RELEASE_POLICY, keyValue.second );
408       }
409       else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
410       {
411         DoSetProperty( Toolkit::DevelImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
412       }
413     }
414   }
415   // Load image immediately if LOAD_POLICY requires it
416   if ( mLoadPolicy == DevelImageVisual::LoadPolicy::IMMEDIATE )
417   {
418     auto attemptAtlasing = AttemptAtlasing();
419     LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
420                  TextureManager::ReloadPolicy::CACHED  );
421   }
422 }
423
424 void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& value )
425 {
426   switch( index )
427   {
428     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
429     {
430       bool sync;
431       if( value.Get( sync ) )
432       {
433         if( sync )
434         {
435           mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
436         }
437         else
438         {
439           mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
440         }
441       }
442       else
443       {
444         DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
445       }
446       break;
447     }
448
449     case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
450     {
451       float desiredWidth;
452       if( value.Get( desiredWidth ) )
453       {
454         mDesiredSize.SetWidth( desiredWidth );
455       }
456       else
457       {
458         DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
459       }
460       break;
461     }
462
463     case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
464     {
465       float desiredHeight;
466       if( value.Get( desiredHeight ) )
467       {
468         mDesiredSize.SetHeight( desiredHeight );
469       }
470       else
471       {
472         DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
473       }
474       break;
475     }
476
477     case Toolkit::ImageVisual::Property::FITTING_MODE:
478     {
479       int fittingMode;
480       Scripting::GetEnumerationProperty( value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode );
481       mFittingMode = Dali::FittingMode::Type( fittingMode );
482       break;
483     }
484
485     case Toolkit::ImageVisual::Property::SAMPLING_MODE:
486     {
487       int samplingMode;
488       Scripting::GetEnumerationProperty( value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode );
489       mSamplingMode = Dali::SamplingMode::Type( samplingMode );
490       break;
491     }
492
493     case Toolkit::ImageVisual::Property::PIXEL_AREA:
494     {
495       value.Get( mPixelArea );
496       break;
497     }
498
499     case Toolkit::ImageVisual::Property::WRAP_MODE_U:
500     {
501       int wrapMode;
502       Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
503       mWrapModeU = Dali::WrapMode::Type( wrapMode );
504       break;
505     }
506
507     case Toolkit::ImageVisual::Property::WRAP_MODE_V:
508     {
509       int wrapMode;
510       Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode );
511       mWrapModeV = Dali::WrapMode::Type( wrapMode );
512       break;
513     }
514
515     case Toolkit::ImageVisual::Property::ATLASING:
516     {
517       value.Get( mAttemptAtlasing );
518       break;
519     }
520
521     case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
522     {
523       std::string alphaUrl;
524       if( value.Get( alphaUrl ) )
525       {
526         AllocateMaskData();
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 );
531       }
532       break;
533     }
534
535     case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
536     {
537       float scale;
538       if( value.Get( scale ) )
539       {
540         AllocateMaskData();
541         mMaskingData->mContentScaleFactor = scale;
542       }
543       break;
544     }
545
546     case Toolkit::ImageVisual::Property::CROP_TO_MASK:
547     {
548       bool crop=false;
549       if( value.Get( crop ) )
550       {
551         AllocateMaskData();
552         mMaskingData->mCropToMask = crop;
553       }
554       break;
555     }
556
557     case Toolkit::DevelImageVisual::Property::RELEASE_POLICY:
558     {
559       int releasePolicy;
560       Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
561       mReleasePolicy = DevelImageVisual::ReleasePolicy::Type( releasePolicy );
562       break;
563     }
564
565     case Toolkit::DevelImageVisual::Property::LOAD_POLICY:
566     {
567       int loadPolicy;
568       Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
569       mLoadPolicy = DevelImageVisual::LoadPolicy::Type( loadPolicy );
570       break;
571     }
572     case Toolkit::DevelImageVisual::Property::ORIENTATION_CORRECTION:
573     {
574       bool orientationCorrection( mOrientationCorrection );
575       if( value.Get( orientationCorrection ) )
576       {
577         mOrientationCorrection = orientationCorrection;
578       }
579       break;
580     }
581   }
582 }
583
584 void ImageVisual::AllocateMaskData()
585 {
586   if( !mMaskingData )
587   {
588     mMaskingData.reset(new TextureManager::MaskingData());
589   }
590 }
591
592 void ImageVisual::GetNaturalSize( Vector2& naturalSize )
593 {
594   if(mImage)
595   {
596     naturalSize.x = mImage.GetWidth();
597     naturalSize.y = mImage.GetHeight();
598     return;
599   }
600   else if( mDesiredSize.GetWidth()>0 && mDesiredSize.GetHeight()>0)
601   {
602     naturalSize.x = mDesiredSize.GetWidth();
603     naturalSize.y = mDesiredSize.GetHeight();
604     return;
605   }
606   else if( mImpl->mRenderer ) // Check if we have a loaded image
607   {
608     auto textureSet = mImpl->mRenderer.GetTextures();
609
610     if( textureSet )
611     {
612       auto texture = textureSet.GetTexture(0);
613       naturalSize.x = texture.GetWidth();
614       naturalSize.y = texture.GetHeight();
615       return;
616     }
617   }
618
619   if( mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
620            mMaskingData->mCropToMask )
621   {
622     ImageDimensions dimensions = Dali::GetClosestImageSize( mMaskingData->mAlphaMaskUrl.GetUrl() );
623     if( dimensions != ImageDimensions( 0, 0 ) )
624     {
625       naturalSize.x = dimensions.GetWidth();
626       naturalSize.y = dimensions.GetHeight();
627     }
628     return;
629   }
630   else if( mImageUrl.IsValid() )
631   {
632     if( mImageUrl.GetProtocolType() == VisualUrl::LOCAL )
633     {
634       ImageDimensions dimensions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
635
636       if( dimensions != ImageDimensions( 0, 0 ) )
637       {
638         naturalSize.x = dimensions.GetWidth();
639         naturalSize.y = dimensions.GetHeight();
640       }
641       else
642       {
643         Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
644
645         naturalSize.x = brokenImage.GetWidth();
646         naturalSize.y = brokenImage.GetWidth();
647       }
648       return;
649     }
650   }
651
652   naturalSize = Vector2::ZERO;
653 }
654
655 void ImageVisual::CreateRenderer( TextureSet& textureSet )
656 {
657   Geometry geometry;
658   Shader shader;
659
660   if( !mImpl->mCustomShader )
661   {
662     geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
663
664     shader = GetImageShader( mFactoryCache,
665                              mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
666                              mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE );
667   }
668   else
669   {
670     geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
671     if( mImpl->mCustomShader->mVertexShader.empty() && mImpl->mCustomShader->mFragmentShader.empty() )
672     {
673       shader = GetImageShader( mFactoryCache, false, true );
674     }
675     else
676     {
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() )
681       {
682         shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
683       }
684     }
685   }
686
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 );
691
692   mImpl->mRenderer = Renderer::New( geometry, shader );
693   if( textureSet )
694   {
695     mImpl->mRenderer.SetTextures( textureSet );
696   }
697   // else still waiting for texture load to finish.
698
699   //Register transform properties
700   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
701 }
702
703 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
704 {
705   Geometry geometry;
706   Shader shader;
707
708   std::string fragmentShader;
709   const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
710   const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
711
712   if( fragmentPreFix )
713   {
714     fragmentShader = fragmentPreFix;
715     fragmentShader += "\n";
716   }
717
718   if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
719   {
720     fragmentShader += mImpl->mCustomShader->mFragmentShader;
721   }
722   else
723   {
724     fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
725   }
726
727   if( customSamplerTypename )
728   {
729     fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
730   }
731
732   if( !mImpl->mCustomShader )
733   {
734     geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
735
736     shader  = Shader::New( VERTEX_SHADER, fragmentShader );
737     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
738   }
739   else
740   {
741     geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
742     shader  = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
743                            fragmentShader,
744                            mImpl->mCustomShader->mHints );
745     if( mImpl->mCustomShader->mVertexShader.empty() )
746     {
747       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
748     }
749   }
750
751   mImpl->mRenderer = Renderer::New( geometry, shader );
752
753   //Register transform properties
754   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
755 }
756
757
758 bool ImageVisual::IsSynchronousResourceLoading() const
759 {
760   return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
761 }
762
763 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
764                                TextureManager::ReloadPolicy forceReload )
765 {
766   TextureManager& textureManager = mFactoryCache.GetTextureManager();
767
768   ImageAtlasManagerPtr atlasManager = nullptr;
769   AtlasUploadObserver* atlasUploadObserver = nullptr;
770   auto textureObserver = this;
771
772   if( atlasing )
773   {
774     atlasManager = mFactoryCache.GetAtlasManager();
775     atlasUploadObserver = this;
776   }
777
778   textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
779                                          mMaskingData, IsSynchronousResourceLoading(), mTextureId,
780                                          atlasRect, atlasing, mLoading, mWrapModeU,
781                                          mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
782                                          mOrientationCorrection,
783                                          forceReload );
784
785   if( atlasing ) // Flag needs to be set before creating renderer
786   {
787     mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
788   }
789   else
790   {
791     mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
792   }
793 }
794
795 bool ImageVisual::AttemptAtlasing()
796 {
797   return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
798 }
799
800 void ImageVisual::InitializeRenderer()
801 {
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
805
806   if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
807   {
808     LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
809                  TextureManager::ReloadPolicy::CACHED );
810   }
811
812   CreateRenderer( mTextures );
813   mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
814
815   if( attemptAtlasing ) // the texture is packed inside atlas
816   {
817     mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
818
819     bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
820
821     if( !defaultWrapMode ) // custom wrap mode
822     {
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 );
826     }
827   }
828 }
829
830 void ImageVisual::InitializeRenderer( const Image& image )
831 {
832   TextureSet textures = TextureSet::New();
833
834   NativeImage nativeImage = NativeImage::DownCast( image );
835   if( nativeImage )
836   {
837     CreateNativeImageRenderer( nativeImage );
838     DALI_ASSERT_DEBUG( textures );
839     mImpl->mRenderer.SetTextures( textures );
840   }
841   else
842   {
843     // reuse existing code for regular images
844     CreateRenderer( textures ); // Textures will be retreived from Image
845   }
846   ApplyImageToSampler( image );
847 }
848
849 void ImageVisual::DoSetOnStage( Actor& actor )
850 {
851   if( mImageUrl.IsValid() )
852   {
853     InitializeRenderer();
854   }
855   else if( mImage )
856   {
857     InitializeRenderer( mImage );
858   }
859
860   if( !mImpl->mRenderer )
861   {
862     return;
863   }
864
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 )
869   {
870      // Layer 3D set, do not align pixels
871      mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
872   }
873
874   if( mPixelArea != FULL_TEXTURE_RECT )
875   {
876     mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
877   }
878
879   if( mLoading == false )
880   {
881     actor.AddRenderer( mImpl->mRenderer );
882     mPlacementActor.Reset();
883
884     // Image loaded and ready to display
885     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
886   }
887 }
888
889 void ImageVisual::DoSetOffStage( Actor& actor )
890 {
891   // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
892
893   // Image release is dependent on the ReleasePolicy, renderer is destroyed.
894   actor.RemoveRenderer( mImpl->mRenderer);
895   if( mReleasePolicy == DevelImageVisual::ReleasePolicy::DETACHED )
896   {
897     RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
898   }
899
900   if( mImageUrl.IsValid() )
901   {
902     // Legacy support for deprecated Dali::Image
903     mImage.Reset();
904   }
905   mLoading = false;
906   mImpl->mRenderer.Reset();
907   mPlacementActor.Reset();
908 }
909
910 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
911 {
912   map.Clear();
913   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
914
915   bool sync = IsSynchronousResourceLoading();
916   map.Insert( SYNCHRONOUS_LOADING, sync );
917   if( mImageUrl.IsValid() )
918   {
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() );
922   }
923   else if( mImage )
924   {
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()) );
927
928     ResourceImage resourceImage = ResourceImage::DownCast(mImage);
929     if( resourceImage )
930     {
931       map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
932     }
933   }
934
935   map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
936   map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
937
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 );
941
942   map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
943
944   if( mMaskingData != NULL )
945   {
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 );
949   }
950
951   map.Insert( Toolkit::DevelImageVisual::Property::LOAD_POLICY, mLoadPolicy );
952   map.Insert( Toolkit::DevelImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
953   map.Insert( Toolkit::DevelImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
954 }
955
956 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
957 {
958   map.Clear();
959   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
960   if( mImageUrl.IsValid() )
961   {
962     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
963     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
964   }
965   else if( mImage )
966   {
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()) );
969   }
970 }
971
972 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
973 {
974   // Check if action is valid for this visual type and perform action if possible
975
976   switch ( actionName )
977   {
978     case DevelImageVisual::Action::RELOAD:
979     {
980       auto attemptAtlasing = AttemptAtlasing();
981       LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
982                    TextureManager::ReloadPolicy::FORCED );
983       break;
984     }
985   }
986 }
987
988 void ImageVisual::OnSetTransform()
989 {
990   if( mImpl->mRenderer )
991   {
992     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
993   }
994 }
995
996 bool ImageVisual::IsResourceReady() const
997 {
998   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
999            mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
1000 }
1001
1002 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
1003 {
1004   Shader shader;
1005   if( atlasing )
1006   {
1007     if( defaultTextureWrapping )
1008     {
1009       shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
1010       if( !shader )
1011       {
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 );
1015       }
1016     }
1017     else
1018     {
1019       shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
1020       if( !shader )
1021       {
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 );
1025       }
1026     }
1027   }
1028   else
1029   {
1030     shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
1031     if( !shader )
1032     {
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 );
1036     }
1037   }
1038
1039   return shader;
1040 }
1041
1042 void ImageVisual::ApplyImageToSampler( const Image& image )
1043 {
1044   if( image )
1045   {
1046     TextureSet textureSet = mImpl->mRenderer.GetTextures();
1047     DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
1048
1049     TextureSetImage( textureSet, 0u, image );
1050     Sampler sampler = Sampler::New();
1051     sampler.SetWrapMode(  mWrapModeU, mWrapModeV  );
1052     textureSet.SetSampler( 0u, sampler );
1053   }
1054 }
1055
1056 // From existing atlas manager
1057 void ImageVisual::UploadCompleted()
1058 {
1059   // Texture has been uploaded. If weak handle is holding a placement actor,
1060   // it is the time to add the renderer to actor.
1061   Actor actor = mPlacementActor.GetHandle();
1062   if( actor )
1063   {
1064     mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1065     actor.AddRenderer( mImpl->mRenderer );
1066     // reset the weak handle so that the renderer only get added to actor once
1067     mPlacementActor.Reset();
1068   }
1069   // Image loaded
1070   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
1071   mLoading = false;
1072 }
1073
1074 // From Texture Manager
1075 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
1076                                   const Vector4& atlasRectangle )
1077 {
1078   Toolkit::Visual::ResourceStatus resourceStatus;
1079   Actor actor = mPlacementActor.GetHandle();
1080   if( actor )
1081   {
1082     if( mImpl->mRenderer )
1083     {
1084       if( usingAtlas )
1085       {
1086         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1087       }
1088
1089       actor.AddRenderer( mImpl->mRenderer );
1090       // reset the weak handle so that the renderer only get added to actor once
1091       mPlacementActor.Reset();
1092       if( loadingSuccess )
1093       {
1094         Sampler sampler = Sampler::New();
1095         sampler.SetWrapMode(  mWrapModeU, mWrapModeV  );
1096         textureSet.SetSampler( 0u, sampler );
1097         mImpl->mRenderer.SetTextures(textureSet);
1098       }
1099       else
1100       {
1101         Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
1102
1103         textureSet = TextureSet::New();
1104         mImpl->mRenderer.SetTextures( textureSet );
1105
1106         ApplyImageToSampler( brokenImage );
1107       }
1108     }
1109   }
1110   // Storing TextureSet needed when renderer staged.
1111   if( ! mImpl->mRenderer )
1112   {
1113     mTextures = textureSet;
1114   }
1115
1116   // Image loaded, set status regardless of staged status.
1117   if( loadingSuccess )
1118   {
1119     resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1120   }
1121   else
1122   {
1123     resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1124   }
1125   // Signal to observers ( control ) that resources are ready. Must be all resources.
1126   ResourceReady( resourceStatus );
1127   mLoading = false;
1128 }
1129
1130 void ImageVisual::RemoveTexture()
1131 {
1132   if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1133   {
1134     mFactoryCache.GetTextureManager().Remove( mTextureId );
1135     mTextureId = TextureManager::INVALID_TEXTURE_ID;
1136   }
1137   else
1138   {
1139     Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1140     Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1141     if( index != Property::INVALID_INDEX )
1142     {
1143       Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1144       atlasRectValue.Get( atlasRect );
1145     }
1146
1147     TextureSet textureSet = mImpl->mRenderer.GetTextures();
1148     mImpl->mRenderer.Reset();
1149
1150     if( index != Property::INVALID_INDEX )
1151     {
1152       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1153     }
1154   }
1155 }
1156
1157 } // namespace Internal
1158
1159 } // namespace Toolkit
1160
1161 } // namespace Dali