42d9634e6862958312dcca93cb307f5b54c9c3e5
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / control-renderer-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 "control-renderer-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/control-renderer-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 ControlRenderer::ControlRenderer( RendererFactoryCache& factoryCache )
49 : mImpl( new Impl() ),
50   mFactoryCache( factoryCache )
51 {
52 }
53
54 ControlRenderer::~ControlRenderer()
55 {
56   delete mImpl;
57 }
58
59 void ControlRenderer::Initialize( Actor& actor, const Property::Map& propertyMap )
60 {
61   if( mImpl->mCustomShader )
62   {
63     mImpl->mCustomShader->SetPropertyMap( propertyMap );
64   }
65   else
66   {
67     Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
68     if( customShaderValue )
69     {
70       Property::Map customShader;
71       if( customShaderValue->Get( customShader ) )
72       {
73         mImpl->mCustomShader = new Impl::CustomShader( propertyMap );
74       }
75     }
76   }
77   DoInitialize( actor, propertyMap );
78 }
79
80 void ControlRenderer::SetSize( const Vector2& size )
81 {
82   mImpl->mSize = size;
83 }
84
85 const Vector2& ControlRenderer::GetSize() const
86 {
87   return mImpl->mSize;
88 }
89
90 void ControlRenderer::GetNaturalSize( Vector2& naturalSize ) const
91 {
92   naturalSize = Vector2::ZERO;
93 }
94
95 void ControlRenderer::SetClipRect( const Rect<int>& clipRect )
96 {
97 }
98
99 void ControlRenderer::SetOffset( const Vector2& offset )
100 {
101   mImpl->mOffset = offset;
102 }
103
104 void ControlRenderer::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 ControlRenderer::GetDepthIndex() const
114 {
115   return mImpl->mDepthIndex;
116 }
117
118 void ControlRenderer::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   mImpl->mFlags |= Impl::IS_ON_STAGE;
126 }
127
128 void ControlRenderer::SetOffStage( Actor& actor )
129 {
130   if( GetIsOnStage() )
131   {
132     DoSetOffStage( actor );
133
134     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
135   }
136 }
137
138 void ControlRenderer::EnablePreMultipliedAlpha( bool preMultipled )
139 {
140   mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
141   if( mImpl->mRenderer )
142   {
143     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
144   }
145 }
146
147 bool ControlRenderer::IsPreMultipliedAlphaEnabled() const
148 {
149   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
150 }
151
152 void ControlRenderer::DoSetOnStage( Actor& actor )
153 {
154 }
155
156 void ControlRenderer::DoSetOffStage( Actor& actor )
157 {
158   actor.RemoveRenderer( mImpl->mRenderer );
159   mImpl->mRenderer.Reset();
160 }
161
162 void ControlRenderer::CreatePropertyMap( Property::Map& map ) const
163 {
164   DoCreatePropertyMap( map );
165
166   if( mImpl->mCustomShader )
167   {
168     mImpl->mCustomShader->CreatePropertyMap( map );
169   }
170 }
171
172 bool ControlRenderer::GetIsOnStage() const
173 {
174   return mImpl->mFlags & Impl::IS_ON_STAGE;
175 }
176
177 bool ControlRenderer::GetIsFromCache() const
178 {
179   return mImpl->mFlags & Impl::IS_FROM_CACHE;
180 }
181
182 } // namespace Internal
183
184 } // namespace Toolkit
185
186 } // namespace Dali