Merge "Geometry Batching" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-data-impl.cpp
1 /*
2  * Copyright (c) 2016 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 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 DALI_ENUM_TO_STRING_TABLE_BEGIN( SHADER_HINT )
44 DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, NONE )
45 DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, OUTPUT_IS_TRANSPARENT )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, MODIFIES_GEOMETRY )
47 DALI_ENUM_TO_STRING_TABLE_END( SHADER_HINT )
48
49 } // unnamed namespace
50
51 Internal::Visual::Base::Impl::Impl()
52 : mCustomShader(NULL),
53   mDepthIndex( 0.0f ),
54   mFlags( 0 )
55 {
56 }
57
58 Internal::Visual::Base::Impl::~Impl()
59 {
60   delete mCustomShader;
61 }
62
63 Internal::Visual::Base::Impl::CustomShader::CustomShader( const Property::Map& map )
64 : mGridSize( 1, 1 ),
65   mHints( Shader::Hint::NONE )
66 {
67   SetPropertyMap( map );
68 }
69
70 void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property::Map& shaderMap )
71 {
72   mVertexShader.clear();
73   mFragmentShader.clear();
74   mGridSize = ImageDimensions( 1, 1 );
75   mHints = Shader::Hint::NONE;
76
77   Property::Value* vertexShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::VERTEX_SHADER, CUSTOM_VERTEX_SHADER );
78   if( vertexShaderValue )
79   {
80     if( !vertexShaderValue->Get( mVertexShader ) )
81     {
82       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_VERTEX_SHADER );
83     }
84   }
85
86   Property::Value* fragmentShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, CUSTOM_FRAGMENT_SHADER );
87   if( fragmentShaderValue )
88   {
89     if( !fragmentShaderValue->Get( mFragmentShader ) )
90     {
91       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_FRAGMENT_SHADER );
92     }
93   }
94
95   Property::Value* subdivideXValue = shaderMap.Find( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X, CUSTOM_SUBDIVIDE_GRID_X );
96   if( subdivideXValue )
97   {
98     int subdivideX;
99     if( !subdivideXValue->Get( subdivideX ) || subdivideX < 1 )
100     {
101       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1\n", CUSTOM_SUBDIVIDE_GRID_X );
102     }
103     else
104     {
105       mGridSize = ImageDimensions( subdivideX, mGridSize.GetY() );
106     }
107   }
108
109   Property::Value* subdivideYValue = shaderMap.Find( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y, CUSTOM_SUBDIVIDE_GRID_Y );
110   if( subdivideYValue )
111   {
112     int subdivideY;
113     if( !subdivideYValue->Get( subdivideY ) || subdivideY < 1 )
114     {
115       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1\n", CUSTOM_SUBDIVIDE_GRID_Y );
116     }
117     else
118     {
119       mGridSize = ImageDimensions( mGridSize.GetX(), subdivideY );
120     }
121   }
122
123   Property::Value* hintsValue = shaderMap.Find( Toolkit::Visual::Shader::Property::HINTS, CUSTOM_SHADER_HINTS );
124   if( hintsValue )
125   {
126     if ( ! Scripting::GetBitmaskEnumerationProperty( *hintsValue, SHADER_HINT_TABLE, SHADER_HINT_TABLE_COUNT, mHints ) )
127     {
128       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint or an array of hint strings\n", CUSTOM_SHADER_HINTS );
129     }
130   }
131 }
132
133 void Internal::Visual::Base::Impl::CustomShader::CreatePropertyMap( Property::Map& map ) const
134 {
135   if( !mVertexShader.empty() || !mFragmentShader.empty() )
136   {
137     Property::Map customShader;
138     if( !mVertexShader.empty() )
139     {
140       customShader.Insert( Toolkit::Visual::Shader::Property::VERTEX_SHADER, mVertexShader );
141     }
142     if( !mFragmentShader.empty() )
143     {
144       customShader.Insert( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, mFragmentShader );
145     }
146
147     if( mGridSize.GetWidth() != 1 )
148     {
149       customShader.Insert( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
150     }
151     if( mGridSize.GetHeight() != 1 )
152     {
153       customShader.Insert( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
154     }
155
156     if( mHints != Dali::Shader::Hint::NONE )
157     {
158       customShader.Insert( Toolkit::Visual::Shader::Property::HINTS, static_cast< int >( mHints ) );
159     }
160
161     map.Insert( Toolkit::Visual::Property::SHADER, customShader );
162   }
163 }
164
165 } // namespace Internal
166
167 } // namespace Toolkit
168
169 } // namespace Dali