Add null check for mTextureSet.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / filters / blur-two-pass-filter.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 "blur-two-pass-filter.h"
20
21 // EXTERNAL INCLUDES
22 #include <sstream>
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>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/internal/controls/control/control-renderers.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43
44 const float DEFAULT_KERNEL0[] = { 12.0f/16.0f, 2.0f/16.0f, 2.0f/16.0f };
45
46 const float DEFAULT_KERNEL1[] = { 8.0f/16.0f, 2.75f/16.0f, 2.75f/16.0f, 1.25f/16.0f,
47                                   1.25f/16.0f };
48
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 };
51
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,
54                                   0.5f/16.0f };
55
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 };
60
61 const char* BLUR_TWO_PASS_FRAGMENT_SOURCE =
62 {
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"
68  "void main()\n"
69  "{\n"
70  "  vec4 color = vec4(0.0);\n"
71  "  for( int i = 0; i < NUM_SAMPLES; ++i )\n"
72  "  {\n"
73  "    color += texture2D( sTexture, vTexCoord + uSampleOffsets[i] ) * uSampleWeights[i];\n"
74  "  }\n"
75  "  gl_FragColor = color;\n"
76  "}\n"
77 };
78
79 std::string GetOffsetUniformName( int index )
80 {
81   std::ostringstream oss;
82   oss << "uSampleOffsets[" << index << "]";
83   return oss.str();
84 }
85
86 std::string GetWeightUniformName( int index )
87 {
88   std::ostringstream oss;
89   oss << "uSampleWeights[" << index << "]";
90   return oss.str();
91 }
92
93 const char* BLEND_TWO_IMAGES_FRAGMENT_SOURCE =
94 {
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"
100  "void main()\n"
101  "{\n"
102  "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uBlurStrength"
103  "               + texture2D( sEffect, vTexCoord )*(1.0-uBlurStrength); \n"
104  "}\n"
105 };
106
107 const char* const BLUR_STRENGTH_UNIFORM_NAME( "uBlurStrength"  );
108 const char* const EFFECT_IMAGE_NAME( "sEffect" );
109
110 } // namespace
111
112
113 BlurTwoPassFilter::BlurTwoPassFilter()
114 : ImageFilter()
115 {
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 );
120 }
121
122 BlurTwoPassFilter::~BlurTwoPassFilter()
123 {
124 }
125
126 void BlurTwoPassFilter::Enable()
127 {
128   // create custom shader effect
129   if( !GetKernelSize() )
130   {
131     CreateKernel( DEFAULT_KERNEL4, sizeof(DEFAULT_KERNEL4)/sizeof(DEFAULT_KERNEL4[0]) );
132   }
133   int kernelSize( static_cast< int >(GetKernelSize()) );
134
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() );
140
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 );
148
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 );
153
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 );
161
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 );
166
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 );
175
176   for( int i = 0; i < kernelSize; ++i )
177   {
178     const std::string offsetUniform( GetOffsetUniformName( i ) );
179     const std::string weightUniform( GetWeightUniformName( i ) );
180
181     mActorForInput.RegisterProperty( offsetUniform, Vector2(mKernel[i]) * Vector2::XAXIS );
182     mActorForInput.RegisterProperty( weightUniform, mKernel[i].z );
183
184     mActorForHorz.RegisterProperty( offsetUniform, Vector2(mKernel[i]) * Vector2::YAXIS );
185     mActorForHorz.RegisterProperty( weightUniform, mKernel[i].z );
186   }
187
188   mRootActor.Add( mActorForInput );
189   mRootActor.Add( mActorForHorz );
190   mRootActor.Add( mActorForBlending );
191
192   SetupCamera();
193   CreateRenderTasks();
194 }
195
196 void BlurTwoPassFilter::Disable()
197 {
198   if( mRootActor )
199   {
200     if( mCameraActor )
201     {
202       mRootActor.Remove( mCameraActor );
203       mCameraActor.Reset();
204     }
205
206     if( mActorForInput )
207     {
208       mRootActor.Remove( mActorForInput );
209       mActorForInput.Reset();
210     }
211
212     if( mActorForHorz )
213     {
214       mRootActor.Remove( mActorForHorz );
215       mActorForHorz.Reset();
216     }
217
218     RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
219
220     if( mRenderTaskForHorz )
221     {
222       taskList.RemoveTask(mRenderTaskForHorz);
223     }
224     if( mRenderTaskForVert )
225     {
226       taskList.RemoveTask(mRenderTaskForVert);
227     }
228     if( mRenderTaskForBlending )
229     {
230       taskList.RemoveTask(mRenderTaskForBlending);
231     }
232
233     mRootActor.Reset();
234   }
235 }
236
237 void BlurTwoPassFilter::Refresh()
238 {
239   if( mRenderTaskForHorz )
240   {
241     mRenderTaskForHorz.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
242   }
243
244   if( mRenderTaskForVert )
245   {
246     mRenderTaskForVert.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
247   }
248
249   if( mRenderTaskForBlending )
250   {
251     mRenderTaskForBlending.SetRefreshRate( mRefreshOnDemand ? RenderTask::REFRESH_ONCE : RenderTask::REFRESH_ALWAYS );
252   }
253 }
254
255 void BlurTwoPassFilter::SetSize( const Vector2& size )
256 {
257   mTargetSize = size;
258   if( mActorForInput )
259   {
260     mActorForInput.SetProperty( Actor::Property::SIZE, mTargetSize);
261   }
262   if( mActorForHorz )
263   {
264     mActorForHorz.SetProperty( Actor::Property::SIZE, mTargetSize);
265   }
266   if( mActorForBlending )
267   {
268     mActorForBlending.SetProperty( Actor::Property::SIZE, mTargetSize);
269   }
270 }
271
272 Handle BlurTwoPassFilter::GetHandleForAnimateBlurStrength()
273 {
274   return mActorForBlending;
275 }
276
277 void BlurTwoPassFilter::CreateRenderTasks()
278 {
279   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
280
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 );
291
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 );
302
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 );
313 }
314
315 } // namespace Internal
316
317 } // namespace Toolkit
318
319 } // namespace Dali