2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
27 using namespace Dali::Toolkit;
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";
38 // this is image is not exist, for negative test
39 static const char* gImageNonExist = "non-exist.jpg";
41 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
43 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
45 Vector4 temp = textureCoordinate * size;
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 );
55 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
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;
63 static unsigned int gCountOfTestFuncCall;
64 class TestUploadObserver : public AtlasUploadObserver
70 virtual ~TestUploadObserver()
73 void UploadCompleted()
75 gCountOfTestFuncCall++;
79 } // anonymous namespace
81 void dali_image_atlas_startup(void)
83 test_return_value = TET_UNDEF;
86 void dali_image_atlas_cleanup(void)
88 test_return_value = TET_PASS;
91 int UtcDaliImageAtlasNew(void)
93 ToolkitTestApplication application;
95 // invoke default handle constructor
98 DALI_TEST_CHECK( !atlas );
101 atlas = ImageAtlas::New( 32, 32 );
103 DALI_TEST_CHECK( atlas );
107 int UtcDaliImageAtlasCopyConstructor(void)
109 ToolkitTestApplication application;
111 ImageAtlas atlas = ImageAtlas::New( 32, 32);
112 ImageAtlas atlasCopy(atlas);
114 DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
118 int UtcDaliImageAtlasAssignmentOperator(void)
120 ToolkitTestApplication application;
122 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
125 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
128 DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
133 int UtcDaliImageAtlasGetAtlas(void)
135 ToolkitTestApplication application;
137 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
138 Texture image = atlas.GetAtlas();
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 );
148 int UtcDaliImageAtlasGetOccupancyRate(void)
150 ToolkitTestApplication application;
152 ImageAtlas atlas = ImageAtlas::New( 100, 100 );
154 DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
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 );
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 );
167 int UtcDaliImageAtlasSetBrokenImage(void)
169 ToolkitTestApplication application;
170 unsigned int size = 200;
171 ImageAtlas atlas = ImageAtlas::New( size, size );
174 atlas.Upload( textureRect, gImageNonExist );
175 DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
178 TestPlatformAbstraction& platform = application.GetPlatform();
179 platform.SetClosestImageSize(Vector2( 34, 34));
180 atlas.SetBrokenImage( gImage_34_RGBA );
182 // the non-exit image will be replaced with the broken image
183 platform.SetClosestImageSize(Vector2( 0, 0));
184 atlas.Upload( textureRect, gImageNonExist );
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 );
193 int UtcDaliImageAtlasUploadP(void)
195 ToolkitTestApplication application;
196 unsigned int size = 200;
197 ImageAtlas atlas = ImageAtlas::New( size, size );
199 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
201 callStack.Enable(true);
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) );
210 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
211 CallbackBase* callback = eventTrigger->GetCallback();
213 eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
215 CallbackBase::Execute( *callback );
217 application.SendNotification();
218 application.Render(RENDER_FRAME_INTERVAL);
220 callStack.Enable(false);
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 );
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 ));
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 );
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 ) );
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 );
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 ) );
253 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
254 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
255 DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
260 int UtcDaliImageAtlasUploadWithObserver01(void)
262 TestApplication application;
263 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
265 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
266 CallbackBase* callback = eventTrigger->GetCallback();
268 gCountOfTestFuncCall = 0;
269 TestUploadObserver uploadObserver;
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 );
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);
284 // Check that TestFunc is called twice
285 DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
290 int UtcDaliImageAtlasUploadWithObserver02(void)
292 TestApplication application;
293 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
295 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
296 CallbackBase* callback = eventTrigger->GetCallback();
298 gCountOfTestFuncCall = 0;
299 TestUploadObserver* uploadObserver = new TestUploadObserver;
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 );
308 // destroy the object.
309 delete uploadObserver;
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();
317 // Check that TestFunc is called twice
318 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
323 int UtcDaliImageAtlasUploadWithObserver03(void)
325 TestApplication application;
327 gCountOfTestFuncCall = 0;
328 TestUploadObserver* uploadObserver = new TestUploadObserver;
331 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
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 );
341 //ImageAtlas is out of scope, so it will get destroyed
343 application.Render(RENDER_FRAME_INTERVAL);
344 application.SendNotification();
345 application.SendNotification();
346 application.Render(RENDER_FRAME_INTERVAL);
348 // Check that TestFunc is called twice
349 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
354 int UtcDaliImageAtlasRemove(void)
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) );
362 atlas.Remove( textureRect1 );
364 Vector4 textureRect2;
365 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
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 );
375 int UtcDaliImageAtlasImageView(void)
377 ToolkitTestApplication application;
379 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
381 callStack.Enable(true);
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 );
388 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
389 while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
392 eventTrigger = EventThreadCallback::Get();
394 CallbackBase* callback = eventTrigger->GetCallback();
396 eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
398 CallbackBase::Execute( *callback );
400 application.SendNotification();
401 application.Render(RENDER_FRAME_INTERVAL);
403 callStack.Enable(false);
405 TraceCallStack::NamedParams params1;
406 params1["width"] = "34";
407 params1["height"] = "34";
408 params1["xoffset"] = "0";
409 params1["yoffset"] = "0";
411 TraceCallStack::NamedParams params2;
412 params2["width"] = "50";
413 params2["height"] = "50";
414 params2["xoffset"] = "0";
415 params2["yoffset"] = "34";
417 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
418 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
421 callStack.Enable(true);
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 );
431 eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
432 CallbackBase::Execute( *callback );
434 application.SendNotification();
435 application.Render(RENDER_FRAME_INTERVAL);
437 callStack.Enable(false);
439 TraceCallStack::NamedParams params3;
440 params3["width"] = "100";
441 params3["height"] = "100";
442 params3["xoffset"] = "0";
443 params3["yoffset"] = "34";
445 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );