Merge "Tizen Directory Migration" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / visual-impl.cpp
1 /*
2  * Copyright (c) 2015 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 "visual-impl.h"
20
21 // EXTERNAL HEADER
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24
25 //INTERNAL HEARDER
26 #include <dali-toolkit/internal/controls/renderers/visual-data-impl.h>
27
28 namespace
29 {
30 //custom shader
31 const char * const CUSTOM_SHADER( "shader" );
32 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
33 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
34 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
35 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
36 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type INTEGER; (bitfield) values from enum Shader::Hints
37 }
38
39 namespace Dali
40 {
41
42 namespace Toolkit
43 {
44
45 namespace Internal
46 {
47
48 Visual::Visual( VisualFactoryCache& factoryCache )
49 : mImpl( new Impl() ),
50   mFactoryCache( factoryCache )
51 {
52 }
53
54 Visual::~Visual()
55 {
56   delete mImpl;
57 }
58
59 void Visual::SetCustomShader( const Property::Map& shaderMap )
60 {
61   if( mImpl->mCustomShader )
62   {
63     mImpl->mCustomShader->SetPropertyMap( shaderMap );
64   }
65   else
66   {
67    mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
68   }
69 }
70
71 void Visual::Initialize( Actor& actor, const Property::Map& propertyMap )
72 {
73   Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
74   if( customShaderValue )
75   {
76     Property::Map shaderMap;
77     if( customShaderValue->Get( shaderMap ) )
78     {
79       SetCustomShader( shaderMap );
80     }
81   }
82
83   DoInitialize( actor, propertyMap );
84 }
85
86 void Visual::SetSize( const Vector2& size )
87 {
88   mImpl->mSize = size;
89 }
90
91 const Vector2& Visual::GetSize() const
92 {
93   return mImpl->mSize;
94 }
95
96 void Visual::GetNaturalSize( Vector2& naturalSize ) const
97 {
98   naturalSize = Vector2::ZERO;
99 }
100
101 void Visual::SetClipRect( const Rect<int>& clipRect )
102 {
103 }
104
105 void Visual::SetOffset( const Vector2& offset )
106 {
107   mImpl->mOffset = offset;
108 }
109
110 void Visual::SetDepthIndex( float index )
111 {
112   mImpl->mDepthIndex = index;
113   if( mImpl->mRenderer )
114   {
115     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
116   }
117 }
118
119 float Visual::GetDepthIndex() const
120 {
121   return mImpl->mDepthIndex;
122 }
123
124 void Visual::SetOnStage( Actor& actor )
125 {
126   DoSetOnStage( actor );
127
128   mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
129   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
130   actor.AddRenderer( mImpl->mRenderer );
131   mImpl->mFlags |= Impl::IS_ON_STAGE;
132 }
133
134 void Visual::SetOffStage( Actor& actor )
135 {
136   if( GetIsOnStage() )
137   {
138     DoSetOffStage( actor );
139
140     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
141   }
142 }
143
144 void Visual::EnablePreMultipliedAlpha( bool preMultipled )
145 {
146   if(preMultipled)
147   {
148     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
149   }
150   else
151   {
152     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
153   }
154
155   if( mImpl->mRenderer )
156   {
157     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
158   }
159 }
160
161 bool Visual::IsPreMultipliedAlphaEnabled() const
162 {
163   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
164 }
165
166 void Visual::DoSetOnStage( Actor& actor )
167 {
168 }
169
170 void Visual::DoSetOffStage( Actor& actor )
171 {
172   actor.RemoveRenderer( mImpl->mRenderer );
173   mImpl->mRenderer.Reset();
174 }
175
176 void Visual::CreatePropertyMap( Property::Map& map ) const
177 {
178   DoCreatePropertyMap( map );
179
180   if( mImpl->mCustomShader )
181   {
182     mImpl->mCustomShader->CreatePropertyMap( map );
183   }
184 }
185
186 bool Visual::GetIsOnStage() const
187 {
188   return mImpl->mFlags & Impl::IS_ON_STAGE;
189 }
190
191 bool Visual::GetIsFromCache() const
192 {
193   return mImpl->mFlags & Impl::IS_FROM_CACHE;
194 }
195
196 } // namespace Internal
197
198 } // namespace Toolkit
199
200 } // namespace Dali