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