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