Make TextureSet a non property owner
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-devel / utc-Dali-TextureSet.cpp
1 /*
2  * Copyright (c) 2015 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 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 namespace
26 {
27 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
28 {
29   current.b = 0.0f;
30 }
31 }
32
33
34 void texture_set_test_startup(void)
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 void texture_set_test_cleanup(void)
40 {
41   test_return_value = TET_PASS;
42 }
43
44 int UtcDaliTextureSetNew01(void)
45 {
46   TestApplication application;
47
48   TextureSet textureSet = TextureSet::New();
49
50   DALI_TEST_CHECK( textureSet );
51   END_TEST;
52 }
53
54 int UtcDaliTextureSetNew02(void)
55 {
56   TestApplication application;
57   TextureSet textureSet;
58   DALI_TEST_CHECK( !textureSet );
59   END_TEST;
60 }
61
62 int UtcDaliTextureSetCopyConstructor(void)
63 {
64   TestApplication application;
65
66   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
67   TextureSet textureSet = TextureSet::New();
68   textureSet.SetImage( 0u, image );
69
70   TextureSet textureSetCopy(textureSet);
71
72   DALI_TEST_CHECK( textureSetCopy );
73
74   END_TEST;
75 }
76
77 int UtcDaliTextureSetAssignmentOperator(void)
78 {
79   TestApplication application;
80   TextureSet textureSet = TextureSet::New();
81
82   TextureSet textureSet2;
83   DALI_TEST_CHECK( !textureSet2 );
84
85   textureSet2 = textureSet;
86   DALI_TEST_CHECK( textureSet2 );
87
88   END_TEST;
89 }
90
91 int UtcDaliTextureSetDownCast01(void)
92 {
93   TestApplication application;
94   TextureSet textureSet = TextureSet::New();
95
96   BaseHandle handle(textureSet);
97   TextureSet textureSet2 = TextureSet::DownCast(handle);
98   DALI_TEST_CHECK( textureSet2 );
99
100   END_TEST;
101 }
102
103 int UtcDaliTextureSetDownCast02(void)
104 {
105   TestApplication application;
106
107   Handle handle = Handle::New(); // Create a custom object
108   TextureSet textureSet = TextureSet::DownCast(handle);
109   DALI_TEST_CHECK( !textureSet );
110   END_TEST;
111 }
112
113 int UtcDaliTextureSetSetImage01(void)
114 {
115   TestApplication application;
116
117   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
118
119   Shader shader = CreateShader();
120   TextureSet textureSet = CreateTextureSet();
121   textureSet.SetImage( 0u, image );
122
123   Geometry geometry = CreateQuadGeometry();
124   Renderer renderer = Renderer::New( geometry, shader );
125   renderer.SetTextures( textureSet );
126
127   Actor actor = Actor::New();
128   actor.AddRenderer(renderer);
129   actor.SetParentOrigin( ParentOrigin::CENTER );
130   actor.SetSize(400, 400);
131
132   Stage::GetCurrent().Add( actor );
133
134   TestGlAbstraction& gl = application.GetGlAbstraction();
135
136   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
137   texParameterTrace.Reset();
138   texParameterTrace.Enable( true );
139   application.SendNotification();
140   application.Render();
141
142   int textureUnit=-1;
143   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
144   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
145
146   texParameterTrace.Enable( false );
147
148   // Verify gl state
149   // There are three calls to TexParameteri when the texture is first created
150   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
151   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
152
153   END_TEST;
154 }
155
156 int UtcDaliTextureSetSetImage02(void)
157 {
158   TestApplication application;
159
160   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
161
162   Shader shader = CreateShader();
163   TextureSet textureSet = CreateTextureSet();
164
165   Sampler sampler = Sampler::New();
166   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
167   textureSet.SetImage( 0u, image );
168   textureSet.SetSampler( 0u, sampler );
169
170   Geometry geometry = CreateQuadGeometry();
171   Renderer renderer = Renderer::New( geometry, shader );
172   renderer.SetTextures( textureSet );
173
174   Actor actor = Actor::New();
175   actor.AddRenderer(renderer);
176   actor.SetParentOrigin( ParentOrigin::CENTER );
177   actor.SetSize(400, 400);
178
179   Stage::GetCurrent().Add( actor );
180
181   TestGlAbstraction& gl = application.GetGlAbstraction();
182
183   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
184   texParameterTrace.Reset();
185   texParameterTrace.Enable( true );
186   application.SendNotification();
187   application.Render();
188
189   int textureUnit=-1;
190   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
191   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
192
193   texParameterTrace.Enable( false );
194
195   // Verify gl state
196   // There are three calls to TexParameteri when the texture is first created
197   // Texture minification and magnification filters are now different than default so
198   //there should have been two extra TexParameteri calls to set the new filter mode
199   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 4, TEST_LOCATION);
200
201   END_TEST;
202 }
203
204 int UtcDaliTextureSetSetSampler(void)
205 {
206   TestApplication application;
207
208   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
209
210   Shader shader = CreateShader();
211   TextureSet textureSet = CreateTextureSet( image );
212
213   Geometry geometry = CreateQuadGeometry();
214   Renderer renderer = Renderer::New( geometry, shader );
215   renderer.SetTextures( textureSet );
216
217   Actor actor = Actor::New();
218   actor.AddRenderer(renderer);
219   actor.SetParentOrigin( ParentOrigin::CENTER );
220   actor.SetSize(400, 400);
221
222   Stage::GetCurrent().Add( actor );
223
224   TestGlAbstraction& gl = application.GetGlAbstraction();
225
226   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
227   texParameterTrace.Reset();
228   texParameterTrace.Enable( true );
229   application.SendNotification();
230   application.Render();
231
232   int textureUnit=-1;
233   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture", textureUnit ) );
234   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
235
236   texParameterTrace.Enable( false );
237
238   // Verify gl state
239   // There are three calls to TexParameteri when the texture is first created
240   // as the texture is using default sampling parametrers there shouldn't be any more calls to TexParameteri
241   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
242
243   texParameterTrace.Reset();
244   texParameterTrace.Enable( true );
245
246   Sampler sampler = Sampler::New();
247   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
248   textureSet.SetSampler( 0u, sampler );
249
250
251   application.SendNotification();
252   application.Render();
253
254   texParameterTrace.Enable( false );
255
256   // Verify gl state
257   //There should have been two calls to TexParameteri to set the new filtering mode
258   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
259
260
261   END_TEST;
262 }
263
264 int UtcDaliTextureSetGetImage(void)
265 {
266   TestApplication application;
267
268   TextureSet textureSet = CreateTextureSet();
269   DALI_TEST_EQUALS( textureSet.GetImage(0), Image(), TEST_LOCATION );
270   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
271   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
272
273   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
274   textureSet.SetImage( 0u, image );
275
276   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
277   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
278   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
279
280   textureSet.SetImage( 2u, image );
281   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
282   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
283   DALI_TEST_EQUALS( textureSet.GetImage(2), image, TEST_LOCATION );
284
285   textureSet.SetImage( 2u, Image() );
286   DALI_TEST_EQUALS( textureSet.GetImage(0), image, TEST_LOCATION );
287   DALI_TEST_EQUALS( textureSet.GetImage(1), Image(), TEST_LOCATION );
288   DALI_TEST_EQUALS( textureSet.GetImage(2), Image(), TEST_LOCATION );
289
290   END_TEST;
291 }
292
293 int UtcDaliTextureSetGetSampler(void)
294 {
295   TestApplication application;
296
297   TextureSet textureSet = CreateTextureSet();
298   DALI_TEST_EQUALS( textureSet.GetSampler(0), Sampler(), TEST_LOCATION );
299   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
300   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
301
302   Sampler sampler = Sampler::New();
303   sampler.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
304   textureSet.SetSampler( 0u, sampler );
305
306   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
307   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
308   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
309
310   textureSet.SetSampler( 2u, sampler );
311   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
312   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
313   DALI_TEST_EQUALS( textureSet.GetSampler(2), sampler, TEST_LOCATION );
314
315   textureSet.SetSampler( 2u, Sampler() );
316   DALI_TEST_EQUALS( textureSet.GetSampler(0), sampler, TEST_LOCATION );
317   DALI_TEST_EQUALS( textureSet.GetSampler(1), Sampler(), TEST_LOCATION );
318   DALI_TEST_EQUALS( textureSet.GetSampler(2), Sampler(), TEST_LOCATION );
319
320   END_TEST;
321 }
322
323 int UtcDaliTextureSetGetTextureCount(void)
324 {
325   TestApplication application;
326
327   TextureSet textureSet = CreateTextureSet();
328   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 0u, TEST_LOCATION );
329
330   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
331   textureSet.SetImage( 0u, image );
332   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 1u, TEST_LOCATION );
333
334   textureSet.SetImage( 1u, image );
335   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 2u, TEST_LOCATION );
336
337   textureSet.SetSampler( 2u, Sampler::New() );
338   DALI_TEST_EQUALS( textureSet.GetTextureCount(), 3u, TEST_LOCATION );
339
340   END_TEST;
341 }