Dali 3D User Interface Engine
utc-Dali-BufferImage.cpp
Go to the documentation of this file.
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>
23 #include <dali-test-suite-utils.h>
24 
25 using std::max;
26 using namespace Dali;
27 
29 {
31 }
32 
34 {
36 }
37 
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 
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 
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  ImageActor actor = ImageActor::New(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 
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();
118  END_TEST;
119 }
120 
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  ImageActor actor = ImageActor::New(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 
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();
159  END_TEST;
160 }
161 
163 {
164  TestApplication application;
165  tet_infoline("Testing Dali::BufferImage::DownCast()");
166 
168  ImageActor imageActor = ImageActor::New(bitmap);
169  application.SendNotification();
170  application.Render(16);
171  application.Render(16);
172  application.SendNotification();
173 
174  Image image = imageActor.GetImage();
175  BufferImage bufferImage = BufferImage::DownCast( image );
176 
177  DALI_TEST_CHECK(bufferImage);
178  END_TEST;
179 }
180 
182 {
183  TestApplication application;
184  tet_infoline("Testing Dali::BufferImage::DownCast()");
185 
186  Image image = ResourceImage::New("IncorrectImageName");
187  ImageActor imageActor = ImageActor::New(image);
188  application.SendNotification();
189  application.Render(16);
190  application.Render(16);
191  application.SendNotification();
192 
193  Image image1 = imageActor.GetImage();
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 
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 
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 
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 
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 
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 
313  END_TEST;
314 }
315 
316 
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 
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  ImageActor actor = ImageActor::New(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  DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "0, 0, 16, 16"), true, TEST_LOCATION);
388 
389  DALI_TEST_CHECK( SignalReceived == true );
390  SignalReceived = false;
391  END_TEST;
392 }
393 
395 {
396  TestApplication application;
397 
398  tet_infoline("UtcDaliBufferImageUpdate02 - Multiple rects");
399 
400  PixelBuffer* buffer = new PixelBuffer[16 * 16];
401  BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
402  ImageActor actor = ImageActor::New(image);
403  Stage::GetCurrent().Add(actor);
404  actor.SetVisible(true);
405 
406  SignalReceived = false;
407  image.UploadedSignal().Connect( ImageUploaded );
408 
409  application.SendNotification();
410  application.Render(0);
411  application.Render(16);
412  application.SendNotification();
413  application.Render(16);
414  application.SendNotification();
415 
416  DALI_TEST_CHECK( image.IsDataExternal() );
417  application.GetGlAbstraction().EnableTextureCallTrace(true);
418 
419  // Check that multiple updates in a frame will be properly uploaded
420  image.Update(RectArea(9,9,5,5));
421  image.Update(RectArea(2,2,4,4));
422  image.Update(RectArea(3,3,1,6));
423 
424  application.SendNotification();
425  application.Render(16);
426  application.Render(16);
427  application.SendNotification();
428  application.Render(16);
429  application.SendNotification();
430 
431  const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
432  DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", "9, 9, 5, 5"), true, TEST_LOCATION);
433  DALI_TEST_EQUALS( callStack.TestMethodAndParams(1, "TexSubImage2D", "2, 2, 4, 4"), true, TEST_LOCATION);
434  DALI_TEST_EQUALS( callStack.TestMethodAndParams(2, "TexSubImage2D", "3, 3, 1, 6"), true, TEST_LOCATION);
435 
436  DALI_TEST_CHECK( SignalReceived == true );
437  SignalReceived = false;
438  END_TEST;
439 }
440 
442 {
443  TestApplication application;
444 
445  tet_infoline("UtcDaliBufferImageUploadedSignal - Test that Uploaded signal is sent when image is staged");
446 
447  PixelBuffer* buffer = new PixelBuffer[16 * 16];
448  BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
449  SignalReceived = false;
450  image.UploadedSignal().Connect( ImageUploaded );
451 
452  application.SendNotification();
453  application.Render(16);
454  application.Render(16);
455  application.SendNotification();
456 
457  Dali::ImageActor imageActor = ImageActor::New(image);
458  Stage::GetCurrent().Add(imageActor);
459  application.SendNotification();
460  application.Render(16);
461  application.SendNotification();
462  application.Render(16);
463  application.SendNotification();
464  application.Render(16);
465  application.SendNotification();
466 
467  DALI_TEST_CHECK( SignalReceived == true );
468  END_TEST;
469 }
470 
472 {
473  TestApplication application;
474 
475  tet_infoline("UtcDaliBufferImageUploadedSignal - Test that Uploaded signal is sent after Update");
476 
477  PixelBuffer* buffer = new PixelBuffer[16 * 16];
478  BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
479  SignalReceived = false;
480  //ScopedConnection connection =
481  image.UploadedSignal().Connect( ImageUploaded );
482 
483  application.SendNotification();
484  application.Render(16);
485  application.Render(16);
486  application.SendNotification();
487 
488  Dali::ImageActor imageActor = ImageActor::New(image);
489  Stage::GetCurrent().Add(imageActor);
490  application.SendNotification();
491  application.Render(16);
492  application.SendNotification();
493  application.Render(16);
494  application.SendNotification();
495  application.Render(16);
496  application.SendNotification();
497  DALI_TEST_CHECK( SignalReceived == true );
498  SignalReceived = false;
499 
500  image.Update(RectArea()); // notify Core that the whole image has been updated
501  application.SendNotification();
502  application.Render(16);
503  application.SendNotification();
504  application.Render(16);
505  application.SendNotification();
506  application.Render(16);
507  application.SendNotification();
508  DALI_TEST_CHECK( SignalReceived == true );
509  END_TEST;
510 }
Dali Docs Home
Read more about Dali