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