4ebb5e4b9bc315be7ca9a8f4c9a4aa59df622de2
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.cpp
1 /*
2  * Copyright (c) 2024 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/adaptor-framework/async-task-manager.h>
31 #include <dali/public-api/rendering/decorated-visual-renderer.h>
32 #include <cstring> // for strlen()
33
34 // INTERNAL HEADERS
35 #include <dali-toolkit/devel-api/visuals/image-visual-actions-devel.h>
36 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
37 #include <dali-toolkit/internal/visuals/image/image-atlas-manager.h>
38 #include <dali-toolkit/internal/visuals/image/image-visual-shader-factory.h>
39 #include <dali-toolkit/internal/visuals/image/image-visual-shader-feature-builder.h>
40 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
41 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
42 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
43 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
44 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
45 #include <dali-toolkit/internal/visuals/visual-url.h>
46 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
47 #include <dali-toolkit/public-api/visuals/visual-properties.h>
48
49 namespace Dali
50 {
51 namespace Toolkit
52 {
53 namespace Internal
54 {
55 namespace
56 {
57 const int CUSTOM_PROPERTY_COUNT(7); // ltr, wrap, pixel area, atlas, pixalign, crop to mask, mask texture ratio
58
59 // fitting modes
60 DALI_ENUM_TO_STRING_TABLE_BEGIN(FITTING_MODE)
61   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SHRINK_TO_FIT)
62   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, SCALE_TO_FILL)
63   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_WIDTH)
64   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, FIT_HEIGHT)
65   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, VISUAL_FITTING)
66   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::FittingMode, DEFAULT)
67 DALI_ENUM_TO_STRING_TABLE_END(FITTING_MODE)
68
69 // sampling modes
70 DALI_ENUM_TO_STRING_TABLE_BEGIN(SAMPLING_MODE)
71   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX)
72   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NEAREST)
73   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, LINEAR)
74   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_NEAREST)
75   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, BOX_THEN_LINEAR)
76   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, NO_FILTER)
77   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::SamplingMode, DONT_CARE)
78 DALI_ENUM_TO_STRING_TABLE_END(SAMPLING_MODE)
79
80 // wrap modes
81 DALI_ENUM_TO_STRING_TABLE_BEGIN(WRAP_MODE)
82   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, DEFAULT)
83   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, CLAMP_TO_EDGE)
84   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, REPEAT)
85   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::WrapMode, MIRRORED_REPEAT)
86 DALI_ENUM_TO_STRING_TABLE_END(WRAP_MODE)
87
88 // load policies
89 DALI_ENUM_TO_STRING_TABLE_BEGIN(LOAD_POLICY)
90   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, IMMEDIATE)
91   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::LoadPolicy, ATTACHED)
92 DALI_ENUM_TO_STRING_TABLE_END(LOAD_POLICY)
93
94 // release policies
95 DALI_ENUM_TO_STRING_TABLE_BEGIN(RELEASE_POLICY)
96   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DETACHED)
97   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, DESTROYED)
98   DALI_ENUM_TO_STRING_WITH_SCOPE(Dali::Toolkit::ImageVisual::ReleasePolicy, NEVER)
99 DALI_ENUM_TO_STRING_TABLE_END(RELEASE_POLICY)
100
101 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
102
103 constexpr uint32_t TEXTURE_COUNT_FOR_GPU_ALPHA_MASK = 2u;
104
105 struct NameIndexMatch
106 {
107   const char* const name;
108   Property::Index   index;
109 };
110
111 const NameIndexMatch NAME_INDEX_MATCH_TABLE[] =
112   {
113     {IMAGE_FITTING_MODE, Toolkit::ImageVisual::Property::FITTING_MODE},
114     {IMAGE_SAMPLING_MODE, Toolkit::ImageVisual::Property::SAMPLING_MODE},
115     {IMAGE_DESIRED_WIDTH, Toolkit::ImageVisual::Property::DESIRED_WIDTH},
116     {IMAGE_DESIRED_HEIGHT, Toolkit::ImageVisual::Property::DESIRED_HEIGHT},
117     {PIXEL_AREA_UNIFORM_NAME, Toolkit::ImageVisual::Property::PIXEL_AREA},
118     {IMAGE_WRAP_MODE_U, Toolkit::ImageVisual::Property::WRAP_MODE_U},
119     {IMAGE_WRAP_MODE_V, Toolkit::ImageVisual::Property::WRAP_MODE_V},
120     {SYNCHRONOUS_LOADING, Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING},
121     {IMAGE_ATLASING, Toolkit::ImageVisual::Property::ATLASING},
122     {ALPHA_MASK_URL, Toolkit::ImageVisual::Property::ALPHA_MASK_URL},
123     {MASK_CONTENT_SCALE_NAME, Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE},
124     {CROP_TO_MASK_NAME, Toolkit::ImageVisual::Property::CROP_TO_MASK},
125     {MASKING_TYPE_NAME, Toolkit::DevelImageVisual::Property::MASKING_TYPE},
126     {ENABLE_BROKEN_IMAGE, Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE},
127     {LOAD_POLICY_NAME, Toolkit::ImageVisual::Property::LOAD_POLICY},
128     {RELEASE_POLICY_NAME, Toolkit::ImageVisual::Property::RELEASE_POLICY},
129     {ORIENTATION_CORRECTION_NAME, Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION},
130     {FAST_TRACK_UPLOADING_NAME, Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING},
131 };
132 const int NAME_INDEX_MATCH_TABLE_SIZE = sizeof(NAME_INDEX_MATCH_TABLE) / sizeof(NAME_INDEX_MATCH_TABLE[0]);
133
134 Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
135 {
136   Geometry geometry;
137
138   if(gridSize == ImageDimensions(1, 1))
139   {
140     geometry = factoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
141   }
142   else
143   {
144     geometry = VisualFactoryCache::CreateGridGeometry(gridSize);
145   }
146
147   return geometry;
148 }
149
150 } // unnamed namespace
151
152 ImageVisualPtr ImageVisual::New(VisualFactoryCache&       factoryCache,
153                                 ImageVisualShaderFactory& shaderFactory,
154                                 const VisualUrl&          imageUrl,
155                                 const Property::Map&      properties,
156                                 ImageDimensions           size,
157                                 FittingMode::Type         fittingMode,
158                                 Dali::SamplingMode::Type  samplingMode)
159 {
160   ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
161   imageVisualPtr->SetProperties(properties);
162   imageVisualPtr->Initialize();
163   return imageVisualPtr;
164 }
165
166 ImageVisualPtr ImageVisual::New(VisualFactoryCache&       factoryCache,
167                                 ImageVisualShaderFactory& shaderFactory,
168                                 const VisualUrl&          imageUrl,
169                                 ImageDimensions           size,
170                                 FittingMode::Type         fittingMode,
171                                 Dali::SamplingMode::Type  samplingMode)
172 {
173   ImageVisualPtr imageVisualPtr(new ImageVisual(factoryCache, shaderFactory, imageUrl, size, fittingMode, samplingMode));
174   imageVisualPtr->Initialize();
175   return imageVisualPtr;
176 }
177
178 ImageVisual::ImageVisual(VisualFactoryCache&       factoryCache,
179                          ImageVisualShaderFactory& shaderFactory,
180                          const VisualUrl&          imageUrl,
181                          ImageDimensions           size,
182                          FittingMode::Type         fittingMode,
183                          Dali::SamplingMode::Type  samplingMode)
184 : Visual::Base(factoryCache, Visual::FittingMode::DONT_CARE, Toolkit::Visual::IMAGE),
185   mPixelArea(FULL_TEXTURE_RECT),
186   mPixelAreaIndex(Property::INVALID_INDEX),
187   mPlacementActor(),
188   mImageUrl(imageUrl),
189   mMaskingData(),
190   mDesiredSize(size),
191   mTextureId(TextureManager::INVALID_TEXTURE_ID),
192   mTextures(),
193   mImageVisualShaderFactory(shaderFactory),
194   mFittingMode(fittingMode),
195   mSamplingMode(samplingMode),
196   mWrapModeU(WrapMode::DEFAULT),
197   mWrapModeV(WrapMode::DEFAULT),
198   mLoadPolicy(Toolkit::ImageVisual::LoadPolicy::ATTACHED),
199   mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED),
200   mAtlasRect(0.0f, 0.0f, 0.0f, 0.0f),
201   mAtlasRectSize(0, 0),
202   mLoadState(TextureManager::LoadState::NOT_STARTED),
203   mAttemptAtlasing(false),
204   mOrientationCorrection(true),
205   mEnableBrokenImage(true)
206 {
207   EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
208 }
209
210 ImageVisual::~ImageVisual()
211 {
212   if(Stage::IsInstalled())
213   {
214     if(mImageUrl.IsValid())
215     {
216       // Decrease reference count of External Resources :
217       // EncodedImageBuffer or ExternalTextures.
218       // Ensure the stage is still valid before accessing texture manager.
219       if(mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
220       {
221         TextureManager& textureManager = mFactoryCache.GetTextureManager();
222         textureManager.RemoveExternalTexture(mImageUrl.GetUrl());
223       }
224       else if(mImageUrl.IsBufferResource())
225       {
226         TextureManager& textureManager = mFactoryCache.GetTextureManager();
227         textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
228       }
229     }
230
231     // ImageVisual destroyed so remove texture unless ReleasePolicy is set to never release
232     if((mTextureId != TextureManager::INVALID_TEXTURE_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
233     {
234       RemoveTexture();
235     }
236
237     ResetFastTrackLoadingTask();
238   }
239 }
240
241 void ImageVisual::DoSetProperties(const Property::Map& propertyMap)
242 {
243   // Url is already received in constructor
244   for(Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter)
245   {
246     KeyValuePair keyValue = propertyMap.GetKeyValue(iter);
247     if(keyValue.first.type == Property::Key::INDEX)
248     {
249       DoSetProperty(keyValue.first.indexKey, keyValue.second);
250     }
251     else
252     {
253       for(int i = 0; i < NAME_INDEX_MATCH_TABLE_SIZE; ++i)
254       {
255         if(keyValue.first == NAME_INDEX_MATCH_TABLE[i].name)
256         {
257           DoSetProperty(NAME_INDEX_MATCH_TABLE[i].index, keyValue.second);
258           break;
259         }
260       }
261     }
262   }
263   // Load image immediately if LOAD_POLICY requires it
264   if(mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::IMMEDIATE)
265   {
266     auto attemptAtlasing = AttemptAtlasing();
267     LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
268   }
269 }
270
271 void ImageVisual::DoSetProperty(Property::Index index, const Property::Value& value)
272 {
273   switch(index)
274   {
275     case Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING:
276     {
277       bool sync = false;
278       if(value.Get(sync))
279       {
280         if(sync)
281         {
282           mImpl->mFlags |= Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
283         }
284         else
285         {
286           mImpl->mFlags &= ~Visual::Base::Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
287         }
288       }
289       else
290       {
291         DALI_LOG_ERROR("ImageVisual: synchronousLoading property has incorrect type\n");
292       }
293       break;
294     }
295
296     case Toolkit::ImageVisual::Property::DESIRED_WIDTH:
297     {
298       int32_t desiredWidth = 0;
299       if(value.Get(desiredWidth))
300       {
301         mDesiredSize.SetWidth(desiredWidth);
302       }
303       else
304       {
305         DALI_LOG_ERROR("ImageVisual: desiredWidth property has incorrect type\n");
306       }
307       break;
308     }
309
310     case Toolkit::ImageVisual::Property::DESIRED_HEIGHT:
311     {
312       int32_t desiredHeight = 0;
313       if(value.Get(desiredHeight))
314       {
315         mDesiredSize.SetHeight(desiredHeight);
316       }
317       else
318       {
319         DALI_LOG_ERROR("ImageVisual: desiredHeight property has incorrect type\n");
320       }
321       break;
322     }
323
324     case Toolkit::ImageVisual::Property::FITTING_MODE:
325     {
326       int fittingMode = 0;
327       Scripting::GetEnumerationProperty(value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, fittingMode);
328       mFittingMode = Dali::FittingMode::Type(fittingMode);
329       break;
330     }
331
332     case Toolkit::ImageVisual::Property::SAMPLING_MODE:
333     {
334       int samplingMode = 0;
335       Scripting::GetEnumerationProperty(value, SAMPLING_MODE_TABLE, SAMPLING_MODE_TABLE_COUNT, samplingMode);
336       mSamplingMode = Dali::SamplingMode::Type(samplingMode);
337       break;
338     }
339
340     case Toolkit::ImageVisual::Property::PIXEL_AREA:
341     {
342       value.Get(mPixelArea);
343
344       if(DALI_UNLIKELY(mImpl->mRenderer))
345       {
346         // Unusual case. SetProperty called after OnInitialize().
347         // Assume that DoAction call UPDATE_PROPERTY.
348         mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
349       }
350       break;
351     }
352
353     case Toolkit::ImageVisual::Property::WRAP_MODE_U:
354     {
355       int wrapMode = 0;
356       Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
357       mWrapModeU = Dali::WrapMode::Type(wrapMode);
358       break;
359     }
360
361     case Toolkit::ImageVisual::Property::WRAP_MODE_V:
362     {
363       int wrapMode = 0;
364       Scripting::GetEnumerationProperty(value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode);
365       mWrapModeV = Dali::WrapMode::Type(wrapMode);
366       break;
367     }
368
369     case Toolkit::ImageVisual::Property::ATLASING:
370     {
371       value.Get(mAttemptAtlasing);
372       break;
373     }
374
375     case Toolkit::ImageVisual::Property::ALPHA_MASK_URL:
376     {
377       std::string alphaUrl = "";
378       if(value.Get(alphaUrl))
379       {
380         AllocateMaskData();
381         mMaskingData->mAlphaMaskUrl = alphaUrl;
382       }
383       break;
384     }
385
386     case Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE:
387     {
388       float scale = 1.0f;
389       if(value.Get(scale))
390       {
391         AllocateMaskData();
392         mMaskingData->mContentScaleFactor = scale;
393       }
394       break;
395     }
396
397     case Toolkit::ImageVisual::Property::CROP_TO_MASK:
398     {
399       bool crop = false;
400       if(value.Get(crop))
401       {
402         AllocateMaskData();
403         mMaskingData->mCropToMask = crop;
404       }
405       break;
406     }
407
408     case Toolkit::DevelImageVisual::Property::MASKING_TYPE:
409     {
410       int maskingType = 0;
411       if(value.Get(maskingType))
412       {
413         AllocateMaskData();
414         if(mImageUrl.IsValid() && mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
415         {
416           // For external textures, only gpu masking is available.
417           // Therefore, MASKING_TYPE is set to MASKING_ON_RENDERING forcelly.
418           mMaskingData->mPreappliedMasking = false;
419         }
420         else
421         {
422           mMaskingData->mPreappliedMasking = (Toolkit::DevelImageVisual::MaskingType::Type(maskingType) == Toolkit::DevelImageVisual::MaskingType::MASKING_ON_LOADING);
423         }
424       }
425       break;
426     }
427
428     case Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE:
429     {
430       value.Get(mEnableBrokenImage);
431       break;
432     }
433
434     case Toolkit::ImageVisual::Property::RELEASE_POLICY:
435     {
436       int releasePolicy = 0;
437       Scripting::GetEnumerationProperty(value, RELEASE_POLICY_TABLE, RELEASE_POLICY_TABLE_COUNT, releasePolicy);
438       mReleasePolicy = Toolkit::ImageVisual::ReleasePolicy::Type(releasePolicy);
439       break;
440     }
441
442     case Toolkit::ImageVisual::Property::LOAD_POLICY:
443     {
444       int loadPolicy = 0;
445       Scripting::GetEnumerationProperty(value, LOAD_POLICY_TABLE, LOAD_POLICY_TABLE_COUNT, loadPolicy);
446       mLoadPolicy = Toolkit::ImageVisual::LoadPolicy::Type(loadPolicy);
447       break;
448     }
449     case Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION:
450     {
451       value.Get(mOrientationCorrection);
452       break;
453     }
454
455     case Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING:
456     {
457       value.Get(mUseFastTrackUploading);
458       break;
459     }
460   }
461 }
462
463 void ImageVisual::AllocateMaskData()
464 {
465   if(!mMaskingData)
466   {
467     mMaskingData.reset(new TextureManager::MaskingData());
468     if(mImageUrl.IsValid() && mImageUrl.GetProtocolType() == VisualUrl::TEXTURE)
469     {
470       mMaskingData->mPreappliedMasking = false;
471     }
472   }
473 }
474
475 void ImageVisual::GetNaturalSize(Vector2& naturalSize)
476 {
477   if(mDesiredSize.GetWidth() > 0 && mDesiredSize.GetHeight() > 0)
478   {
479     if(mImpl->mRenderer)
480     {
481       auto textureSet = mImpl->mRenderer.GetTextures();
482       if(textureSet && textureSet.GetTextureCount())
483       {
484         auto texture = textureSet.GetTexture(0);
485         if(texture)
486         {
487           if(mTextureSize != Vector2::ZERO)
488           {
489             naturalSize = mTextureSize;
490             return;
491           }
492         }
493       }
494     }
495
496     naturalSize.x = mDesiredSize.GetWidth();
497     naturalSize.y = mDesiredSize.GetHeight();
498     return;
499   }
500   else if(mImpl->mRenderer) // Check if we have a loaded image
501   {
502     if(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED)
503     {
504       naturalSize.x = mAtlasRectSize.GetWidth();
505       naturalSize.y = mAtlasRectSize.GetHeight();
506       return;
507     }
508
509     auto textureSet = mImpl->mRenderer.GetTextures();
510     if(textureSet && textureSet.GetTextureCount())
511     {
512       if(mTextureSize != Vector2::ZERO)
513       {
514         naturalSize = mTextureSize;
515         return;
516       }
517     }
518   }
519
520   if(mMaskingData != NULL && mMaskingData->mAlphaMaskUrl.IsValid() &&
521      mMaskingData->mCropToMask)
522   {
523     ImageDimensions dimensions = Dali::GetClosestImageSize(mMaskingData->mAlphaMaskUrl.GetUrl());
524     if(dimensions != ImageDimensions(0, 0))
525     {
526       naturalSize.x = dimensions.GetWidth();
527       naturalSize.y = dimensions.GetHeight();
528     }
529     return;
530   }
531   else if(mImageUrl.IsValid())
532   {
533     if(mImageUrl.GetProtocolType() == VisualUrl::LOCAL)
534     {
535       ImageDimensions dimensions = Dali::GetClosestImageSize(mImageUrl.GetUrl());
536
537       if(dimensions != ImageDimensions(0, 0))
538       {
539         naturalSize.x = dimensions.GetWidth();
540         naturalSize.y = dimensions.GetHeight();
541       }
542       else
543       {
544         Actor   actor     = mPlacementActor.GetHandle();
545         Vector2 imageSize = Vector2::ZERO;
546         if(actor)
547         {
548           imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
549         }
550         else
551         {
552           imageSize = mPlacementActorSize;
553         }
554
555         mUseBrokenImageRenderer = true;
556         mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
557         Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
558         naturalSize.x       = brokenImage.GetWidth();
559         naturalSize.y       = brokenImage.GetWidth();
560       }
561       return;
562     }
563   }
564   naturalSize = Vector2::ZERO;
565 }
566
567 void ImageVisual::OnInitialize()
568 {
569   // Increase reference count of External Resources :
570   // EncodedImageBuffer or ExternalTextures.
571   // Reference count will be decreased at destructor of the visual.
572   if(mImageUrl.IsValid() && (mImageUrl.IsBufferResource() || mImageUrl.GetProtocolType() == VisualUrl::TEXTURE))
573   {
574     TextureManager& textureManager = mFactoryCache.GetTextureManager();
575     textureManager.UseExternalResource(mImageUrl.GetUrl());
576   }
577
578   // Generate geometry and shader. Note that we should check AddOn when generate geometry, due to LoadPolicy::IMMEDIATE case
579   Geometry geometry = GenerateGeometry(mTextureId, true);
580   Shader   shader   = GenerateShader();
581
582   // Create the renderer
583   mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
584   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
585
586   //Register transform properties
587   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
588
589   EnablePreMultipliedAlpha(IsPreMultipliedAlphaEnabled());
590
591   if(mMaskingData)
592   {
593     mImpl->mRenderer.RegisterProperty(CROP_TO_MASK_NAME, static_cast<float>(mMaskingData->mCropToMask));
594   }
595 }
596
597 void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& textures, bool orientationCorrection, TextureManager::ReloadPolicy forceReload)
598 {
599   TextureManager& textureManager = mFactoryCache.GetTextureManager();
600
601   ImageAtlasManagerPtr atlasManager        = nullptr;
602   AtlasUploadObserver* atlasUploadObserver = nullptr;
603   auto                 textureObserver     = this;
604
605   if(atlasing)
606   {
607     atlasManager        = mFactoryCache.GetAtlasManager();
608     atlasUploadObserver = this;
609   }
610
611   auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
612                              ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
613                              : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
614
615   bool synchronousLoading = IsSynchronousLoadingRequired();
616   bool loadingStatus      = false;
617
618   // Remove previous loading task.
619   ResetFastTrackLoadingTask();
620
621   // Rare case. If someone call LoadTexture during fast track loading task running, (Ex : Action::RELOAD)
622   // we should remove previously added renderer now.
623   if(mRendererAdded)
624   {
625     Actor actor = mPlacementActor.GetHandle();
626     if(actor)
627     {
628       actor.RemoveRenderer(mImpl->mRenderer);
629       mRendererAdded = false;
630     }
631   }
632
633   /**
634    * @brief Check whether FastTrackUploading is avaliable or not.
635    * @return True if we can use fast track uploading feature. False otherwise.
636    */
637   auto IsFastTrackUploadingAvailable = [&]() {
638     if(mUseFastTrackUploading &&
639        mLoadPolicy == Toolkit::ImageVisual::LoadPolicy::ATTACHED &&
640        mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED &&
641        forceReload == TextureManager::ReloadPolicy::CACHED &&
642        (mImageUrl.GetProtocolType() == VisualUrl::LOCAL || mImageUrl.GetProtocolType() == VisualUrl::REMOTE) &&
643        !synchronousLoading &&
644        !atlasing &&
645        !mImpl->mCustomShader &&
646        !(mMaskingData && mMaskingData->mAlphaMaskUrl.IsValid()))
647     {
648       return true;
649     }
650     else if(mUseFastTrackUploading)
651     {
652       DALI_LOG_DEBUG_INFO("FastTrack : Fail to load fast track. mUrl : [%s]%s%s%s%s%s%s%s%s\n",
653                           mImageUrl.GetEllipsedUrl().c_str(),
654                           (mLoadPolicy != Toolkit::ImageVisual::LoadPolicy::ATTACHED) ? "/ mLoadPolicy != ATTACHED" : "",
655                           (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::DETACHED) ? "/ mReleasePolicy != DETACHED" : "",
656                           (forceReload != TextureManager::ReloadPolicy::CACHED) ? "/ forceReload != CACHED" : "",
657                           (!(mImageUrl.GetProtocolType() == VisualUrl::LOCAL || mImageUrl.GetProtocolType() == VisualUrl::REMOTE)) ? "/ url is not image" : "",
658                           (synchronousLoading) ? "/ synchronousLoading" : "",
659                           (atlasing) ? "/ atlasing" : "",
660                           (mImpl->mCustomShader) ? "/ use customs shader" : "",
661                           (mMaskingData && mMaskingData->mAlphaMaskUrl.IsValid()) ? "/ use masking url" : "");
662     }
663     return false;
664   };
665
666   if(IsFastTrackUploadingAvailable())
667   {
668     // Enable PremultipliedAlpha first.
669     EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
670
671     // Set new TextureSet with fast track loading task
672     mFastTrackLoadingTask = new FastTrackLoadingTask(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mOrientationCorrection, preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF, mFactoryCache.GetLoadYuvPlanes(), MakeCallback(this, &ImageVisual::FastLoadComplete));
673
674     TextureSet textureSet = TextureSet::New();
675     if(!mFastTrackLoadingTask->mLoadPlanesAvaliable)
676     {
677       DALI_ASSERT_ALWAYS(mFastTrackLoadingTask->mTextures.size() >= 1u);
678       textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
679     }
680     else
681     {
682       DALI_ASSERT_ALWAYS(mFastTrackLoadingTask->mTextures.size() >= 3u);
683       textureSet.SetTexture(2u, mFastTrackLoadingTask->mTextures[2]);
684       textureSet.SetTexture(1u, mFastTrackLoadingTask->mTextures[1]);
685       textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
686
687       // We cannot determine what kind of shader will be used.
688       // Just use unified shader, and then change shader after load completed.
689       mNeedUnifiedYuvAndRgb = true;
690       UpdateShader();
691     }
692     mImpl->mRenderer.SetTextures(textureSet);
693
694     Dali::AsyncTaskManager::Get().AddTask(mFastTrackLoadingTask);
695
696     mLoadState = TextureManager::LoadState::LOADING;
697   }
698   else
699   {
700     textures = textureManager.LoadTexture(mImageUrl, mDesiredSize, mFittingMode, mSamplingMode, mMaskingData, synchronousLoading, mTextureId, atlasRect, mAtlasRectSize, atlasing, loadingStatus, textureObserver, atlasUploadObserver, atlasManager, mOrientationCorrection, forceReload, preMultiplyOnLoad);
701   }
702
703   if(textures)
704   {
705     if(loadingStatus)
706     {
707       mLoadState = TextureManager::LoadState::LOADING;
708     }
709     else
710     {
711       mLoadState = TextureManager::LoadState::LOAD_FINISHED;
712     }
713
714     EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
715     if(!atlasing && (mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT))
716     {
717       Sampler sampler = Sampler::New();
718       sampler.SetWrapMode(mWrapModeU, mWrapModeV);
719       textures.SetSampler(0u, sampler);
720     }
721   }
722   else if(synchronousLoading)
723   {
724     // Synchronous loading is failed
725     mLoadState = TextureManager::LoadState::LOAD_FAILED;
726   }
727
728   if(atlasing) // Flag needs to be set before creating renderer
729   {
730     mImpl->mFlags |= Visual::Base::Impl::IS_ATLASING_APPLIED;
731   }
732   else
733   {
734     mImpl->mFlags &= ~Visual::Base::Impl::IS_ATLASING_APPLIED;
735   }
736 }
737
738 bool ImageVisual::AttemptAtlasing()
739 {
740   return (!mImpl->mCustomShader && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()) && mAttemptAtlasing);
741 }
742
743 void ImageVisual::InitializeRenderer()
744 {
745   auto attemptAtlasing = AttemptAtlasing();
746
747   // Load Texture if mTextures is empty.
748   // mTextures is already set, the mTexture can be used to create Renderer.
749   // There are two cases mTextures is empty.
750   // 1. mTextureId == TextureManager::INVALID_TEXTURE_ID
751   //  - Visual is on stage with LoadPolicy::ATTACHED
752   // 2. mTextureId != TextureManager::INVALID_TEXTURE_ID
753   //  - If ReleasePolicy is DESTROYED, InitializeRenderer called every on stage called.
754   //  - Then every resources those contained in Visual are Reset but mTextureId is remained when the Off stage time,
755   //  - So, mTextures needed to be get from texture manager to created resources like mImpl->mRenderer.
756   if(!mTextures)
757   {
758     if(mTextureId == TextureManager::INVALID_TEXTURE_ID)
759     {
760       LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::CACHED);
761     }
762     else
763     {
764       mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
765       if(!(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED) &&
766          mTextures &&
767          (mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT))
768       {
769         Sampler sampler = Sampler::New();
770         sampler.SetWrapMode(mWrapModeU, mWrapModeV);
771         mTextures.SetSampler(0u, sampler);
772       }
773     }
774   }
775
776   if(mTextures)
777   {
778     mImpl->mRenderer.SetTextures(mTextures);
779     ComputeTextureSize();
780     CheckMaskTexture();
781
782     bool needToUpdateShader = DevelTexture::IsNative(mTextures.GetTexture(0)) || mUseBrokenImageRenderer;
783
784     if(mTextures.GetTextureCount() == 3)
785     {
786       if(mTextures.GetTexture(0).GetPixelFormat() == Pixel::L8 && mTextures.GetTexture(1).GetPixelFormat() == Pixel::CHROMINANCE_U && mTextures.GetTexture(2).GetPixelFormat() == Pixel::CHROMINANCE_V)
787       {
788         mNeedYuvToRgb      = true;
789         needToUpdateShader = true;
790       }
791     }
792
793     if(needToUpdateShader)
794     {
795       UpdateShader();
796     }
797     mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
798
799     if(DALI_UNLIKELY(mUseBrokenImageRenderer))
800     {
801       // We need to re-generate geometry only if it was broken image before, and result changed after Reload.
802       auto geometry = GenerateGeometry(mTextureId, true);
803
804       // Update geometry only if we need.
805       if(geometry)
806       {
807         mImpl->mRenderer.SetGeometry(geometry);
808       }
809     }
810
811     // We don't use broken image anymore.
812     mUseBrokenImageRenderer = false;
813   }
814
815   if(attemptAtlasing) // the texture is packed inside atlas
816   {
817     mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
818
819     bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
820
821     if(!defaultWrapMode) // custom wrap mode
822     {
823       Vector2 wrapMode(mWrapModeU - WrapMode::CLAMP_TO_EDGE, mWrapModeV - WrapMode::CLAMP_TO_EDGE);
824       wrapMode.Clamp(Vector2::ZERO, Vector2(2.f, 2.f));
825       mImpl->mRenderer.RegisterProperty(WRAP_MODE_UNIFORM_NAME, wrapMode);
826     }
827   }
828 }
829
830 void ImageVisual::DoSetOnScene(Actor& actor)
831 {
832   if(mImageUrl.IsValid())
833   {
834     InitializeRenderer();
835   }
836
837   if(!mImpl->mRenderer)
838   {
839     return;
840   }
841
842   mPlacementActor = actor;
843
844   if(mPixelArea != FULL_TEXTURE_RECT)
845   {
846     mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
847   }
848
849   if(mLoadState == TextureManager::LoadState::LOAD_FINISHED)
850   {
851     actor.AddRenderer(mImpl->mRenderer);
852     mRendererAdded = true;
853     mPlacementActor.Reset();
854
855     // Image loaded and ready to display
856     ResourceReady(Toolkit::Visual::ResourceStatus::READY);
857   }
858   else if(mLoadState == TextureManager::LoadState::LOAD_FAILED)
859   {
860     ShowBrokenImage();
861     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
862   }
863   else
864   {
865     if(mFastTrackLoadingTask)
866     {
867       actor.AddRenderer(mImpl->mRenderer);
868       mRendererAdded = true;
869     }
870   }
871 }
872
873 void ImageVisual::DoSetOffScene(Actor& actor)
874 {
875   // Visual::Base::SetOffScene only calls DoSetOffScene if mRenderer exists (is on onstage)
876
877   // Image release is dependent on the ReleasePolicy, renderer is removed.
878   actor.RemoveRenderer(mImpl->mRenderer);
879   mRendererAdded = false;
880
881   if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
882   {
883     ResetRenderer();
884   }
885
886   mPlacementActor.Reset();
887 }
888
889 void ImageVisual::DoCreatePropertyMap(Property::Map& map) const
890 {
891   map.Clear();
892   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
893
894   bool sync = IsSynchronousLoadingRequired();
895   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
896   if(mImageUrl.IsValid())
897   {
898     map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
899     map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
900     map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
901   }
902
903   map.Insert(Toolkit::ImageVisual::Property::FITTING_MODE, mFittingMode);
904   map.Insert(Toolkit::ImageVisual::Property::SAMPLING_MODE, mSamplingMode);
905
906   if(mImpl->mRenderer && mPixelAreaIndex != Property::INVALID_INDEX)
907   {
908     // Update values from Renderer
909     Vector4 pixelArea = mImpl->mRenderer.GetProperty<Vector4>(mPixelAreaIndex);
910     map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, pixelArea);
911   }
912   else
913   {
914     map.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea);
915   }
916
917   map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU);
918   map.Insert(Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV);
919
920   map.Insert(Toolkit::ImageVisual::Property::ATLASING, mAttemptAtlasing);
921
922   if(mMaskingData != NULL)
923   {
924     map.Insert(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mMaskingData->mAlphaMaskUrl.GetUrl());
925     map.Insert(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, mMaskingData->mContentScaleFactor);
926     map.Insert(Toolkit::ImageVisual::Property::CROP_TO_MASK, mMaskingData->mCropToMask);
927     map.Insert(Toolkit::DevelImageVisual::Property::MASKING_TYPE, mMaskingData->mPreappliedMasking ? DevelImageVisual::MaskingType::MASKING_ON_LOADING : DevelImageVisual::MaskingType::MASKING_ON_RENDERING);
928   }
929
930   map.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, mLoadPolicy);
931   map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
932   map.Insert(Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION, mOrientationCorrection);
933
934   map.Insert(Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING, mUseFastTrackUploading);
935 }
936
937 void ImageVisual::DoCreateInstancePropertyMap(Property::Map& map) const
938 {
939   map.Clear();
940   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
941   if(mImageUrl.IsValid())
942   {
943     map.Insert(Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth());
944     map.Insert(Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight());
945   }
946 }
947
948 void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::Property::Value& attributes)
949 {
950   // Check if action is valid for this visual type and perform action if possible
951
952   switch(actionId)
953   {
954     case DevelImageVisual::Action::RELOAD:
955     {
956       auto attemptAtlasing = AttemptAtlasing();
957
958       // Reset resource ready status when we call reload.
959       ResourceReady(Toolkit::Visual::ResourceStatus::PREPARING);
960       mLoadState = TextureManager::LoadState::NOT_STARTED;
961
962       LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::FORCED);
963       break;
964     }
965   }
966 }
967
968 void ImageVisual::OnSetTransform()
969 {
970   if(mImpl->mRenderer)
971   {
972     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
973   }
974 }
975
976 void ImageVisual::UpdateShader()
977 {
978   if(mImpl->mRenderer)
979   {
980     Shader shader = GenerateShader();
981     mImpl->mRenderer.SetShader(shader);
982   }
983 }
984
985 // From existing atlas manager
986 void ImageVisual::UploadCompleted()
987 {
988   // Texture has been uploaded. If weak handle is holding a placement actor,
989   // it is the time to add the renderer to actor.
990   Actor actor = mPlacementActor.GetHandle();
991   if(actor)
992   {
993     mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
994     actor.AddRenderer(mImpl->mRenderer);
995     mRendererAdded = true;
996     // reset the weak handle so that the renderer only get added to actor once
997     mPlacementActor.Reset();
998   }
999
1000   // Image loaded
1001   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
1002   mLoadState = TextureManager::LoadState::LOAD_FINISHED;
1003 }
1004
1005 // From FastTrackLoadingTask
1006 void ImageVisual::FastLoadComplete(FastTrackLoadingTaskPtr task)
1007 {
1008   Toolkit::Visual::ResourceStatus resourceStatus;
1009
1010   DALI_ASSERT_ALWAYS(mFastTrackLoadingTask == task && "Task was not canceled successfully!");
1011   DALI_ASSERT_ALWAYS(mRendererAdded && "Some FastTrack logic missed!");
1012
1013   Actor actor = mPlacementActor.GetHandle();
1014
1015   if(mFastTrackLoadingTask && mFastTrackLoadingTask->mLoadSuccess)
1016   {
1017     resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1018     mLoadState     = TextureManager::LoadState::LOAD_FINISHED;
1019
1020     // Change premultiplied alpha flag after change renderer.
1021     EnablePreMultipliedAlpha(mFastTrackLoadingTask->mPremultiplied);
1022
1023     if(mFastTrackLoadingTask->mLoadPlanesAvaliable)
1024     {
1025       if(mFastTrackLoadingTask->mPlanesLoaded)
1026       {
1027         // Let we use regular yuv cases.
1028         mNeedYuvToRgb = true;
1029       }
1030       else
1031       {
1032         // Let we use regular image cases.
1033         mNeedYuvToRgb = false;
1034
1035         auto textureSet = mImpl->mRenderer.GetTextures();
1036         DALI_ASSERT_ALWAYS(textureSet && textureSet.GetTextureCount() > 0u && "Previous texture set must exist!");
1037
1038         Dali::TextureSet newTextureSet = TextureSet::New();
1039         newTextureSet.SetTexture(0u, textureSet.GetTexture(0u));
1040         mImpl->mRenderer.SetTextures(newTextureSet);
1041       }
1042
1043       // We can specify what kind of shader we need to use now. Update shader.
1044       mNeedUnifiedYuvAndRgb = false;
1045       UpdateShader();
1046     }
1047   }
1048   else
1049   {
1050     resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1051     mLoadState     = TextureManager::LoadState::LOAD_FAILED;
1052
1053     // Change renderer as broken.
1054     ShowBrokenImage();
1055   }
1056
1057   mFastTrackLoadingTask.Reset();
1058
1059   // Signal to observers ( control ) that resources are ready. Must be all resources.
1060   ResourceReady(resourceStatus);
1061 }
1062
1063 // From Texture Manager
1064 void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
1065 {
1066   Toolkit::Visual::ResourceStatus resourceStatus;
1067
1068   if(mImpl->mRenderer)
1069   {
1070     EnablePreMultipliedAlpha(textureInformation.preMultiplied);
1071
1072     Actor actor = mPlacementActor.GetHandle();
1073     if(!loadingSuccess)
1074     {
1075       ShowBrokenImage();
1076       textureInformation.textureSet = mImpl->mRenderer.GetTextures();
1077     }
1078     else
1079     {
1080       if(mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT)
1081       {
1082         Sampler sampler = Sampler::New();
1083         sampler.SetWrapMode(mWrapModeU, mWrapModeV);
1084         textureInformation.textureSet.SetSampler(0u, sampler);
1085       }
1086
1087       mImpl->mRenderer.SetTextures(textureInformation.textureSet);
1088       ComputeTextureSize();
1089       CheckMaskTexture();
1090
1091       bool needToUpdateShader = mUseBrokenImageRenderer;
1092
1093       if(textureInformation.textureSet.GetTextureCount() == 3)
1094       {
1095         if(textureInformation.textureSet.GetTexture(0).GetPixelFormat() == Pixel::L8 && textureInformation.textureSet.GetTexture(1).GetPixelFormat() == Pixel::CHROMINANCE_U && textureInformation.textureSet.GetTexture(2).GetPixelFormat() == Pixel::CHROMINANCE_V)
1096         {
1097           mNeedYuvToRgb      = true;
1098           needToUpdateShader = true;
1099         }
1100       }
1101
1102       if(needToUpdateShader)
1103       {
1104         UpdateShader();
1105       }
1106
1107       if(actor)
1108       {
1109         actor.AddRenderer(mImpl->mRenderer);
1110         mRendererAdded = true;
1111         // reset the weak handle so that the renderer only get added to actor once
1112         mPlacementActor.Reset();
1113       }
1114
1115       auto geometry = GenerateGeometry(textureInformation.textureId, mUseBrokenImageRenderer);
1116
1117       if(DALI_UNLIKELY(geometry))
1118       {
1119         // Rare cases. If load successed image don't use quad geometry (i.e. Show some n-patch broken image, and call Reload(), and success)
1120         // or If given texture use AddOn,
1121         // then we need to make to use quad geometry and update shader agian.
1122         mImpl->mRenderer.SetGeometry(geometry);
1123       }
1124
1125       // We don't use broken image anymore.
1126       mUseBrokenImageRenderer = false;
1127     }
1128   }
1129
1130   // Storing TextureSet needed when renderer staged.
1131   if(!mImpl->mRenderer)
1132   {
1133     mTextures = textureInformation.textureSet;
1134   }
1135
1136   // Image loaded, set status regardless of staged status.
1137   if(loadingSuccess)
1138   {
1139     resourceStatus = Toolkit::Visual::ResourceStatus::READY;
1140     mLoadState     = TextureManager::LoadState::LOAD_FINISHED;
1141   }
1142   else
1143   {
1144     resourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
1145     mLoadState     = TextureManager::LoadState::LOAD_FAILED;
1146   }
1147
1148   // Signal to observers ( control ) that resources are ready. Must be all resources.
1149   ResourceReady(resourceStatus);
1150 }
1151
1152 void ImageVisual::RemoveTexture()
1153 {
1154   if(mTextureId != TextureManager::INVALID_TEXTURE_ID)
1155   {
1156     mFactoryCache.GetTextureManager().RequestRemove(mTextureId, this);
1157     mTextureId = TextureManager::INVALID_TEXTURE_ID;
1158   }
1159   else
1160   {
1161     ResetFastTrackLoadingTask();
1162
1163     Vector4         atlasRect(0.f, 0.f, 1.f, 1.f);
1164     Property::Index index = mImpl->mRenderer.GetPropertyIndex(ATLAS_RECT_UNIFORM_NAME);
1165     if(index != Property::INVALID_INDEX)
1166     {
1167       Property::Value atlasRectValue = mImpl->mRenderer.GetProperty(index);
1168       atlasRectValue.Get(atlasRect);
1169     }
1170
1171     TextureSet textureSet = mImpl->mRenderer.GetTextures();
1172
1173     if(index != Property::INVALID_INDEX)
1174     {
1175       mFactoryCache.GetAtlasManager()->Remove(textureSet, atlasRect);
1176     }
1177   }
1178 }
1179
1180 void ImageVisual::ComputeTextureSize()
1181 {
1182   if(mImpl->mRenderer)
1183   {
1184     auto textureSet = mImpl->mRenderer.GetTextures();
1185     if(textureSet && textureSet.GetTextureCount())
1186     {
1187       auto texture = textureSet.GetTexture(0);
1188       if(texture)
1189       {
1190         mTextureSize.x = texture.GetWidth();
1191         mTextureSize.y = texture.GetHeight();
1192         if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1193         {
1194           Texture maskTexture = textureSet.GetTexture(1);
1195           if(maskTexture)
1196           {
1197             mTextureSize.x = std::min(static_cast<uint32_t>(mTextureSize.x * mMaskingData->mContentScaleFactor), maskTexture.GetWidth());
1198             mTextureSize.y = std::min(static_cast<uint32_t>(mTextureSize.y * mMaskingData->mContentScaleFactor), maskTexture.GetHeight());
1199           }
1200         }
1201       }
1202     }
1203   }
1204 }
1205
1206 Vector2 ImageVisual::ComputeMaskTextureRatio()
1207 {
1208   Vector2 maskTextureRatio;
1209   if(mImpl->mRenderer)
1210   {
1211     auto textureSet = mImpl->mRenderer.GetTextures();
1212     if(textureSet && textureSet.GetTextureCount())
1213     {
1214       auto texture = textureSet.GetTexture(0);
1215       if(texture)
1216       {
1217         if(textureSet.GetTextureCount() > 1u && mMaskingData && !mMaskingData->mPreappliedMasking && mMaskingData->mCropToMask)
1218         {
1219           Texture maskTexture = textureSet.GetTexture(1);
1220           if(maskTexture)
1221           {
1222             float textureWidth  = std::max(static_cast<float>(texture.GetWidth() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1223             float textureHeight = std::max(static_cast<float>(texture.GetHeight() * mMaskingData->mContentScaleFactor), Dali::Math::MACHINE_EPSILON_1);
1224             maskTextureRatio    = Vector2(std::min(static_cast<float>(maskTexture.GetWidth()), textureWidth) / textureWidth,
1225                                        std::min(static_cast<float>(maskTexture.GetHeight()), textureHeight) / textureHeight);
1226           }
1227         }
1228       }
1229     }
1230   }
1231   return maskTextureRatio;
1232 }
1233
1234 Shader ImageVisual::GenerateShader() const
1235 {
1236   Shader shader;
1237
1238   const bool useStandardShader = !mImpl->mCustomShader;
1239   const bool useNativeImage    = (mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)));
1240
1241   if(useStandardShader)
1242   {
1243     bool requiredAlphaMaskingOnRendering = (mMaskingData && !mMaskingData->mMaskImageLoadingFailed) ? !mMaskingData->mPreappliedMasking : false;
1244     // Create and cache the standard shader
1245     shader = mImageVisualShaderFactory.GetShader(
1246       mFactoryCache,
1247       ImageVisualShaderFeatureBuilder()
1248         .EnableTextureAtlas(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED && !useNativeImage)
1249         .ApplyDefaultTextureWrapMode(mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE)
1250         .EnableRoundedCorner(IsRoundedCornerRequired())
1251         .EnableBorderline(IsBorderlineRequired())
1252         .SetTextureForFragmentShaderCheck(useNativeImage ? mTextures.GetTexture(0) : Dali::Texture())
1253         .EnableAlphaMaskingOnRendering(requiredAlphaMaskingOnRendering)
1254         .EnableYuvToRgb(mNeedYuvToRgb, mNeedUnifiedYuvAndRgb));
1255   }
1256   else
1257   {
1258     bool             usesWholeTexture = true;
1259     std::string_view vertexShaderView;
1260     std::string_view fragmentShaderView;
1261
1262     if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
1263     {
1264       vertexShaderView = mImpl->mCustomShader->mVertexShader;
1265       usesWholeTexture = false; // Impossible to tell.
1266     }
1267     else
1268     {
1269       vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
1270     }
1271
1272     if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
1273     {
1274       fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
1275     }
1276     else
1277     {
1278       fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
1279     }
1280
1281     // If the texture is native, we may need to change prefix and sampler in
1282     // the fragment shader
1283     if(useNativeImage)
1284     {
1285       bool        modifiedFragmentShader = false;
1286       Texture     nativeTexture          = mTextures.GetTexture(0);
1287       std::string fragmentShaderString   = std::string(fragmentShaderView);
1288
1289       modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
1290       if(modifiedFragmentShader)
1291       {
1292         fragmentShaderView = fragmentShaderString;
1293       }
1294
1295       // Create shader here cause fragmentShaderString scope issue
1296       shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1297     }
1298     else
1299     {
1300       shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
1301     }
1302
1303     if(usesWholeTexture)
1304     {
1305       shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
1306     }
1307   }
1308
1309   return shader;
1310 }
1311
1312 Dali::Property ImageVisual::OnGetPropertyObject(Dali::Property::Key key)
1313 {
1314   if((key.type == Property::Key::INDEX && key.indexKey == Toolkit::ImageVisual::Property::PIXEL_AREA) || (key.type == Property::Key::STRING && key.stringKey == PIXEL_AREA_UNIFORM_NAME))
1315   {
1316     if(DALI_LIKELY(mImpl->mRenderer))
1317     {
1318       if(mPixelAreaIndex == Property::INVALID_INDEX)
1319       {
1320         mPixelAreaIndex = mImpl->mRenderer.RegisterProperty(mPixelAreaIndex, PIXEL_AREA_UNIFORM_NAME, mPixelArea);
1321       }
1322       return Dali::Property(mImpl->mRenderer, mPixelAreaIndex);
1323     }
1324   }
1325
1326   Handle handle;
1327   return Dali::Property(handle, Property::INVALID_INDEX);
1328 }
1329
1330 void ImageVisual::CheckMaskTexture()
1331 {
1332   if(mMaskingData && !mMaskingData->mPreappliedMasking)
1333   {
1334     bool       maskLoadFailed = true;
1335     TextureSet textures       = mImpl->mRenderer.GetTextures();
1336     if(textures && textures.GetTextureCount() >= TEXTURE_COUNT_FOR_GPU_ALPHA_MASK)
1337     {
1338       if(mMaskingData->mCropToMask)
1339       {
1340         mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, ComputeMaskTextureRatio());
1341       }
1342       else
1343       {
1344         mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, Vector2::ONE);
1345       }
1346       maskLoadFailed = false;
1347     }
1348
1349     if(mMaskingData->mMaskImageLoadingFailed != maskLoadFailed)
1350     {
1351       mMaskingData->mMaskImageLoadingFailed = maskLoadFailed;
1352       UpdateShader();
1353     }
1354   }
1355 }
1356
1357 void ImageVisual::ResetRenderer()
1358 {
1359   RemoveTexture(); // If INVALID_TEXTURE_ID then removal will be attempted on atlas
1360   mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
1361
1362   TextureSet textureSet = TextureSet::New();
1363   mImpl->mRenderer.SetTextures(textureSet);
1364   ComputeTextureSize();
1365
1366   mLoadState = TextureManager::LoadState::NOT_STARTED;
1367 }
1368
1369 void ImageVisual::ShowBrokenImage()
1370 {
1371   if(mEnableBrokenImage)
1372   {
1373     Actor actor = mPlacementActor.GetHandle();
1374
1375     Vector2 imageSize = Vector2::ZERO;
1376     if(actor)
1377     {
1378       imageSize           = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
1379       mPlacementActorSize = imageSize;
1380
1381       if(mRendererAdded)
1382       {
1383         actor.RemoveRenderer(mImpl->mRenderer);
1384         mRendererAdded = false;
1385       }
1386     }
1387
1388     mUseBrokenImageRenderer = true;
1389     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
1390     if(actor)
1391     {
1392       actor.AddRenderer(mImpl->mRenderer);
1393       mRendererAdded = true;
1394       mPlacementActor.Reset();
1395     }
1396   }
1397   else
1398   {
1399     if(mRendererAdded)
1400     {
1401       Actor actor = mPlacementActor.GetHandle();
1402       if(actor)
1403       {
1404         actor.RemoveRenderer(mImpl->mRenderer);
1405         mRendererAdded = false;
1406       }
1407     }
1408     ResetRenderer();
1409   }
1410 }
1411
1412 void ImageVisual::ResetFastTrackLoadingTask()
1413 {
1414   if(mFastTrackLoadingTask)
1415   {
1416     Dali::AsyncTaskManager::Get().RemoveTask(mFastTrackLoadingTask);
1417     mFastTrackLoadingTask.Reset();
1418   }
1419 }
1420
1421 Geometry ImageVisual::GenerateGeometry(TextureManager::TextureId textureId, bool createForce)
1422 {
1423   Geometry geometry;
1424   if(Stage::IsInstalled())
1425   {
1426     if(mImpl->mCustomShader)
1427     {
1428       if(createForce)
1429       {
1430         geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
1431       }
1432     }
1433     else
1434     {
1435       uint32_t firstElementCount{0u};
1436       uint32_t secondElementCount{0u};
1437
1438       geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(textureId, firstElementCount, secondElementCount);
1439       if(geometry)
1440       {
1441         if(mImpl->mRenderer)
1442         {
1443           Dali::DevelRenderer::DrawCommand drawCommand{};
1444           drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
1445
1446           if(firstElementCount)
1447           {
1448             drawCommand.firstIndex   = 0;
1449             drawCommand.elementCount = firstElementCount;
1450             drawCommand.queue        = DevelRenderer::RENDER_QUEUE_OPAQUE;
1451             DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
1452           }
1453
1454           if(secondElementCount)
1455           {
1456             drawCommand.firstIndex   = firstElementCount;
1457             drawCommand.elementCount = secondElementCount;
1458             drawCommand.queue        = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
1459             DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
1460           }
1461         }
1462       }
1463       else if(createForce)
1464       {
1465         // Create default quad geometry now
1466         geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
1467       }
1468     }
1469   }
1470
1471   return geometry;
1472 }
1473
1474 } // namespace Internal
1475
1476 } // namespace Toolkit
1477
1478 } // namespace Dali