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