2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "control-renderer-impl.h"
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
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
48 ControlRenderer::ControlRenderer( RendererFactoryCache& factoryCache )
49 : mImpl( new Impl() ),
50 mFactoryCache( factoryCache )
54 ControlRenderer::~ControlRenderer()
59 void ControlRenderer::Initialize( const Property::Map& propertyMap )
61 if( mImpl->mCustomShader )
63 mImpl->mCustomShader->SetPropertyMap( propertyMap );
67 Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
68 if( customShaderValue )
70 Property::Map customShader;
71 if( customShaderValue->Get( customShader ) )
73 mImpl->mCustomShader = new Impl::CustomShader( propertyMap );
77 DoInitialize( propertyMap );
79 if( mImpl->mIsOnStage )
81 InitializeRenderer( mImpl->mRenderer );
85 void ControlRenderer::SetSize( const Vector2& size )
90 const Vector2& ControlRenderer::GetSize() const
95 void ControlRenderer::GetNaturalSize( Vector2& naturalSize ) const
97 naturalSize = Vector2::ZERO;
100 void ControlRenderer::SetClipRect( const Rect<int>& clipRect )
102 mImpl->mClipRect = clipRect;
105 void ControlRenderer::SetOffset( const Vector2& offset )
107 mImpl->mOffset = offset;
110 void ControlRenderer::SetDepthIndex( float index )
112 mImpl->mDepthIndex = index;
113 if( mImpl->mRenderer )
115 mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
119 float ControlRenderer::GetDepthIndex() const
121 return mImpl->mDepthIndex;
124 void ControlRenderer::SetCachedRendererKey( const std::string& cachedRendererKey )
126 if( mImpl->mCachedRendererKey == cachedRendererKey )
130 if( !mImpl->mIsOnStage )
132 mImpl->mCachedRendererKey = cachedRendererKey;
136 //clean the renderer from the cache since it may no longer be in use
137 mFactoryCache.CleanRendererCache( mImpl->mCachedRendererKey );
139 //add the new renderer
140 mImpl->mCachedRendererKey = cachedRendererKey;
141 if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
143 DALI_ASSERT_DEBUG( mImpl->mRenderer && "The control render is on stage but it doesn't have a valid renderer.");
144 mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
149 void ControlRenderer::SetOnStage( Actor& actor )
151 if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
153 mImpl->mRenderer = mFactoryCache.GetRenderer( mImpl->mCachedRendererKey );
154 if( !mImpl->mRenderer )
156 InitializeRenderer( mImpl->mRenderer );
157 mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
161 if( !mImpl->mRenderer )
163 InitializeRenderer( mImpl->mRenderer );
166 mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
167 actor.AddRenderer( mImpl->mRenderer );
168 mImpl->mIsOnStage = true;
170 DoSetOnStage( actor );
173 void ControlRenderer::SetOffStage( Actor& actor )
175 if( mImpl->mIsOnStage )
177 DoSetOffStage( actor );
178 actor.RemoveRenderer( mImpl->mRenderer );
179 mImpl->mRenderer.Reset();
181 //clean the renderer from the cache since it may no longer be in use
182 mFactoryCache.CleanRendererCache( mImpl->mCachedRendererKey );
184 mImpl->mIsOnStage = false;
188 void ControlRenderer::DoSetOnStage( Actor& actor )
192 void ControlRenderer::DoSetOffStage( Actor& actor )
196 void ControlRenderer::CreatePropertyMap( Property::Map& map ) const
198 if( mImpl->mCustomShader )
200 mImpl->mCustomShader->CreatePropertyMap( map );
202 DoCreatePropertyMap( map );
205 } // namespace Internal
207 } // namespace Toolkit