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