Merge "Update the refined dali c# application to support more argument options" into...
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageAtlas.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <dali/dali.h>
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <toolkit-event-thread-callback.h>
23 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
24 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31 // resolution: 34*34, pixel format: RGBA8888
32 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
33 // resolution: 50*50, pixel format: RGBA8888
34 static const char* gImage_50_RGBA = TEST_RESOURCE_DIR "/icon-delete.png";
35 // resolution: 128*128, pixel format: RGB888
36 static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
37
38 // this is image is not exist, for negative test
39 static const char* gImageNonExist = "non-exist.jpg";
40
41 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
42
43 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
44 {
45   Vector4 temp = textureCoordinate * size;
46   Rect<int> pixelArea;
47   pixelArea.x = static_cast<int>( temp.x );
48   pixelArea.y = static_cast<int>( temp.y );
49   pixelArea.width = static_cast<int>( temp.z-temp.x+1.f );
50   pixelArea.height = static_cast<int>( temp.w-temp.y+1.f );
51
52   return pixelArea;
53 }
54
55 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
56 {
57  return rect1.x < rect2.x+rect2.width
58      && rect2.x < rect1.x+rect1.width
59      && rect1.y < rect2.y+rect2.height
60      && rect2.y < rect1.y+rect1.height;
61 }
62
63 static unsigned int gCountOfTestFuncCall;
64 class TestUploadObserver : public AtlasUploadObserver
65 {
66 public:
67   TestUploadObserver()
68   {}
69
70   virtual ~TestUploadObserver()
71   {}
72
73   void UploadCompleted()
74   {
75     gCountOfTestFuncCall++;
76   }
77 };
78
79 } // anonymous namespace
80
81 void dali_image_atlas_startup(void)
82 {
83   test_return_value = TET_UNDEF;
84 }
85
86 void dali_image_atlas_cleanup(void)
87 {
88   test_return_value = TET_PASS;
89 }
90
91 int UtcDaliImageAtlasNew(void)
92 {
93   ToolkitTestApplication application;
94
95   // invoke default handle constructor
96   ImageAtlas atlas;
97
98   DALI_TEST_CHECK( !atlas );
99
100   // initialise handle
101   atlas = ImageAtlas::New( 32, 32 );
102
103   DALI_TEST_CHECK( atlas );
104   END_TEST;
105 }
106
107 int UtcDaliImageAtlasCopyConstructor(void)
108 {
109   ToolkitTestApplication application;
110
111   ImageAtlas atlas = ImageAtlas::New( 32, 32);
112   ImageAtlas atlasCopy(atlas);
113
114   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
115   END_TEST;
116 }
117
118 int UtcDaliImageAtlasAssignmentOperator(void)
119 {
120   ToolkitTestApplication application;
121
122   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
123
124   ImageAtlas atlas2;
125   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
126
127   atlas2 = atlas;
128   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
129
130   END_TEST;
131 }
132
133 int UtcDaliImageAtlasGetAtlas(void)
134 {
135   ToolkitTestApplication application;
136
137   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
138   Texture image = atlas.GetAtlas();
139
140   // test the atlas created
141   DALI_TEST_EQUALS( (bool)image, true, TEST_LOCATION );
142   DALI_TEST_CHECK( image.GetHeight() == 32u );
143   DALI_TEST_CHECK( image.GetWidth() == 32u );
144
145   END_TEST;
146 }
147
148 int UtcDaliImageAtlasGetOccupancyRate(void)
149 {
150   ToolkitTestApplication application;
151
152   ImageAtlas atlas = ImageAtlas::New( 100, 100 );
153
154   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
155
156   Vector4 textureRect1;
157   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
158   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 34.f*34.f/10000.f, 0.001f, TEST_LOCATION );
159
160   Vector4 textureRect2;
161   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
162   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), (34.f*34.f+50.f*50.f)/10000.f, 0.001f, TEST_LOCATION );
163
164   END_TEST;
165 }
166
167 int UtcDaliImageAtlasSetBrokenImage(void)
168 {
169   ToolkitTestApplication application;
170   unsigned int size = 200;
171   ImageAtlas atlas = ImageAtlas::New( size, size );
172
173   Vector4 textureRect;
174   atlas.Upload( textureRect, gImageNonExist );
175   DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
176
177   // Set broken image
178   TestPlatformAbstraction& platform = application.GetPlatform();
179   platform.SetClosestImageSize(Vector2( 34, 34));
180   atlas.SetBrokenImage( gImage_34_RGBA );
181
182   // the non-exit image will be replaced with the broken image
183   platform.SetClosestImageSize(Vector2( 0, 0));
184   atlas.Upload( textureRect, gImageNonExist );
185
186   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect, size);
187   DALI_TEST_EQUALS( pixelArea.width, 34, TEST_LOCATION );
188   DALI_TEST_EQUALS( pixelArea.height, 34, TEST_LOCATION );
189
190   END_TEST;
191 }
192
193 int UtcDaliImageAtlasUploadP(void)
194 {
195   ToolkitTestApplication application;
196   unsigned int size = 200;
197   ImageAtlas atlas = ImageAtlas::New( size, size );
198
199   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
200   callStack.Reset();
201   callStack.Enable(true);
202
203   Vector4 textureRect1;
204   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
205   Vector4 textureRect2;
206   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
207   Vector4 textureRect3;
208   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
209
210   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
211   CallbackBase* callback = eventTrigger->GetCallback();
212
213   eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
214
215   CallbackBase::Execute( *callback );
216
217   application.SendNotification();
218   application.Render(RENDER_FRAME_INTERVAL);
219
220   callStack.Enable(false);
221
222   Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
223   DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
224   DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
225
226   TraceCallStack::NamedParams params;
227   params["width"] = ToString(pixelArea1.width);
228   params["height"] = ToString(pixelArea1.height);
229   params["xoffset"] = ToString(pixelArea1.x);
230   params["yoffset"] = ToString(pixelArea1.y);
231   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
232
233   Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
234   DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
235   DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
236
237   params["width"] = ToString(pixelArea2.width);
238   params["height"] = ToString(pixelArea2.height);
239   params["xoffset"] = ToString(pixelArea2.x);
240   params["yoffset"] = ToString(pixelArea2.y);
241   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
242
243   Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
244   DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
245   DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
246
247   params["width"] = ToString(pixelArea3.width);
248   params["height"] = ToString(pixelArea3.height);
249   params["xoffset"] = ToString(pixelArea3.x);
250   params["yoffset"] = ToString(pixelArea3.y);
251   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
252
253   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
254   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
255   DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
256
257   END_TEST;
258 }
259
260 int UtcDaliImageAtlasUploadWithObserver01(void)
261 {
262   TestApplication application;
263   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
264
265   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
266   CallbackBase* callback = eventTrigger->GetCallback();
267
268   gCountOfTestFuncCall = 0;
269   TestUploadObserver uploadObserver;
270
271   Vector4 textureRect1;
272   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, &uploadObserver );
273   Vector4 textureRect2;
274   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, NULL );
275   Vector4 textureRect3;
276   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, &uploadObserver );
277
278   // waiting until all three images are loaded and uploaded to atlas
279   eventTrigger->WaitingForTrigger( 3 );
280   CallbackBase::Execute( *callback );
281   application.SendNotification();
282   application.Render(RENDER_FRAME_INTERVAL);
283
284   // Check that TestFunc is called twice
285   DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
286
287   END_TEST;
288 }
289
290 int UtcDaliImageAtlasUploadWithObserver02(void)
291 {
292   TestApplication application;
293   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
294
295   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
296   CallbackBase* callback = eventTrigger->GetCallback();
297
298   gCountOfTestFuncCall = 0;
299   TestUploadObserver* uploadObserver = new TestUploadObserver;
300
301   Vector4 textureRect1;
302   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
303   Vector4 textureRect2;
304   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
305   Vector4 textureRect3;
306   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
307
308   // destroy the object.
309   delete uploadObserver;
310
311  // waiting until all three images are loaded and uploaded to atlas
312   eventTrigger->WaitingForTrigger( 3 );
313   CallbackBase::Execute( *callback );
314   application.Render(RENDER_FRAME_INTERVAL);
315   application.SendNotification();
316
317   // Check that TestFunc is called twice
318   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
319
320   END_TEST;
321 }
322
323 int UtcDaliImageAtlasUploadWithObserver03(void)
324 {
325   TestApplication application;
326
327   gCountOfTestFuncCall = 0;
328   TestUploadObserver* uploadObserver = new TestUploadObserver;
329
330   {
331     ImageAtlas atlas = ImageAtlas::New( 200, 200 );
332
333     Vector4 textureRect1;
334     atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
335     Vector4 textureRect2;
336     atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
337     Vector4 textureRect3;
338     atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
339   }
340
341   //ImageAtlas is out of scope, so it will get destroyed
342
343   application.Render(RENDER_FRAME_INTERVAL);
344   application.SendNotification();
345   application.SendNotification();
346   application.Render(RENDER_FRAME_INTERVAL);
347
348   // Check that TestFunc is called twice
349   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
350
351   END_TEST;
352 }
353
354 int UtcDaliImageAtlasRemove(void)
355 {
356   TestApplication application;
357   unsigned int size = 100;
358   ImageAtlas atlas = ImageAtlas::New( size, size );
359   Vector4 textureRect1;
360   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
361
362   atlas.Remove( textureRect1 );
363
364   Vector4 textureRect2;
365   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
366
367   // one pixel gap
368   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
369   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
370   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
371
372   END_TEST;
373 }
374
375 int UtcDaliImageAtlasImageView(void)
376 {
377   ToolkitTestApplication application;
378
379   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
380   callStack.Reset();
381   callStack.Enable(true);
382
383   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
384   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
385   Stage::GetCurrent().Add( imageView1 );
386   Stage::GetCurrent().Add( imageView2 );
387
388   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
389   while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
390   {
391     usleep(10);
392     eventTrigger = EventThreadCallback::Get();
393   }
394   CallbackBase* callback = eventTrigger->GetCallback();
395
396   eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
397
398   CallbackBase::Execute( *callback );
399
400   application.SendNotification();
401   application.Render(RENDER_FRAME_INTERVAL);
402
403   callStack.Enable(false);
404
405   TraceCallStack::NamedParams params1;
406   params1["width"] = "34";
407   params1["height"] = "34";
408   params1["xoffset"] = "0";
409   params1["yoffset"] = "0";
410
411   TraceCallStack::NamedParams params2;
412   params2["width"] = "50";
413   params2["height"] = "50";
414   params2["xoffset"] = "0";
415   params2["yoffset"] = "34";
416
417   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
418   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
419
420   callStack.Reset();
421   callStack.Enable(true);
422
423   // remove the imageView2 from stage, the second image will also be removed from atlas
424   // then the space on the atlas will be used by the third image added.
425   Stage::GetCurrent().Remove( imageView2 );
426   application.SendNotification();
427   application.Render(RENDER_FRAME_INTERVAL);
428   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
429   Stage::GetCurrent().Add( imageView3 );
430
431   eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
432   CallbackBase::Execute( *callback );
433
434   application.SendNotification();
435   application.Render(RENDER_FRAME_INTERVAL);
436
437   callStack.Enable(false);
438
439   TraceCallStack::NamedParams params3;
440   params3["width"] = "100";
441   params3["height"] = "100";
442   params3["xoffset"] = "0";
443   params3["yoffset"] = "34";
444
445   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
446
447   END_TEST;
448 }