Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-TextureSet.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
21 // INTERNAL INCLUDES
22 #include <dali-test-suite-utils.h>
23 #include <mesh-builder.h>
24
25 using namespace Dali;
26
27 namespace
28 {
29
30 enum SetSampler
31 {
32   SET_SAMPLER,
33   DONT_SET_SAMPLER
34 };
35
36 Actor CreateActor( SetSampler setSamplerOption )
37 {
38   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
39
40   Shader shader = CreateShader();
41   TextureSet textureSet = CreateTextureSet();
42
43   Sampler sampler = Sampler::New();
44   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
45   textureSet.SetTexture( 0u, texture );
46   if( setSamplerOption == SET_SAMPLER )
47   {
48     textureSet.SetSampler( 0u, sampler );
49   }
50
51   Geometry geometry = CreateQuadGeometry();
52   Renderer renderer = Renderer::New( geometry, shader );
53   renderer.SetTextures( textureSet );
54
55   Actor actor = Actor::New();
56   actor.AddRenderer(renderer);
57   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
58   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
59
60   return actor;
61 }
62
63 } // namespace
64
65
66 void texture_set_test_startup(void)
67 {
68   test_return_value = TET_UNDEF;
69 }
70
71 void texture_set_test_cleanup(void)
72 {
73   test_return_value = TET_PASS;
74 }
75
76 int UtcDaliTextureSetNew01(void)
77 {
78   TestApplication application;
79
80   TextureSet textureSet = TextureSet::New();
81
82   DALI_TEST_CHECK( textureSet );
83   END_TEST;
84 }
85
86 int UtcDaliTextureSetNew02(void)
87 {
88   TestApplication application;
89   TextureSet textureSet;
90   DALI_TEST_CHECK( !textureSet );
91   END_TEST;
92 }
93
94 int UtcDaliTextureSetCopyConstructor(void)
95 {
96   TestApplication application;
97
98   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 32, 32);
99   TextureSet textureSet = TextureSet::New();
100   textureSet.SetTexture( 0u, image );
101
102   TextureSet textureSetCopy(textureSet);
103
104   DALI_TEST_CHECK( textureSetCopy );
105
106   END_TEST;
107 }
108
109 int UtcDaliTextureSetAssignmentOperator(void)
110 {
111   TestApplication application;
112   TextureSet textureSet = TextureSet::New();
113
114   TextureSet textureSet2;
115   DALI_TEST_CHECK( !textureSet2 );
116
117   textureSet2 = textureSet;
118   DALI_TEST_CHECK( textureSet2 );
119
120   END_TEST;
121 }
122
123 int UtcDaliTextureSetDownCast01(void)
124 {
125   TestApplication application;
126   TextureSet textureSet = TextureSet::New();
127
128   BaseHandle handle(textureSet);
129   TextureSet textureSet2 = TextureSet::DownCast(handle);
130   DALI_TEST_CHECK( textureSet2 );
131
132   END_TEST;
133 }
134
135 int UtcDaliTextureSetDownCast02(void)
136 {
137   TestApplication application;
138
139   Handle handle = Handle::New(); // Create a custom object
140   TextureSet textureSet = TextureSet::DownCast(handle);
141   DALI_TEST_CHECK( !textureSet );
142   END_TEST;
143 }
144
145
146 int UtcDaliTextureSetTexture01(void)
147 {
148   TestApplication application;
149
150   Actor actor = CreateActor( DONT_SET_SAMPLER );
151
152   application.GetScene().Add( actor );
153
154   TestGlAbstraction& gl = application.GetGlAbstraction();
155
156   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
157   texParameterTrace.Reset();
158   texParameterTrace.Enable( true );
159   application.SendNotification();
160   application.Render();
161
162   int textureUnit=-1;
163   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
164   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
165
166   texParameterTrace.Enable( false );
167
168   // Verify gl state
169   // There are four calls to TexParameteri when the texture is first created
170   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
171   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
172
173   END_TEST;
174 }
175
176 int UtcDaliTextureSetTexture02(void)
177 {
178   TestApplication application;
179
180   Actor actor = CreateActor(SET_SAMPLER);
181
182   application.GetScene().Add( actor );
183
184   TestGlAbstraction& gl = application.GetGlAbstraction();
185
186   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
187   texParameterTrace.Reset();
188   texParameterTrace.Enable( true );
189   application.SendNotification();
190   application.Render();
191
192   int textureUnit=-1;
193   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
194   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
195
196   texParameterTrace.Enable( false );
197
198   // Verify gl state
199   // There are four calls to TexParameteri when the texture is first created
200   // Texture minification and magnification filters are now different than default so
201   //there should have been two extra TexParameteri calls to set the new filter mode
202   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 6, TEST_LOCATION);
203
204   END_TEST;
205 }
206
207 int UtcDaliTextureSetMultiple(void)
208 {
209   TestApplication application;
210
211   Actor actor1 = CreateActor(SET_SAMPLER);
212   Actor actor2 = CreateActor(SET_SAMPLER);
213
214   application.GetScene().Add( actor1 );
215   application.GetScene().Add( actor2 );
216
217   TestGlAbstraction& gl = application.GetGlAbstraction();
218
219   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
220   texParameterTrace.Reset();
221   texParameterTrace.Enable( true );
222   application.SendNotification();
223   application.Render();
224
225   int textureUnit=-1;
226   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
227   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
228
229   texParameterTrace.Enable( false );
230
231   // Verify gl state
232   // For each actor there are four calls to TexParameteri when the texture is first created
233   // Texture minification and magnification filters are now different than default so
234   //there should have been two extra TexParameteri calls to set the new filter mode
235   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2 * 6, TEST_LOCATION);
236
237   END_TEST;
238 }
239
240 int UtcDaliTextureSetSetSampler(void)
241 {
242   TestApplication application;
243
244   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
245
246   Shader shader = CreateShader();
247   TextureSet textureSet = CreateTextureSet( image );
248
249   Geometry geometry = CreateQuadGeometry();
250   Renderer renderer = Renderer::New( geometry, shader );
251   renderer.SetTextures( textureSet );
252
253   Actor actor = Actor::New();
254   actor.AddRenderer(renderer);
255   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
256   actor.SetProperty( Actor::Property::SIZE, Vector2( 400.0f, 400.0f ) );
257
258   application.GetScene().Add( actor );
259
260   TestGlAbstraction& gl = application.GetGlAbstraction();
261
262   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
263   texParameterTrace.Reset();
264   texParameterTrace.Enable( true );
265   application.SendNotification();
266   application.Render();
267
268   int textureUnit=-1;
269   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
270   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
271
272   texParameterTrace.Enable( false );
273
274   // Verify gl state
275   // There are 4 calls to TexParameteri when the texture is first created
276   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
277   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
278
279   texParameterTrace.Reset();
280   texParameterTrace.Enable( true );
281
282   Sampler sampler = Sampler::New();
283   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
284   textureSet.SetSampler( 0u, sampler );
285
286
287   application.SendNotification();
288   application.Render();
289
290   texParameterTrace.Enable( false );
291
292   // Verify gl state
293   //There should have been two calls to TexParameteri to set the new filtering mode
294   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
295
296
297   END_TEST;
298 }
299
300 int UtcDaliTextureSetGetTexture(void)
301 {
302   TestApplication application;
303
304   TextureSet textureSet = CreateTextureSet();
305   DALI_TEST_EQUALS( textureSet.GetTexture(0), Texture(), TEST_LOCATION );
306   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
307   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
308
309   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
310   textureSet.SetTexture( 0u, texture );
311
312   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
313   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
314   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
315
316   textureSet.SetTexture( 2u, texture );
317   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
318   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
319   DALI_TEST_EQUALS( textureSet.GetTexture(2), texture, TEST_LOCATION );
320
321   textureSet.SetTexture( 2u, Texture() );
322   DALI_TEST_EQUALS( textureSet.GetTexture(0), texture, TEST_LOCATION );
323   DALI_TEST_EQUALS( textureSet.GetTexture(1), Texture(), TEST_LOCATION );
324   DALI_TEST_EQUALS( textureSet.GetTexture(2), Texture(), TEST_LOCATION );
325
326   END_TEST;
327 }
328
329 int UtcDaliTextureSetGetSampler(void)
330 {
331   TestApplication application;
332
333   TextureSet textureSet = CreateTextureSet();
334   DALI_TEST_EQUALS( textureSet.GetSampler(0), Sampler(), TEST_LOCATION );
335   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
336   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
337
338   Sampler sampler = Sampler::New();
339   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
340   textureSet.SetSampler( 0u, sampler );
341
342   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
343   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
344   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
345
346   textureSet.SetSampler( 2u, sampler );
347   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
348   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
349   DALI_TEST_EQUALS( textureSet.GetSampler(2), sampler, TEST_LOCATION );
350
351   textureSet.SetSampler( 2u, Sampler() );
352   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
353   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
354   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
355
356   END_TEST;
357 }
358
359 int UtcDaliTextureSetGetTextureCount0(void)
360 {
361   TestApplication application;
362
363   TextureSet textureSet = CreateTextureSet();
364   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
365
366   Texture image = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
367   textureSet.SetTexture( 0u, image );
368   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
369
370   textureSet.SetTexture( 1u, image );
371   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
372
373   Sampler sampler = Sampler::New();
374   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
375   textureSet.SetSampler( 2u, sampler );
376   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
377
378   textureSet.SetTexture( 2u, image );
379   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
380   DALI_TEST_EQUALS( textureSet.GetSampler(2u), sampler, TEST_LOCATION );
381
382   END_TEST;
383 }
384
385 int UtcDaliTextureSetGetTextureCount1(void)
386 {
387   TestApplication application;
388
389   TextureSet textureSet = CreateTextureSet();
390   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
391
392   Texture texture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64 );
393   textureSet.SetTexture( 0u, texture );
394   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
395
396   textureSet.SetTexture( 1u, texture );
397   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
398
399   Sampler sampler = Sampler::New();
400   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
401   textureSet.SetSampler( 2u, sampler );
402   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
403
404   textureSet.SetTexture( 2u, texture );
405   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
406   DALI_TEST_EQUALS( textureSet.GetSampler(2u), sampler, TEST_LOCATION );
407
408
409   END_TEST;
410 }