[NUI] Refactor dispose pattern to reduce duplication (#1112)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / TextureSet.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.ComponentModel;
18 using System.Runtime.InteropServices;
19
20 namespace Tizen.NUI
21 {
22     /// <summary>
23     /// TextureSet is a handle to an object that specifies the set of images used as textures by a renderer.<br />
24     /// The images have to be ordered in the same order they are declared in the shader.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class TextureSet : BaseHandle
28     {
29
30         /// <summary>
31         /// Create an instance of TextureSet.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         public TextureSet() : this(Interop.TextureSet.TextureSet_New(), true)
35         {
36             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
37         }
38
39         internal TextureSet(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.TextureSet.TextureSet_SWIGUpcast(cPtr), cMemoryOwn)
40         {
41         }
42
43         /// <summary>
44         /// Sets the texture at position "index".
45         /// </summary>
46         /// <param name="index">The position in the texture set of the texture.</param>
47         /// <param name="texture">The texture.</param>
48         /// <since_tizen> 3 </since_tizen>
49         public void SetTexture(uint index, Texture texture)
50         {
51             Interop.TextureSet.TextureSet_SetTexture(swigCPtr, index, Texture.getCPtr(texture));
52             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53         }
54
55         /// <summary>
56         /// Gets the image at position "index".
57         /// </summary>
58         /// <param name="index">The position in the texture set of the image.</param>
59         /// <returns>A handle to the image at the the specified position.</returns>
60         /// <since_tizen> 3 </since_tizen>
61         public Texture GetTexture(uint index)
62         {
63             //to fix memory leak issue, match the handle count with native side.
64             System.IntPtr cPtr = Interop.TextureSet.TextureSet_GetTexture(swigCPtr, index);
65             HandleRef CPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
66             Texture ret = Registry.GetManagedBaseHandleFromNativePtr(CPtr.Handle) as Texture;
67             if (cPtr != null && ret == null)
68             {
69                 ret = new Texture(cPtr, false);
70                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
71                 return ret;
72             }
73             Interop.BaseHandle.delete_BaseHandle(CPtr);
74             CPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
75
76             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77             return ret;
78         }
79
80         /// <summary>
81         /// Sets the sampler to be used by the image at position "index".
82         /// </summary>
83         /// <param name="index">The position in the texture set of the image.</param>
84         /// <param name="sampler">The sampler to use.</param>
85         /// <since_tizen> 3 </since_tizen>
86         public void SetSampler(uint index, Sampler sampler)
87         {
88             Interop.TextureSet.TextureSet_SetSampler(swigCPtr, index, Sampler.getCPtr(sampler));
89             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
90         }
91
92         /// <summary>
93         /// Sets the sampler to be used by the image at position "index".
94         /// </summary>
95         /// <param name="index">The position in the texture set of the image.</param>
96         /// <returns>A handle to the sampler at the specified position.</returns>
97         /// <since_tizen> 3 </since_tizen>
98         public Sampler GetSampler(uint index)
99         {
100             //to fix memory leak issue, match the handle count with native side.
101             System.IntPtr cPtr = Interop.TextureSet.TextureSet_GetSampler(swigCPtr, index);
102             Sampler ret = this.GetInstanceSafely<Sampler>(cPtr);
103             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
104             return ret;
105         }
106
107         /// <summary>
108         /// Gets the number of textures present in the TextureSet.
109         /// </summary>
110         /// <returns>The number of textures in the TextureSet.</returns>
111         /// <since_tizen> 3 </since_tizen>
112         public uint GetTextureCount()
113         {
114             uint ret = Interop.TextureSet.TextureSet_GetTextureCount(swigCPtr);
115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116             return ret;
117         }
118
119         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TextureSet obj)
120         {
121             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
122         }
123
124         /// This will not be public opened.
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
127         {
128             Interop.TextureSet.delete_TextureSet(swigCPtr);
129         }
130     }
131 }