[Tizen] Add screen and client rotation itself function
[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 4 calls to TexParameteri when the texture is first created
158   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
159
160   std::stringstream out;
161   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
162   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
163
164   /**************************************************************/
165   // Linear/Linear
166   texParameterTrace.Reset();
167   texParameterTrace.Enable( true );
168
169   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
170
171   // Flush the queue and render once
172   application.SendNotification();
173   application.Render();
174
175   texParameterTrace.Enable( false );
176
177   // Verify gl state
178
179   // Should not make any calls when settings are the same (DEFAULT = LINEAR )
180   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
181
182   /**************************************************************/
183   // Nearest/Nearest
184   texParameterTrace.Reset();
185   texParameterTrace.Enable( true );
186
187   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
188
189   // Flush the queue and render once
190   application.SendNotification();
191   application.Render();
192
193   texParameterTrace.Enable( false );
194
195   // Verify actor gl state
196   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
197
198   out.str("");
199   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
200   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
201
202   out.str("");
203   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
204   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
205
206
207   /**************************************************************/
208   // Nearest/Linear
209   texParameterTrace.Reset();
210   texParameterTrace.Enable( true );
211
212   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::LINEAR );
213
214   // Flush the queue and render once
215   application.SendNotification();
216   application.Render();
217
218   texParameterTrace.Enable( false );
219
220   // Verify actor gl state
221   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
222
223   out.str("");
224   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
225   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
226
227   /**************************************************************/
228   // NONE/NONE
229   texParameterTrace.Reset();
230   texParameterTrace.Enable( true );
231
232   sampler.SetFilterMode( FilterMode::NONE, FilterMode::NONE );
233
234   // Flush the queue and render once
235   application.SendNotification();
236   application.Render();
237
238   texParameterTrace.Enable( false );
239
240   // Verify actor gl state
241   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
242
243   out.str("");
244   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
245   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
246
247   END_TEST;
248 }
249
250 int UtcSamplerSetWrapMode1(void)
251 {
252   TestApplication application;
253
254   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
255   TextureSet textureSet = CreateTextureSet();
256   Sampler sampler = Sampler::New();
257   TextureSetImage( textureSet, 0u, image );
258   textureSet.SetSampler( 0u, sampler );
259
260   Shader shader = CreateShader();
261   Geometry geometry = CreateQuadGeometry();
262   Renderer renderer = Renderer::New( geometry, shader );
263   renderer.SetTextures( textureSet );
264
265   Actor actor = Actor::New();
266   actor.AddRenderer(renderer);
267   actor.SetParentOrigin( ParentOrigin::CENTER );
268   actor.SetSize(400, 400);
269   Stage::GetCurrent().Add( actor );
270
271   TestGlAbstraction& gl = application.GetGlAbstraction();
272
273   //****************************************
274   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
275   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
276   texParameterTrace.Reset();
277   texParameterTrace.Enable( true );
278
279   application.SendNotification();
280   application.Render();
281
282   texParameterTrace.Enable( false );
283
284   // Verify gl state
285
286   // There are 4 calls to TexParameteri when the texture is first created
287   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
288
289   std::stringstream out;
290   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
291   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
292
293   out.str("");
294   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
295   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(3, "TexParameteri", out.str()), true, TEST_LOCATION);
296
297   texParameterTrace.Reset();
298   texParameterTrace.Enable( true );
299
300   sampler.SetWrapMode( WrapMode::DEFAULT, WrapMode::DEFAULT );
301
302   // Flush the queue and render once
303   application.SendNotification();
304   application.Render();
305
306   texParameterTrace.Enable( false );
307
308   // Verify gl state
309
310   // Should not make any calls when settings are the same
311   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
312
313   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
314
315   END_TEST;
316 }
317
318 int UtcSamplerSetWrapMode2(void)
319 {
320   TestApplication application;
321
322   // Create a cube-map texture.
323   unsigned int width = 8u;
324   unsigned int height = 8u;
325   Texture texture = Texture::New( TextureType::TEXTURE_CUBE, Pixel::RGBA8888, width, height );
326
327   // Create source image data.
328   unsigned int bufferSize( width * height * 4 );
329   unsigned char* buffer= new unsigned char[ bufferSize ];
330   memset( buffer, 0u, bufferSize );
331
332   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGBA8888, PixelData::DELETE_ARRAY );
333
334   // Upload the source image data to all 6 sides of our cube-map.
335   texture.Upload( pixelData, CubeMapLayer::POSITIVE_X, 0u, 0u, 0u, width, height );
336   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_X, 0u, 0u, 0u, width, height );
337   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Y, 0u, 0u, 0u, width, height );
338   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Y, 0u, 0u, 0u, width, height );
339   texture.Upload( pixelData, CubeMapLayer::POSITIVE_Z, 0u, 0u, 0u, width, height );
340   texture.Upload( pixelData, CubeMapLayer::NEGATIVE_Z, 0u, 0u, 0u, width, height );
341
342   // Finalize the cube-map setup.
343   TextureSet textureSet = TextureSet::New();
344   textureSet.SetTexture( 0u, texture );
345
346   Sampler sampler = Sampler::New();
347   textureSet.SetSampler( 0u, sampler );
348
349   Shader shader = CreateShader();
350   Geometry geometry = CreateQuadGeometry();
351   Renderer renderer = Renderer::New( geometry, shader );
352   renderer.SetTextures( textureSet );
353
354   Actor actor = Actor::New();
355   actor.AddRenderer(renderer);
356   actor.SetParentOrigin( ParentOrigin::CENTER );
357   actor.SetSize( 400, 400 );
358   Stage::GetCurrent().Add( actor );
359
360   TestGlAbstraction& gl = application.GetGlAbstraction();
361
362   application.SendNotification();
363   application.Render();
364
365   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
366   texParameterTrace.Reset();
367   texParameterTrace.Enable( true );
368
369   // Call the 3 dimensional wrap mode API.
370   sampler.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
371
372   application.SendNotification();
373   application.Render();
374
375   // Verify that no TexParameteri calls occurred since wrap mode hasn't changed.
376   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0u, TEST_LOCATION );
377
378
379   sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
380   texParameterTrace.Reset();
381   application.SendNotification();
382   application.Render();
383
384   texParameterTrace.Enable( false );
385
386   // Verify that 3 TexParameteri calls occurred.
387   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3u, TEST_LOCATION );
388   END_TEST;
389 }