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