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"
38 const float DEFAULT_KERNEL0[] = { 12.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f };
40 const float DEFAULT_KERNEL1[] = { 8.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.25f/16.0f,
43 const float DEFAULT_KERNEL2[] = { 5.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.75f/16.0f,
44 1.75f/16.0f, 1.5f/16.0f, 1.5f/16.0f };
46 const float DEFAULT_KERNEL3[] = { 3.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f,
47 2.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f, 0.5f/16.0f,
50 const float DEFAULT_KERNEL4[] = { 2.0f/16.0f, 1.5f/16.0f, 1.5f/16.0f, 1.5f/16.0f,
51 1.5f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f,
52 1.0f/16.0f, 1.0f/16.0f, 1.0f/16.0f, 0.5f/16.0f,
53 0.5f/16.0f, 0.5f/16.0f, 0.5f/16.0f };
56 const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
58 const char* BLUR_TWO_PASS_FRAGMENT_SOURCE =
60 "uniform vec2 uSampleOffsets[NUM_SAMPLES];\n"
61 "uniform float uSampleWeights[NUM_SAMPLES];\n"
64 " vec4 color = vec4(0.0);\n"
65 "# ifdef DEBUG_RENDER\n"
66 " if( vTexCoord.s < 0.495 )\n"
68 "# endif //def DEBUG_RENDER\n"
69 " for( int i = 0; i < NUM_SAMPLES; ++i )\n"
71 " color += texture2D( sTexture, vTexCoord + uSampleOffsets[i] ) * uSampleWeights[i];\n"
73 "# ifdef DEBUG_RENDER\n"
75 " else if( vTexCoord.s > 0.505 )\n"
77 " color = texture2D( sTexture, vTexCoord );\n"
81 " color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
83 "# endif //def DEBUG_RENDER\n"
84 " gl_FragColor = color;\n"
88 std::string GetOffsetUniformName( int index )
90 std::ostringstream oss;
91 oss << "uSampleOffsets[" << index << "]";
95 std::string GetWeightUniformName( int index )
97 std::ostringstream oss;
98 oss << "uSampleWeights[" << index << "]";
102 const char* BLEND_TWO_IMAGES_FRAGMENT_SOURCE =
104 "precision highp float;\n"
105 "uniform float uBlurStrength; \n "
108 " gl_FragColor = texture2D( sTexture, vTexCoord ) * uBlurStrength"
109 " + texture2D( sEffect, vTexCoord )*(1.0-uBlurStrength); \n"
113 std::string GetBlurStrengthUniformName()
115 return "uBlurStrength";
121 BlurTwoPassFilter::BlurTwoPassFilter()
124 mShaderForBlending = ShaderEffect::New( "", BLEND_TWO_IMAGES_FRAGMENT_SOURCE );
125 mShaderForBlending.SetUniform( GetBlurStrengthUniformName(), 1.f );
126 mBlurStrengthPropertyIndex = mShaderForBlending.GetPropertyIndex( GetBlurStrengthUniformName() );
129 BlurTwoPassFilter::~BlurTwoPassFilter()
133 void BlurTwoPassFilter::Enable()
135 mCameraForBlur = CameraActor::New();
136 mCameraForBlur.SetParentOrigin(ParentOrigin::CENTER);
138 // create actor to render input with applied emboss effect
139 mActorForInput = ImageActor::New( mInputImage );
140 mActorForInput.SetParentOrigin( ParentOrigin::CENTER );
141 mActorForInput.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
142 mActorForInput.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
144 // create internal offscreen for result of horizontal pass
145 mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
147 // create an actor to render mImageForHorz for vertical blur pass
148 mActorForHorz = ImageActor::New( mImageForHorz );
149 mActorForHorz.SetParentOrigin( ParentOrigin::CENTER );
150 mActorForHorz.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
151 mActorForHorz.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
153 // create internal offscreen for result of the two pass blurred image
154 mBlurredImage = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
156 // create an actor to blend the blurred image and the input image with the given blur strength
157 mActorForBlending = ImageActor::New( mBlurredImage );
158 mActorForBlending.SetParentOrigin( ParentOrigin::CENTER );
159 mActorForBlending.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
160 mActorForBlending.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
162 mRootActor.Add( mActorForInput );
163 mRootActor.Add( mActorForHorz );
164 mRootActor.Add( mActorForBlending );
165 mRootActor.Add( mCameraForBlur );
167 // create custom shader effect
168 if( !GetKernelSize() )
170 CreateKernel( DEFAULT_KERNEL4, sizeof(DEFAULT_KERNEL4)/sizeof(DEFAULT_KERNEL4[0]) );
172 int kernelSize( static_cast< int >(GetKernelSize()) );
174 std::ostringstream fragmentSource;
177 fragmentSource << "#define DEBUG_RENDER\n";
179 fragmentSource << "#define NUM_SAMPLES " << kernelSize << "\n";
180 fragmentSource << BLUR_TWO_PASS_FRAGMENT_SOURCE;
181 mShaderForHorz = ShaderEffect::New( "", fragmentSource.str() );
182 mActorForInput.SetShaderEffect( mShaderForHorz );
183 mShaderForVert = ShaderEffect::New( "", fragmentSource.str() );
184 mActorForHorz.SetShaderEffect( mShaderForVert );
186 for( int i = 0; i < kernelSize; ++i )
188 const std::string offsetUniform( GetOffsetUniformName( i ) );
189 const std::string weightUniform( GetWeightUniformName( i ) );
191 mShaderForHorz.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::XAXIS );
192 mShaderForHorz.SetUniform( weightUniform, mKernel[i].z );
194 mShaderForVert.SetUniform( offsetUniform, Vector2(mKernel[i]) * Vector2::YAXIS );
195 mShaderForVert.SetUniform( weightUniform, mKernel[i].z );
198 mActorForBlending.SetShaderEffect( mShaderForBlending );
199 mShaderForBlending.SetEffectImage( mInputImage );
205 void BlurTwoPassFilter::Disable()
211 mRootActor.Remove( mCameraForBlur );
212 mCameraForBlur.Reset();
217 mRootActor.Remove( mActorForInput );
218 mActorForInput.Reset();
223 mRootActor.Remove( mActorForHorz );
224 mActorForHorz.Reset();
227 if( mActorForBlending )
229 mRootActor.Remove( mActorForBlending );
230 mActorForBlending.Reset();
233 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
235 if( mRenderTaskForHorz )
237 taskList.RemoveTask(mRenderTaskForHorz);
239 if( mRenderTaskForVert )
241 taskList.RemoveTask(mRenderTaskForVert);
243 if( mRenderTaskForBlending )
245 taskList.RemoveTask(mRenderTaskForBlending);
252 void BlurTwoPassFilter::Refresh()
254 if( mRenderTaskForHorz )
256 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
259 if( mRenderTaskForVert )
261 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
264 if( mRenderTaskForBlending )
266 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
270 Constrainable BlurTwoPassFilter::GetHandleForAnimateBlurStrength()
272 return mShaderForBlending;
275 void BlurTwoPassFilter::SetupCamera()
277 // Create and place a camera for the embossing render, corresponding to its render target size
278 mCameraForBlur.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
279 mCameraForBlur.SetNearClippingPlane(1.0f);
280 mCameraForBlur.SetAspectRatio(mTargetSize.width / mTargetSize.height);
281 mCameraForBlur.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
282 mCameraForBlur.SetRotation(Quaternion(M_PI, Vector3::YAXIS));
283 mCameraForBlur.SetPosition(0.0f, 0.0f, ((mTargetSize.height * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
286 void BlurTwoPassFilter::CreateRenderTasks()
288 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
290 // perform a horizontal blur targeting the internal buffer
291 mRenderTaskForHorz = taskList.CreateTask();
292 mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
293 mRenderTaskForHorz.SetSourceActor( mActorForInput );
294 mRenderTaskForHorz.SetExclusive(true);
295 mRenderTaskForHorz.SetInputEnabled( false );
296 mRenderTaskForHorz.SetClearEnabled( true );
297 mRenderTaskForHorz.SetClearColor( mBackgroundColor );
298 mRenderTaskForHorz.SetTargetFrameBuffer( mImageForHorz );
299 mRenderTaskForHorz.SetCameraActor( mCameraForBlur );
301 // use the internal buffer and perform a horizontal blur targeting the output buffer
302 mRenderTaskForVert = taskList.CreateTask();
303 mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
304 mRenderTaskForVert.SetSourceActor( mActorForHorz );
305 mRenderTaskForVert.SetExclusive(true);
306 mRenderTaskForVert.SetInputEnabled( false );
307 mRenderTaskForVert.SetClearEnabled( true );
308 mRenderTaskForVert.SetClearColor( mBackgroundColor );
309 mRenderTaskForVert.SetTargetFrameBuffer( mBlurredImage );
310 mRenderTaskForVert.SetCameraActor( mCameraForBlur );
312 //Perform a blending between the blurred image and the input image
313 mRenderTaskForBlending = taskList.CreateTask();
314 mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
315 mRenderTaskForBlending.SetSourceActor( mActorForBlending );
316 mRenderTaskForBlending.SetExclusive(true);
317 mRenderTaskForBlending.SetInputEnabled( false );
318 mRenderTaskForBlending.SetClearEnabled( true );
319 mRenderTaskForBlending.SetClearColor( mBackgroundColor );
320 mRenderTaskForBlending.SetTargetFrameBuffer( mOutputImage );
321 mRenderTaskForBlending.SetCameraActor( mCameraForBlur );
324 } // namespace Internal
326 } // namespace Toolkit