Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Sampler.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 // EXTERNAL INCLUDES
19 #include <dali/public-api/dali-core.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-test-suite-utils.h>
25 #include <mesh-builder.h>
26
27 using namespace Dali;
28
29 void sampler_test_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void sampler_test_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39 int UtcDaliSamplerNew01(void)
40 {
41   TestApplication application;
42   Sampler sampler = Sampler::New();
43
44   DALI_TEST_EQUALS( (bool)sampler, true, TEST_LOCATION );
45   END_TEST;
46 }
47
48 int UtcDaliSamplerNew02(void)
49 {
50   TestApplication application;
51   Sampler sampler;
52   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
53   END_TEST;
54 }
55
56 int UtcDaliSamplerCopyConstructor(void)
57 {
58   TestApplication application;
59   tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
60
61   // Initialize an object, ref count == 1
62   Sampler sampler = Sampler::New();
63
64   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
65
66   // Copy the object, ref count == 2
67   Sampler copy(sampler);
68   DALI_TEST_CHECK(copy);
69   if (copy)
70   {
71     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
72   }
73
74   END_TEST;
75 }
76
77 int UtcDaliSamplerMoveConstructor(void)
78 {
79   TestApplication application;
80
81   Sampler sampler = Sampler::New();
82   DALI_TEST_CHECK( sampler );
83   DALI_TEST_EQUALS( 1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION );
84
85   Sampler move = std::move( sampler );
86   DALI_TEST_CHECK( move );
87   DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
88   DALI_TEST_CHECK( !sampler );
89
90   END_TEST;
91 }
92
93 int UtcDaliSamplerMoveAssignment(void)
94 {
95   TestApplication application;
96
97   Sampler sampler = Sampler::New();
98   DALI_TEST_CHECK( sampler );
99   DALI_TEST_EQUALS( 1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION );
100
101   Sampler move;
102   move = std::move( sampler );
103   DALI_TEST_CHECK( move );
104   DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
105   DALI_TEST_CHECK( !sampler );
106
107   END_TEST;
108 }
109
110 int UtcDaliSamplerDownCast01(void)
111 {
112   TestApplication application;
113   Sampler sampler = Sampler::New();
114
115   BaseHandle handle(sampler);
116   Sampler sampler2 = Sampler::DownCast(handle);
117   DALI_TEST_EQUALS( (bool)sampler2, true, TEST_LOCATION );
118   END_TEST;
119 }
120
121 int UtcDaliSamplerDownCast02(void)
122 {
123   TestApplication application;
124
125   BaseHandle handle;
126   Sampler sampler = Sampler::DownCast(handle);
127   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
128   END_TEST;
129 }
130
131 int UtcDaliSamplerAssignmentOperator(void)
132 {
133   TestApplication application;
134   Sampler sampler1 = Sampler::New();
135
136   Sampler sampler2;
137
138   DALI_TEST_CHECK(!(sampler1 == sampler2));
139
140   sampler2 = sampler1;
141
142   DALI_TEST_CHECK(sampler1 == sampler2);
143
144   sampler2 = Sampler::New();
145
146   DALI_TEST_CHECK(!(sampler1 == sampler2));
147
148   END_TEST;
149 }
150
151 int UtcSamplerSetFilterMode(void)
152 {
153   TestApplication application;
154
155   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
156   Sampler sampler = Sampler::New();
157
158   TextureSet textureSet = CreateTextureSet();
159   textureSet.SetTexture(0u, image);
160   textureSet.SetSampler( 0u, sampler );
161
162   Shader shader = CreateShader();
163   Geometry geometry = CreateQuadGeometry();
164   Renderer renderer = Renderer::New( geometry, shader );
165   renderer.SetTextures( textureSet );
166   Actor actor = Actor::New();
167   actor.AddRenderer(renderer);
168   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
169   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
170   application.GetScene().Add( actor );
171
172
173   TestGlAbstraction& gl = application.GetGlAbstraction();
174
175   /**************************************************************/
176   // Default/Default
177   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
178   texParameterTrace.Reset();
179   texParameterTrace.Enable( true );
180
181   sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
182   application.SendNotification();
183   application.Render();
184
185   texParameterTrace.Enable( false );
186
187   // Verify gl state
188
189   // There are 4 calls to TexParameteri when the texture is first created
190   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
191
192   std::stringstream out;
193   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
194   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
195
196   /**************************************************************/
197   // Linear/Linear
198   texParameterTrace.Reset();
199   texParameterTrace.Enable( true );
200
201   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
202
203   // Flush the queue and render once
204   application.SendNotification();
205   application.Render();
206
207   texParameterTrace.Enable( false );
208
209   // Verify gl state
210
211   // Should not make any calls when settings are the same (DEFAULT = LINEAR )
212   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
213
214   /**************************************************************/
215   // Nearest/Nearest
216   texParameterTrace.Reset();
217   texParameterTrace.Enable( true );
218
219   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
220
221   // Flush the queue and render once
222   application.SendNotification();
223   application.Render();
224
225   texParameterTrace.Enable( false );
226
227   // Verify actor gl state
228   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
229
230   out.str("");
231   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
232   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
233
234   out.str("");
235   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
236   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
237
238
239   /**************************************************************/
240   // Nearest/Linear
241   texParameterTrace.Reset();
242   texParameterTrace.Enable( true );
243
244   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
245
246   // Flush the queue and render once
247   application.SendNotification();
248   application.Render();
249
250   texParameterTrace.Enable( false );
251
252   // Verify actor gl state
253   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
254
255   out.str("");
256   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
257   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
258
259   /**************************************************************/
260   // NONE/NONE
261   texParameterTrace.Reset();
262   texParameterTrace.Enable( true );
263
264   sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
265
266   // Flush the queue and render once
267   application.SendNotification();
268   application.Render();
269
270   texParameterTrace.Enable( false );
271
272   // Verify actor gl state
273   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
274
275   out.str("");
276   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
277   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
278
279   END_TEST;
280 }
281
282 int UtcSamplerSetWrapMode1(void)
283 {
284   TestApplication application;
285
286   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
287   TextureSet textureSet = CreateTextureSet();
288   Sampler sampler = Sampler::New();
289   textureSet.SetTexture(0u, image);
290   textureSet.SetSampler( 0u, sampler );
291
292   Shader shader = CreateShader();
293   Geometry geometry = CreateQuadGeometry();
294   Renderer renderer = Renderer::New( geometry, shader );
295   renderer.SetTextures( textureSet );
296
297   Actor actor = Actor::New();
298   actor.AddRenderer(renderer);
299   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
300   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
301   application.GetScene().Add( actor );
302
303   TestGlAbstraction& gl = application.GetGlAbstraction();
304
305   //****************************************
306   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
307   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
308   texParameterTrace.Reset();
309   texParameterTrace.Enable( true );
310
311   application.SendNotification();
312   application.Render();
313
314   texParameterTrace.Enable( false );
315
316   // Verify gl state
317
318   // There are 4 calls to TexParameteri when the texture is first created
319   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
320
321   std::stringstream out;
322   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
323   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
324
325   out.str("");
326   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
327   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
328
329   texParameterTrace.Reset();
330   texParameterTrace.Enable( true );
331
332   sampler.SetWrapMode( WrapMode::DEFAULT, WrapMode::DEFAULT );
333
334   // Flush the queue and render once
335   application.SendNotification();
336   application.Render();
337
338   texParameterTrace.Enable( false );
339
340   // Verify gl state
341
342   // Should not make any calls when settings are the same
343   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
344
345   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
346
347   END_TEST;
348 }
349
350 int UtcSamplerSetWrapMode2(void)
351 {
352   TestApplication application;
353
354   // Create a cube-map texture.
355   unsigned int width = 8u;
356   unsigned int height = 8u;
357   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
358
359   // Create source image data.
360   unsigned int bufferSize( width * height * 4 );
361   unsigned char* buffer= new unsigned char[ bufferSize ];
362   memset( buffer, 0u, bufferSize );
363
364   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
365
366   // Upload the source image data to all 6 sides of our cube-map.
367   texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
368   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
369   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
370   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
371   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
372   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
373
374   // Finalize the cube-map setup.
375   TextureSet textureSet = TextureSet::New();
376   textureSet.SetTexture( 0u, texture );
377
378   Sampler sampler = Sampler::New();
379   textureSet.SetSampler( 0u, sampler );
380
381   Shader shader = CreateShader();
382   Geometry geometry = CreateQuadGeometry();
383   Renderer renderer = Renderer::New( geometry, shader );
384   renderer.SetTextures( textureSet );
385
386   Actor actor = Actor::New();
387   actor.AddRenderer(renderer);
388   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
389   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
390   application.GetScene().Add( actor );
391
392   TestGlAbstraction& gl = application.GetGlAbstraction();
393
394   application.SendNotification();
395   application.Render();
396
397   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
398   texParameterTrace.Reset();
399   texParameterTrace.Enable( true );
400
401   // Call the 3 dimensional wrap mode API.
402   sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
403
404   application.SendNotification();
405   application.Render();
406
407   // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
408   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0u, TEST_LOCATION );
409
410
411   sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
412   texParameterTrace.Reset();
413   application.SendNotification();
414   application.Render();
415
416   texParameterTrace.Enable( false );
417
418   // Verify that 3 TexParameteri calls occurred.
419   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );
420   END_TEST;
421 }
422
423 int UtcDaliSamplerSetWrapModeNegative01(void)
424 {
425   TestApplication application;
426   Dali::Sampler instance;
427   try
428   {
429     Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
430     Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
431     instance.SetWrapMode(arg1,arg2);
432     DALI_TEST_CHECK(false); // Should not get here
433   }
434   catch(...)
435   {
436     DALI_TEST_CHECK(true); // We expect an assert
437   }
438   END_TEST;
439 }
440
441 int UtcDaliSamplerSetWrapModeNegative02(void)
442 {
443   TestApplication application;
444   Dali::Sampler instance;
445   try
446   {
447     Dali::WrapMode::Type arg1(static_cast<Dali::WrapMode::Type>(-1));
448     Dali::WrapMode::Type arg2(static_cast<Dali::WrapMode::Type>(-1));
449     Dali::WrapMode::Type arg3(static_cast<Dali::WrapMode::Type>(-1));
450     instance.SetWrapMode(arg1,arg2,arg3);
451     DALI_TEST_CHECK(false); // Should not get here
452   }
453   catch(...)
454   {
455     DALI_TEST_CHECK(true); // We expect an assert
456   }
457   END_TEST;
458 }
459
460 int UtcDaliSamplerSetFilterModeNegative(void)
461 {
462   TestApplication application;
463   Dali::Sampler instance;
464   try
465   {
466     Dali::FilterMode::Type arg1(static_cast<Dali::FilterMode::Type>(-1));
467     Dali::FilterMode::Type arg2(static_cast<Dali::FilterMode::Type>(-1));
468     instance.SetFilterMode(arg1,arg2);
469     DALI_TEST_CHECK(false); // Should not get here
470   }
471   catch(...)
472   {
473     DALI_TEST_CHECK(true); // We expect an assert
474   }
475   END_TEST;
476 }