0bee5e58a3bf375204477b45a2a5fdd006ae328d
[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   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
211
212   application.SendNotification();
213   application.Render(RENDER_FRAME_INTERVAL);
214
215   callStack.Enable(false);
216
217   Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
218   DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
219   DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
220
221   TraceCallStack::NamedParams params;
222   params["width"] = ToString(pixelArea1.width);
223   params["height"] = ToString(pixelArea1.height);
224   params["xoffset"] = ToString(pixelArea1.x);
225   params["yoffset"] = ToString(pixelArea1.y);
226   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
227
228   Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
229   DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
230   DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
231
232   params["width"] = ToString(pixelArea2.width);
233   params["height"] = ToString(pixelArea2.height);
234   params["xoffset"] = ToString(pixelArea2.x);
235   params["yoffset"] = ToString(pixelArea2.y);
236   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
237
238   Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
239   DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
240   DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
241
242   params["width"] = ToString(pixelArea3.width);
243   params["height"] = ToString(pixelArea3.height);
244   params["xoffset"] = ToString(pixelArea3.x);
245   params["yoffset"] = ToString(pixelArea3.y);
246   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
247
248   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
249   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
250   DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
251
252   END_TEST;
253 }
254
255 int UtcDaliImageAtlasUploadWithObserver01(void)
256 {
257   TestApplication application;
258   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
259
260
261   gCountOfTestFuncCall = 0;
262   TestUploadObserver uploadObserver;
263
264   Vector4 textureRect1;
265   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, &uploadObserver );
266   Vector4 textureRect2;
267   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, NULL );
268   Vector4 textureRect3;
269   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, &uploadObserver );
270
271   // waiting until all three images are loaded and uploaded to atlas
272   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
273   application.SendNotification();
274   application.Render(RENDER_FRAME_INTERVAL);
275
276   // Check that TestFunc is called twice
277   DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
278
279   END_TEST;
280 }
281
282 int UtcDaliImageAtlasUploadWithObserver02(void)
283 {
284   TestApplication application;
285   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
286
287   gCountOfTestFuncCall = 0;
288   TestUploadObserver* uploadObserver = new TestUploadObserver;
289
290   Vector4 textureRect1;
291   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
292   Vector4 textureRect2;
293   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
294   Vector4 textureRect3;
295   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
296
297   // destroy the object.
298   delete uploadObserver;
299
300  // waiting until all three images are loaded and uploaded to atlas
301   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
302
303   application.Render(RENDER_FRAME_INTERVAL);
304   application.SendNotification();
305
306   // Check that TestFunc is called twice
307   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
308
309   END_TEST;
310 }
311
312 int UtcDaliImageAtlasUploadWithObserver03(void)
313 {
314   TestApplication application;
315
316   gCountOfTestFuncCall = 0;
317   TestUploadObserver* uploadObserver = new TestUploadObserver;
318
319   {
320     ImageAtlas atlas = ImageAtlas::New( 200, 200 );
321
322     Vector4 textureRect1;
323     atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
324     Vector4 textureRect2;
325     atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
326     Vector4 textureRect3;
327     atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
328   }
329
330   //ImageAtlas is out of scope, so it will get destroyed
331
332   application.Render(RENDER_FRAME_INTERVAL);
333   application.SendNotification();
334   application.SendNotification();
335   application.Render(RENDER_FRAME_INTERVAL);
336
337   // Check that TestFunc is called twice
338   DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
339
340   END_TEST;
341 }
342
343 int UtcDaliImageAtlasRemove(void)
344 {
345   TestApplication application;
346   unsigned int size = 100;
347   ImageAtlas atlas = ImageAtlas::New( size, size );
348   Vector4 textureRect1;
349   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
350
351   atlas.Remove( textureRect1 );
352
353   Vector4 textureRect2;
354   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
355
356   // one pixel gap
357   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
358   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
359   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
360
361   END_TEST;
362 }
363
364 int UtcDaliImageAtlasImageView(void)
365 {
366   ToolkitTestApplication application;
367
368   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
369   callStack.Reset();
370   callStack.Enable(true);
371
372   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
373   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
374
375   // ImageView doesn't do size negotiation properly: it only listens to OnSizeSet:
376   imageView1.SetSize( 100, 100 );
377   imageView2.SetSize( 100, 100 );
378   imageView1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
379   imageView2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
380
381   application.GetPlatform().SetClosestImageSize(  Vector2(34, 34) );
382   Stage::GetCurrent().Add( imageView1 );
383   application.GetPlatform().SetClosestImageSize(  Vector2(50, 50) );
384   Stage::GetCurrent().Add( imageView2 );
385
386   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
387
388   application.SendNotification();
389   application.Render(RENDER_FRAME_INTERVAL);
390
391   callStack.Enable(false);
392
393   TraceCallStack::NamedParams params1;
394   params1["width"] = "34";
395   params1["height"] = "34";
396   params1["xoffset"] = "0";
397   params1["yoffset"] = "0";
398
399   TraceCallStack::NamedParams params2;
400   params2["width"] = "50";
401   params2["height"] = "50";
402   params2["xoffset"] = "0";
403   params2["yoffset"] = "34";
404
405   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
406   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
407
408   callStack.Reset();
409   callStack.Enable(true);
410
411   // remove the imageView2 from stage, the second image will also be removed from atlas
412   // then the space on the atlas will be used by the third image added.
413   Stage::GetCurrent().Remove( imageView2 );
414   application.SendNotification();
415   application.Render(RENDER_FRAME_INTERVAL);
416   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
417   application.GetPlatform().SetClosestImageSize(  Vector2(100, 100) );
418   Stage::GetCurrent().Add( imageView3 );
419
420   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
421
422   application.SendNotification();
423   application.Render(RENDER_FRAME_INTERVAL);
424
425   callStack.Enable(false);
426
427   TraceCallStack::NamedParams params3;
428   params3["width"] = "100";
429   params3["height"] = "100";
430   params3["xoffset"] = "0";
431   params3["yoffset"] = "34";
432
433   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
434
435   END_TEST;
436 }