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