Merge "Added support for Multiple Render Targets, to Dali::FrameBuffer." into devel...
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-FrameBuffer.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 <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void framebuffer_set_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void framebuffer_set_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliFrameBufferNew01(void)
36 {
37   TestApplication application;
38
39   unsigned int width(64);
40   unsigned int height(64);
41   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
42
43   DALI_TEST_CHECK( frameBuffer );
44
45   application.SendNotification();
46   application.Render();
47
48   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 0u, TEST_LOCATION);
49   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
50   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
51
52   END_TEST;
53 }
54
55 int UtcDaliFrameBufferNew02(void)
56 {
57   TestApplication application;
58
59   unsigned int width(64);
60   unsigned int height(64);
61   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::DEPTH );
62
63   DALI_TEST_CHECK( frameBuffer );
64
65   application.SendNotification();
66   application.Render();
67
68   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 0u, TEST_LOCATION);
69   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
70   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
71
72   END_TEST;
73 }
74
75 int UtcDaliFrameBufferNew03(void)
76 {
77   TestApplication application;
78
79   unsigned int width(64);
80   unsigned int height(64);
81   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::STENCIL );
82
83   DALI_TEST_CHECK( frameBuffer );
84
85   application.SendNotification();
86   application.Render();
87
88   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 0u, TEST_LOCATION);
89   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
90   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
91
92   END_TEST;
93 }
94
95 int UtcDaliFrameBufferNew04(void)
96 {
97   TestApplication application;
98
99   unsigned int width(64);
100   unsigned int height(64);
101   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::DEPTH_STENCIL );
102
103   DALI_TEST_CHECK( frameBuffer );
104
105   application.SendNotification();
106   application.Render();
107
108   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 0u, TEST_LOCATION);
109   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
110   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
111
112   END_TEST;
113 }
114
115 int UtcDaliFrameBufferNew05(void)
116 {
117   TestApplication application;
118   FrameBuffer frameBuffer;
119   DALI_TEST_CHECK( !frameBuffer );
120   END_TEST;
121 }
122
123 int UtcDaliFrameBufferNew06(void)
124 {
125   TestApplication application;
126
127   unsigned int width(64);
128   unsigned int height(64);
129   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::DEPTH | FrameBuffer::Attachment::STENCIL );
130
131   DALI_TEST_CHECK( frameBuffer );
132
133   application.SendNotification();
134   application.Render();
135
136   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 0u, TEST_LOCATION);
137   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
138   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
139
140   END_TEST;
141 }
142
143 int UtcDaliFrameBufferNewWithColor01(void)
144 {
145   TestApplication application;
146   uint32_t width = 64;
147   uint32_t height = 64;
148   FrameBuffer frameBuffer = FrameBuffer::New( width, height );
149   application.SendNotification();
150   application.Render();
151   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
152   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
153   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
154   // check that texture is not empty handle
155   DALI_TEST_CHECK( frameBuffer.GetColorTexture() );
156   END_TEST;
157 }
158
159 int UtcDaliFrameBufferNewWithColor02(void)
160 {
161   TestApplication application;
162   uint32_t width = 64;
163   uint32_t height = 64;
164   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::COLOR );
165   application.SendNotification();
166   application.Render();
167   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
168   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
169   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
170   // check that texture is not empty handle
171   DALI_TEST_CHECK( frameBuffer.GetColorTexture() );
172   END_TEST;
173 }
174
175 int UtcDaliFrameBufferNewWithColor03(void)
176 {
177   TestApplication application;
178   uint32_t width = 64;
179   uint32_t height = 64;
180   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::COLOR_DEPTH );
181   application.SendNotification();
182   application.Render();
183   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
184   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
185   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
186   // check that texture is not empty handle
187   DALI_TEST_CHECK( frameBuffer.GetColorTexture() );
188   END_TEST;
189 }
190
191 int UtcDaliFrameBufferNewWithColor04(void)
192 {
193   TestApplication application;
194   uint32_t width = 64;
195   uint32_t height = 64;
196   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::COLOR_STENCIL );
197   application.SendNotification();
198   application.Render();
199   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
200   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
201   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
202   // check that texture is not empty handle
203   DALI_TEST_CHECK( frameBuffer.GetColorTexture() );
204   END_TEST;
205 }
206
207 int UtcDaliFrameBufferNewWithColor05(void)
208 {
209   TestApplication application;
210   uint32_t width = 64;
211   uint32_t height = 64;
212   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::COLOR_DEPTH_STENCIL );
213   application.SendNotification();
214   application.Render();
215   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
216   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
217   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
218   // check that texture is not empty handle
219   DALI_TEST_CHECK( frameBuffer.GetColorTexture() );
220   END_TEST;
221 }
222
223 int UtcDaliFrameBufferCopyConstructor(void)
224 {
225   TestApplication application;
226
227   unsigned int width(64);
228   unsigned int height(64);
229   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
230
231   FrameBuffer frameBufferCopy( frameBuffer );
232
233   DALI_TEST_CHECK( frameBufferCopy );
234
235   END_TEST;
236 }
237
238 int UtcDaliFrameBufferAssignmentOperator(void)
239 {
240   TestApplication application;
241
242   unsigned int width(64);
243   unsigned int height(64);
244   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
245
246   FrameBuffer frameBuffer2;
247   DALI_TEST_CHECK( !frameBuffer2 );
248
249   frameBuffer2 = frameBuffer;
250   DALI_TEST_CHECK( frameBuffer2 );
251
252   END_TEST;
253 }
254
255 int UtcDaliFrameBufferDownCast01(void)
256 {
257   TestApplication application;
258   unsigned int width(64);
259   unsigned int height(64);
260   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
261
262   BaseHandle handle(frameBuffer);
263   FrameBuffer frameBuffer2 = FrameBuffer::DownCast(handle);
264   DALI_TEST_CHECK( frameBuffer2 );
265
266   END_TEST;
267 }
268
269 int UtcDaliFrameBufferDownCast02(void)
270 {
271   TestApplication application;
272
273   Handle handle = Handle::New(); // Create a custom object
274   FrameBuffer frameBuffer = FrameBuffer::DownCast(handle);
275   DALI_TEST_CHECK( !frameBuffer );
276   END_TEST;
277 }
278
279 int UtcDaliFrameBufferAttachColorTexture01(void)
280 {
281   TestApplication application;
282
283   unsigned int width(64);
284   unsigned int height(64);
285   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::DEPTH_STENCIL );
286   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
287   frameBuffer.AttachColorTexture( texture );
288
289   application.SendNotification();
290   application.Render();
291
292   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
293   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
294   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
295
296   END_TEST;
297 }
298
299 int UtcDaliFrameBufferAttachColorTexture02(void)
300 {
301   TestApplication application;
302
303   unsigned int width(64);
304   unsigned int height(64);
305   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
306   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
307   texture.GenerateMipmaps();
308
309   //Attach mipmap 1
310   frameBuffer.AttachColorTexture( texture, 0u, 1u );
311
312   application.SendNotification();
313   application.Render();
314
315   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
316   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
317   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
318
319   END_TEST;
320 }
321
322 int UtcDaliFrameBufferAttachColorTexture03(void)
323 {
324   TestApplication application;
325
326   unsigned int width(64);
327   unsigned int height(64);
328   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
329   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
330   texture.GenerateMipmaps();
331
332   //Attach NEGATIVE_Y face of the cubemap
333   frameBuffer.AttachColorTexture( texture, 0u, CubeMapLayer::NEGATIVE_Y );
334
335   application.SendNotification();
336   application.Render();
337
338   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
339   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
340   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
341
342   END_TEST;
343 }
344
345 int UtcDaliFrameBufferAttachColorTexture04(void)
346 {
347   TestApplication application;
348
349   unsigned int width(64);
350   unsigned int height(64);
351   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::DEPTH | FrameBuffer::Attachment::STENCIL );
352   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
353   frameBuffer.AttachColorTexture( texture );
354
355   application.SendNotification();
356   application.Render();
357
358   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), 1u, TEST_LOCATION);
359   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
360   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_TRUE, TEST_LOCATION);
361
362   END_TEST;
363 }
364
365 int UtcDaliFrameBufferAttachColorTexture05(void)
366 {
367   TestApplication application;
368
369   unsigned int width(64);
370   unsigned int height(64);
371   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
372   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
373
374   // N.B. it doesn't make sense per se, however the OGL standard doesn't seem to forbid attaching the same texture to different slots.
375   for (int i = 0; i < Dali::FrameBuffer::MAX_COLOR_ATTACHMENTS + 1; ++i)
376   {
377     frameBuffer.AttachColorTexture( texture );
378   }
379
380   application.SendNotification();
381   application.Render();
382
383   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferColorAttachmentCount(), Dali::FrameBuffer::MAX_COLOR_ATTACHMENTS, TEST_LOCATION);
384   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferDepthAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
385   DALI_TEST_EQUALS(application.GetGlAbstraction().CheckFramebufferStencilAttachment(), (GLenum)GL_FALSE, TEST_LOCATION);
386
387   END_TEST;
388 }
389
390 int UtcDaliFrameBufferGetColorTexture01(void)
391 {
392   TestApplication application;
393
394   unsigned int width(64);
395   unsigned int height(64);
396   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
397   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
398   frameBuffer.AttachColorTexture( texture );
399
400   DALI_TEST_EQUALS(frameBuffer.GetColorTexture(), texture, TEST_LOCATION);
401
402   END_TEST;
403 }
404
405 int UtcDaliFrameBufferGetColorTexture02(void)
406 {
407   TestApplication application;
408
409   unsigned int width(64);
410   unsigned int height(64);
411   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
412   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
413   frameBuffer.AttachColorTexture( texture, 0u, 1u );
414
415   DALI_TEST_EQUALS(frameBuffer.GetColorTexture(), texture, TEST_LOCATION);
416
417   END_TEST;
418 }
419
420 int UtcDaliFrameBufferGetColorTexture03(void)
421 { // FrameBuffer::GetColorTexture() and GetColorTexture(0) are equivalent
422   TestApplication application;
423
424   unsigned int width(64);
425   unsigned int height(64);
426   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
427   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
428   frameBuffer.AttachColorTexture( texture, 0u, 1u );
429
430   DALI_TEST_EQUALS(frameBuffer.GetColorTexture(), texture, TEST_LOCATION);
431   DALI_TEST_EQUALS(frameBuffer.GetColorTexture(0), texture, TEST_LOCATION);
432
433   END_TEST;
434 }
435
436 int UtcDaliFrameBufferGetColorTexture04(void)
437 {
438   TestApplication application;
439
440   unsigned int width(64);
441   unsigned int height(64);
442   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
443   Texture textures[] = {
444       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
445       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
446       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
447       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
448       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
449       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
450       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
451       Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height ),
452   };
453
454   for (auto& t: textures)
455   {
456     frameBuffer.AttachColorTexture( t, 0u, 1u );
457   }
458
459   for (unsigned int i = 0; i < std::extent<decltype(textures)>::value; ++i)
460   {
461     DALI_TEST_EQUALS(frameBuffer.GetColorTexture(i), textures[i], TEST_LOCATION);
462   }
463
464   END_TEST;
465 }
466
467 int UtcDaliFramebufferContextLoss(void)
468 {
469   tet_infoline("UtcDaliFramebufferContextLoss\n");
470   TestApplication application; // Default config: DALI_DISCARDS_ALL_DATA
471
472   //Create the texture
473   unsigned int width(64);
474   unsigned int height(64);
475   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height );
476   DALI_TEST_CHECK( texture );
477   FrameBuffer frameBuffer = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
478   DALI_TEST_CHECK( frameBuffer );
479   frameBuffer.AttachColorTexture( texture, 0u, 1u );
480
481   Geometry geometry = CreateQuadGeometry();
482   Shader shader = CreateShader();
483   Renderer renderer = Renderer::New(geometry, shader);
484
485   application.SendNotification();
486   application.Render(16);
487
488   // Lose & regain context (in render 'thread')
489   application.ResetContext();
490   DALI_TEST_CHECK( frameBuffer );
491
492   END_TEST;
493 }