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 } // anonymous namespace
65 void dali_image_atlas_startup(void)
67 test_return_value = TET_UNDEF;
70 void dali_image_atlas_cleanup(void)
72 test_return_value = TET_PASS;
75 int UtcDaliImageAtlasNew(void)
77 ToolkitTestApplication application;
79 // invoke default handle constructor
82 DALI_TEST_CHECK( !atlas );
85 atlas = ImageAtlas::New( 32, 32 );
87 DALI_TEST_CHECK( atlas );
91 int UtcDaliImageAtlasCopyConstructor(void)
93 ToolkitTestApplication application;
95 ImageAtlas atlas = ImageAtlas::New( 32, 32);
96 ImageAtlas atlasCopy(atlas);
98 DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
102 int UtcDaliImageAtlasAssignmentOperator(void)
104 ToolkitTestApplication application;
106 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
109 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
112 DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
117 int UtcDaliImageAtlasGetAtlas(void)
119 ToolkitTestApplication application;
121 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
122 Texture image = atlas.GetAtlas();
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 );
132 int UtcDaliImageAtlasGetOccupancyRate(void)
134 ToolkitTestApplication application;
136 ImageAtlas atlas = ImageAtlas::New( 100, 100 );
138 DALI_TEST_EQUALS( atlas.GetOccupancyRate(), 0.f, TEST_LOCATION );
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 );
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 );
151 int UtcDaliImageAtlasSetBrokenImage(void)
153 ToolkitTestApplication application;
154 unsigned int size = 200;
155 ImageAtlas atlas = ImageAtlas::New( size, size );
158 atlas.Upload( textureRect, gImageNonExist );
159 DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
162 TestPlatformAbstraction& platform = application.GetPlatform();
163 platform.SetClosestImageSize(Vector2( 34, 34));
164 atlas.SetBrokenImage( gImage_34_RGBA );
166 // the non-exit image will be replaced with the broken image
167 platform.SetClosestImageSize(Vector2( 0, 0));
168 atlas.Upload( textureRect, gImageNonExist );
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 );
177 int UtcDaliImageAtlasUploadP(void)
179 ToolkitTestApplication application;
180 unsigned int size = 200;
181 ImageAtlas atlas = ImageAtlas::New( size, size );
183 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
185 callStack.Enable(true);
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) );
194 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
195 CallbackBase* callback = eventTrigger->GetCallback();
197 eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
199 CallbackBase::Execute( *callback );
201 application.SendNotification();
202 application.Render(RENDER_FRAME_INTERVAL);
204 callStack.Enable(false);
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 );
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 ));
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 );
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 ) );
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 );
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 ) );
237 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
238 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
239 DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
244 int UtcDaliImageAtlasRemove(void)
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) );
252 atlas.Remove( textureRect1 );
254 Vector4 textureRect2;
255 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
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 );
265 int UtcDaliImageAtlasImageView(void)
267 ToolkitTestApplication application;
269 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
271 callStack.Enable(true);
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 );
278 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
279 while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
282 eventTrigger = EventThreadCallback::Get();
284 CallbackBase* callback = eventTrigger->GetCallback();
286 eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
288 CallbackBase::Execute( *callback );
290 application.SendNotification();
291 application.Render(RENDER_FRAME_INTERVAL);
293 callStack.Enable(false);
295 TraceCallStack::NamedParams params1;
296 params1["width"] = "34";
297 params1["height"] = "34";
298 params1["xoffset"] = "0";
299 params1["yoffset"] = "0";
301 TraceCallStack::NamedParams params2;
302 params2["width"] = "50";
303 params2["height"] = "50";
304 params2["xoffset"] = "0";
305 params2["yoffset"] = "34";
307 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
308 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
311 callStack.Enable(true);
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 );
321 eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
322 CallbackBase::Execute( *callback );
324 application.SendNotification();
325 application.Render(RENDER_FRAME_INTERVAL);
327 callStack.Enable(false);
329 TraceCallStack::NamedParams params3;
330 params3["width"] = "100";
331 params3["height"] = "100";
332 params3["xoffset"] = "0";
333 params3["yoffset"] = "34";
335 DALI_TEST_EQUALS( callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );