Added automatic premultiplication of image visual images
[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( Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE )
98 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED )
99 DALI_ENUM_TO_STRING_TABLE_END( LOAD_POLICY )
100
101 // release policies
102 DALI_ENUM_TO_STRING_TABLE_BEGIN( RELEASE_POLICY )
103 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED )
104 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED )
105 DALI_ENUM_TO_STRING_WITH_SCOPE( Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER )
106 DALI_ENUM_TO_STRING_TABLE_END( RELEASE_POLICY )
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( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
286   mReleasePolicy( Toolkit::ImageVisual::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( Toolkit::ImageVisual::LoadPolicy::ATTACHED ),
309   mReleasePolicy( Toolkit::ImageVisual::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 != Toolkit::ImageVisual::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::ImageVisual::Property::LOAD_POLICY, keyValue.second );
404       }
405       else if( keyValue.first == RELEASE_POLICY_NAME )
406       {
407         DoSetProperty( Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second );
408       }
409       else if( keyValue.first == ORIENTATION_CORRECTION_NAME )
410       {
411         DoSetProperty( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second );
412       }
413     }
414   }
415   // Load image immediately if LOAD_POLICY requires it
416   if ( mLoadPolicy == Toolkit::ImageVisual::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::ImageVisual::Property::RELEASE_POLICY:
558     {
559       int releasePolicy;
560       Scripting::GetEnumerationProperty( value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy );
561       mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type( releasePolicy );
562       break;
563     }
564
565     case Toolkit::ImageVisual::Property::LOAD_POLICY:
566     {
567       int loadPolicy;
568       Scripting::GetEnumerationProperty( value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy );
569       mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type( loadPolicy );
570       break;
571     }
572     case Toolkit::ImageVisual::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   if( IsPreMultipliedAlphaEnabled() )
703   {
704     EnablePreMultipliedAlpha( true );
705   }
706 }
707
708 void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
709 {
710   Geometry geometry;
711   Shader shader;
712
713   std::string fragmentShader;
714   const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
715   const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
716
717   if( fragmentPreFix )
718   {
719     fragmentShader = fragmentPreFix;
720     fragmentShader += "\n";
721   }
722
723   if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
724   {
725     fragmentShader += mImpl->mCustomShader->mFragmentShader;
726   }
727   else
728   {
729     fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
730   }
731
732   if( customSamplerTypename )
733   {
734     fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
735   }
736
737   if( !mImpl->mCustomShader )
738   {
739     geometry = CreateGeometry( mFactoryCache, ImageDimensions( 1, 1 ) );
740
741     shader  = Shader::New( VERTEX_SHADER, fragmentShader );
742     shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
743   }
744   else
745   {
746     geometry = CreateGeometry( mFactoryCache, mImpl->mCustomShader->mGridSize );
747     shader  = Shader::New( mImpl->mCustomShader->mVertexShader.empty() ? VERTEX_SHADER : mImpl->mCustomShader->mVertexShader,
748                            fragmentShader,
749                            mImpl->mCustomShader->mHints );
750     if( mImpl->mCustomShader->mVertexShader.empty() )
751     {
752       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
753     }
754   }
755
756   mImpl->mRenderer = Renderer::New( geometry, shader );
757
758   //Register transform properties
759   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
760 }
761
762 bool ImageVisual::IsSynchronousResourceLoading() const
763 {
764   return mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
765 }
766
767 void ImageVisual::LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection,
768                                TextureManager::ReloadPolicy forceReload )
769 {
770   TextureManager& textureManager = mFactoryCache.GetTextureManager();
771
772   ImageAtlasManagerPtr atlasManager = nullptr;
773   AtlasUploadObserver* atlasUploadObserver = nullptr;
774   auto textureObserver = this;
775
776   if( atlasing )
777   {
778     atlasManager = mFactoryCache.GetAtlasManager();
779     atlasUploadObserver = this;
780   }
781
782   auto preMultiplyOnLoad = mFactoryCache.GetPreMultiplyOnLoad()
783     ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
784     : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
785
786   textures = textureManager.LoadTexture( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode,
787                                          mMaskingData, IsSynchronousResourceLoading(), mTextureId,
788                                          atlasRect, atlasing, mLoading, mWrapModeU,
789                                          mWrapModeV, textureObserver, atlasUploadObserver, atlasManager,
790                                          mOrientationCorrection, forceReload, preMultiplyOnLoad);
791
792   if( textures && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
793   {
794     EnablePreMultipliedAlpha( true );
795   }
796
797   if( atlasing ) // Flag needs to be set before creating renderer
798   {
799     mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
800   }
801   else
802   {
803     mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
804   }
805 }
806
807 bool ImageVisual::AttemptAtlasing()
808 {
809   return ( ! mImpl->mCustomShader && mImageUrl.GetProtocolType() == VisualUrl::LOCAL && mAttemptAtlasing );
810 }
811
812 void ImageVisual::InitializeRenderer()
813 {
814   auto attemptAtlasing = AttemptAtlasing();
815   // texture set has to be created first as we need to know if atlasing succeeded or not
816   // when selecting the shader
817
818   if( mTextureId == TextureManager::INVALID_TEXTURE_ID && ! mTextures ) // Only load the texture once
819   {
820     LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
821                  TextureManager::ReloadPolicy::CACHED );
822   }
823
824   CreateRenderer( mTextures );
825   mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
826
827   if( attemptAtlasing ) // the texture is packed inside atlas
828   {
829     mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
830
831     bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
832
833     if( !defaultWrapMode ) // custom wrap mode
834     {
835       Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
836       wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
837       mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
838     }
839   }
840 }
841
842 void ImageVisual::InitializeRenderer( const Image& image )
843 {
844   TextureSet textures = TextureSet::New();
845
846   NativeImage nativeImage = NativeImage::DownCast( image );
847   if( nativeImage )
848   {
849     CreateNativeImageRenderer( nativeImage );
850     DALI_ASSERT_DEBUG( textures );
851     mImpl->mRenderer.SetTextures( textures );
852   }
853   else
854   {
855     // reuse existing code for regular images
856     CreateRenderer( textures ); // Textures will be retreived from Image
857   }
858   ApplyImageToSampler( image );
859 }
860
861 void ImageVisual::DoSetOnStage( Actor& actor )
862 {
863   if( mImageUrl.IsValid() )
864   {
865     InitializeRenderer();
866   }
867   else if( mImage )
868   {
869     InitializeRenderer( mImage );
870   }
871
872   if( !mImpl->mRenderer )
873   {
874     return;
875   }
876
877   mPlacementActor = actor;
878   // Search the Actor tree to find if Layer UI behaviour set.
879   Layer layer = actor.GetLayer();
880   if( layer && layer.GetBehavior() == Layer::LAYER_3D )
881   {
882      // Layer 3D set, do not align pixels
883      mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
884   }
885
886   if( mPixelArea != FULL_TEXTURE_RECT )
887   {
888     mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
889   }
890
891   if( mLoading == false )
892   {
893     actor.AddRenderer( mImpl->mRenderer );
894     mPlacementActor.Reset();
895
896     // Image loaded and ready to display
897     ResourceReady( Toolkit::Visual::ResourceStatus::READY );
898   }
899 }
900
901 void ImageVisual::DoSetOffStage( Actor& actor )
902 {
903   // Visual::Base::SetOffStage only calls DoSetOffStage if mRenderer exists (is on onstage)
904
905   // Image release is dependent on the ReleasePolicy, renderer is destroyed.
906   actor.RemoveRenderer( mImpl->mRenderer);
907   if( mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED )
908   {
909     RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
910   }
911
912   if( mImageUrl.IsValid() )
913   {
914     // Legacy support for deprecated Dali::Image
915     mImage.Reset();
916   }
917   mLoading = false;
918   mImpl->mRenderer.Reset();
919   mPlacementActor.Reset();
920 }
921
922 void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
923 {
924   map.Clear();
925   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
926
927   bool sync = IsSynchronousResourceLoading();
928   map.Insert( SYNCHRONOUS_LOADING, sync );
929   if( mImageUrl.IsValid() )
930   {
931     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
932     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
933     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
934   }
935   else if( mImage )
936   {
937     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
938     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
939
940     ResourceImage resourceImage = ResourceImage::DownCast(mImage);
941     if( resourceImage )
942     {
943       map.Insert( Toolkit::ImageVisual::Property::URL, resourceImage.GetUrl() );
944     }
945   }
946
947   map.Insert( Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode );
948   map.Insert( Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode );
949
950   map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
951   map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
952   map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
953
954   map.Insert( Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing );
955
956   if( mMaskingData != NULL )
957   {
958     map.Insert( Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl() );
959     map.Insert( Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor );
960     map.Insert( Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask );
961   }
962
963   map.Insert( Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy );
964   map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
965   map.Insert( Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection );
966 }
967
968 void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
969 {
970   map.Clear();
971   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
972   if( mImageUrl.IsValid() )
973   {
974     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
975     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
976   }
977   else if( mImage )
978   {
979     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
980     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
981   }
982 }
983
984 void ImageVisual::OnDoAction( const Dali::Property::Index actionName, const Dali::Property::Value& attributes )
985 {
986   // Check if action is valid for this visual type and perform action if possible
987
988   switch ( actionName )
989   {
990     case DevelImageVisual::Action::RELOAD:
991     {
992       auto attemptAtlasing = AttemptAtlasing();
993       LoadTexture( attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection,
994                    TextureManager::ReloadPolicy::FORCED );
995       break;
996     }
997   }
998 }
999
1000 void ImageVisual::OnSetTransform()
1001 {
1002   if( mImpl->mRenderer )
1003   {
1004     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
1005   }
1006 }
1007
1008 bool ImageVisual::IsResourceReady() const
1009 {
1010   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
1011            mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED );
1012 }
1013
1014 Shader ImageVisual::GetImageShader( VisualFactoryCache& factoryCache, bool atlasing, bool defaultTextureWrapping )
1015 {
1016   Shader shader;
1017   if( atlasing )
1018   {
1019     if( defaultTextureWrapping )
1020     {
1021       shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP );
1022       if( !shader )
1023       {
1024         shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_CLAMP );
1025         shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1026         factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP, shader );
1027       }
1028     }
1029     else
1030     {
1031       shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP );
1032       if( !shader )
1033       {
1034         shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ATLAS_VARIOUS_WRAP );
1035         shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1036         factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP, shader );
1037       }
1038     }
1039   }
1040   else
1041   {
1042     shader = factoryCache.GetShader( VisualFactoryCache::IMAGE_SHADER );
1043     if( !shader )
1044     {
1045       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_NO_ATLAS );
1046       shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
1047       factoryCache.SaveShader( VisualFactoryCache::IMAGE_SHADER, shader );
1048     }
1049   }
1050
1051   return shader;
1052 }
1053
1054 void ImageVisual::ApplyImageToSampler( const Image& image )
1055 {
1056   if( image )
1057   {
1058     TextureSet textureSet = mImpl->mRenderer.GetTextures();
1059     DALI_ASSERT_DEBUG( textureSet ); // texture set should always exist by this time
1060
1061     TextureSetImage( textureSet, 0u, image );
1062     Sampler sampler = Sampler::New();
1063     sampler.SetWrapMode(  mWrapModeU, mWrapModeV  );
1064     textureSet.SetSampler( 0u, sampler );
1065   }
1066 }
1067
1068 // From existing atlas manager
1069 void ImageVisual::UploadCompleted()
1070 {
1071   // Texture has been uploaded. If weak handle is holding a placement actor,
1072   // it is the time to add the renderer to actor.
1073   Actor actor = mPlacementActor.GetHandle();
1074   if( actor )
1075   {
1076     mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1077     actor.AddRenderer( mImpl->mRenderer );
1078     // reset the weak handle so that the renderer only get added to actor once
1079     mPlacementActor.Reset();
1080   }
1081   // Image loaded
1082   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
1083   mLoading = false;
1084 }
1085
1086 // From Texture Manager
1087 void ImageVisual::UploadComplete( bool loadingSuccess, int32_t textureId, TextureSet textureSet, bool usingAtlas,
1088                                   const Vector4& atlasRectangle, bool preMultiplied )
1089 {
1090   Toolkit::Visual::ResourceStatus resourceStatus;
1091   Actor actor = mPlacementActor.GetHandle();
1092   if( actor )
1093   {
1094     if( mImpl->mRenderer )
1095     {
1096       if( usingAtlas )
1097       {
1098         mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, mAtlasRect );
1099       }
1100       else if( preMultiplied )
1101       {
1102         EnablePreMultipliedAlpha( true );
1103       }
1104
1105       actor.AddRenderer( mImpl->mRenderer );
1106       // reset the weak handle so that the renderer only get added to actor once
1107       mPlacementActor.Reset();
1108       if( loadingSuccess )
1109       {
1110         Sampler sampler = Sampler::New();
1111         sampler.SetWrapMode(  mWrapModeU, mWrapModeV  );
1112         textureSet.SetSampler( 0u, sampler );
1113         mImpl->mRenderer.SetTextures(textureSet);
1114       }
1115       else
1116       {
1117         Image brokenImage = VisualFactoryCache::GetBrokenVisualImage();
1118
1119         textureSet = TextureSet::New();
1120         mImpl->mRenderer.SetTextures( textureSet );
1121
1122         ApplyImageToSampler( brokenImage );
1123       }
1124     }
1125   }
1126   // Storing TextureSet needed when renderer staged.
1127   if( ! mImpl->mRenderer )
1128   {
1129     mTextures = textureSet;
1130   }
1131
1132   // Image loaded, set status regardless of staged status.
1133   if( loadingSuccess )
1134   {
1135     resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1136   }
1137   else
1138   {
1139     resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1140   }
1141   // Signal to observers ( control ) that resources are ready. Must be all resources.
1142   ResourceReady( resourceStatus );
1143   mLoading = false;
1144 }
1145
1146 void ImageVisual::RemoveTexture()
1147 {
1148   if( mTextureId != TextureManager::INVALID_TEXTURE_ID )
1149   {
1150     mFactoryCache.GetTextureManager().Remove( mTextureId );
1151     mTextureId = TextureManager::INVALID_TEXTURE_ID;
1152   }
1153   else
1154   {
1155     Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
1156     Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
1157     if( index != Property::INVALID_INDEX )
1158     {
1159       Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
1160       atlasRectValue.Get( atlasRect );
1161     }
1162
1163     TextureSet textureSet = mImpl->mRenderer.GetTextures();
1164     mImpl->mRenderer.Reset();
1165
1166     if( index != Property::INVALID_INDEX )
1167     {
1168       mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
1169     }
1170   }
1171 }
1172
1173 } // namespace Internal
1174
1175 } // namespace Toolkit
1176
1177 } // namespace Dali