Async image loading
[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 } // anonymous namespace
64
65 void dali_image_atlas_startup(void)
66 {
67   test_return_value = TET_UNDEF;
68 }
69
70 void dali_image_atlas_cleanup(void)
71 {
72   test_return_value = TET_PASS;
73 }
74
75 int UtcDaliImageAtlasNew(void)
76 {
77   ToolkitTestApplication application;
78
79   // invoke default handle constructor
80   ImageAtlas atlas;
81
82   DALI_TEST_CHECK( !atlas );
83
84   // initialise handle
85   atlas = ImageAtlas::New( 32, 32 );
86
87   DALI_TEST_CHECK( atlas );
88   END_TEST;
89 }
90
91 int UtcDaliImageAtlasCopyConstructor(void)
92 {
93   ToolkitTestApplication application;
94
95   ImageAtlas atlas = ImageAtlas::New( 32, 32);
96   ImageAtlas atlasCopy(atlas);
97
98   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
99   END_TEST;
100 }
101
102 int UtcDaliImageAtlasAssignmentOperator(void)
103 {
104   ToolkitTestApplication application;
105
106   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
107
108   ImageAtlas atlas2;
109   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
110
111   atlas2 = atlas;
112   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
113
114   END_TEST;
115 }
116
117 int UtcDaliImageAtlasGetAtlas(void)
118 {
119   ToolkitTestApplication application;
120
121   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
122   Texture image = atlas.GetAtlas();
123
124   // test the atlas created
125   DALI_TEST_EQUALS( (bool)image, true, TEST_LOCATION );
126   DALI_TEST_CHECK( image.GetHeight() == 32u );
127   DALI_TEST_CHECK( image.GetWidth() == 32u );
128
129   END_TEST;
130 }
131
132 int UtcDaliImageAtlasGetOccupancyRate(void)
133 {
134   ToolkitTestApplication application;
135
136   ImageAtlas atlas = ImageAtlas::New( 100, 100 );
137
138   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
139
140   Vector4 textureRect1;
141   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
142   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 34.f*34.f/10000.f, 0.001f, TEST_LOCATION );
143
144   Vector4 textureRect2;
145   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
146   DALI_TEST_EQUALS( atlas.GetOccupancyRate(), (34.f*34.f+50.f*50.f)/10000.f, 0.001f, TEST_LOCATION );
147
148   END_TEST;
149 }
150
151 int UtcDaliImageAtlasSetBrokenImage(void)
152 {
153   ToolkitTestApplication application;
154   unsigned int size = 200;
155   ImageAtlas atlas = ImageAtlas::New( size, size );
156
157   Vector4 textureRect;
158   atlas.Upload( textureRect, gImageNonExist );
159   DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
160
161   // Set broken image
162   TestPlatformAbstraction& platform = application.GetPlatform();
163   platform.SetClosestImageSize(Vector2( 34, 34));
164   atlas.SetBrokenImage( gImage_34_RGBA );
165
166   // the non-exit image will be replaced with the broken image
167   platform.SetClosestImageSize(Vector2( 0, 0));
168   atlas.Upload( textureRect, gImageNonExist );
169
170   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect, size);
171   DALI_TEST_EQUALS( pixelArea.width, 34, TEST_LOCATION );
172   DALI_TEST_EQUALS( pixelArea.height, 34, TEST_LOCATION );
173
174   END_TEST;
175 }
176
177 int UtcDaliImageAtlasUploadP(void)
178 {
179   ToolkitTestApplication application;
180   unsigned int size = 200;
181   ImageAtlas atlas = ImageAtlas::New( size, size );
182
183   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
184   callStack.Reset();
185   callStack.Enable(true);
186
187   Vector4 textureRect1;
188   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
189   Vector4 textureRect2;
190   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
191   Vector4 textureRect3;
192   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
193
194   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
195   CallbackBase* callback = eventTrigger->GetCallback();
196
197   eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
198
199   CallbackBase::Execute( *callback );
200
201   application.SendNotification();
202   application.Render(RENDER_FRAME_INTERVAL);
203
204   callStack.Enable(false);
205
206   Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
207   DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
208   DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
209
210   TraceCallStack::NamedParams params;
211   params["width"] = ToString(pixelArea1.width);
212   params["height"] = ToString(pixelArea1.height);
213   params["xoffset"] = ToString(pixelArea1.x);
214   params["yoffset"] = ToString(pixelArea1.y);
215   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
216
217   Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
218   DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
219   DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
220
221   params["width"] = ToString(pixelArea2.width);
222   params["height"] = ToString(pixelArea2.height);
223   params["xoffset"] = ToString(pixelArea2.x);
224   params["yoffset"] = ToString(pixelArea2.y);
225   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
226
227   Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
228   DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
229   DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
230
231   params["width"] = ToString(pixelArea3.width);
232   params["height"] = ToString(pixelArea3.height);
233   params["xoffset"] = ToString(pixelArea3.x);
234   params["yoffset"] = ToString(pixelArea3.y);
235   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
236
237   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
238   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
239   DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
240
241   END_TEST;
242 }
243
244 int UtcDaliImageAtlasRemove(void)
245 {
246   TestApplication application;
247   unsigned int size = 100;
248   ImageAtlas atlas = ImageAtlas::New( size, size );
249   Vector4 textureRect1;
250   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
251
252   atlas.Remove( textureRect1 );
253
254   Vector4 textureRect2;
255   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
256
257   // one pixel gap
258   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
259   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
260   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
261
262   END_TEST;
263 }
264
265 int UtcDaliImageAtlasImageView(void)
266 {
267   ToolkitTestApplication application;
268
269   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
270   callStack.Reset();
271   callStack.Enable(true);
272
273   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
274   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
275   Stage::GetCurrent().Add( imageView1 );
276   Stage::GetCurrent().Add( imageView2 );
277
278   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
279   while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
280   {
281     usleep(10);
282     eventTrigger = EventThreadCallback::Get();
283   }
284   CallbackBase* callback = eventTrigger->GetCallback();
285
286   eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
287
288   CallbackBase::Execute( *callback );
289
290   application.SendNotification();
291   application.Render(RENDER_FRAME_INTERVAL);
292
293   callStack.Enable(false);
294
295   TraceCallStack::NamedParams params1;
296   params1["width"] = "34";
297   params1["height"] = "34";
298   params1["xoffset"] = "0";
299   params1["yoffset"] = "0";
300
301   TraceCallStack::NamedParams params2;
302   params2["width"] = "50";
303   params2["height"] = "50";
304   params2["xoffset"] = "0";
305   params2["yoffset"] = "34";
306
307   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
308   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
309
310   callStack.Reset();
311   callStack.Enable(true);
312
313   // remove the imageView2 from stage, the second image will also be removed from atlas
314   // then the space on the atlas will be used by the third image added.
315   Stage::GetCurrent().Remove( imageView2 );
316   application.SendNotification();
317   application.Render(RENDER_FRAME_INTERVAL);
318   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
319   Stage::GetCurrent().Add( imageView3 );
320
321   eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
322   CallbackBase::Execute( *callback );
323
324   application.SendNotification();
325   application.Render(RENDER_FRAME_INTERVAL);
326
327   callStack.Enable(false);
328
329   TraceCallStack::NamedParams params3;
330   params3["width"] = "100";
331   params3["height"] = "100";
332   params3["xoffset"] = "0";
333   params3["yoffset"] = "34";
334
335   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
336
337   END_TEST;
338 }