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