252ec9d61f55e079bfb884757d997c7ad105ed3c
[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/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 namespace
27 {
28
29 static const char* VertexSource = "VertexSource: this can be whatever you want it to be, but don't make it exact the same as default shader\n";
30
31 static const char* FragmentSource = "FragmentSource: this can be whatever you want it to be, but don't make it exact the same as default shader\n";
32
33 const int GETSOURCE_BUFFER_SIZE = 0x10000;
34
35 static const char* TestImageFilename = "icon_wrt.png";
36
37 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
38 {
39   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
40   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888,  imageWidth,imageHeight,imageWidth,imageHeight );
41   unsigned int bytesPerPixel = GetBytesPerPixel(  Pixel::RGBA8888 );
42
43   memset( pixbuffer,  initialColor , imageHeight*imageWidth*bytesPerPixel);
44
45   return bitmap;
46 }
47
48 } // Anonymous namespace
49
50
51 int UtcDaliShaderEffectFromProperties01(void)
52 {
53   TestApplication application;
54   tet_infoline("UtcDaliShaderEffectFromProperties01()");
55
56   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
57   std::string vertexShaderPrefix = "#define TEST_VS 1";
58   std::string vertexShader(VertexSource);
59   std::string fragmentShader(FragmentSource);
60
61   // Call render to compile default shaders.
62   application.SendNotification();
63   application.Render();
64
65   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
66
67   // create from type registry
68
69   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
70   DALI_TEST_CHECK( typeInfo );
71   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
72   DALI_TEST_CHECK( effect );
73
74   Property::Value programMap = Property::Value(Property::MAP);
75
76   programMap.SetValue("vertex", vertexShader);
77   programMap.SetValue("fragment", fragmentShader);
78
79   programMap.SetValue("vertex-prefix", vertexShaderPrefix);
80   programMap.SetValue("fragment-prefix", fragmentShaderPrefix);
81
82   programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
83
84   effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
85
86   Property::Value imageMap = Property::Value(Property::MAP);
87   imageMap.SetValue("filename", Property::Value(TestImageFilename));
88   effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
89
90   // do a update & render to get the image request
91   application.SendNotification();
92   application.Render();
93
94   Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
95   // create the image
96   Integration::Bitmap* bitmap = CreateBitmap( 10, 10, 0xFF );
97   Integration::ResourcePointer resourcePtr(bitmap);
98   TestPlatformAbstraction& platform = application.GetPlatform();
99   platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resourcePtr);
100
101   BitmapImage image(CreateBitmapImage());
102   ImageActor actor = ImageActor::New( image );
103   actor.SetSize( 100.0f, 100.0f );
104   actor.SetName("TestImageFilenameActor");
105   actor.SetShaderEffect(effect);
106   Stage::GetCurrent().Add(actor);
107
108   application.SendNotification();
109   application.Render();
110   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
111
112   // we should have compiled 2 shaders.
113   DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
114
115   std::string actualVertexShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 1 );
116   DALI_TEST_EQUALS( vertexShaderPrefix, actualVertexShader.substr( 0, vertexShaderPrefix.length() ), TEST_LOCATION );
117   DALI_TEST_EQUALS( vertexShader, actualVertexShader.substr( actualVertexShader.length() - vertexShader.length() ), TEST_LOCATION );
118
119   std::string actualFragmentShader = application.GetGlAbstraction().GetShaderSource( lastShaderCompiledBefore + 2 );
120   DALI_TEST_EQUALS( fragmentShaderPrefix, actualFragmentShader.substr( 0, fragmentShaderPrefix.length() ), TEST_LOCATION );
121   DALI_TEST_EQUALS( fragmentShader, actualFragmentShader.substr( actualFragmentShader.length() - fragmentShader.length() ), TEST_LOCATION );
122
123   END_TEST;
124 }
125
126 int UtcDaliShaderEffectFromProperties02(void)
127 {
128   try
129   {
130     TestApplication application;
131     tet_infoline("UtcDaliShaderEffectFromProperties02()");
132
133     // Call render to compile default shaders.
134     application.SendNotification();
135     application.Render();
136     application.Render();
137     application.Render();
138
139     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
140     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
141     DALI_TEST_CHECK( typeInfo );
142     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
143     DALI_TEST_CHECK( effect );
144
145     Property::Value programMap = Property::Value(Property::MAP);
146
147     programMap.SetValue("vertex",   std::string(VertexSource));
148     programMap.SetValue("fragment", std::string(FragmentSource));
149
150     // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
151     // dont set by value
152     programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
153
154     effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
155
156     tet_result( TET_FAIL );
157   }
158   catch(Dali::DaliException& e)
159   {
160     DALI_TEST_PRINT_ASSERT( e );
161   }
162   END_TEST;
163 }
164
165 int UtcDaliShaderEffectFromProperties03(void)
166 {
167   try
168   {
169     TestApplication application;
170     tet_infoline("UtcDaliShaderEffectFromProperties03()");
171
172     // Call render to compile default shaders.
173     application.SendNotification();
174     application.Render();
175     application.Render();
176     application.Render();
177
178     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
179     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
180     DALI_TEST_CHECK( typeInfo );
181     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
182     DALI_TEST_CHECK( effect );
183
184     // dont set unknown
185     effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
186
187     tet_result( TET_FAIL );
188   }
189   catch(Dali::DaliException& e)
190   {
191     DALI_TEST_PRINT_ASSERT( e );
192   }
193   END_TEST;
194 }