44c9fac851bc3d770aa871ee104bc344eb176d6e
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.cpp
1 /*
2  * Copyright (c) 2022 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 <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/devel-api/rendering/texture-devel.h>
26 #include <dali/devel-api/scripting/enum-helper.h>
27 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <cstring> // for strlen()
31
32 // INTERNAL HEADERS
33 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
34 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
37 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
42 #include <dali-toolkit/internal/visuals/visual-url.h>
43 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
44 #include <dali-toolkit/public-api/visuals/visual-properties.h>
45
46 namespace Dali
47 {
48 namespace Toolkit
49 {
50 namespace Internal
51 {
52 namespace
53 {
54 const int CUSTOM_PROPERTY_COUNT(12); // ltr, wrap, pixel area, atlas, pixalign, crop to mask, mask texture ratio + border/corner
55
56 // fitting modes
57 DALI_ENUM_TO_STRING_TABLE_BEGIN(FITTING_MODE)
58   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SHRINK_TO_FIT)
59   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SCALE_TO_FILL)
60   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_WIDTH)
61   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_HEIGHT)
62   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, DEFAULT)
63 DALI_ENUM_TO_STRING_TABLE_END(FITTING_MODE)
64
65 // sampling modes
66 DALI_ENUM_TO_STRING_TABLE_BEGIN(SAMPLING_MODE)
67   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX)
68   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NEAREST)
69   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, LINEAR)
70   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_NEAREST)
71   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_LINEAR)
72   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NO_FILTER)
73   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, DONT_CARE)
74 DALI_ENUM_TO_STRING_TABLE_END(SAMPLING_MODE)
75
76 // wrap modes
77 DALI_ENUM_TO_STRING_TABLE_BEGIN(WRAP_MODE)
78   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, DEFAULT)
79   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, CLAMP_TO_EDGE)
80   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, REPEAT)
81   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, MIRRORED_REPEAT)
82 DALI_ENUM_TO_STRING_TABLE_END(WRAP_MODE)
83
84 // load policies
85 DALI_ENUM_TO_STRING_TABLE_BEGIN(LOAD_POLICY)
86   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE)
87   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED)
88 DALI_ENUM_TO_STRING_TABLE_END(LOAD_POLICY)
89
90 // release policies
91 DALI_ENUM_TO_STRING_TABLE_BEGIN(RELEASE_POLICY)
92   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED)
93   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED)
94   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER)
95 DALI_ENUM_TO_STRING_TABLE_END(RELEASE_POLICY)
96
97 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
98
99 const float PIXEL_ALIGN_ON  = 1.0f;
100 const float PIXEL_ALIGN_OFF = 0.0f;
101
102 constexpr uint32_t TEXTURE_COUNT_FOR_GPU_ALPHA_MASK = 2u;
103
104 Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
105 {
106   Geometry geometry;
107
108   if(gridSize == ImageDimensions(1, 1))
109   {
110     geometry = factoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
111   }
112   else
113   {
114     geometry = VisualFactoryCache::CreateGridGeometry(gridSize);
115   }
116
117   return geometry;
118 }
119
120 } // unnamed namespace
121
122 ImageVisualPtr ImageVisual::New(VisualFactoryCache&       factoryCache,
123                                 ImageVisualShaderFactory& shaderFactory,
124                                 const VisualUrl&          imageUrl,
125                                 const Property::Map&      properties,
126                                 ImageDimensions           size,
127                                 FittingMode::Type         fittingMode,
128                                 Dali::SamplingMode::Type  samplingMode)
129 {
130   ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
131   imageVisualPtr->SetProperties(properties);
132   imageVisualPtr->Initialize();
133   return imageVisualPtr;
134 }
135
136 ImageVisualPtr ImageVisual::New(VisualFactoryCache&       factoryCache,
137                                 ImageVisualShaderFactory& shaderFactory,
138                                 const VisualUrl&          imageUrl,
139                                 ImageDimensions           size,
140                                 FittingMode::Type         fittingMode,
141                                 Dali::SamplingMode::Type  samplingMode)
142 {
143   ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
144   imageVisualPtr->Initialize();
145   return imageVisualPtr;
146 }
147
148 ImageVisual::ImageVisual(VisualFactoryCache&       factoryCache,
149                          ImageVisualShaderFactory& shaderFactory,
150                          const VisualUrl&          imageUrl,
151                          ImageDimensions           size,
152                          FittingMode::Type         fittingMode,
153                          Dali::SamplingMode::Type  samplingMode)
154 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::IMAGE),
155   mPixelArea(FULL_TEXTURE_RECT),
156   mPlacementActor(),
157   mImageUrl(imageUrl),
158   mMaskingData(),
159   mDesiredSize(size),
160   mTextureId(TextureManager::INVALID_TEXTURE_ID),
161   mTextures(),
162   mImageVisualShaderFactory(shaderFactory),
163   mFittingMode(fittingMode),
164   mSamplingMode(samplingMode),
165   mWrapModeU(WrapMode::DEFAULT),
166   mWrapModeV(WrapMode::DEFAULT),
167   mLoadPolicy(Toolkit::ImageVisual::LoadPolicy::ATTACHED),
168   mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED),
169   mAtlasRect(0.0f, 0.0f, 0.0f, 0.0f),
170   mAtlasRectSize(0, 0),
171   mLoadState(TextureManager::LoadState::NOT_STARTED),
172   mAttemptAtlasing(false),
173   mOrientationCorrection(true)
174 {
175   EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
176 }
177
178 ImageVisual::~ImageVisual()
179 {
180   if(Stage::IsInstalled())
181   {
182     if(mMaskingData)
183     {
184       // TextureManager could have been deleted before the actor that contains this
185       // ImageVisual is destroyed (e.g. due to stage shutdown). Ensure the stage
186       // is still valid before accessing texture manager.
187       if(mMaskingData->mAlphaMaskId != TextureManager::INVALID_TEXTURE_ID)
188       {
189         TextureManager& textureManager = mFactoryCache.GetTextureManager();
190         textureManager.Remove(mMaskingData->mAlphaMaskId, this);
191       }
192     }
193
194     if(mImageUrl.IsValid())
195     {
196       // Decrease reference count of External Resources :
197       // EncodedImageBuffer or ExternalTextures.
198       // Ensure the stage is still valid before accessing texture manager.
199       if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
200       {
201         TextureManager& textureManager = mFactoryCache.GetTextureManager();
202         textureManager.RemoveExternalTexture(mImageUrl.GetUrl());
203       }
204       else if(mImageUrl.IsBufferResource())
205       {
206         TextureManager& textureManager = mFactoryCache.GetTextureManager();
207         textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
208       }
209     }
210
211     // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
212     if((mTextureId != TextureManager::INVALID_TEXTURE_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
213     {
214       RemoveTexture();
215     }
216   }
217 }
218
219 void ImageVisual::DoSetProperties(const Property::Map& propertyMap)
220 {
221   // Url is already received in constructor
222   for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
223   {
224     KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
225     if(keyValue.first.type == Property::Key::INDEX)
226     {
227       DoSetProperty(keyValue.first.indexKey, keyValue.second);
228     }
229     else
230     {
231       if(keyValue.first == IMAGE_FITTING_MODE)
232       {
233         DoSetProperty(Toolkit::ImageVisual::Property::FITTING_MODE, keyValue.second);
234       }
235       else if(keyValue.first == IMAGE_SAMPLING_MODE)
236       {
237         DoSetProperty(Toolkit::ImageVisual::Property::SAMPLING_MODE, keyValue.second);
238       }
239       else if(keyValue.first == IMAGE_DESIRED_WIDTH)
240       {
241         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_WIDTH, keyValue.second);
242       }
243       else if(keyValue.first == IMAGE_DESIRED_HEIGHT)
244       {
245         DoSetProperty(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, keyValue.second);
246       }
247       else if(keyValue.first == PIXEL_AREA_UNIFORM_NAME)
248       {
249         DoSetProperty(Toolkit::ImageVisual::Property::PIXEL_AREA, keyValue.second);
250       }
251       else if(keyValue.first == IMAGE_WRAP_MODE_U)
252       {
253         DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_U, keyValue.second);
254       }
255       else if(keyValue.first == IMAGE_WRAP_MODE_V)
256       {
257         DoSetProperty(Toolkit::ImageVisual::Property::WRAP_MODE_V, keyValue.second);
258       }
259       else if(keyValue.first == SYNCHRONOUS_LOADING)
260       {
261         DoSetProperty(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second);
262       }
263       else if(keyValue.first == IMAGE_ATLASING)
264       {
265         DoSetProperty(Toolkit::ImageVisual::Property::ATLASING, keyValue.second);
266       }
267       else if(keyValue.first == ALPHA_MASK_URL)
268       {
269         DoSetProperty(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, keyValue.second);
270       }
271       else if(keyValue.first == MASK_CONTENT_SCALE_NAME)
272       {
273         DoSetProperty(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, keyValue.second);
274       }
275       else if(keyValue.first == CROP_TO_MASK_NAME)
276       {
277         DoSetProperty(Toolkit::ImageVisual::Property::CROP_TO_MASK, keyValue.second);
278       }
279       else if(keyValue.first == MASKING_TYPE_NAME)
280       {
281         DoSetProperty(Toolkit::DevelImageVisual::Property::MASKING_TYPE, keyValue.second);
282       }
283       else if(keyValue.first == LOAD_POLICY_NAME)
284       {
285         DoSetProperty(Toolkit::ImageVisual::Property::LOAD_POLICY, keyValue.second);
286       }
287       else if(keyValue.first == RELEASE_POLICY_NAME)
288       {
289         DoSetProperty(Toolkit::ImageVisual::Property::RELEASE_POLICY, keyValue.second);
290       }
291       else if(keyValue.first == ORIENTATION_CORRECTION_NAME)
292       {
293         DoSetProperty(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, keyValue.second);
294       }
295     }
296   }
297   // Load image immediately if LOAD_POLICY requires it
298   if(mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE)
299   {
300     auto attemptAtlasing = AttemptAtlasing();
301     LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
302   }
303 }
304
305 void ImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
306 {
307   switch(index)
308   {
309     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
310     {
311       bool sync = false;
312       if(value.Get(sync))
313       {
314         if(sync)
315         {
316           mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
317         }
318         else
319         {
320           mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
321         }
322       }
323       else
324       {
325         DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
326       }
327       break;
328     }
329
330     case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
331     {
332       float desiredWidth = 0.0f;
333       if(value.Get(desiredWidth))
334       {
335         mDesiredSize.SetWidth(desiredWidth);
336       }
337       else
338       {
339         DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
340       }
341       break;
342     }
343
344     case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
345     {
346       float desiredHeight = 0.0f;
347       if(value.Get(desiredHeight))
348       {
349         mDesiredSize.SetHeight(desiredHeight);
350       }
351       else
352       {
353         DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
354       }
355       break;
356     }
357
358     case Toolkit::ImageVisual::Property::FITTING_MODE:
359     {
360       int fittingMode = 0;
361       Scripting::GetEnumerationProperty(value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode);
362       mFittingMode = Dali::FittingMode::Type(fittingMode);
363       break;
364     }
365
366     case Toolkit::ImageVisual::Property::SAMPLING_MODE:
367     {
368       int samplingMode = 0;
369       Scripting::GetEnumerationProperty(value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode);
370       mSamplingMode = Dali::SamplingMode::Type(samplingMode);
371       break;
372     }
373
374     case Toolkit::ImageVisual::Property::PIXEL_AREA:
375     {
376       value.Get(mPixelArea);
377       break;
378     }
379
380     case Toolkit::ImageVisual::Property::WRAP_MODE_U:
381     {
382       int wrapMode = 0;
383       Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
384       mWrapModeU = Dali::WrapMode::Type(wrapMode);
385       break;
386     }
387
388     case Toolkit::ImageVisual::Property::WRAP_MODE_V:
389     {
390       int wrapMode = 0;
391       Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
392       mWrapModeV = Dali::WrapMode::Type(wrapMode);
393       break;
394     }
395
396     case Toolkit::ImageVisual::Property::ATLASING:
397     {
398       value.Get(mAttemptAtlasing);
399       break;
400     }
401
402     case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
403     {
404       std::string alphaUrl = "";
405       if(value.Get(alphaUrl))
406       {
407         AllocateMaskData();
408         mMaskingData->mAlphaMaskUrl = alphaUrl;
409       }
410       break;
411     }
412
413     case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
414     {
415       float scale = 1.0f;
416       if(value.Get(scale))
417       {
418         AllocateMaskData();
419         mMaskingData->mContentScaleFactor = scale;
420       }
421       break;
422     }
423
424     case Toolkit::ImageVisual::Property::CROP_TO_MASK:
425     {
426       bool crop = false;
427       if(value.Get(crop))
428       {
429         AllocateMaskData();
430         mMaskingData->mCropToMask = crop;
431       }
432       break;
433     }
434
435     case Toolkit::DevelImageVisual::Property::MASKING_TYPE:
436     {
437       int maskingType = 0;
438       if(value.Get(maskingType))
439       {
440         AllocateMaskData();
441         mMaskingData->mPreappliedMasking = Toolkit::DevelImageVisual::MaskingType::Type(maskingType) == Toolkit::DevelImageVisual::MaskingType::MASKING_ON_LOADING ? true : false;
442       }
443       break;
444     }
445
446     case Toolkit::ImageVisual::Property::RELEASE_POLICY:
447     {
448       int releasePolicy = 0;
449       Scripting::GetEnumerationProperty(value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy);
450       mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type(releasePolicy);
451       break;
452     }
453
454     case Toolkit::ImageVisual::Property::LOAD_POLICY:
455     {
456       int loadPolicy = 0;
457       Scripting::GetEnumerationProperty(value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy);
458       mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type(loadPolicy);
459       break;
460     }
461     case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
462     {
463       bool orientationCorrection(mOrientationCorrection);
464       if(value.Get(orientationCorrection))
465       {
466         mOrientationCorrection = orientationCorrection;
467       }
468       break;
469     }
470   }
471 }
472
473 void ImageVisual::AllocateMaskData()
474 {
475   if(!mMaskingData)
476   {
477     mMaskingData.reset(new TextureManager::MaskingData());
478   }
479 }
480
481 void ImageVisual::GetNaturalSize(Vector2& naturalSize)
482 {
483   if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
484   {
485     naturalSize.x = mDesiredSize.GetWidth();
486     naturalSize.y = mDesiredSize.GetHeight();
487     return;
488   }
489   else if(mImpl->mRenderer) // Check if we have a loaded image
490   {
491     if(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED)
492     {
493       naturalSize.x = mAtlasRectSize.GetWidth();
494       naturalSize.y = mAtlasRectSize.GetHeight();
495       return;
496     }
497
498     auto textureSet = mImpl->mRenderer.GetTextures();
499     if(textureSet && textureSet.GetTextureCount())
500     {
501       if(mTextureSize != Vector2::ZERO)
502       {
503         naturalSize = mTextureSize;
504         return;
505       }
506     }
507   }
508
509   if(mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
510      mMaskingData->mCropToMask)
511   {
512     ImageDimensions dimensions = Dali::GetClosestImageSize(mMaskingData->mAlphaMaskUrl.GetUrl());
513     if(dimensions != ImageDimensions(0, 0))
514     {
515       naturalSize.x = dimensions.GetWidth();
516       naturalSize.y = dimensions.GetHeight();
517     }
518     return;
519   }
520   else if(mImageUrl.IsValid())
521   {
522     if(mImageUrl.GetProtocolType() == VisualUrl::LOCAL)
523     {
524       ImageDimensions dimensions = Dali::GetClosestImageSize(mImageUrl.GetUrl());
525
526       if(dimensions != ImageDimensions(0, 0))
527       {
528         naturalSize.x = dimensions.GetWidth();
529         naturalSize.y = dimensions.GetHeight();
530       }
531       else
532       {
533         Actor   actor     = mPlacementActor.GetHandle();
534         Vector2 imageSize = Vector2::ZERO;
535         if(actor)
536         {
537           imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
538         }
539         mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
540         Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
541         naturalSize.x       = brokenImage.GetWidth();
542         naturalSize.y       = brokenImage.GetWidth();
543       }
544       return;
545     }
546   }
547   naturalSize = Vector2::ZERO;
548 }
549
550 void ImageVisual::OnInitialize()
551 {
552   Geometry geometry;
553
554   // Get the geometry
555   if(mImpl->mCustomShader)
556   {
557     geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
558   }
559   else // Get any geometry associated with the texture
560   {
561     TextureManager& textureManager = mFactoryCache.GetTextureManager();
562
563     uint32_t firstElementCount{0u};
564     uint32_t secondElementCount{0u};
565     geometry = textureManager.GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
566
567     if(!firstElementCount && !secondElementCount) // Otherwise use quad
568     {
569       geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
570     }
571   }
572
573   // Increase reference count of External Resources :
574   // EncodedImageBuffer or ExternalTextures.
575   // Reference count will be decreased at destructor of the visual.
576   if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE))
577   {
578     TextureManager& textureManager = mFactoryCache.GetTextureManager();
579     textureManager.UseExternalResource(mImageUrl.GetUrl());
580   }
581
582   Shader shader = GenerateShader();
583
584   // Create the renderer
585   mImpl->mRenderer = VisualRenderer::New(geometry, shader);
586   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
587
588   //Register transform properties
589   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
590
591   EnablePreMultipliedAlpha(IsPreMultipliedAlphaEnabled());
592
593   if(mMaskingData)
594   {
595     mImpl->mRenderer.RegisterProperty(CROP_TO_MASK_NAME, static_cast<float>(mMaskingData->mCropToMask));
596   }
597 }
598
599 void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload)
600 {
601   TextureManager& textureManager = mFactoryCache.GetTextureManager();
602
603   ImageAtlasManagerPtr atlasManager        = nullptr;
604   AtlasUploadObserver* atlasUploadObserver = nullptr;
605   auto                 textureObserver     = this;
606
607   if(atlasing)
608   {
609     atlasManager        = mFactoryCache.GetAtlasManager();
610     atlasUploadObserver = this;
611   }
612
613   auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
614                              ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
615                              : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
616
617   bool synchronousLoading = IsSynchronousLoadingRequired();
618   bool loadingStatus;
619
620   textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, mWrapModeU, mWrapModeV, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
621
622   if(textures)
623   {
624     if(loadingStatus)
625     {
626       mLoadState = TextureManager::LoadState::LOADING;
627     }
628     else
629     {
630       mLoadState = TextureManager::LoadState::LOAD_FINISHED;
631     }
632
633     EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
634   }
635   else if(synchronousLoading)
636   {
637     // Synchronous loading is failed
638     mLoadState = TextureManager::LoadState::LOAD_FAILED;
639   }
640
641   if(atlasing) // Flag needs to be set before creating renderer
642   {
643     mImpl->mFlags |= Visual::Base::Impl::IS_ATLASING_APPLIED;
644   }
645   else
646   {
647     mImpl->mFlags &= ~Visual::Base::Impl::IS_ATLASING_APPLIED;
648   }
649 }
650
651 bool ImageVisual::AttemptAtlasing()
652 {
653   return (!mImpl->mCustomShader && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()) && mAttemptAtlasing);
654 }
655
656 void ImageVisual::InitializeRenderer()
657 {
658   auto attemptAtlasing = AttemptAtlasing();
659
660   // Load Texture if mTextures is empty.
661   // mTextures is already set, the mTexture can be used to create Renderer.
662   // There are two cases mTextures is empty.
663   // 1. mTextureId == TextureManager::INVALID_TEXTURE_ID
664   //  - Visual is on stage with LoadPolicy::ATTACHED
665   // 2. mTextureId != TextureManager::INVALID_TEXTURE_ID
666   //  - If ReleasePolicy is DESTROYED, InitializeRenderer called every on stage called.
667   //  - Then every resources those contained in Visual are Reset but mTextureId is remained when the Off stage time,
668   //  - So, mTextures needed to be get from texture manager to created resources like mImpl->mRenderer.
669   if(!mTextures)
670   {
671     if(mTextureId == TextureManager::INVALID_TEXTURE_ID)
672     {
673       LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
674     }
675     else
676     {
677       mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
678     }
679   }
680
681   if(mTextures)
682   {
683     mImpl->mRenderer.SetTextures(mTextures);
684     ComputeTextureSize();
685     CheckMaskTexture();
686     if(DevelTexture::IsNative(mTextures.GetTexture(0)))
687     {
688       UpdateShader();
689     }
690     mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
691   }
692
693   if(attemptAtlasing) // the texture is packed inside atlas
694   {
695     mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
696
697     bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
698
699     if(!defaultWrapMode) // custom wrap mode
700     {
701       Vector2 wrapMode(mWrapModeU - WrapMode::CLAMP_TO_EDGE, mWrapModeV - WrapMode::CLAMP_TO_EDGE);
702       wrapMode.Clamp(Vector2::ZERO, Vector2(2.f, 2.f));
703       mImpl->mRenderer.RegisterProperty(WRAP_MODE_UNIFORM_NAME, wrapMode);
704     }
705   }
706 }
707
708 void ImageVisual::DoSetOnScene(Actor& actor)
709 {
710   if(mImageUrl.IsValid())
711   {
712     InitializeRenderer();
713   }
714
715   if(!mImpl->mRenderer)
716   {
717     return;
718   }
719
720   mPlacementActor = actor;
721   // Search the Actor tree to find if Layer UI behaviour set.
722   Layer layer = actor.GetLayer();
723   if(layer && layer.GetProperty<Layer::Behavior>(Layer::Property::BEHAVIOR) == Layer::LAYER_3D)
724   {
725     // Layer 3D set, do not align pixels
726     mImpl->mRenderer.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
727   }
728
729   if(mPixelArea != FULL_TEXTURE_RECT)
730   {
731     mImpl->mRenderer.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, mPixelArea);
732   }
733
734   if(mLoadState == TextureManager::LoadState::LOAD_FINISHED)
735   {
736     actor.AddRenderer(mImpl->mRenderer);
737     mPlacementActor.Reset();
738
739     // Image loaded and ready to display
740     ResourceReady(Toolkit::Visual::ResourceStatus::READY);
741   }
742   else if(mLoadState == TextureManager::LoadState::LOAD_FAILED)
743   {
744     Vector2 imageSize = Vector2::ZERO;
745     if(actor)
746     {
747       imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
748     }
749     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
750     actor.AddRenderer(mImpl->mRenderer);
751     mPlacementActor.Reset();
752
753     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
754   }
755 }
756
757 void ImageVisual::DoSetOffScene(Actor& actor)
758 {
759   // Visual::Base::SetOffScene only calls DoSetOffScene if mRenderer exists (is on onstage)
760
761   // Image release is dependent on the ReleasePolicy, renderer is removed.
762   actor.RemoveRenderer(mImpl->mRenderer);
763   if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
764   {
765     RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
766     mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
767
768     TextureSet textureSet = TextureSet::New();
769     mImpl->mRenderer.SetTextures(textureSet);
770     ComputeTextureSize();
771
772     mLoadState = TextureManager::LoadState::NOT_STARTED;
773   }
774
775   mPlacementActor.Reset();
776 }
777
778 void ImageVisual::DoCreatePropertyMap(Property::Map& map) const
779 {
780   map.Clear();
781   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
782
783   bool sync = IsSynchronousLoadingRequired();
784   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
785   if(mImageUrl.IsValid())
786   {
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());
790   }
791
792   map.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode);
793   map.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode);
794
795   map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
796   map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU);
797   map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV);
798
799   map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
800
801   if(mMaskingData != NULL)
802   {
803     map.Insert(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl());
804     map.Insert(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor);
805     map.Insert(Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask);
806     map.Insert(Toolkit::DevelImageVisual::Property::MASKING_TYPE, mMaskingData->mPreappliedMasking ? DevelImageVisual::MaskingType::MASKING_ON_LOADING : DevelImageVisual::MaskingType::MASKING_ON_RENDERING);
807   }
808
809   map.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy);
810   map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
811   map.Insert(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection);
812 }
813
814 void ImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
815 {
816   map.Clear();
817   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
818   if(mImageUrl.IsValid())
819   {
820     map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
821     map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
822   }
823 }
824
825 void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes)
826 {
827   // Check if action is valid for this visual type and perform action if possible
828
829   switch(actionId)
830   {
831     case DevelImageVisual::Action::RELOAD:
832     {
833       auto attemptAtlasing = AttemptAtlasing();
834       LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::FORCED);
835       break;
836     }
837   }
838 }
839
840 void ImageVisual::OnSetTransform()
841 {
842   if(mImpl->mRenderer)
843   {
844     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
845   }
846 }
847
848 void ImageVisual::UpdateShader()
849 {
850   if(mImpl->mRenderer)
851   {
852     Shader shader = GenerateShader();
853     mImpl->mRenderer.SetShader(shader);
854   }
855 }
856
857 // From existing atlas manager
858 void ImageVisual::UploadCompleted()
859 {
860   // Texture has been uploaded. If weak handle is holding a placement actor,
861   // it is the time to add the renderer to actor.
862   Actor actor = mPlacementActor.GetHandle();
863   if(actor)
864   {
865     mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
866     actor.AddRenderer(mImpl->mRenderer);
867     // reset the weak handle so that the renderer only get added to actor once
868     mPlacementActor.Reset();
869   }
870
871   // Image loaded
872   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
873   mLoadState = TextureManager::LoadState::LOAD_FINISHED;
874 }
875
876 // From Texture Manager
877 void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
878 {
879   Toolkit::Visual::ResourceStatus resourceStatus;
880   if(mImpl->mRenderer)
881   {
882     if(textureInformation.useAtlasing)
883     {
884       mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
885     }
886
887     EnablePreMultipliedAlpha(textureInformation.preMultiplied);
888
889     Actor actor = mPlacementActor.GetHandle();
890     if(!loadingSuccess)
891     {
892       Vector2 imageSize = Vector2::ZERO;
893       if(actor)
894       {
895         imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
896       }
897       mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
898       textureInformation.textureSet = mImpl->mRenderer.GetTextures();
899     }
900     else
901     {
902       Sampler sampler = Sampler::New();
903       sampler.SetWrapMode(mWrapModeU, mWrapModeV);
904       textureInformation.textureSet.SetSampler(0u, sampler);
905       mImpl->mRenderer.SetTextures(textureInformation.textureSet);
906       ComputeTextureSize();
907       CheckMaskTexture();
908     }
909
910     if(actor)
911     {
912       actor.AddRenderer(mImpl->mRenderer);
913       // reset the weak handle so that the renderer only get added to actor once
914       mPlacementActor.Reset();
915     }
916   }
917
918   // Storing TextureSet needed when renderer staged.
919   if(!mImpl->mRenderer)
920   {
921     mTextures = textureInformation.textureSet;
922   }
923
924   // Image loaded, set status regardless of staged status.
925   if(loadingSuccess)
926   {
927     resourceStatus = Toolkit::Visual::ResourceStatus::READY;
928     mLoadState     = TextureManager::LoadState::LOAD_FINISHED;
929   }
930   else
931   {
932     resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
933     mLoadState     = TextureManager::LoadState::LOAD_FAILED;
934   }
935
936   // use geometry if needed
937   if(loadingSuccess)
938   {
939     uint32_t firstElementCount{0u};
940     uint32_t secondElementCount{0u};
941     auto     geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
942     if(mImpl->mRenderer && geometry)
943     {
944       mImpl->mRenderer.SetGeometry(geometry);
945       Dali::DevelRenderer::DrawCommand drawCommand{};
946       drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
947
948       if(firstElementCount)
949       {
950         drawCommand.firstIndex   = 0;
951         drawCommand.elementCount = firstElementCount;
952         drawCommand.queue        = DevelRenderer::RENDER_QUEUE_OPAQUE;
953         DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
954       }
955
956       if(secondElementCount)
957       {
958         drawCommand.firstIndex   = firstElementCount;
959         drawCommand.elementCount = secondElementCount;
960         drawCommand.queue        = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
961         DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
962       }
963     }
964   }
965
966   // Signal to observers ( control ) that resources are ready. Must be all resources.
967   ResourceReady(resourceStatus);
968 }
969
970 void ImageVisual::RemoveTexture()
971 {
972   if(mTextureId != TextureManager::INVALID_TEXTURE_ID)
973   {
974     mFactoryCache.GetTextureManager().Remove(mTextureId, this);
975     mTextureId = TextureManager::INVALID_TEXTURE_ID;
976   }
977   else
978   {
979     Vector4         atlasRect(0.f, 0.f, 1.f, 1.f);
980     Property::Index index = mImpl->mRenderer.GetPropertyIndex(ATLAS_RECT_UNIFORM_NAME);
981     if(index != Property::INVALID_INDEX)
982     {
983       Property::Value atlasRectValue = mImpl->mRenderer.GetProperty(index);
984       atlasRectValue.Get(atlasRect);
985     }
986
987     TextureSet textureSet = mImpl->mRenderer.GetTextures();
988
989     if(index != Property::INVALID_INDEX)
990     {
991       mFactoryCache.GetAtlasManager()->Remove(textureSet, atlasRect);
992     }
993   }
994 }
995
996 void ImageVisual::ComputeTextureSize()
997 {
998   if(mImpl->mRenderer)
999   {
1000     auto textureSet = mImpl->mRenderer.GetTextures();
1001     if(textureSet && textureSet.GetTextureCount())
1002     {
1003       auto texture = textureSet.GetTexture(0);
1004       if(texture)
1005       {
1006         mTextureSize.x = texture.GetWidth();
1007         mTextureSize.y = texture.GetHeight();
1008         if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1009         {
1010           Texture maskTexture = textureSet.GetTexture(1);
1011           if(maskTexture)
1012           {
1013             mTextureSize.x = std::min(static_cast<uint32_t>(mTextureSize.x * mMaskingData->mContentScaleFactor), maskTexture.GetWidth());
1014             mTextureSize.y = std::min(static_cast<uint32_t>(mTextureSize.y * mMaskingData->mContentScaleFactor), maskTexture.GetHeight());
1015           }
1016         }
1017       }
1018     }
1019   }
1020 }
1021
1022 Vector2 ImageVisual::ComputeMaskTextureRatio()
1023 {
1024   Vector2 maskTextureRatio;
1025   if(mImpl->mRenderer)
1026   {
1027     auto textureSet = mImpl->mRenderer.GetTextures();
1028     if(textureSet && textureSet.GetTextureCount())
1029     {
1030       auto texture = textureSet.GetTexture(0);
1031       if(texture)
1032       {
1033         if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1034         {
1035           Texture maskTexture = textureSet.GetTexture(1);
1036           if(maskTexture)
1037           {
1038             float textureWidth  = std::max(static_cast<float>(texture.GetWidth() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1039             float textureHeight = std::max(static_cast<float>(texture.GetHeight() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1040             maskTextureRatio    = Vector2(std::min(static_cast<float>(maskTexture.GetWidth()), textureWidth) / textureWidth,
1041                                        std::min(static_cast<float>(maskTexture.GetHeight()), textureHeight) / textureHeight);
1042           }
1043         }
1044       }
1045     }
1046   }
1047   return maskTextureRatio;
1048 }
1049
1050 Shader ImageVisual::GenerateShader() const
1051 {
1052   Shader shader;
1053
1054   bool       usesWholeTexture  = true;
1055   const bool useStandardShader = !mImpl->mCustomShader;
1056   const bool useNativeImage    = (mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)));
1057
1058   if(useStandardShader)
1059   {
1060     bool   requiredAlphaMaskingOnRendering = (mMaskingData && !mMaskingData->mMaskImageLoadingFailed) ? !mMaskingData->mPreappliedMasking : false;
1061     // Create and cache the standard shader
1062     shader = mImageVisualShaderFactory.GetShader(
1063       mFactoryCache,
1064       ImageVisualShaderFeature::FeatureBuilder()
1065         .EnableTextureAtlas(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED && !useNativeImage)
1066         .ApplyDefaultTextureWrapMode(mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE)
1067         .EnableRoundedCorner(IsRoundedCornerRequired())
1068         .EnableBorderline(IsBorderlineRequired())
1069         .SetTextureForFragmentShaderCheck(useNativeImage ? mTextures.GetTexture(0) : Dali::Texture())
1070         .EnableAlphaMaskingOnRendering(requiredAlphaMaskingOnRendering));
1071   }
1072   else
1073   {
1074     std::string_view vertexShaderView;
1075     std::string_view fragmentShaderView;
1076
1077     if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
1078     {
1079       vertexShaderView = mImpl->mCustomShader->mVertexShader;
1080       usesWholeTexture = false; // Impossible to tell.
1081     }
1082     else
1083     {
1084       vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
1085     }
1086
1087     if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
1088     {
1089       fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
1090     }
1091     else
1092     {
1093       fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
1094     }
1095
1096     // If the texture is native, we may need to change prefix and sampler in
1097     // the fragment shader
1098     if(useNativeImage)
1099     {
1100       bool        modifiedFragmentShader = false;
1101       Texture     nativeTexture          = mTextures.GetTexture(0);
1102       std::string fragmentShaderString   = std::string(fragmentShaderView);
1103
1104       modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
1105       if(modifiedFragmentShader)
1106       {
1107         fragmentShaderView = fragmentShaderString;
1108       }
1109
1110       // Create shader here cause fragmentShaderString scope issue
1111       shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1112     }
1113     else
1114     {
1115       shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1116     }
1117   }
1118
1119   if(usesWholeTexture)
1120   {
1121     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
1122   }
1123
1124   // Set pixel align off as default.
1125   // ToDo: Pixel align causes issues such as rattling image animation.
1126   // We should trun it off until issues are resolved
1127   shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
1128
1129   return shader;
1130 }
1131
1132 void ImageVisual::CheckMaskTexture()
1133 {
1134   if(mMaskingData && !mMaskingData->mPreappliedMasking)
1135   {
1136     bool maskLoadFailed = true;
1137     TextureSet textures = mImpl->mRenderer.GetTextures();
1138     if(textures && textures.GetTextureCount() >= TEXTURE_COUNT_FOR_GPU_ALPHA_MASK)
1139     {
1140       if(mMaskingData->mCropToMask)
1141       {
1142         mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, ComputeMaskTextureRatio());
1143       }
1144       maskLoadFailed = false;
1145     }
1146
1147     if(mMaskingData->mMaskImageLoadingFailed != maskLoadFailed)
1148     {
1149       mMaskingData->mMaskImageLoadingFailed = maskLoadFailed;
1150       UpdateShader();
1151     }
1152   }
1153 }
1154
1155 } // namespace Internal
1156
1157 } // namespace Toolkit
1158
1159 } // namespace Dali