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/devel-api/images/atlas.h>
24 #include <dali-toolkit/devel-api/image-atlas/image-atlas.h>
25 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
28 using namespace Dali::Toolkit;
32 // resolution: 34*34, pixel format: RGBA8888
33 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
34 // resolution: 50*50, pixel format: RGBA8888
35 static const char* gImage_50_RGBA = TEST_RESOURCE_DIR "/icon-delete.png";
36 // resolution: 128*128, pixel format: RGB888
37 static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
39 // this is image is not exist, for negative test
40 static const char* gImageNonExist = "non-exist.jpg";
42 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
44 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
46 Vector4 temp = textureCoordinate * size;
48 pixelArea.x = static_cast<int>( temp.x );
49 pixelArea.y = static_cast<int>( temp.y );
50 pixelArea.width = static_cast<int>( temp.z-temp.x+1.f );
51 pixelArea.height = static_cast<int>( temp.w-temp.y+1.f );
56 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
58 return rect1.x < rect2.x+rect2.width
59 && rect2.x < rect1.x+rect1.width
60 && rect1.y < rect2.y+rect2.height
61 && rect2.y < rect1.y+rect1.height;
66 void dali_image_atlas_startup(void)
68 test_return_value = TET_UNDEF;
71 void dali_image_atlas_cleanup(void)
73 test_return_value = TET_PASS;
76 int UtcDaliImageAtlasNew(void)
78 ToolkitTestApplication application;
80 // invoke default handle constructor
83 DALI_TEST_CHECK( !atlas );
86 atlas = ImageAtlas::New( 32, 32 );
88 DALI_TEST_CHECK( atlas );
92 int UtcDaliImageAtlasCopyConstructor(void)
94 ToolkitTestApplication application;
96 ImageAtlas atlas = ImageAtlas::New( 32, 32);
97 ImageAtlas atlasCopy(atlas);
99 DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
103 int UtcDaliImageAtlasAssignmentOperator(void)
105 ToolkitTestApplication application;
107 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
110 DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
113 DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
118 int UtcDaliImageAtlasGetAtlas(void)
120 ToolkitTestApplication application;
122 ImageAtlas atlas = ImageAtlas::New( 32, 32 );
123 Image image = atlas.GetAtlas();
125 // test the atlas created
126 DALI_TEST_EQUALS( (bool)image, true, TEST_LOCATION );
127 DALI_TEST_CHECK( image.GetHeight() == 32u );
128 DALI_TEST_CHECK( image.GetWidth() == 32u );
130 Atlas coreAtlas = Atlas::DownCast( image );
131 DALI_TEST_EQUALS( (bool)coreAtlas, true, TEST_LOCATION );
136 int UtcDaliImageAtlasSetBrokenImage(void)
138 ToolkitTestApplication application;
139 unsigned int size = 200;
140 ImageAtlas atlas = ImageAtlas::New( size, size );
143 atlas.Upload( textureRect, gImageNonExist );
144 DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
147 TestPlatformAbstraction& platform = application.GetPlatform();
148 platform.SetClosestImageSize(Vector2( 34, 34));
149 atlas.SetBrokenImage( gImage_34_RGBA );
151 // the non-exit image will be replaced with the broken image
152 platform.SetClosestImageSize(Vector2( 0, 0));
153 atlas.Upload( textureRect, gImageNonExist );
155 Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect, size);
156 DALI_TEST_EQUALS( pixelArea.width, 34, TEST_LOCATION );
157 DALI_TEST_EQUALS( pixelArea.height, 34, TEST_LOCATION );
162 int UtcDaliImageAtlasUploadP(void)
164 ToolkitTestApplication application;
165 unsigned int size = 200;
166 ImageAtlas atlas = ImageAtlas::New( size, size );
168 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
169 CallbackBase* callback = eventTrigger->GetCallback();
171 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
173 callStack.Enable(true);
175 Vector4 textureRect1;
176 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
177 Vector4 textureRect2;
178 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
179 Vector4 textureRect3;
180 atlas.Upload( textureRect3, gImage_128_RGB, ImageDimensions(128, 128) );
182 eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
184 CallbackBase::Execute( *callback );
186 application.SendNotification();
187 application.Render(RENDER_FRAME_INTERVAL);
189 callStack.Enable(false);
191 Rect<int> pixelArea1 = TextureCoordinateToPixelArea(textureRect1, size);
192 DALI_TEST_EQUALS( pixelArea1.width, 34, TEST_LOCATION );
193 DALI_TEST_EQUALS( pixelArea1.height, 34, TEST_LOCATION );
194 std::stringstream out;
195 out<<pixelArea1.x<<", "<<pixelArea1.y<<", "<<pixelArea1.width<<", "<<pixelArea1.height;
196 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str()) );
198 Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
199 DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
200 DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
202 out<<pixelArea2.x<<", "<<pixelArea2.y<<", "<<pixelArea2.width<<", "<<pixelArea2.height;
203 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str()) );
205 Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
206 DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
207 DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
209 out<<pixelArea3.x<<", "<<pixelArea3.y<<", "<<pixelArea3.width<<", "<<pixelArea3.height;
210 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", out.str()) );
212 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
213 DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
214 DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
219 int UtcDaliImageAtlasRemove(void)
221 TestApplication application;
222 unsigned int size = 100;
223 ImageAtlas atlas = ImageAtlas::New( size, size );
224 Vector4 textureRect1;
225 atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
227 atlas.Remove( textureRect1 );
229 Vector4 textureRect2;
230 atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
233 Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
234 DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
235 DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
240 int UtcDaliImageAtlasImageView(void)
242 ToolkitTestApplication application;
244 TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
246 callStack.Enable(true);
248 ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
249 ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
250 Stage::GetCurrent().Add( imageView1 );
251 Stage::GetCurrent().Add( imageView2 );
253 EventThreadCallback* eventTrigger = EventThreadCallback::Get();
254 while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
257 eventTrigger = EventThreadCallback::Get();
259 CallbackBase* callback = eventTrigger->GetCallback();
261 eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
263 CallbackBase::Execute( *callback );
265 application.SendNotification();
266 application.Render(RENDER_FRAME_INTERVAL);
268 callStack.Enable(false);
270 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 0, 34, 34" ) );
271 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 34, 50, 50" ) );
274 callStack.Enable(true);
276 // remove the imageView2 from stage, the second image will also be removed from atlas
277 // then the space on the atlas will be used by the third image added.
278 Stage::GetCurrent().Remove( imageView2 );
279 application.SendNotification();
280 application.Render(RENDER_FRAME_INTERVAL);
281 ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
282 Stage::GetCurrent().Add( imageView3 );
284 eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
285 CallbackBase::Execute( *callback );
287 application.SendNotification();
288 application.Render(RENDER_FRAME_INTERVAL);
290 callStack.Enable(false);
291 DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", "0, 34, 100, 100" ) );