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