[dali_1.1.40] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageAtlas.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
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>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 namespace
31 {
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";
38
39 // this is image is not exist, for negative test
40 static const char* gImageNonExist = "non-exist.jpg";
41
42 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
43
44 Rect<int> TextureCoordinateToPixelArea( const Vector4& textureCoordinate, float size )
45 {
46   Vector4 temp = textureCoordinate * size;
47   Rect<int> pixelArea;
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 );
52
53   return pixelArea;
54 }
55
56 bool IsOverlap( Rect<int> rect1, Rect<int> rect2 )
57 {
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;
62 }
63
64 } // anonymous namespace
65
66 void dali_image_atlas_startup(void)
67 {
68   test_return_value = TET_UNDEF;
69 }
70
71 void dali_image_atlas_cleanup(void)
72 {
73   test_return_value = TET_PASS;
74 }
75
76 int UtcDaliImageAtlasNew(void)
77 {
78   ToolkitTestApplication application;
79
80   // invoke default handle constructor
81   ImageAtlas atlas;
82
83   DALI_TEST_CHECK( !atlas );
84
85   // initialise handle
86   atlas = ImageAtlas::New( 32, 32 );
87
88   DALI_TEST_CHECK( atlas );
89   END_TEST;
90 }
91
92 int UtcDaliImageAtlasCopyConstructor(void)
93 {
94   ToolkitTestApplication application;
95
96   ImageAtlas atlas = ImageAtlas::New( 32, 32);
97   ImageAtlas atlasCopy(atlas);
98
99   DALI_TEST_EQUALS( (bool)atlasCopy, true, TEST_LOCATION );
100   END_TEST;
101 }
102
103 int UtcDaliImageAtlasAssignmentOperator(void)
104 {
105   ToolkitTestApplication application;
106
107   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
108
109   ImageAtlas atlas2;
110   DALI_TEST_EQUALS( (bool)atlas2, false, TEST_LOCATION );
111
112   atlas2 = atlas;
113   DALI_TEST_EQUALS( (bool)atlas2, true, TEST_LOCATION );
114
115   END_TEST;
116 }
117
118 int UtcDaliImageAtlasGetAtlas(void)
119 {
120   ToolkitTestApplication application;
121
122   ImageAtlas atlas = ImageAtlas::New( 32, 32 );
123   Image image = atlas.GetAtlas();
124
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 );
129
130   Atlas coreAtlas = Atlas::DownCast( image );
131   DALI_TEST_EQUALS( (bool)coreAtlas, true, TEST_LOCATION );
132
133   END_TEST;
134 }
135
136 int UtcDaliImageAtlasSetBrokenImage(void)
137 {
138   ToolkitTestApplication application;
139   unsigned int size = 200;
140   ImageAtlas atlas = ImageAtlas::New( size, size );
141
142   Vector4 textureRect;
143   atlas.Upload( textureRect, gImageNonExist );
144   DALI_TEST_EQUALS( textureRect, Vector4::ZERO, TEST_LOCATION );
145
146   // Set broken image
147   TestPlatformAbstraction& platform = application.GetPlatform();
148   platform.SetClosestImageSize(Vector2( 34, 34));
149   atlas.SetBrokenImage( gImage_34_RGBA );
150
151   // the non-exit image will be replaced with the broken image
152   platform.SetClosestImageSize(Vector2( 0, 0));
153   atlas.Upload( textureRect, gImageNonExist );
154
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 );
158
159   END_TEST;
160 }
161
162 int UtcDaliImageAtlasUploadP(void)
163 {
164   ToolkitTestApplication application;
165   unsigned int size = 200;
166   ImageAtlas atlas = ImageAtlas::New( size, size );
167
168   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
169   CallbackBase* callback = eventTrigger->GetCallback();
170
171   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
172   callStack.Reset();
173   callStack.Enable(true);
174
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) );
181
182   eventTrigger->WaitingForTrigger( 3 );// waiting until all three images are loaded
183
184   CallbackBase::Execute( *callback );
185
186   application.SendNotification();
187   application.Render(RENDER_FRAME_INTERVAL);
188
189   callStack.Enable(false);
190
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
195   TraceCallStack::NamedParams params;
196   params["width"] = ToString(pixelArea1.width);
197   params["height"] = ToString(pixelArea1.height);
198   params["xoffset"] = ToString(pixelArea1.x);
199   params["yoffset"] = ToString(pixelArea1.y);
200   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ));
201
202   Rect<int> pixelArea2 = TextureCoordinateToPixelArea(textureRect2, size);
203   DALI_TEST_EQUALS( pixelArea2.width, 50, TEST_LOCATION );
204   DALI_TEST_EQUALS( pixelArea2.height, 50, TEST_LOCATION );
205
206   params["width"] = ToString(pixelArea2.width);
207   params["height"] = ToString(pixelArea2.height);
208   params["xoffset"] = ToString(pixelArea2.x);
209   params["yoffset"] = ToString(pixelArea2.y);
210   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
211
212   Rect<int> pixelArea3 = TextureCoordinateToPixelArea(textureRect3, size);
213   DALI_TEST_EQUALS( pixelArea3.width, 128, TEST_LOCATION );
214   DALI_TEST_EQUALS( pixelArea3.height, 128, TEST_LOCATION );
215
216   params["width"] = ToString(pixelArea3.width);
217   params["height"] = ToString(pixelArea3.height);
218   params["xoffset"] = ToString(pixelArea3.x);
219   params["yoffset"] = ToString(pixelArea3.y);
220   DALI_TEST_CHECK( callStack.FindMethodAndParams("TexSubImage2D", params ) );
221
222   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea2) );
223   DALI_TEST_CHECK( ! IsOverlap(pixelArea1, pixelArea3) );
224   DALI_TEST_CHECK( ! IsOverlap(pixelArea2, pixelArea3) );
225
226   END_TEST;
227 }
228
229 int UtcDaliImageAtlasRemove(void)
230 {
231   TestApplication application;
232   unsigned int size = 100;
233   ImageAtlas atlas = ImageAtlas::New( size, size );
234   Vector4 textureRect1;
235   atlas.Upload( textureRect1, gImage_34_RGBA, ImageDimensions(34, 34) );
236
237   atlas.Remove( textureRect1 );
238
239   Vector4 textureRect2;
240   atlas.Upload( textureRect2, gImage_50_RGBA, ImageDimensions(50, 50) );
241
242   // one pixel gap
243   Rect<int> pixelArea = TextureCoordinateToPixelArea(textureRect2, size);
244   DALI_TEST_EQUALS( pixelArea.x, 0, TEST_LOCATION );
245   DALI_TEST_EQUALS( pixelArea.y, 0, TEST_LOCATION );
246
247   END_TEST;
248 }
249
250 int UtcDaliImageAtlasImageView(void)
251 {
252   ToolkitTestApplication application;
253
254   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
255   callStack.Reset();
256   callStack.Enable(true);
257
258   ImageView imageView1 = ImageView::New( gImage_34_RGBA, ImageDimensions(34, 34) );
259   ImageView imageView2 = ImageView::New( gImage_50_RGBA, ImageDimensions(50, 50) );
260   Stage::GetCurrent().Add( imageView1 );
261   Stage::GetCurrent().Add( imageView2 );
262
263   EventThreadCallback* eventTrigger = EventThreadCallback::Get();
264   while( eventTrigger == NULL) // waiting uintil the ImageAtlas is created by ImageAtlasManager
265   {
266     usleep(10);
267     eventTrigger = EventThreadCallback::Get();
268   }
269   CallbackBase* callback = eventTrigger->GetCallback();
270
271   eventTrigger->WaitingForTrigger( 2 );// waiting until both images are loaded
272
273   CallbackBase::Execute( *callback );
274
275   application.SendNotification();
276   application.Render(RENDER_FRAME_INTERVAL);
277
278   callStack.Enable(false);
279
280   TraceCallStack::NamedParams params1;
281   params1["width"] = "34";
282   params1["height"] = "34";
283   params1["xoffset"] = "0";
284   params1["yoffset"] = "0";
285
286   TraceCallStack::NamedParams params2;
287   params2["width"] = "50";
288   params2["height"] = "50";
289   params2["xoffset"] = "0";
290   params2["yoffset"] = "34";
291
292   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params1 ), true, TEST_LOCATION );
293   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params2 ), true, TEST_LOCATION );
294
295   callStack.Reset();
296   callStack.Enable(true);
297
298   // remove the imageView2 from stage, the second image will also be removed from atlas
299   // then the space on the atlas will be used by the third image added.
300   Stage::GetCurrent().Remove( imageView2 );
301   application.SendNotification();
302   application.Render(RENDER_FRAME_INTERVAL);
303   ImageView imageView3 = ImageView::New( gImage_128_RGB, ImageDimensions(100, 100) );
304   Stage::GetCurrent().Add( imageView3 );
305
306   eventTrigger->WaitingForTrigger( 3 ); // waiting for the third image loaded
307   CallbackBase::Execute( *callback );
308
309   application.SendNotification();
310   application.Render(RENDER_FRAME_INTERVAL);
311
312   callStack.Enable(false);
313
314   TraceCallStack::NamedParams params3;
315   params3["width"] = "100";
316   params3["height"] = "100";
317   params3["xoffset"] = "0";
318   params3["yoffset"] = "34";
319
320   DALI_TEST_EQUALS(  callStack.FindMethodAndParams("TexSubImage2D", params3 ), true, TEST_LOCATION );
321
322   END_TEST;
323 }