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/devel-api/visuals/image-visual-properties-devel.h>
26 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
27 #include <dali-toolkit/dali-toolkit.h>
30 using namespace Dali::Toolkit;
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";
41 // Empty image, for testing broken image loading
42 static const char* gEmptyImage = TEST_RESOURCE_DIR "/empty.bmp";
44 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
46 PixelData CreatePixelData( unsigned int width, unsigned int height )
48 unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
50 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
51 PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
56 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
58 Vector4 temp = textureCoordinate * size;
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 );
68 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float width, float height )
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 );
79 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
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;
87 static unsigned int gCountOfTestFuncCall;
88 class TestUploadObserver : public AtlasUploadObserver
94 virtual ~TestUploadObserver()
97 void UploadCompleted()
99 gCountOfTestFuncCall++;
103 } // anonymous namespace
105 void dali_image_atlas_startup(void)
107 test_return_value = TET_UNDEF;
110 void dali_image_atlas_cleanup(void)
112 test_return_value = TET_PASS;
115 int UtcDaliImageAtlasNew(void)
117 ToolkitTestApplication application;
119 // invoke default handle constructor
122 DALI_TEST_CHECK( !atlas );
125 atlas = ImageAtlas::New( 32, 32 );
127 DALI_TEST_CHECK( atlas );
131 int UtcDaliImageAtlasCopyConstructor(void)
133 ToolkitTestApplication application;
135 ImageAtlas atlas = ImageAtlas::New( 32, 32);
136 ImageAtlas atlasCopy(atlas);
138 DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
142 int UtcDaliImageAtlasAssignmentOperator(void)
144 ToolkitTestApplication application;
146 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
149 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
152 DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
157 int UtcDaliImageAtlasGetAtlas(void)
159 ToolkitTestApplication application;
161 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
162 Texture image = atlas.GetAtlas();
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 );
172 int UtcDaliImageAtlasGetOccupancyRate(void)
174 ToolkitTestApplication application;
176 ImageAtlas atlas = ImageAtlas::New( 100, 100 );
178 DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
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 );
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 );
191 int UtcDaliImageAtlasSetBrokenImage(void)
193 ToolkitTestApplication application;
194 unsigned int size = 200;
195 ImageAtlas atlas = ImageAtlas::New( size, size );
198 TestPlatformAbstraction& platform = application.GetPlatform();
199 platform.SetClosestImageSize(Vector2( 34, 34));
200 atlas.SetBrokenImage( gImage_34_RGBA );
204 // the empty image will be replaced with the broken image
205 platform.SetClosestImageSize(Vector2( 20, 20));
206 atlas.Upload( textureRect, gEmptyImage );
207 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
209 Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect, size);
210 DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
211 DALI_TEST_EQUALS( pixelArea.height, 20, TEST_LOCATION );
218 int UtcDaliImageAtlasUploadP(void)
220 ToolkitTestApplication application;
221 unsigned int size = 200;
222 ImageAtlas atlas = ImageAtlas::New( size, size );
224 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
226 callStack.Enable(true);
228 Vector4 textureRect1;
229 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
230 Vector4 textureRect2;
231 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
232 Vector4 textureRect3;
233 atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
235 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
237 application.SendNotification();
238 application.Render(RENDER_FRAME_INTERVAL);
240 callStack.Enable(false);
242 Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
243 DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
244 DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
246 TraceCallStack::NamedParams params;
247 params["width"] = ToString(pixelArea1.width);
248 params["height"] = ToString(pixelArea1.height);
249 params["xoffset"] = ToString(pixelArea1.x);
250 params["yoffset"] = ToString(pixelArea1.y);
251 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
253 Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
254 DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
255 DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
257 params["width"] = ToString(pixelArea2.width);
258 params["height"] = ToString(pixelArea2.height);
259 params["xoffset"] = ToString(pixelArea2.x);
260 params["yoffset"] = ToString(pixelArea2.y);
261 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
263 Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
264 DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
265 DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
267 params["width"] = ToString(pixelArea3.width);
268 params["height"] = ToString(pixelArea3.height);
269 params["xoffset"] = ToString(pixelArea3.x);
270 params["yoffset"] = ToString(pixelArea3.y);
271 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
273 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
274 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
275 DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
280 int UtcDaliImageAtlasUploadWithObserver01(void)
282 TestApplication application;
283 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
286 gCountOfTestFuncCall = 0;
287 TestUploadObserver uploadObserver;
289 Vector4 textureRect1;
290 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, &uploadObserver );
291 Vector4 textureRect2;
292 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, NULL );
293 Vector4 textureRect3;
294 atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, &uploadObserver );
296 // waiting until all three images are loaded and uploaded to atlas
297 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
298 application.SendNotification();
299 application.Render(RENDER_FRAME_INTERVAL);
301 // Check that TestFunc is called twice
302 DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
307 int UtcDaliImageAtlasUploadWithObserver02(void)
309 TestApplication application;
310 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
312 gCountOfTestFuncCall = 0;
313 TestUploadObserver* uploadObserver = new TestUploadObserver;
315 Vector4 textureRect1;
316 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
317 Vector4 textureRect2;
318 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
319 Vector4 textureRect3;
320 atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
322 // destroy the object.
323 delete uploadObserver;
325 // waiting until all three images are loaded and uploaded to atlas
326 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
328 application.Render(RENDER_FRAME_INTERVAL);
329 application.SendNotification();
331 // Check that TestFunc is called twice
332 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
337 int UtcDaliImageAtlasUploadWithObserver03(void)
339 TestApplication application;
341 gCountOfTestFuncCall = 0;
342 TestUploadObserver* uploadObserver = new TestUploadObserver;
345 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
347 Vector4 textureRect1;
348 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34), FittingMode::DEFAULT, true, uploadObserver );
349 Vector4 textureRect2;
350 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50), FittingMode::DEFAULT, true, uploadObserver );
351 Vector4 textureRect3;
352 atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, uploadObserver );
355 //ImageAtlas is out of scope, so it will get destroyed
357 application.Render(RENDER_FRAME_INTERVAL);
358 application.SendNotification();
359 application.SendNotification();
360 application.Render(RENDER_FRAME_INTERVAL);
362 // Check that TestFunc is called twice
363 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
368 int UtcDaliImageAtlasRemove(void)
370 TestApplication application;
371 unsigned int size = 100;
372 ImageAtlas atlas = ImageAtlas::New( size, size );
373 Vector4 textureRect1;
374 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
376 atlas.Remove( textureRect1 );
378 Vector4 textureRect2;
379 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
382 Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
383 DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
384 DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
389 int UtcDaliImageAtlasImageView(void)
391 ToolkitTestApplication application;
393 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
395 callStack.Enable(true);
397 Property::Map imageMap1;
399 imageMap1[ ImageVisual::Property::URL ] = gImage_34_RGBA;
400 imageMap1[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
401 imageMap1[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
402 imageMap1[ DevelImageVisual::Property::ATLASING] = true;
404 Property::Map imageMap2;
406 imageMap2[ ImageVisual::Property::URL ] = gImage_50_RGBA;
407 imageMap2[ ImageVisual::Property::DESIRED_HEIGHT ] = 50;
408 imageMap2[ ImageVisual::Property::DESIRED_WIDTH ] = 50;
409 imageMap2[ DevelImageVisual::Property::ATLASING ] = true;
411 ImageView imageView1 = ImageView::New();
412 imageView1.SetProperty( ImageView::Property::IMAGE, imageMap1 );
414 ImageView imageView2 = ImageView::New();
415 imageView2.SetProperty( ImageView::Property::IMAGE, imageMap2 );
417 // ImageView doesn't do size negotiation properly: it only listens to OnSizeSet:
418 imageView1.SetSize( 100, 100 );
419 imageView2.SetSize( 100, 100 );
420 imageView1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
421 imageView2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
423 application.GetPlatform().SetClosestImageSize( Vector2(34, 34) );
424 Stage::GetCurrent().Add( imageView1 );
425 application.GetPlatform().SetClosestImageSize( Vector2(50, 50) );
426 Stage::GetCurrent().Add( imageView2 );
428 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
430 application.SendNotification();
431 application.Render(RENDER_FRAME_INTERVAL);
433 callStack.Enable(false);
435 TraceCallStack::NamedParams params1;
436 params1["width"] = "34";
437 params1["height"] = "34";
438 params1["xoffset"] = "0";
439 params1["yoffset"] = "0";
441 TraceCallStack::NamedParams params2;
442 params2["width"] = "50";
443 params2["height"] = "50";
444 params2["xoffset"] = "0";
445 params2["yoffset"] = "34";
447 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
448 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
451 callStack.Enable(true);
453 // remove the imageView2 from stage, the second image will also be removed from atlas
454 // then the space on the atlas will be used by the third image added.
455 Stage::GetCurrent().Remove( imageView2 );
456 application.SendNotification();
457 application.Render(RENDER_FRAME_INTERVAL);
459 Property::Map imageMap3;
460 imageMap3[ ImageVisual::Property::URL ] = gImage_128_RGB;
461 imageMap3[ ImageVisual::Property::DESIRED_HEIGHT ] = 100;
462 imageMap3[ ImageVisual::Property::DESIRED_WIDTH ] = 100;
463 imageMap3[ DevelImageVisual::Property::ATLASING ] = true;
465 ImageView imageView3 = ImageView::New();
466 imageView3.SetProperty( ImageView::Property::IMAGE, imageMap3 );
468 application.GetPlatform().SetClosestImageSize( Vector2(100, 100) );
469 Stage::GetCurrent().Add( imageView3 );
471 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
473 application.SendNotification();
474 application.Render(RENDER_FRAME_INTERVAL);
476 callStack.Enable(false);
478 TraceCallStack::NamedParams params3;
479 params3["width"] = "100";
480 params3["height"] = "100";
481 params3["xoffset"] = "0";
482 params3["yoffset"] = "34";
484 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
489 int UtcDaliImageAtlasPackToAtlas(void)
491 ToolkitTestApplication application;
493 std::vector<PixelData> pixelDataContainer;
494 pixelDataContainer.push_back( CreatePixelData( 20, 30 ) );
495 pixelDataContainer.push_back( CreatePixelData( 10, 10 ) );
496 pixelDataContainer.push_back( CreatePixelData( 45, 30 ) );
497 pixelDataContainer.push_back( CreatePixelData( 20, 20 ) );
499 Dali::Vector<Vector4> textureRects;
500 Texture texture = ImageAtlas::PackToAtlas( pixelDataContainer, textureRects );
512 DALI_TEST_EQUALS( texture.GetWidth(), 45, TEST_LOCATION );
513 DALI_TEST_EQUALS( texture.GetHeight(), 60, TEST_LOCATION );
515 Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRects[0], texture.GetWidth(), texture.GetHeight());
516 DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
517 DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
518 DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
519 DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
521 pixelArea = TextureCoordinateToPixelArea(textureRects[1], texture.GetWidth(), texture.GetHeight());
522 DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
523 DALI_TEST_EQUALS( pixelArea.y, 50, TEST_LOCATION );
524 DALI_TEST_EQUALS( pixelArea.width, 10, TEST_LOCATION );
525 DALI_TEST_EQUALS( pixelArea.height, 10, TEST_LOCATION );
527 pixelArea = TextureCoordinateToPixelArea(textureRects[2], texture.GetWidth(), texture.GetHeight());
528 DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
529 DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
530 DALI_TEST_EQUALS( pixelArea.width, 45, TEST_LOCATION );
531 DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
533 pixelArea = TextureCoordinateToPixelArea(textureRects[3], texture.GetWidth(), texture.GetHeight());
534 DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
535 DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
536 DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
537 DALI_TEST_EQUALS( pixelArea.height, 20, TEST_LOCATION );