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