2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
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";
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";
33 const int GETSOURCE_BUFFER_SIZE = 0x10000;
35 static const char* TestImageFilename = "icon_wrt.png";
37 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor )
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 );
43 memset( pixbuffer, initialColor , imageHeight*imageWidth*bytesPerPixel);
48 } // Anonymous namespace
51 int UtcDaliShaderEffectFromProperties01(void)
53 TestApplication application;
54 tet_infoline("UtcDaliShaderEffectFromProperties01()");
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);
61 // Call render to compile default shaders.
62 application.SendNotification();
65 GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
67 // create from type registry
69 TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
70 DALI_TEST_CHECK( typeInfo );
71 ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
72 DALI_TEST_CHECK( effect );
74 Property::Value programMap = Property::Value(Property::MAP);
76 programMap.SetValue("vertex", vertexShader);
77 programMap.SetValue("fragment", fragmentShader);
79 programMap.SetValue("vertex-prefix", vertexShaderPrefix);
80 programMap.SetValue("fragment-prefix", fragmentShaderPrefix);
82 programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
84 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
86 Property::Value imageMap = Property::Value(Property::MAP);
87 imageMap.SetValue("filename", Property::Value(TestImageFilename));
88 effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
90 // do a update & render to get the image request
91 application.SendNotification();
94 Integration::ResourceRequest* request = application.GetPlatform().GetRequest();
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);
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);
108 application.SendNotification();
109 application.Render();
110 GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
112 // we should have compiled 2 shaders.
113 DALI_TEST_EQUALS(lastShaderCompiledAfter, lastShaderCompiledBefore + 2, TEST_LOCATION );
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 );
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 );
126 int UtcDaliShaderEffectFromProperties02(void)
130 TestApplication application;
131 tet_infoline("UtcDaliShaderEffectFromProperties02()");
133 // Call render to compile default shaders.
134 application.SendNotification();
135 application.Render();
136 application.Render();
137 application.Render();
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 );
145 Property::Value programMap = Property::Value(Property::MAP);
147 programMap.SetValue("vertex", std::string(VertexSource));
148 programMap.SetValue("fragment", std::string(FragmentSource));
150 // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
152 programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
154 effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
156 tet_result( TET_FAIL );
158 catch(Dali::DaliException& e)
160 DALI_TEST_PRINT_ASSERT( e );
165 int UtcDaliShaderEffectFromProperties03(void)
169 TestApplication application;
170 tet_infoline("UtcDaliShaderEffectFromProperties03()");
172 // Call render to compile default shaders.
173 application.SendNotification();
174 application.Render();
175 application.Render();
176 application.Render();
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 );
185 effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
187 tet_result( TET_FAIL );
189 catch(Dali::DaliException& e)
191 DALI_TEST_PRINT_ASSERT( e );