Merge changes I48753b8f,If7ef493d into devel/master
[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( "vertex-shader" );
33 const char * const CUSTOM_FRAGMENT_SHADER( "fragment-shader" );
34 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivide-grid-x" );
35 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivide-grid-y" );
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( 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( propertyMap );
78
79   if( mImpl->mIsOnStage )
80   {
81     InitializeRenderer( mImpl->mRenderer );
82   }
83 }
84
85 void ControlRenderer::SetSize( const Vector2& size )
86 {
87   mImpl->mSize = size;
88 }
89
90 const Vector2& ControlRenderer::GetSize() const
91 {
92   return mImpl->mSize;
93 }
94
95 void ControlRenderer::GetNaturalSize( Vector2& naturalSize ) const
96 {
97   naturalSize = Vector2::ZERO;
98 }
99
100 void ControlRenderer::SetClipRect( const Rect<int>& clipRect )
101 {
102   mImpl->mClipRect = clipRect;
103 }
104
105 void ControlRenderer::SetOffset( const Vector2& offset )
106 {
107   mImpl->mOffset = offset;
108 }
109
110 void ControlRenderer::SetDepthIndex( float index )
111 {
112   mImpl->mDepthIndex = index;
113   if( mImpl->mRenderer )
114   {
115     mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
116   }
117 }
118
119 float ControlRenderer::GetDepthIndex() const
120 {
121   return mImpl->mDepthIndex;
122 }
123
124 void ControlRenderer::SetCachedRendererKey( const std::string& cachedRendererKey )
125 {
126   if( mImpl->mCachedRendererKey == cachedRendererKey )
127   {
128     return;
129   }
130   if( !mImpl->mIsOnStage )
131   {
132     mImpl->mCachedRendererKey = cachedRendererKey;
133   }
134   else
135   {
136     //remove the cached renderer from the cache if we and the cache are the only things that hold a reference to it
137     if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->ReferenceCount() == 2 )
138     {
139       mFactoryCache.RemoveRenderer( mImpl->mCachedRenderer->mKey );
140     }
141     mImpl->mCachedRenderer.Reset();
142
143     //add the new renderer
144     mImpl->mCachedRendererKey = cachedRendererKey;
145     if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
146     {
147       DALI_ASSERT_DEBUG( mImpl->mRenderer && "The control render is on stage but it doesn't have a valid renderer.");
148       mImpl->mCachedRenderer = mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
149     }
150   }
151 }
152
153 void ControlRenderer::SetOnStage( Actor& actor )
154 {
155   if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
156   {
157     mImpl->mCachedRenderer = mFactoryCache.GetRenderer( mImpl->mCachedRendererKey );
158     if( !mImpl->mCachedRenderer || !mImpl->mCachedRenderer->mRenderer )
159     {
160       InitializeRenderer( mImpl->mRenderer );
161       mImpl->mCachedRenderer = mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
162     }
163
164     if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->mRenderer )
165     {
166       mImpl->mRenderer = mImpl->mCachedRenderer->mRenderer;
167     }
168   }
169
170   if( !mImpl->mRenderer )
171   {
172     InitializeRenderer( mImpl->mRenderer );
173   }
174
175   mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
176   actor.AddRenderer( mImpl->mRenderer );
177   mImpl->mIsOnStage = true;
178
179   DoSetOnStage( actor );
180 }
181
182 void ControlRenderer::SetOffStage( Actor& actor )
183 {
184   if( mImpl->mIsOnStage )
185   {
186     DoSetOffStage( actor );
187
188     //remove the cached renderer from the cache if we and the cache are the only things that hold a reference to it
189     if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->ReferenceCount() == 2 )
190     {
191       mFactoryCache.RemoveRenderer( mImpl->mCachedRenderer->mKey );
192     }
193     mImpl->mCachedRenderer.Reset();
194
195     actor.RemoveRenderer( mImpl->mRenderer );
196     mImpl->mRenderer.Reset();
197
198     mImpl->mIsOnStage = false;
199   }
200 }
201
202 void ControlRenderer::DoSetOnStage( Actor& actor )
203 {
204 }
205
206 void ControlRenderer::DoSetOffStage( Actor& actor )
207 {
208 }
209
210 void ControlRenderer::CreatePropertyMap( Property::Map& map ) const
211 {
212   if( mImpl->mCustomShader )
213   {
214     mImpl->mCustomShader->CreatePropertyMap( map );
215   }
216   DoCreatePropertyMap( map );
217 }
218
219 } // namespace Internal
220
221 } // namespace Toolkit
222
223 } // namespace Dali