[3.0] Remove/move experimental features
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BufferImage.cpp
1 /*
2  * Copyright (c) 2016 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 UtcDaliBufferImageDownCast(void)
78 {
79   TestApplication application;
80   tet_infoline("Testing Dali::BufferImage::DownCast()");
81
82   BufferImage bitmap = BufferImage::New(1, 1, Dali::Pixel::BGRA8888);
83   Actor actor = CreateRenderableActor( bitmap );
84   application.SendNotification();
85   application.Render(16);
86   application.Render(16);
87   application.SendNotification();
88
89   Image image = GetTexture( actor );
90   BufferImage bufferImage = BufferImage::DownCast( image );
91
92   DALI_TEST_CHECK(bufferImage);
93   END_TEST;
94 }
95
96 int UtcDaliBufferImageDownCast2(void)
97 {
98   TestApplication application;
99   tet_infoline("Testing Dali::BufferImage::DownCast()");
100
101   Image image = ResourceImage::New("IncorrectImageName");
102   Actor actor = CreateRenderableActor( image );
103   application.SendNotification();
104   application.Render(16);
105   application.Render(16);
106   application.SendNotification();
107
108   Image image1 = GetTexture( actor );
109
110   BufferImage bufferImage = BufferImage::DownCast( image1 );
111   DALI_TEST_CHECK(!bufferImage);
112
113   Actor unInitialzedActor;
114   bufferImage = BufferImage::DownCast( unInitialzedActor );
115   DALI_TEST_CHECK(!bufferImage);
116   END_TEST;
117 }
118
119 int UtcDaliBufferImageWHITE(void)
120 {
121   TestApplication application;
122
123   tet_infoline("UtcDaliBufferImageWHITE - BufferImage::WHITE()");
124
125   BufferImage image = BufferImage::WHITE();               // creates a 1x1 RGBA white pixel
126   application.SendNotification();
127   application.Render(16);
128   application.Render(16);
129   application.SendNotification();
130
131   PixelBuffer* buffer = image.GetBuffer();
132
133   DALI_TEST_CHECK( image.GetWidth() == 1 &&               // 1 pixel wide
134                    buffer != NULL &&                      // valid buffer
135                    *buffer == 0xff);                       // r component is 255
136   END_TEST;
137 }
138
139 int UtcDaliBufferImageGetBuffer(void)
140 {
141   TestApplication application;
142
143   tet_infoline("UtcDaliBufferImageGetBuffer");
144
145   BufferImage image = BufferImage::WHITE();               // creates a 1x1 RGBA white pixel
146
147   PixelBuffer* buffer = image.GetBuffer();
148   application.SendNotification();
149   application.Render();
150   application.Render();
151   application.SendNotification();
152
153   DALI_TEST_CHECK( image.GetWidth() == 1 &&               // 1 pixel wide
154                    buffer != NULL &&                      // valid buffer
155                    *((unsigned int*)buffer) == 0xffffffff); // all component are 255
156   END_TEST;
157 }
158
159 int UtcDaliBufferImageGetBufferSize(void)
160 {
161   TestApplication application;
162
163   tet_infoline("UtcDaliBufferImageGetBufferSize");
164
165   BufferImage image = BufferImage::WHITE();               // creates a 1x1 RGBA white pixel
166   application.SendNotification();
167   application.Render();
168   application.Render();
169   application.SendNotification();
170
171   PixelBuffer* buffer = image.GetBuffer();
172   unsigned int bufferSize = image.GetBufferSize();
173   unsigned int pixelSize = Pixel::GetBytesPerPixel(image.GetPixelFormat());
174
175   DALI_TEST_CHECK( image.GetWidth() == 1 &&               // 1 pixel wide
176                    buffer != NULL &&                      // valid buffer
177                    bufferSize == pixelSize);              // r component is 255
178   END_TEST;
179 }
180
181 int UtcDaliBufferImageGetBufferStride(void)
182 {
183   TestApplication application;
184
185   tet_infoline("UtcDaliBufferImageGetBufferStride");
186
187   BufferImage image = BufferImage::WHITE();               // creates a 1x1 RGBA white pixel
188   application.SendNotification();
189   application.Render();
190   application.Render();
191   application.SendNotification();
192
193   unsigned int pixelSize = Pixel::GetBytesPerPixel(image.GetPixelFormat());
194   unsigned int bufferStride = image.GetBufferStride();
195   DALI_TEST_CHECK( bufferStride == pixelSize );
196   DALI_TEST_CHECK( !image.IsDataExternal() );
197
198   PixelBuffer* buffer = new PixelBuffer[20 * 16];
199   image = BufferImage::New(buffer, 16, 16, Pixel::A8, 20);
200   application.SendNotification();
201   application.Render(16);
202   application.Render(16);
203   application.SendNotification();
204
205   bufferStride = image.GetBufferStride();
206
207   DALI_TEST_CHECK( bufferStride == 20);
208   DALI_TEST_CHECK( image.IsDataExternal() );
209
210   delete [] buffer;
211   END_TEST;
212 }
213
214 int UtcDaliBufferImageGetPixelFormat(void)
215 {
216   TestApplication application;
217
218   tet_infoline("UtcDaliBufferImageGetPixelFormat");
219
220   // Set pixel format to a non-default
221   BufferImage image = BufferImage::New( 16, 16, Pixel::A8 );
222   application.SendNotification();
223   application.Render(16);
224   application.Render(16);
225   application.SendNotification();
226
227   DALI_TEST_CHECK( image.GetPixelFormat() == Pixel::A8 );
228   END_TEST;
229 }
230
231
232 int UtcDaliBufferImageIsDataExternal(void)
233 {
234   TestApplication application;
235
236   tet_infoline("UtcDaliBufferImageIsDataExternal - BufferImage::IsDataExternal()");
237
238   PixelBuffer* buffer = new PixelBuffer[16 * 16];
239   BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
240   application.SendNotification();
241   application.Render();
242   application.Render();
243   application.SendNotification();
244
245   DALI_TEST_CHECK( image.IsDataExternal() );
246   END_TEST;
247 }
248
249 namespace
250 {
251
252 static bool SignalReceived;
253 static void ImageUploaded(Image image)
254 {
255   tet_infoline("Received image uploaded signal");
256   SignalReceived = true;
257 }
258 }
259
260 int UtcDaliBufferImageUpdate01(void)
261 {
262   TestApplication application;
263
264   tet_infoline("UtcDaliBufferImageUpdate01 - single empty rect");
265
266   PixelBuffer* buffer = new PixelBuffer[16 * 16];
267
268   BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
269   Actor actor = CreateRenderableActor( image );
270   Stage::GetCurrent().Add(actor);
271   actor.SetVisible(true);
272
273   SignalReceived = false;
274   image.UploadedSignal().Connect( ImageUploaded );
275
276   std::vector<GLuint> ids;
277   ids.push_back(200);
278   ids.push_back(201);
279   ids.push_back(202);
280   application.GetGlAbstraction().SetNextTextureIds(ids);
281
282   // Allow actor to be staged and rendered
283   application.SendNotification();
284   application.Render(0);
285   application.Render(16);
286   application.SendNotification();
287   application.Render(16);
288   application.SendNotification();
289
290   DALI_TEST_CHECK( image.IsDataExternal() );
291   application.GetGlAbstraction().EnableTextureCallTrace(true);
292
293   image.Update();//(RectArea()); // notify Core that the image has been updated
294   application.SendNotification();
295   application.Render(16);
296   application.Render(16);
297   application.SendNotification();
298   application.Render(16);
299   application.SendNotification();
300
301   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
302
303   std::stringstream out;
304   out << GL_TEXTURE_2D <<", "<< 0u << ", " << 0u << ", " << 0u << ", " << 16u <<", "<< 16u;
305   DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", out.str().c_str() ), true, TEST_LOCATION);
306
307   DALI_TEST_CHECK( SignalReceived == true );
308   SignalReceived = false;
309   END_TEST;
310 }
311
312 int UtcDaliBufferImageUpdate02(void)
313 {
314   TestApplication application;
315
316   tet_infoline("UtcDaliBufferImageUpdate02 - Multiple rects");
317
318   PixelBuffer* buffer = new PixelBuffer[16 * 16];
319   BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
320   Actor actor = CreateRenderableActor( image );
321   Stage::GetCurrent().Add(actor);
322   actor.SetVisible(true);
323
324   SignalReceived = false;
325   image.UploadedSignal().Connect( ImageUploaded );
326
327   application.SendNotification();
328   application.Render(0);
329   application.Render(16);
330   application.SendNotification();
331   application.Render(16);
332   application.SendNotification();
333
334   DALI_TEST_CHECK( image.IsDataExternal() );
335   application.GetGlAbstraction().EnableTextureCallTrace(true);
336
337   // Check that multiple updates in a frame will be properly uploaded
338   image.Update(RectArea(9,9,5,5));
339   image.Update(RectArea(2,2,4,4));
340   image.Update(RectArea(3,3,1,6));
341
342   application.SendNotification();
343   application.Render(16);
344   application.Render(16);
345   application.SendNotification();
346   application.Render(16);
347   application.SendNotification();
348
349   const TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
350
351   {
352     std::stringstream out;
353     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 9u << ", " << 9u << ", " << 5u <<", "<< 5u;
354     DALI_TEST_EQUALS( callStack.TestMethodAndParams(0, "TexSubImage2D", out.str().c_str()), true, TEST_LOCATION);
355   }
356
357   {
358     std::stringstream out;
359     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 2u << ", " << 2u << ", " << 4u <<", "<< 4u;
360     DALI_TEST_EQUALS( callStack.TestMethodAndParams(1, "TexSubImage2D", out.str().c_str()), true, TEST_LOCATION);
361   }
362
363   {
364     std::stringstream out;
365     out << GL_TEXTURE_2D <<", "<< 0u << ", " << 3u << ", " << 3u << ", " << 1u <<", "<< 6u;
366     DALI_TEST_EQUALS( callStack.TestMethodAndParams(2, "TexSubImage2D", out.str().c_str()), true, TEST_LOCATION);
367   }
368
369   DALI_TEST_CHECK( SignalReceived == true );
370   SignalReceived = false;
371   END_TEST;
372 }
373
374 int UtcDaliBufferImageUploadedSignal01(void)
375 {
376   TestApplication application;
377
378   tet_infoline("UtcDaliBufferImageUploadedSignal - Test that Uploaded signal is sent when image is staged");
379
380   PixelBuffer* buffer = new PixelBuffer[16 * 16];
381   BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
382   SignalReceived = false;
383   image.UploadedSignal().Connect( ImageUploaded );
384
385   application.SendNotification();
386   application.Render(16);
387   application.Render(16);
388   application.SendNotification();
389
390   Actor actor = CreateRenderableActor( image );
391   Stage::GetCurrent().Add(actor);
392   application.SendNotification();
393   application.Render(16);
394   application.SendNotification();
395   application.Render(16);
396   application.SendNotification();
397   application.Render(16);
398   application.SendNotification();
399
400   DALI_TEST_CHECK( SignalReceived == true );
401   END_TEST;
402 }
403
404 int UtcDaliBufferImageUploadedSignal02(void)
405 {
406   TestApplication application;
407
408   tet_infoline("UtcDaliBufferImageUploadedSignal - Test that Uploaded signal is sent after Update");
409
410   PixelBuffer* buffer = new PixelBuffer[16 * 16];
411   BufferImage image = BufferImage::New(buffer, 16, 16, Pixel::A8);
412   SignalReceived = false;
413   //ScopedConnection connection =
414   image.UploadedSignal().Connect( ImageUploaded );
415
416   application.SendNotification();
417   application.Render(16);
418   application.Render(16);
419   application.SendNotification();
420
421   Actor actor = CreateRenderableActor( image );
422   Stage::GetCurrent().Add(actor);
423   application.SendNotification();
424   application.Render(16);
425   application.SendNotification();
426   application.Render(16);
427   application.SendNotification();
428   application.Render(16);
429   application.SendNotification();
430   DALI_TEST_CHECK( SignalReceived == true );
431   SignalReceived = false;
432
433   image.Update(RectArea());              // notify Core that the whole image has been updated
434   application.SendNotification();
435   application.Render(16);
436   application.SendNotification();
437   application.Render(16);
438   application.SendNotification();
439   application.Render(16);
440   application.SendNotification();
441   DALI_TEST_CHECK( SignalReceived == true );
442   END_TEST;
443 }