Merge remote-tracking branch 'origin/API11' into tizen_8.0
[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         static GaussianBlurView()
34         {
35             if (NUIApplication.IsUsingXaml)
36             {
37                 BlurStrengthProperty = BindableProperty.Create(nameof(BlurStrength), typeof(float), typeof(GaussianBlurView), default(float),
38                     propertyChanged: SetInternalBlurStrengthProperty, defaultValueCreator: GetInternalBlurStrengthProperty);
39
40             }
41         }
42
43         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
44         [EditorBrowsable(EditorBrowsableState.Never)]
45         public static BindableProperty BlurStrengthProperty = null;
46         internal static void SetInternalBlurStrengthProperty(BindableObject bindable, object oldValue, object newValue)
47         {
48             var gaussianBlurView = (GaussianBlurView)bindable;
49             if (newValue != null)
50             {
51                 Tizen.NUI.Object.SetProperty(gaussianBlurView.SwigCPtr, gaussianBlurView.GetBlurStrengthPropertyIndex(), new Tizen.NUI.PropertyValue((float)newValue));
52             }
53         }
54         internal static object GetInternalBlurStrengthProperty(BindableObject bindable)
55         {
56             var gaussianBlurView = (GaussianBlurView)bindable;
57             float temp;
58             Tizen.NUI.Object.GetProperty(gaussianBlurView.SwigCPtr, gaussianBlurView.GetBlurStrengthPropertyIndex()).Get(out temp);
59             return temp;
60         }
61
62         internal GaussianBlurView(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, cMemoryOwn)
63         {
64         }
65
66         internal GaussianBlurView(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
67         {
68         }
69
70         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
71         {
72             Interop.GaussianBlurView.DeleteGaussianBlurView(swigCPtr);
73         }
74
75         /// <summary>
76         /// Dispose GaussianBlurView and all children on it.
77         /// </summary>
78         /// <param name="type">Dispose type.</param>
79         /// <since_tizen> 6 </since_tizen>
80         protected override void Dispose(DisposeTypes type)
81         {
82             if (disposed)
83             {
84                 return;
85             }
86
87             //Release your own unmanaged resources here.
88             //You should not access any managed member here except static instance.
89             //because the execution order of Finalizes is non-deterministic.
90             if (finishedCallback != null)
91             {
92                 FinishedSignal().Disconnect(finishedCallback);
93             }
94
95             base.Dispose(type);
96         }
97
98         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
99         private delegate void FinishedCallbackType(IntPtr application);
100         private DaliEventHandler<object, EventArgs> finishedEventHandler;
101         private FinishedCallbackType finishedCallback;
102
103         /// <summary>
104         /// If ActivateOnce has been called, then connect to this signal to be notified when the target actor has been rendered.
105         /// </summary>
106         /// <since_tizen> 6 </since_tizen>
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         public event DaliEventHandler<object, EventArgs> Finished
109         {
110             add
111             {
112                 // Restricted to only one listener
113                 if (finishedEventHandler == null)
114                 {
115                     finishedCallback = new FinishedCallbackType(OnFinished);
116                     FinishedSignal().Connect(finishedCallback);
117                 }
118                 finishedEventHandler += value;
119             }
120
121             remove
122             {
123                 finishedEventHandler -= value;
124
125                 if (finishedEventHandler == null && FinishedSignal().Empty() == false)
126                 {
127                     FinishedSignal().Disconnect(finishedCallback);
128                 }
129             }
130         }
131
132         // Callback for GaussianBlurView FinishedSignal
133         private void OnFinished(IntPtr data)
134         {
135             EventArgs e = new EventArgs();
136
137             if (finishedEventHandler != null)
138             {
139                 //here we send all data to user event handlers
140                 finishedEventHandler(this, e);
141             }
142         }
143
144         /// <summary>
145         /// The BlurStrength property. A value of 0.0 is zero blur and 1.0 is full blur. Default is 1.0.
146         /// if you set the blur to 0.0, the result will be no blur BUT the internal rendering will still be happening.
147         /// If you wish to turn the blur off, you should remove the GaussianBlurView object from the window also.
148         /// </summary>
149         /// <since_tizen> 6 </since_tizen>
150         [EditorBrowsable(EditorBrowsableState.Never)]
151         public float BlurStrength
152         {
153             get
154             {
155                 if (NUIApplication.IsUsingXaml)
156                 {
157                     return (float)GetValue(BlurStrengthProperty);
158                 }
159                 else
160                 {
161                     return (float)GetInternalBlurStrengthProperty(this);
162                 }
163             }
164             set
165             {
166                 if (NUIApplication.IsUsingXaml)
167                 {
168                     SetValue(BlurStrengthProperty, value);
169                 }
170                 else
171                 {
172                     SetInternalBlurStrengthProperty(this, null, value);
173                 }
174                 NotifyPropertyChanged();
175             }
176         }
177
178         /// <summary>
179         /// Constructor
180         /// </summary>
181         [EditorBrowsable(EditorBrowsableState.Never)]
182         public GaussianBlurView() : this(Interop.GaussianBlurView.New(), true)
183         {
184             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185         }
186
187         /// <summary>
188         /// Constructor with parameters.
189         /// </summary>
190         /// <param name="numSamples">The size of the Gaussian blur kernel (number of samples in horizontal / vertical blur directions)</param>
191         /// <param name="blurBellCurveWidth">
192         /// 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
193         /// 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
194         /// 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
195         /// 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
196         /// have close to zero weights.
197         /// </param>
198         /// <param name="renderTargetPixelFormat">The pixel format of the render targets we are using to perform the blur.</param>
199         /// <param name="downsampleWidthScale">
200         ///  width scale factor applied during the blur process, scaling the size of the source image to the size of the final blurred image output.
201         ///  Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
202         /// </param>
203         /// <param name="downsampleHeightScale">
204         /// 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.
205         /// Useful for downsampling - trades visual quality for processing speed. A value of 1.0f results in no scaling applied.
206         /// </param>
207         /// <param name="blurUserImage">
208         /// 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
209         /// SetUserImageAndOutputRenderTarget().
210         /// </param>
211         /// <since_tizen> 6 </since_tizen>
212         [EditorBrowsable(EditorBrowsableState.Never)]
213         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)
214         {
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216         }
217
218         /// <summary>
219         /// Copy constructor
220         /// </summary>
221         /// <since_tizen> 6 </since_tizen>
222         [EditorBrowsable(EditorBrowsableState.Never)]
223         public GaussianBlurView(GaussianBlurView handle) : this(Interop.GaussianBlurView.NewGaussianBlurView(GaussianBlurView.getCPtr(handle)), true, false)
224         {
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226         }
227
228         /// <summary>
229         /// Start rendering the GaussianBlurView. Must be called after you Add() it to the window.
230         /// </summary>
231         /// <since_tizen> 6 </since_tizen>
232         [EditorBrowsable(EditorBrowsableState.Never)]
233         public void Activate()
234         {
235             Interop.GaussianBlurView.Activate(SwigCPtr);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237         }
238
239         /// <summary>
240         /// Render the GaussianBlurView once.
241         /// Must be called after you Add() it to the window.
242         /// Listen to the Finished signal to determine when the rendering has completed.
243         /// </summary>
244         /// <since_tizen> 6 </since_tizen>
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public void ActivateOnce()
247         {
248             Interop.GaussianBlurView.ActivateOnce(SwigCPtr);
249             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
250         }
251
252         /// <summary>
253         /// Stop rendering the GaussianBlurView. Must be called after you Remove() it from the window.
254         /// </summary>
255         /// <since_tizen> 6 </since_tizen>
256         [EditorBrowsable(EditorBrowsableState.Never)]
257         public void Deactivate()
258         {
259             Interop.GaussianBlurView.Deactivate(SwigCPtr);
260             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
261         }
262
263         private int GetBlurStrengthPropertyIndex()
264         {
265             int ret = Interop.GaussianBlurView.GetBlurStrengthPropertyIndex(SwigCPtr);
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267             return ret;
268         }
269
270
271         internal void SetBackgroundColor(Vector4 color)
272         {
273             Interop.GaussianBlurView.SetBackgroundColor(SwigCPtr, Vector4.getCPtr(color));
274             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
275         }
276
277         internal Vector4 GetBackgroundColor()
278         {
279             Vector4 ret = new Vector4(Interop.GaussianBlurView.GetBackgroundColor(SwigCPtr), true);
280             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281             return ret;
282         }
283
284         private GaussianBlurViewSignal FinishedSignal()
285         {
286             GaussianBlurViewSignal ret = new GaussianBlurViewSignal(Interop.GaussianBlurView.FinishedSignal(SwigCPtr), false);
287             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
288             return ret;
289         }
290
291         internal void SetUserImageAndOutputRenderTarget(Texture inputTexture, FrameBuffer outputRenderTarget)
292         {
293             Interop.GaussianBlurView.SetUserImageAndOutputRenderTarget(SwigCPtr, Texture.getCPtr(inputTexture), FrameBuffer.getCPtr(outputRenderTarget));
294             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
295         }
296     }
297 }