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 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
212 application.SendNotification();
213 application.Render(RENDER_FRAME_INTERVAL);
215 callStack.Enable(false);
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 );
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 ));
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 );
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 ) );
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 );
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 ) );
248 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
249 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
250 DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
255 int UtcDaliImageAtlasUploadWithObserver01(void)
257 TestApplication application;
258 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
261 gCountOfTestFuncCall = 0;
262 TestUploadObserver uploadObserver;
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 );
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);
276 // Check that TestFunc is called twice
277 DALI_TEST_EQUALS( gCountOfTestFuncCall, 2, TEST_LOCATION );
282 int UtcDaliImageAtlasUploadWithObserver02(void)
284 TestApplication application;
285 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
287 gCountOfTestFuncCall = 0;
288 TestUploadObserver* uploadObserver = new TestUploadObserver;
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 );
297 // destroy the object.
298 delete uploadObserver;
300 // waiting until all three images are loaded and uploaded to atlas
301 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
303 application.Render(RENDER_FRAME_INTERVAL);
304 application.SendNotification();
306 // Check that TestFunc is called twice
307 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
312 int UtcDaliImageAtlasUploadWithObserver03(void)
314 TestApplication application;
316 gCountOfTestFuncCall = 0;
317 TestUploadObserver* uploadObserver = new TestUploadObserver;
320 ImageAtlas atlas = ImageAtlas::New( 200, 200 );
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 );
330 //ImageAtlas is out of scope, so it will get destroyed
332 application.Render(RENDER_FRAME_INTERVAL);
333 application.SendNotification();
334 application.SendNotification();
335 application.Render(RENDER_FRAME_INTERVAL);
337 // Check that TestFunc is called twice
338 DALI_TEST_EQUALS( gCountOfTestFuncCall, 0, TEST_LOCATION );
343 int UtcDaliImageAtlasRemove(void)
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) );
351 atlas.Remove( textureRect1 );
353 Vector4 textureRect2;
354 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
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 );
364 int UtcDaliImageAtlasImageView(void)
366 ToolkitTestApplication application;
368 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
370 callStack.Enable(true);
372 ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
373 ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
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 );
381 application.GetPlatform().SetClosestImageSize( Vector2(34, 34) );
382 Stage::GetCurrent().Add( imageView1 );
383 application.GetPlatform().SetClosestImageSize( Vector2(50, 50) );
384 Stage::GetCurrent().Add( imageView2 );
386 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 2 ), true, TEST_LOCATION );
388 application.SendNotification();
389 application.Render(RENDER_FRAME_INTERVAL);
391 callStack.Enable(false);
393 TraceCallStack::NamedParams params1;
394 params1["width"] = "34";
395 params1["height"] = "34";
396 params1["xoffset"] = "0";
397 params1["yoffset"] = "0";
399 TraceCallStack::NamedParams params2;
400 params2["width"] = "50";
401 params2["height"] = "50";
402 params2["xoffset"] = "0";
403 params2["yoffset"] = "34";
405 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
406 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
409 callStack.Enable(true);
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 );
420 DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 3 ), true, TEST_LOCATION );
422 application.SendNotification();
423 application.Render(RENDER_FRAME_INTERVAL);
425 callStack.Enable(false);
427 TraceCallStack::NamedParams params3;
428 params3["width"] = "100";
429 params3["height"] = "100";
430 params3["xoffset"] = "0";
431 params3["yoffset"] = "34";
433 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );