Merge "Remove profile build dependencies" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageVisual.cpp
1 /*
2  * Copyright (c) 2017 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 <iostream>
18 #include <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <toolkit-timer.h>
21 #include <toolkit-bitmap-loader.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <dali/public-api/rendering/renderer.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/public-api/rendering/shader.h>
26 #include <dali/devel-api/images/nine-patch-image.h>
27 #include <dali/devel-api/object/handle-devel.h>
28 #include <dali-toolkit/devel-api/align-enums.h>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali-toolkit/dali-toolkit.h>
34 #include "dummy-control.h"
35
36 using namespace Dali;
37 using namespace Dali::Toolkit;
38
39 void dali_image_visual_startup(void)
40 {
41   test_return_value = TET_UNDEF;
42 }
43
44 void dali_image_visual_cleanup(void)
45 {
46   test_return_value = TET_PASS;
47 }
48
49 namespace
50 {
51 const char* TEST_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR  "/gallery_small-1.jpg";
52 const char* TEST_LARGE_IMAGE_FILE_NAME =  TEST_RESOURCE_DIR "/tbcol.png";
53 const char* TEST_SMALL_IMAGE_FILE_NAME = TEST_RESOURCE_DIR "/icon-edit.png";
54 const char* TEST_REMOTE_IMAGE_FILE_NAME = "https://www.tizen.org/sites/all/themes/tizen_theme/logo.png";
55 const char* TEST_INVALID_FILE_NAME =  TEST_RESOURCE_DIR  "/invalid.jpg";
56 const char* TEST_REMOTE_INVALID_FILE_NAME = "https://www.tizen.org/invalid.png";
57 }
58
59
60 Actor CreateActorWithImageVisual(const Property::Map& map)
61 {
62   VisualFactory factory = VisualFactory::Get();
63   DummyControl actor = DummyControl::New();
64   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
65   Visual::Base visual = factory.CreateVisual( map );
66   DALI_TEST_CHECK( visual );
67   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
68   actor.SetSize( 200.f, 200.f );
69   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
70   return actor;
71 }
72
73 void TestVisualRender( ToolkitTestApplication& application,
74                        DummyControl& actor,
75                        Visual::Base& visual,
76                        std::size_t expectedSamplers = 0,
77                        ImageDimensions imageDimensions = ImageDimensions(),
78                        Integration::ResourcePointer resourcePtr = Integration::ResourcePointer())
79 {
80   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
81   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
82
83   if( resourcePtr )
84   {
85     // set the image size, for test case, this needs to be set before loading started
86     application.GetPlatform().SetClosestImageSize(  Vector2(imageDimensions.GetWidth(), imageDimensions.GetHeight()) );
87   }
88
89   actor.SetSize( 200.f, 200.f );
90   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
91
92   Stage::GetCurrent().Add( actor );
93
94   application.SendNotification(); // Send messages to update
95   application.Render();           // process update and render
96   application.SendNotification(); // process any signals to event
97
98   if( resourcePtr )
99   {
100     DALI_TEST_EQUALS( application.GetPlatform().WasCalled(TestPlatformAbstraction::LoadResourceSynchronouslyFunc ), true, TEST_LOCATION);
101   }
102
103   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
104 }
105
106 static void TestMixColor( Visual::Base visual, Property::Index mixColorIndex, const Vector4& testColor )
107 {
108   Property::Map map;
109   visual.CreatePropertyMap(map);
110   Property::Value* value = map.Find( mixColorIndex );
111   DALI_TEST_CHECK( value );
112   Vector3 mixColor1;
113   DALI_TEST_CHECK( value->Get( mixColor1 ) );
114   DALI_TEST_EQUALS( mixColor1, Vector3(testColor), 0.001, TEST_LOCATION );
115
116   value = map.Find( DevelVisual::Property::MIX_COLOR );
117   DALI_TEST_CHECK( value );
118   Vector4 mixColor2;
119   DALI_TEST_CHECK( value->Get( mixColor2 ) );
120   DALI_TEST_EQUALS( mixColor2, testColor, 0.001, TEST_LOCATION );
121
122   value = map.Find( DevelVisual::Property::OPACITY );
123   DALI_TEST_CHECK( value );
124   float opacity;
125   DALI_TEST_CHECK( value->Get( opacity ) );
126   DALI_TEST_EQUALS( opacity, testColor.a, 0.001, TEST_LOCATION );
127 }
128
129
130
131 int UtcDaliImageVisualPropertyMap(void)
132 {
133   ToolkitTestApplication application;
134   tet_infoline( "Request image visual with a Property::Map" );
135
136   VisualFactory factory = VisualFactory::Get();
137   DALI_TEST_CHECK( factory );
138
139   Property::Map propertyMap;
140   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
141   propertyMap.Insert( ImageVisual::Property::URL,  TEST_LARGE_IMAGE_FILE_NAME );
142
143   Visual::Base visual = factory.CreateVisual( propertyMap );
144   DALI_TEST_CHECK( visual );
145
146   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
147   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
148
149   TestGlAbstraction& gl = application.GetGlAbstraction();
150   TraceCallStack& textureTrace = gl.GetTextureTrace();
151   textureTrace.Enable(true);
152
153   DummyControl actor = DummyControl::New();
154   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
155   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
156
157   actor.SetSize( 200.f, 200.f );
158   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
159
160   Stage::GetCurrent().Add( actor );
161   application.SendNotification();
162   application.Render();
163
164   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
165
166   application.SendNotification();
167   application.Render();
168
169   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
170
171   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
172
173   Stage::GetCurrent().Remove( actor );
174   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
175
176   END_TEST;
177 }
178
179
180 int UtcDaliImageVisualRemoteImageLoad(void)
181 {
182   ToolkitTestApplication application;
183   tet_infoline( "Request remote image visual with a Property::Map" );
184
185   VisualFactory factory = VisualFactory::Get();
186   DALI_TEST_CHECK( factory );
187
188   Property::Map propertyMap;
189   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
190   propertyMap.Insert( ImageVisual::Property::URL,  TEST_REMOTE_IMAGE_FILE_NAME );
191
192   Visual::Base visual = factory.CreateVisual( propertyMap );
193   DALI_TEST_CHECK( visual );
194
195   TestGlAbstraction& gl = application.GetGlAbstraction();
196   TraceCallStack& textureTrace = gl.GetTextureTrace();
197   textureTrace.Enable(true);
198
199   DummyControl actor = DummyControl::New();
200   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
201   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
202
203   actor.SetSize( 200.f, 200.f );
204   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
205
206   Stage::GetCurrent().Add( actor );
207   application.SendNotification();
208
209   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
210
211   application.SendNotification();
212   application.Render();
213
214   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
215   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
216
217   Stage::GetCurrent().Remove( actor );
218   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
219
220   END_TEST;
221 }
222
223 int UtcDaliImageVisualTextureReuse1(void)
224 {
225   ToolkitTestApplication application;
226   tet_infoline( "Request remote image visual with a Property::Map; request a second visual with the same property map - should reuse texture" );
227
228   Property::Map propertyMap;
229   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
230   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
231
232   TestGlAbstraction& gl = application.GetGlAbstraction();
233   TraceCallStack& textureTrace = gl.GetTextureTrace();
234   textureTrace.Enable(true);
235   TraceCallStack& drawTrace = gl.GetDrawTrace();
236   drawTrace.Enable(true);
237
238   Actor actor = CreateActorWithImageVisual( propertyMap );
239   Stage::GetCurrent().Add( actor );
240   application.SendNotification();
241
242   // Wait for image to load
243   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
244
245   application.SendNotification();
246   application.Render();
247
248   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
249   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
250   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
251   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
252   textureTrace.Reset();
253   drawTrace.Reset();
254
255   Actor actor2 = CreateActorWithImageVisual( propertyMap );
256   Stage::GetCurrent().Add(actor2);
257
258   application.SendNotification(); // Send messages to update
259   application.Render();           // process update and render
260   application.SendNotification(); // process any signals to event
261
262   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
263
264   tet_infoline("Test that 2 draw calls occur with no new texture gens/binds, i.e. both\n"
265                "draw calls use the same texture as the previous draw call\n" );
266
267   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
268   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
269   DALI_TEST_EQUALS( textureTrace.CountMethod("BindTexture"), 0, TEST_LOCATION );
270
271   tet_infoline("Test that removing 1 actor doesn't delete the texture\n");
272
273   Stage::GetCurrent().Remove( actor );
274   application.SendNotification();
275   application.Render();
276
277   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
278   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 0, TEST_LOCATION );
279
280   tet_infoline("Test that removing last actor does delete the texture\n");
281
282   Stage::GetCurrent().Remove( actor2 );
283   application.SendNotification();
284   application.Render();
285
286   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
287   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
288
289   END_TEST;
290 }
291
292
293 int UtcDaliImageVisualTextureReuse2(void)
294 {
295   ToolkitTestApplication application;
296   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" );
297
298   Property::Map propertyMap;
299   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
300   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
301
302   TestGlAbstraction& gl = application.GetGlAbstraction();
303   TraceCallStack& textureTrace = gl.GetTextureTrace();
304   textureTrace.Enable(true);
305   TraceCallStack& drawTrace = gl.GetDrawTrace();
306   drawTrace.Enable(true);
307
308   Actor actor = CreateActorWithImageVisual( propertyMap );
309   Stage::GetCurrent().Add( actor );
310   application.SendNotification();
311
312   // Wait for image to load
313   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
314
315   application.SendNotification();
316   application.Render();
317
318   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
319   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
320   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
321   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), true, TEST_LOCATION );
322   textureTrace.Reset();
323   drawTrace.Reset();
324
325   propertyMap.Insert( ImageVisual::Property::SAMPLING_MODE, Dali::SamplingMode::NEAREST );
326   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, 100 );
327   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, 100 );
328   Actor actor2 = CreateActorWithImageVisual( propertyMap );
329   Stage::GetCurrent().Add(actor2);
330
331   application.SendNotification();
332
333   // Wait for image to load
334   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
335
336   application.SendNotification();
337   application.Render();
338
339   DALI_TEST_EQUALS( actor2.GetRendererCount(), 1u, TEST_LOCATION );
340
341   tet_infoline("Test that 2 draw calls occur with 1 new texture gen/bind, i.e. both "
342                "renderers are using different textures\n" );
343
344   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), true, TEST_LOCATION );
345   DALI_TEST_EQUALS( drawTrace.CountMethod("DrawArrays"), 2, TEST_LOCATION );
346   DALI_TEST_EQUALS( textureTrace.CountMethod("BindTexture"), 2, TEST_LOCATION );
347
348   tet_infoline("Test that removing 1 actor deletes it's texture\n");
349
350   Stage::GetCurrent().Remove( actor );
351   application.SendNotification();
352   application.Render();
353
354   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
355   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 1, TEST_LOCATION );
356
357   tet_infoline("Test that removing last actor deletes it's texture\n");
358
359   Stage::GetCurrent().Remove( actor2 );
360   application.SendNotification();
361   application.Render();
362
363   DALI_TEST_CHECK( actor2.GetRendererCount() == 0u );
364   DALI_TEST_EQUALS( textureTrace.CountMethod("DeleteTextures"), 2, TEST_LOCATION );
365
366   END_TEST;
367 }
368
369
370 int UtcDaliImageVisualImageHandle(void)
371 {
372   ToolkitTestApplication application;
373   tet_infoline( "Request image visual with an image handle" );
374
375   VisualFactory factory = VisualFactory::Get();
376   DALI_TEST_CHECK( factory );
377
378   Image image = ResourceImage::New(TEST_IMAGE_FILE_NAME);
379   Visual::Base visual = factory.CreateVisual( image );
380
381   // For tesing the LoadResourceFunc is called, a big image size should be set, so the atlasing is not applied.
382   // Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
383
384   const int width=512;
385   const int height=513;
386
387   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
388   bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, width, height,width, height );
389
390   TestGlAbstraction& gl = application.GetGlAbstraction();
391   TraceCallStack& textureTrace = gl.GetTextureTrace();
392   textureTrace.Enable(true);
393
394   DummyControl actor = DummyControl::New();
395   TestVisualRender( application, actor, visual, 1u,
396                     ImageDimensions(width, height),
397                     Integration::ResourcePointer(bitmap) );
398
399   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
400   END_TEST;
401 }
402
403 int UtcDaliImageVisualCustomWrapModePixelArea(void)
404 {
405   ToolkitTestApplication application;
406   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area with atlasing" );
407
408   VisualFactory factory = VisualFactory::Get();
409   DALI_TEST_CHECK( factory );
410
411   // Test wrap mode with atlasing. Image with a size smaller than 512*512 will be uploaded as a part of the atlas.
412   const int width=34;
413   const int height=34;
414   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
415
416   Property::Map propertyMap;
417   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
418   propertyMap.Insert( ImageVisual::Property::URL, TEST_SMALL_IMAGE_FILE_NAME );
419   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
420   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
421   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
422   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
423   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
424   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
425   propertyMap.Insert( DevelImageVisual::Property::ATLASING, true );
426
427   Visual::Base visual = factory.CreateVisual( propertyMap );
428   DALI_TEST_CHECK( visual );
429
430   TestGlAbstraction& gl = application.GetGlAbstraction();
431   TraceCallStack& textureTrace = gl.GetTextureTrace();
432   textureTrace.Enable(true);
433   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
434   texParameterTrace.Enable( true );
435
436   DummyControl actor = DummyControl::New();
437   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
438   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
439   actor.SetSize(2000, 2000);
440   actor.SetParentOrigin(ParentOrigin::CENTER);
441   Stage::GetCurrent().Add( actor );
442
443   // loading started
444   application.SendNotification();
445   application.Render();
446
447   BitmapLoader loader = BitmapLoader::GetLatestCreated();
448   DALI_TEST_CHECK( loader );
449   loader.WaitForLoading();// waiting until the image to be loaded
450   DALI_TEST_CHECK( loader.IsLoaded() );
451
452   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
453
454   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
455
456   // WITH atlasing, the wrapping is handled manually in shader, so the following gl function should not be called
457   std::stringstream out;
458   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
459   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
460   out.str("");
461   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
462   DALI_TEST_CHECK( !texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
463
464   // test the uniforms which used to handle the wrap mode
465   Renderer renderer = actor.GetRendererAt( 0u );
466   DALI_TEST_CHECK( renderer );
467
468   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
469   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
470   Vector4 pixelAreaUniform;
471   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
472   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
473
474   Property::Value wrapModeValue = renderer.GetProperty( renderer.GetPropertyIndex( "wrapMode" ) );
475   Vector2 wrapMode( WrapMode::MIRRORED_REPEAT-1, WrapMode::REPEAT-1 );
476   DALI_TEST_EQUALS( wrapModeValue.Get<Vector2>(), wrapMode, TEST_LOCATION );
477   Vector2 wrapModeUniform;
478   DALI_TEST_CHECK( gl.GetUniformValue<Vector2>( "wrapMode", wrapModeUniform ) );
479   DALI_TEST_EQUALS( wrapMode, wrapModeUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
480
481   actor.Unparent( );
482   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
483
484   END_TEST;
485 }
486
487 int UtcDaliImageVisualCustomWrapModeNoAtlas(void)
488 {
489   ToolkitTestApplication application;
490   tet_infoline( "Request image visual with a Property::Map, test custom wrap mode and pixel area without atlasing" );
491
492   VisualFactory factory = VisualFactory::Get();
493   DALI_TEST_CHECK( factory );
494
495   // Test wrap mode without atlasing. Image with a size bigger than 512*512 will NOT be uploaded as a part of the atlas.
496   const int width=600;
497   const int height=600;
498   const Vector4 pixelArea(-0.5f, -0.5f, 2.f, 2.f);
499
500   Property::Map propertyMap;
501   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
502   propertyMap.Insert( ImageVisual::Property::URL, TEST_LARGE_IMAGE_FILE_NAME );
503   propertyMap.Insert( ImageVisual::Property::DESIRED_WIDTH, width );
504   propertyMap.Insert( ImageVisual::Property::DESIRED_HEIGHT, height );
505   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
506   propertyMap.Insert( ImageVisual::Property::PIXEL_AREA, pixelArea );
507   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_U, WrapMode::MIRRORED_REPEAT );
508   propertyMap.Insert( ImageVisual::Property::WRAP_MODE_V, WrapMode::REPEAT );
509
510   Visual::Base visual = factory.CreateVisual( propertyMap );
511   DALI_TEST_CHECK( visual );
512
513   TestGlAbstraction& gl = application.GetGlAbstraction();
514   TraceCallStack& textureTrace = gl.GetTextureTrace();
515   textureTrace.Enable(true);
516   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
517   texParameterTrace.Enable( true );
518
519   DummyControl actor = DummyControl::New();
520   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
521   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
522   actor.SetSize(2000, 2000);
523   actor.SetParentOrigin(ParentOrigin::CENTER);
524   Stage::GetCurrent().Add( actor );
525
526   // loading started
527   application.SendNotification();
528   application.Render();
529
530   BitmapLoader loader = BitmapLoader::GetLatestCreated();
531   DALI_TEST_CHECK( loader );
532   loader.WaitForLoading();// waiting until the image to be loaded
533   DALI_TEST_CHECK( loader.IsLoaded() );
534
535   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
536
537   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
538
539   // WITHOUT atlasing, the wrapping is handled by setting gl texture parameters
540   std::stringstream out;
541   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_MIRRORED_REPEAT;
542   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
543   out.str("");
544   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_REPEAT;
545   DALI_TEST_CHECK( texParameterTrace.FindMethodAndParams("TexParameteri", out.str()) );
546
547   // test the uniforms which used to handle the wrap mode
548   Renderer renderer = actor.GetRendererAt( 0u );
549   DALI_TEST_CHECK( renderer );
550
551   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
552   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelArea, TEST_LOCATION );
553   Vector4 pixelAreaUniform;
554   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
555   DALI_TEST_EQUALS( pixelArea, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
556
557   Property::Index wrapModeIndex = renderer.GetPropertyIndex( "wrapMode" );
558   DALI_TEST_CHECK(wrapModeIndex == Property::INVALID_INDEX);
559
560   actor.Unparent();
561   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
562
563   END_TEST;
564 }
565
566 int UtcDaliImageVisualAnimateMixColor(void)
567 {
568   ToolkitTestApplication application;
569   tet_infoline( "Animate mix color" );
570
571   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
572
573   VisualFactory factory = VisualFactory::Get();
574   Property::Map propertyMap;
575   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
576   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
577   propertyMap.Insert("mixColor", Color::BLUE);
578   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
579   Visual::Base visual = factory.CreateVisual( propertyMap );
580
581   DummyControl actor = DummyControl::New(true);
582   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
583   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
584
585   actor.SetSize(2000, 2000);
586   actor.SetParentOrigin(ParentOrigin::CENTER);
587   actor.SetColor(Color::BLACK);
588   Stage::GetCurrent().Add(actor);
589
590   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
591
592   Renderer renderer = actor.GetRendererAt(0);
593   Property::Index index = DevelHandle::GetPropertyIndex( renderer, DevelVisual::Property::MIX_COLOR );
594   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
595   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
596
597   tet_infoline("Test that the renderer has the mixColor property");
598   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
599
600   const Vector4 TARGET_MIX_COLOR( 1.0f, 0.0f, 0.0f, 0.5f );
601
602   Property::Map map;
603   map["target"] = "testVisual";
604   map["property"] = "mixColor";
605   map["initialValue"] = Color::MAGENTA;
606   map["targetValue"] = TARGET_MIX_COLOR;
607   map["animator"] = Property::Map()
608     .Add("alphaFunction", "LINEAR")
609     .Add("timePeriod", Property::Map()
610          .Add("delay", 0.0f)
611          .Add("duration", 4.0f));
612
613   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
614
615   Animation animation = dummyImpl.CreateTransition( transition );
616
617   blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
618   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
619
620   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
621   animation.Play();
622
623   application.SendNotification();
624   application.Render(0); // Ensure animation starts
625   application.Render(2000u); // Halfway point
626   Vector4 testColor(1.0f, 0.0f, 0.5f, 0.75f );
627
628   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Vector4(0.5f, 0.5f, 0.5f, 1.0f )), true, TEST_LOCATION );
629   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", Vector3(testColor)), true, TEST_LOCATION );
630   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", testColor.a), true, TEST_LOCATION );
631
632   application.Render(2000u); // Halfway point between blue and white
633
634   DALI_TEST_EQUALS( actor.GetCurrentColor(), Color::WHITE, TEST_LOCATION );
635   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("uColor", Color::WHITE ), true, TEST_LOCATION );
636   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector3>("mixColor", Vector3(TARGET_MIX_COLOR)), true, TEST_LOCATION );
637   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", TARGET_MIX_COLOR.a), true, TEST_LOCATION );
638
639   TestMixColor( visual, DevelVisual::Property::MIX_COLOR, TARGET_MIX_COLOR );
640
641   blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
642   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
643
644   END_TEST;
645 }
646
647 int UtcDaliImageVisualAnimateOpacity(void)
648 {
649   ToolkitTestApplication application;
650   tet_infoline( "Animate image visual opacity" );
651
652   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
653
654   VisualFactory factory = VisualFactory::Get();
655   Property::Map propertyMap;
656   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
657   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
658   propertyMap.Insert("opacity", 0.5f);
659   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
660   Visual::Base visual = factory.CreateVisual( propertyMap );
661
662   DummyControl actor = DummyControl::New(true);
663   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
664   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
665
666   actor.SetSize(2000, 2000);
667   actor.SetParentOrigin(ParentOrigin::CENTER);
668   actor.SetColor(Color::BLACK);
669   Stage::GetCurrent().Add(actor);
670
671   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
672
673   Renderer renderer = actor.GetRendererAt(0);
674   tet_infoline("Test that the renderer has the opacity property");
675   Property::Index index = DevelHandle::GetPropertyIndex( renderer, DevelVisual::Property::OPACITY );
676   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
677
678
679   Property::Value blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
680   DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
681
682   {
683     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." );
684
685     Property::Map map;
686     map["target"] = "testVisual";
687     map["property"] = "opacity";
688     map["targetValue"] = 1.0f;
689     map["animator"] = Property::Map()
690       .Add("alphaFunction", "LINEAR")
691       .Add("timePeriod", Property::Map()
692            .Add("delay", 0.0f)
693            .Add("duration", 4.0f));
694
695     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
696     Animation animation = dummyImpl.CreateTransition( transition );
697     animation.Play();
698
699     application.SendNotification();
700     application.Render(0);     // Ensure animation starts
701     application.Render(2000u); // Halfway point through animation
702     application.SendNotification(); // Handle any signals
703
704     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.75f), true, TEST_LOCATION );
705
706     application.Render(2001u); // end
707     application.SendNotification(); // ensure animation finished signal is sent
708
709     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 1.0f), true, TEST_LOCATION );
710
711     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
712     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::AUTO, TEST_LOCATION );
713   }
714
715
716   {
717     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." );
718
719     Property::Map map;
720     map["target"] = "testVisual";
721     map["property"] = DevelVisual::Property::OPACITY;
722     map["targetValue"] = 0.1f;
723     map["animator"] = Property::Map()
724       .Add("alphaFunction", "LINEAR")
725       .Add("timePeriod", Property::Map()
726            .Add("delay", 0.0f)
727            .Add("duration", 4.0f));
728
729     Dali::Toolkit::TransitionData transition = TransitionData::New( map );
730     Animation animation = dummyImpl.CreateTransition( transition );
731     animation.Play();
732
733     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
734     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
735
736     application.SendNotification();
737     application.Render(0);     // Ensure animation starts
738     application.Render(2000u); // Halfway point
739     application.SendNotification();
740
741     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.55f), true, TEST_LOCATION );
742
743     application.Render(2016u); // end
744     application.SendNotification();
745
746     DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<float>("opacity", 0.1f), true, TEST_LOCATION );
747
748     blendModeValue = renderer.GetProperty( Renderer::Property::BLEND_MODE );
749     DALI_TEST_EQUALS( blendModeValue.Get<int>(), (int)BlendMode::ON, TEST_LOCATION );
750   }
751
752
753   END_TEST;
754 }
755
756 int UtcDaliImageVisualAnimatePixelArea(void)
757 {
758   ToolkitTestApplication application;
759   tet_infoline( "ImageVisual animate pixel area" );
760
761   application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
762
763   VisualFactory factory = VisualFactory::Get();
764   Property::Map propertyMap;
765   propertyMap.Insert(Visual::Property::TYPE,  Visual::IMAGE);
766   propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME );
767   propertyMap.Insert("mixColor", Color::BLUE);
768   propertyMap.Insert(ImageVisual::Property::SYNCHRONOUS_LOADING, true);
769   Visual::Base visual = factory.CreateVisual( propertyMap );
770
771   DummyControl actor = DummyControl::New(true);
772   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(actor.GetImplementation());
773   dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual );
774
775   actor.SetSize(2000, 2000);
776   actor.SetParentOrigin(ParentOrigin::CENTER);
777   actor.SetColor(Color::BLACK);
778   Stage::GetCurrent().Add(actor);
779
780   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION);
781
782   Renderer renderer = actor.GetRendererAt(0);
783   Property::Index index = DevelHandle::GetPropertyIndex( renderer, DevelVisual::Property::MIX_COLOR );
784
785   tet_infoline("Test that the renderer has the mixColor property");
786   DALI_TEST_CHECK( index != Property::INVALID_INDEX );
787
788   // TransitionData only takes string keys
789   Property::Map map;
790   map["target"] = "testVisual";
791   map["property"] = "pixelArea";
792   map["initialValue"] = Vector4( 0,0,0,1 );
793   map["targetValue"] = Vector4( 0,0,1,1 ); // Animate width from zero to full
794   map["animator"] = Property::Map()
795     .Add("alphaFunction", "LINEAR")
796     .Add("timePeriod", Property::Map()
797          .Add("delay", 0.0f)
798          .Add("duration", 4.0f));
799
800   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
801
802   Animation animation = dummyImpl.CreateTransition( transition );
803   animation.AnimateTo( Property(actor, Actor::Property::COLOR), Color::WHITE );
804   animation.Play();
805
806   application.SendNotification();
807   application.Render(0);     // Ensure animation starts
808   application.Render(2000u); // Halfway point
809
810   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4(0.0f, 0.0f, 0.5f, 1.0f )), true, TEST_LOCATION );
811
812   application.Render(2000u); // End of animation
813
814   DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue<Vector4>("pixelArea", Vector4( 0.0f, 0.0f, 1.0f, 1.0f )), true, TEST_LOCATION );
815
816   END_TEST;
817 }
818
819 int UtcDaliImageVisualTextureCancelRemoteLoad(void)
820 {
821   ToolkitTestApplication application;
822   tet_infoline( "Request remote image visual, then destroy visual to cancel load" );
823
824   Property::Map propertyMap;
825   propertyMap.Insert( Visual::Property::TYPE,  Visual::IMAGE );
826   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_IMAGE_FILE_NAME );
827
828   TestGlAbstraction& gl = application.GetGlAbstraction();
829   TraceCallStack& textureTrace = gl.GetTextureTrace();
830   textureTrace.Enable(true);
831   TraceCallStack& drawTrace = gl.GetDrawTrace();
832   drawTrace.Enable(true);
833
834   Actor actor = CreateActorWithImageVisual( propertyMap );
835   Stage::GetCurrent().Add( actor );
836   application.SendNotification();
837
838   Stage::GetCurrent().Remove( actor );
839   application.SendNotification();
840
841   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
842   DALI_TEST_EQUALS( textureTrace.FindMethod("GenTextures"), false, TEST_LOCATION );
843   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), false, TEST_LOCATION );
844   DALI_TEST_EQUALS( drawTrace.FindMethod("DrawArrays"), false, TEST_LOCATION );
845
846   END_TEST;
847 }
848
849 int UtcDaliImageVisualSetInvalidAsyncImage(void)
850 {
851   ToolkitTestApplication application;
852   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
853
854   VisualFactory factory = VisualFactory::Get();
855   DALI_TEST_CHECK( factory );
856
857   Property::Map propertyMap;
858   propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
859   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
860
861   Visual::Base visual = factory.CreateVisual( propertyMap );
862   DALI_TEST_CHECK( visual );
863
864   TestGlAbstraction& gl = application.GetGlAbstraction();
865   TraceCallStack& textureTrace = gl.GetTextureTrace();
866   textureTrace.Enable(true);
867
868   DummyControl actor = DummyControl::New();
869   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
870   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
871
872   actor.SetSize( 200.f, 200.f );
873   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
874
875   Stage::GetCurrent().Add( actor );
876
877   application.SendNotification();
878   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
879
880   application.SendNotification();
881   application.Render();
882
883   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
884   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
885
886   Stage::GetCurrent().Remove( actor );
887   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
888
889   END_TEST;
890 }
891
892 int UtcDaliImageVisualSetInvalidSyncImage(void)
893 {
894   ToolkitTestApplication application;
895   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
896
897   VisualFactory factory = VisualFactory::Get();
898   DALI_TEST_CHECK( factory );
899
900   Property::Map propertyMap;
901   propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
902   propertyMap.Insert( ImageVisual::Property::URL, TEST_INVALID_FILE_NAME );
903   propertyMap.Insert( ImageVisual::Property::SYNCHRONOUS_LOADING, true );
904
905   Visual::Base visual = factory.CreateVisual( propertyMap );
906   DALI_TEST_CHECK( visual );
907
908   TestGlAbstraction& gl = application.GetGlAbstraction();
909   TraceCallStack& textureTrace = gl.GetTextureTrace();
910   textureTrace.Enable(true);
911
912   DummyControl actor = DummyControl::New();
913   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
914   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
915
916   actor.SetSize( 200.f, 200.f );
917   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
918
919   Stage::GetCurrent().Add( actor );
920
921   application.SendNotification();
922   application.Render();
923
924   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
925   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
926
927   Stage::GetCurrent().Remove( actor );
928   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
929
930   END_TEST;
931 }
932
933 int UtcDaliImageVisualSetInvalidRemoteImage(void)
934 {
935   ToolkitTestApplication application;
936   tet_infoline( "Request image visual with invalid images - should draw broken.png" );
937
938   VisualFactory factory = VisualFactory::Get();
939   DALI_TEST_CHECK( factory );
940
941   // Local invalid file, asynchronous loading
942   Property::Map propertyMap;
943   propertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
944   propertyMap.Insert( ImageVisual::Property::URL, TEST_REMOTE_INVALID_FILE_NAME );
945
946   Visual::Base visual = factory.CreateVisual( propertyMap );
947   DALI_TEST_CHECK( visual );
948
949   TestGlAbstraction& gl = application.GetGlAbstraction();
950   TraceCallStack& textureTrace = gl.GetTextureTrace();
951   textureTrace.Enable(true);
952
953   DummyControl actor = DummyControl::New();
954   DummyControlImpl& dummyImpl = static_cast<DummyControlImpl&>(actor.GetImplementation());
955   dummyImpl.RegisterVisual( Control::CONTROL_PROPERTY_END_INDEX + 1, visual );
956
957   actor.SetSize( 200.f, 200.f );
958   DALI_TEST_EQUALS( actor.GetRendererCount(), 0u, TEST_LOCATION );
959
960   Stage::GetCurrent().Add( actor );
961
962   application.SendNotification();
963   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
964
965   application.SendNotification();
966   application.Render();
967
968   DALI_TEST_EQUALS( actor.GetRendererCount(), 1u, TEST_LOCATION );
969   DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
970
971   Stage::GetCurrent().Remove( actor );
972   DALI_TEST_CHECK( actor.GetRendererCount() == 0u );
973
974   END_TEST;
975 }