Stop resetting transform values to default in OnRelayout & OnSizeSet
[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 PixelData CreatePixelData( unsigned int width, unsigned int height )
44 {
45   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
46
47   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
48   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
49
50   return pixelData;
51 }
52
53 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
54 {
55   Vector4 temp = textureCoordinate * size;
56   Rect<int> pixelArea;
57   pixelArea.x = static_cast<int>( temp.x );
58   pixelArea.y = static_cast<int>( temp.y );
59   pixelArea.width = static_cast<int>( temp.z-temp.x+1.01f );
60   pixelArea.height = static_cast<int>( temp.w-temp.y+1.01f );
61
62   return pixelArea;
63 }
64
65 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float width, float height )
66 {
67   Rect<int> pixelArea;
68   pixelArea.x = static_cast<int>( textureCoordinate.x*width );
69   pixelArea.y = static_cast<int>( textureCoordinate.y*height);
70   pixelArea.width = static_cast<int>( (textureCoordinate.z-textureCoordinate.x)*width+1.01f );
71   pixelArea.height = static_cast<int>( (textureCoordinate.w-textureCoordinate.y)*height+1.01f );
72
73   return pixelArea;
74 }
75
76 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
77 {
78  return rect1.x < rect2.x+rect2.width
79      && rect2.x < rect1.x+rect1.width
80      && rect1.y < rect2.y+rect2.height
81      && rect2.y < rect1.y+rect1.height;
82 }
83
84 static unsigned int gCountOfTestFuncCall;
85 class TestUploadObserver : public AtlasUploadObserver
86 {
87 public:
88   TestUploadObserver()
89   {}
90
91   virtual ~TestUploadObserver()
92   {}
93
94   void UploadCompleted()
95   {
96     gCountOfTestFuncCall++;
97   }
98 };
99
100 } // anonymous namespace
101
102 void dali_image_atlas_startup(void)
103 {
104   test_return_value = TET_UNDEF;
105 }
106
107 void dali_image_atlas_cleanup(void)
108 {
109   test_return_value = TET_PASS;
110 }
111
112 int UtcDaliImageAtlasNew(void)
113 {
114   ToolkitTestApplication application;
115
116   // invoke default handle constructor
117   ImageAtlas atlas;
118
119   DALI_TEST_CHECK( !atlas );
120
121   // initialise handle
122   atlas = ImageAtlas::New( 32, 32 );
123
124   DALI_TEST_CHECK( atlas );
125   END_TEST;
126 }
127
128 int UtcDaliImageAtlasCopyConstructor(void)
129 {
130   ToolkitTestApplication application;
131
132   ImageAtlas atlas = ImageAtlas::New( 32, 32);
133   ImageAtlas atlasCopy(atlas);
134
135   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
136   END_TEST;
137 }
138
139 int UtcDaliImageAtlasAssignmentOperator(void)
140 {
141   ToolkitTestApplication application;
142
143   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
144
145   ImageAtlas atlas2;
146   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
147
148   atlas2 = atlas;
149   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
150
151   END_TEST;
152 }
153
154 int UtcDaliImageAtlasGetAtlas(void)
155 {
156   ToolkitTestApplication application;
157
158   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
159   Texture image = atlas.GetAtlas();
160
161   // test the atlas created
162   DALI_TEST_EQUALS( (bool)image, true, TEST_LOCATION );
163   DALI_TEST_CHECK( image.GetHeight() == 32u );
164   DALI_TEST_CHECK( image.GetWidth() == 32u );
165
166   END_TEST;
167 }
168
169 int UtcDaliImageAtlasGetOccupancyRate(void)
170 {
171   ToolkitTestApplication application;
172
173   ImageAtlas atlas = ImageAtlas::New( 100, 100 );
174
175   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
176
177   Vector4 textureRect1;
178   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
179   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 34.f*34.f/10000.f, 0.001f, TEST_LOCATION );
180
181   Vector4 textureRect2;
182   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
183   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), (34.f*34.f+50.f*50.f)/10000.f, 0.001f, TEST_LOCATION );
184
185   END_TEST;
186 }
187
188 int UtcDaliImageAtlasSetBrokenImage(void)
189 {
190   ToolkitTestApplication application;
191   unsigned int size = 200;
192   ImageAtlas atlas = ImageAtlas::New( size, size );
193
194   Vector4 textureRect;
195   atlas.Upload( textureRect, gImageNonExist );
196   DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
197
198   // Set broken image
199   TestPlatformAbstraction& platform = application.GetPlatform();
200   platform.SetClosestImageSize(Vector2( 34, 34));
201   atlas.SetBrokenImage( gImage_34_RGBA );
202
203   // the non-exit image will be replaced with the broken image
204   platform.SetClosestImageSize(Vector2( 0, 0));
205   atlas.Upload( textureRect, gImageNonExist );
206
207   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect, size);
208   DALI_TEST_EQUALS( pixelArea.width, 34, TEST_LOCATION );
209   DALI_TEST_EQUALS( pixelArea.height, 34, TEST_LOCATION );
210
211   END_TEST;
212 }
213
214 int UtcDaliImageAtlasUploadP(void)
215 {
216   ToolkitTestApplication application;
217   unsigned int size = 200;
218   ImageAtlas atlas = ImageAtlas::New( size, size );
219
220   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
221   callStack.Reset();
222   callStack.Enable(true);
223
224   Vector4 textureRect1;
225   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
226   Vector4 textureRect2;
227   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
228   Vector4 textureRect3;
229   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
230
231   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
232
233   application.SendNotification();
234   application.Render(RENDER_FRAME_INTERVAL);
235
236   callStack.Enable(false);
237
238   Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
239   DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
240   DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
241
242   TraceCallStack::NamedParams params;
243   params["width"] = ToString(pixelArea1.width);
244   params["height"] = ToString(pixelArea1.height);
245   params["xoffset"] = ToString(pixelArea1.x);
246   params["yoffset"] = ToString(pixelArea1.y);
247   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
248
249   Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
250   DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
251   DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
252
253   params["width"] = ToString(pixelArea2.width);
254   params["height"] = ToString(pixelArea2.height);
255   params["xoffset"] = ToString(pixelArea2.x);
256   params["yoffset"] = ToString(pixelArea2.y);
257   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
258
259   Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
260   DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
261   DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
262
263   params["width"] = ToString(pixelArea3.width);
264   params["height"] = ToString(pixelArea3.height);
265   params["xoffset"] = ToString(pixelArea3.x);
266   params["yoffset"] = ToString(pixelArea3.y);
267   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
268
269   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
270   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
271   DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
272
273   END_TEST;
274 }
275
276 int UtcDaliImageAtlasUploadWithObserver01(void)
277 {
278   TestApplication application;
279   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
280
281
282   gCountOfTestFuncCall = 0;
283   TestUploadObserver uploadObserver;
284
285   Vector4 textureRect1;
286   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, &uploadObserver );
287   Vector4 textureRect2;
288   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, NULL );
289   Vector4 textureRect3;
290   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, &uploadObserver );
291
292   // waiting until all three images are loaded and uploaded to atlas
293   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
294   application.SendNotification();
295   application.Render(RENDER_FRAME_INTERVAL);
296
297   // Check that TestFunc is called twice
298   DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
299
300   END_TEST;
301 }
302
303 int UtcDaliImageAtlasUploadWithObserver02(void)
304 {
305   TestApplication application;
306   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
307
308   gCountOfTestFuncCall = 0;
309   TestUploadObserver* uploadObserver = new TestUploadObserver;
310
311   Vector4 textureRect1;
312   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
313   Vector4 textureRect2;
314   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
315   Vector4 textureRect3;
316   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
317
318   // destroy the object.
319   delete uploadObserver;
320
321  // waiting until all three images are loaded and uploaded to atlas
322   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
323
324   application.Render(RENDER_FRAME_INTERVAL);
325   application.SendNotification();
326
327   // Check that TestFunc is called twice
328   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
329
330   END_TEST;
331 }
332
333 int UtcDaliImageAtlasUploadWithObserver03(void)
334 {
335   TestApplication application;
336
337   gCountOfTestFuncCall = 0;
338   TestUploadObserver* uploadObserver = new TestUploadObserver;
339
340   {
341     ImageAtlas atlas = ImageAtlas::New( 200, 200 );
342
343     Vector4 textureRect1;
344     atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
345     Vector4 textureRect2;
346     atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
347     Vector4 textureRect3;
348     atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
349   }
350
351   //ImageAtlas is out of scope, so it will get destroyed
352
353   application.Render(RENDER_FRAME_INTERVAL);
354   application.SendNotification();
355   application.SendNotification();
356   application.Render(RENDER_FRAME_INTERVAL);
357
358   // Check that TestFunc is called twice
359   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
360
361   END_TEST;
362 }
363
364 int UtcDaliImageAtlasRemove(void)
365 {
366   TestApplication application;
367   unsigned int size = 100;
368   ImageAtlas atlas = ImageAtlas::New( size, size );
369   Vector4 textureRect1;
370   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
371
372   atlas.Remove( textureRect1 );
373
374   Vector4 textureRect2;
375   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
376
377   // one pixel gap
378   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
379   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
380   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
381
382   END_TEST;
383 }
384
385 int UtcDaliImageAtlasImageView(void)
386 {
387   ToolkitTestApplication application;
388
389   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
390   callStack.Reset();
391   callStack.Enable(true);
392
393   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
394   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
395
396   // ImageView doesn't do size negotiation properly: it only listens to OnSizeSet:
397   imageView1.SetSize( 100, 100 );
398   imageView2.SetSize( 100, 100 );
399   imageView1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
400   imageView2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
401
402   application.GetPlatform().SetClosestImageSize(  Vector2(34, 34) );
403   Stage::GetCurrent().Add( imageView1 );
404   application.GetPlatform().SetClosestImageSize(  Vector2(50, 50) );
405   Stage::GetCurrent().Add( imageView2 );
406
407   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
408
409   application.SendNotification();
410   application.Render(RENDER_FRAME_INTERVAL);
411
412   callStack.Enable(false);
413
414   TraceCallStack::NamedParams params1;
415   params1["width"] = "34";
416   params1["height"] = "34";
417   params1["xoffset"] = "0";
418   params1["yoffset"] = "0";
419
420   TraceCallStack::NamedParams params2;
421   params2["width"] = "50";
422   params2["height"] = "50";
423   params2["xoffset"] = "0";
424   params2["yoffset"] = "34";
425
426   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
427   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
428
429   callStack.Reset();
430   callStack.Enable(true);
431
432   // remove the imageView2 from stage, the second image will also be removed from atlas
433   // then the space on the atlas will be used by the third image added.
434   Stage::GetCurrent().Remove( imageView2 );
435   application.SendNotification();
436   application.Render(RENDER_FRAME_INTERVAL);
437   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
438   application.GetPlatform().SetClosestImageSize(  Vector2(100, 100) );
439   Stage::GetCurrent().Add( imageView3 );
440
441   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
442
443   application.SendNotification();
444   application.Render(RENDER_FRAME_INTERVAL);
445
446   callStack.Enable(false);
447
448   TraceCallStack::NamedParams params3;
449   params3["width"] = "100";
450   params3["height"] = "100";
451   params3["xoffset"] = "0";
452   params3["yoffset"] = "34";
453
454   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
455
456   END_TEST;
457 }
458
459 int UtcDaliImageAtlasPackToAtlas(void)
460 {
461   ToolkitTestApplication application;
462
463   std::vector<PixelData> pixelDataContainer;
464   pixelDataContainer.push_back( CreatePixelData( 20, 30 ) );
465   pixelDataContainer.push_back( CreatePixelData( 10, 10 ) );
466   pixelDataContainer.push_back( CreatePixelData( 45, 30 ) );
467   pixelDataContainer.push_back( CreatePixelData( 20, 20 ) );
468
469   Dali::Vector<Vector4> textureRects;
470   Texture texture = ImageAtlas::PackToAtlas( pixelDataContainer, textureRects  );
471
472  // --------------
473  // |            |
474  // |    45*30   |
475 //  |            |
476 //  --------------
477 //  | 20 |    | 20*20
478 //  |  * |____|
479 //  | 30 |  |  10*10
480 //  --------
481
482   DALI_TEST_EQUALS( texture.GetWidth(), 45, TEST_LOCATION );
483   DALI_TEST_EQUALS( texture.GetHeight(), 60, TEST_LOCATION );
484
485   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRects[0], texture.GetWidth(), texture.GetHeight());
486   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
487   DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
488   DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
489   DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
490
491   pixelArea = TextureCoordinateToPixelArea(textureRects[1], texture.GetWidth(), texture.GetHeight());
492   DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
493   DALI_TEST_EQUALS( pixelArea.y, 50, TEST_LOCATION );
494   DALI_TEST_EQUALS( pixelArea.width, 10, TEST_LOCATION );
495   DALI_TEST_EQUALS( pixelArea.height, 10, TEST_LOCATION );
496
497   pixelArea = TextureCoordinateToPixelArea(textureRects[2], texture.GetWidth(), texture.GetHeight());
498   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
499   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
500   DALI_TEST_EQUALS( pixelArea.width, 45, TEST_LOCATION );
501   DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
502
503   pixelArea = TextureCoordinateToPixelArea(textureRects[3], texture.GetWidth(), texture.GetHeight());
504   DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
505   DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
506   DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
507   DALI_TEST_EQUALS( pixelArea.height, 20, TEST_LOCATION );
508
509   END_TEST;
510 }