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 "control-renderer-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>
40 const char * const CUSTOM_SHADER( "shader" );
41 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
42 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
43 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
44 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
45 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type STRING for a hint from the below hint strings or an ARRAY of of hint strings
48 * where hints should be contain strings of the following shader hints:
49 * "none" | corresponds to HINT_NONE
50 * "outputIsTransparent" | corresponds to HINT_OUTPUT_IS_TRANSPARENT
51 * "modifiesGeometry" | corresponds to HINT_MODIFIES_GEOMETRY
54 Shader::ShaderHints HintFromString( std::string hintString )
56 if( hintString == "none" )
58 return Shader::HINT_NONE;
60 else if( hintString == "outputIsTransparent" )
62 return Shader::HINT_OUTPUT_IS_TRANSPARENT;
64 else if( hintString == "modifiesGeometry" )
66 return Shader::HINT_MODIFIES_GEOMETRY;
70 DALI_LOG_ERROR( "'%s' hint string is not recognised", hintString.c_str() );
73 return Shader::HINT_NONE;
78 Internal::ControlRenderer::Impl::Impl()
79 : mCustomShader(NULL),
85 Internal::ControlRenderer::Impl::~Impl()
90 Internal::ControlRenderer::Impl::CustomShader::CustomShader( const Property::Map& map )
92 mHints( Shader::HINT_NONE )
94 SetPropertyMap( map );
97 void Internal::ControlRenderer::Impl::CustomShader::SetPropertyMap( const Property::Map& shaderMap )
99 mVertexShader.clear();
100 mFragmentShader.clear();
101 mGridSize = ImageDimensions( 1, 1 );
102 mHints = Shader::HINT_NONE;
104 Property::Value* vertexShaderValue = shaderMap.Find( CUSTOM_VERTEX_SHADER );
105 if( vertexShaderValue )
107 if( !vertexShaderValue->Get( mVertexShader ) )
109 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_VERTEX_SHADER );
113 Property::Value* fragmentShaderValue = shaderMap.Find( CUSTOM_FRAGMENT_SHADER );
114 if( fragmentShaderValue )
116 if( !fragmentShaderValue->Get( mFragmentShader ) )
118 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_FRAGMENT_SHADER );
122 Property::Value* subdivideXValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_X );
123 if( subdivideXValue )
126 if( !subdivideXValue->Get( subdivideX ) || subdivideX < 1 )
128 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_X );
132 mGridSize = ImageDimensions( subdivideX, mGridSize.GetY() );
136 Property::Value* subdivideYValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_Y );
137 if( subdivideYValue )
140 if( !subdivideYValue->Get( subdivideY ) || subdivideY < 1 )
142 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_Y );
146 mGridSize = ImageDimensions( mGridSize.GetX(), subdivideY );
150 Property::Value* hintsValue = shaderMap.Find( CUSTOM_SHADER_HINTS );
153 std::string hintString;
154 Property::Array hintsArray;
156 if( hintsValue->Get( hintString ) )
158 mHints = HintFromString( hintString );
160 else if( hintsValue->Get( hintsArray ) )
162 int hints = Shader::HINT_NONE;
163 for( Property::Array::SizeType i = 0; i < hintsArray.Count(); ++i)
165 Property::Value hintValue = hintsArray[ i ];
166 if( hintValue.Get( hintString ) )
168 hints |= static_cast< int >( HintFromString( hintString ) );
172 DALI_LOG_ERROR( "'%s' parameter does not correctly specify an hint string at index %d", CUSTOM_SHADER_HINTS, i );
175 mHints = static_cast< Shader::ShaderHints >( hints );
180 DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint string or an array of hint strings", CUSTOM_SHADER_HINTS );
185 void Internal::ControlRenderer::Impl::CustomShader::CreatePropertyMap( Property::Map& map ) const
187 if( !mVertexShader.empty() || !mFragmentShader.empty() )
189 Property::Map customShader;
190 if( !mVertexShader.empty() )
192 customShader.Insert(CUSTOM_VERTEX_SHADER, mVertexShader );
194 if( !mFragmentShader.empty() )
196 customShader.Insert(CUSTOM_FRAGMENT_SHADER, mFragmentShader );
199 if( mGridSize.GetWidth() != 1 )
201 customShader.Insert(CUSTOM_SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
203 if( mGridSize.GetHeight() != 1 )
205 customShader.Insert(CUSTOM_SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
208 if( mHints != Dali::Shader::HINT_NONE )
210 customShader.Insert(CUSTOM_SHADER_HINTS, static_cast< int >(mHints) );
213 map.Insert( CUSTOM_SHADER, customShader );
217 } // namespace Internal
219 } // namespace Toolkit