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