Merge "TextFit : modified it to work even if you change the text or multi-line attrib...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / super-blur-view / super-blur-view-impl.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 "super-blur-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/object/property-map.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali/public-api/rendering/renderer.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL_INCLUDES
33 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
34 #include <dali-toolkit/devel-api/controls/control-devel.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
37
38 namespace //Unnamed namespace
39 {
40
41 using namespace Dali;
42
43 //Todo: make these properties instead of constants
44 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
45 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
46 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
47 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
48 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGBA8888;
49 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
50 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
51
52 const char* ALPHA_UNIFORM_NAME( "uAlpha" );
53 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
54   varying mediump vec2 vTexCoord;\n
55   uniform sampler2D sTexture;\n
56   uniform lowp vec4 uColor;\n
57   uniform lowp float uAlpha;\n
58   \n
59   void main()\n
60   {\n
61     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
62     gl_FragColor.a *= uAlpha;
63   }\n
64 );
65
66 /**
67  * The constraint is used to blend the group of blurred images continuously with a unified blur strength property value which ranges from zero to one.
68  */
69 struct ActorOpacityConstraint
70 {
71   ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
72   {
73     float rangeLength = 1.f / static_cast<float>( totalImageNum );
74     float index = static_cast<float>( currentImageIdx );
75     mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
76   }
77
78   void operator()( float& current, const PropertyInputContainer& inputs )
79   {
80     float blurStrength = inputs[0]->GetFloat();
81     if(blurStrength < mRange.x)
82     {
83       current = 0.f;
84     }
85     else if(blurStrength > mRange.y)
86     {
87       current = 1.f;
88     }
89     else
90     {
91       current = ( blurStrength - mRange.x) / ( mRange.y - mRange.x );
92     }
93   }
94
95   Vector2 mRange;
96 };
97
98 #define DALI_COMPOSE_SHADER(STR) #STR
99
100 const char * const BASIC_VERTEX_SOURCE = DALI_COMPOSE_SHADER(
101   precision mediump float;\n
102   attribute mediump vec2 aPosition;\n
103   attribute mediump vec2 aTexture;\n
104   varying mediump vec2 vTexCoord;\n
105   uniform mediump mat4 uMvpMatrix;\n
106   uniform mediump vec3 uSize;\n
107   \n
108   void main()\n
109   {\n
110     mediump vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0);\n
111     vTexCoord = aTexture;\n
112     gl_Position = uMvpMatrix * vertexPosition;\n
113   }\n
114 );
115
116 const char * const BASIC_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER(
117   precision mediump float;\n
118   varying mediump vec2 vTexCoord;\n
119   uniform sampler2D sTexture;\n
120   uniform vec4 uColor;\n
121   \n
122   void main()\n
123   {\n
124     gl_FragColor = texture2D(sTexture, vTexCoord);\n
125     gl_FragColor *= uColor;
126   }\n
127 );
128
129 Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc )
130 {
131   Shader shader = Shader::New( vertexSrc, fragmentSrc );
132
133   Geometry texturedQuadGeometry = Geometry::New();
134
135   struct VertexPosition { Vector2 position; };
136   struct VertexTexture { Vector2 texture; };
137
138   VertexPosition positionArray[] =
139   {
140     { Vector2( -0.5f, -0.5f ) },
141     { Vector2(  0.5f, -0.5f ) },
142     { Vector2( -0.5f,  0.5f ) },
143     { Vector2(  0.5f,  0.5f ) }
144   };
145   uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
146
147   VertexTexture uvArray[] =
148   {
149     { Vector2( 0.0f, 0.0f ) },
150     { Vector2( 1.0f, 0.0f ) },
151     { Vector2( 0.0f, 1.0f ) },
152     { Vector2( 1.0f, 1.0f ) }
153   };
154
155   Property::Map positionVertexFormat;
156   positionVertexFormat["aPosition"] = Property::VECTOR2;
157   PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
158   positionVertices.SetData( positionArray, numberOfVertices );
159   texturedQuadGeometry.AddVertexBuffer( positionVertices );
160
161   Property::Map textureVertexFormat;
162   textureVertexFormat["aTexture"] = Property::VECTOR2;
163   PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
164   textureVertices.SetData( uvArray, numberOfVertices );
165   texturedQuadGeometry.AddVertexBuffer( textureVertices );
166
167   const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
168   texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
169
170   Renderer renderer = Renderer::New( texturedQuadGeometry, shader );
171
172   TextureSet textureSet = TextureSet::New();
173   renderer.SetTextures( textureSet );
174
175   return renderer;
176 }
177
178 void SetRendererTexture( Renderer& renderer, Texture& texture )
179 {
180   if( renderer )
181   {
182     TextureSet textureSet = renderer.GetTextures();
183     textureSet.SetTexture( 0u, texture );
184   }
185 }
186
187 void SetRendererTexture( Renderer& renderer, FrameBuffer& frameBuffer )
188 {
189   if( frameBuffer )
190   {
191     Texture texture = frameBuffer.GetColorTexture();
192     SetRendererTexture( renderer, texture );
193   }
194 }
195
196 } // namespace
197
198 namespace Dali
199 {
200
201 namespace Toolkit
202 {
203
204 namespace Internal
205 {
206
207 namespace
208 {
209
210 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
211
212 BaseHandle Create()
213 {
214   return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
215 }
216
217 // Setup properties, signals and actions using the type-registry.
218 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
219
220 DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "imageUrl", STRING, IMAGE_URL )
221
222 DALI_TYPE_REGISTRATION_END()
223
224 } // unnamed namespace
225
226 SuperBlurView::SuperBlurView( unsigned int blurLevels )
227 : Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS ) ),
228   mTargetSize( Vector2::ZERO ),
229   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
230   mBlurLevels( blurLevels ),
231   mResourcesCleared( true )
232 {
233   DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
234   mGaussianBlurView.assign( blurLevels, Toolkit::GaussianBlurView() );
235   mBlurredImage.assign( blurLevels, FrameBuffer() );
236   mRenderers.assign( blurLevels+1, Dali::Renderer() );
237 }
238
239 SuperBlurView::~SuperBlurView()
240 {
241 }
242
243 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
244 {
245   //Create the implementation
246   IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
247
248   //Pass ownership to CustomActor via derived handle
249   Toolkit::SuperBlurView handle( *superBlurView );
250
251   // Second-phase init of the implementation
252   // This can only be done after the CustomActor connection has been made...
253   superBlurView->Initialize();
254
255   return handle;
256 }
257
258 void SuperBlurView::OnInitialize()
259 {
260   Actor self( Self() );
261
262   mBlurStrengthPropertyIndex = self.RegisterProperty( "blurStrength", 0.f );
263 }
264
265 void SuperBlurView::SetTexture( Texture texture )
266 {
267   mInputTexture = texture;
268
269   if( mTargetSize == Vector2::ZERO )
270   {
271     return;
272   }
273
274   ClearBlurResource();
275
276   Actor self( Self() );
277
278   BlurTexture( 0, mInputTexture );
279   SetRendererTexture( mRenderers[0], texture );
280
281   unsigned int i = 1;
282   for(; i<mBlurLevels; i++)
283   {
284     BlurTexture( i, mBlurredImage[i-1].GetColorTexture() );
285     SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
286   }
287
288   SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
289
290   mResourcesCleared = false;
291 }
292
293 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
294 {
295   return mBlurStrengthPropertyIndex;
296 }
297
298 void SuperBlurView::SetBlurStrength( float blurStrength )
299 {
300   Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
301 }
302
303 float SuperBlurView::GetCurrentBlurStrength() const
304 {
305   float blurStrength;
306   (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
307
308   return blurStrength;
309 }
310
311 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
312 {
313   return mBlurFinishedSignal;
314 }
315
316 Texture SuperBlurView::GetBlurredTexture( unsigned int level )
317 {
318   DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
319
320   FrameBuffer frameBuffer = mBlurredImage[level-1];
321
322   return frameBuffer.GetColorTexture();
323 }
324
325 void SuperBlurView::BlurTexture( unsigned int idx, Texture texture )
326 {
327   DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
328   mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
329                                                            GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
330                                                            GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
331                                                            GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
332   mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
333   mGaussianBlurView[idx].SetSize(mTargetSize);
334   Stage::GetCurrent().Add( mGaussianBlurView[idx] );
335
336   mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( texture, mBlurredImage[idx] );
337
338   mGaussianBlurView[idx].ActivateOnce();
339   if( idx == mBlurLevels-1 )
340   {
341     mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
342   }
343 }
344
345 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
346 {
347   ClearBlurResource();
348   Toolkit::SuperBlurView handle( GetOwner() );
349   mBlurFinishedSignal.Emit( handle );
350 }
351
352 void SuperBlurView::ClearBlurResource()
353 {
354   if( !mResourcesCleared )
355   {
356     DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
357     for(unsigned int i=0; i<mBlurLevels;i++)
358     {
359       Stage::GetCurrent().Remove( mGaussianBlurView[i] );
360       mGaussianBlurView[i].Deactivate();
361     }
362     mResourcesCleared = true;
363   }
364 }
365
366 void SuperBlurView::OnSizeSet( const Vector3& targetSize )
367 {
368   if( mTargetSize != Vector2(targetSize) )
369   {
370     mTargetSize = Vector2(targetSize);
371
372     Actor self = Self();
373     for( unsigned int i = 1; i <= mBlurLevels; i++ )
374     {
375       float exponent = static_cast<float>(i);
376
377       unsigned int width = mTargetSize.width/std::pow(2.f,exponent);
378       unsigned int height = mTargetSize.height/std::pow(2.f,exponent);
379
380       mBlurredImage[i-1] = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
381       Texture texture = Texture::New( TextureType::TEXTURE_2D, GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, unsigned(width), unsigned(height) );
382       mBlurredImage[i-1].AttachColorTexture( texture );
383     }
384
385     if( mInputTexture )
386     {
387       SetTexture( mInputTexture );
388     }
389   }
390
391   Control::OnSizeSet( targetSize );
392 }
393
394 void SuperBlurView::OnStageConnection( int depth )
395 {
396   if( mTargetSize == Vector2::ZERO )
397   {
398     return;
399   }
400
401   // Exception to the rule, chaining up first ensures visuals have SetOnStage called to create their renderers
402   Control::OnStageConnection( depth );
403
404   Actor self = Self();
405
406   for(unsigned int i=0; i<mBlurLevels+1;i++)
407   {
408     mRenderers[i] = CreateRenderer( BASIC_VERTEX_SOURCE, FRAGMENT_SHADER );
409     mRenderers[i].SetProperty( Dali::Renderer::Property::DEPTH_INDEX, (int)i );
410     self.AddRenderer( mRenderers[i] );
411
412     if( i > 0 )
413     {
414       Renderer renderer = mRenderers[i];
415       Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f );
416       Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) );
417       constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) );
418       constraint.Apply();
419     }
420   }
421
422   if( mInputTexture )
423   {
424     SetRendererTexture( mRenderers[0], mInputTexture );
425     unsigned int i = 1;
426     for(; i<mBlurLevels; i++)
427     {
428       SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
429     }
430     SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
431   }
432 }
433
434 void SuperBlurView::OnStageDisconnection()
435 {
436   for(unsigned int i=0; i<mBlurLevels+1;i++)
437   {
438     Self().RemoveRenderer( mRenderers[i] );
439     mRenderers[i].Reset();
440   }
441
442   Control::OnStageDisconnection();
443 }
444
445 Vector3 SuperBlurView::GetNaturalSize()
446 {
447   if( mInputTexture )
448   {
449     return Vector3( mInputTexture.GetWidth(), mInputTexture.GetHeight(), 0.f );
450   }
451   return Vector3::ZERO;
452 }
453
454 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
455 {
456   Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
457
458   if( superBlurView )
459   {
460     SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
461
462     if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
463     {
464       value.Get( superBlurViewImpl.mUrl );
465
466       PixelData pixels = SyncImageLoader::Load( superBlurViewImpl.mUrl );
467
468       if ( pixels )
469       {
470         Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
471         texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
472
473         superBlurViewImpl.SetTexture( texture );
474       }
475       else
476       {
477         DALI_LOG_ERROR( "Cannot create image from property value\n" );
478       }
479     }
480   }
481 }
482
483 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
484 {
485   Property::Value value;
486
487   Toolkit::SuperBlurView blurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
488
489   if( blurView )
490   {
491     SuperBlurView& superBlurViewImpl( GetImpl( blurView ) );
492
493     if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
494     {
495       value = superBlurViewImpl.mUrl;
496     }
497   }
498
499   return value;
500 }
501
502 } // namespace Internal
503
504 } // namespace Toolkit
505
506 } // namespace Dali