Caching texture instead of textureSet in TextureManager
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
1 /*
2  * Copyright (c) 2022 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 "npatch-visual.h"
20
21 // EXTERNAL INCLUDES
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/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/utility/npatch-helper.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
33 #include <dali-toolkit/internal/visuals/npatch-loader.h>
34 #include <dali-toolkit/internal/visuals/rendering-addon.h>
35 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
38 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
40 #include <dali-toolkit/public-api/visuals/visual-properties.h>
41
42 namespace Dali
43 {
44 namespace Toolkit
45 {
46 namespace Internal
47 {
48 namespace
49 {
50 const int CUSTOM_PROPERTY_COUNT(5); // fixed(3),stretch,aux
51 }
52
53 /////////////////NPatchVisual////////////////
54
55 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
56 {
57   NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
58   nPatchVisual->mImageUrl = imageUrl;
59   nPatchVisual->SetProperties(properties);
60   nPatchVisual->Initialize();
61   return nPatchVisual;
62 }
63
64 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
65 {
66   NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
67   nPatchVisual->mImageUrl = imageUrl;
68   nPatchVisual->Initialize();
69   return nPatchVisual;
70 }
71
72 void NPatchVisual::LoadImages()
73 {
74   TextureManager& textureManager     = mFactoryCache.GetTextureManager();
75   bool            synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
76
77   if(mId == NPatchData::INVALID_NPATCH_DATA_ID && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()))
78   {
79     bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
80     mId                    = mLoader.Load(textureManager, this, mImageUrl, mBorder, preMultiplyOnLoad, synchronousLoading);
81
82     const NPatchData* data;
83     if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
84     {
85       EnablePreMultipliedAlpha(data->IsPreMultiplied());
86     }
87   }
88
89   if(mAuxiliaryTextureId == TextureManager::INVALID_TEXTURE_ID && mAuxiliaryUrl.IsValid() && (mAuxiliaryUrl.IsLocalResource() || mAuxiliaryUrl.IsBufferResource()))
90   {
91     auto preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader
92                                ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD
93                                : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
94
95     TextureManager::MaskingDataPointer maskingDataPtr       = nullptr;
96     ImageAtlasManagerPtr               imageAtlasManagerPtr = nullptr;
97
98     bool atlasing      = false;
99     auto atlasRect     = Vector4::ZERO;
100     auto atlasRectSize = Dali::ImageDimensions();
101
102     bool loadingStatus = false;
103
104     // Load the auxiliary image
105     mAuxiliaryTextureSet = textureManager.LoadTexture(mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, maskingDataPtr, synchronousLoading, mAuxiliaryTextureId, atlasRect, atlasRectSize, atlasing, loadingStatus, this, nullptr, imageAtlasManagerPtr, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad);
106
107     if(mAuxiliaryTextureSet)
108     {
109       Sampler sampler = Sampler::New();
110       sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
111       mAuxiliaryTextureSet.SetSampler(0u, sampler);
112     }
113
114     // If synchronousLoading is true, we can check the auxiliaryResource's status now.
115     if(synchronousLoading)
116     {
117       mAuxiliaryResourceStatus = (mAuxiliaryTextureSet && mAuxiliaryTextureSet.GetTextureCount() > 0u) ? Toolkit::Visual::ResourceStatus::READY : Toolkit::Visual::ResourceStatus::FAILED;
118     }
119   }
120 }
121
122 void NPatchVisual::GetNaturalSize(Vector2& naturalSize)
123 {
124   naturalSize.x = 0u;
125   naturalSize.y = 0u;
126
127   // load now if not already loaded
128   const NPatchData* data;
129   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
130   {
131     naturalSize.x = data->GetCroppedWidth();
132     naturalSize.y = data->GetCroppedHeight();
133   }
134   else
135   {
136     if(mImageUrl.IsValid())
137     {
138       ImageDimensions dimensions = Dali::GetOriginalImageSize(mImageUrl.GetUrl());
139       if(dimensions != ImageDimensions(0, 0))
140       {
141         naturalSize.x = dimensions.GetWidth();
142         naturalSize.y = dimensions.GetHeight();
143       }
144     }
145   }
146
147   if(mAuxiliaryTextureSet && mAuxiliaryTextureSet.GetTextureCount() > 0u)
148   {
149     naturalSize.x = std::max(naturalSize.x, float(mAuxiliaryTextureSet.GetTexture(0u).GetWidth()));
150     naturalSize.y = std::max(naturalSize.y, float(mAuxiliaryTextureSet.GetTexture(0u).GetHeight()));
151   }
152 }
153
154 void NPatchVisual::DoSetProperties(const Property::Map& propertyMap)
155 {
156   // URL is already passed in via constructor
157
158   Property::Value* borderOnlyValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY);
159   if(borderOnlyValue)
160   {
161     borderOnlyValue->Get(mBorderOnly);
162   }
163
164   Property::Value* borderValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER, BORDER);
165   if(borderValue && !borderValue->Get(mBorder)) // If value exists and is rect, just set mBorder
166   {
167     // Not a rect so try vector4
168     Vector4 border;
169     if(borderValue->Get(border))
170     {
171       mBorder.left   = static_cast<int>(border.x);
172       mBorder.right  = static_cast<int>(border.y);
173       mBorder.bottom = static_cast<int>(border.z);
174       mBorder.top    = static_cast<int>(border.w);
175     }
176   }
177
178   Property::Value* auxImage = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME);
179   if(auxImage)
180   {
181     std::string url;
182     if(auxImage->Get(url))
183     {
184       mAuxiliaryUrl = url;
185     }
186   }
187
188   Property::Value* auxImageAlpha = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME);
189   if(auxImageAlpha)
190   {
191     auxImageAlpha->Get(mAuxiliaryImageAlpha);
192   }
193
194   Property::Value* synchronousLoading = propertyMap.Find(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING);
195   if(synchronousLoading)
196   {
197     bool sync = false;
198     synchronousLoading->Get(sync);
199     if(sync)
200     {
201       mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
202     }
203     else
204     {
205       mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
206     }
207   }
208
209   Property::Value* releasePolicy = propertyMap.Find(Toolkit::ImageVisual::Property::RELEASE_POLICY, RELEASE_POLICY_NAME);
210   if(releasePolicy)
211   {
212     releasePolicy->Get(mReleasePolicy);
213   }
214 }
215
216 void NPatchVisual::DoSetOnScene(Actor& actor)
217 {
218   // load when first go on stage
219   LoadImages();
220
221   // Set mPlacementActor now, because some case, LoadImages can use this information in LoadComplete API.
222   // at this case, we try to SetResouce to mPlaceActor twice. so, we should avoid that case.
223   mPlacementActor = actor;
224
225   const NPatchData* data;
226   if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
227   {
228     // If mAuxiliaryUrl need to be loaded, we should wait it until LoadComplete called.
229     if(!mAuxiliaryUrl.IsValid() || mAuxiliaryResourceStatus != Toolkit::Visual::ResourceStatus::PREPARING)
230     {
231       SetResource();
232     }
233   }
234 }
235
236 void NPatchVisual::DoSetOffScene(Actor& actor)
237 {
238   if(mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
239   {
240     if(mId != NPatchData::INVALID_NPATCH_DATA_ID)
241     {
242       mLoader.Remove(mId, this);
243       mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
244       mId                    = NPatchData::INVALID_NPATCH_DATA_ID;
245     }
246     if(mAuxiliaryTextureId != TextureManager::INVALID_TEXTURE_ID)
247     {
248       TextureManager& textureManager = mFactoryCache.GetTextureManager();
249       textureManager.Remove(mAuxiliaryTextureId, this);
250       mAuxiliaryTextureId      = TextureManager::INVALID_TEXTURE_ID;
251       mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
252       mAuxiliaryTextureSet.Reset();
253     }
254   }
255
256   actor.RemoveRenderer(mImpl->mRenderer);
257   mPlacementActor.Reset();
258 }
259
260 void NPatchVisual::OnSetTransform()
261 {
262   if(mImpl->mRenderer)
263   {
264     mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
265   }
266 }
267
268 void NPatchVisual::DoCreatePropertyMap(Property::Map& map) const
269 {
270   map.Clear();
271   bool sync = IsSynchronousLoadingRequired();
272   map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
273   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH);
274   map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
275   map.Insert(Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly);
276   map.Insert(Toolkit::ImageVisual::Property::BORDER, mBorder);
277   map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
278
279   if(mAuxiliaryUrl.IsValid())
280   {
281     map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
282     map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
283   }
284 }
285
286 void NPatchVisual::DoCreateInstancePropertyMap(Property::Map& map) const
287 {
288   if(mAuxiliaryUrl.IsValid())
289   {
290     map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
291     map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
292   }
293 }
294
295 NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory)
296 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH),
297   mPlacementActor(),
298   mLoader(factoryCache.GetNPatchLoader()),
299   mImageVisualShaderFactory(shaderFactory),
300   mImageUrl(),
301   mAuxiliaryUrl(),
302   mId(NPatchData::INVALID_NPATCH_DATA_ID),
303   mAuxiliaryTextureSet(),
304   mAuxiliaryTextureId(TextureManager::INVALID_TEXTURE_ID),
305   mAuxiliaryResourceStatus(Toolkit::Visual::ResourceStatus::PREPARING),
306   mBorderOnly(false),
307   mBorder(),
308   mAuxiliaryImageAlpha(0.0f),
309   mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED)
310 {
311   EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
312 }
313
314 NPatchVisual::~NPatchVisual()
315 {
316   if(Stage::IsInstalled())
317   {
318     if(mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER)
319     {
320       if(mId != NPatchData::INVALID_NPATCH_DATA_ID)
321       {
322         mLoader.Remove(mId, this);
323         mId = NPatchData::INVALID_NPATCH_DATA_ID;
324       }
325       if(mAuxiliaryTextureId != TextureManager::INVALID_TEXTURE_ID)
326       {
327         TextureManager& textureManager = mFactoryCache.GetTextureManager();
328         textureManager.Remove(mAuxiliaryTextureId, this);
329         mAuxiliaryTextureId = TextureManager::INVALID_TEXTURE_ID;
330         mAuxiliaryTextureSet.Reset();
331       }
332     }
333   }
334 }
335
336 void NPatchVisual::OnInitialize()
337 {
338   // Get basic geometry and shader
339   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
340   Shader   shader   = mImageVisualShaderFactory.GetShader(
341     mFactoryCache,
342     ImageVisualShaderFeature::FeatureBuilder());
343
344   mImpl->mRenderer = VisualRenderer::New(geometry, shader);
345   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
346
347   //Register transform properties
348   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
349 }
350
351 Geometry NPatchVisual::CreateGeometry()
352 {
353   Geometry          geometry;
354   const NPatchData* data;
355   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
356   {
357     if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
358     {
359       if(DALI_UNLIKELY(mBorderOnly))
360       {
361         geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY);
362       }
363       else
364       {
365         if(data->GetRenderingMap())
366         {
367           uint32_t elementCount[2];
368           geometry = RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), Uint16Pair(3, 3), elementCount);
369           if(mImpl->mRenderer)
370           {
371             RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
372           }
373         }
374         else
375         {
376           geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
377         }
378       }
379     }
380     else if(data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
381     {
382       Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1);
383       if(!data->GetRenderingMap())
384       {
385         geometry = !mBorderOnly ? NPatchHelper::CreateGridGeometry(gridSize) : NPatchHelper::CreateBorderGeometry(gridSize);
386       }
387       else
388       {
389         uint32_t elementCount[2];
390         geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : NPatchHelper::CreateBorderGeometry(gridSize);
391         if(mImpl->mRenderer)
392         {
393           RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
394         }
395       }
396     }
397   }
398   else
399   {
400     // no N patch data so use default geometry
401     geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
402   }
403   return geometry;
404 }
405
406 Shader NPatchVisual::CreateShader()
407 {
408   Shader            shader;
409   const NPatchData* data;
410   // 0 is either no data (load failed?) or no stretch regions on image
411   // for both cases we use the default shader
412   NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
413   NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
414
415   auto fragmentShader = mAuxiliaryResourceStatus == Toolkit::Visual::ResourceStatus::READY ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
416                                                                                            : SHADER_NPATCH_VISUAL_SHADER_FRAG;
417   auto shaderType = mAuxiliaryResourceStatus == Toolkit::Visual::ResourceStatus::READY ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
418                                                                                        : VisualFactoryCache::NINE_PATCH_SHADER;
419
420   // ask loader for the regions
421   if(mLoader.GetNPatchData(mId, data))
422   {
423     xStretchCount = data->GetStretchPixelsX().Count();
424     yStretchCount = data->GetStretchPixelsY().Count();
425   }
426
427   if(DALI_LIKELY(!mImpl->mCustomShader))
428   {
429     if(DALI_LIKELY((xStretchCount == 1 && yStretchCount == 1) ||
430                    (xStretchCount == 0 && yStretchCount == 0)))
431     {
432       shader = mFactoryCache.GetShader(shaderType);
433       if(DALI_UNLIKELY(!shader))
434       {
435         shader = Shader::New(SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader);
436         // Only cache vanilla 9 patch shaders
437         mFactoryCache.SaveShader(shaderType, shader);
438       }
439     }
440     else if(xStretchCount > 0 || yStretchCount > 0)
441     {
442       std::stringstream vertexShader;
443       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
444                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
445                    << SHADER_NPATCH_VISUAL_SHADER_VERT;
446
447       shader = Shader::New(vertexShader.str(), fragmentShader);
448     }
449   }
450   else
451   {
452     Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
453
454     if(!mImpl->mCustomShader->mFragmentShader.empty())
455     {
456       fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
457     }
458     hints = mImpl->mCustomShader->mHints;
459
460     /* Apply Custom Vertex Shader only if image is 9-patch */
461     if((xStretchCount == 1 && yStretchCount == 1) ||
462        (xStretchCount == 0 && yStretchCount == 0))
463     {
464       const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
465
466       if(!mImpl->mCustomShader->mVertexShader.empty())
467       {
468         vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
469       }
470       shader = Shader::New(vertexShader, fragmentShader, hints);
471     }
472     else if(xStretchCount > 0 || yStretchCount > 0)
473     {
474       std::stringstream vertexShader;
475       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
476                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
477                    << SHADER_NPATCH_VISUAL_SHADER_VERT;
478
479       shader = Shader::New(vertexShader.str(), fragmentShader, hints);
480     }
481   }
482
483   return shader;
484 }
485
486 void NPatchVisual::ApplyTextureAndUniforms()
487 {
488   const NPatchData* data;
489   TextureSet        textureSet;
490
491   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
492   {
493     textureSet = data->GetTextures();
494     NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data);
495
496     if(mAuxiliaryResourceStatus == Toolkit::Visual::ResourceStatus::READY)
497     {
498       DALI_ASSERT_ALWAYS(mAuxiliaryTextureId != TextureManager::INVALID_TEXTURE_ID);
499       DALI_ASSERT_ALWAYS(mAuxiliaryTextureSet && mAuxiliaryTextureSet.GetTextureCount() > 0u);
500
501       // TODO : This code exist due to the texture cache manager hold TextureSet, not Texture.
502       // If we call textureSet.SetTexture(1, texture) directly, the cached TextureSet also be changed.
503       // We should make pass utc-Dali-VisualFactory.cpp UtcDaliNPatchVisualAuxiliaryImage02().
504       TextureSet tempTextureSet = TextureSet::New();
505       tempTextureSet.SetTexture(0, textureSet.GetTexture(0));
506       tempTextureSet.SetTexture(1, mAuxiliaryTextureSet.GetTexture(0));
507       textureSet = tempTextureSet;
508
509       mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
510                                         AUXILIARY_IMAGE_ALPHA_NAME,
511                                         mAuxiliaryImageAlpha);
512     }
513     mImpl->mRenderer.SetTextures(textureSet);
514   }
515   else
516   {
517     DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str());
518     Actor   actor     = mPlacementActor.GetHandle();
519     Vector2 imageSize = Vector2::ZERO;
520     if(actor)
521     {
522       imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
523     }
524     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
525   }
526
527   // Register transform properties
528   mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
529 }
530
531 Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType subType)
532 {
533   Geometry geometry = mFactoryCache.GetGeometry(subType);
534   if(!geometry)
535   {
536     if(DALI_LIKELY(VisualFactoryCache::NINE_PATCH_GEOMETRY == subType))
537     {
538       geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3, 3));
539     }
540     else if(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType)
541     {
542       geometry = NPatchHelper::CreateBorderGeometry(Uint16Pair(3, 3));
543     }
544     mFactoryCache.SaveGeometry(subType, geometry);
545   }
546   return geometry;
547 }
548
549 void NPatchVisual::SetResource()
550 {
551   const NPatchData* data;
552   if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data))
553   {
554     Geometry geometry = CreateGeometry();
555     Shader   shader   = CreateShader();
556
557     mImpl->mRenderer.SetGeometry(geometry);
558     mImpl->mRenderer.SetShader(shader);
559
560     if(RenderingAddOn::Get().IsValid())
561     {
562       RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
563     }
564     Actor actor = mPlacementActor.GetHandle();
565     if(actor)
566     {
567       ApplyTextureAndUniforms();
568       actor.AddRenderer(mImpl->mRenderer);
569       mPlacementActor.Reset();
570     }
571
572     // npatch loaded and ready to display
573     if(data->GetLoadingState() != NPatchData::LoadingState::LOAD_COMPLETE)
574     {
575       ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
576     }
577     else
578     {
579       ResourceReady(Toolkit::Visual::ResourceStatus::READY);
580     }
581   }
582 }
583
584 void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInformation)
585 {
586   if(textureInformation.url.length() > 0) // For the Url.
587   {
588     if(DALI_UNLIKELY(mId == NPatchData::INVALID_NPATCH_DATA_ID))
589     {
590       // Special case when mLoader.Load call LoadComplete function before mId setup.
591       // We can overwrite mId.
592       mId = static_cast<NPatchData::NPatchDataId>(textureInformation.textureId);
593     }
594     if(loadSuccess)
595     {
596       EnablePreMultipliedAlpha(textureInformation.preMultiplied);
597     }
598   }
599   else // For the AuxiliaryUrl
600   {
601     if(DALI_UNLIKELY(mAuxiliaryTextureId == TextureManager::INVALID_TEXTURE_ID))
602     {
603       // Special case when TextureManager.LoadTexture call LoadComplete function before mAuxiliaryTextureId setup.
604       // We can overwrite mAuxiliaryTextureId.
605       mAuxiliaryTextureId = textureInformation.textureId;
606     }
607     if(loadSuccess)
608     {
609       mAuxiliaryTextureSet = textureInformation.textureSet;
610       if(mAuxiliaryTextureSet)
611       {
612         Sampler sampler = Sampler::New();
613         sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
614         mAuxiliaryTextureSet.SetSampler(0u, sampler);
615       }
616
617       mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::READY;
618     }
619     else
620     {
621       mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::FAILED;
622     }
623   }
624
625   // If auxiliaryUrl didn't required OR auxiliaryUrl load done.
626   if(!mAuxiliaryUrl.IsValid() || mAuxiliaryResourceStatus != Toolkit::Visual::ResourceStatus::PREPARING)
627   {
628     const NPatchData* data;
629     // and.. If Url loading done.
630     if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
631     {
632       SetResource();
633     }
634   }
635 }
636
637 } // namespace Internal
638
639 } // namespace Toolkit
640
641 } // namespace Dali