Returns the Pixel::Format of a BitmapImage's Bitmap
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BitmapImage.cpp
1 /*
2  * Copyright (c) 2014 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 <iostream>
19 #include <algorithm>
20
21 #include <stdlib.h>
22 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
24
25 using std::max;
26 using namespace Dali;
27
28 void utc_dali_bitmap_image_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_bitmap_image_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 int UtcDaliBitmapImageNew01(void)
39 {
40   TestApplication application;
41
42   tet_infoline("UtcDaliBitmapImageNew01 - BitmapImage::New(unsigned int, unsigned int, Pixel::Format)");
43
44   // invoke default handle constructor
45   BitmapImage image;
46
47   // initialise handle
48   image = BitmapImage::New(16, 16);
49   application.SendNotification();
50   application.Render(16);
51   application.Render(16);
52   application.SendNotification();
53
54   ImageAttributes attributes = image.GetAttributes();
55
56   DALI_TEST_CHECK( attributes.GetWidth() == 16);
57   END_TEST;
58 }
59
60 int UtcDaliBitmapImageNew02(void)
61 {
62   TestApplication application;
63
64   tet_infoline("UtcDaliBitmapImageNew02 - BitmapImage::New(PixelBuffer*, unsigned int, unsigned int, Pixel::Format, unsigned int)");
65
66   PixelBuffer* buffer = new PixelBuffer[16 * 16];
67   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
68   application.SendNotification();
69   application.Render(16);
70   application.Render(16);
71   application.SendNotification();
72
73   Dali::ImageAttributes attributes = image.GetAttributes();
74
75   DALI_TEST_CHECK( attributes.GetWidth() == 16);
76
77   delete [] buffer;
78   END_TEST;
79 }
80
81 int UtcDaliBitmapImageNewWithPolicy01(void)
82 {
83   TestApplication application;
84
85   tet_infoline("UtcDaliBitmapImageNewWithPolicy01 - BitmapImage::New(unsigned int, unsigned int, Pixel::Format, LoadPolicy, ReleasePolicy)");
86
87   // Force texture id's
88   std::vector<GLuint> ids;
89   ids.push_back( 23 );
90   application.GetGlAbstraction().SetNextTextureIds( ids );
91
92   // invoke default handle constructor
93   BitmapImage image;
94
95   // initialise handle
96   image = BitmapImage::New(16, 16, Pixel::A8, Image::OnDemand, Image::Unused);
97   application.SendNotification();
98   application.Render(16);
99   application.Render(16);
100   application.SendNotification();
101
102   ImageAttributes attributes = image.GetAttributes();
103   DALI_TEST_CHECK( attributes.GetWidth() == 16);
104   /// @todo: how to test OnDemand? - resource id would be 0 if buffer only allocated on first call to ::GetBuffer()
105
106   ImageActor actor = ImageActor::New(image);
107   Stage::GetCurrent().Add(actor);
108
109   application.SendNotification();
110   application.Render(16);
111   // testing ReleasePolicy::Unused
112   // fake loading image
113   application.Render(16);
114   application.SendNotification();
115
116   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
117
118   // discard texture when actor comes off stage
119   Stage::GetCurrent().Remove(actor);
120   application.Render(16);
121   application.SendNotification();
122   application.Render(16);
123   application.SendNotification();
124   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
125   END_TEST;
126 }
127
128 int UtcDaliBitmapImageNewWithPolicy02(void)
129 {
130   TestApplication application;
131
132   tet_infoline("UtcDaliBitmapImageNewWithPolicy02 - BitmapImage::New(PixelBuffer*, unsigned int, unsigned int, Pixel::Format, unsigned int, ReleasePolicy)");
133
134   // Force texture id's
135   std::vector<GLuint> ids;
136   ids.push_back( 23 );
137   application.GetGlAbstraction().SetNextTextureIds( ids );
138
139   PixelBuffer* buffer = new PixelBuffer[16 * 16];
140   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8, 16, Image::Unused);
141   application.SendNotification();
142   application.Render(16);
143   application.Render(16);
144   application.SendNotification();
145
146   ImageAttributes attributes = image.GetAttributes();
147   DALI_TEST_CHECK( attributes.GetWidth() == 16);
148   /// @todo: how to test OnDemand? - resource id would be 0 if buffer only allocated on first call to ::GetBuffer()
149
150   ImageActor actor = ImageActor::New(image);
151   Stage::GetCurrent().Add(actor);
152
153   application.SendNotification();
154   application.Render(16);
155   // testing ReleasePolicy::Unused
156   // fake loading image
157   application.Render(16);
158   application.SendNotification();
159
160   DALI_TEST_CHECK ( !application.GetGlAbstraction().CheckTextureDeleted(23) );
161
162   // discard texture when actor comes off stage
163   Stage::GetCurrent().Remove(actor);
164   application.Render(16);
165   application.SendNotification();
166   application.Render(16);
167   application.SendNotification();
168   DALI_TEST_CHECK ( application.GetGlAbstraction().CheckTextureDeleted(23) );
169   END_TEST;
170 }
171
172 int UtcDaliBitmapImageDownCast(void)
173 {
174   TestApplication application;
175   tet_infoline("Testing Dali::BitmapImage::DownCast()");
176
177   BitmapImage bitmap = BitmapImage::New(1, 1, Dali::Pixel::BGRA8888);
178   ImageActor imageActor = ImageActor::New(bitmap);
179   application.SendNotification();
180   application.Render(16);
181   application.Render(16);
182   application.SendNotification();
183
184   Image image = imageActor.GetImage();
185   BitmapImage bitmapImage = BitmapImage::DownCast( image );
186
187   DALI_TEST_CHECK(bitmapImage);
188   END_TEST;
189 }
190
191 int UtcDaliBitmapImageDownCast2(void)
192 {
193   TestApplication application;
194   tet_infoline("Testing Dali::BitmapImage::DownCast()");
195
196   Image image = Image::New("IncorrectImageName");
197   ImageActor imageActor = ImageActor::New(image);
198   application.SendNotification();
199   application.Render(16);
200   application.Render(16);
201   application.SendNotification();
202
203   Image image1 = imageActor.GetImage();
204
205   BitmapImage bitmapImage = BitmapImage::DownCast( image1 );
206   DALI_TEST_CHECK(!bitmapImage);
207
208   Actor unInitialzedActor;
209   bitmapImage = BitmapImage::DownCast( unInitialzedActor );
210   DALI_TEST_CHECK(!bitmapImage);
211   END_TEST;
212 }
213
214 int UtcDaliBitmapImageWHITE(void)
215 {
216   TestApplication application;
217
218   tet_infoline("UtcDaliBitmapImageWHITE - BitmapImage::WHITE()");
219
220   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
221   application.SendNotification();
222   application.Render(16);
223   application.Render(16);
224   application.SendNotification();
225
226   Dali::ImageAttributes attributes = image.GetAttributes();
227   PixelBuffer* buffer = image.GetBuffer();
228
229   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
230                    buffer != NULL &&                      // valid buffer
231                    *buffer == 0xff);                       // r component is 255
232   END_TEST;
233 }
234
235 int UtcDaliBitmapImageGetBuffer(void)
236 {
237   TestApplication application;
238
239   tet_infoline("UtcDaliBitmapImageGetBuffer");
240
241   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
242
243   PixelBuffer* buffer = image.GetBuffer();
244   application.SendNotification();
245   application.Render();
246   application.Render();
247   application.SendNotification();
248
249   ImageAttributes attributes = image.GetAttributes();
250   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
251                    buffer != NULL &&                      // valid buffer
252                    *((unsigned int*)buffer) == 0xffffffff); // all component are 255
253   END_TEST;
254 }
255
256 int UtcDaliBitmapImageGetBufferSize(void)
257 {
258   TestApplication application;
259
260   tet_infoline("UtcDaliBitmapImageGetBufferSize");
261
262   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
263   application.SendNotification();
264   application.Render();
265   application.Render();
266   application.SendNotification();
267
268   Dali::ImageAttributes attributes = image.GetAttributes();
269   PixelBuffer* buffer = image.GetBuffer();
270   unsigned int bufferSize = image.GetBufferSize();
271   unsigned int pixelSize = Pixel::GetBytesPerPixel(attributes.GetPixelFormat());
272
273   DALI_TEST_CHECK( attributes.GetWidth() == 1 &&               // 1 pixel wide
274                    buffer != NULL &&                      // valid buffer
275                    bufferSize == pixelSize);              // r component is 255
276   END_TEST;
277 }
278
279 int UtcDaliBitmapImageGetBufferStride(void)
280 {
281   TestApplication application;
282
283   tet_infoline("UtcDaliBitmapImageGetBufferStride");
284
285   BitmapImage image = BitmapImage::WHITE();               // creates a 1x1 RGBA white pixel
286   application.SendNotification();
287   application.Render();
288   application.Render();
289   application.SendNotification();
290
291   Dali::ImageAttributes attributes = image.GetAttributes();
292   unsigned int pixelSize = Pixel::GetBytesPerPixel(attributes.GetPixelFormat());
293   unsigned int bufferStride = image.GetBufferStride();
294   DALI_TEST_CHECK( bufferStride == pixelSize );
295   DALI_TEST_CHECK( !image.IsDataExternal() );
296
297   PixelBuffer* buffer = new PixelBuffer[20 * 16];
298   image = BitmapImage::New(buffer, 16, 16, Pixel::A8, 20);
299   application.SendNotification();
300   application.Render(16);
301   application.Render(16);
302   application.SendNotification();
303
304   bufferStride = image.GetBufferStride();
305
306   DALI_TEST_CHECK( bufferStride == 20);
307   DALI_TEST_CHECK( image.IsDataExternal() );
308
309   delete [] buffer;
310   END_TEST;
311 }
312
313 int UtcDaliBitmapImageGetPixelFormat(void)
314 {
315   TestApplication application;
316
317   tet_infoline("UtcDaliBitmapImageGetPixelFormat");
318
319   // Set pixel format to a non-default
320   BitmapImage image = BitmapImage::New( 16, 16, Pixel::A8 );
321   application.SendNotification();
322   application.Render(16);
323   application.Render(16);
324   application.SendNotification();
325
326   DALI_TEST_CHECK( image.GetPixelFormat() == Pixel::A8 );
327   END_TEST;
328 }
329
330
331 int UtcDaliBitmapImageIsDataExternal(void)
332 {
333   TestApplication application;
334
335   tet_infoline("UtcDaliBitmapImageIsDataExternal - BitmapImage::IsDataExternal()");
336
337   PixelBuffer* buffer = new PixelBuffer[16 * 16];
338   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
339   application.SendNotification();
340   application.Render();
341   application.Render();
342   application.SendNotification();
343
344   DALI_TEST_CHECK( image.IsDataExternal() );
345   END_TEST;
346 }
347
348 namespace
349 {
350
351 static bool SignalReceived;
352 static void ImageUploaded(Image image)
353 {
354   tet_infoline("Received image uploaded signal");
355   SignalReceived = true;
356 }
357 }
358
359 int UtcDaliBitmapImageUpdate01(void)
360 {
361   TestApplication application;
362
363   tet_infoline("UtcDaliBitmapImageUpdate01 - single empty rect");
364
365   PixelBuffer* buffer = new PixelBuffer[16 * 16];
366
367   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
368   ImageActor actor = ImageActor::New(image);
369   Stage::GetCurrent().Add(actor);
370   actor.SetVisible(true);
371
372   SignalReceived = false;
373   image.UploadedSignal().Connect( ImageUploaded );
374
375   std::vector<GLuint> ids;
376   ids.push_back(200);
377   ids.push_back(201);
378   ids.push_back(202);
379   application.GetGlAbstraction().SetNextTextureIds(ids);
380
381   // Allow actor to be staged and rendered
382   application.SendNotification();
383   application.Render(0);
384   application.Render(16);
385   application.SendNotification();
386   application.Render(16);
387   application.SendNotification();
388
389   DALI_TEST_CHECK( image.IsDataExternal() );
390   application.GetGlAbstraction().EnableTextureCallTrace(true);
391
392   image.Update();//(RectArea()); // notify Core that the image has been updated
393   application.SendNotification();
394   application.Render(16);
395   application.Render(16);
396   application.SendNotification();
397   application.Render(16);
398   application.SendNotification();
399
400   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
401   DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "0, 0, 16, 16"), true, TEST_LOCATION);
402
403   DALI_TEST_CHECK( SignalReceived == true );
404   SignalReceived = false;
405   END_TEST;
406 }
407
408 int UtcDaliBitmapImageUpdate02(void)
409 {
410   TestApplication application;
411
412   tet_infoline("UtcDaliBitmapImageUpdate02 - Multiple rects");
413
414   PixelBuffer* buffer = new PixelBuffer[16 * 16];
415   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
416   ImageActor actor = ImageActor::New(image);
417   Stage::GetCurrent().Add(actor);
418   actor.SetVisible(true);
419
420   SignalReceived = false;
421   image.UploadedSignal().Connect( ImageUploaded );
422
423   std::vector<GLuint> ids;
424   ids.push_back(200);
425   ids.push_back(201);
426   ids.push_back(202);
427   application.GetGlAbstraction().SetNextTextureIds(ids);
428
429   application.SendNotification();
430   application.Render(0);
431   application.Render(16);
432   application.SendNotification();
433   application.Render(16);
434   application.SendNotification();
435
436   DALI_TEST_CHECK( image.IsDataExternal() );
437   application.GetGlAbstraction().EnableTextureCallTrace(true);
438
439   image.Update(RectArea(9,9,5,5));              // notify Core that the image has been updated
440
441   application.SendNotification();
442   application.Render(16);
443   application.Render(16);
444   application.SendNotification();
445   application.Render(16);
446   application.SendNotification();
447
448   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
449   DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "9, 9, 5, 1"), true, TEST_LOCATION);
450   DALI_TEST_EQUALS( callStack.TestMethodAndParams(1, "TexSubImage2D", "9, 10, 5, 1"), true, TEST_LOCATION);
451   DALI_TEST_EQUALS( callStack.TestMethodAndParams(2, "TexSubImage2D", "9, 11, 5, 1"), true, TEST_LOCATION);
452   DALI_TEST_EQUALS( callStack.TestMethodAndParams(3, "TexSubImage2D", "9, 12, 5, 1"), true, TEST_LOCATION);
453   DALI_TEST_EQUALS( callStack.TestMethodAndParams(4, "TexSubImage2D", "9, 13, 5, 1"), true, TEST_LOCATION);
454
455   DALI_TEST_CHECK( SignalReceived == true );
456   SignalReceived = false;
457   END_TEST;
458 }
459
460 int UtcDaliBitmapImageUploadedSignal01(void)
461 {
462   TestApplication application;
463
464   tet_infoline("UtcDaliBitmapImageUploadedSignal - Test that Uploaded signal is sent when image is staged");
465
466   PixelBuffer* buffer = new PixelBuffer[16 * 16];
467   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
468   SignalReceived = false;
469   image.UploadedSignal().Connect( ImageUploaded );
470
471   application.SendNotification();
472   application.Render(16);
473   application.Render(16);
474   application.SendNotification();
475
476   Dali::ImageActor imageActor = ImageActor::New(image);
477   Stage::GetCurrent().Add(imageActor);
478   application.SendNotification();
479   application.Render(16);
480   application.SendNotification();
481   application.Render(16);
482   application.SendNotification();
483   application.Render(16);
484   application.SendNotification();
485
486   DALI_TEST_CHECK( SignalReceived == true );
487   END_TEST;
488 }
489
490 int UtcDaliBitmapImageUploadedSignal02(void)
491 {
492   TestApplication application;
493
494   tet_infoline("UtcDaliBitmapImageUploadedSignal - Test that Uploaded signal is sent after Update");
495
496   PixelBuffer* buffer = new PixelBuffer[16 * 16];
497   BitmapImage image = BitmapImage::New(buffer, 16, 16, Pixel::A8);
498   SignalReceived = false;
499   //ScopedConnection connection =
500   image.UploadedSignal().Connect( ImageUploaded );
501
502   application.SendNotification();
503   application.Render(16);
504   application.Render(16);
505   application.SendNotification();
506
507   Dali::ImageActor imageActor = ImageActor::New(image);
508   Stage::GetCurrent().Add(imageActor);
509   application.SendNotification();
510   application.Render(16);
511   application.SendNotification();
512   application.Render(16);
513   application.SendNotification();
514   application.Render(16);
515   application.SendNotification();
516   DALI_TEST_CHECK( SignalReceived == true );
517   SignalReceived = false;
518
519   image.Update(RectArea());              // notify Core that the whole image has been updated
520   application.SendNotification();
521   application.Render(16);
522   application.SendNotification();
523   application.Render(16);
524   application.SendNotification();
525   application.Render(16);
526   application.SendNotification();
527   DALI_TEST_CHECK( SignalReceived == true );
528   END_TEST;
529 }