[NUI] Reduce code duplication - refactor dispose codes (#1010)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / PixelData.cs
1 /*
2  * Copyright(c) 2017 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.ComponentModel;
18
19 namespace Tizen.NUI
20 {
21     /// <summary>
22     ///  The PixelData object holds a pixel buffer.<br />
23     ///  The PixelData takes over the ownership of the pixel buffer.<br />
24     ///  The buffer memory must not be released outside of this class, instead,
25     ///  the PixelData object will release it automatically when the reference count falls to zero.
26     /// </summary>
27     /// Please DO NOT use! This will be deprecated!
28     /// PixelData class requires externally allocated pixel memory buffer and this buffer loses its ownershop by native DALi.
29     /// And this would make some problem, because dotnet runtime would change the address of memory allocated.
30     /// So this is required to be removed.
31     /// currently no use. will be added later
32     /// <since_tizen> 5 </since_tizen>
33     /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
34     [EditorBrowsable(EditorBrowsableState.Never)]
35     public class PixelData : BaseHandle
36     {
37         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
38
39         /// <summary>
40         /// Creates a PixelData object.
41         /// </summary>
42         /// <param name="buffer">The raw pixel data.</param>
43         /// <param name="bufferSize">The size of the buffer in bytes.</param>
44         /// <param name="width">Buffer width in pixels.</param>
45         /// <param name="height">Buffer height in pixels.</param>
46         /// <param name="pixelFormat">The pixel format.</param>
47         /// <param name="releaseFunction">The function used to release the memory.</param>
48         /// <since_tizen> 5 </since_tizen>
49         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public PixelData(byte[] buffer, uint bufferSize, uint width, uint height, PixelFormat pixelFormat, PixelData.ReleaseFunction releaseFunction) : this(Interop.PixelData.PixelData_New(buffer, bufferSize, width, height, (int)pixelFormat, (int)releaseFunction), true)
52         {
53             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54
55         }
56
57         internal PixelData(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.PixelData.PixelData_SWIGUpcast(cPtr), cMemoryOwn)
58         {
59             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
60         }
61
62         /// <summary>
63         /// Enumeration for function to release the pixel buffer.
64         /// </summary>
65         /// <since_tizen> 5 </since_tizen>
66         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public enum ReleaseFunction
69         {
70             /// <summary>
71             /// Use free function to release the pixel buffer.
72             /// </summary>
73             Free,
74
75             /// <summary>
76             /// Use delete[] operator to release the pixel buffer.
77             /// </summary>
78             DeleteArray
79         }
80
81         /// <summary>
82         /// Gets the width of the buffer in pixels.
83         /// </summary>
84         /// <returns>The width of the buffer in pixels.</returns>
85         /// <since_tizen> 5 </since_tizen>
86         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public uint GetWidth()
89         {
90             uint ret = Interop.PixelData.PixelData_GetWidth(swigCPtr);
91             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
92             return ret;
93         }
94
95         /// <summary>
96         /// Gets the height of the buffer in pixels.
97         /// </summary>
98         /// <returns>The height of the buffer in pixels.</returns>
99         /// <since_tizen> 5 </since_tizen>
100         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
101         [EditorBrowsable(EditorBrowsableState.Never)]
102         public uint GetHeight()
103         {
104             uint ret = Interop.PixelData.PixelData_GetHeight(swigCPtr);
105             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
106             return ret;
107         }
108
109         /// <summary>
110         /// Gets the pixel format.
111         /// </summary>
112         /// <returns>The pixel format.</returns>
113         /// <since_tizen> 5 </since_tizen>
114         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public PixelFormat GetPixelFormat()
117         {
118             PixelFormat ret = (PixelFormat)Interop.PixelData.PixelData_GetPixelFormat(swigCPtr);
119             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
120             return ret;
121         }
122
123         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(PixelData obj)
124         {
125             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
126         }
127
128         /// <summary>
129         /// Dispose.
130         /// </summary>
131         /// <since_tizen> 5 </since_tizen>
132         protected override void Dispose(DisposeTypes type)
133         {
134             if (disposed)
135             {
136                 return;
137             }
138
139             //Release your own unmanaged resources here.
140             //You should not access any managed member here except static instance.
141             //Because the execution order of Finalizes is non-deterministic.
142
143             if (swigCPtr.Handle != global::System.IntPtr.Zero)
144             {
145                 if (swigCMemOwn)
146                 {
147                     swigCMemOwn = false;
148                     Interop.PixelData.delete_PixelData(swigCPtr);
149                 }
150                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
151             }
152
153             base.Dispose(type);
154         }
155     }
156 }