2 * Copyright (c) 2015 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.
19 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/property-array.h>
38 const char * const CUSTOM_SHADER( "shader" );
39 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
40 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
41 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
42 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
43 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type STRING for a hint from the below hint strings or an ARRAY of of hint strings
46 * where hints should be contain strings of the following shader hints:
47 * "none" | corresponds to Shader::Hint::NONE
48 * "outputIsTransparent" | corresponds to Shader::Hint::OUTPUT_IS_TRANSPARENT
49 * "modifiesGeometry" | corresponds to Shader::Hint::MODIFIES_GEOMETRY
52 Shader::Hint::Value HintFromString( std::string hintString )
54 if( hintString == "none" )
56 return Shader::Hint::NONE;
58 else if( hintString == "outputIsTransparent" )
60 return Shader::Hint::OUTPUT_IS_TRANSPARENT;
62 else if( hintString == "modifiesGeometry" )
64 return Shader::Hint::MODIFIES_GEOMETRY;
68 DALI_LOG_ERROR( "'%s' hint string is not recognised", hintString.c_str() );
71 return Shader::Hint::NONE;
76 Internal::Visual::Base::Impl::Impl()
77 : mCustomShader(NULL),
83 Internal::Visual::Base::Impl::~Impl()
88 Internal::Visual::Base::Impl::CustomShader::CustomShader( const Property::Map& map )
90 mHints( Shader::Hint::NONE )
92 SetPropertyMap( map );
95 void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property::Map& shaderMap )
97 mVertexShader.clear();
98 mFragmentShader.clear();
99 mGridSize = ImageDimensions( 1, 1 );
100 mHints = Shader::Hint::NONE;
102 Property::Value* vertexShaderValue = shaderMap.Find( CUSTOM_VERTEX_SHADER );
103 if( vertexShaderValue )
105 if( !vertexShaderValue->Get( mVertexShader ) )
107 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_VERTEX_SHADER );
111 Property::Value* fragmentShaderValue = shaderMap.Find( CUSTOM_FRAGMENT_SHADER );
112 if( fragmentShaderValue )
114 if( !fragmentShaderValue->Get( mFragmentShader ) )
116 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_FRAGMENT_SHADER );
120 Property::Value* subdivideXValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_X );
121 if( subdivideXValue )
124 if( !subdivideXValue->Get( subdivideX ) || subdivideX < 1 )
126 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_X );
130 mGridSize = ImageDimensions( subdivideX, mGridSize.GetY() );
134 Property::Value* subdivideYValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_Y );
135 if( subdivideYValue )
138 if( !subdivideYValue->Get( subdivideY ) || subdivideY < 1 )
140 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_Y );
144 mGridSize = ImageDimensions( mGridSize.GetX(), subdivideY );
148 Property::Value* hintsValue = shaderMap.Find( CUSTOM_SHADER_HINTS );
151 std::string hintString;
152 Property::Array hintsArray;
154 if( hintsValue->Get( hintString ) )
156 mHints = HintFromString( hintString );
158 else if( hintsValue->Get( hintsArray ) )
160 int hints = Shader::Hint::NONE;
161 for( Property::Array::SizeType i = 0; i < hintsArray.Count(); ++i)
163 Property::Value hintValue = hintsArray[ i ];
164 if( hintValue.Get( hintString ) )
166 hints |= static_cast< int >( HintFromString( hintString ) );
170 DALI_LOG_ERROR( "'%s' parameter does not correctly specify an hint string at index %d", CUSTOM_SHADER_HINTS, i );
173 mHints = static_cast< Shader::Hint::Value >( hints );
178 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint string or an array of hint strings", CUSTOM_SHADER_HINTS );
183 void Internal::Visual::Base::Impl::CustomShader::CreatePropertyMap( Property::Map& map ) const
185 if( !mVertexShader.empty() || !mFragmentShader.empty() )
187 Property::Map customShader;
188 if( !mVertexShader.empty() )
190 customShader.Insert(CUSTOM_VERTEX_SHADER, mVertexShader );
192 if( !mFragmentShader.empty() )
194 customShader.Insert(CUSTOM_FRAGMENT_SHADER, mFragmentShader );
197 if( mGridSize.GetWidth() != 1 )
199 customShader.Insert(CUSTOM_SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
201 if( mGridSize.GetHeight() != 1 )
203 customShader.Insert(CUSTOM_SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
206 if( mHints != Dali::Shader::Hint::NONE )
208 customShader.Insert(CUSTOM_SHADER_HINTS, static_cast< int >(mHints) );
211 map.Insert( CUSTOM_SHADER, customShader );
215 } // namespace Internal
217 } // namespace Toolkit