2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "blur-two-pass-filter.h"
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/devel-api/common/stage.h>
25 #include <dali/public-api/object/property-map.h>
26 #include <dali/public-api/render-tasks/render-task-list.h>
27 #include <dali/public-api/rendering/renderer.h>
30 #include <dali-toolkit/internal/controls/control/control-renderers.h>
44 const float DEFAULT_KERNEL0[] = { 12.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f };
46 const float DEFAULT_KERNEL1[] = { 8.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.25f/16.0f,
49 const float DEFAULT_KERNEL2[] = { 5.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.75f/16.0f,
50 1.75f/16.0f, 1.5f/16.0f, 1.5f/16.0f };
52 const float DEFAULT_KERNEL3[] = { 3.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f,
53 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 0.5f/16.0f,
56 const float DEFAULT_KERNEL4[] = { 2.0f/16.0f, 1.5f/16.0f, 1.5f/16.0f, 1.5f/16.0f,
57 1.5f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f,
58 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 0.5f/16.0f,
59 0.5f/16.0f, 0.5f/16.0f, 0.5f/16.0f };
61 const char* BLUR_TWO_PASS_FRAGMENT_SOURCE =
63 "precision highp float;\n"
64 "varying mediump vec2 vTexCoord;\n"
65 "uniform sampler2D sTexture;\n"
66 "uniform vec2 uSampleOffsets[NUM_SAMPLES];\n"
67 "uniform float uSampleWeights[NUM_SAMPLES];\n"
70 " vec4 color = vec4(0.0);\n"
71 " for( int i = 0; i < NUM_SAMPLES; ++i )\n"
73 " color += texture2D( sTexture, vTexCoord + uSampleOffsets[i] ) * uSampleWeights[i];\n"
75 " gl_FragColor = color;\n"
79 std::string GetOffsetUniformName( int index )
81 std::ostringstream oss;
82 oss << "uSampleOffsets[" << index << "]";
86 std::string GetWeightUniformName( int index )
88 std::ostringstream oss;
89 oss << "uSampleWeights[" << index << "]";
93 const char* BLEND_TWO_IMAGES_FRAGMENT_SOURCE =
95 "precision highp float;\n"
96 "uniform float uBlurStrength;\n "
97 "uniform sampler2D sTexture;\n"
98 "uniform sampler2D sEffect;\n"
99 "varying mediump vec2 vTexCoord;\n"
102 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uBlurStrength"
103 " + texture2D( sEffect, vTexCoord )*(1.0-uBlurStrength); \n"
107 const char* const BLUR_STRENGTH_UNIFORM_NAME( "uBlurStrength" );
108 const char* const EFFECT_IMAGE_NAME( "sEffect" );
113 BlurTwoPassFilter::BlurTwoPassFilter()
116 // create blending actor and register the property in constructor
117 // to make sure that GetBlurStrengthPropertyIndex() always returns a valid index
118 mActorForBlending = Actor::New();
119 mBlurStrengthPropertyIndex = mActorForBlending.RegisterProperty( BLUR_STRENGTH_UNIFORM_NAME, 1.f );
122 BlurTwoPassFilter::~BlurTwoPassFilter()
126 void BlurTwoPassFilter::Enable()
128 // create custom shader effect
129 if( !GetKernelSize() )
131 CreateKernel( DEFAULT_KERNEL4, sizeof(DEFAULT_KERNEL4)/sizeof(DEFAULT_KERNEL4[0]) );
133 int kernelSize( static_cast< int >(GetKernelSize()) );
135 // Set up blur-two-pass custom shader
136 std::ostringstream sstream;
137 sstream << "#define NUM_SAMPLES " << kernelSize << "\n";
138 sstream << BLUR_TWO_PASS_FRAGMENT_SOURCE;
139 std::string fragmentSource( sstream.str() );
141 // create actor to render input with applied emboss effect
142 mActorForInput = Actor::New();
143 mActorForInput.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
144 mActorForInput.SetProperty( Actor::Property::SIZE, mTargetSize );
145 Renderer rendererForInput = CreateRenderer( BASIC_VERTEX_SOURCE, fragmentSource.c_str() );
146 SetRendererTexture( rendererForInput, mInputTexture );
147 mActorForInput.AddRenderer( rendererForInput );
149 // create internal offscreen for result of horizontal pass
150 mFrameBufferForHorz = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
151 Texture textureForHorz = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
152 mFrameBufferForHorz.AttachColorTexture( textureForHorz );
154 // create an actor to render mImageForHorz for vertical blur pass
155 mActorForHorz = Actor::New();
156 mActorForHorz.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
157 mActorForHorz.SetProperty( Actor::Property::SIZE, mTargetSize );
158 Renderer rendererForHorz = CreateRenderer( BASIC_VERTEX_SOURCE, fragmentSource.c_str() );
159 SetRendererTexture( rendererForHorz, textureForHorz );
160 mActorForHorz.AddRenderer( rendererForHorz );
162 // create internal offscreen for result of the two pass blurred image
163 mBlurredFrameBuffer = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
164 Texture blurredTexture = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
165 mBlurredFrameBuffer.AttachColorTexture( blurredTexture );
167 // create an actor to blend the blurred image and the input image with the given blur strength
168 Renderer rendererForBlending = CreateRenderer( BASIC_VERTEX_SOURCE, BLEND_TWO_IMAGES_FRAGMENT_SOURCE );
169 TextureSet textureSetForBlending = rendererForBlending.GetTextures();
170 textureSetForBlending.SetTexture( 0u, blurredTexture );
171 textureSetForBlending.SetTexture( 1u, mInputTexture );
172 mActorForBlending.AddRenderer( rendererForBlending );
173 mActorForBlending.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
174 mActorForBlending.SetProperty( Actor::Property::SIZE, mTargetSize );
176 for( int i = 0; i < kernelSize; ++i )
178 const std::string offsetUniform( GetOffsetUniformName( i ) );
179 const std::string weightUniform( GetWeightUniformName( i ) );
181 mActorForInput.RegisterProperty( offsetUniform, Vector2(mKernel[i]) * Vector2::XAXIS );
182 mActorForInput.RegisterProperty( weightUniform, mKernel[i].z );
184 mActorForHorz.RegisterProperty( offsetUniform, Vector2(mKernel[i]) * Vector2::YAXIS );
185 mActorForHorz.RegisterProperty( weightUniform, mKernel[i].z );
188 mRootActor.Add( mActorForInput );
189 mRootActor.Add( mActorForHorz );
190 mRootActor.Add( mActorForBlending );
196 void BlurTwoPassFilter::Disable()
202 mRootActor.Remove( mCameraActor );
203 mCameraActor.Reset();
208 mRootActor.Remove( mActorForInput );
209 mActorForInput.Reset();
214 mRootActor.Remove( mActorForHorz );
215 mActorForHorz.Reset();
218 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
220 if( mRenderTaskForHorz )
222 taskList.RemoveTask(mRenderTaskForHorz);
224 if( mRenderTaskForVert )
226 taskList.RemoveTask(mRenderTaskForVert);
228 if( mRenderTaskForBlending )
230 taskList.RemoveTask(mRenderTaskForBlending);
237 void BlurTwoPassFilter::Refresh()
239 if( mRenderTaskForHorz )
241 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
244 if( mRenderTaskForVert )
246 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
249 if( mRenderTaskForBlending )
251 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
255 void BlurTwoPassFilter::SetSize( const Vector2& size )
260 mActorForInput.SetProperty( Actor::Property::SIZE, mTargetSize);
264 mActorForHorz.SetProperty( Actor::Property::SIZE, mTargetSize);
266 if( mActorForBlending )
268 mActorForBlending.SetProperty( Actor::Property::SIZE, mTargetSize);
272 Handle BlurTwoPassFilter::GetHandleForAnimateBlurStrength()
274 return mActorForBlending;
277 void BlurTwoPassFilter::CreateRenderTasks()
279 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
281 // perform a horizontal blur targeting the internal buffer
282 mRenderTaskForHorz = taskList.CreateTask();
283 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
284 mRenderTaskForHorz.SetSourceActor( mActorForInput );
285 mRenderTaskForHorz.SetExclusive(true);
286 mRenderTaskForHorz.SetInputEnabled( false );
287 mRenderTaskForHorz.SetClearEnabled( true );
288 mRenderTaskForHorz.SetClearColor( mBackgroundColor );
289 mRenderTaskForHorz.SetFrameBuffer( mFrameBufferForHorz );
290 mRenderTaskForHorz.SetCameraActor( mCameraActor );
292 // use the internal buffer and perform a horizontal blur targeting the output buffer
293 mRenderTaskForVert = taskList.CreateTask();
294 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
295 mRenderTaskForVert.SetSourceActor( mActorForHorz );
296 mRenderTaskForVert.SetExclusive(true);
297 mRenderTaskForVert.SetInputEnabled( false );
298 mRenderTaskForVert.SetClearEnabled( true );
299 mRenderTaskForVert.SetClearColor( mBackgroundColor );
300 mRenderTaskForVert.SetFrameBuffer( mBlurredFrameBuffer );
301 mRenderTaskForVert.SetCameraActor( mCameraActor );
303 //Perform a blending between the blurred image and the input image
304 mRenderTaskForBlending = taskList.CreateTask();
305 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
306 mRenderTaskForBlending.SetSourceActor( mActorForBlending );
307 mRenderTaskForBlending.SetExclusive(true);
308 mRenderTaskForBlending.SetInputEnabled( false );
309 mRenderTaskForBlending.SetClearEnabled( true );
310 mRenderTaskForBlending.SetClearColor( mBackgroundColor );
311 mRenderTaskForBlending.SetFrameBuffer( mOutputFrameBuffer );
312 mRenderTaskForBlending.SetCameraActor( mCameraActor );
315 } // namespace Internal
317 } // namespace Toolkit