Merge "Updated visuals to separate alpha channel from mixColor" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.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 "color-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
30 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44
45 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
46   attribute mediump vec2 aPosition;\n
47   uniform mediump mat4 uMvpMatrix;\n
48   uniform mediump vec3 uSize;\n
49   \n
50
51   //Visual size and offset
52   uniform mediump vec2 offset;\n
53   uniform mediump vec2 size;\n
54   uniform mediump vec4 offsetSizeMode;\n
55   uniform mediump vec2 origin;\n
56   uniform mediump vec2 anchorPoint;\n
57
58   vec4 ComputeVertexPosition()\n
59   {\n
60     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
61     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
62     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
63   }\n
64
65   void main()\n
66   {\n
67     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
68   }\n
69 );
70
71 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
72   uniform lowp vec4 uColor;\n
73   uniform lowp vec3 mixColor;\n
74   uniform lowp float opacity;\n
75   \n
76   void main()\n
77   {\n
78     gl_FragColor = vec4(mixColor, opacity)*uColor;\n
79   }\n
80 );
81 }
82
83 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
84 {
85   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
86   colorVisualPtr->SetProperties( properties );
87   return colorVisualPtr;
88 }
89
90 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
91 : Visual::Base( factoryCache )
92 {
93 }
94
95 ColorVisual::~ColorVisual()
96 {
97 }
98
99 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
100 {
101   // By virtue of DoSetProperties being called last, this will override
102   // anything set by DevelVisual::Property::MIX_COLOR
103   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
104   if( colorValue )
105   {
106     Vector4 color;
107     if( colorValue->Get( color ) )
108     {
109       Property::Type type = colorValue->GetType();
110       if( type == Property::VECTOR4 )
111       {
112         SetMixColor( color );
113       }
114       else if( type == Property::VECTOR3 )
115       {
116         Vector3 color3(color);
117         SetMixColor( color3 );
118       }
119     }
120     else
121     {
122       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
123     }
124   }
125 }
126
127 void ColorVisual::DoSetOnStage( Actor& actor )
128 {
129   InitializeRenderer();
130
131   actor.AddRenderer( mImpl->mRenderer );
132 }
133
134 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
135 {
136   map.Clear();
137   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::COLOR );
138   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
139 }
140
141 void ColorVisual::OnSetTransform()
142 {
143   if( mImpl->mRenderer )
144   {
145     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
146   }
147 }
148
149 void ColorVisual::InitializeRenderer()
150 {
151   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
152
153   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
154   if( !shader )
155   {
156     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
157     mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
158   }
159
160   mImpl->mRenderer = Renderer::New( geometry, shader );
161
162   // ColorVisual has it's own index key for mix color - use this instead
163   // of using the new base index to avoid changing existing applications
164   // String keys will get to this property.
165   mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
166
167   if( mImpl->mMixColor.a < 1.f )
168   {
169     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
170   }
171
172   // Register transform properties
173   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
174 }
175
176 } // namespace Internal
177
178 } // namespace Toolkit
179
180 } // namespace Dali