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