Merge "Added ability to build for coverage for target" into devel/master
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Sampler.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 // EXTERNAL INCLUDES
19 #include <dali/public-api/dali-core.h>
20 #include <dali/devel-api/images/texture-set-image.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-test-suite-utils.h>
26 #include <mesh-builder.h>
27
28 using namespace Dali;
29
30 void sampler_test_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void sampler_test_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 int UtcDaliSamplerNew01(void)
41 {
42   TestApplication application;
43   Sampler sampler = Sampler::New();
44
45   DALI_TEST_EQUALS( (bool)sampler, true, TEST_LOCATION );
46   END_TEST;
47 }
48
49 int UtcDaliSamplerNew02(void)
50 {
51   TestApplication application;
52   Sampler sampler;
53   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
54   END_TEST;
55 }
56
57 int UtcDaliSamplerCopyConstructor(void)
58 {
59   TestApplication application;
60   tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
61
62   // Initialize an object, ref count == 1
63   Sampler sampler = Sampler::New();
64
65   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
66
67   // Copy the object, ref count == 2
68   Sampler copy(sampler);
69   DALI_TEST_CHECK(copy);
70   if (copy)
71   {
72     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
73   }
74
75   END_TEST;
76 }
77
78 int UtcDaliSamplerDownCast01(void)
79 {
80   TestApplication application;
81   Sampler sampler = Sampler::New();
82
83   BaseHandle handle(sampler);
84   Sampler sampler2 = Sampler::DownCast(handle);
85   DALI_TEST_EQUALS( (bool)sampler2, true, TEST_LOCATION );
86   END_TEST;
87 }
88
89 int UtcDaliSamplerDownCast02(void)
90 {
91   TestApplication application;
92
93   BaseHandle handle;
94   Sampler sampler = Sampler::DownCast(handle);
95   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
96   END_TEST;
97 }
98
99 int UtcDaliSamplerAssignmentOperator(void)
100 {
101   TestApplication application;
102   Sampler sampler1 = Sampler::New();
103
104   Sampler sampler2;
105
106   DALI_TEST_CHECK(!(sampler1 == sampler2));
107
108   sampler2 = sampler1;
109
110   DALI_TEST_CHECK(sampler1 == sampler2);
111
112   sampler2 = Sampler::New();
113
114   DALI_TEST_CHECK(!(sampler1 == sampler2));
115
116   END_TEST;
117 }
118
119 int UtcSamplerSetFilterMode(void)
120 {
121   TestApplication application;
122
123   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
124   Sampler sampler = Sampler::New();
125
126   TextureSet textureSet = CreateTextureSet();
127   TextureSetImage( textureSet, 0u, image );
128   textureSet.SetSampler( 0u, sampler );
129
130   Shader shader = CreateShader();
131   Geometry geometry = CreateQuadGeometry();
132   Renderer renderer = Renderer::New( geometry, shader );
133   renderer.SetTextures( textureSet );
134   Actor actor = Actor::New();
135   actor.AddRenderer(renderer);
136   actor.SetParentOrigin( ParentOrigin::CENTER );
137   actor.SetSize(400, 400);
138   Stage::GetCurrent().Add( actor );
139
140
141   TestGlAbstraction& gl = application.GetGlAbstraction();
142
143   /**************************************************************/
144   // Default/Default
145   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
146   texParameterTrace.Reset();
147   texParameterTrace.Enable( true );
148
149   sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
150   application.SendNotification();
151   application.Render();
152
153   texParameterTrace.Enable( false );
154
155   // Verify gl state
156
157   // There are three calls to TexParameteri when the texture is first created
158   // Texture mag filter is not called as the first time set it uses the system default
159   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
160
161   std::stringstream out;
162   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
163   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
164
165   /**************************************************************/
166   // Default/Default
167   texParameterTrace.Reset();
168   texParameterTrace.Enable( true );
169
170   sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
171
172   // Flush the queue and render once
173   application.SendNotification();
174   application.Render();
175
176   texParameterTrace.Enable( false );
177
178   // Verify gl state
179
180   // Should not make any calls when settings are the same
181   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
182
183   /**************************************************************/
184   // Nearest/Nearest
185   texParameterTrace.Reset();
186   texParameterTrace.Enable( true );
187
188   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
189
190   // Flush the queue and render once
191   application.SendNotification();
192   application.Render();
193
194   texParameterTrace.Enable( false );
195
196   // Verify actor gl state
197   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
198
199   out.str("");
200   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
201   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
202
203   out.str("");
204   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
205   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
206
207
208   /**************************************************************/
209   // Nearest/Linear
210   texParameterTrace.Reset();
211   texParameterTrace.Enable( true );
212
213   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
214
215   // Flush the queue and render once
216   application.SendNotification();
217   application.Render();
218
219   texParameterTrace.Enable( false );
220
221   // Verify actor gl state
222   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
223
224   out.str("");
225   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
226   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
227
228   /**************************************************************/
229   // NONE/NONE
230   texParameterTrace.Reset();
231   texParameterTrace.Enable( true );
232
233   sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
234
235   // Flush the queue and render once
236   application.SendNotification();
237   application.Render();
238
239   texParameterTrace.Enable( false );
240
241   // Verify actor gl state
242   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
243
244   out.str("");
245   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
246   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
247
248   END_TEST;
249 }
250
251 int UtcSamplerSetWrapMode1(void)
252 {
253   TestApplication application;
254
255   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
256   TextureSet textureSet = CreateTextureSet();
257   Sampler sampler = Sampler::New();
258   TextureSetImage( textureSet, 0u, image );
259   textureSet.SetSampler( 0u, sampler );
260
261   Shader shader = CreateShader();
262   Geometry geometry = CreateQuadGeometry();
263   Renderer renderer = Renderer::New( geometry, shader );
264   renderer.SetTextures( textureSet );
265
266   Actor actor = Actor::New();
267   actor.AddRenderer(renderer);
268   actor.SetParentOrigin( ParentOrigin::CENTER );
269   actor.SetSize(400, 400);
270   Stage::GetCurrent().Add( actor );
271
272   TestGlAbstraction& gl = application.GetGlAbstraction();
273
274   //****************************************
275   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
276   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
277   texParameterTrace.Reset();
278   texParameterTrace.Enable( true );
279
280   application.SendNotification();
281   application.Render();
282
283   texParameterTrace.Enable( false );
284
285   // Verify gl state
286
287   // There are three calls to TexParameteri when the texture is first created
288   // Texture mag filter is not called as the first time set it uses the system default
289   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
290
291   std::stringstream out;
292   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
293   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
294
295   out.str("");
296   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
297   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
298
299   texParameterTrace.Reset();
300   texParameterTrace.Enable( true );
301
302   sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
303
304   // Flush the queue and render once
305   application.SendNotification();
306   application.Render();
307
308   texParameterTrace.Enable( false );
309
310   // Verify gl state
311
312   // Should not make any calls when settings are the same
313   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
314
315   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
316
317   END_TEST;
318 }
319
320 int UtcSamplerSetWrapMode2(void)
321 {
322   TestApplication application;
323
324   // Create a cube-map texture.
325   unsigned int width = 8u;
326   unsigned int height = 8u;
327   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
328
329   // Create source image data.
330   unsigned int bufferSize( width * height * 4 );
331   unsigned char* buffer= new unsigned char[ bufferSize ];
332   memset( buffer, 0u, bufferSize );
333
334   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
335
336   // Upload the source image data to all 6 sides of our cube-map.
337   texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
338   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
339   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
340   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
341   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
342   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
343
344   // Finalize the cube-map setup.
345   TextureSet textureSet = TextureSet::New();
346   textureSet.SetTexture( 0u, texture );
347
348   Sampler sampler = Sampler::New();
349   textureSet.SetSampler( 0u, sampler );
350
351   Shader shader = CreateShader();
352   Geometry geometry = CreateQuadGeometry();
353   Renderer renderer = Renderer::New( geometry, shader );
354   renderer.SetTextures( textureSet );
355
356   Actor actor = Actor::New();
357   actor.AddRenderer(renderer);
358   actor.SetParentOrigin( ParentOrigin::CENTER );
359   actor.SetSize( 400, 400 );
360   Stage::GetCurrent().Add( actor );
361
362   TestGlAbstraction& gl = application.GetGlAbstraction();
363
364   application.SendNotification();
365   application.Render();
366
367   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
368   texParameterTrace.Reset();
369   texParameterTrace.Enable( true );
370
371   // Call the 3 dimensional wrap mode API.
372   sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
373
374   application.SendNotification();
375   application.Render();
376
377   texParameterTrace.Enable( false );
378
379   // Verify that 3 TexParameteri calls occurred.
380   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );
381
382   END_TEST;
383 }