4622431154ebca691ed945c91f00b994823454dd
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageVisual.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 #include <stdlib.h>
18 #include <iostream>
19 #include <vector>
20
21 #include <dali-toolkit-test-suite-utils.h>
22
23 #include <toolkit-event-thread-callback.h>
24 #include <toolkit-timer.h>
25
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit/devel-api/controls/control-devel.h>
28 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
29 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
30 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/public-api/image-loader/image-url.h>
33 #include <dali-toolkit/public-api/image-loader/image.h>
34
35 #include <test-encoded-image-buffer.h>
36 #include "dummy-control.h"
37 #include "test-native-image-source.h"
38
39 using namespace Dali;
40 using namespace Dali::Toolkit;
41
42 void dali_image_visual_startup(void)
43 {
44   test_return_value = TET_UNDEF;
45 }
46
47 void dali_image_visual_cleanup(void)
48 {
49   test_return_value = TET_PASS;
50 }
51
52 namespace
53 {
54 const char* TEST_IMAGE_FILE_NAME          = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
55 const char* TEST_BROKEN_IMAGE_FILE_NAME   = TEST_RESOURCE_DIR "/a-random-nonimage.jpg";
56 const char* TEST_LARGE_IMAGE_FILE_NAME    = TEST_RESOURCE_DIR "/tbcol.png";
57 const char* TEST_SMALL_IMAGE_FILE_NAME    = TEST_RESOURCE_DIR "/icon-edit.png";
58 const char* TEST_REMOTE_IMAGE_FILE_NAME   = "https://www.tizen.org/sites/all/themes/tizen_theme/logo.png";
59 const char* TEST_INVALID_FILE_NAME        = TEST_RESOURCE_DIR "/invalid.jpg";
60 const char* TEST_REMOTE_INVALID_FILE_NAME = "https://www.tizen.org/invalid.png";
61 const char* TEST_MASK_IMAGE_FILE_NAME     = TEST_RESOURCE_DIR "/mask.png";
62 const char* TEST_ROTATED_IMAGE            = TEST_RESOURCE_DIR "/keyboard-Landscape.jpg";
63
64 bool             gResourceReadySignalFired = false;
65 std::vector<int> gReadyIds                 = {};
66 void             ResourceReadySignal(Control control)
67 {
68   gResourceReadySignalFired = true;
69   gReadyIds.push_back(control.GetProperty<int>(Actor::Property::ID));
70 }
71 void ClearReadyIds()
72 {
73   gReadyIds.clear();
74 }
75
76 Actor CreateActorWithImageVisual(const Property::Map& map)
77 {
78   VisualFactory     factory   = VisualFactory::Get();
79   DummyControl      actor     = DummyControl::New();
80   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
81   Visual::Base      visual    = factory.CreateVisual(map);
82   DALI_TEST_CHECK(visual);
83   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
84   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
85   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
86   return actor;
87 }
88
89 Visual::Base CreateVisualWithPolicy(const char* url, Property::Index key, const Property::Value& value)
90 {
91   VisualFactory factory = VisualFactory::Get();
92
93   Property::Map propertyMap;
94   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
95   propertyMap.Insert(ImageVisual::Property::URL, url);
96   propertyMap.Insert(ImageVisual::Property::DESIRED_WIDTH, 20);
97   propertyMap.Insert(ImageVisual::Property::DESIRED_HEIGHT, 30);
98   propertyMap.Insert(key, value);
99
100   return factory.CreateVisual(propertyMap);
101 }
102
103 } // namespace
104
105 void TestVisualRender(ToolkitTestApplication&      application,
106                       DummyControl&                actor,
107                       Visual::Base&                visual,
108                       std::size_t                  expectedSamplers = 0,
109                       ImageDimensions              imageDimensions  = ImageDimensions(),
110                       Integration::ResourcePointer resourcePtr      = Integration::ResourcePointer())
111 {
112   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
113   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
114
115   if(resourcePtr)
116   {
117     // set the image size, for test case, this needs to be set before loading started
118     application.GetPlatform().SetClosestImageSize(Vector2(imageDimensions.GetWidth(), imageDimensions.GetHeight()));
119   }
120
121   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
122   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
123
124   application.GetScene().Add(actor);
125
126   application.SendNotification(); // Send messages to update
127   application.Render();           // process update and render
128   application.SendNotification(); // process any signals to event
129
130   if(resourcePtr)
131   {
132     DALI_TEST_EQUALS(application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceSynchronouslyFunc), true, TEST_LOCATION);
133   }
134
135   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
136 }
137
138 static void TestMixColor(Visual::Base visual, Property::Index mixColorIndex, const Vector4& testColor)
139 {
140   Property::Map map;
141   visual.CreatePropertyMap(map);
142   Property::Value* value = map.Find(mixColorIndex);
143   DALI_TEST_CHECK(value);
144   Vector3 mixColor1;
145   DALI_TEST_CHECK(value->Get(mixColor1));
146   DALI_TEST_EQUALS(mixColor1, Vector3(testColor), 0.001, TEST_LOCATION);
147
148   value = map.Find(Visual::Property::MIX_COLOR);
149   DALI_TEST_CHECK(value);
150   Vector4 mixColor2;
151   DALI_TEST_CHECK(value->Get(mixColor2));
152   DALI_TEST_EQUALS(mixColor2, testColor, 0.001, TEST_LOCATION);
153
154   value = map.Find(Visual::Property::OPACITY);
155   DALI_TEST_CHECK(value);
156   float opacity;
157   DALI_TEST_CHECK(value->Get(opacity));
158   DALI_TEST_EQUALS(opacity, testColor.a, 0.001, TEST_LOCATION);
159 }
160
161 int UtcDaliImageVisualPropertyMap(void)
162 {
163   ToolkitTestApplication application;
164   tet_infoline("Request image visual with a Property::Map");
165
166   VisualFactory factory = VisualFactory::Get();
167   DALI_TEST_CHECK(factory);
168   factory.SetPreMultiplyOnLoad(true);
169
170   Property::Map propertyMap;
171   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
172   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
173
174   Visual::Base visual = factory.CreateVisual(propertyMap);
175   DALI_TEST_CHECK(visual);
176
177   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
178   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
179
180   TestGlAbstraction& gl           = application.GetGlAbstraction();
181   TraceCallStack&    textureTrace = gl.GetTextureTrace();
182   textureTrace.Enable(true);
183
184   DummyControl      actor     = DummyControl::New();
185   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
186   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
187
188   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
189   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
190
191   application.GetScene().Add(actor);
192   application.SendNotification();
193   application.Render();
194
195   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
196
197   application.SendNotification();
198   application.Render();
199
200   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
201   auto renderer           = actor.GetRendererAt(0);
202   auto preMultipliedIndex = renderer.GetPropertyIndex("preMultipliedAlpha");
203   DALI_TEST_CHECK(preMultipliedIndex != Property::INVALID_INDEX);
204   auto preMultipliedAlpha  = renderer.GetProperty<float>(preMultipliedIndex);
205   auto preMultipliedAlpha2 = renderer.GetProperty<bool>(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA);
206   DALI_TEST_EQUALS(preMultipliedAlpha, 1.0f, TEST_LOCATION);
207   DALI_TEST_EQUALS(preMultipliedAlpha2, true, TEST_LOCATION);
208   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
209
210   application.GetScene().Remove(actor);
211   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
212
213   END_TEST;
214 }
215
216 int UtcDaliImageVisualNoPremultipliedAlpha01(void)
217 {
218   ToolkitTestApplication application;
219   tet_infoline("Request image visual without pre-multiplied alpha");
220
221   VisualFactory factory = VisualFactory::Get();
222   DALI_TEST_CHECK(factory);
223   factory.SetPreMultiplyOnLoad(false);
224
225   Property::Map propertyMap;
226   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
227   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
228
229   Visual::Base visual = factory.CreateVisual(propertyMap);
230   DALI_TEST_CHECK(visual);
231
232   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
233   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
234
235   TestGlAbstraction& gl           = application.GetGlAbstraction();
236   TraceCallStack&    textureTrace = gl.GetTextureTrace();
237   textureTrace.Enable(true);
238
239   DummyControl      actor     = DummyControl::New();
240   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
241   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
242
243   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
244   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
245
246   application.GetScene().Add(actor);
247   application.SendNotification();
248   application.Render();
249
250   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
251
252   application.SendNotification();
253   application.Render();
254
255   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
256   auto renderer           = actor.GetRendererAt(0);
257   auto preMultipliedIndex = renderer.GetPropertyIndex("preMultipliedAlpha");
258   DALI_TEST_CHECK(preMultipliedIndex != Property::INVALID_INDEX);
259   auto preMultipliedAlpha  = renderer.GetProperty<bool>(preMultipliedIndex);
260   auto preMultipliedAlpha2 = renderer.GetProperty<bool>(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA);
261
262   DALI_TEST_EQUALS(preMultipliedAlpha, false, TEST_LOCATION);
263   DALI_TEST_EQUALS(preMultipliedAlpha2, false, TEST_LOCATION);
264
265   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
266
267   application.GetScene().Remove(actor);
268   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
269
270   END_TEST;
271 }
272
273 int UtcDaliImageVisualNoPremultipliedAlpha02(void)
274 {
275   ToolkitTestApplication application;
276   tet_infoline("Request image visual with no alpha channel");
277
278   VisualFactory factory = VisualFactory::Get();
279   DALI_TEST_CHECK(factory);
280
281   Property::Map propertyMap;
282   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
283   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
284
285   Visual::Base visual = factory.CreateVisual(propertyMap);
286   DALI_TEST_CHECK(visual);
287
288   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
289   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
290
291   TestGlAbstraction& gl           = application.GetGlAbstraction();
292   TraceCallStack&    textureTrace = gl.GetTextureTrace();
293   textureTrace.Enable(true);
294
295   DummyControl      actor     = DummyControl::New();
296   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
297   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
298
299   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
300   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
301
302   application.GetScene().Add(actor);
303   application.SendNotification();
304   application.Render();
305
306   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
307
308   application.SendNotification();
309   application.Render();
310
311   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
312   auto renderer           = actor.GetRendererAt(0);
313   auto preMultipliedIndex = renderer.GetPropertyIndex("preMultipliedAlpha");
314   DALI_TEST_CHECK(preMultipliedIndex != Property::INVALID_INDEX);
315   auto preMultipliedAlpha  = renderer.GetProperty<bool>(preMultipliedIndex);
316   auto preMultipliedAlpha2 = renderer.GetProperty<bool>(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA);
317
318   DALI_TEST_EQUALS(preMultipliedAlpha, false, TEST_LOCATION);
319   DALI_TEST_EQUALS(preMultipliedAlpha2, false, TEST_LOCATION);
320
321   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
322
323   int srcFactorRgb    = renderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_SRC_RGB);
324   int destFactorRgb   = renderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_DEST_RGB);
325   int srcFactorAlpha  = renderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_SRC_ALPHA);
326   int destFactorAlpha = renderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_DEST_ALPHA);
327   DALI_TEST_CHECK(srcFactorRgb == BlendFactor::SRC_ALPHA);
328   DALI_TEST_CHECK(destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA);
329   DALI_TEST_CHECK(srcFactorAlpha == BlendFactor::ONE);
330   DALI_TEST_CHECK(destFactorAlpha == BlendFactor::ONE_MINUS_SRC_ALPHA);
331
332   textureTrace.Reset();
333
334   // Make a new visual with the same image
335   Visual::Base newVisual = factory.CreateVisual(propertyMap);
336   DALI_TEST_CHECK(newVisual);
337
338   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
339   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
340
341   DummyControl      newActor     = DummyControl::New();
342   DummyControlImpl& newDummyImpl = static_cast<DummyControlImpl&>(newActor.GetImplementation());
343   newDummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, newVisual);
344
345   newActor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
346   DALI_TEST_EQUALS(newActor.GetRendererCount(), 0u, TEST_LOCATION);
347
348   application.GetScene().Add(newActor);
349
350   application.SendNotification();
351   application.Render();
352
353   DALI_TEST_EQUALS(newActor.GetRendererCount(), 1u, TEST_LOCATION);
354   auto newRenderer   = newActor.GetRendererAt(0);
355   preMultipliedIndex = newRenderer.GetPropertyIndex("preMultipliedAlpha");
356   DALI_TEST_CHECK(preMultipliedIndex != Property::INVALID_INDEX);
357   preMultipliedAlpha  = newRenderer.GetProperty<bool>(preMultipliedIndex);
358   preMultipliedAlpha2 = newRenderer.GetProperty<bool>(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA);
359
360   DALI_TEST_EQUALS(preMultipliedAlpha, false, TEST_LOCATION);
361   DALI_TEST_EQUALS(preMultipliedAlpha2, false, TEST_LOCATION);
362
363   srcFactorRgb    = newRenderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_SRC_RGB);
364   destFactorRgb   = newRenderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_DEST_RGB);
365   srcFactorAlpha  = newRenderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_SRC_ALPHA);
366   destFactorAlpha = newRenderer.GetProperty<int>(Renderer::Property::BLEND_FACTOR_DEST_ALPHA);
367   DALI_TEST_CHECK(srcFactorRgb == BlendFactor::SRC_ALPHA);
368   DALI_TEST_CHECK(destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA);
369   DALI_TEST_CHECK(srcFactorAlpha == BlendFactor::ONE);
370   DALI_TEST_CHECK(destFactorAlpha == BlendFactor::ONE_MINUS_SRC_ALPHA);
371
372   application.GetScene().Remove(actor);
373   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
374
375   END_TEST;
376 }
377
378 int UtcDaliImageVisualRemoteImageLoad(void)
379 {
380   ToolkitTestApplication application;
381   tet_infoline("Request remote image visual with a Property::Map");
382
383   VisualFactory factory = VisualFactory::Get();
384   DALI_TEST_CHECK(factory);
385
386   Property::Map propertyMap;
387   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
388   propertyMap.Insert(ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME);
389
390   Visual::Base visual = factory.CreateVisual(propertyMap);
391   DALI_TEST_CHECK(visual);
392
393   TestGlAbstraction& gl           = application.GetGlAbstraction();
394   TraceCallStack&    textureTrace = gl.GetTextureTrace();
395   textureTrace.Enable(true);
396
397   DummyControl      actor     = DummyControl::New();
398   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
399   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
400
401   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
402   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
403
404   application.GetScene().Add(actor);
405   application.SendNotification();
406
407   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
408
409   application.SendNotification();
410   application.Render();
411
412   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
413   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
414
415   application.GetScene().Remove(actor);
416   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
417
418   END_TEST;
419 }
420
421 int UtcDaliImageVisualWithNativeImage(void)
422 {
423   ToolkitTestApplication application;
424   tet_infoline("Use Native Image as url");
425
426   NativeImageSourcePtr nativeImageSource = NativeImageSource::New(500, 500, NativeImageSource::COLOR_DEPTH_DEFAULT);
427   ImageUrl             imageUrl          = Dali::Toolkit::Image::GenerateUrl(nativeImageSource);
428   std::string          url               = imageUrl.GetUrl();
429
430   VisualFactory factory = VisualFactory::Get();
431   DALI_TEST_CHECK(factory);
432
433   Property::Map propertyMap;
434   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
435   propertyMap.Insert(ImageVisual::Property::URL, url);
436
437   Visual::Base visual = factory.CreateVisual(propertyMap);
438   DALI_TEST_CHECK(visual);
439
440   DummyControl      actor     = DummyControl::New();
441   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
442   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
443
444   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
445
446   application.GetScene().Add(actor);
447
448   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
449
450   Renderer renderer = actor.GetRendererAt(0);
451   Shader   shader   = renderer.GetShader();
452
453   Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
454   DALI_TEST_CHECK(value.GetType() == Property::MAP);
455   const Property::Map* outMap         = value.GetMap();
456   std::string          fragmentShader = (*outMap)["fragment"].Get<std::string>();
457
458   const char* fragmentPrefix = Dali::NativeImageSourceTest::GetCustomFragmentPrefix();
459   size_t      pos            = fragmentShader.find(fragmentPrefix);
460
461   DALI_TEST_EQUALS(pos != std::string::npos, true, TEST_LOCATION);
462
463   END_TEST;
464 }
465
466 int UtcDaliImageVisualWithNativeImageCustomShader(void)
467 {
468   ToolkitTestApplication application;
469   tet_infoline("Use Native Image as url and Use custom shader");
470
471   NativeImageSourcePtr nativeImageSource = NativeImageSource::New(500, 500, NativeImageSource::COLOR_DEPTH_DEFAULT);
472   ImageUrl             imageUrl          = Dali::Toolkit::Image::GenerateUrl(nativeImageSource);
473   std::string          url               = imageUrl.GetUrl();
474
475   VisualFactory factory = VisualFactory::Get();
476   DALI_TEST_CHECK(factory);
477
478   Property::Map     propertyMap;
479   Property::Map     shaderMap;
480   const std::string customVertexShaderSource                    = "Foobar";
481   const std::string customFragmentShaderSource                  = "Foobar";
482   shaderMap[Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = customFragmentShaderSource;
483   shaderMap[Toolkit::Visual::Shader::Property::VERTEX_SHADER]   = customVertexShaderSource;
484
485   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
486   propertyMap.Insert(Toolkit::Visual::Property::SHADER, shaderMap);
487   propertyMap.Insert(ImageVisual::Property::URL, url);
488
489   Visual::Base visual = factory.CreateVisual(propertyMap);
490   DALI_TEST_CHECK(visual);
491
492   DummyControl      actor     = DummyControl::New();
493   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
494   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
495
496   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
497   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
498
499   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
500
501   application.GetScene().Add(actor);
502
503   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
504
505   application.SendNotification();
506   application.Render(16);
507
508   Renderer renderer = actor.GetRendererAt(0);
509   Shader   shader   = renderer.GetShader();
510
511   Property::Value value = shader.GetProperty(Shader::Property::PROGRAM);
512   DALI_TEST_CHECK(value.GetType() == Property::MAP);
513   const Property::Map* outMap               = value.GetMap();
514   std::string          fragmentShaderSource = (*outMap)["fragment"].Get<std::string>();
515   std::string          vertexShaderSource   = (*outMap)["vertex"].Get<std::string>();
516
517   // Compare vertex shader is equal
518   DALI_TEST_EQUALS(customVertexShaderSource, vertexShaderSource, TEST_LOCATION);
519
520   // Check fragment shader changed
521   const char* fragmentPrefix = Dali::NativeImageSourceTest::GetCustomFragmentPrefix();
522   size_t      pos            = fragmentShaderSource.find(fragmentPrefix);
523
524   DALI_TEST_EQUALS(pos != std::string::npos, true, TEST_LOCATION);
525
526   DALI_TEST_EQUALS(std::string(fragmentPrefix) + customFragmentShaderSource, fragmentShaderSource, TEST_LOCATION);
527
528   END_TEST;
529 }
530
531 int UtcDaliImageVisualWithNativeImageRemoved(void)
532 {
533   ToolkitTestApplication application;
534   tet_infoline("Use Native Image as url");
535
536   TestGlAbstraction& gl           = application.GetGlAbstraction();
537   TraceCallStack&    textureTrace = gl.GetTextureTrace();
538   textureTrace.Enable(true);
539
540   NativeImageSourcePtr nativeImageSource = NativeImageSource::New(500, 500, NativeImageSource::COLOR_DEPTH_DEFAULT);
541   ImageUrl             imageUrl          = Dali::Toolkit::Image::GenerateUrl(nativeImageSource);
542   std::string          url               = imageUrl.GetUrl();
543
544   VisualFactory factory = VisualFactory::Get();
545   DALI_TEST_CHECK(factory);
546
547   Property::Map propertyMap;
548   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
549   propertyMap.Insert(ImageVisual::Property::URL, url);
550
551   Visual::Base visual = factory.CreateVisual(propertyMap);
552   DALI_TEST_CHECK(visual);
553
554   DummyControl      actor     = DummyControl::New();
555   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
556   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
557
558   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
559
560   application.GetScene().Add(actor);
561   application.SendNotification();
562   application.Render();
563
564   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
565   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
566
567   tet_infoline("No delete texture because reference count is not zero");
568   imageUrl.Reset();
569   application.GetScene().Remove(actor);
570   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
571   application.SendNotification();
572   application.Render();
573
574   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
575   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
576
577   tet_infoline("Delete texture because reference count is zero");
578   visual.Reset();
579   application.SendNotification();
580   application.Render();
581
582   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
583
584   END_TEST;
585 }
586
587 int UtcDaliImageVisualWithEncodedImageBuffer(void)
588 {
589   ToolkitTestApplication application;
590   tet_infoline("Use Encoded Image Buffer as url");
591
592   EncodedImageBuffer rawBuffer = ConvertFileToEncodedImageBuffer(TEST_LARGE_IMAGE_FILE_NAME);
593   ImageUrl           url       = Dali::Toolkit::Image::GenerateUrl(rawBuffer);
594
595   VisualFactory factory = VisualFactory::Get();
596   DALI_TEST_CHECK(factory);
597
598   Property::Map propertyMap;
599   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
600   propertyMap.Insert(ImageVisual::Property::URL, url.GetUrl());
601
602   Visual::Base visual = factory.CreateVisual(propertyMap);
603   DALI_TEST_CHECK(visual);
604
605   TestGlAbstraction& gl           = application.GetGlAbstraction();
606   TraceCallStack&    textureTrace = gl.GetTextureTrace();
607   textureTrace.Enable(true);
608
609   DummyControl      actor     = DummyControl::New();
610   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
611   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
612
613   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
614   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
615
616   application.GetScene().Add(actor);
617   application.SendNotification();
618
619   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
620
621   application.SendNotification();
622   application.Render();
623
624   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
625   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
626
627   application.GetScene().Remove(actor);
628   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
629
630   END_TEST;
631 }
632
633 int UtcDaliImageVisualWithEncodedImageBufferRemoved(void)
634 {
635   ToolkitTestApplication application;
636   tet_infoline("Use Encoded Image Buffer as url");
637
638   TestGlAbstraction& gl           = application.GetGlAbstraction();
639   TraceCallStack&    textureTrace = gl.GetTextureTrace();
640   textureTrace.Enable(true);
641
642   EncodedImageBuffer rawBuffer = ConvertFileToEncodedImageBuffer(TEST_LARGE_IMAGE_FILE_NAME);
643   ImageUrl           imageUrl  = Dali::Toolkit::Image::GenerateUrl(rawBuffer);
644   std::string        url       = imageUrl.GetUrl();
645
646   VisualFactory factory = VisualFactory::Get();
647   DALI_TEST_CHECK(factory);
648
649   Property::Map propertyMap;
650   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
651   propertyMap.Insert(ImageVisual::Property::URL, url);
652
653   Visual::Base visual = factory.CreateVisual(propertyMap);
654   DALI_TEST_CHECK(visual);
655
656   DummyControl      actor     = DummyControl::New();
657   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
658   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
659
660   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
661
662   application.GetScene().Add(actor);
663   application.SendNotification();
664
665   // Wait for decode buffer and make texture.
666   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
667
668   application.SendNotification();
669   application.Render();
670
671   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
672   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
673
674   tet_infoline("Delete texture because there is no actor to use decoded texture");
675   imageUrl.Reset();
676   application.GetScene().Remove(actor);
677   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
678   application.SendNotification();
679   application.Render();
680
681   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
682   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
683
684   END_TEST;
685 }
686
687 int UtcDaliImageVisualTextureReuse1(void)
688 {
689   ToolkitTestApplication application;
690   tet_infoline("Request remote image visual with a Property::Map; request a second visual with the same property map - should reuse texture");
691
692   Property::Map propertyMap;
693   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
694   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
695   propertyMap.Insert(ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DETACHED);
696
697   TestGlAbstraction& gl           = application.GetGlAbstraction();
698   TraceCallStack&    textureTrace = gl.GetTextureTrace();
699   textureTrace.Enable(true);
700   TraceCallStack& drawTrace = gl.GetDrawTrace();
701   drawTrace.Enable(true);
702
703   Actor actor = CreateActorWithImageVisual(propertyMap);
704   application.GetScene().Add(actor);
705   application.SendNotification();
706
707   // Wait for image to load
708   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
709
710   application.SendNotification();
711   application.Render();
712
713   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
714   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
715   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
716   DALI_TEST_EQUALS(drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION);
717   textureTrace.Reset();
718   drawTrace.Reset();
719
720   Actor actor2 = CreateActorWithImageVisual(propertyMap);
721   application.GetScene().Add(actor2);
722
723   application.SendNotification(); // Send messages to update
724   application.Render();           // process update and render
725   application.SendNotification(); // process any signals to event
726
727   DALI_TEST_EQUALS(actor2.GetRendererCount(), 1u, TEST_LOCATION);
728
729   tet_infoline(
730     "Test that 2 draw calls occur with no new texture gens/binds, i.e. both\n"
731     "draw calls use the same texture as the previous draw call\n");
732
733   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
734   DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
735   // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
736   //  DALI_TEST_EQUALS( textureTrace.CountMethod("BindTexture"), 0, TEST_LOCATION );
737
738   tet_infoline("Test that removing 1 actor doesn't delete the texture\n");
739
740   application.GetScene().Remove(actor);
741   application.SendNotification();
742   application.Render();
743
744   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
745   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
746
747   tet_infoline("Test that removing last actor does delete the texture\n");
748
749   application.GetScene().Remove(actor2); // Detaches remaining ImageVisual
750   application.SendNotification();
751   application.Render();
752
753   DALI_TEST_CHECK(actor2.GetRendererCount() == 0u);
754   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
755
756   END_TEST;
757 }
758
759 int UtcDaliImageVisualTextureReuse2(void)
760 {
761   ToolkitTestApplication application;
762   tet_infoline("Request remote image visual with a Property::Map; request a second visual with the same url but different property map - should create new texture");
763
764   Property::Map propertyMap;
765   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
766   propertyMap.Insert(ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME);
767
768   TestGlAbstraction& gl           = application.GetGlAbstraction();
769   TraceCallStack&    textureTrace = gl.GetTextureTrace();
770   textureTrace.Enable(true);
771   TraceCallStack& drawTrace = gl.GetDrawTrace();
772   drawTrace.Enable(true);
773
774   Actor actor = CreateActorWithImageVisual(propertyMap);
775   application.GetScene().Add(actor);
776   application.SendNotification();
777
778   // Wait for image to load
779   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
780
781   application.SendNotification();
782   application.Render();
783
784   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
785   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
786   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
787   DALI_TEST_EQUALS(drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION);
788   textureTrace.Reset();
789   drawTrace.Reset();
790
791   propertyMap.Insert(ImageVisual::Property::SAMPLING_MODE, Dali::SamplingMode::NEAREST);
792   propertyMap.Insert(ImageVisual::Property::DESIRED_WIDTH, 100);
793   propertyMap.Insert(ImageVisual::Property::DESIRED_HEIGHT, 100);
794   Actor actor2 = CreateActorWithImageVisual(propertyMap);
795   application.GetScene().Add(actor2);
796
797   application.SendNotification();
798
799   // Wait for image to load
800   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
801
802   application.SendNotification();
803   application.Render();
804
805   DALI_TEST_EQUALS(actor2.GetRendererCount(), 1u, TEST_LOCATION);
806
807   tet_infoline(
808     "Test that 2 draw calls occur with 1 new texture gen/bind, i.e. both "
809     "renderers are using different textures\n");
810
811   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
812   DALI_TEST_EQUALS(drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION);
813   TraceCallStack::NamedParams tex1;
814   tex1["texture"] << 1;
815   TraceCallStack::NamedParams tex2;
816   tex2["texture"] << 2;
817   DALI_TEST_EQUALS(textureTrace.FindMethodAndParams("BindTexture", tex1), true, TEST_LOCATION);
818   DALI_TEST_EQUALS(textureTrace.FindMethodAndParams("BindTexture", tex2), true, TEST_LOCATION);
819
820   tet_infoline("Test that removing 1 actor deletes it's texture\n");
821
822   application.GetScene().Remove(actor);
823   application.SendNotification();
824   application.Render();
825
826   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
827   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
828
829   tet_infoline("Test that removing last actor deletes it's texture\n");
830
831   application.GetScene().Remove(actor2);
832   application.SendNotification();
833   application.Render();
834
835   DALI_TEST_CHECK(actor2.GetRendererCount() == 0u);
836   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 2, TEST_LOCATION);
837
838   END_TEST;
839 }
840
841 int UtcDaliImageVisualCustomWrapModePixelArea(void)
842 {
843   ToolkitTestApplication application;
844   tet_infoline("Request image visual with a Property::Map, test custom wrap mode and pixel area with atlasing");
845
846   static std::vector<UniformData> customUniforms =
847     {
848       UniformData("pixelArea", Property::Type::VECTOR4),
849       UniformData("wrapMode", Property::Type::VECTOR2),
850     };
851
852   TestGraphicsController& graphics = application.GetGraphicsController();
853   graphics.AddCustomUniforms(customUniforms);
854
855   VisualFactory factory = VisualFactory::Get();
856   DALI_TEST_CHECK(factory);
857
858   // Test wrap mode with atlasing. Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
859   const int     width  = 34;
860   const int     height = 34;
861   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
862
863   Property::Map propertyMap;
864   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
865   propertyMap.Insert(ImageVisual::Property::URL, TEST_SMALL_IMAGE_FILE_NAME);
866   propertyMap.Insert(ImageVisual::Property::DESIRED_WIDTH, width);
867   propertyMap.Insert(ImageVisual::Property::DESIRED_HEIGHT, height);
868   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
869   propertyMap.Insert(ImageVisual::Property::PIXEL_AREA, pixelArea);
870   propertyMap.Insert(ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT);
871   propertyMap.Insert(ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT);
872   propertyMap.Insert(ImageVisual::Property::ATLASING, true);
873
874   Visual::Base visual = factory.CreateVisual(propertyMap);
875   DALI_TEST_CHECK(visual);
876
877   TestGlAbstraction& gl           = application.GetGlAbstraction();
878   TraceCallStack&    textureTrace = gl.GetTextureTrace();
879   textureTrace.Enable(true);
880   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
881   texParameterTrace.Enable(true);
882
883   DummyControl      actor     = DummyControl::New();
884   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
885   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
886   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
887   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
888   application.GetScene().Add(actor);
889
890   // loading started
891   application.SendNotification();
892   application.Render();
893
894   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
895
896   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
897
898   // WITH atlasing, the wrapping is handled manually in shader, so the following gl function should not be called
899   std::stringstream out;
900   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
901   DALI_TEST_CHECK(!texParameterTrace.FindMethodAndParams("TexParameteri", out.str()));
902   out.str("");
903   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
904   DALI_TEST_CHECK(!texParameterTrace.FindMethodAndParams("TexParameteri", out.str()));
905
906   // test the uniforms which used to handle the wrap mode
907   Renderer renderer = actor.GetRendererAt(0u);
908   DALI_TEST_CHECK(renderer);
909
910   Property::Value pixelAreaValue = renderer.GetProperty(renderer.GetPropertyIndex("pixelArea"));
911   DALI_TEST_EQUALS(pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION);
912   Vector4 pixelAreaUniform;
913   DALI_TEST_CHECK(gl.GetUniformValue<Vector4>("pixelArea", pixelAreaUniform));
914   DALI_TEST_EQUALS(pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION);
915
916   Property::Value wrapModeValue = renderer.GetProperty(renderer.GetPropertyIndex("wrapMode"));
917   Vector2         wrapMode(WrapMode::MIRRORED_REPEAT - 1, WrapMode::REPEAT - 1);
918   DALI_TEST_EQUALS(wrapModeValue.Get<Vector2>(), wrapMode, TEST_LOCATION);
919   Vector2 wrapModeUniform;
920   DALI_TEST_CHECK(gl.GetUniformValue<Vector2>("wrapMode", wrapModeUniform));
921   DALI_TEST_EQUALS(wrapMode, wrapModeUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION);
922
923   actor.Unparent();
924   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
925
926   END_TEST;
927 }
928
929 int UtcDaliImageVisualCustomWrapModeNoAtlas(void)
930 {
931   ToolkitTestApplication application;
932   tet_infoline("Request image visual with a Property::Map, test custom wrap mode and pixel area without atlasing");
933
934   static std::vector<UniformData> customUniforms =
935     {
936       UniformData("pixelArea", Property::Type::VECTOR4),
937     };
938
939   TestGraphicsController& graphics = application.GetGraphicsController();
940   graphics.AddCustomUniforms(customUniforms);
941
942   VisualFactory factory = VisualFactory::Get();
943   DALI_TEST_CHECK(factory);
944
945   // Test wrap mode without atlasing. Image with a size bigger than 512*512 will NOT be uploaded as a part of the atlas.
946   const int     width  = 600;
947   const int     height = 600;
948   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
949
950   Property::Map propertyMap;
951   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
952   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
953   propertyMap.Insert(ImageVisual::Property::DESIRED_WIDTH, width);
954   propertyMap.Insert(ImageVisual::Property::DESIRED_HEIGHT, height);
955   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
956   propertyMap.Insert(ImageVisual::Property::PIXEL_AREA, pixelArea);
957   propertyMap.Insert(ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT);
958   propertyMap.Insert(ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT);
959
960   Visual::Base visual = factory.CreateVisual(propertyMap);
961   DALI_TEST_CHECK(visual);
962
963   TestGlAbstraction& gl           = application.GetGlAbstraction();
964   TraceCallStack&    textureTrace = gl.GetTextureTrace();
965   textureTrace.Enable(true);
966   textureTrace.EnableLogging(true);
967   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
968   texParameterTrace.Enable(true);
969   texParameterTrace.EnableLogging(true);
970
971   DummyControl      actor     = DummyControl::New();
972   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
973   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
974   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
975   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
976   application.GetScene().Add(actor);
977
978   // loading started
979   application.SendNotification();
980   application.Render();
981   application.SendNotification();
982
983   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
984
985   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
986
987   // WITHOUT atlasing, the wrapping is handled by setting gl texture parameters
988   std::stringstream out;
989   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
990   DALI_TEST_CHECK(texParameterTrace.FindMethodAndParams("TexParameteri", out.str()));
991   out.str("");
992   out << std::hex << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
993   DALI_TEST_CHECK(texParameterTrace.FindMethodAndParams("TexParameteri", out.str()));
994
995   // test the uniforms which used to handle the wrap mode
996   Renderer renderer = actor.GetRendererAt(0u);
997   DALI_TEST_CHECK(renderer);
998
999   Property::Value pixelAreaValue = renderer.GetProperty(renderer.GetPropertyIndex("pixelArea"));
1000   DALI_TEST_EQUALS(pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION);
1001   Vector4 pixelAreaUniform;
1002   DALI_TEST_CHECK(gl.GetUniformValue<Vector4>("pixelArea", pixelAreaUniform));
1003   DALI_TEST_EQUALS(pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION);
1004
1005   Property::Index wrapModeIndex = renderer.GetPropertyIndex("wrapMode");
1006   DALI_TEST_CHECK(wrapModeIndex == Property::INVALID_INDEX);
1007
1008   actor.Unparent();
1009   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1010
1011   END_TEST;
1012 }
1013
1014 int UtcDaliImageVisualAnimateMixColor(void)
1015 {
1016   ToolkitTestApplication application;
1017   tet_infoline("Animate mix color");
1018
1019   static std::vector<UniformData> customUniforms =
1020     {
1021       UniformData("mixColor", Property::Type::VECTOR3),
1022     };
1023
1024   TestGraphicsController& graphics = application.GetGraphicsController();
1025   graphics.AddCustomUniforms(customUniforms);
1026
1027   application.GetPlatform().SetClosestImageSize(Vector2(100, 100));
1028
1029   VisualFactory factory = VisualFactory::Get();
1030   Property::Map propertyMap;
1031   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
1032   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1033   propertyMap.Insert("mixColor", Color::BLUE);
1034   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1035   Visual::Base visual = factory.CreateVisual(propertyMap);
1036
1037   DummyControl        actor     = DummyControl::New(true);
1038   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1039   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
1040
1041   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
1042   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
1043   actor.SetProperty(Actor::Property::COLOR, Color::BLACK);
1044   application.GetScene().Add(actor);
1045
1046   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1047
1048   Renderer renderer = actor.GetRendererAt(0);
1049
1050   // @todo Should we add API to make this code work again?
1051   // Property::Index index = renderer.GetPropertyIndex( Visual::Property::MIX_COLOR );
1052
1053   Property::Value blendModeValue = renderer.GetProperty(Renderer::Property::BLEND_MODE);
1054   DALI_TEST_EQUALS(blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION);
1055
1056   const Vector4 TARGET_MIX_COLOR(1.0f, 0.0f, 0.0f, 0.5f);
1057
1058   Property::Map map;
1059   map["target"]       = "testVisual";
1060   map["property"]     = "mixColor";
1061   map["initialValue"] = Color::MAGENTA;
1062   map["targetValue"]  = TARGET_MIX_COLOR;
1063   map["animator"]     = Property::Map()
1064                       .Add("alphaFunction", "LINEAR")
1065                       .Add("timePeriod", Property::Map().Add("delay", 0.0f).Add("duration", 4.0f));
1066
1067   Dali::Toolkit::TransitionData transition = TransitionData::New(map);
1068
1069   Animation animation = dummyImpl.CreateTransition(transition);
1070
1071   animation.AnimateTo(Property(actor, Actor::Property::COLOR), Color::WHITE);
1072   animation.Play();
1073
1074   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1075   glAbstraction.EnableEnableDisableCallTrace(true);
1076   TraceCallStack&    glEnableStack = glAbstraction.GetEnableDisableTrace();
1077   std::ostringstream blendStr;
1078   blendStr << std::hex << GL_BLEND;
1079
1080   application.SendNotification();
1081   application.Render(0);     // Ensure animation starts
1082   application.Render(2000u); // Halfway point
1083   Vector3 testColor(1.0f, 0.0f, 0.5f);
1084
1085   // uColor.a should be actor's alpha * mixColor.a.
1086   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Vector4(0.5f, 0.5f, 0.5f, 0.75f)), true, TEST_LOCATION);
1087   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", testColor), true, TEST_LOCATION);
1088
1089   DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Enable", blendStr.str()));
1090
1091   glEnableStack.Reset();
1092
1093   application.SendNotification();
1094   application.Render(2000u);
1095
1096   application.SendNotification();
1097   application.Render();
1098   application.SendNotification();
1099
1100   DALI_TEST_EQUALS(actor.GetCurrentProperty<Vector4>(Actor::Property::COLOR), Color::WHITE, TEST_LOCATION);
1101   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Vector4(1.0f, 1.0f, 1.0f, 0.5f)), true, TEST_LOCATION);
1102   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", Vector3(TARGET_MIX_COLOR)), true, TEST_LOCATION);
1103
1104   // GL_BLEND should not be changed: Keep enabled
1105   // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
1106   //  DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
1107   DALI_TEST_CHECK(!glEnableStack.FindMethodAndParams("Disable", blendStr.str()));
1108
1109   TestMixColor(visual, Visual::Property::MIX_COLOR, TARGET_MIX_COLOR);
1110
1111   END_TEST;
1112 }
1113
1114 int UtcDaliImageVisualAnimateOpacity(void)
1115 {
1116   ToolkitTestApplication application;
1117   tet_infoline("Animate image visual opacity");
1118
1119   application.GetPlatform().SetClosestImageSize(Vector2(100, 100));
1120
1121   VisualFactory factory = VisualFactory::Get();
1122   Property::Map propertyMap;
1123   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
1124   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1125   propertyMap.Insert("opacity", 0.5f);
1126   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1127   Visual::Base visual = factory.CreateVisual(propertyMap);
1128
1129   DummyControl        actor     = DummyControl::New(true);
1130   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1131   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
1132
1133   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
1134   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
1135   actor.SetProperty(Actor::Property::COLOR, Color::BLACK);
1136   application.GetScene().Add(actor);
1137
1138   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1139
1140   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1141   glAbstraction.EnableEnableDisableCallTrace(true);
1142   TraceCallStack&    glEnableStack = glAbstraction.GetEnableDisableTrace();
1143   std::ostringstream blendStr;
1144   blendStr << std::hex << GL_BLEND;
1145
1146   application.SendNotification();
1147   application.Render();
1148
1149   DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Enable", blendStr.str()));
1150
1151   {
1152     tet_infoline("Test that the opacity can be increased to full via animation, and that the blend mode is set appropriately at the start and end of the animation.");
1153
1154     Property::Map map;
1155     map["target"]      = "testVisual";
1156     map["property"]    = "opacity";
1157     map["targetValue"] = 1.0f;
1158     map["animator"]    = Property::Map()
1159                         .Add("alphaFunction", "LINEAR")
1160                         .Add("timePeriod", Property::Map().Add("delay", 0.0f).Add("duration", 4.0f));
1161
1162     Dali::Toolkit::TransitionData transition = TransitionData::New(map);
1163     Animation                     animation  = dummyImpl.CreateTransition(transition);
1164     animation.Play();
1165
1166     glEnableStack.Reset();
1167
1168     application.SendNotification();
1169     application.Render(0);          // Ensure animation starts
1170     application.Render(2000u);      // Halfway point through animation
1171     application.SendNotification(); // Handle any signals
1172
1173     Vector4 color;
1174     DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1175     DALI_TEST_EQUALS(color.a, 0.75f, TEST_LOCATION);
1176
1177     application.Render(2001u);      // end
1178     application.SendNotification(); // ensure animation finished signal is sent
1179
1180     DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1181     DALI_TEST_EQUALS(color.a, 1.0f, TEST_LOCATION);
1182
1183     // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
1184     //    DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
1185     DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Disable", blendStr.str()));
1186   }
1187
1188   {
1189     tet_infoline("Test that the opacity can be reduced via animation, and that the blend mode is set appropriately at the start and end of the animation.");
1190
1191     Property::Map map;
1192     map["target"]      = "testVisual";
1193     map["property"]    = Visual::Property::OPACITY;
1194     map["targetValue"] = 0.1f;
1195     map["animator"]    = Property::Map()
1196                         .Add("alphaFunction", "LINEAR")
1197                         .Add("timePeriod", Property::Map().Add("delay", 0.0f).Add("duration", 4.0f));
1198
1199     Dali::Toolkit::TransitionData transition = TransitionData::New(map);
1200     Animation                     animation  = dummyImpl.CreateTransition(transition);
1201     animation.Play();
1202
1203     glEnableStack.Reset();
1204
1205     application.SendNotification();
1206     application.Render(0);     // Ensure animation starts
1207     application.Render(2000u); // Halfway point
1208     application.SendNotification();
1209
1210     Vector4 color;
1211     DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1212     DALI_TEST_EQUALS(color.a, 0.55f, TEST_LOCATION);
1213
1214     DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Enable", blendStr.str()));
1215
1216     glEnableStack.Reset();
1217
1218     application.Render(2016u); // end
1219     application.SendNotification();
1220
1221     DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1222     DALI_TEST_EQUALS(color.a, 0.1f, TEST_LOCATION);
1223
1224     // GL_BLEND should not be changed: Keep enabled
1225     // @todo
1226     // TODO: Temporarily commented out the line below when caching is disabled. Will need to add it back.
1227     //    DALI_TEST_CHECK( !glEnableStack.FindMethodAndParams( "Enable", blendStr.str() ) );
1228     DALI_TEST_CHECK(!glEnableStack.FindMethodAndParams("Disable", blendStr.str()));
1229   }
1230
1231   END_TEST;
1232 }
1233
1234 int UtcDaliImageVisualAnimateOpacity02(void)
1235 {
1236   ToolkitTestApplication application;
1237   tet_infoline("Animate image visual opacity");
1238
1239   application.GetPlatform().SetClosestImageSize(Vector2(100, 100));
1240
1241   VisualFactory factory = VisualFactory::Get();
1242   Property::Map propertyMap;
1243   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
1244   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1245   propertyMap.Insert("opacity", 0.5f);
1246   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1247   Visual::Base visual = factory.CreateVisual(propertyMap);
1248
1249   DummyControl        actor     = DummyControl::New(true);
1250   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1251   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
1252
1253   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
1254   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
1255   actor.SetProperty(Actor::Property::COLOR, Color::BLACK);
1256
1257   tet_infoline("Test that the opacity doesn't animate when actor not staged");
1258
1259   Property::Array array;
1260
1261   Property::Map map;
1262   map["target"]       = "testVisual";
1263   map["property"]     = "opacity";
1264   map["initialValue"] = 0.0f;
1265   map["targetValue"]  = 1.0f;
1266   map["animator"]     = Property::Map()
1267                       .Add("alphaFunction", "LINEAR")
1268                       .Add("timePeriod", Property::Map().Add("delay", 0.0f).Add("duration", 4.0f));
1269
1270   Property::Map map2;
1271   map2["target"]      = "testVisual";
1272   map2["property"]    = "size";
1273   map2["targetValue"] = Vector2(1.0f, 1.0f);
1274
1275   array.Add(map).Add(map2);
1276
1277   Dali::Toolkit::TransitionData transition = TransitionData::New(array);
1278   Animation                     animation  = dummyImpl.CreateTransition(transition);
1279
1280   application.GetScene().Add(actor);
1281   application.SendNotification();
1282   application.Render(0); // Ensure animation starts
1283
1284   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1285
1286   Renderer        renderer       = actor.GetRendererAt(0);
1287   Property::Value blendModeValue = renderer.GetProperty(Renderer::Property::BLEND_MODE);
1288   DALI_TEST_EQUALS(blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION);
1289
1290   animation = dummyImpl.CreateTransition(transition);
1291   animation.Play();
1292
1293   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
1294   glAbstraction.EnableEnableDisableCallTrace(true);
1295   TraceCallStack&    glEnableStack = glAbstraction.GetEnableDisableTrace();
1296   std::ostringstream blendStr;
1297   blendStr << std::hex << GL_BLEND;
1298
1299   application.SendNotification();
1300   application.Render(0);          // Ensure animation starts
1301   application.Render(2000u);      // Halfway point through animation
1302   application.SendNotification(); // Handle any signals
1303
1304   DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Enable", blendStr.str()));
1305
1306   Vector4 color;
1307   DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1308   DALI_TEST_EQUALS(color.a, 0.5f, TEST_LOCATION);
1309
1310   glEnableStack.Reset();
1311
1312   application.Render(2001u);      // end
1313   application.SendNotification(); // ensure animation finished signal is sent
1314
1315   DALI_TEST_CHECK(application.GetGlAbstraction().GetUniformValue<Vector4>("uColor", color));
1316   DALI_TEST_EQUALS(color.a, 1.0f, TEST_LOCATION);
1317
1318   DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Disable", blendStr.str()));
1319
1320   END_TEST;
1321 }
1322
1323 int UtcDaliImageVisualAnimatePixelArea(void)
1324 {
1325   ToolkitTestApplication application;
1326   tet_infoline("ImageVisual animate pixel area");
1327
1328   static std::vector<UniformData> customUniforms =
1329     {
1330       UniformData("pixelArea", Property::Type::VECTOR4),
1331     };
1332
1333   TestGraphicsController& graphics = application.GetGraphicsController();
1334   graphics.AddCustomUniforms(customUniforms);
1335
1336   application.GetPlatform().SetClosestImageSize(Vector2(100, 100));
1337
1338   VisualFactory factory = VisualFactory::Get();
1339   Property::Map propertyMap;
1340   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
1341   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1342   propertyMap.Insert("mixColor", Color::BLUE);
1343   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1344   Visual::Base visual = factory.CreateVisual(propertyMap);
1345
1346   DummyControl        actor     = DummyControl::New(true);
1347   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1348   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
1349
1350   actor.SetProperty(Actor::Property::SIZE, Vector2(2000, 2000));
1351   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
1352   actor.SetProperty(Actor::Property::COLOR, Color::BLACK);
1353   application.GetScene().Add(actor);
1354
1355   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1356
1357   Renderer renderer = actor.GetRendererAt(0);
1358   // @todo Implement this feature?
1359   //Property::Index index = renderer.GetPropertyIndex( Visual::Property::MIX_COLOR );
1360   //tet_infoline("Test that the renderer has the mixColor property");
1361   //DALI_TEST_CHECK( index != Property::INVALID_INDEX );
1362
1363   // TransitionData only takes string keys
1364   Property::Map map;
1365   map["target"]       = "testVisual";
1366   map["property"]     = "pixelArea";
1367   map["initialValue"] = Vector4(0, 0, 0, 1);
1368   map["targetValue"]  = Vector4(0, 0, 1, 1); // Animate width from zero to full
1369   map["animator"]     = Property::Map()
1370                       .Add("alphaFunction", "LINEAR")
1371                       .Add("timePeriod", Property::Map().Add("delay", 0.0f).Add("duration", 4.0f));
1372
1373   Dali::Toolkit::TransitionData transition = TransitionData::New(map);
1374
1375   Animation animation = dummyImpl.CreateTransition(transition);
1376   animation.AnimateTo(Property(actor, Actor::Property::COLOR), Color::WHITE);
1377   animation.Play();
1378
1379   application.SendNotification();
1380   application.Render(0);     // Ensure animation starts
1381   application.Render(2000u); // Halfway point
1382
1383   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4(0.0f, 0.0f, 0.5f, 1.0f)), true, TEST_LOCATION);
1384
1385   application.Render(2000u); // End of animation
1386
1387   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4(0.0f, 0.0f, 1.0f, 1.0f)), true, TEST_LOCATION);
1388
1389   END_TEST;
1390 }
1391
1392 int UtcDaliImageVisualTextureCancelRemoteLoad(void)
1393 {
1394   ToolkitTestApplication application;
1395   tet_infoline("Request remote image visual, then destroy visual to cancel load");
1396
1397   Property::Map propertyMap;
1398   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1399   propertyMap.Insert(ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME);
1400
1401   TestGlAbstraction& gl           = application.GetGlAbstraction();
1402   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1403   textureTrace.Enable(true);
1404   TraceCallStack& drawTrace = gl.GetDrawTrace();
1405   drawTrace.Enable(true);
1406
1407   Actor actor = CreateActorWithImageVisual(propertyMap);
1408   application.GetScene().Add(actor);
1409   application.SendNotification();
1410
1411   application.GetScene().Remove(actor);
1412   application.SendNotification();
1413
1414   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1415   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
1416   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), false, TEST_LOCATION);
1417   DALI_TEST_EQUALS(drawTrace.FindMethod("DrawArrays"), false, TEST_LOCATION);
1418
1419   END_TEST;
1420 }
1421
1422 int UtcDaliImageVisualTextureCancelAsyncLoad(void)
1423 {
1424   ToolkitTestApplication application;
1425   tet_infoline("Load image asynchronously, cancel loading, then load again");
1426
1427   VisualFactory factory = VisualFactory::Get();
1428   DALI_TEST_CHECK(factory);
1429
1430   Property::Map propertyMap;
1431   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1432   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1433
1434   Visual::Base visual = factory.CreateVisual(propertyMap);
1435   DALI_TEST_CHECK(visual);
1436
1437   TestGlAbstraction& gl           = application.GetGlAbstraction();
1438   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1439   textureTrace.Enable(true);
1440   TraceCallStack& drawTrace = gl.GetDrawTrace();
1441   drawTrace.Enable(true);
1442
1443   DummyControl      actor     = DummyControl::New();
1444   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1445   dummyImpl.RegisterVisual(Control::Property::BACKGROUND, visual);
1446
1447   application.GetScene().Add(actor);
1448
1449   // Cancel loading
1450   application.GetScene().Remove(actor);
1451
1452   application.GetScene().Add(actor);
1453
1454   // Create another visual with the same image
1455   visual = factory.CreateVisual(propertyMap);
1456   DALI_TEST_CHECK(visual);
1457
1458   dummyImpl.RegisterVisual(Control::Property::BACKGROUND, visual);
1459
1460   application.SendNotification();
1461   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1462
1463   application.SendNotification();
1464   application.Render();
1465
1466   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1467   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
1468   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1469   DALI_TEST_EQUALS(drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION);
1470
1471   END_TEST;
1472 }
1473
1474 int UtcDaliImageVisualSetInvalidAsyncImage(void)
1475 {
1476   ToolkitTestApplication application;
1477   tet_infoline("Request image visual with invalid images - should draw broken.png");
1478
1479   VisualFactory factory = VisualFactory::Get();
1480   DALI_TEST_CHECK(factory);
1481
1482   Property::Map propertyMap;
1483   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1484   propertyMap.Insert(ImageVisual::Property::URL, TEST_INVALID_FILE_NAME);
1485
1486   Visual::Base visual = factory.CreateVisual(propertyMap);
1487   DALI_TEST_CHECK(visual);
1488
1489   TestGlAbstraction& gl           = application.GetGlAbstraction();
1490   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1491   textureTrace.Enable(true);
1492
1493   DummyControl      actor     = DummyControl::New();
1494   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1495   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1496
1497   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1498   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1499
1500   application.GetScene().Add(actor);
1501
1502   application.SendNotification();
1503   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1504
1505   application.SendNotification();
1506   application.Render();
1507
1508   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1509   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1510
1511   application.GetScene().Remove(actor);
1512   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1513
1514   END_TEST;
1515 }
1516
1517 int UtcDaliImageVisualSetInvalidSyncImage(void)
1518 {
1519   ToolkitTestApplication application;
1520   tet_infoline("Request image visual with invalid images - should draw broken.png");
1521
1522   VisualFactory factory = VisualFactory::Get();
1523   DALI_TEST_CHECK(factory);
1524
1525   Property::Map propertyMap;
1526   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1527   propertyMap.Insert(ImageVisual::Property::URL, TEST_INVALID_FILE_NAME);
1528   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1529
1530   Visual::Base visual = factory.CreateVisual(propertyMap);
1531   DALI_TEST_CHECK(visual);
1532
1533   TestGlAbstraction& gl           = application.GetGlAbstraction();
1534   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1535   textureTrace.Enable(true);
1536
1537   DummyControl      actor     = DummyControl::New();
1538   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1539   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1540
1541   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1542   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1543
1544   application.GetScene().Add(actor);
1545
1546   application.SendNotification();
1547   application.Render();
1548
1549   // Check resource status
1550   Visual::ResourceStatus status = actor.GetVisualResourceStatus(Control::CONTROL_PROPERTY_END_INDEX + 1);
1551   DALI_TEST_EQUALS(status, Visual::ResourceStatus::FAILED, TEST_LOCATION);
1552
1553   // The broken image should be shown.
1554   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1555   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1556
1557   application.GetScene().Remove(actor);
1558   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1559
1560   END_TEST;
1561 }
1562
1563 int UtcDaliImageVisualSetInvalidRemoteImage(void)
1564 {
1565   ToolkitTestApplication application;
1566   tet_infoline("Request image visual with invalid images - should draw broken.png");
1567
1568   VisualFactory factory = VisualFactory::Get();
1569   DALI_TEST_CHECK(factory);
1570
1571   // Local invalid file, asynchronous loading
1572   Property::Map propertyMap;
1573   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1574   propertyMap.Insert(ImageVisual::Property::URL, TEST_REMOTE_INVALID_FILE_NAME);
1575
1576   Visual::Base visual = factory.CreateVisual(propertyMap);
1577   DALI_TEST_CHECK(visual);
1578
1579   TestGlAbstraction& gl           = application.GetGlAbstraction();
1580   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1581   textureTrace.Enable(true);
1582
1583   DummyControl      actor     = DummyControl::New();
1584   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1585   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1586
1587   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1588   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1589
1590   application.GetScene().Add(actor);
1591
1592   application.SendNotification();
1593   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1594
1595   application.SendNotification();
1596   application.Render();
1597
1598   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1599   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1600
1601   application.GetScene().Remove(actor);
1602   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1603
1604   END_TEST;
1605 }
1606
1607 int UtcDaliImageVisualAlphaMask(void)
1608 {
1609   ToolkitTestApplication application;
1610   tet_infoline("Request image visual with a Property::Map containing an Alpha mask");
1611
1612   VisualFactory factory = VisualFactory::Get();
1613   DALI_TEST_CHECK(factory);
1614
1615   Property::Map propertyMap;
1616   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1617   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
1618   propertyMap.Insert(ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME);
1619
1620   Visual::Base visual = factory.CreateVisual(propertyMap);
1621   DALI_TEST_CHECK(visual);
1622
1623   Property::Map testMap;
1624   visual.CreatePropertyMap(testMap);
1625   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL), Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION);
1626
1627   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1628   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1629
1630   TestGlAbstraction& gl           = application.GetGlAbstraction();
1631   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1632   textureTrace.Enable(true);
1633
1634   DummyControl      actor     = DummyControl::New();
1635   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1636   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1637
1638   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1639   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1640   DALI_TEST_EQUALS(actor.IsResourceReady(), false, TEST_LOCATION);
1641
1642   application.GetScene().Add(actor);
1643   application.SendNotification();
1644   application.Render();
1645
1646   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
1647
1648   application.SendNotification();
1649   application.Render();
1650
1651   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1652   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1653   DALI_TEST_EQUALS(actor.IsResourceReady(), true, TEST_LOCATION);
1654
1655   dummyImpl.UnregisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1);
1656   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1657
1658   END_TEST;
1659 }
1660
1661 int UtcDaliImageVisualSynchronousLoadAlphaMask(void)
1662 {
1663   ToolkitTestApplication application;
1664   tet_infoline("Request image visual with a Property::Map containing an Alpha mask with synchronous loading");
1665
1666   VisualFactory factory = VisualFactory::Get();
1667   DALI_TEST_CHECK(factory);
1668
1669   Property::Map propertyMap;
1670   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1671   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
1672   propertyMap.Insert(ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME);
1673   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
1674
1675   Visual::Base visual = factory.CreateVisual(propertyMap);
1676   DALI_TEST_CHECK(visual);
1677
1678   Property::Map testMap;
1679   visual.CreatePropertyMap(testMap);
1680   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL), Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION);
1681
1682   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1683   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1684
1685   TestGlAbstraction& gl           = application.GetGlAbstraction();
1686   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1687   textureTrace.Enable(true);
1688
1689   DummyControl      actor     = DummyControl::New();
1690   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1691   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1692
1693   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1694   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1695   DALI_TEST_EQUALS(actor.IsResourceReady(), false, TEST_LOCATION);
1696
1697   application.GetScene().Add(actor);
1698
1699   // Do not wait for any EventThreadTrigger in synchronous alpha mask.
1700
1701   application.SendNotification();
1702   application.Render();
1703
1704   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1705   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1706   DALI_TEST_EQUALS(actor.IsResourceReady(), true, TEST_LOCATION);
1707
1708   dummyImpl.UnregisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1);
1709   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1710
1711   END_TEST;
1712 }
1713
1714 int UtcDaliImageVisualRemoteAlphaMask(void)
1715 {
1716   ToolkitTestApplication application;
1717   tet_infoline("Request image visual with a Property::Map containing an Alpha mask");
1718
1719   VisualFactory factory = VisualFactory::Get();
1720   DALI_TEST_CHECK(factory);
1721
1722   const std::string MASK_IMAGE = TEST_REMOTE_IMAGE_FILE_NAME;
1723
1724   Property::Map propertyMap;
1725   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1726   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
1727   propertyMap.Insert(ImageVisual::Property::ALPHA_MASK_URL, MASK_IMAGE);
1728
1729   Visual::Base visual = factory.CreateVisual(propertyMap);
1730   DALI_TEST_CHECK(visual);
1731
1732   Property::Map testMap;
1733   visual.CreatePropertyMap(testMap);
1734
1735   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL), Property::Value(MASK_IMAGE), TEST_LOCATION);
1736
1737   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1738   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1739
1740   TestGlAbstraction& gl           = application.GetGlAbstraction();
1741   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1742   textureTrace.Enable(true);
1743
1744   DummyControl      actor     = DummyControl::New();
1745   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1746   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1747
1748   DALI_TEST_EQUALS(actor.IsResourceReady(), false, TEST_LOCATION);
1749
1750   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1751   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1752
1753   application.GetScene().Add(actor);
1754   application.SendNotification();
1755   application.Render();
1756
1757   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
1758
1759   application.SendNotification();
1760   application.Render();
1761
1762   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1763   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1764   DALI_TEST_EQUALS(actor.IsResourceReady(), true, TEST_LOCATION);
1765
1766   END_TEST;
1767 }
1768
1769 int UtcDaliImageVisualAlphaMaskCrop(void)
1770 {
1771   ToolkitTestApplication application;
1772   tet_infoline("Request image visual with an Alpha mask and scale/cropping");
1773
1774   VisualFactory factory = VisualFactory::Get();
1775   DALI_TEST_CHECK(factory);
1776
1777   Property::Map propertyMap;
1778   propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE);
1779   propertyMap.Insert(ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME);
1780   propertyMap.Insert(ImageVisual::Property::ALPHA_MASK_URL, TEST_MASK_IMAGE_FILE_NAME);
1781   propertyMap.Insert(ImageVisual::Property::MASK_CONTENT_SCALE, 1.6f);
1782   propertyMap.Insert(ImageVisual::Property::CROP_TO_MASK, true);
1783
1784   Visual::Base visual = factory.CreateVisual(propertyMap);
1785   DALI_TEST_CHECK(visual);
1786
1787   Property::Map testMap;
1788   visual.CreatePropertyMap(testMap);
1789   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::ALPHA_MASK_URL), Property::Value(TEST_MASK_IMAGE_FILE_NAME), TEST_LOCATION);
1790   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::MASK_CONTENT_SCALE), Property::Value(1.6f), TEST_LOCATION);
1791   DALI_TEST_EQUALS(*testMap.Find(ImageVisual::Property::CROP_TO_MASK), Property::Value(true), TEST_LOCATION);
1792
1793   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
1794   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
1795
1796   TestGlAbstraction& gl           = application.GetGlAbstraction();
1797   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1798   textureTrace.Enable(true);
1799
1800   DummyControl      actor     = DummyControl::New();
1801   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
1802   dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual);
1803
1804   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1805   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
1806   DALI_TEST_EQUALS(actor.IsResourceReady(), false, TEST_LOCATION);
1807
1808   application.GetScene().Add(actor);
1809   application.SendNotification();
1810   application.Render();
1811
1812   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
1813
1814   application.SendNotification();
1815   application.Render();
1816
1817   Vector2 size;
1818   visual.GetNaturalSize(size);
1819
1820   DALI_TEST_EQUALS(size, Vector2(100.0f, 100.0f), 0.001f, TEST_LOCATION);
1821   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1822   DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
1823   DALI_TEST_EQUALS(actor.IsResourceReady(), true, TEST_LOCATION);
1824
1825   END_TEST;
1826 }
1827
1828 int UtcDaliImageVisualReleasePolicy01(void)
1829 {
1830   ToolkitTestApplication application;
1831   tet_infoline("UtcDaliImageVisualReleasePolicy01 Detached Policy, disabling visual with this policy deletes texture");
1832
1833   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DETACHED);
1834   DALI_TEST_CHECK(imageVisual);
1835
1836   // Set up debug trace
1837   TestGlAbstraction& gl           = application.GetGlAbstraction();
1838   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1839   textureTrace.Enable(true);
1840
1841   tet_infoline("Register visual with control and ensure it has the only handle");
1842   DummyControl        actor     = DummyControl::New(true);
1843   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1844   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
1845   imageVisual.Reset();
1846
1847   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1848
1849   application.SendNotification();
1850   application.Render(0);
1851   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1852   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
1853
1854   application.GetScene().Add(actor);
1855
1856   // Wait for image to load
1857   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1858
1859   application.SendNotification();
1860   application.Render(0);
1861   // Test renderer and texture created
1862   tet_infoline("Confirm texture created");
1863   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1864   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
1865
1866   tet_infoline("Disable visual causing the texture to be deleted");
1867   dummyImpl.EnableVisual(DummyControl::Property::TEST_VISUAL, false);
1868
1869   application.SendNotification();
1870   application.Render(0);
1871   // Test renderer and textures removed.
1872   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1873   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
1874
1875   END_TEST;
1876 }
1877
1878 int UtcDaliImageVisualReleasePolicy02(void)
1879 {
1880   ToolkitTestApplication application;
1881   tet_infoline("UtcDaliImageVisualReleasePolicy02 Destroyed Policy, Texture should be deleted when visual destroyed");
1882
1883   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED);
1884   DALI_TEST_CHECK(imageVisual);
1885
1886   // Setup debug trace
1887   TestGlAbstraction& gl           = application.GetGlAbstraction();
1888   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1889   textureTrace.Enable(true);
1890
1891   tet_infoline("Register visual with control and ensure it has the only handle");
1892   DummyControl        actor     = DummyControl::New(true);
1893   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1894   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
1895   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1896
1897   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1898
1899   application.SendNotification();
1900   application.Render(0);
1901   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1902   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
1903
1904   application.GetScene().Add(actor);
1905
1906   // Wait for image to load
1907   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1908
1909   application.SendNotification();
1910   application.Render(0);
1911   // Test renderer and texture created
1912   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1913   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
1914
1915   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
1916   tet_infoline("Destroy visual by UnRegistering visual with control, check renderer is destroyed");
1917   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
1918   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1919   application.SendNotification();
1920   application.Render();
1921
1922   // Test texture removed after visual destroyed.
1923   tet_infoline("Ensure texture is deleted after visual destroyed");
1924   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
1925
1926   END_TEST;
1927 }
1928
1929 int UtcDaliImageVisualReleasePolicy03(void)
1930 {
1931   ToolkitTestApplication application;
1932   tet_infoline("UtcDaliImageVisualReleasePolicy03 Never Policy, texture should not be deleted after visual destroyed");
1933
1934   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER);
1935   DALI_TEST_CHECK(imageVisual);
1936
1937   TestGlAbstraction& gl           = application.GetGlAbstraction();
1938   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1939   textureTrace.Enable(true);
1940
1941   tet_infoline("Register visual with control and ensure it has the only handle");
1942   DummyControl        actor     = DummyControl::New(true);
1943   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1944   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
1945   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
1946
1947   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
1948
1949   application.SendNotification();
1950   application.Render(0);
1951   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1952   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
1953
1954   application.GetScene().Add(actor);
1955
1956   // Wait for image to load
1957   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
1958
1959   application.SendNotification();
1960   application.Render(0);
1961   // Test renderer and texture created
1962   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
1963   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
1964
1965   tet_infoline("Destroy visual by UnRegistering visual with control, check renderer is destroyed");
1966   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
1967   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
1968   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
1969   application.SendNotification();
1970   application.Render();
1971
1972   tet_infoline("Ensure texture is not deleted as policy is set to NEVER");
1973   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
1974
1975   END_TEST;
1976 }
1977
1978 int UtcDaliImageVisualReleasePolicy04(void)
1979 {
1980   ToolkitTestApplication application;
1981   tet_infoline("UtcDaliImageVisualReleasePolicy04 Two visuals with different policies sharing a texture");
1982
1983   tet_infoline("Create first visual with Never release policy");
1984   Visual::Base imageVisualNever = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER);
1985
1986   tet_infoline("Create second visual with Destroyed release policy");
1987   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED);
1988
1989   // Set up trace debug
1990   TestGlAbstraction& gl           = application.GetGlAbstraction();
1991   TraceCallStack&    textureTrace = gl.GetTextureTrace();
1992   textureTrace.Enable(true);
1993
1994   tet_infoline("Register visuals with control and ensure it has the only handles");
1995   DummyControl        actor     = DummyControl::New(true);
1996   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
1997   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisualNever);
1998   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL2, imageVisualDestroyed);
1999   imageVisualNever.Reset();     // reduce ref count so only the control keeps the visual alive.
2000   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
2001
2002   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2003
2004   // Test initially zero renderers
2005   application.SendNotification();
2006   application.Render(0);
2007   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2008   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2009
2010   application.GetScene().Add(actor);
2011
2012   // Wait for image to load
2013   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2014
2015   application.SendNotification();
2016   application.Render(0);
2017   tet_infoline("Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer");
2018   DALI_TEST_EQUALS(actor.GetRendererCount(), 2u, TEST_LOCATION);
2019   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2020
2021   // Test renderer removed when visual destroyed
2022   DALI_TEST_CHECK(actor.GetRendererCount() == 2u);
2023   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL2); // TEST_VISUAL2 no longer requires the texture as release policy DESTROYED
2024   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
2025   application.SendNotification();
2026   application.Render();
2027
2028   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
2029   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2030
2031   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
2032   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2033   application.SendNotification();
2034   application.Render();
2035
2036   tet_infoline("Ensure a texture is not deleted as second visual used the NEVER release policy");
2037   // Test texture was not deleted as TEST_VISUAL release policy is NEVER so it is still required.
2038   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2039
2040   END_TEST;
2041 }
2042
2043 int UtcDaliImageVisualReleasePolicy05(void)
2044 {
2045   ToolkitTestApplication application;
2046   tet_infoline("UtcDaliImageVisualReleasePolicy05 Testing settung by string currents correct enum");
2047
2048   VisualFactory factory = VisualFactory::Get();
2049
2050   Property::Map propertyMapNeverReleasePolicy;
2051   propertyMapNeverReleasePolicy.Insert(Visual::Property::TYPE, Visual::IMAGE);
2052   propertyMapNeverReleasePolicy.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
2053   propertyMapNeverReleasePolicy.Insert(ImageVisual::Property::DESIRED_WIDTH, 20);
2054   propertyMapNeverReleasePolicy.Insert(ImageVisual::Property::DESIRED_HEIGHT, 30);
2055   propertyMapNeverReleasePolicy.Insert("releasePolicy", "never");
2056
2057   Visual::Base imageVisualNever = factory.CreateVisual(propertyMapNeverReleasePolicy);
2058
2059   Property::Map resultMap;
2060   imageVisualNever.CreatePropertyMap(resultMap);
2061   DALI_TEST_CHECK(!resultMap.Empty());
2062
2063   DALI_TEST_EQUALS((resultMap.Find(ImageVisual::Property::RELEASE_POLICY))->Get<int>(), (int)ImageVisual::ReleasePolicy::NEVER, TEST_LOCATION);
2064
2065   END_TEST;
2066 }
2067
2068 int UtcDaliImageVisualReleasePolicy06(void)
2069 {
2070   ToolkitTestApplication application;
2071   tet_infoline("UtcDaliImageVisualReleasePolicy06 Never Policy, texture should not be affected by Disabling and Enabling visual");
2072
2073   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::NEVER);
2074   DALI_TEST_CHECK(imageVisual);
2075
2076   TestGlAbstraction& gl           = application.GetGlAbstraction();
2077   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2078   textureTrace.Enable(true);
2079
2080   tet_infoline("Register visual with control and ensure it has the only handle");
2081   DummyControl        actor     = DummyControl::New(true);
2082   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2083   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2084   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2085
2086   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2087
2088   application.SendNotification();
2089   application.Render(0);
2090   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2091   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2092
2093   application.GetScene().Add(actor);
2094
2095   // Wait for image to load
2096   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2097
2098   application.SendNotification();
2099   application.Render(0);
2100   // Test renderer and texture created
2101   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
2102   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2103   textureTrace.Reset();
2104
2105   tet_infoline("Disable Visual and check texture not affected");
2106   dummyImpl.EnableVisual(DummyControl::Property::TEST_VISUAL, false);
2107   application.SendNotification();
2108   application.Render(0);
2109   tet_infoline("Check renderer is destroyed when visual off stage");
2110   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2111   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2112   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2113   textureTrace.Reset();
2114
2115   tet_infoline("Re-enable Visual and check texture not affected");
2116   dummyImpl.EnableVisual(DummyControl::Property::TEST_VISUAL, true);
2117   application.SendNotification();
2118   application.Render(0);
2119   tet_infoline("Check texture not affected and renderer is destroyed when visual off stage");
2120   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
2121   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2122   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2123
2124   END_TEST;
2125 }
2126
2127 int UtcDaliImageVisualReleasePolicy07(void)
2128 {
2129   ToolkitTestApplication application;
2130   tet_infoline("UtcDaliImageVisualReleasePolicy07 Two visuals with different policies sharing a texture DETACHED and DESTROYED");
2131
2132   tet_infoline("Create first visual with DESTROYED release policy");
2133   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED);
2134
2135   tet_infoline("Create second visual with DETACHED release policy");
2136   Visual::Base imageVisualDetached = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DETACHED);
2137
2138   // Set up trace debug
2139   TestGlAbstraction& gl           = application.GetGlAbstraction();
2140   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2141   textureTrace.Enable(true);
2142
2143   tet_infoline("Register visuals with control and ensure it has the only handles");
2144   DummyControl        actor     = DummyControl::New(true);
2145   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2146   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisualDestroyed);
2147   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL2, imageVisualDetached);
2148   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
2149   imageVisualDetached.Reset();  // reduce ref count so only the control keeps the visual alive.
2150
2151   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2152
2153   // Test initially zero renderers
2154   application.SendNotification();
2155   application.Render(0);
2156   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2157   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2158
2159   application.GetScene().Add(actor);
2160
2161   // Wait for image to load
2162   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2163
2164   application.SendNotification();
2165   application.Render(0);
2166   tet_infoline("Ensure a texture is created, shared amongst both visuals.  Each visual has its own renderer");
2167   DALI_TEST_EQUALS(actor.GetRendererCount(), 2u, TEST_LOCATION);
2168   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2169
2170   // Test renderer removed when visual destroyed
2171   DALI_TEST_CHECK(actor.GetRendererCount() == 2u);
2172   dummyImpl.EnableVisual(DummyControl::Property::TEST_VISUAL2, false); // TEST_VISUAL2 no longer requires the texture as release policy DETACHED
2173   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
2174   application.SendNotification();
2175   application.Render();
2176
2177   // Test texture was not deleted as TEST_VISUAL release policy is DESTROYED and is still required.
2178   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2179
2180   dummyImpl.EnableVisual(DummyControl::Property::TEST_VISUAL, false);
2181   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2182   application.SendNotification();
2183   application.Render();
2184
2185   tet_infoline("Ensure a texture is not deleted as second visual used the DESTROYED release policy");
2186   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2187
2188   END_TEST;
2189 }
2190
2191 int UtcDaliImageVisualReleasePolicy08(void)
2192 {
2193   ToolkitTestApplication application;
2194   tet_infoline("UtcDaliImageVisualReleasePolicy08 Ensure TextureSet is same after detach/attach on stage when texture used the DESTROYED release policy");
2195
2196   tet_infoline("Create first visual with DESTROYED release policy");
2197   Visual::Base imageVisualDestroyed = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::RELEASE_POLICY, ImageVisual::ReleasePolicy::DESTROYED);
2198
2199   // Set up trace debug
2200   TestGlAbstraction& gl           = application.GetGlAbstraction();
2201   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2202   textureTrace.Enable(true);
2203
2204   tet_infoline("Register visuals with control and ensure it has the only handles");
2205   DummyControl        actor     = DummyControl::New(true);
2206   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2207   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisualDestroyed);
2208   imageVisualDestroyed.Reset(); // reduce ref count so only the control keeps the visual alive.
2209
2210   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2211
2212   // Test initially zero renderers
2213   application.SendNotification();
2214   application.Render(0);
2215   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2216   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2217   textureTrace.Reset();
2218
2219   application.GetScene().Add(actor);
2220
2221   // Wait for image to load
2222   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2223
2224   application.SendNotification();
2225   application.Render(0);
2226   tet_infoline("Ensure a texture is created");
2227   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
2228   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2229   textureTrace.Reset();
2230
2231   // Ensure TextureSet is same after detach/attach on stage when texture used the DESTROYED release policy
2232   // 1. Get TextureSet
2233   TextureSet textureSetBefore = actor.GetRendererAt(0u).GetTextures();
2234
2235   // 2.Remove actor from stage. In this case, renderer also is deleted.
2236   tet_infoline("Remove actor from stage");
2237   application.GetScene().Remove(actor);
2238   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2239   application.SendNotification();
2240   application.Render();
2241
2242   tet_infoline("Ensure a texture is not deleted as visual used the DESTROYED release policy");
2243   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION);
2244   textureTrace.Reset();
2245
2246   // 3.Add actor in stage. In this case, renderer is created.
2247   tet_infoline("Add actor in stage");
2248   application.GetScene().Add(actor);
2249   DALI_TEST_CHECK(actor.GetRendererCount() == 1u);
2250   application.SendNotification();
2251   application.Render();
2252   tet_infoline("Ensure a texture is not created again");
2253   DALI_TEST_EQUALS(textureTrace.CountMethod("GenTextures"), 0, TEST_LOCATION);
2254   textureTrace.Reset();
2255
2256   // 4.Compare Texture with before and after. textureSet need to be same because release policy is the DESTROYED.
2257   tet_infoline("Ensure a textureSet is not deleted because it is used the DESTROYED release policy");
2258   TextureSet textureSetAfter = actor.GetRendererAt(0u).GetTextures();
2259   DALI_TEST_CHECK(textureSetBefore == textureSetAfter);
2260   textureSetBefore.Reset();
2261   textureSetAfter.Reset();
2262
2263   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
2264   DALI_TEST_CHECK(actor.GetRendererCount() == 0u);
2265   application.SendNotification();
2266   application.Render();
2267   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
2268
2269   END_TEST;
2270 }
2271
2272 int UtcDaliImageVisualLoadPolicy01(void)
2273 {
2274   ToolkitTestApplication application;
2275   tet_infoline("UtcDaliImageVisualLoadPolicy01 Load a visual image before attaching to stage");
2276
2277   // Set up trace debug
2278   TestGlAbstraction& gl           = application.GetGlAbstraction();
2279   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2280   textureTrace.Enable(true);
2281
2282   tet_infoline("Create visual with IMMEDIATE load policy");
2283   VisualFactory factory = VisualFactory::Get();
2284
2285   Property::Map propertyMap;
2286   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
2287   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME);
2288   propertyMap.Insert(ImageVisual::Property::DESIRED_WIDTH, 20);
2289   propertyMap.Insert(ImageVisual::Property::DESIRED_HEIGHT, 30);
2290   propertyMap.Insert("loadPolicy", ImageVisual::LoadPolicy::IMMEDIATE);
2291
2292   Visual::Base imageVisual = factory.CreateVisual(propertyMap);
2293
2294   Property::Map resultMap;
2295   imageVisual.CreatePropertyMap(resultMap);
2296   DALI_TEST_CHECK(!resultMap.Empty());
2297   DALI_TEST_EQUALS((resultMap.Find(ImageVisual::Property::LOAD_POLICY))->Get<int>(), (int)ImageVisual::LoadPolicy::IMMEDIATE, TEST_LOCATION);
2298
2299   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2300
2301   // Ensure texture has been uploaded
2302   application.SendNotification();
2303   application.Render();
2304
2305   tet_infoline("Ensure texture loading starts after visual created");
2306   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2307   textureTrace.Reset();
2308
2309   tet_infoline("Register visuals with control and ensure it has the only handles");
2310   DummyControl        actor     = DummyControl::New(true);
2311   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2312   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2313   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2314
2315   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2316   application.GetScene().Add(actor);
2317   tet_infoline("Ensure nothing triggers another load as texure already loaded");
2318   const unsigned int TIME_OUT_3_SECONDS = 3;
2319   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1, TIME_OUT_3_SECONDS), false, TEST_LOCATION);
2320
2321   application.SendNotification();
2322   application.Render();
2323
2324   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
2325   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2326
2327   // Ensure texture is deleted when no longer needed (ref count was correct )
2328   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
2329
2330   application.SendNotification();
2331   application.Render();
2332
2333   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
2334   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
2335
2336   END_TEST;
2337 }
2338
2339 int UtcDaliImageVisualLoadPolicy02(void)
2340 {
2341   ToolkitTestApplication application;
2342   tet_infoline("UtcDaliImageVisualLoadPolicy01 Load a visual image only after attached to stage");
2343
2344   // Set up trace debug
2345   TestGlAbstraction& gl           = application.GetGlAbstraction();
2346   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2347   textureTrace.Enable(true);
2348
2349   tet_infoline("Create visual with IMMEDIATE load policy");
2350   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED);
2351
2352   const unsigned int TIME_OUT_3_SECONDS = 3;
2353   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1, TIME_OUT_3_SECONDS), false, TEST_LOCATION);
2354
2355   // Act on meeage queue even although nothing expected to load
2356   application.SendNotification();
2357   application.Render();
2358
2359   tet_infoline("Ensure texture is not generated yet");
2360   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION);
2361   textureTrace.Reset();
2362
2363   tet_infoline("Register visuals with control and ensure it has the only handles");
2364   DummyControl        actor     = DummyControl::New(true);
2365   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2366   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2367   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2368
2369   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2370   application.GetScene().Add(actor);
2371   tet_infoline("Allow image time to load");
2372   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2373
2374   application.SendNotification();
2375   application.Render();
2376
2377   tet_infoline("Ensure texture generated and renderer created");
2378   DALI_TEST_EQUALS(actor.GetRendererCount(), 1u, TEST_LOCATION);
2379   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2380
2381   // Ensure texture is delete when no longer needed
2382   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
2383
2384   application.SendNotification();
2385   application.Render();
2386
2387   DALI_TEST_EQUALS(actor.GetRendererCount(), 0u, TEST_LOCATION);
2388   DALI_TEST_EQUALS(textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION);
2389
2390   END_TEST;
2391 }
2392
2393 int UtcDaliImageVisualLoadPolicy03(void)
2394 {
2395   ToolkitTestApplication application;
2396   tet_infoline("UtcDaliImageVisualLoadPolicy03 Load a visual image and receive ResourceReady Signal when loaded");
2397
2398   const bool VISUAL_NOT_ENABLED(false); // Instead of just passing 'false' into an API.
2399
2400   // Set up trace debug
2401   TestGlAbstraction& gl           = application.GetGlAbstraction();
2402   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2403   textureTrace.Enable(true);
2404
2405   tet_infoline("Create a control and connect to resource ready signal without adding to stage");
2406   DummyControl actor = DummyControl::New(true);
2407   actor.ResourceReadySignal().Connect(&ResourceReadySignal);
2408   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2409   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2410
2411   tet_infoline("Create visual with IMMEDIATE load policy");
2412   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE);
2413
2414   tet_infoline("Registering visual allows control to get a signal once loaded even if visual not enabled( not staged )");
2415   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual, VISUAL_NOT_ENABLED);
2416   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2417
2418   tet_infoline("Allow image time to load resource");
2419   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2420   application.SendNotification();
2421   application.Render();
2422
2423   // Ensure texture has been uploaded
2424   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2425   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2426
2427   END_TEST;
2428 }
2429
2430 int UtcDaliImageVisualLoadPolicy04(void)
2431 {
2432   ToolkitTestApplication application;
2433   tet_infoline("UtcDaliImageVisualLoadPolicy04 First part  Load a visual image before attaching to stage");
2434   tet_infoline("Second part, Reuse the same image in aonther control and check resource ready signal fired");
2435
2436   const bool VISUAL_NOT_ENABLED(false); // Instead of just passing false into an API.
2437
2438   // Set up trace debug
2439   TestGlAbstraction& gl           = application.GetGlAbstraction();
2440   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2441   textureTrace.Enable(true);
2442
2443   tet_infoline("Create a control and connect to resource ready signal");
2444   DummyControl actor = DummyControl::New(true);
2445   actor.ResourceReadySignal().Connect(&ResourceReadySignal);
2446   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2447   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2448
2449   tet_infoline("Create visual with IMMEDIATE load policy");
2450   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE);
2451
2452   tet_infoline("Registering visual allows control to get a signal once loaded even if visual not enabled( staged )");
2453   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual, VISUAL_NOT_ENABLED);
2454   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2455
2456   tet_infoline("Allow image time to load");
2457   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2458   application.SendNotification();
2459   application.Render();
2460
2461   tet_infoline("Testing texture is loaded and resource ready signal fired");
2462   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2463   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2464
2465   tet_infoline("Original control correctly signalled, now testing for signal with new Control reusing the image");
2466
2467   gResourceReadySignalFired        = false; // Reset signal check ready for testing next Control
2468   Visual::Base        imageVisual2 = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE);
2469   DummyControl        actor2       = DummyControl::New(true);
2470   Impl::DummyControl& dummyImpl2   = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2471   actor2.ResourceReadySignal().Connect(&ResourceReadySignal);
2472
2473   tet_infoline("Registering visual this should trigger the loading signal as is already image loaded for previous control");
2474   dummyImpl2.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual2);
2475   imageVisual2.Reset(); // reduce ref count so only the control keeps the visual alive.
2476   actor2.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2477   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(0), true, TEST_LOCATION); // Not expecting any further loading as texture is being reused.
2478   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2479
2480   END_TEST;
2481 }
2482
2483 int UtcDaliImageVisualLoadPolicy05(void)
2484 {
2485   ToolkitTestApplication application;
2486   tet_infoline("UtcDaliImageVisualLoadPolicy05 LoadPolicy::ATTACHED (default) First part  Load a visual image before attaching to stage");
2487   tet_infoline("Second part, Reuse the same image in aonther control and check resource ready signal fired");
2488   // Set up trace debug
2489   TestGlAbstraction& gl           = application.GetGlAbstraction();
2490   TraceCallStack&    textureTrace = gl.GetTextureTrace();
2491   textureTrace.Enable(true);
2492
2493   tet_infoline("Create a control and connect to resource ready signal");
2494   DummyControl actor = DummyControl::New(true);
2495   actor.ResourceReadySignal().Connect(&ResourceReadySignal);
2496   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2497   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2498   application.GetScene().Add(actor);
2499
2500   tet_infoline("Create visual with ATTACHED load policy");
2501   Visual::Base imageVisual = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED);
2502
2503   tet_infoline("Registering visual allows control to get a signal once loaded");
2504   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2505   imageVisual.Reset(); // reduce ref count so only the control keeps the visual alive.
2506
2507   tet_infoline("Allow image time to load");
2508   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2509   application.SendNotification();
2510   application.Render();
2511
2512   tet_infoline("Testing texture is loaded and resource ready signal fired");
2513   DALI_TEST_EQUALS(textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION);
2514   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2515
2516   tet_infoline("Original control correctly signalled, now testing for signal with new Control reusing the image");
2517
2518   gResourceReadySignalFired        = false; // Reset signal check ready for testing next Control
2519   Visual::Base        imageVisual2 = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::ATTACHED);
2520   DummyControl        actor2       = DummyControl::New(true);
2521   Impl::DummyControl& dummyImpl2   = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2522   actor2.ResourceReadySignal().Connect(&ResourceReadySignal);
2523
2524   tet_infoline("Registering visual this should trigger the loading signal as is already image loaded for previous control");
2525   dummyImpl2.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual2);
2526   imageVisual2.Reset(); // reduce ref count so only the control keeps the visual alive.
2527   actor2.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2528   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(0), true, TEST_LOCATION); // Not expecting any further loading as texture is being reused.
2529   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2530
2531   END_TEST;
2532 }
2533
2534 int UtcDaliImageVisualOrientationCorrection(void)
2535 {
2536   ToolkitTestApplication application;
2537   tet_infoline("UtcDaliImageVisualOrientationCorrection Enabling OrientationCorrection should rotate an image with exif (90deg) orientation data with requested");
2538
2539   VisualFactory factory = VisualFactory::Get();
2540   tet_infoline("Create visual with Orientation correction set OFF");
2541   Property::Map propertyMap;
2542   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
2543   propertyMap.Insert(ImageVisual::Property::URL, TEST_ROTATED_IMAGE);
2544   propertyMap.Insert("orientationCorrection", false);
2545   Visual::Base imageVisual = factory.CreateVisual(propertyMap);
2546
2547   tet_infoline("Create control for visual, need to loaded it");
2548   DummyControl        actor     = DummyControl::New(true);
2549   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2550   application.GetScene().Add(actor);
2551
2552   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2553   // Wait for image to load
2554   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2555
2556   Vector2 originalImageSize;
2557   tet_infoline("Get size of original visual to compare later with rotated image");
2558   imageVisual.GetNaturalSize(originalImageSize);
2559   DALI_TEST_GREATER(originalImageSize.width, originalImageSize.height, TEST_LOCATION); // Width and Height must be different for this test.
2560   imageVisual.Reset();                                                                 // remove handle so can unregister it and remove from cache
2561   dummyImpl.UnregisterVisual(DummyControl::Property::TEST_VISUAL);
2562   application.SendNotification();
2563   application.Render();
2564
2565   tet_infoline("Create visual with Orientation correction set ON ");
2566   propertyMap.Clear();
2567   propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
2568   propertyMap.Insert(ImageVisual::Property::URL, TEST_ROTATED_IMAGE);
2569   propertyMap.Insert(ImageVisual::Property::ORIENTATION_CORRECTION, true);
2570   imageVisual = factory.CreateVisual(propertyMap);
2571
2572   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual);
2573   // Wait for image to load
2574   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2575
2576   Vector2 rotatedImageSize;
2577   imageVisual.GetNaturalSize(rotatedImageSize);
2578   tet_infoline("Confirm that visual has rotated");
2579   DALI_TEST_EQUALS(originalImageSize.width, rotatedImageSize.height, TEST_LOCATION);
2580   DALI_TEST_EQUALS(originalImageSize.height, rotatedImageSize.width, TEST_LOCATION);
2581
2582   Property::Map resultMap;
2583   imageVisual.CreatePropertyMap(resultMap);
2584
2585   // check the Property::ORIENTATION_CORRECTION value from the returned map
2586   Property::Value* typeValue = resultMap.Find(ImageVisual::Property::ORIENTATION_CORRECTION, Property::BOOLEAN);
2587   DALI_TEST_EQUALS(typeValue->Get<bool>(), true, TEST_LOCATION);
2588
2589   END_TEST;
2590 }
2591
2592 int UtcDaliImageVisualCustomShader(void)
2593 {
2594   ToolkitTestApplication application;
2595   tet_infoline("UtcDaliImageVisualCustomShader Test custom shader");
2596
2597   VisualFactory     factory = VisualFactory::Get();
2598   Property::Map     properties;
2599   Property::Map     shader;
2600   const std::string vertexShader                    = "Foobar";
2601   const std::string fragmentShader                  = "Foobar";
2602   shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
2603   shader[Visual::Shader::Property::VERTEX_SHADER]   = vertexShader;
2604
2605   properties[Visual::Property::TYPE]     = Visual::IMAGE;
2606   properties[Visual::Property::SHADER]   = shader;
2607   properties[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
2608
2609   Visual::Base visual = factory.CreateVisual(properties);
2610
2611   // trigger creation through setting on stage
2612   DummyControl        dummy     = DummyControl::New(true);
2613   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummy.GetImplementation());
2614   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual);
2615
2616   dummy.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2617   dummy.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
2618   application.GetScene().Add(dummy);
2619
2620   application.SendNotification();
2621   application.Render();
2622
2623   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2624
2625   Renderer        renderer = dummy.GetRendererAt(0);
2626   Shader          shader2  = renderer.GetShader();
2627   Property::Value value    = shader2.GetProperty(Shader::Property::PROGRAM);
2628   Property::Map*  map      = value.GetMap();
2629   DALI_TEST_CHECK(map);
2630
2631   Property::Value* fragment = map->Find("fragment"); // fragment key name from shader-impl.cpp
2632   DALI_TEST_EQUALS(fragmentShader, fragment->Get<std::string>(), TEST_LOCATION);
2633
2634   Property::Value* vertex = map->Find("vertex"); // vertex key name from shader-impl.cpp
2635   DALI_TEST_EQUALS(vertexShader, vertex->Get<std::string>(), TEST_LOCATION);
2636
2637   shader.Clear();
2638
2639   shader[Visual::Shader::Property::HINTS] = Shader::Hint::OUTPUT_IS_TRANSPARENT;
2640   properties[Visual::Property::SHADER]    = shader;
2641
2642   Visual::Base visual1 = factory.CreateVisual(properties);
2643
2644   // trigger creation through setting on stage
2645   DummyControl        dummy1     = DummyControl::New(true);
2646   Impl::DummyControl& dummyImpl1 = static_cast<Impl::DummyControl&>(dummy1.GetImplementation());
2647   dummyImpl1.RegisterVisual(DummyControl::Property::TEST_VISUAL, visual1);
2648   dummy1.SetProperty(Actor::Property::SIZE, Vector2(200, 200));
2649   dummy1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
2650   application.GetScene().Add(dummy1);
2651
2652   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
2653   glAbstraction.EnableEnableDisableCallTrace(true);
2654
2655   application.SendNotification();
2656   application.Render();
2657
2658   TraceCallStack&    glEnableStack = glAbstraction.GetEnableDisableTrace();
2659   std::ostringstream blendStr;
2660   blendStr << std::hex << GL_BLEND;
2661   DALI_TEST_CHECK(glEnableStack.FindMethodAndParams("Enable", blendStr.str()));
2662
2663   END_TEST;
2664 }
2665
2666 void ResourceReadyLoadNext(Control control)
2667 {
2668   static int callNumber = 0;
2669
2670   gResourceReadySignalFired = true;
2671   gReadyIds.push_back(control.GetProperty<int>(Actor::Property::ID));
2672
2673   if(callNumber == 0)
2674   {
2675     DALI_TEST_EQUALS(control.GetVisualResourceStatus(DummyControl::Property::TEST_VISUAL), Toolkit::Visual::ResourceStatus::FAILED, TEST_LOCATION);
2676
2677     tet_infoline("Create visual with loaded image from within the signal handler");
2678     VisualFactory factory     = VisualFactory::Get();
2679     Visual::Base  imageVisual = factory.CreateVisual(TEST_IMAGE_FILE_NAME, ImageDimensions{20, 30});
2680
2681     Impl::DummyControl& controlImpl = static_cast<Impl::DummyControl&>(control.GetImplementation());
2682     controlImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual); // This should trigger another signal.
2683     callNumber = 1;
2684   }
2685   else
2686   {
2687     tet_infoline("3rd signal called");
2688     DALI_TEST_CHECK(true);
2689   }
2690 }
2691
2692 int UtcDaliImageVisualLoadReady01(void)
2693 {
2694   ToolkitTestApplication application;
2695   tet_infoline("UtcDaliImageVisualLoadReady01");
2696   tet_infoline("First part:  Load an image visual for one resource, then another image visual for a second resource.");
2697   tet_infoline("Second part, In the ready signal for the second image visual, add a 3rd visual with the first URL");
2698   tet_infoline("Should get a ready signal for all three visuals");
2699
2700   ClearReadyIds();
2701
2702   tet_infoline("Create a control and connect to resource ready signal");
2703   DummyControl actor    = DummyControl::New(true);
2704   int          actor1Id = actor.GetProperty<int>(Actor::Property::ID);
2705   actor.ResourceReadySignal().Connect(&ResourceReadySignal);
2706   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
2707   actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2708   application.GetScene().Add(actor);
2709
2710   tet_infoline("Create visual with IMMEDIATE load policy");
2711   Visual::Base imageVisual1 = CreateVisualWithPolicy(TEST_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE);
2712
2713   tet_infoline("Registering visual allows control to get a signal once loaded even if visual not enabled( staged )");
2714   dummyImpl.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual1);
2715
2716   tet_infoline("Allow image time to load");
2717   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2718   application.SendNotification();
2719   application.Render();
2720
2721   tet_infoline("Testing texture is loaded and resource ready signal fired");
2722   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2723   DALI_TEST_EQUALS(gReadyIds[0], actor1Id, TEST_LOCATION);
2724
2725   tet_infoline("Original control correctly signalled, now testing failing image");
2726
2727   gResourceReadySignalFired = false; // Reset signal check ready for testing next Control
2728   ClearReadyIds();
2729
2730   Visual::Base imageVisual2 = CreateVisualWithPolicy(TEST_BROKEN_IMAGE_FILE_NAME, ImageVisual::Property::LOAD_POLICY, ImageVisual::LoadPolicy::IMMEDIATE);
2731
2732   DummyControl        actor2     = DummyControl::New(true);
2733   int                 actor2Id   = actor2.GetProperty<int>(Actor::Property::ID);
2734   Impl::DummyControl& dummyImpl2 = static_cast<Impl::DummyControl&>(actor2.GetImplementation());
2735   actor2.ResourceReadySignal().Connect(&ResourceReadyLoadNext);
2736
2737   tet_infoline("Registering visual this should trigger the ready signal when the image fails to load");
2738   dummyImpl2.RegisterVisual(DummyControl::Property::TEST_VISUAL, imageVisual2);
2739
2740   actor2.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f));
2741   application.GetScene().Add(actor2);
2742
2743   tet_infoline("Wait for loading thread to finish");
2744   DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
2745   DALI_TEST_EQUALS(gResourceReadySignalFired, true, TEST_LOCATION);
2746
2747   DALI_TEST_EQUALS(gReadyIds[0], actor2Id, TEST_LOCATION);
2748
2749   tet_infoline("Check for 3rd signal");
2750   application.SendNotification();
2751   DALI_TEST_EQUALS(gReadyIds.size(), 2, TEST_LOCATION);
2752   DALI_TEST_EQUALS(gReadyIds[1], actor2Id, TEST_LOCATION);
2753
2754   END_TEST;
2755 }