Merge "Prevent render of outline, shadow when transparent" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextureManager.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 #include <iostream>
19
20 #include <stdlib.h>
21
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <toolkit-event-thread-callback.h>
24 #include <toolkit-timer.h>
25
26 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
27 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
28 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
29 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-impl.h> ///< For VisualFactory's member TextureManager.
31 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
32
33 #include <test-encoded-image-buffer.h>
34
35 #if defined(ELDBUS_ENABLED)
36 #include <automated-tests/src/dali-toolkit-internal/dali-toolkit-test-utils/dbus-wrapper.h>
37 #endif
38
39 using namespace Dali::Toolkit::Internal;
40
41 void utc_dali_toolkit_texture_manager_startup(void)
42 {
43   setenv("LOG_TEXTURE_MANAGER", "3", 1);
44   test_return_value = TET_UNDEF;
45 #if defined(ELDBUS_ENABLED)
46   DBusWrapper::Install(std::unique_ptr<DBusWrapper>(new TestDBusWrapper));
47 #endif
48 }
49
50 void utc_dali_toolkit_texture_manager_cleanup(void)
51 {
52   test_return_value = TET_PASS;
53 }
54
55 namespace
56 {
57 const char* TEST_IMAGE_FILE_NAME   = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
58 const char* TEST_IMAGE_2_FILE_NAME = TEST_RESOURCE_DIR "/icon-delete.png";
59 const char* TEST_MASK_FILE_NAME    = TEST_RESOURCE_DIR "/mask.png";
60
61 class TestObserver : public Dali::Toolkit::TextureUploadObserver
62 {
63 public:
64   enum class CompleteType
65   {
66     NOT_COMPLETED = 0,
67     UPLOAD_COMPLETE,
68     LOAD_COMPLETE
69   };
70
71 public:
72   TestObserver()
73   : mCompleteType(CompleteType::NOT_COMPLETED),
74     mLoaded(false),
75     mObserverCalled(false),
76     mTextureSet()
77   {
78   }
79
80   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
81   {
82     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
83     {
84       mCompleteType = CompleteType::UPLOAD_COMPLETE;
85     }
86     else
87     {
88       mCompleteType = CompleteType::LOAD_COMPLETE;
89     }
90     mLoaded         = loadSuccess;
91     mObserverCalled = true;
92     mTextureSet     = textureInformation.textureSet;
93   }
94
95   CompleteType mCompleteType;
96   bool         mLoaded;
97   bool         mObserverCalled;
98   TextureSet   mTextureSet;
99 };
100
101 class TestObserverRemoveAndGenerateUrl : public TestObserver
102 {
103 public:
104   TestObserverRemoveAndGenerateUrl(TextureManager* textureManagerPtr)
105   : TestObserver(),
106     mTextureManagerPtr(textureManagerPtr)
107   {
108   }
109
110   virtual void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override
111   {
112     if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE)
113     {
114       mCompleteType = CompleteType::UPLOAD_COMPLETE;
115     }
116     else
117     {
118       mCompleteType = CompleteType::LOAD_COMPLETE;
119     }
120     mLoaded         = loadSuccess;
121     mObserverCalled = true;
122     mTextureSet     = textureInformation.textureSet;
123
124     // Remove during LoadComplete
125     mTextureManagerPtr->Remove(textureInformation.textureId, nullptr);
126
127     // ...And generate string which using texture id.
128     mGeneratedExternalUrl = mTextureManagerPtr->AddExternalTexture(mTextureSet);
129   }
130
131 public:
132   std::string mGeneratedExternalUrl;
133
134 protected:
135   TextureManager* mTextureManagerPtr; // Keep the pointer of texture manager.
136 };
137
138 } // namespace
139
140 int UtcTextureManagerRequestLoad(void)
141 {
142   ToolkitTestApplication application;
143
144   TextureManager textureManager; // Create new texture manager
145
146   TestObserver              observer;
147   std::string               filename("image.png");
148   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
149   TextureManager::TextureId textureId   = textureManager.RequestLoad(
150     filename,
151     ImageDimensions(),
152     FittingMode::SCALE_TO_FILL,
153     SamplingMode::BOX_THEN_LINEAR,
154     TextureManager::UseAtlas::NO_ATLAS,
155     &observer,
156     true,
157     TextureManager::ReloadPolicy::CACHED,
158     preMultiply);
159
160   VisualUrl url = textureManager.GetVisualUrl(textureId);
161
162   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
163
164   END_TEST;
165 }
166
167 int UtcTextureManagerGenerateHash(void)
168 {
169   ToolkitTestApplication application;
170
171   TextureManager textureManager; // Create new texture manager
172
173   TestObserver              observer;
174   std::string               filename("image.png");
175   auto                      preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
176   TextureManager::TextureId textureId   = textureManager.RequestLoad(
177     filename,
178     ImageDimensions(),
179     FittingMode::SCALE_TO_FILL,
180     SamplingMode::BOX_THEN_LINEAR,
181     TextureManager::UseAtlas::NO_ATLAS,
182     &observer,
183     true,
184     TextureManager::ReloadPolicy::CACHED,
185     preMultiply);
186
187   VisualUrl url = textureManager.GetVisualUrl(textureId);
188
189   DALI_TEST_EQUALS(url.GetUrl().compare(filename), 0, TEST_LOCATION);
190
191   END_TEST;
192 }
193
194 int UtcTextureManagerEncodedImageBuffer(void)
195 {
196   ToolkitTestApplication application;
197   tet_infoline("UtcTextureManagerEncodedImageBuffer");
198
199   auto  visualFactory  = Toolkit::VisualFactory::Get();
200   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
201
202   // Get encoded raw-buffer image and generate url
203   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
204   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
205
206   std::string url1 = textureManager.AddEncodedImageBuffer(buffer1);
207   std::string url2 = textureManager.AddEncodedImageBuffer(buffer1);
208   std::string url3 = VisualUrl::CreateBufferUrl(""); ///< Impossible Buffer URL. for coverage
209
210   // Check if same EncodedImageBuffer get same url
211   DALI_TEST_CHECK(url1 == url2);
212   // Reduce reference count
213   textureManager.RemoveEncodedImageBuffer(url1);
214   // Check whethere url1 still valid
215   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
216
217   url2 = textureManager.AddEncodedImageBuffer(buffer2);
218   // Check if difference EncodedImageBuffer get difference url
219   DALI_TEST_CHECK(url1 != url2);
220
221   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
222
223   TestObserver observer1;
224   textureManager.RequestLoad(
225     url1,
226     ImageDimensions(),
227     FittingMode::SCALE_TO_FILL,
228     SamplingMode::BOX_THEN_LINEAR,
229     TextureManager::UseAtlas::NO_ATLAS,
230     &observer1,
231     true, ///< orientationCorrection
232     TextureManager::ReloadPolicy::CACHED,
233     preMultiply);
234
235   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
236   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
237
238   application.SendNotification();
239   application.Render();
240
241   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
242
243   application.SendNotification();
244   application.Render();
245
246   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
247   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
248   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
249
250   TestObserver observer2;
251   // Syncload
252   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
253     url2,
254     ImageDimensions(),
255     FittingMode::SCALE_TO_FILL,
256     SamplingMode::BOX_THEN_LINEAR,
257     true, ///< synchronousLoading
258     &observer2,
259     true, ///< orientationCorrection
260     preMultiply);
261
262   DALI_TEST_CHECK(pixelBuffer);
263   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
264   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
265
266   // Asyncload
267   pixelBuffer = textureManager.LoadPixelBuffer(
268     url2,
269     ImageDimensions(),
270     FittingMode::SCALE_TO_FILL,
271     SamplingMode::BOX_THEN_LINEAR,
272     false, ///< synchronousLoading
273     &observer2,
274     true, ///< orientationCorrection
275     preMultiply);
276
277   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
278   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
279
280   application.SendNotification();
281   application.Render();
282
283   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
284
285   application.SendNotification();
286   application.Render();
287
288   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
289   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
290   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
291
292   textureManager.RemoveEncodedImageBuffer(url1);
293   textureManager.RemoveEncodedImageBuffer(url2);
294
295   // Now url1 and url2 is invalid type. mLoaded will return false
296
297   TestObserver observer3;
298   textureManager.RequestLoad(
299     url1,
300     ImageDimensions(),
301     FittingMode::SCALE_TO_FILL,
302     SamplingMode::BOX_THEN_LINEAR,
303     TextureManager::UseAtlas::NO_ATLAS,
304     &observer3,
305     true, ///< orientationCorrection
306     TextureManager::ReloadPolicy::CACHED,
307     preMultiply);
308
309   // Load will be success because url1 is cached
310   DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
311   DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
312   DALI_TEST_EQUALS(observer3.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
313
314   TestObserver observer4;
315   textureManager.RequestLoad(
316     url2,
317     ImageDimensions(),
318     FittingMode::SCALE_TO_FILL,
319     SamplingMode::BOX_THEN_LINEAR,
320     TextureManager::UseAtlas::NO_ATLAS,
321     &observer4,
322     true, ///< orientationCorrection
323     TextureManager::ReloadPolicy::FORCED,
324     preMultiply);
325
326   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
327   DALI_TEST_EQUALS(observer4.mObserverCalled, false, TEST_LOCATION);
328   application.SendNotification();
329   application.Render();
330
331   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
332
333   application.SendNotification();
334   application.Render();
335
336   // Load will be failed becuase reloadpolicy is forced
337   DALI_TEST_EQUALS(observer4.mLoaded, false, TEST_LOCATION);
338   DALI_TEST_EQUALS(observer4.mObserverCalled, true, TEST_LOCATION);
339   DALI_TEST_EQUALS(observer4.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
340
341   TestObserver observer5;
342   pixelBuffer = textureManager.LoadPixelBuffer(
343     url2,
344     ImageDimensions(),
345     FittingMode::SCALE_TO_FILL,
346     SamplingMode::BOX_THEN_LINEAR,
347     true, ///< synchronousLoading
348     &observer5,
349     true, ///< orientationCorrection
350     preMultiply);
351
352   // Load will be faild because synchronousLoading doesn't use cached texture
353   DALI_TEST_CHECK(!pixelBuffer);
354   DALI_TEST_EQUALS(observer5.mLoaded, false, TEST_LOCATION);
355   DALI_TEST_EQUALS(observer5.mObserverCalled, false, TEST_LOCATION);
356
357   TestObserver observer6;
358   pixelBuffer = textureManager.LoadPixelBuffer(
359     url3,
360     ImageDimensions(),
361     FittingMode::SCALE_TO_FILL,
362     SamplingMode::BOX_THEN_LINEAR,
363     false, ///< synchronousLoading
364     &observer6,
365     true, ///< orientationCorrection
366     preMultiply);
367
368   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
369   DALI_TEST_EQUALS(observer6.mObserverCalled, false, TEST_LOCATION);
370
371   application.SendNotification();
372   application.Render();
373
374   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
375
376   application.SendNotification();
377   application.Render();
378
379   // Load will be failed because url3 is invalid URL
380   DALI_TEST_EQUALS(observer6.mLoaded, false, TEST_LOCATION);
381   DALI_TEST_EQUALS(observer6.mObserverCalled, true, TEST_LOCATION);
382   DALI_TEST_EQUALS(observer6.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
383
384   END_TEST;
385 }
386
387 int UtcTextureManagerEncodedImageBufferReferenceCount(void)
388 {
389   ToolkitTestApplication application;
390   tet_infoline("UtcTextureManagerEncodedImageBuffer check reference count works well");
391
392   auto  visualFactory  = Toolkit::VisualFactory::Get();
393   auto& textureManager = GetImplementation(visualFactory).GetTextureManager(); // Use VisualFactory's texture manager
394
395   // Get encoded raw-buffer image and generate url
396   EncodedImageBuffer buffer1 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
397   EncodedImageBuffer buffer2 = ConvertFileToEncodedImageBuffer(TEST_IMAGE_FILE_NAME);
398
399   std::string url1 = textureManager.AddEncodedImageBuffer(buffer1);
400   std::string url2 = textureManager.AddEncodedImageBuffer(buffer1);
401
402   // Check if same EncodedImageBuffer get same url
403   DALI_TEST_CHECK(url1 == url2);
404
405   // Reduce reference count
406   textureManager.RemoveEncodedImageBuffer(url1);
407   // Check whethere url1 still valid
408   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
409
410   // Reduce reference count
411   textureManager.RemoveEncodedImageBuffer(url1);
412   // Check whethere url1 is not valid anymore
413   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
414
415   // UseExternalTexture doesn't create new buffer.
416   // So, reference count is still zero.
417   textureManager.UseExternalResource(url1);
418   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
419
420   url1 = textureManager.AddEncodedImageBuffer(buffer1);
421
422   url2 = textureManager.AddEncodedImageBuffer(buffer2);
423   // Check if difference EncodedImageBuffer get difference url
424   DALI_TEST_CHECK(url1 != url2);
425
426   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
427
428   // url1 load image by cache
429   TestObserver observer1;
430   textureManager.RequestLoad(
431     url1,
432     ImageDimensions(),
433     FittingMode::SCALE_TO_FILL,
434     SamplingMode::BOX_THEN_LINEAR,
435     TextureManager::UseAtlas::NO_ATLAS,
436     &observer1,
437     true, ///< orientationCorrection
438     TextureManager::ReloadPolicy::CACHED,
439     preMultiply);
440
441   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
442   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
443
444   application.SendNotification();
445   application.Render();
446
447   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
448
449   application.SendNotification();
450   application.Render();
451
452   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
453   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
454   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
455
456   // LoadPixelBuffer doen't use cache. url2 will not be cached
457   TestObserver       observer2;
458   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
459     url2,
460     ImageDimensions(),
461     FittingMode::SCALE_TO_FILL,
462     SamplingMode::BOX_THEN_LINEAR,
463     false, ///< synchronousLoading
464     &observer2,
465     true, ///< orientationCorrection
466     preMultiply);
467
468   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
469   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
470
471   application.SendNotification();
472   application.Render();
473
474   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
475
476   application.SendNotification();
477   application.Render();
478
479   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
480   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
481   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
482
483   // Decrease each url's reference count.
484   textureManager.RemoveEncodedImageBuffer(url1);
485   textureManager.RemoveEncodedImageBuffer(url2);
486
487   // url1 buffer is still have 1 reference count because it is cached.
488   // But url2 not valid because it is not cached.
489   DALI_TEST_CHECK(textureManager.GetEncodedImageBuffer(url1));
490   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url2));
491
492   // Check url1 buffer have 1 reference count because it is cached.
493   textureManager.RemoveEncodedImageBuffer(url1);
494   DALI_TEST_CHECK(!textureManager.GetEncodedImageBuffer(url1));
495
496   END_TEST;
497 }
498
499 int UtcTextureManagerCachingForDifferentLoadingType(void)
500 {
501   ToolkitTestApplication application;
502   tet_infoline("UtcTextureManagerCachingForDifferentLoadingType");
503
504   TextureManager textureManager; // Create new texture manager
505
506   TestObserver observer1;
507   std::string  filename(TEST_IMAGE_FILE_NAME);
508   auto         preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
509   textureManager.RequestLoad(
510     filename,
511     ImageDimensions(),
512     FittingMode::SCALE_TO_FILL,
513     SamplingMode::BOX_THEN_LINEAR,
514     TextureManager::UseAtlas::NO_ATLAS,
515     &observer1,
516     true,
517     TextureManager::ReloadPolicy::CACHED,
518     preMultiply);
519
520   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
521   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
522
523   application.SendNotification();
524   application.Render();
525
526   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
527
528   application.SendNotification();
529   application.Render();
530
531   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
532   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
533   DALI_TEST_EQUALS(observer1.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
534
535   TestObserver       observer2;
536   Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
537     filename,
538     ImageDimensions(),
539     FittingMode::SCALE_TO_FILL,
540     SamplingMode::BOX_THEN_LINEAR,
541     false,
542     &observer2,
543     true,
544     preMultiply);
545
546   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
547   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
548
549   application.SendNotification();
550   application.Render();
551
552   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
553
554   application.SendNotification();
555   application.Render();
556
557   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
558   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
559   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::LOAD_COMPLETE, TEST_LOCATION);
560
561   END_TEST;
562 }
563
564 int UtcTextureManagerUseInvalidMask(void)
565 {
566   ToolkitTestApplication application;
567   tet_infoline("UtcTextureManagerUseInvalidMask");
568
569   TextureManager textureManager; // Create new texture manager
570
571   TestObserver                       observer;
572   std::string                        filename(TEST_IMAGE_FILE_NAME);
573   std::string                        maskname("");
574   TextureManager::MaskingDataPointer maskInfo = nullptr;
575   maskInfo.reset(new TextureManager::MaskingData());
576   maskInfo->mAlphaMaskUrl       = maskname;
577   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
578   maskInfo->mCropToMask         = true;
579   maskInfo->mContentScaleFactor = 1.0f;
580
581   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
582   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
583   Dali::ImageDimensions         atlasRectSize(0, 0);
584   bool                          synchronousLoading(false);
585   bool                          atlasingStatus(false);
586   bool                          loadingStatus(false);
587   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
588   ImageAtlasManagerPtr          atlasManager        = nullptr;
589   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
590
591   textureManager.LoadTexture(
592     filename,
593     ImageDimensions(),
594     FittingMode::SCALE_TO_FILL,
595     SamplingMode::BOX_THEN_LINEAR,
596     maskInfo,
597     synchronousLoading,
598     textureId,
599     atlasRect,
600     atlasRectSize,
601     atlasingStatus,
602     loadingStatus,
603     &observer,
604     atlasUploadObserver,
605     atlasManager,
606     true,
607     TextureManager::ReloadPolicy::CACHED,
608     preMultiply);
609
610   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
611   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
612
613   application.SendNotification();
614   application.Render();
615
616   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
617
618   application.SendNotification();
619   application.Render();
620
621   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
622   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
623   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
624
625   END_TEST;
626 }
627
628 int UtcTextureManagerUseInvalidMaskAndMaskLoadedFirst(void)
629 {
630   ToolkitTestApplication application;
631   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded first");
632   tet_infoline("Try to check PostLoad works well");
633
634   TextureManager textureManager; // Create new texture manager
635
636   TestObserver                       observer;
637   std::string                        filename(TEST_IMAGE_FILE_NAME);
638   std::string                        maskname("invalid.png");
639   TextureManager::MaskingDataPointer maskInfo = nullptr;
640   maskInfo.reset(new TextureManager::MaskingData());
641   maskInfo->mAlphaMaskUrl       = maskname;
642   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
643   maskInfo->mCropToMask         = true;
644   maskInfo->mContentScaleFactor = 1.0f;
645
646   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
647   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
648   Dali::ImageDimensions         atlasRectSize(0, 0);
649   bool                          synchronousLoading(false);
650   bool                          atlasingStatus(false);
651   bool                          loadingStatus(false);
652   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
653   ImageAtlasManagerPtr          atlasManager        = nullptr;
654   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
655
656   textureManager.LoadTexture(
657     filename,
658     ImageDimensions(),
659     FittingMode::SCALE_TO_FILL,
660     SamplingMode::BOX_THEN_LINEAR,
661     maskInfo,
662     synchronousLoading,
663     textureId,
664     atlasRect,
665     atlasRectSize,
666     atlasingStatus,
667     loadingStatus,
668     &observer,
669     atlasUploadObserver,
670     atlasManager,
671     true,
672     TextureManager::ReloadPolicy::CACHED,
673     preMultiply);
674
675   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
676   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
677
678   application.SendNotification();
679   application.Render();
680
681   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
682
683   application.SendNotification();
684   application.Render();
685
686   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
687   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
688   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
689
690   END_TEST;
691 }
692
693 int UtcTextureManagerUseInvalidMaskAndMaskLoadedLater(void)
694 {
695   ToolkitTestApplication application;
696   tet_infoline("UtcTextureManagerUseInvalidMask when normal image loaded first, and mask image loaded later");
697   tet_infoline("Try to check CheckForWaitingTexture called");
698
699   TextureManager textureManager; // Create new texture manager
700
701   TestObserver                       observer;
702   std::string                        filename(TEST_IMAGE_FILE_NAME);
703   std::string                        maskname("invalid.png");
704   TextureManager::MaskingDataPointer maskInfo = nullptr;
705   maskInfo.reset(new TextureManager::MaskingData());
706   maskInfo->mAlphaMaskUrl       = maskname;
707   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
708   maskInfo->mCropToMask         = true;
709   maskInfo->mContentScaleFactor = 1.0f;
710
711   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
712   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
713   Dali::ImageDimensions         atlasRectSize(0, 0);
714   bool                          synchronousLoading(false);
715   bool                          atlasingStatus(false);
716   bool                          loadingStatus(false);
717   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
718   ImageAtlasManagerPtr          atlasManager        = nullptr;
719   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
720
721   textureManager.LoadTexture(
722     filename,
723     ImageDimensions(),
724     FittingMode::SCALE_TO_FILL,
725     SamplingMode::BOX_THEN_LINEAR,
726     maskInfo,
727     synchronousLoading,
728     textureId,
729     atlasRect,
730     atlasRectSize,
731     atlasingStatus,
732     loadingStatus,
733     &observer,
734     atlasUploadObserver,
735     atlasManager,
736     true,
737     TextureManager::ReloadPolicy::CACHED,
738     preMultiply);
739
740   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
741   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
742
743   application.SendNotification();
744   application.Render();
745
746   // CAPTION : HARD-CODING for coverage. If you are a good boy, Do not follow this code.
747   {
748     Dali::Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(
749       filename,
750       ImageDimensions(),
751       FittingMode::SCALE_TO_FILL,
752       SamplingMode::BOX_THEN_LINEAR,
753       true, ///< synchronousLoading
754       nullptr,
755       true, ///< orientationCorrection
756       preMultiply);
757
758     std::vector<Devel::PixelBuffer> pixelBuffers;
759     pixelBuffers.push_back(pixelBuffer);
760     textureManager.AsyncLoadComplete(textureId, pixelBuffers);
761     std::vector<Devel::PixelBuffer> maskBuffers;
762     textureManager.AsyncLoadComplete(maskInfo->mAlphaMaskId, maskBuffers);
763     textureManager.Remove(maskInfo->mAlphaMaskId, nullptr);
764     textureManager.Remove(textureId, &observer);
765   }
766
767   application.SendNotification();
768   application.Render();
769
770   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
771   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
772   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
773
774   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
775
776   END_TEST;
777 }
778
779 int UtcTextureManagerSynchronousLoadingFail(void)
780 {
781   ToolkitTestApplication application;
782   tet_infoline("UtcTextureManagerSynchronousLoadingFail");
783
784   TextureManager textureManager; // Create new texture manager
785
786   std::string                        maskname("");
787   TextureManager::MaskingDataPointer maskInfo = nullptr;
788   maskInfo.reset(new TextureManager::MaskingData());
789   maskInfo->mAlphaMaskUrl       = maskname;
790   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
791   maskInfo->mCropToMask         = true;
792   maskInfo->mContentScaleFactor = 1.0f;
793
794   std::string                   filename("dummy");
795   auto                          textureId(TextureManager::INVALID_TEXTURE_ID);
796   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
797   Dali::ImageDimensions         atlasRectSize(0, 0);
798   bool                          atlasingStatus(false);
799   bool                          loadingStatus(false);
800   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
801   ImageAtlasManagerPtr          atlasManager        = nullptr;
802   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
803
804   // load image synchronously.
805   TestObserver observer;
806   TextureSet   textureSet = textureManager.LoadTexture(
807     filename,
808     ImageDimensions(),
809     FittingMode::SCALE_TO_FILL,
810     SamplingMode::BOX_THEN_LINEAR,
811     maskInfo,
812     true, // synchronous loading.
813     textureId,
814     atlasRect,
815     atlasRectSize,
816     atlasingStatus,
817     loadingStatus,
818     &observer,
819     atlasUploadObserver,
820     atlasManager,
821     true,
822     TextureManager::ReloadPolicy::CACHED,
823     preMultiply);
824
825   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
826   DALI_TEST_CHECK(!textureSet);                                     // texture loading fail.
827   DALI_TEST_CHECK(textureId == TextureManager::INVALID_TEXTURE_ID); // invalid texture id is returned.
828
829   END_TEST;
830 }
831
832 int UtcTextureManagerCachingSynchronousLoading(void)
833 {
834   ToolkitTestApplication application;
835   tet_infoline("UtcTextureManagerCachingSynchronousLoading");
836
837   TextureManager textureManager; // Create new texture manager
838
839   std::string filename(TEST_IMAGE_FILE_NAME);
840
841   std::string                        maskname("");
842   TextureManager::MaskingDataPointer maskInfo = nullptr;
843   maskInfo.reset(new TextureManager::MaskingData());
844   maskInfo->mAlphaMaskUrl       = maskname;
845   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
846   maskInfo->mCropToMask         = true;
847   maskInfo->mContentScaleFactor = 1.0f;
848
849   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
850   Dali::ImageDimensions         atlasRectSize(0, 0);
851   bool                          atlasingStatus(false);
852   bool                          loadingStatus(false);
853   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
854   ImageAtlasManagerPtr          atlasManager        = nullptr;
855   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
856
857   // load image synchronously.
858   TestObserver observer;
859   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
860   TextureSet   textureSet = textureManager.LoadTexture(
861     filename,
862     ImageDimensions(),
863     FittingMode::SCALE_TO_FILL,
864     SamplingMode::BOX_THEN_LINEAR,
865     maskInfo,
866     true, // synchronous loading.
867     textureId,
868     atlasRect,
869     atlasRectSize,
870     atlasingStatus,
871     loadingStatus,
872     &observer,
873     atlasUploadObserver,
874     atlasManager,
875     true,
876     TextureManager::ReloadPolicy::CACHED,
877     preMultiply);
878
879   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
880   DALI_TEST_CHECK(textureSet); // texture is loaded.
881
882   // observer isn't called in synchronous loading.
883   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
884   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
885
886   // load same image asynchronously.
887   TestObserver asyncObserver;
888   auto         asyncTextureId(TextureManager::INVALID_TEXTURE_ID);
889   loadingStatus              = false;
890   TextureSet asyncTextureSet = textureManager.LoadTexture(
891     filename,
892     ImageDimensions(),
893     FittingMode::SCALE_TO_FILL,
894     SamplingMode::BOX_THEN_LINEAR,
895     maskInfo,
896     false, // asynchronous loading.
897     asyncTextureId,
898     atlasRect,
899     atlasRectSize,
900     atlasingStatus,
901     loadingStatus,
902     &asyncObserver,
903     atlasUploadObserver,
904     atlasManager,
905     true,
906     TextureManager::ReloadPolicy::CACHED,
907     preMultiply);
908
909   DALI_TEST_EQUALS(asyncTextureId, textureId, TEST_LOCATION); // texture is loaded.
910   DALI_TEST_EQUALS(loadingStatus, false, TEST_LOCATION);
911   DALI_TEST_CHECK(asyncTextureSet); // Cached texture.
912
913   // observer is directly called because textureSet is retrieved by cache.
914   DALI_TEST_EQUALS(asyncObserver.mLoaded, true, TEST_LOCATION);
915   DALI_TEST_EQUALS(asyncObserver.mObserverCalled, true, TEST_LOCATION);
916
917   END_TEST;
918 }
919
920 int UtcTextureManagerAsyncSyncAsync(void)
921 {
922   ToolkitTestApplication application;
923   tet_infoline("UtcTextureManagerAsyncSyncAsync");
924
925   TextureManager textureManager; // Create new texture manager
926
927   std::string filename(TEST_IMAGE_FILE_NAME);
928
929   std::string                        maskname("");
930   TextureManager::MaskingDataPointer maskInfo = nullptr;
931   maskInfo.reset(new TextureManager::MaskingData());
932   maskInfo->mAlphaMaskUrl       = maskname;
933   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
934   maskInfo->mCropToMask         = true;
935   maskInfo->mContentScaleFactor = 1.0f;
936
937   Vector4                       atlasRect(0.f, 0.f, 0.f, 0.f);
938   Dali::ImageDimensions         atlasRectSize(0, 0);
939   bool                          atlasingStatus(false);
940   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
941   ImageAtlasManagerPtr          atlasManager        = nullptr;
942   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
943
944   // load image asynchronously.
945   TestObserver asyncObserver1;
946   auto         asyncTextureId1(TextureManager::INVALID_TEXTURE_ID);
947   bool         asyncLoadingStatus1 = false;
948   TextureSet   asyncTextureSet1    = textureManager.LoadTexture(
949     filename,
950     ImageDimensions(),
951     FittingMode::SCALE_TO_FILL,
952     SamplingMode::BOX_THEN_LINEAR,
953     maskInfo,
954     false, // asynchronous loading.
955     asyncTextureId1,
956     atlasRect,
957     atlasRectSize,
958     atlasingStatus,
959     asyncLoadingStatus1,
960     &asyncObserver1,
961     atlasUploadObserver,
962     atlasManager,
963     true,
964     TextureManager::ReloadPolicy::CACHED,
965     preMultiply);
966
967   DALI_TEST_EQUALS(asyncLoadingStatus1, true, TEST_LOCATION); // texture is loading now.
968   DALI_TEST_CHECK(!asyncTextureSet1);                         // texture is not loaded yet.
969
970   // observer is still not called.
971   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
972   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
973
974   // load same image synchronously just after asynchronous loading.
975   TestObserver syncObserver;
976   auto         textureId(TextureManager::INVALID_TEXTURE_ID);
977   bool         syncLoadingStatus = false;
978   TextureSet   syncTextureSet    = textureManager.LoadTexture(
979     filename,
980     ImageDimensions(),
981     FittingMode::SCALE_TO_FILL,
982     SamplingMode::BOX_THEN_LINEAR,
983     maskInfo,
984     true, // synchronous loading.
985     textureId,
986     atlasRect,
987     atlasRectSize,
988     atlasingStatus,
989     syncLoadingStatus,
990     &syncObserver,
991     atlasUploadObserver,
992     atlasManager,
993     true,
994     TextureManager::ReloadPolicy::CACHED,
995     preMultiply);
996
997   DALI_TEST_EQUALS(asyncTextureId1, textureId, TEST_LOCATION); // texture is loaded.
998   DALI_TEST_EQUALS(syncLoadingStatus, false, TEST_LOCATION);   // texture is loaded.
999   DALI_TEST_CHECK(syncTextureSet);                             // texture is loaded.
1000
1001   // syncObserver isn't called in synchronous loading.
1002   DALI_TEST_EQUALS(syncObserver.mLoaded, false, TEST_LOCATION);
1003   DALI_TEST_EQUALS(syncObserver.mObserverCalled, false, TEST_LOCATION);
1004
1005   // asyncObserver1 is still not called too.
1006   DALI_TEST_EQUALS(asyncObserver1.mLoaded, false, TEST_LOCATION);
1007   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, false, TEST_LOCATION);
1008
1009   // load image asynchronously.
1010   TestObserver asyncObserver2;
1011   auto         asyncTextureId2(TextureManager::INVALID_TEXTURE_ID);
1012   bool         asyncLoadingStatus2 = false;
1013   TextureSet   asyncTextureSet2    = textureManager.LoadTexture(
1014     filename,
1015     ImageDimensions(),
1016     FittingMode::SCALE_TO_FILL,
1017     SamplingMode::BOX_THEN_LINEAR,
1018     maskInfo,
1019     false, // asynchronous loading.
1020     asyncTextureId2,
1021     atlasRect,
1022     atlasRectSize,
1023     atlasingStatus,
1024     asyncLoadingStatus2,
1025     &asyncObserver2,
1026     atlasUploadObserver,
1027     atlasManager,
1028     true,
1029     TextureManager::ReloadPolicy::CACHED,
1030     preMultiply);
1031
1032   DALI_TEST_EQUALS(asyncLoadingStatus2, false, TEST_LOCATION); // texture is loaded by previous sync request
1033   DALI_TEST_CHECK(asyncTextureSet2);                           // texture is loaded
1034   Texture syncTexture   = syncTextureSet.GetTexture(0u);
1035   Texture asyncTexture2 = asyncTextureSet2.GetTexture(0u);
1036   DALI_TEST_CHECK(syncTexture);
1037   DALI_TEST_CHECK(asyncTexture2);
1038   DALI_TEST_CHECK(asyncTexture2 == syncTexture); // check loaded two texture is same.
1039
1040   // observer is called synchronously because the texture is cached.
1041   DALI_TEST_EQUALS(asyncObserver2.mLoaded, true, TEST_LOCATION);
1042   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, true, TEST_LOCATION);
1043
1044   asyncObserver2.mLoaded         = false;
1045   asyncObserver2.mObserverCalled = false;
1046
1047   application.SendNotification();
1048   application.Render();
1049
1050   // Requested asynchronous loading at first is finished now and async observer is called now.
1051   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1052   DALI_TEST_EQUALS(asyncObserver1.mLoaded, true, TEST_LOCATION);
1053   DALI_TEST_EQUALS(asyncObserver1.mObserverCalled, true, TEST_LOCATION);
1054   DALI_TEST_CHECK(asyncObserver1.mTextureSet);
1055
1056   Texture observerTexture = asyncObserver1.mTextureSet.GetTexture(0u);
1057   DALI_TEST_CHECK(observerTexture == asyncTexture2); // check loaded two texture is same.
1058
1059   // asyncObserver2 was already called so it isn't called here.
1060   DALI_TEST_EQUALS(asyncObserver2.mLoaded, false, TEST_LOCATION);
1061   DALI_TEST_EQUALS(asyncObserver2.mObserverCalled, false, TEST_LOCATION);
1062
1063   END_TEST;
1064 }
1065
1066 int UtcTextureManagerQueueRemoveDuringObserve(void)
1067 {
1068   ToolkitTestApplication application;
1069   tet_infoline("UtcTextureManagerQueueRemoveDuringObserve");
1070
1071   TextureManager textureManager; // Create new texture manager
1072
1073   TestObserverRemoveAndGenerateUrl observer(&textureManager); // special observer for this UTC.
1074
1075   std::string filename(TEST_IMAGE_FILE_NAME);
1076   auto        preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1077
1078   TextureManager::TextureId textureId = textureManager.RequestLoad(
1079     filename,
1080     ImageDimensions(),
1081     FittingMode::SCALE_TO_FILL,
1082     SamplingMode::BOX_THEN_LINEAR,
1083     TextureManager::UseAtlas::NO_ATLAS,
1084     &observer,
1085     true,
1086     TextureManager::ReloadPolicy::CACHED,
1087     preMultiply);
1088
1089   DALI_TEST_EQUALS(observer.mLoaded, false, TEST_LOCATION);
1090   DALI_TEST_EQUALS(observer.mObserverCalled, false, TEST_LOCATION);
1091
1092   application.SendNotification();
1093   application.Render();
1094
1095   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1096
1097   application.SendNotification();
1098   application.Render();
1099
1100   DALI_TEST_EQUALS(observer.mLoaded, true, TEST_LOCATION);
1101   DALI_TEST_EQUALS(observer.mObserverCalled, true, TEST_LOCATION);
1102   DALI_TEST_EQUALS(observer.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
1103
1104   tet_printf("loaded textureId is %d, generated url is %s\n", static_cast<int>(textureId), observer.mGeneratedExternalUrl.c_str());
1105
1106   DALI_TEST_CHECK(static_cast<int>(textureId) != std::stoi(VisualUrl::GetLocation(observer.mGeneratedExternalUrl))); // Check we don't reuse textureId during observe
1107
1108   // Decrease external texture reference count who observer created
1109   textureManager.RemoveExternalTexture(observer.mGeneratedExternalUrl);
1110
1111   application.SendNotification();
1112   application.Render();
1113
1114   END_TEST;
1115 }
1116
1117 int UtcTextureManagerRemoveDuringApplyMasking(void)
1118 {
1119   ToolkitTestApplication application;
1120   tet_infoline("UtcTextureManagerRemoveDuringApplyMasking");
1121
1122   TextureManager textureManager; // Create new texture manager
1123
1124   TestObserver observer1;
1125   TestObserver observer2;
1126
1127   std::string                        filename(TEST_IMAGE_FILE_NAME);
1128   std::string                        maskname(TEST_MASK_FILE_NAME);
1129   TextureManager::MaskingDataPointer maskInfo = nullptr;
1130   maskInfo.reset(new TextureManager::MaskingData());
1131   maskInfo->mAlphaMaskUrl       = maskname;
1132   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1133   maskInfo->mCropToMask         = true;
1134   maskInfo->mContentScaleFactor = 1.0f;
1135
1136   auto                          textureId1(TextureManager::INVALID_TEXTURE_ID);
1137   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1138   Dali::ImageDimensions         atlasRectSize(0, 0);
1139   bool                          synchronousLoading(false);
1140   bool                          atlasingStatus(false);
1141   bool                          loadingStatus(false);
1142   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1143   ImageAtlasManagerPtr          atlasManager        = nullptr;
1144   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1145
1146   textureManager.LoadTexture(
1147     filename,
1148     ImageDimensions(),
1149     FittingMode::SCALE_TO_FILL,
1150     SamplingMode::BOX_THEN_LINEAR,
1151     maskInfo,
1152     synchronousLoading,
1153     textureId1,
1154     atlasRect,
1155     atlasRectSize,
1156     atlasingStatus,
1157     loadingStatus,
1158     &observer1,
1159     atlasUploadObserver,
1160     atlasManager,
1161     true,
1162     TextureManager::ReloadPolicy::CACHED,
1163     preMultiply);
1164
1165   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1166   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1167
1168   application.SendNotification();
1169   application.Render();
1170
1171   // Load image and mask image.
1172   // Now, LoadState become MASK_APPLYING
1173   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
1174
1175   tet_printf("Current textureId1:%d's state become MASK_APPLYING\n", static_cast<int>(textureId1));
1176
1177   application.SendNotification();
1178   application.Render();
1179
1180   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1181   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1182
1183   // Remove current textureId1. and request new texture again.
1184   textureManager.Remove(textureId1, &observer1);
1185   auto textureId2 = textureManager.RequestLoad(
1186     filename,
1187     ImageDimensions(),
1188     FittingMode::SCALE_TO_FILL,
1189     SamplingMode::BOX_THEN_LINEAR,
1190     TextureManager::UseAtlas::NO_ATLAS,
1191     &observer2,
1192     true, ///< orientationCorrection
1193     TextureManager::ReloadPolicy::CACHED,
1194     preMultiply,
1195     synchronousLoading);
1196
1197   application.SendNotification();
1198   application.Render();
1199
1200   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1201   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1202   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION);
1203   DALI_TEST_EQUALS(observer2.mObserverCalled, false, TEST_LOCATION);
1204
1205   tet_printf("textureId1:%d removed and textureId2:%d requested\n", static_cast<int>(textureId1), static_cast<int>(textureId2));
1206
1207   // CAPTION : HARD-CODING.
1208   {
1209     std::vector<Devel::PixelBuffer> pixelBuffers;
1210     textureManager.AsyncLoadComplete(textureId2, pixelBuffers);
1211     textureManager.Remove(textureId2, &observer2);
1212   }
1213
1214   DALI_TEST_EQUALS(observer2.mLoaded, false, TEST_LOCATION); ///< Note that we call AsyncLoadComplete hardly with empty pixelbuffer.
1215   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1216   DALI_TEST_EQUALS(observer2.mCompleteType, TestObserver::CompleteType::UPLOAD_COMPLETE, TEST_LOCATION);
1217
1218   END_TEST;
1219 }
1220
1221 int UtcTextureManagerMaskCacheTest(void)
1222 {
1223   ToolkitTestApplication application;
1224   tet_infoline("UtcTextureManagerMaskCacheTest");
1225
1226   TextureManager textureManager; // Create new texture manager
1227
1228   TestObserver observer1;
1229   TestObserver observer2;
1230
1231   std::string                        filename(TEST_IMAGE_FILE_NAME);
1232   std::string                        filename2(TEST_IMAGE_2_FILE_NAME);
1233   std::string                        maskname(TEST_MASK_FILE_NAME);
1234   TextureManager::MaskingDataPointer maskInfo = nullptr;
1235   maskInfo.reset(new TextureManager::MaskingData());
1236   maskInfo->mAlphaMaskUrl       = maskname;
1237   maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1238   maskInfo->mCropToMask         = true;
1239   maskInfo->mContentScaleFactor = 1.0f;
1240
1241   TextureManager::MaskingDataPointer maskInfo2 = nullptr;
1242   maskInfo2.reset(new TextureManager::MaskingData());
1243   maskInfo2->mAlphaMaskUrl       = maskname;
1244   maskInfo2->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1245   maskInfo2->mCropToMask         = true;
1246   maskInfo2->mContentScaleFactor = 1.0f;
1247
1248   auto                          textureId1(TextureManager::INVALID_TEXTURE_ID);
1249   auto                          textureId2(TextureManager::INVALID_TEXTURE_ID);
1250   Vector4                       atlasRect(0.f, 0.f, 1.f, 1.f);
1251   Dali::ImageDimensions         atlasRectSize(0, 0);
1252   bool                          synchronousLoading(false);
1253   bool                          atlasingStatus(false);
1254   bool                          loadingStatus(false);
1255   auto                          preMultiply         = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
1256   ImageAtlasManagerPtr          atlasManager        = nullptr;
1257   Toolkit::AtlasUploadObserver* atlasUploadObserver = nullptr;
1258
1259   textureManager.LoadTexture(
1260     filename,
1261     ImageDimensions(),
1262     FittingMode::SCALE_TO_FILL,
1263     SamplingMode::BOX_THEN_LINEAR,
1264     maskInfo,
1265     synchronousLoading,
1266     textureId1,
1267     atlasRect,
1268     atlasRectSize,
1269     atlasingStatus,
1270     loadingStatus,
1271     &observer1,
1272     atlasUploadObserver,
1273     atlasManager,
1274     true,
1275     TextureManager::ReloadPolicy::CACHED,
1276     preMultiply);
1277
1278   DALI_TEST_EQUALS(observer1.mLoaded, false, TEST_LOCATION);
1279   DALI_TEST_EQUALS(observer1.mObserverCalled, false, TEST_LOCATION);
1280
1281   application.SendNotification();
1282   application.Render();
1283
1284   // Load image and mask image.
1285   // Now, LoadState become MASK_APPLYING
1286   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
1287
1288   tet_printf("Current textureId1:%d's state become MASK_APPLYING\n", static_cast<int>(textureId1));
1289
1290   textureManager.LoadTexture(
1291     filename2,
1292     ImageDimensions(),
1293     FittingMode::SCALE_TO_FILL,
1294     SamplingMode::BOX_THEN_LINEAR,
1295     maskInfo2,
1296     synchronousLoading,
1297     textureId2,
1298     atlasRect,
1299     atlasRectSize,
1300     atlasingStatus,
1301     loadingStatus,
1302     &observer2,
1303     atlasUploadObserver,
1304     atlasManager,
1305     true,
1306     TextureManager::ReloadPolicy::CACHED,
1307     preMultiply);
1308
1309   application.SendNotification();
1310   application.Render();
1311
1312   // Load image2 + image1 apply mask + image2 apply mask = total 3 event trigger required.
1313   // Note that we use cached mask image.
1314   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
1315
1316   DALI_TEST_EQUALS(observer1.mLoaded, true, TEST_LOCATION);
1317   DALI_TEST_EQUALS(observer1.mObserverCalled, true, TEST_LOCATION);
1318   DALI_TEST_EQUALS(observer2.mLoaded, true, TEST_LOCATION);
1319   DALI_TEST_EQUALS(observer2.mObserverCalled, true, TEST_LOCATION);
1320
1321   try
1322   {
1323     // Remove textureId1 first, and then remove textureId2. Check whether segfault occured.
1324     textureManager.Remove(textureId1, &observer1);
1325     textureManager.Remove(textureId2, &observer2);
1326
1327     TestObserver observer3;
1328     maskInfo.reset(new TextureManager::MaskingData());
1329     maskInfo->mAlphaMaskUrl       = maskname;
1330     maskInfo->mAlphaMaskId        = TextureManager::INVALID_TEXTURE_ID;
1331     maskInfo->mCropToMask         = true;
1332     maskInfo->mContentScaleFactor = 1.0f;
1333
1334     textureManager.LoadTexture(
1335       filename,
1336       ImageDimensions(),
1337       FittingMode::SCALE_TO_FILL,
1338       SamplingMode::BOX_THEN_LINEAR,
1339       maskInfo,
1340       synchronousLoading,
1341       textureId1,
1342       atlasRect,
1343       atlasRectSize,
1344       atlasingStatus,
1345       loadingStatus,
1346       &observer3,
1347       atlasUploadObserver,
1348       atlasManager,
1349       true,
1350       TextureManager::ReloadPolicy::CACHED,
1351       preMultiply);
1352
1353     DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
1354     DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
1355
1356     application.SendNotification();
1357     application.Render();
1358
1359     // Load image and mask image.
1360     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1361     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1362
1363     DALI_TEST_EQUALS(observer3.mLoaded, false, TEST_LOCATION);
1364     DALI_TEST_EQUALS(observer3.mObserverCalled, false, TEST_LOCATION);
1365
1366     // Apply mask.
1367     DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1368
1369     DALI_TEST_EQUALS(observer3.mLoaded, true, TEST_LOCATION);
1370     DALI_TEST_EQUALS(observer3.mObserverCalled, true, TEST_LOCATION);
1371   }
1372   catch(...)
1373   {
1374     DALI_TEST_CHECK(false);
1375   }
1376
1377   END_TEST;
1378 }