[NUI] Resolve many problems relative with BaseHandle
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Images / EncodedImageBuffer.cs
1 /*
2  * Copyright(c) 2021 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
18 using System;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI
22 {
23     using global::System;
24     using global::System.ComponentModel;
25     using global::System.Runtime.InteropServices;
26
27     /// <summary>
28     /// Class for Encoded Image Buffer.
29     /// Buffer comes from System.IO.Stream.
30     /// This data will decode internally when you use GeneratedUrl as View's ResourceUrl.
31     /// Note: This class doesn't allow to fix/change anything.
32     /// Only constructor allow to setup data.
33     /// </summary>
34     /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
35     [EditorBrowsable(EditorBrowsableState.Never)]
36     public class EncodedImageBuffer : BaseHandle
37     {
38         private VectorUnsignedChar mCachedBuffer = null; // cached encoded raw buffer
39
40         /// <summary>
41         /// Constructor.
42         /// </summary>
43         /// <param name="stream">The Stream of the image file.</param>
44         /// <exception cref="ArgumentNullException"> Thrown when stream is null. </exception>
45         /// <exception cref="InvalidOperationException"> Thrown when stream don't have any data. </exception>
46         /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public EncodedImageBuffer(System.IO.Stream stream) : this(GetRawBuffrFromStreamHelper(stream))
49         {
50             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
51         }
52
53         internal EncodedImageBuffer(VectorUnsignedChar buffer) : this(Interop.EncodedImageBuffer.New(VectorUnsignedChar.getCPtr(buffer)), true)
54         {
55             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
56             mCachedBuffer = buffer;
57         }
58
59         internal EncodedImageBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
60         {
61         }
62
63         /// <summary>
64         /// Generate URI from current buffer.
65         /// We can now use this url for ImageView.ResourceUrl
66         /// Note : the url lifecycle is same as ImageUrl and it's internal usage.
67         /// Store only ImageUrl.ToString() result and re-use that url is Undefined Behavior.
68         /// </summary>
69         /// <remarks>
70         /// This API should not be called at worker thread.
71         /// </remarks>
72         /// <remarks>Hidden API: Only for inhouse or developing usage. The behavior and interface can be changed anytime.</remarks>
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public ImageUrl GenerateUrl()
75         {
76             return new ImageUrl(Interop.EncodedImageBuffer.GenerateUrl(this.SwigCPtr.Handle), true);
77         }
78
79         /// <summary>
80         /// Get current raw buffer. (read-only)
81         /// Note : the raw buffer doesn't have memory ownership.
82         /// Access to write something to raw buffer is Undefined Behavior.
83         /// </summary>
84         /// This will not be public opened.
85         internal VectorUnsignedChar GetRawBuffer()
86         {
87             mCachedBuffer ??= new VectorUnsignedChar(Interop.EncodedImageBuffer.GetRawBuffer(this.SwigCPtr.Handle), false);
88             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
89             return mCachedBuffer;
90         }
91
92         /// <summary>
93         /// Dispose
94         /// </summary>
95         /// <param name="type"></param>
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         protected override void Dispose(DisposeTypes type)
98         {
99             if (disposed)
100             {
101                 return;
102             }
103
104             if (type == DisposeTypes.Explicit)
105             {
106                 //Called by User
107                 //Release your own managed resources here.
108                 //You should release all of your own disposable objects here.
109                 mCachedBuffer?.Dispose();
110             }
111
112             base.Dispose(type);
113         }
114
115         /// This will not be public opened.
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         protected override void ReleaseSwigCPtr(HandleRef swigCPtr)
118         {
119             Interop.EncodedImageBuffer.DeleteEncodedImageBuffer(swigCPtr);
120             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121         }
122
123         /// <summary>
124         /// Get VectorUnsignedChar from System.IO.Stream.
125         /// This funcion exist only for Constructor.
126         /// </summary>
127         /// This will not be public opened.
128         private static VectorUnsignedChar GetRawBuffrFromStreamHelper(System.IO.Stream stream)
129         {
130             if(stream == null)
131             {
132                 throw new ArgumentNullException(nameof(stream));
133             }
134             if(!stream.CanRead)
135             {
136                 throw new InvalidOperationException("stream don't support to read");
137             }
138
139             // Copy stream to memoryStream
140             System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
141             stream.CopyTo(memoryStream);
142             memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
143
144             long streamLength = memoryStream.Length;
145             if(streamLength <= 0)
146             {
147                 throw new InvalidOperationException("stream length is <= 0");
148             }
149
150             // Allocate buffer that internal DALi engine can read
151             VectorUnsignedChar buffer = new VectorUnsignedChar();
152
153             buffer.Resize((uint)streamLength);
154             var bufferBegin = buffer.Begin();
155             global::System.Runtime.InteropServices.HandleRef bufferRef = SWIGTYPE_p_unsigned_char.getCPtr(bufferBegin);
156
157             // Copy data from memoryStream to buffer
158             System.Runtime.InteropServices.Marshal.Copy(memoryStream.GetBuffer(), 0, bufferRef.Handle, (int)streamLength);
159
160             memoryStream.Dispose();
161
162             return buffer;
163         }
164     }
165 }