2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "blur-two-pass-filter.h"
37 const float DEFAULT_KERNEL0[] = { 12.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f };
39 const float DEFAULT_KERNEL1[] = { 8.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.25f/16.0f,
42 const float DEFAULT_KERNEL2[] = { 5.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.75f/16.0f,
43 1.75f/16.0f, 1.5f/16.0f, 1.5f/16.0f };
45 const float DEFAULT_KERNEL3[] = { 3.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f,
46 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 0.5f/16.0f,
49 const float DEFAULT_KERNEL4[] = { 2.0f/16.0f, 1.5f/16.0f, 1.5f/16.0f, 1.5f/16.0f,
50 1.5f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f,
51 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 0.5f/16.0f,
52 0.5f/16.0f, 0.5f/16.0f, 0.5f/16.0f };
55 const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
57 const char* BLUR_TWO_PASS_FRAGMENT_SOURCE =
59 "uniform vec2 uSampleOffsets[NUM_SAMPLES];\n"
60 "uniform float uSampleWeights[NUM_SAMPLES];\n"
63 " vec4 color = vec4(0.0);\n"
64 "# ifdef DEBUG_RENDER\n"
65 " if( vTexCoord.s < 0.495 )\n"
67 "# endif //def DEBUG_RENDER\n"
68 " for( int i = 0; i < NUM_SAMPLES; ++i )\n"
70 " color += texture2D( sTexture, vTexCoord + uSampleOffsets[i] ) * uSampleWeights[i];\n"
72 "# ifdef DEBUG_RENDER\n"
74 " else if( vTexCoord.s > 0.505 )\n"
76 " color = texture2D( sTexture, vTexCoord );\n"
80 " color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
82 "# endif //def DEBUG_RENDER\n"
83 " gl_FragColor = color;\n"
87 std::string GetOffsetUniformName( int index )
89 std::ostringstream oss;
90 oss << "uSampleOffsets[" << index << "]";
94 std::string GetWeightUniformName( int index )
96 std::ostringstream oss;
97 oss << "uSampleWeights[" << index << "]";
101 const char* BLEND_TWO_IMAGES_FRAGMENT_SOURCE =
103 "precision highp float;\n"
104 "uniform float uBlurStrength; \n "
107 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uBlurStrength"
108 " + texture2D( sEffect, vTexCoord )*(1.0-uBlurStrength); \n"
112 std::string GetBlurStrengthUniformName()
114 return "uBlurStrength";
120 BlurTwoPassFilter::BlurTwoPassFilter()
123 mShaderForBlending = ShaderEffect::New( "", BLEND_TWO_IMAGES_FRAGMENT_SOURCE );
124 mShaderForBlending.SetUniform( GetBlurStrengthUniformName(), 1.f );
125 mBlurStrengthPropertyIndex = mShaderForBlending.GetPropertyIndex( GetBlurStrengthUniformName() );
128 BlurTwoPassFilter::~BlurTwoPassFilter()
132 void BlurTwoPassFilter::Enable()
134 mCameraForBlur = CameraActor::New();
135 mCameraForBlur.SetParentOrigin(ParentOrigin::CENTER);
137 // create actor to render input with applied emboss effect
138 mActorForInput = ImageActor::New( mInputImage );
139 mActorForInput.SetParentOrigin( ParentOrigin::CENTER );
140 mActorForInput.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
141 mActorForInput.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
143 // create internal offscreen for result of horizontal pass
144 mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
146 // create an actor to render mImageForHorz for vertical blur pass
147 mActorForHorz = ImageActor::New( mImageForHorz );
148 mActorForHorz.SetParentOrigin( ParentOrigin::CENTER );
149 mActorForHorz.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
150 mActorForHorz.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
152 // create internal offscreen for result of the two pass blurred image
153 mBlurredImage = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
155 // create an actor to blend the blurred image and the input image with the given blur strength
156 mActorForBlending = ImageActor::New( mBlurredImage );
157 mActorForBlending.SetParentOrigin( ParentOrigin::CENTER );
158 mActorForBlending.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
159 mActorForBlending.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
161 mRootActor.Add( mActorForInput );
162 mRootActor.Add( mActorForHorz );
163 mRootActor.Add( mActorForBlending );
164 mRootActor.Add( mCameraForBlur );
166 // create custom shader effect
167 if( !GetKernelSize() )
169 CreateKernel( DEFAULT_KERNEL4, sizeof(DEFAULT_KERNEL4)/sizeof(DEFAULT_KERNEL4[0]) );
171 int kernelSize( static_cast< int >(GetKernelSize()) );
173 std::ostringstream fragmentSource;
176 fragmentSource << "#define DEBUG_RENDER\n";
178 fragmentSource << "#define NUM_SAMPLES " << kernelSize << "\n";
179 fragmentSource << BLUR_TWO_PASS_FRAGMENT_SOURCE;
180 mShaderForHorz = ShaderEffect::New( "", fragmentSource.str() );
181 mActorForInput.SetShaderEffect( mShaderForHorz );
182 mShaderForVert = ShaderEffect::New( "", fragmentSource.str() );
183 mActorForHorz.SetShaderEffect( mShaderForVert );
185 for( int i = 0; i < kernelSize; ++i )
187 const std::string offsetUniform( GetOffsetUniformName( i ) );
188 const std::string weightUniform( GetWeightUniformName( i ) );
190 mShaderForHorz.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::XAXIS );
191 mShaderForHorz.SetUniform( weightUniform, mKernel[i].z );
193 mShaderForVert.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::YAXIS );
194 mShaderForVert.SetUniform( weightUniform, mKernel[i].z );
197 mActorForBlending.SetShaderEffect( mShaderForBlending );
198 mShaderForBlending.SetEffectImage( mInputImage );
204 void BlurTwoPassFilter::Disable()
210 mRootActor.Remove( mCameraForBlur );
211 mCameraForBlur.Reset();
216 mRootActor.Remove( mActorForInput );
217 mActorForInput.Reset();
222 mRootActor.Remove( mActorForHorz );
223 mActorForHorz.Reset();
226 if( mActorForBlending )
228 mRootActor.Remove( mActorForBlending );
229 mActorForBlending.Reset();
232 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
234 if( mRenderTaskForHorz )
236 taskList.RemoveTask(mRenderTaskForHorz);
238 if( mRenderTaskForVert )
240 taskList.RemoveTask(mRenderTaskForVert);
242 if( mRenderTaskForBlending )
244 taskList.RemoveTask(mRenderTaskForBlending);
251 void BlurTwoPassFilter::Refresh()
253 if( mRenderTaskForHorz )
255 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
258 if( mRenderTaskForVert )
260 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
263 if( mRenderTaskForBlending )
265 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
269 Constrainable BlurTwoPassFilter::GetHandleForAnimateBlurStrength()
271 return mShaderForBlending;
274 void BlurTwoPassFilter::SetupCamera()
276 // Create and place a camera for the embossing render, corresponding to its render target size
277 mCameraForBlur.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
278 mCameraForBlur.SetNearClippingPlane(1.0f);
279 mCameraForBlur.SetAspectRatio(mTargetSize.width / mTargetSize.height);
280 mCameraForBlur.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
281 mCameraForBlur.SetRotation(Quaternion(M_PI, Vector3::YAXIS));
282 mCameraForBlur.SetPosition(0.0f, 0.0f, ((mTargetSize.height * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
285 void BlurTwoPassFilter::CreateRenderTasks()
287 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
289 // perform a horizontal blur targeting the internal buffer
290 mRenderTaskForHorz = taskList.CreateTask();
291 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
292 mRenderTaskForHorz.SetSourceActor( mActorForInput );
293 mRenderTaskForHorz.SetExclusive(true);
294 mRenderTaskForHorz.SetInputEnabled( false );
295 mRenderTaskForHorz.SetClearEnabled( true );
296 mRenderTaskForHorz.SetClearColor( mBackgroundColor );
297 mRenderTaskForHorz.SetTargetFrameBuffer( mImageForHorz );
298 mRenderTaskForHorz.SetCameraActor( mCameraForBlur );
300 // use the internal buffer and perform a horizontal blur targeting the output buffer
301 mRenderTaskForVert = taskList.CreateTask();
302 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
303 mRenderTaskForVert.SetSourceActor( mActorForHorz );
304 mRenderTaskForVert.SetExclusive(true);
305 mRenderTaskForVert.SetInputEnabled( false );
306 mRenderTaskForVert.SetClearEnabled( true );
307 mRenderTaskForVert.SetClearColor( mBackgroundColor );
308 mRenderTaskForVert.SetTargetFrameBuffer( mBlurredImage );
309 mRenderTaskForVert.SetCameraActor( mCameraForBlur );
311 //Perform a blending between the blurred image and the input image
312 mRenderTaskForBlending = taskList.CreateTask();
313 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
314 mRenderTaskForBlending.SetSourceActor( mActorForBlending );
315 mRenderTaskForBlending.SetExclusive(true);
316 mRenderTaskForBlending.SetInputEnabled( false );
317 mRenderTaskForBlending.SetClearEnabled( true );
318 mRenderTaskForBlending.SetClearColor( mBackgroundColor );
319 mRenderTaskForBlending.SetTargetFrameBuffer( mOutputImage );
320 mRenderTaskForBlending.SetCameraActor( mCameraForBlur );
323 } // namespace Internal
325 } // namespace Toolkit