Merge "Tizen Directory Migration" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / color / color-renderer.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 "color-renderer.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
26 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
27 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
28 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41 const char * const COLOR_NAME("mixColor");
42
43 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
44   attribute mediump vec2 aPosition;\n
45   uniform mediump mat4 uMvpMatrix;\n
46   uniform mediump vec3 uSize;\n
47   \n
48   void main()\n
49   {\n
50     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
51     vertexPosition.xyz *= uSize;\n
52     gl_Position = uMvpMatrix * vertexPosition;\n
53   }\n
54 );
55
56 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
57   uniform lowp vec4 uColor;\n
58   uniform lowp vec4 mixColor;\n
59   \n
60   void main()\n
61   {\n
62     gl_FragColor = mixColor*uColor;\n
63   }\n
64 );
65 }
66
67 ColorRenderer::ColorRenderer( RendererFactoryCache& factoryCache )
68 : ControlRenderer( factoryCache ),
69   mMixColorIndex( Property::INVALID_INDEX )
70 {
71 }
72
73 ColorRenderer::~ColorRenderer()
74 {
75 }
76
77 void ColorRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
78 {
79   Property::Value* color = propertyMap.Find( COLOR_NAME );
80   if( !( color && color->Get(mMixColor) ) )
81   {
82     DALI_LOG_ERROR( "Fail to provide a color to the ColorRenderer object" );
83   }
84 }
85
86 void ColorRenderer::SetSize( const Vector2& size )
87 {
88   ControlRenderer::SetSize( size );
89
90   // ToDo: renderer responds to the size change
91 }
92
93 void ColorRenderer::SetClipRect( const Rect<int>& clipRect )
94 {
95   ControlRenderer::SetClipRect( clipRect );
96
97   //ToDo: renderer responds to the clipRect change
98 }
99
100 void ColorRenderer::SetOffset( const Vector2& offset )
101 {
102   //ToDo: renderer applies the offset
103 }
104
105 void ColorRenderer::DoSetOnStage( Actor& actor )
106 {
107   InitializeRenderer();
108 }
109
110 void ColorRenderer::DoCreatePropertyMap( Property::Map& map ) const
111 {
112   map.Clear();
113   map.Insert( RENDERER_TYPE, COLOR_RENDERER );
114   map.Insert( COLOR_NAME, mMixColor );
115 }
116
117 void ColorRenderer::InitializeRenderer()
118 {
119   Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
120   if( !geometry )
121   {
122     geometry =  RendererFactoryCache::CreateQuadGeometry();
123     mFactoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, geometry );
124   }
125
126   Shader shader = mFactoryCache.GetShader( RendererFactoryCache::COLOR_SHADER );
127   if( !shader )
128   {
129     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
130     mFactoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, shader );
131   }
132
133   mImpl->mRenderer = Renderer::New( geometry, shader );
134
135   mMixColorIndex = mImpl->mRenderer.RegisterProperty( COLOR_NAME, mMixColor );
136   if( mMixColor.a < 1.f )
137   {
138     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
139   }
140 }
141
142 void ColorRenderer::SetColor(const Vector4& color)
143 {
144   mMixColor = color;
145
146   if( mImpl->mRenderer )
147   {
148     (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
149     if( color.a < 1.f )
150     {
151       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
152     }
153   }
154 }
155
156 } // namespace Internal
157
158 } // namespace Toolkit
159
160 } // namespace Dali