2 * Copyright (c) 2014 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/public-api/common/stage.h>
25 #include <dali/public-api/render-tasks/render-task-list.h>
41 const float DEFAULT_KERNEL0[] = { 12.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f };
43 const float DEFAULT_KERNEL1[] = { 8.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.25f/16.0f,
46 const float DEFAULT_KERNEL2[] = { 5.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.75f/16.0f,
47 1.75f/16.0f, 1.5f/16.0f, 1.5f/16.0f };
49 const float DEFAULT_KERNEL3[] = { 3.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f,
50 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 0.5f/16.0f,
53 const float DEFAULT_KERNEL4[] = { 2.0f/16.0f, 1.5f/16.0f, 1.5f/16.0f, 1.5f/16.0f,
54 1.5f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f,
55 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 0.5f/16.0f,
56 0.5f/16.0f, 0.5f/16.0f, 0.5f/16.0f };
59 const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
61 const char* BLUR_TWO_PASS_FRAGMENT_SOURCE =
63 "uniform vec2 uSampleOffsets[NUM_SAMPLES];\n"
64 "uniform float uSampleWeights[NUM_SAMPLES];\n"
67 " vec4 color = vec4(0.0);\n"
68 "# ifdef DEBUG_RENDER\n"
69 " if( vTexCoord.s < 0.495 )\n"
71 "# endif //def DEBUG_RENDER\n"
72 " for( int i = 0; i < NUM_SAMPLES; ++i )\n"
74 " color += texture2D( sTexture, vTexCoord + uSampleOffsets[i] ) * uSampleWeights[i];\n"
76 "# ifdef DEBUG_RENDER\n"
78 " else if( vTexCoord.s > 0.505 )\n"
80 " color = texture2D( sTexture, vTexCoord );\n"
84 " color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
86 "# endif //def DEBUG_RENDER\n"
87 " gl_FragColor = color;\n"
91 std::string GetOffsetUniformName( int index )
93 std::ostringstream oss;
94 oss << "uSampleOffsets[" << index << "]";
98 std::string GetWeightUniformName( int index )
100 std::ostringstream oss;
101 oss << "uSampleWeights[" << index << "]";
105 const char* BLEND_TWO_IMAGES_FRAGMENT_SOURCE =
107 "precision highp float;\n"
108 "uniform float uBlurStrength; \n "
111 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uBlurStrength"
112 " + texture2D( sEffect, vTexCoord )*(1.0-uBlurStrength); \n"
116 std::string GetBlurStrengthUniformName()
118 return "uBlurStrength";
124 BlurTwoPassFilter::BlurTwoPassFilter()
127 mShaderForBlending = ShaderEffect::New( "", BLEND_TWO_IMAGES_FRAGMENT_SOURCE );
128 mShaderForBlending.SetUniform( GetBlurStrengthUniformName(), 1.f );
129 mBlurStrengthPropertyIndex = mShaderForBlending.GetPropertyIndex( GetBlurStrengthUniformName() );
132 BlurTwoPassFilter::~BlurTwoPassFilter()
136 void BlurTwoPassFilter::Enable()
138 mCameraForBlur = CameraActor::New();
139 mCameraForBlur.SetParentOrigin(ParentOrigin::CENTER);
141 // create actor to render input with applied emboss effect
142 mActorForInput = ImageActor::New( mInputImage );
143 mActorForInput.SetParentOrigin( ParentOrigin::CENTER );
144 mActorForInput.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
145 mActorForInput.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
147 // create internal offscreen for result of horizontal pass
148 mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
150 // create an actor to render mImageForHorz for vertical blur pass
151 mActorForHorz = ImageActor::New( mImageForHorz );
152 mActorForHorz.SetParentOrigin( ParentOrigin::CENTER );
153 mActorForHorz.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
154 mActorForHorz.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
156 // create internal offscreen for result of the two pass blurred image
157 mBlurredImage = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
159 // create an actor to blend the blurred image and the input image with the given blur strength
160 mActorForBlending = ImageActor::New( mBlurredImage );
161 mActorForBlending.SetParentOrigin( ParentOrigin::CENTER );
162 mActorForBlending.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
163 mActorForBlending.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
165 mRootActor.Add( mActorForInput );
166 mRootActor.Add( mActorForHorz );
167 mRootActor.Add( mActorForBlending );
168 mRootActor.Add( mCameraForBlur );
170 // create custom shader effect
171 if( !GetKernelSize() )
173 CreateKernel( DEFAULT_KERNEL4, sizeof(DEFAULT_KERNEL4)/sizeof(DEFAULT_KERNEL4[0]) );
175 int kernelSize( static_cast< int >(GetKernelSize()) );
177 std::ostringstream fragmentSource;
180 fragmentSource << "#define DEBUG_RENDER\n";
182 fragmentSource << "#define NUM_SAMPLES " << kernelSize << "\n";
183 fragmentSource << BLUR_TWO_PASS_FRAGMENT_SOURCE;
184 mShaderForHorz = ShaderEffect::New( "", fragmentSource.str() );
185 mActorForInput.SetShaderEffect( mShaderForHorz );
186 mShaderForVert = ShaderEffect::New( "", fragmentSource.str() );
187 mActorForHorz.SetShaderEffect( mShaderForVert );
189 for( int i = 0; i < kernelSize; ++i )
191 const std::string offsetUniform( GetOffsetUniformName( i ) );
192 const std::string weightUniform( GetWeightUniformName( i ) );
194 mShaderForHorz.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::XAXIS );
195 mShaderForHorz.SetUniform( weightUniform, mKernel[i].z );
197 mShaderForVert.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::YAXIS );
198 mShaderForVert.SetUniform( weightUniform, mKernel[i].z );
201 mActorForBlending.SetShaderEffect( mShaderForBlending );
202 mShaderForBlending.SetEffectImage( mInputImage );
208 void BlurTwoPassFilter::Disable()
214 mRootActor.Remove( mCameraForBlur );
215 mCameraForBlur.Reset();
220 mRootActor.Remove( mActorForInput );
221 mActorForInput.Reset();
226 mRootActor.Remove( mActorForHorz );
227 mActorForHorz.Reset();
230 if( mActorForBlending )
232 mRootActor.Remove( mActorForBlending );
233 mActorForBlending.Reset();
236 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
238 if( mRenderTaskForHorz )
240 taskList.RemoveTask(mRenderTaskForHorz);
242 if( mRenderTaskForVert )
244 taskList.RemoveTask(mRenderTaskForVert);
246 if( mRenderTaskForBlending )
248 taskList.RemoveTask(mRenderTaskForBlending);
255 void BlurTwoPassFilter::Refresh()
257 if( mRenderTaskForHorz )
259 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
262 if( mRenderTaskForVert )
264 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
267 if( mRenderTaskForBlending )
269 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
273 Constrainable BlurTwoPassFilter::GetHandleForAnimateBlurStrength()
275 return mShaderForBlending;
278 void BlurTwoPassFilter::SetupCamera()
280 // Create and place a camera for the embossing render, corresponding to its render target size
281 mCameraForBlur.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
282 mCameraForBlur.SetNearClippingPlane(1.0f);
283 mCameraForBlur.SetAspectRatio(mTargetSize.width / mTargetSize.height);
284 mCameraForBlur.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
285 mCameraForBlur.SetRotation(Quaternion(M_PI, Vector3::YAXIS));
286 mCameraForBlur.SetPosition(0.0f, 0.0f, ((mTargetSize.height * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
289 void BlurTwoPassFilter::CreateRenderTasks()
291 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
293 // perform a horizontal blur targeting the internal buffer
294 mRenderTaskForHorz = taskList.CreateTask();
295 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
296 mRenderTaskForHorz.SetSourceActor( mActorForInput );
297 mRenderTaskForHorz.SetExclusive(true);
298 mRenderTaskForHorz.SetInputEnabled( false );
299 mRenderTaskForHorz.SetClearEnabled( true );
300 mRenderTaskForHorz.SetClearColor( mBackgroundColor );
301 mRenderTaskForHorz.SetTargetFrameBuffer( mImageForHorz );
302 mRenderTaskForHorz.SetCameraActor( mCameraForBlur );
304 // use the internal buffer and perform a horizontal blur targeting the output buffer
305 mRenderTaskForVert = taskList.CreateTask();
306 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
307 mRenderTaskForVert.SetSourceActor( mActorForHorz );
308 mRenderTaskForVert.SetExclusive(true);
309 mRenderTaskForVert.SetInputEnabled( false );
310 mRenderTaskForVert.SetClearEnabled( true );
311 mRenderTaskForVert.SetClearColor( mBackgroundColor );
312 mRenderTaskForVert.SetTargetFrameBuffer( mBlurredImage );
313 mRenderTaskForVert.SetCameraActor( mCameraForBlur );
315 //Perform a blending between the blurred image and the input image
316 mRenderTaskForBlending = taskList.CreateTask();
317 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
318 mRenderTaskForBlending.SetSourceActor( mActorForBlending );
319 mRenderTaskForBlending.SetExclusive(true);
320 mRenderTaskForBlending.SetInputEnabled( false );
321 mRenderTaskForBlending.SetClearEnabled( true );
322 mRenderTaskForBlending.SetClearColor( mBackgroundColor );
323 mRenderTaskForBlending.SetTargetFrameBuffer( mOutputImage );
324 mRenderTaskForBlending.SetCameraActor( mCameraForBlur );
327 } // namespace Internal
329 } // namespace Toolkit