Create Renderer when the Visual is created
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2021 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/rendering/renderer-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/devel-api/visuals/color-visual-actions-devel.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
46 {
47   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
48   colorVisualPtr->SetProperties( properties );
49   colorVisualPtr->Initialize();
50   return colorVisualPtr;
51 }
52
53 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
54 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
55   mBlurRadius(0.0f),
56   mBlurRadiusIndex(Property::INVALID_INDEX),
57   mRenderIfTransparent(false),
58   mNeedBlurRadius(false)
59 {
60 }
61
62 ColorVisual::~ColorVisual()
63 {
64 }
65
66 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
67 {
68   // By virtue of DoSetProperties being called last, this will override
69   // anything set by Toolkit::Visual::Property::MIX_COLOR
70   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
71   if( colorValue )
72   {
73     Vector4 color;
74     if( colorValue->Get( color ) )
75     {
76       Property::Type type = colorValue->GetType();
77       if( type == Property::VECTOR4 )
78       {
79         SetMixColor( color );
80       }
81       else if( type == Property::VECTOR3 )
82       {
83         Vector3 color3(color);
84         SetMixColor( color3 );
85       }
86     }
87     else
88     {
89       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
90     }
91   }
92
93   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
94   if( renderIfTransparentValue )
95   {
96     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
97     {
98       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
99     }
100   }
101
102   Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
103   if( blurRadiusValue )
104   {
105     if( !blurRadiusValue->Get( mBlurRadius ) )
106     {
107       DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
108     }
109   }
110 }
111
112 void ColorVisual::DoSetOnScene( Actor& actor )
113 {
114   // Only add the renderer if it's not fully transparent
115   // We cannot avoid creating a renderer as it's used in the base class
116   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
117   {
118     actor.AddRenderer( mImpl->mRenderer );
119   }
120
121   // Color Visual generated and ready to display
122   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
123 }
124
125 void ColorVisual::DoSetOffScene(Actor& actor)
126 {
127   actor.RemoveRenderer(mImpl->mRenderer);
128 }
129
130 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
131 {
132   map.Clear();
133   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
134   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
135   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
136
137   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
138   {
139     // Update values from Renderer
140     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
141     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
142   }
143   else
144   {
145     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
146   }
147 }
148
149 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
150 {
151   // Do nothing
152 }
153
154 void ColorVisual::OnSetTransform()
155 {
156   if( mImpl->mRenderer )
157   {
158     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
159   }
160 }
161
162 void ColorVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
163 {
164   // Check if action is valid for this visual type and perform action if possible
165   switch( actionId )
166   {
167     case DevelColorVisual::Action::UPDATE_PROPERTY:
168     {
169       const Property::Map* map = attributes.GetMap();
170       if( map )
171       {
172         DoSetProperties( *map );
173       }
174       break;
175     }
176   }
177 }
178
179 void ColorVisual::UpdateShader()
180 {
181   if(mImpl->mRenderer)
182   {
183     Shader shader = GetShader();
184     mImpl->mRenderer.SetShader(shader);
185   }
186 }
187
188 void ColorVisual::OnInitialize()
189 {
190   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
191
192   Shader shader = GetShader();
193
194   mImpl->mRenderer = Renderer::New(geometry, shader);
195
196   // ColorVisual has it's own index key for mix color - use this instead
197   // of using the new base index to avoid changing existing applications
198   // String keys will get to this property.
199   mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
200
201   if(!EqualsZero(mBlurRadius))
202   {
203     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
204     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
205   }
206
207   // Register transform properties
208   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
209 }
210
211 Shader ColorVisual::GetShader()
212 {
213   Shader shader;
214   if(!EqualsZero(mBlurRadius) || mNeedBlurRadius)
215   {
216     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
217     if( !shader )
218     {
219       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_FRAG.data() );
220       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
221     }
222   }
223   else if( !IsRoundedCornerRequired() )
224   {
225     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
226     if( !shader )
227     {
228       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_FRAG.data() );
229       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
230     }
231   }
232   else
233   {
234     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
235     if( !shader )
236     {
237       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_FRAG.data() );
238       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
239     }
240   }
241
242   return shader;
243 }
244
245 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
246 {
247   if(!mImpl->mRenderer)
248   {
249     Handle handle;
250     return Dali::Property(handle, Property::INVALID_INDEX);
251   }
252
253   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
254   {
255     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
256
257     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
258
259     mNeedBlurRadius = true;
260
261     // Change shader
262     UpdateShader();
263
264     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
265   }
266
267   Handle handle;
268   return Dali::Property(handle, Property::INVALID_INDEX);
269 }
270
271 } // namespace Internal
272
273 } // namespace Toolkit
274
275 } // namespace Dali