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 UtcDaliSamplerDownCast01(void)
78 {
79   TestApplication application;
80   Sampler sampler = Sampler::New();
81
82   BaseHandle handle(sampler);
83   Sampler sampler2 = Sampler::DownCast(handle);
84   DALI_TEST_EQUALS( (bool)sampler2, true, TEST_LOCATION );
85   END_TEST;
86 }
87
88 int UtcDaliSamplerDownCast02(void)
89 {
90   TestApplication application;
91
92   BaseHandle handle;
93   Sampler sampler = Sampler::DownCast(handle);
94   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
95   END_TEST;
96 }
97
98 int UtcDaliSamplerAssignmentOperator(void)
99 {
100   TestApplication application;
101   Sampler sampler1 = Sampler::New();
102
103   Sampler sampler2;
104
105   DALI_TEST_CHECK(!(sampler1 == sampler2));
106
107   sampler2 = sampler1;
108
109   DALI_TEST_CHECK(sampler1 == sampler2);
110
111   sampler2 = Sampler::New();
112
113   DALI_TEST_CHECK(!(sampler1 == sampler2));
114
115   END_TEST;
116 }
117
118 int UtcSamplerSetFilterMode(void)
119 {
120   TestApplication application;
121
122   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
123   Sampler sampler = Sampler::New();
124
125   TextureSet textureSet = CreateTextureSet();
126   textureSet.SetTexture(0u, image);
127   textureSet.SetSampler( 0u, sampler );
128
129   Shader shader = CreateShader();
130   Geometry geometry = CreateQuadGeometry();
131   Renderer renderer = Renderer::New( geometry, shader );
132   renderer.SetTextures( textureSet );
133   Actor actor = Actor::New();
134   actor.AddRenderer(renderer);
135   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
136   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
137   application.GetScene().Add( actor );
138
139
140   TestGlAbstraction& gl = application.GetGlAbstraction();
141
142   /**************************************************************/
143   // Default/Default
144   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
145   texParameterTrace.Reset();
146   texParameterTrace.Enable( true );
147
148   sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
149   application.SendNotification();
150   application.Render();
151
152   texParameterTrace.Enable( false );
153
154   // Verify gl state
155
156   // There are 4 calls to TexParameteri when the texture is first created
157   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
158
159   std::stringstream out;
160   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
161   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
162
163   /**************************************************************/
164   // Linear/Linear
165   texParameterTrace.Reset();
166   texParameterTrace.Enable( true );
167
168   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
169
170   // Flush the queue and render once
171   application.SendNotification();
172   application.Render();
173
174   texParameterTrace.Enable( false );
175
176   // Verify gl state
177
178   // Should not make any calls when settings are the same (DEFAULT = LINEAR )
179   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
180
181   /**************************************************************/
182   // Nearest/Nearest
183   texParameterTrace.Reset();
184   texParameterTrace.Enable( true );
185
186   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
187
188   // Flush the queue and render once
189   application.SendNotification();
190   application.Render();
191
192   texParameterTrace.Enable( false );
193
194   // Verify actor gl state
195   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
196
197   out.str("");
198   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
199   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
200
201   out.str("");
202   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
203   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
204
205
206   /**************************************************************/
207   // Nearest/Linear
208   texParameterTrace.Reset();
209   texParameterTrace.Enable( true );
210
211   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
212
213   // Flush the queue and render once
214   application.SendNotification();
215   application.Render();
216
217   texParameterTrace.Enable( false );
218
219   // Verify actor gl state
220   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
221
222   out.str("");
223   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
224   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
225
226   /**************************************************************/
227   // NONE/NONE
228   texParameterTrace.Reset();
229   texParameterTrace.Enable( true );
230
231   sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
232
233   // Flush the queue and render once
234   application.SendNotification();
235   application.Render();
236
237   texParameterTrace.Enable( false );
238
239   // Verify actor gl state
240   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
241
242   out.str("");
243   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
244   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
245
246   END_TEST;
247 }
248
249 int UtcSamplerSetWrapMode1(void)
250 {
251   TestApplication application;
252
253   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
254   TextureSet textureSet = CreateTextureSet();
255   Sampler sampler = Sampler::New();
256   textureSet.SetTexture(0u, image);
257   textureSet.SetSampler( 0u, sampler );
258
259   Shader shader = CreateShader();
260   Geometry geometry = CreateQuadGeometry();
261   Renderer renderer = Renderer::New( geometry, shader );
262   renderer.SetTextures( textureSet );
263
264   Actor actor = Actor::New();
265   actor.AddRenderer(renderer);
266   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
267   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
268   application.GetScene().Add( actor );
269
270   TestGlAbstraction& gl = application.GetGlAbstraction();
271
272   //****************************************
273   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
274   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
275   texParameterTrace.Reset();
276   texParameterTrace.Enable( true );
277
278   application.SendNotification();
279   application.Render();
280
281   texParameterTrace.Enable( false );
282
283   // Verify gl state
284
285   // There are 4 calls to TexParameteri when the texture is first created
286   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
287
288   std::stringstream out;
289   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
290   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
291
292   out.str("");
293   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
294   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
295
296   texParameterTrace.Reset();
297   texParameterTrace.Enable( true );
298
299   sampler.SetWrapMode( WrapMode::DEFAULT, WrapMode::DEFAULT );
300
301   // Flush the queue and render once
302   application.SendNotification();
303   application.Render();
304
305   texParameterTrace.Enable( false );
306
307   // Verify gl state
308
309   // Should not make any calls when settings are the same
310   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
311
312   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
313
314   END_TEST;
315 }
316
317 int UtcSamplerSetWrapMode2(void)
318 {
319   TestApplication application;
320
321   // Create a cube-map texture.
322   unsigned int width = 8u;
323   unsigned int height = 8u;
324   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
325
326   // Create source image data.
327   unsigned int bufferSize( width * height * 4 );
328   unsigned char* buffer= new unsigned char[ bufferSize ];
329   memset( buffer, 0u, bufferSize );
330
331   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
332
333   // Upload the source image data to all 6 sides of our cube-map.
334   texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
335   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
336   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
337   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
338   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
339   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
340
341   // Finalize the cube-map setup.
342   TextureSet textureSet = TextureSet::New();
343   textureSet.SetTexture( 0u, texture );
344
345   Sampler sampler = Sampler::New();
346   textureSet.SetSampler( 0u, sampler );
347
348   Shader shader = CreateShader();
349   Geometry geometry = CreateQuadGeometry();
350   Renderer renderer = Renderer::New( geometry, shader );
351   renderer.SetTextures( textureSet );
352
353   Actor actor = Actor::New();
354   actor.AddRenderer(renderer);
355   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
356   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
357   application.GetScene().Add( actor );
358
359   TestGlAbstraction& gl = application.GetGlAbstraction();
360
361   application.SendNotification();
362   application.Render();
363
364   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
365   texParameterTrace.Reset();
366   texParameterTrace.Enable( true );
367
368   // Call the 3 dimensional wrap mode API.
369   sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
370
371   application.SendNotification();
372   application.Render();
373
374   // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
375   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0u, TEST_LOCATION );
376
377
378   sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
379   texParameterTrace.Reset();
380   application.SendNotification();
381   application.Render();
382
383   texParameterTrace.Enable( false );
384
385   // Verify that 3 TexParameteri calls occurred.
386   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );
387   END_TEST;
388 }