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