[Tizen] Ensuring that Registry.Unregister is called from the most derived class befor...
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Image.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     using System;
21     using System.Runtime.InteropServices;
22
23
24     internal class Image : BaseHandle
25     {
26         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
27
28         internal Image(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Image_SWIGUpcast(cPtr), cMemoryOwn)
29         {
30             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
31         }
32
33         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Image obj)
34         {
35             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
36         }
37
38         protected override void Dispose(DisposeTypes type)
39         {
40             if (disposed)
41             {
42                 return;
43             }
44
45             if (type == DisposeTypes.Explicit)
46             {
47                 //Called by User
48                 //Release your own managed resources here.
49                 //You should release all of your own disposable objects here.
50
51             }
52
53             //Release your own unmanaged resources here.
54             //You should not access any managed member here except static instance.
55             //because the execution order of Finalizes is non-deterministic.
56
57             if (swigCPtr.Handle != global::System.IntPtr.Zero)
58             {
59                 if (swigCMemOwn)
60                 {
61                     swigCMemOwn = false;
62
63                     //Unreference this instance from Registry.
64                     Registry.Unregister(this);
65
66                     NDalicPINVOKE.delete_Image(swigCPtr);
67                 }
68                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
69             }
70
71             base.Dispose(type);
72         }
73
74
75
76         /**
77           * @brief Event arguments that passed via Uploaded signal
78           *
79           */
80         public class UploadedEventArgs : EventArgs
81         {
82             private Image _image;
83             /**
84               * @brief Image - is the image data that gets uploaded to GL.
85               *
86               */
87             public Image Image
88             {
89                 get
90                 {
91                     return _image;
92                 }
93                 set
94                 {
95                     _image = value;
96                 }
97             }
98         }
99
100         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
101         private delegate void UploadedEventCallbackDelegate(IntPtr image);
102         private DaliEventHandler<object, UploadedEventArgs> _imageUploadedEventHandler;
103         private UploadedEventCallbackDelegate _imageUploadedEventCallbackDelegate;
104
105         /**
106           * @brief Event for Uploaded signal which can be used to subscribe/unsubscribe the event handler
107           * (in the type of UploadedEventHandler-DaliEventHandler<object,UploadedEventArgs>)
108           * provided by the user. Uploaded signal is emitted when the image data gets uploaded to GL.
109           */
110         public event DaliEventHandler<object, UploadedEventArgs> Uploaded
111         {
112             add
113             {
114                 lock (this)
115                 {
116                     // Restricted to only one listener
117                     if (_imageUploadedEventHandler == null)
118                     {
119                         _imageUploadedEventHandler += value;
120
121                         _imageUploadedEventCallbackDelegate = new UploadedEventCallbackDelegate(OnUploaded);
122                         this.UploadedSignal().Connect(_imageUploadedEventCallbackDelegate);
123                     }
124                 }
125             }
126
127             remove
128             {
129                 lock (this)
130                 {
131                     if (_imageUploadedEventHandler != null)
132                     {
133                         this.UploadedSignal().Disconnect(_imageUploadedEventCallbackDelegate);
134                     }
135
136                     _imageUploadedEventHandler -= value;
137                 }
138             }
139         }
140
141         // Callback for Image UploadedSignal
142         private void OnUploaded(IntPtr data)
143         {
144             UploadedEventArgs e = new UploadedEventArgs();
145
146             // Populate all members of "e" (UploadedEventArgs) with real data
147             e.Image = Image.GetImageFromPtr(data);
148
149             if (_imageUploadedEventHandler != null)
150             {
151                 //here we send all data to user event handlers
152                 _imageUploadedEventHandler(this, e);
153             }
154         }
155
156
157         public static Image GetImageFromPtr(global::System.IntPtr cPtr)
158         {
159             Image ret = new Image(cPtr, false);
160             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
161             return ret;
162         }
163
164
165         public Image() : this(NDalicPINVOKE.new_Image__SWIG_0(), true)
166         {
167             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168         }
169
170         public Image(Image handle) : this(NDalicPINVOKE.new_Image__SWIG_1(Image.getCPtr(handle)), true)
171         {
172             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173         }
174
175         public Image Assign(Image rhs)
176         {
177             Image ret = new Image(NDalicPINVOKE.Image_Assign(swigCPtr, Image.getCPtr(rhs)), false);
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179             return ret;
180         }
181
182         public static Image DownCast(BaseHandle handle)
183         {
184             Image ret = new Image(NDalicPINVOKE.Image_DownCast(BaseHandle.getCPtr(handle)), true);
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186             return ret;
187         }
188
189         public uint GetWidth()
190         {
191             uint ret = NDalicPINVOKE.Image_GetWidth(swigCPtr);
192             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
193             return ret;
194         }
195
196         public uint GetHeight()
197         {
198             uint ret = NDalicPINVOKE.Image_GetHeight(swigCPtr);
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200             return ret;
201         }
202
203         public ImageSignal UploadedSignal()
204         {
205             ImageSignal ret = new ImageSignal(NDalicPINVOKE.Image_UploadedSignal(swigCPtr), false);
206             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207             return ret;
208         }
209
210     }
211
212 }