aefcd0b8eb334b5fd32a3f84945945e629b2d43f
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Utility / GaussianBlurView.cs
1 /*
2  * Copyright(c) 2019 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 using System;
18 using System.Runtime.InteropServices;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Binding;
21
22 using System.ComponentModel;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// GaussianBlurView is a class for applying a render process that blurs an image.
28     /// </summary>
29     /// <since_tizen> 6 </since_tizen>
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public class GaussianBlurView : View
32     {
33         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34         [EditorBrowsable(EditorBrowsableState.Never)]
35         public static readonly BindableProperty BlurStrengthProperty = BindableProperty.Create(nameof(BlurStrength), typeof(float), typeof(GaussianBlurView), default(float), propertyChanged: (bindable, oldValue, newValue) =>
36         {
37             var gaussianBlurView = (GaussianBlurView)bindable;
38             if (newValue != null)
39             {
40                 Tizen.NUI.Object.SetProperty(gaussianBlurView.SwigCPtr, gaussianBlurView.GetBlurStrengthPropertyIndex(), new Tizen.NUI.PropertyValue((float)newValue));
41             }
42         },
43         defaultValueCreator: (bindable) =>
44         {
45             var gaussianBlurView = (GaussianBlurView)bindable;
46             float temp;
47             Tizen.NUI.Object.GetProperty(gaussianBlurView.SwigCPtr, gaussianBlurView.GetBlurStrengthPropertyIndex()).Get(out temp);
48             return temp;
49         });
50
51         internal GaussianBlurView(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, cMemoryOwn)
52         {
53         }
54
55         internal GaussianBlurView(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
56         {
57         }
58
59         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
60         {
61             Interop.GaussianBlurView.DeleteGaussianBlurView(swigCPtr);
62         }
63
64         /// <summary>
65         /// Dispose GaussianBlurView and all children on it.
66         /// </summary>
67         /// <param name="type">Dispose type.</param>
68         /// <since_tizen> 6 </since_tizen>
69         protected override void Dispose(DisposeTypes type)
70         {
71             if (disposed)
72             {
73                 return;
74             }
75
76             //Release your own unmanaged resources here.
77             //You should not access any managed member here except static instance.
78             //because the execution order of Finalizes is non-deterministic.
79             if (finishedCallback != null)
80             {
81                 FinishedSignal().Disconnect(finishedCallback);
82             }
83
84             base.Dispose(type);
85         }
86
87         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
88         private delegate void FinishedCallbackType(IntPtr application);
89         private DaliEventHandler<object, EventArgs> finishedEventHandler;
90         private FinishedCallbackType finishedCallback;
91
92         /// <summary>
93         /// If ActivateOnce has been called, then connect to this signal to be notified when the target actor has been rendered.
94         /// </summary>
95         /// <since_tizen> 6 </since_tizen>
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public event DaliEventHandler<object, EventArgs> Finished
98         {
99             add
100             {
101                 // Restricted to only one listener
102                 if (finishedEventHandler == null)
103                 {
104                     finishedCallback = new FinishedCallbackType(OnFinished);
105                     FinishedSignal().Connect(finishedCallback);
106                 }
107                 finishedEventHandler += value;
108             }
109
110             remove
111             {
112                 finishedEventHandler -= value;
113
114                 if (finishedEventHandler == null && FinishedSignal().Empty() == false)
115                 {
116                     FinishedSignal().Disconnect(finishedCallback);
117                 }
118             }
119         }
120
121         // Callback for GaussianBlurView FinishedSignal
122         private void OnFinished(IntPtr data)
123         {
124             EventArgs e = new EventArgs();
125
126             if (finishedEventHandler != null)
127             {
128                 //here we send all data to user event handlers
129                 finishedEventHandler(this, e);
130             }
131         }
132
133         /// <summary>
134         /// The BlurStrength property. A value of 0.0 is zero blur and 1.0 is full blur. Default is 1.0.
135         /// if you set the blur to 0.0, the result will be no blur BUT the internal rendering will still be happening.
136         /// If you wish to turn the blur off, you should remove the GaussianBlurView object from the window also.
137         /// </summary>
138         /// <since_tizen> 6 </since_tizen>
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public float BlurStrength
141         {
142             get
143             {
144                 return (float)GetValue(BlurStrengthProperty);
145             }
146             set
147             {
148                 SetValue(BlurStrengthProperty, value);
149                 NotifyPropertyChanged();
150             }
151         }
152
153         /// <summary>
154         /// Constructor
155         /// </summary>
156         [EditorBrowsable(EditorBrowsableState.Never)]
157         public GaussianBlurView() : this(Interop.GaussianBlurView.New(), true)
158         {
159             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
160         }
161
162         /// <summary>
163         /// Constructor with parameters.
164         /// </summary>
165         /// <param name="numSamples">The size of the Gaussian blur kernel (number of samples in horizontal / vertical blur directions)</param>
166         /// <param name="blurBellCurveWidth">
167         /// The constant controlling the Gaussian function, must be > 0.0. Controls the width of the bell curve, i.e. the look of the blur and also indirectly
168         /// the amount of blurriness Smaller numbers for a tighter curve. Useful values in the range [0.5..3.0] - near the bottom of that range the curve is weighted heavily towards
169         /// the centre pixel of the kernel (so there won't be much blur), near the top of that range the pixels have nearly equal weighting (closely approximating a box filter
170         /// therefore). Values close to zero result in the bell curve lying almost entirely within a single pixel, in other words there will be basically no blur as neighbouring pixels
171         /// have close to zero weights.
172         /// </param>
173         /// <param name="renderTargetPixelFormat">The pixel format of the render targets we are using to perform the blur.</param>
174         /// <param name="downsampleWidthScale">
175         ///  width scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
176         ///  Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
177         /// </param>
178         /// <param name="downsampleHeightScale">
179         /// The height scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
180         /// Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
181         /// </param>
182         /// <param name="blurUserImage">
183         /// If this is set to true, the GaussianBlurView object will operate in a special mode that allows the user to blur an image of their choice. See
184         /// SetUserImageAndOutputRenderTarget().
185         /// </param>
186         /// <since_tizen> 6 </since_tizen>
187         [EditorBrowsable(EditorBrowsableState.Never)]
188         public GaussianBlurView(uint numSamples, float blurBellCurveWidth, PixelFormat renderTargetPixelFormat, float downsampleWidthScale, float downsampleHeightScale, bool blurUserImage) : this(Interop.GaussianBlurView.New(numSamples, blurBellCurveWidth, (int)renderTargetPixelFormat, downsampleWidthScale, downsampleHeightScale, blurUserImage), true)
189         {
190             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191         }
192
193         /// <summary>
194         /// Copy constructor
195         /// </summary>
196         /// <since_tizen> 6 </since_tizen>
197         [EditorBrowsable(EditorBrowsableState.Never)]
198         public GaussianBlurView(GaussianBlurView handle) : this(Interop.GaussianBlurView.NewGaussianBlurView(GaussianBlurView.getCPtr(handle)), true, false)
199         {
200             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201         }
202
203         /// <summary>
204         /// Start rendering the GaussianBlurView. Must be called after you Add() it to the window.
205         /// </summary>
206         /// <since_tizen> 6 </since_tizen>
207         [EditorBrowsable(EditorBrowsableState.Never)]
208         public void Activate()
209         {
210             Interop.GaussianBlurView.Activate(SwigCPtr);
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212         }
213
214         /// <summary>
215         /// Render the GaussianBlurView once.
216         /// Must be called after you Add() it to the window.
217         /// Listen to the Finished signal to determine when the rendering has completed.
218         /// </summary>
219         /// <since_tizen> 6 </since_tizen>
220         [EditorBrowsable(EditorBrowsableState.Never)]
221         public void ActivateOnce()
222         {
223             Interop.GaussianBlurView.ActivateOnce(SwigCPtr);
224             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225         }
226
227         /// <summary>
228         /// Stop rendering the GaussianBlurView. Must be called after you Remove() it from the window.
229         /// </summary>
230         /// <since_tizen> 6 </since_tizen>
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         public void Deactivate()
233         {
234             Interop.GaussianBlurView.Deactivate(SwigCPtr);
235             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236         }
237
238         private int GetBlurStrengthPropertyIndex()
239         {
240             int ret = Interop.GaussianBlurView.GetBlurStrengthPropertyIndex(SwigCPtr);
241             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
242             return ret;
243         }
244
245
246         internal void SetBackgroundColor(Vector4 color)
247         {
248             Interop.GaussianBlurView.SetBackgroundColor(SwigCPtr, Vector4.getCPtr(color));
249             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
250         }
251
252         internal Vector4 GetBackgroundColor()
253         {
254             Vector4 ret = new Vector4(Interop.GaussianBlurView.GetBackgroundColor(SwigCPtr), true);
255             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256             return ret;
257         }
258
259         private GaussianBlurViewSignal FinishedSignal()
260         {
261             GaussianBlurViewSignal ret = new GaussianBlurViewSignal(Interop.GaussianBlurView.FinishedSignal(SwigCPtr), false);
262             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
263             return ret;
264         }
265
266         internal void SetUserImageAndOutputRenderTarget(Texture inputTexture, FrameBuffer outputRenderTarget)
267         {
268             Interop.GaussianBlurView.SetUserImageAndOutputRenderTarget(SwigCPtr, Texture.getCPtr(inputTexture), FrameBuffer.getCPtr(outputRenderTarget));
269             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
270         }
271     }
272 }