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