ed3526733f2a1bb92125347906f1dceda47ee298
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bloom-view / bloom-view-impl.cpp
1 /*
2  * Copyright (c) 2020 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 "bloom-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <sstream>
23 #include <iomanip>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/devel-api/common/stage.h>
27 #include <dali/public-api/object/property-map.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/render-tasks/render-task-list.h>
31 #include <dali/public-api/rendering/renderer.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
35 #include <dali-toolkit/devel-api/controls/bloom-view/bloom-view.h>
36 #include <dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.h>
37 #include <dali-toolkit/internal/controls/control/control-renderers.h>
38 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51
52 using namespace Dali;
53
54 BaseHandle Create()
55 {
56   return Toolkit::BloomView::New();
57 }
58
59 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::BloomView, Toolkit::Control, Create )
60 DALI_TYPE_REGISTRATION_END()
61
62 // default parameters
63 const float BLOOM_THRESHOLD_DEFAULT = 0.25f;
64 const float BLOOM_BLUR_STRENGTH_DEFAULT = 1.0f;
65 const float BLOOM_INTENSITY_DEFAULT = 1.0f;
66 const float IMAGE_INTENSITY_DEFAULT = 1.0f;
67 const float BLOOM_SATURATION_DEFAULT = 1.0f;
68 const float IMAGE_SATURATION_DEFAULT = 1.0f;
69
70 // gaussian blur
71 const unsigned int BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_NUM_SAMPLES = 5;
72 const float BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_BELL_CURVE_WIDTH = 1.5f;
73 const Pixel::Format BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGBA8888;
74 const float BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_FADE_IN = 1.0f;                                       // default, fully blurred
75 const float BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
76 const float BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
77
78 const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
79
80 const char* const BLOOM_BLUR_STRENGTH_PROPERTY_NAME = "BlurStrengthProperty";
81 const char* const BLOOM_THRESHOLD_PROPERTY_NAME = "uBloomThreshold";
82 const char* const RECIP_ONE_MINUS_BLOOM_THRESHOLD_PROPERTY_NAME = "uRecipOneMinusBloomThreshold";
83 const char* const BLOOM_INTENSITY_PROPERTY_NAME = "uBloomIntensity";
84 const char* const BLOOM_SATURATION_PROPERTY_NAME = "uBloomSaturation";
85 const char* const IMAGE_INTENSITY_PROPERTY_NAME = "uImageIntensity";
86 const char* const IMAGE_SATURATION_PROPERTY_NAME = "uImageSaturation";
87
88 ///////////////////////////////////////////////////////
89 //
90 // Bloom shaders
91 //
92
93 const char* const BLOOM_EXTRACT_FRAGMENT_SOURCE =
94   "varying mediump vec2 vTexCoord;\n"
95   "uniform sampler2D sTexture;\n"
96   "uniform lowp vec4 uColor;\n"
97   "uniform mediump float uBloomThreshold;\n"
98   "uniform mediump float uRecipOneMinusBloomThreshold;\n"
99   "void main()\n"
100   "{\n"
101   "  mediump vec4 col;\n"
102   "  col = texture2D(sTexture, vTexCoord);\n"
103   "  col = (col - uBloomThreshold) * uRecipOneMinusBloomThreshold;\n" // remove intensities lower than the thresold and remap intensities above the threshold to [0..1]
104   "  gl_FragColor = clamp(col, 0.0, 1.0);\n"
105   "}\n";
106
107 const char* const COMPOSITE_FRAGMENT_SOURCE =
108   "precision mediump float;\n"
109   "varying mediump vec2 vTexCoord;\n"
110   "uniform sampler2D sTexture;\n"
111   "uniform sampler2D sEffect;\n"
112   "uniform lowp vec4 uColor;\n"
113   "uniform float uBloomIntensity;\n"
114   "uniform float uImageIntensity;\n"
115   "uniform float uBloomSaturation;\n"
116   "uniform float uImageSaturation;\n"
117
118   "vec4 ChangeSaturation(vec4 col, float sat)\n"
119   "{\n"
120   "  float grey = dot(col.rgb, vec3(0.3, 0.6, 0.1));\n"
121   "  return mix(vec4(grey, grey, grey, 1.0), col, sat);\n"
122   "}\n"
123
124   "void main()\n"
125   "{\n"
126   "  mediump vec4 image;\n"
127   "  mediump vec4 bloom;\n"
128   "  image = texture2D(sTexture, vTexCoord);\n"
129   "  bloom = texture2D(sEffect, vTexCoord);\n"
130   "  image = ChangeSaturation(image, uImageSaturation) * uImageIntensity;\n"
131   "  bloom = ChangeSaturation(bloom, uBloomSaturation) * uBloomIntensity;\n"
132   "  image *= 1.0 - clamp(bloom, 0.0, 1.0);\n" // darken base where bloom is strong, to prevent excessive burn-out of result
133   "  gl_FragColor = image + bloom;\n"
134   "}\n";
135
136 } // namespace
137
138
139
140 BloomView::BloomView()
141 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
142   mBlurNumSamples(BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_NUM_SAMPLES),
143   mBlurBellCurveWidth(BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_BELL_CURVE_WIDTH),
144   mPixelFormat(BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_RENDER_TARGET_PIXEL_FORMAT),
145   mDownsampleWidthScale(BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_WIDTH_SCALE),
146   mDownsampleHeightScale(BLOOM_GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_HEIGHT_SCALE),
147   mDownsampledWidth( 0.0f ),
148   mDownsampledHeight( 0.0f ),
149   mTargetSize(Vector2::ZERO),
150   mLastSize(Vector2::ZERO),
151   mChildrenRoot(Actor::New()),
152   mInternalRoot(Actor::New() ),
153   mBloomThresholdPropertyIndex(Property::INVALID_INDEX),
154   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
155   mBloomIntensityPropertyIndex(Property::INVALID_INDEX),
156   mBloomSaturationPropertyIndex(Property::INVALID_INDEX),
157   mImageIntensityPropertyIndex(Property::INVALID_INDEX),
158   mImageSaturationPropertyIndex(Property::INVALID_INDEX),
159   mActivated( false )
160 {
161 }
162
163 BloomView::BloomView( const unsigned int blurNumSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat,
164                                     const float downsampleWidthScale, const float downsampleHeightScale)
165 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
166   mBlurNumSamples(blurNumSamples),
167   mBlurBellCurveWidth(blurBellCurveWidth),
168   mPixelFormat(renderTargetPixelFormat),
169   mDownsampleWidthScale(downsampleWidthScale),
170   mDownsampleHeightScale(downsampleHeightScale),
171   mDownsampledWidth( 0.0f ),
172   mDownsampledHeight( 0.0f ),
173   mTargetSize(Vector2::ZERO),
174   mLastSize(Vector2::ZERO),
175   mChildrenRoot(Actor::New()),
176   mInternalRoot(Actor::New()),
177   mBloomThresholdPropertyIndex(Property::INVALID_INDEX),
178   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
179   mBloomIntensityPropertyIndex(Property::INVALID_INDEX),
180   mBloomSaturationPropertyIndex(Property::INVALID_INDEX),
181   mImageIntensityPropertyIndex(Property::INVALID_INDEX),
182   mImageSaturationPropertyIndex(Property::INVALID_INDEX),
183   mActivated( false )
184 {
185 }
186
187 BloomView::~BloomView()
188 {
189 }
190
191 Toolkit::BloomView BloomView::New()
192 {
193   BloomView* impl = new BloomView();
194
195   Dali::Toolkit::BloomView handle = Dali::Toolkit::BloomView( *impl );
196
197   // Second-phase init of the implementation
198   // This can only be done after the CustomActor connection has been made...
199   impl->Initialize();
200
201   return handle;
202 }
203
204 Toolkit::BloomView BloomView::New(const unsigned int blurNumSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat,
205                                                 const float downsampleWidthScale, const float downsampleHeightScale)
206 {
207   BloomView* impl = new BloomView( blurNumSamples, blurBellCurveWidth, renderTargetPixelFormat, downsampleWidthScale, downsampleHeightScale);
208
209   Dali::Toolkit::BloomView handle = Dali::Toolkit::BloomView( *impl );
210
211   // Second-phase init of the implementation
212   // This can only be done after the CustomActor connection has been made...
213   impl->Initialize();
214
215   return handle;
216 }
217
218 ///////////////////////////////////////////////////////////
219 //
220 // Private methods
221 //
222
223 void BloomView::OnInitialize()
224 {
225   // root actor to parent all user added actors, needed to allow us to set that subtree as exclusive for our child render task
226   mChildrenRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
227   mInternalRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
228
229   //////////////////////////////////////////////////////
230   // Create actors
231
232   // Create an image view for rendering from the scene texture to the bloom texture
233   mBloomExtractActor = Actor::New();
234   mBloomExtractActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
235
236   // Create an image view for compositing the result (scene and bloom textures) to output
237   mCompositeActor = Actor::New();
238   mCompositeActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
239
240   // Create an image view for holding final result, i.e. the blurred image. This will get rendered to screen later, via default / user render task
241   mTargetActor = Actor::New();
242   mTargetActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
243
244   // Create the Gaussian Blur object + render tasks
245   // Note that we use mBloomExtractTarget as the source image and also re-use this as the gaussian blur final render target. This saves the gaussian blur code from creating it
246   // render targets etc internally, so we make better use of resources
247   // Note, this also internally creates the render tasks used by the Gaussian blur, this must occur after the bloom extraction and before the compositing
248   mGaussianBlurView = Dali::Toolkit::GaussianBlurView::New(mBlurNumSamples, mBlurBellCurveWidth, mPixelFormat, mDownsampleWidthScale, mDownsampleHeightScale, true);
249   mGaussianBlurView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
250
251
252   //////////////////////////////////////////////////////
253   // Create cameras for the renders corresponding to the (potentially downsampled) render targets' size
254   mRenderDownsampledCamera = CameraActor::New();
255   mRenderDownsampledCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
256   mRenderDownsampledCamera.SetInvertYAxis( true );
257
258   mRenderFullSizeCamera = CameraActor::New();
259   mRenderFullSizeCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
260   mRenderFullSizeCamera.SetInvertYAxis( true );
261
262
263   ////////////////////////////////
264   // Connect to actor tree
265   Self().Add( mChildrenRoot );
266   Self().Add( mInternalRoot );
267   mInternalRoot.Add( mBloomExtractActor );
268   mInternalRoot.Add( mGaussianBlurView );
269   mInternalRoot.Add( mCompositeActor );
270   mInternalRoot.Add( mTargetActor );
271   mInternalRoot.Add( mRenderDownsampledCamera );
272   mInternalRoot.Add( mRenderFullSizeCamera );
273
274   // bind properties for / set shader constants to defaults
275   SetupProperties();
276
277   DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
278     return std::unique_ptr< Dali::Accessibility::Accessible >(
279       new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::ANIMATION ) );
280   } );
281 }
282
283 void BloomView::OnSizeSet(const Vector3& targetSize)
284 {
285   mTargetSize = Vector2(targetSize);
286   mChildrenRoot.SetProperty( Actor::Property::SIZE, targetSize);
287   mCompositeActor.SetProperty( Actor::Property::SIZE, targetSize);
288   mTargetActor.SetProperty( Actor::Property::SIZE, targetSize);
289
290   // Children render camera must move when GaussianBlurView object is
291   // resized. This is since we cannot change render target size - so we need
292   // to remap the child actors' rendering accordingly so they still exactly
293   // fill the render target. Note that this means the effective resolution of
294   // the child render changes as the GaussianBlurView object changes size,
295   // this is the trade off for not being able to modify render target size
296   // Change camera z position based on GaussianBlurView actor height
297   float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f);
298   mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION_Z,  mTargetSize.height * cameraPosConstraintScale);
299
300   // if we have already activated the blur, need to update render target sizes now to reflect the new size of this actor
301   if(mActivated)
302   {
303     Deactivate();
304     Activate();
305   }
306
307   Control::OnSizeSet( targetSize );
308 }
309
310 void BloomView::OnChildAdd( Actor& child )
311 {
312   if( child != mChildrenRoot && child != mInternalRoot)
313   {
314     mChildrenRoot.Add( child );
315   }
316
317   Control::OnChildAdd( child );
318 }
319
320 void BloomView::OnChildRemove( Actor& child )
321 {
322   mChildrenRoot.Remove( child );
323
324   Control::OnChildRemove( child );
325 }
326
327 void BloomView::AllocateResources()
328 {
329   // size of render targets etc is based on the size of this actor, ignoring z
330   if(mTargetSize != mLastSize || !mActivated)
331   {
332     mLastSize = mTargetSize;
333
334     // get size of downsampled render targets
335     mDownsampledWidth = mTargetSize.width * mDownsampleWidthScale;
336     mDownsampledHeight = mTargetSize.height * mDownsampleHeightScale;
337
338
339     //////////////////////////////////////////////////////
340     // Create cameras
341
342     // Create and place a camera for the renders corresponding to the (potentially downsampled) render targets' size
343     mRenderDownsampledCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
344     mRenderDownsampledCamera.SetNearClippingPlane(1.0f);
345     mRenderDownsampledCamera.SetAspectRatio(mDownsampledWidth / mDownsampledHeight);
346     mRenderDownsampledCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
347
348     mRenderDownsampledCamera.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, ( ( mDownsampledHeight * 0.5f ) / tanf( ARBITRARY_FIELD_OF_VIEW * 0.5f ) ) ) );
349
350     // Create and place a camera for the children render, corresponding to its render target size
351     mRenderFullSizeCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
352     mRenderFullSizeCamera.SetNearClippingPlane(1.0f);
353     mRenderFullSizeCamera.SetAspectRatio(mTargetSize.width / mTargetSize.height);
354     mRenderFullSizeCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
355
356     float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f);
357     mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale ) );
358
359     //////////////////////////////////////////////////////
360     // Pass size change onto GaussianBlurView, so it matches
361     mGaussianBlurView.SetProperty( Actor::Property::SIZE, mTargetSize );
362     GetImpl(mGaussianBlurView).AllocateResources();
363
364     mGaussianBlurView.SetProperty( Actor::Property::VISIBLE, true );
365
366     //////////////////////////////////////////////////////
367     // Create render targets
368
369     // create off screen buffer of new size to render our child actors to
370     mRenderTargetForRenderingChildren = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
371     Texture textureForChildren = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
372     mRenderTargetForRenderingChildren.AttachColorTexture( textureForChildren );
373
374     mBloomExtractTarget = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE );
375     Texture texture = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight) );
376     mBloomExtractTarget.AttachColorTexture( texture );
377
378     FrameBuffer blurExtractTarget = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE );
379     texture = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight) );
380     blurExtractTarget.AttachColorTexture( texture );
381
382     mOutputRenderTarget = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
383     Texture outputTexture = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
384     mOutputRenderTarget.AttachColorTexture( outputTexture );
385
386     //////////////////////////////////////////////////////
387     // Point actors and render tasks at new render targets
388
389     Renderer bloomRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, BLOOM_EXTRACT_FRAGMENT_SOURCE );
390     SetRendererTexture( bloomRenderer, mRenderTargetForRenderingChildren );
391     mBloomExtractActor.AddRenderer( bloomRenderer );
392     mBloomExtractActor.SetProperty( Actor::Property::SIZE, Vector2( mDownsampledWidth, mDownsampledHeight ) ); // size needs to match render target
393
394     // set GaussianBlurView to blur our extracted bloom
395     mGaussianBlurView.SetUserImageAndOutputRenderTarget( mBloomExtractTarget.GetColorTexture(), blurExtractTarget );
396
397     // use the completed blur in the first buffer and composite with the original child actors render
398     Renderer compositeRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, COMPOSITE_FRAGMENT_SOURCE );
399     SetRendererTexture( compositeRenderer, mRenderTargetForRenderingChildren );
400     TextureSet textureSet = compositeRenderer.GetTextures();
401     textureSet.SetTexture( 0u, mRenderTargetForRenderingChildren.GetColorTexture() );
402     textureSet.SetTexture( 1u, blurExtractTarget.GetColorTexture() );
403     mCompositeActor.AddRenderer( compositeRenderer );
404
405     // set up target actor for rendering result, i.e. the blurred image
406     Renderer targetRenderer = CreateRenderer( BASIC_VERTEX_SOURCE, BASIC_FRAGMENT_SOURCE );
407     SetRendererTexture( targetRenderer, mOutputRenderTarget );
408     mTargetActor.AddRenderer( targetRenderer );
409   }
410 }
411
412 void BloomView::CreateRenderTasks()
413 {
414   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
415
416   // create render task to render our child actors to offscreen buffer
417   mRenderChildrenTask = taskList.CreateTask();
418   mRenderChildrenTask.SetSourceActor( mChildrenRoot );
419   mRenderChildrenTask.SetExclusive(true);
420   mRenderChildrenTask.SetInputEnabled( false );
421   mRenderChildrenTask.SetClearEnabled( true );
422   mRenderChildrenTask.SetCameraActor(mRenderFullSizeCamera); // use camera that covers render target exactly
423   mRenderChildrenTask.SetFrameBuffer( mRenderTargetForRenderingChildren );
424
425   // Extract the bright part of the image and render to a new buffer. Downsampling also occurs at this stage to save pixel fill, if it is set up.
426   mBloomExtractTask = taskList.CreateTask();
427   mBloomExtractTask.SetSourceActor( mBloomExtractActor );
428   mBloomExtractTask.SetExclusive(true);
429   mBloomExtractTask.SetInputEnabled( false );
430   mBloomExtractTask.SetClearEnabled( true );
431   mBloomExtractTask.SetCameraActor(mRenderDownsampledCamera);
432   mBloomExtractTask.SetFrameBuffer( mBloomExtractTarget );
433
434   // GaussianBlurView tasks must be created here, so they are executed in the correct order with respect to BloomView tasks
435   GetImpl(mGaussianBlurView).CreateRenderTasks();
436
437   // Use an image view displaying the children render and composite it with the blurred bloom buffer, targeting the output
438   mCompositeTask = taskList.CreateTask();
439   mCompositeTask.SetSourceActor( mCompositeActor );
440   mCompositeTask.SetExclusive(true);
441   mCompositeTask.SetInputEnabled( false );
442   mCompositeTask.SetClearEnabled( true );
443   mCompositeTask.SetCameraActor(mRenderFullSizeCamera);
444   mCompositeTask.SetFrameBuffer( mOutputRenderTarget );
445 }
446
447 void BloomView::RemoveRenderTasks()
448 {
449   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
450
451   taskList.RemoveTask(mRenderChildrenTask);
452   taskList.RemoveTask(mBloomExtractTask);
453
454   GetImpl(mGaussianBlurView).RemoveRenderTasks();
455
456   taskList.RemoveTask(mCompositeTask);
457 }
458
459 void BloomView::Activate()
460 {
461   // make sure resources are allocated and start the render tasks processing
462   AllocateResources();
463   CreateRenderTasks();
464   mActivated = true;
465 }
466
467 void BloomView::Deactivate()
468 {
469   // stop render tasks processing
470   // Note: render target resources are automatically freed since we set the Image::Unused flag
471   RemoveRenderTasks();
472
473   mRenderTargetForRenderingChildren.Reset();
474   mBloomExtractTarget.Reset();
475   mOutputRenderTarget.Reset();
476
477   // Reset children
478   mBloomExtractActor.RemoveRenderer( 0u );
479   mTargetActor.RemoveRenderer( 0u );
480   mCompositeActor.RemoveRenderer( 0u );
481
482   mGaussianBlurView.SetProperty( Actor::Property::VISIBLE, false );
483
484   mActivated = false;
485 }
486
487 /**
488  * RecipOneMinusConstraint
489  *
490  * f(current, property) = property
491  */
492 struct RecipOneMinusConstraint
493 {
494   RecipOneMinusConstraint(){}
495
496   void operator()( float& current, const PropertyInputContainer& inputs )
497   {
498     current = 1.0f / ( 1.0f - inputs[0]->GetFloat() );
499   }
500 };
501
502 // create properties and constraints to tie internal shader etc settings to BloomView object. User can therefore animate / set them via BloomView object without knowing about
503 // internal implementation classes
504 void BloomView::SetupProperties()
505 {
506   CustomActor self = Self();
507
508
509   ///////////////////////////////////////////
510   // bloom threshold
511
512   // set defaults, makes sure properties are registered with shader
513   mBloomExtractActor.RegisterProperty( BLOOM_THRESHOLD_PROPERTY_NAME, BLOOM_THRESHOLD_DEFAULT );
514   mBloomExtractActor.RegisterProperty( RECIP_ONE_MINUS_BLOOM_THRESHOLD_PROPERTY_NAME, 1.0f / (1.0f - BLOOM_THRESHOLD_DEFAULT) );
515
516   // Register a property that the user can control to change the bloom threshold
517   mBloomThresholdPropertyIndex = self.RegisterProperty(BLOOM_THRESHOLD_PROPERTY_NAME, BLOOM_THRESHOLD_DEFAULT);
518   Property::Index shaderBloomThresholdPropertyIndex = mBloomExtractActor.GetPropertyIndex(BLOOM_THRESHOLD_PROPERTY_NAME);
519   Constraint bloomThresholdConstraint = Constraint::New<float>( mBloomExtractActor, shaderBloomThresholdPropertyIndex, EqualToConstraint());
520   bloomThresholdConstraint.AddSource( Source(self, mBloomThresholdPropertyIndex) );
521   bloomThresholdConstraint.Apply();
522
523   // precalc 1.0 / (1.0 - threshold) on CPU to save shader insns, using constraint to tie to the normal threshold property
524   Property::Index shaderRecipOneMinusBloomThresholdPropertyIndex = mBloomExtractActor.GetPropertyIndex(RECIP_ONE_MINUS_BLOOM_THRESHOLD_PROPERTY_NAME);
525   Constraint thresholdConstraint = Constraint::New<float>( mBloomExtractActor, shaderRecipOneMinusBloomThresholdPropertyIndex, RecipOneMinusConstraint());
526   thresholdConstraint.AddSource( LocalSource(shaderBloomThresholdPropertyIndex) );
527   thresholdConstraint.Apply();
528
529
530   ////////////////////////////////////////////
531   // bloom strength
532
533   // Register a property that the user can control to fade the blur in / out via internal GaussianBlurView object
534   mBlurStrengthPropertyIndex = self.RegisterProperty(BLOOM_BLUR_STRENGTH_PROPERTY_NAME, BLOOM_BLUR_STRENGTH_DEFAULT);
535   Constraint blurStrengthConstraint = Constraint::New<float>( mGaussianBlurView, mGaussianBlurView.GetBlurStrengthPropertyIndex(), EqualToConstraint());
536   blurStrengthConstraint.AddSource( Source(self, mBlurStrengthPropertyIndex) );
537   blurStrengthConstraint.Apply();
538
539
540   ////////////////////////////////////////////
541   // bloom intensity
542
543   // Register a property that the user can control to fade the bloom intensity via internally hidden shader
544   mBloomIntensityPropertyIndex = self.RegisterProperty(BLOOM_INTENSITY_PROPERTY_NAME, BLOOM_INTENSITY_DEFAULT);
545   mCompositeActor.RegisterProperty( BLOOM_INTENSITY_PROPERTY_NAME, BLOOM_INTENSITY_DEFAULT );
546   Property::Index shaderBloomIntensityPropertyIndex = mCompositeActor.GetPropertyIndex(BLOOM_INTENSITY_PROPERTY_NAME);
547   Constraint bloomIntensityConstraint = Constraint::New<float>( mCompositeActor, shaderBloomIntensityPropertyIndex, EqualToConstraint());
548   bloomIntensityConstraint.AddSource( Source(self, mBloomIntensityPropertyIndex) );
549   bloomIntensityConstraint.Apply();
550
551
552   ////////////////////////////////////////////
553   // bloom saturation
554
555   // Register a property that the user can control to fade the bloom saturation via internally hidden shader
556   mBloomSaturationPropertyIndex = self.RegisterProperty(BLOOM_SATURATION_PROPERTY_NAME, BLOOM_SATURATION_DEFAULT);
557   mCompositeActor.RegisterProperty( BLOOM_SATURATION_PROPERTY_NAME, BLOOM_SATURATION_DEFAULT );
558   Property::Index shaderBloomSaturationPropertyIndex = mCompositeActor.GetPropertyIndex(BLOOM_SATURATION_PROPERTY_NAME);
559   Constraint bloomSaturationConstraint = Constraint::New<float>( mCompositeActor, shaderBloomSaturationPropertyIndex, EqualToConstraint());
560   bloomSaturationConstraint.AddSource( Source(self, mBloomSaturationPropertyIndex) );
561   bloomSaturationConstraint.Apply();
562
563
564   ////////////////////////////////////////////
565   // image intensity
566
567   // Register a property that the user can control to fade the image intensity via internally hidden shader
568   mImageIntensityPropertyIndex = self.RegisterProperty(IMAGE_INTENSITY_PROPERTY_NAME, IMAGE_INTENSITY_DEFAULT);
569   mCompositeActor.RegisterProperty( IMAGE_INTENSITY_PROPERTY_NAME, IMAGE_INTENSITY_DEFAULT );
570   Property::Index shaderImageIntensityPropertyIndex = mCompositeActor.GetPropertyIndex(IMAGE_INTENSITY_PROPERTY_NAME);
571   Constraint imageIntensityConstraint = Constraint::New<float>( mCompositeActor, shaderImageIntensityPropertyIndex, EqualToConstraint());
572   imageIntensityConstraint.AddSource( Source(self, mImageIntensityPropertyIndex) );
573   imageIntensityConstraint.Apply();
574
575
576   ////////////////////////////////////////////
577   // image saturation
578
579   // Register a property that the user can control to fade the image saturation via internally hidden shader
580   mImageSaturationPropertyIndex = self.RegisterProperty(IMAGE_SATURATION_PROPERTY_NAME, IMAGE_SATURATION_DEFAULT);
581   mCompositeActor.RegisterProperty( IMAGE_SATURATION_PROPERTY_NAME, IMAGE_SATURATION_DEFAULT );
582   Property::Index shaderImageSaturationPropertyIndex = mCompositeActor.GetPropertyIndex(IMAGE_SATURATION_PROPERTY_NAME);
583   Constraint imageSaturationConstraint = Constraint::New<float>( mCompositeActor, shaderImageSaturationPropertyIndex, EqualToConstraint());
584   imageSaturationConstraint.AddSource( Source(self, mImageSaturationPropertyIndex) );
585   imageSaturationConstraint.Apply();
586 }
587
588 } // namespace Internal
589
590 } // namespace Toolkit
591
592 } // namespace Dali