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