(ImageAtlas) Add method for packing a group of pixelData into atlas
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageAtlas.cpp
index 5fe744f..17b4432 100644 (file)
@@ -40,14 +40,35 @@ static const char* gImageNonExist = "non-exist.jpg";
 
 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
 
+PixelData CreatePixelData( unsigned int width, unsigned int height )
+{
+  unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::RGBA8888 );
+
+  unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
+  PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::FREE );
+
+  return pixelData;
+}
+
 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
 {
   Vector4 temp = textureCoordinate * size;
   Rect<int> pixelArea;
   pixelArea.x = static_cast<int>( temp.x );
   pixelArea.y = static_cast<int>( temp.y );
-  pixelArea.width = static_cast<int>( temp.z-temp.x+1.f );
-  pixelArea.height = static_cast<int>( temp.w-temp.y+1.f );
+  pixelArea.width = static_cast<int>( temp.z-temp.x+1.01f );
+  pixelArea.height = static_cast<int>( temp.w-temp.y+1.01f );
+
+  return pixelArea;
+}
+
+Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float width, float height )
+{
+  Rect<int> pixelArea;
+  pixelArea.x = static_cast<int>( textureCoordinate.x*width );
+  pixelArea.y = static_cast<int>( textureCoordinate.y*height);
+  pixelArea.width = static_cast<int>( (textureCoordinate.z-textureCoordinate.x)*width+1.01f );
+  pixelArea.height = static_cast<int>( (textureCoordinate.w-textureCoordinate.y)*height+1.01f );
 
   return pixelArea;
 }
@@ -207,12 +228,7 @@ int UtcDaliImageAtlasUploadP(void)
   Vector4 textureRect3;
   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  CallbackBase* callback = eventTrigger->GetCallback();
-
-  eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
-
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
 
   application.SendNotification();
   application.Render(RENDER_FRAME_INTERVAL);
@@ -262,8 +278,6 @@ int UtcDaliImageAtlasUploadWithObserver01(void)
   TestApplication application;
   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  CallbackBase* callback = eventTrigger->GetCallback();
 
   gCountOfTestFuncCall = 0;
   TestUploadObserver uploadObserver;
@@ -276,8 +290,7 @@ int UtcDaliImageAtlasUploadWithObserver01(void)
   atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128), FittingMode::DEFAULT, true, &uploadObserver );
 
   // waiting until all three images are loaded and uploaded to atlas
-  eventTrigger->WaitingForTrigger( 3 );
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
   application.SendNotification();
   application.Render(RENDER_FRAME_INTERVAL);
 
@@ -292,9 +305,6 @@ int UtcDaliImageAtlasUploadWithObserver02(void)
   TestApplication application;
   ImageAtlas atlas = ImageAtlas::New( 200, 200 );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  CallbackBase* callback = eventTrigger->GetCallback();
-
   gCountOfTestFuncCall = 0;
   TestUploadObserver* uploadObserver = new TestUploadObserver;
 
@@ -309,8 +319,8 @@ int UtcDaliImageAtlasUploadWithObserver02(void)
   delete uploadObserver;
 
  // waiting until all three images are loaded and uploaded to atlas
-  eventTrigger->WaitingForTrigger( 3 );
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
+
   application.Render(RENDER_FRAME_INTERVAL);
   application.SendNotification();
 
@@ -382,20 +392,19 @@ int UtcDaliImageAtlasImageView(void)
 
   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
-  Stage::GetCurrent().Add( imageView1 );
-  Stage::GetCurrent().Add( imageView2 );
 
-  EventThreadCallback* eventTrigger = EventThreadCallback::Get();
-  while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
-  {
-    usleep(10);
-    eventTrigger = EventThreadCallback::Get();
-  }
-  CallbackBase* callback = eventTrigger->GetCallback();
+  // ImageView doesn't do size negotiation properly: it only listens to OnSizeSet:
+  imageView1.SetSize( 100, 100 );
+  imageView2.SetSize( 100, 100 );
+  imageView1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+  imageView2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
 
-  eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
+  application.GetPlatform().SetClosestImageSize(  Vector2(34, 34) );
+  Stage::GetCurrent().Add( imageView1 );
+  application.GetPlatform().SetClosestImageSize(  Vector2(50, 50) );
+  Stage::GetCurrent().Add( imageView2 );
 
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
 
   application.SendNotification();
   application.Render(RENDER_FRAME_INTERVAL);
@@ -426,10 +435,10 @@ int UtcDaliImageAtlasImageView(void)
   application.SendNotification();
   application.Render(RENDER_FRAME_INTERVAL);
   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
+  application.GetPlatform().SetClosestImageSize(  Vector2(100, 100) );
   Stage::GetCurrent().Add( imageView3 );
 
-  eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
-  CallbackBase::Execute( *callback );
+  DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
 
   application.SendNotification();
   application.Render(RENDER_FRAME_INTERVAL);
@@ -446,3 +455,56 @@ int UtcDaliImageAtlasImageView(void)
 
   END_TEST;
 }
+
+int UtcDaliImageAtlasPackToAtlas(void)
+{
+  ToolkitTestApplication application;
+
+  std::vector<PixelData> pixelDataContainer;
+  pixelDataContainer.push_back( CreatePixelData( 20, 30 ) );
+  pixelDataContainer.push_back( CreatePixelData( 10, 10 ) );
+  pixelDataContainer.push_back( CreatePixelData( 45, 30 ) );
+  pixelDataContainer.push_back( CreatePixelData( 20, 20 ) );
+
+  Dali::Vector<Vector4> textureRects;
+  Texture texture = ImageAtlas::PackToAtlas( pixelDataContainer, textureRects  );
+
+ // --------------
+ // |            |
+ // |    45*30   |
+//  |            |
+//  --------------
+//  | 20 |    | 20*20
+//  |  * |____|
+//  | 30 |  |  10*10
+//  --------
+
+  DALI_TEST_EQUALS( texture.GetWidth(), 45, TEST_LOCATION );
+  DALI_TEST_EQUALS( texture.GetHeight(), 60, TEST_LOCATION );
+
+  Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRects[0], texture.GetWidth(), texture.GetHeight());
+  DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
+
+  pixelArea = TextureCoordinateToPixelArea(textureRects[1], texture.GetWidth(), texture.GetHeight());
+  DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.y, 50, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.width, 10, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.height, 10, TEST_LOCATION );
+
+  pixelArea = TextureCoordinateToPixelArea(textureRects[2], texture.GetWidth(), texture.GetHeight());
+  DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.width, 45, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.height, 30, TEST_LOCATION );
+
+  pixelArea = TextureCoordinateToPixelArea(textureRects[3], texture.GetWidth(), texture.GetHeight());
+  DALI_TEST_EQUALS( pixelArea.x, 20, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.y, 30, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.width, 20, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelArea.height, 20, TEST_LOCATION );
+
+  END_TEST;
+}