Merge "Disable pixel alignment while scrolling the text" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2017 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/public-api/visuals/visual-properties.h>
28 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
47   attribute mediump vec2 aPosition;\n
48   uniform mediump mat4 uMvpMatrix;\n
49   uniform mediump vec3 uSize;\n
50   \n
51
52   //Visual size and offset
53   uniform mediump vec2 offset;\n
54   uniform mediump vec2 size;\n
55   uniform mediump vec4 offsetSizeMode;\n
56   uniform mediump vec2 origin;\n
57   uniform mediump vec2 anchorPoint;\n
58
59   vec4 ComputeVertexPosition()\n
60   {\n
61     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
62     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
63     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
64   }\n
65
66   void main()\n
67   {\n
68     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
69   }\n
70 );
71
72 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
73   uniform lowp vec4 uColor;\n
74   uniform lowp vec3 mixColor;\n
75   uniform lowp float opacity;\n
76   \n
77   void main()\n
78   {\n
79     gl_FragColor = vec4(mixColor, opacity)*uColor;\n
80   }\n
81 );
82 }
83
84 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
85 {
86   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
87   colorVisualPtr->SetProperties( properties );
88   return colorVisualPtr;
89 }
90
91 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
92 : Visual::Base( factoryCache ),
93   mRenderIfTransparent( false )
94 {
95 }
96
97 ColorVisual::~ColorVisual()
98 {
99 }
100
101 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
102 {
103   // By virtue of DoSetProperties being called last, this will override
104   // anything set by Toolkit::Visual::Property::MIX_COLOR
105   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
106   if( colorValue )
107   {
108     Vector4 color;
109     if( colorValue->Get( color ) )
110     {
111       Property::Type type = colorValue->GetType();
112       if( type == Property::VECTOR4 )
113       {
114         SetMixColor( color );
115       }
116       else if( type == Property::VECTOR3 )
117       {
118         Vector3 color3(color);
119         SetMixColor( color3 );
120       }
121     }
122     else
123     {
124       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
125     }
126   }
127
128   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
129   if( renderIfTransparentValue )
130   {
131     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
132     {
133       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
134     }
135   }
136 }
137
138 void ColorVisual::DoSetOnStage( Actor& actor )
139 {
140   InitializeRenderer();
141
142   // Only add the renderer if it's not fully transparent
143   // We cannot avoid creating a renderer as it's used in the base class
144   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
145   {
146     actor.AddRenderer( mImpl->mRenderer );
147   }
148
149   // Color Visual generated and ready to display
150   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
151 }
152
153 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
154 {
155   map.Clear();
156   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
157   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
158   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
159 }
160
161 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
162 {
163   // Do nothing
164 }
165
166
167 void ColorVisual::OnSetTransform()
168 {
169   if( mImpl->mRenderer )
170   {
171     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
172   }
173 }
174
175 void ColorVisual::InitializeRenderer()
176 {
177   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
178
179   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
180   if( !shader )
181   {
182     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
183     mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
184   }
185
186   mImpl->mRenderer = Renderer::New( geometry, shader );
187
188   // ColorVisual has it's own index key for mix color - use this instead
189   // of using the new base index to avoid changing existing applications
190   // String keys will get to this property.
191   mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
192
193   if( mImpl->mMixColor.a < 1.f )
194   {
195     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
196   }
197
198   // Register transform properties
199   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
200 }
201
202 } // namespace Internal
203
204 } // namespace Toolkit
205
206 } // namespace Dali