Wired up material API for blending options
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Material.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 void material_test_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void material_test_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliMaterialNew01(void)
36 {
37   TestApplication application;
38
39   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
40   Material material = Material::New(shader);
41
42   DALI_TEST_EQUALS( (bool)material, true, TEST_LOCATION );
43   END_TEST;
44 }
45
46 int UtcDaliMaterialNew02(void)
47 {
48   TestApplication application;
49   Material material;
50   DALI_TEST_EQUALS( (bool)material, false, TEST_LOCATION );
51   END_TEST;
52 }
53
54 int UtcDaliMaterialDownCast01(void)
55 {
56   TestApplication application;
57   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
58   Material material = Material::New(shader);
59
60   BaseHandle handle(material);
61   Material material2 = Material::DownCast(handle);
62   DALI_TEST_EQUALS( (bool)material2, true, TEST_LOCATION );
63   END_TEST;
64 }
65
66 int UtcDaliMaterialDownCast02(void)
67 {
68   TestApplication application;
69
70   Handle handle = Handle::New(); // Create a custom object
71   Material material = Material::DownCast(handle);
72   DALI_TEST_EQUALS( (bool)material, false, TEST_LOCATION );
73   END_TEST;
74 }
75
76
77
78 int UtcDaliMaterialBlendingOptions01(void)
79 {
80   TestApplication application;
81
82   tet_infoline("Test SetBlendFunc(src, dest) ");
83
84   Geometry geometry = CreateQuadGeometry();
85   Material material = CreateMaterial(0.5f);
86   Renderer renderer = Renderer::New( geometry, material );
87
88   Actor actor = Actor::New();
89   actor.AddRenderer(renderer);
90   actor.SetSize(400, 400);
91   Stage::GetCurrent().Add(actor);
92
93   material.SetBlendFunc(BlendingFactor::ONE_MINUS_SRC_COLOR, BlendingFactor::SRC_ALPHA_SATURATE);
94
95   // Test that Set was successful:
96   {
97     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
98     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
99     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
100     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
101     material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
102
103     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorRgb,    TEST_LOCATION );
104     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorRgb,   TEST_LOCATION );
105     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorAlpha,  TEST_LOCATION );
106     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorAlpha, TEST_LOCATION );
107   }
108
109   application.SendNotification();
110   application.Render();
111
112   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
113
114   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
115   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
116   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
117   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
118
119   END_TEST;
120 }
121
122 int UtcDaliMaterialBlendingOptions02(void)
123 {
124   TestApplication application;
125
126   tet_infoline("Test SetBlendFunc(srcRgb, destRgb, srcAlpha, destAlpha) ");
127
128   Geometry geometry = CreateQuadGeometry();
129   Material material = CreateMaterial(0.5f);
130   Renderer renderer = Renderer::New( geometry, material );
131
132   Actor actor = Actor::New();
133   actor.AddRenderer(renderer);
134   actor.SetSize(400, 400);
135   Stage::GetCurrent().Add(actor);
136
137   material.SetBlendFunc( BlendingFactor::CONSTANT_COLOR, BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
138                          BlendingFactor::CONSTANT_ALPHA, BlendingFactor::ONE_MINUS_CONSTANT_ALPHA );
139
140   // Test that Set was successful:
141   {
142     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
143     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
144     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
145     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
146     material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
147
148     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_COLOR,            srcFactorRgb,    TEST_LOCATION );
149     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_COLOR,  destFactorRgb,   TEST_LOCATION );
150     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_ALPHA,            srcFactorAlpha,  TEST_LOCATION );
151     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_ALPHA,  destFactorAlpha, TEST_LOCATION );
152   }
153
154   application.SendNotification();
155   application.Render();
156
157   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
158   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_COLOR,           glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
159   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_COLOR, glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
160   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_ALPHA,           glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
161   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_ALPHA, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
162
163   END_TEST;
164 }
165
166
167
168 int UtcDaliMaterialBlendingOptions03(void)
169 {
170   TestApplication application;
171
172   tet_infoline("Test GetBlendEquation() defaults ");
173
174   Geometry geometry = CreateQuadGeometry();
175   Material material = CreateMaterial(0.5f);
176   Renderer renderer = Renderer::New( geometry, material );
177
178   Actor actor = Actor::New();
179   actor.AddRenderer(renderer);
180   actor.SetSize(400, 400);
181   Stage::GetCurrent().Add(actor);
182
183   // Test the defaults as documented int blending.h
184   {
185     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
186     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
187     material.GetBlendEquation( equationRgb, equationAlpha );
188     DALI_TEST_EQUALS( BlendingEquation::ADD, equationRgb, TEST_LOCATION );
189     DALI_TEST_EQUALS( BlendingEquation::ADD, equationAlpha, TEST_LOCATION );
190   }
191
192   END_TEST;
193 }
194
195
196 int UtcDaliMaterialBlendingOptions04(void)
197 {
198   TestApplication application;
199
200   tet_infoline("Test SetBlendEquation() ");
201
202   Geometry geometry = CreateQuadGeometry();
203   Material material = CreateMaterial(0.5f);
204   Renderer renderer = Renderer::New( geometry, material );
205
206   Actor actor = Actor::New();
207   actor.AddRenderer(renderer);
208   actor.SetSize(400, 400);
209   Stage::GetCurrent().Add(actor);
210
211   // Test the single blending equation setting
212   {
213     material.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT );
214     BlendingEquation::Type equationRgba( BlendingEquation::SUBTRACT );
215     material.GetBlendEquation( equationRgba, equationRgba );
216     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgba, TEST_LOCATION );
217   }
218
219   material.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT, BlendingEquation::REVERSE_SUBTRACT );
220
221   // Test that Set was successful
222   {
223     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
224     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
225     material.GetBlendEquation( equationRgb, equationAlpha );
226     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgb, TEST_LOCATION );
227     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationAlpha, TEST_LOCATION );
228   }
229
230   // Render & check GL commands
231   application.SendNotification();
232   application.Render();
233
234   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
235   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationRgb(),   TEST_LOCATION );
236   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationAlpha(), TEST_LOCATION );
237
238   END_TEST;
239 }
240
241 int UtcDaliMaterialSetBlendMode01(void)
242 {
243   TestApplication application;
244
245   tet_infoline("Test setting the blend mode to on with an opaque color renders with blending enabled");
246
247   Geometry geometry = CreateQuadGeometry();
248   Material material = CreateMaterial(1.0f);
249   Renderer renderer = Renderer::New( geometry, material );
250
251   Actor actor = Actor::New();
252   actor.AddRenderer(renderer);
253   actor.SetSize(400, 400);
254   Stage::GetCurrent().Add(actor);
255
256   material.SetBlendMode(BlendingMode::ON);
257
258   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
259   glAbstraction.EnableCullFaceCallTrace(true);
260
261   application.SendNotification();
262   application.Render();
263
264   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
265   std::ostringstream blendStr;
266   blendStr << GL_BLEND;
267   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
268
269   END_TEST;
270 }
271
272
273 int UtcDaliMaterialSetBlendMode02(void)
274 {
275   TestApplication application;
276
277   tet_infoline("Test setting the blend mode to off with a transparent color renders with blending disabled (and not enabled)");
278
279   Geometry geometry = CreateQuadGeometry();
280   Material material = CreateMaterial(0.5f);
281   Renderer renderer = Renderer::New( geometry, material );
282
283   Actor actor = Actor::New();
284   actor.AddRenderer(renderer);
285   actor.SetSize(400, 400);
286   Stage::GetCurrent().Add(actor);
287
288   material.SetBlendMode(BlendingMode::OFF);
289
290   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
291   glAbstraction.EnableCullFaceCallTrace(true);
292
293   application.SendNotification();
294   application.Render();
295
296   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
297   std::ostringstream blendStr;
298   blendStr << GL_BLEND;
299   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
300
301   END_TEST;
302 }
303
304 int UtcDaliMaterialSetBlendMode03(void)
305 {
306   TestApplication application;
307
308   tet_infoline("Test setting the blend mode to auto with a transparent color renders with blending enabled");
309
310   Geometry geometry = CreateQuadGeometry();
311   Material material = CreateMaterial(0.5f);
312   Renderer renderer = Renderer::New( geometry, material );
313
314   Actor actor = Actor::New();
315   actor.AddRenderer(renderer);
316   actor.SetSize(400, 400);
317   Stage::GetCurrent().Add(actor);
318
319   material.SetBlendMode(BlendingMode::AUTO);
320
321   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
322   glAbstraction.EnableCullFaceCallTrace(true);
323
324   application.SendNotification();
325   application.Render();
326
327   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
328   std::ostringstream blendStr;
329   blendStr << GL_BLEND;
330   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
331
332   END_TEST;
333 }
334
335 int UtcDaliMaterialSetBlendMode04(void)
336 {
337   TestApplication application;
338
339   tet_infoline("Test setting the blend mode to auto with an opaque color renders with blending disabled");
340
341   Geometry geometry = CreateQuadGeometry();
342   Material material = CreateMaterial(1.0f);
343   Renderer renderer = Renderer::New( geometry, material );
344
345   Actor actor = Actor::New();
346   actor.AddRenderer(renderer);
347   actor.SetSize(400, 400);
348   Stage::GetCurrent().Add(actor);
349
350   material.SetBlendMode(BlendingMode::AUTO);
351
352   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
353   glAbstraction.EnableCullFaceCallTrace(true);
354
355   application.SendNotification();
356   application.Render();
357
358   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
359   std::ostringstream blendStr;
360   blendStr << GL_BLEND;
361   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
362
363   END_TEST;
364 }
365
366 int UtcDaliMaterialSetBlendMode05(void)
367 {
368   TestApplication application;
369
370   tet_infoline("Test setting the blend mode to auto with an opaque color and an image with an alpha channel renders with blending enabled");
371
372   Geometry geometry = CreateQuadGeometry();
373   BufferImage image = BufferImage::New( 40, 40, Pixel::RGBA8888 );
374   Material material = CreateMaterial(1.0f, image);
375   Renderer renderer = Renderer::New( geometry, material );
376
377   Actor actor = Actor::New();
378   actor.AddRenderer(renderer);
379   actor.SetSize(400, 400);
380   Stage::GetCurrent().Add(actor);
381
382   material.SetBlendMode(BlendingMode::AUTO);
383
384   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
385   glAbstraction.EnableCullFaceCallTrace(true);
386
387   application.SendNotification();
388   application.Render();
389
390   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
391   std::ostringstream blendStr;
392   blendStr << GL_BLEND;
393   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
394
395   END_TEST;
396 }