License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / utc-Dali-ShaderEffect.cpp
1 /*
2  * Copyright (c) 2014 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 <iostream>
19
20 #include <stdlib.h>
21 #include <dali/dali.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 namespace
27 {
28
29 static const char* VertexSource =
30 "void main()\n"
31 "{\n"
32 "  gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
33 "  vTexCoord = aTexCoord;\n"
34 "}\n";
35
36 static const char* FragmentSource =
37 "void main()\n"
38 "{\n"
39 "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
40 "}\n";
41
42 const int GETSOURCE_BUFFER_SIZE = 0x10000;
43
44 static const char* TestImageFilename = "icon_wrt.png";
45
46 } // Anonymous namespace
47
48
49 int UtcDaliShaderEffectFromProperties01(void)
50 {
51   TestApplication application;
52   tet_infoline("UtcDaliShaderEffectFromProperties01()");
53
54   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
55   std::string vertexShaderPrefix = "#define TEST_VS 1";
56
57   // Call render to compile default shaders.
58   application.SendNotification();
59   application.Render();
60   application.Render();
61   application.Render();
62
63   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
64
65   // create from type registry
66
67   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
68   DALI_TEST_CHECK( typeInfo );
69   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
70   DALI_TEST_CHECK( effect );
71
72   Property::Value programMap = Property::Value(Property::MAP);
73
74   programMap.SetValue("vertex", std::string(VertexSource));
75   programMap.SetValue("fragment", std::string(FragmentSource));
76
77   programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
78   programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
79
80   programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
81
82   effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
83
84   Property::Value imageMap = Property::Value(Property::MAP);
85   imageMap.SetValue("filename", Property::Value(TestImageFilename));
86
87   effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
88
89   BitmapImage image(CreateBitmapImage());
90
91   ImageActor actor = ImageActor::New( image );
92   actor.SetSize( 100.0f, 100.0f );
93   actor.SetName("TestImageFilenameActor");
94   actor.SetShaderEffect(effect);
95   Stage::GetCurrent().Add(actor);
96
97   application.SendNotification();
98   application.Render();
99   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
100   bool testResult = false;
101
102   // we should have compiled 4 shaders.
103   DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
104   if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
105   {
106     char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
107     char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
108
109     // we are interested in the first two.
110     GLuint vertexShaderId = lastShaderCompiledBefore + 1;
111     GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
112
113     GLsizei lengthVertexResult;
114     GLsizei lengthFragmentResult;
115
116     application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
117     application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
118
119     int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
120     int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
121     testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
122   }
123   DALI_TEST_CHECK(testResult);
124   END_TEST;
125 }
126
127 int UtcDaliShaderEffectFromProperties02(void)
128 {
129   try
130   {
131     TestApplication application;
132     tet_infoline("UtcDaliShaderEffectFromProperties02()");
133
134     // Call render to compile default shaders.
135     application.SendNotification();
136     application.Render();
137     application.Render();
138     application.Render();
139
140     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
141     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
142     DALI_TEST_CHECK( typeInfo );
143     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
144     DALI_TEST_CHECK( effect );
145
146     Property::Value programMap = Property::Value(Property::MAP);
147
148     programMap.SetValue("vertex",   std::string(VertexSource));
149     programMap.SetValue("fragment", std::string(FragmentSource));
150
151     // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
152     // dont set by value
153     programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
154
155     effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
156
157     tet_result( TET_FAIL );
158   }
159   catch(Dali::DaliException& e)
160   {
161     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
162   }
163   END_TEST;
164 }
165
166 int UtcDaliShaderEffectFromProperties03(void)
167 {
168   try
169   {
170     TestApplication application;
171     tet_infoline("UtcDaliShaderEffectFromProperties03()");
172
173     // Call render to compile default shaders.
174     application.SendNotification();
175     application.Render();
176     application.Render();
177     application.Render();
178
179     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
180     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
181     DALI_TEST_CHECK( typeInfo );
182     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
183     DALI_TEST_CHECK( effect );
184
185     // dont set unknown
186     effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
187
188     tet_result( TET_FAIL );
189   }
190   catch(Dali::DaliException& e)
191   {
192     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
193   }
194   END_TEST;
195 }