[NUI] Fix svace issue of Marshal.Copy
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / DirectRenderingGLView.cs
1 using System;
2 using System.ComponentModel;
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Diagnostics.CodeAnalysis;
7
8 namespace Tizen.NUI.BaseComponents
9 {
10     /// <summary>
11     /// DirectRenderingGLView allows drawing with OpenGL. You can render to a Window directly.
12     /// DirectRenderingGLView creates a context.
13     /// </summary>
14     /// <since_tizen> 11 </since_tizen>
15     [EditorBrowsable(EditorBrowsableState.Never)]
16     public class DirectRenderingGLView : View
17     {
18         /// <summary>
19         /// The parameter of the RenderFrame Callback.
20         /// It has data to render directly.
21         /// </summary>
22         [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
23         public class RenderCallbackInput
24         {
25             Matrix mvp;
26             Matrix projection;
27             Size2D size;
28             Rectangle clippingBox;
29             ReadOnlyCollection<int> textureBindings;
30
31             public RenderCallbackInput(Matrix mvp, Matrix projection, Size2D size, Rectangle clippingBox, int[] textureBindings)
32             {
33                 this.mvp = mvp;
34                 this.projection = projection;
35                 this.size = size;
36                 this.clippingBox = clippingBox;
37                 this.textureBindings = new ReadOnlyCollection<int>(textureBindings);
38             }
39
40             /// <summary>
41             /// MVP matrix
42             /// </summary>
43             public Matrix Mvp
44             {
45                 get { return mvp; }
46             }
47
48             /// <summary>
49             /// Projection matrix
50             /// </summary>
51             public Matrix Projection
52             {
53                 get { return projection; }
54             }
55
56             /// <summary>
57             /// The size of the DirectRenderingGLView
58             /// </summary>
59             public Size2D Size
60             {
61                 get { return size; }
62             }
63
64             /// <summary>
65             /// The area of DirectRenderingGLView. You can use this for glScissor()
66             /// </summary>
67             public Rectangle ClippingBox
68             {
69                 get { return clippingBox; }
70             }
71
72             /// <summary>
73             /// Texture bindings
74             /// </summary>
75             public ReadOnlyCollection<int> TextureBindings
76             {
77                 get { return textureBindings; }
78             }
79         }
80
81         private GLInitializeDelegate glInitializeCallback;
82         private GLRenderFrameDelegate glRenderFrameCallback;
83         private GLTerminateDelegate glTerminateCallback;
84         private InternalGLRenderFrameDelegate internalRenderFrameCallback;
85
86         /// <summary>
87         /// Type of callback to initialize OpenGLES.
88         /// </summary>
89         public delegate void GLInitializeDelegate();
90
91         /// <summary>
92         /// Type of callback to render the frame with OpenGLES APIs.
93         /// If the return value of this callback is not 0, the eglSwapBuffers() will be called.
94         /// </summary>
95         /// <returns>The return value is not 0, the eglSwapBuffers() will be called.</returns>
96         public delegate int GLRenderFrameDelegate(in RenderCallbackInput input);
97
98         private delegate int InternalGLRenderFrameDelegate(global::System.IntPtr cPtr);
99
100         /// <summary>
101         /// Type of callback to clean up GL resource.
102         /// </summary>
103         public delegate void GLTerminateDelegate();
104
105         internal DirectRenderingGLView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
106         {
107         }
108
109         /// <summary>
110         /// Creates an initialized DirectRenderingGLView.
111         /// </summary>
112         /// <param name="colorFormat">The format of the color buffer</param>
113         public DirectRenderingGLView(ColorFormat colorFormat) : this(Interop.GLView.New(0, (int)colorFormat), true)
114         {
115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116         }
117
118         /// <summary>
119         /// Enumeration for the color format of the color buffer
120         /// </summary>
121         public enum ColorFormat
122         {
123             /// <summary>
124             /// 8 red bits, 8 green bits, 8 blue bits
125             /// </summary>
126             RGB888 = 0,
127
128             /// <summary>
129             /// 8 red bits, 8 green bits, 8 blue bits, alpha 8 bits
130             /// </summary>
131             RGBA8888
132         }
133
134         /// <summary>
135         /// Gets or sets the rendering mode of the DirectRenderingGLView.
136         /// </summary>
137         public GLRenderingMode RenderingMode
138         {
139             [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "SWIG boilerplate, no exceptions are expected")]
140             get
141             {
142                 GLRenderingMode ret = (GLRenderingMode)Interop.GLView.GlViewGetRenderingMode(SwigCPtr);
143                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
144                 return ret;
145             }
146             set
147             {
148                 Interop.GLView.GlViewSetRenderingMode(SwigCPtr, (int)value);
149                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150             }
151         }
152
153         /// <summary>
154         /// Registers GL callback functions to render with OpenGL ES
155         /// </summary>
156         /// <param name="glInit">The callback function for GL initialization</param>
157         /// <param name="glRenderFrame">The callback function to render the frame</param>
158         /// <param name="glTerminate">The callback function to clean up GL resources</param>
159         public void RegisterGLCallbacks(GLInitializeDelegate glInit, GLRenderFrameDelegate glRenderFrame, GLTerminateDelegate glTerminate)
160         {
161             glInitializeCallback = glInit;
162             HandleRef InitHandleRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(glInitializeCallback));
163
164             glRenderFrameCallback = glRenderFrame;
165             internalRenderFrameCallback = OnRenderFrame;
166             HandleRef RenderHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(internalRenderFrameCallback));
167
168             glTerminateCallback = glTerminate;
169             HandleRef TerminateHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(glTerminateCallback));
170
171             Interop.GLView.GlViewRegisterGlCallbacks(SwigCPtr, InitHandleRef, RenderHandlerRef, TerminateHandlerRef);
172
173             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174         }
175
176         /// <summary>
177         /// Binds textures to own context.
178         /// You can get the bind IDs in RenderCallbackInput in the glRenderFrame callback.
179         /// </summary>
180         /// <param name="textures">List of Textures</param>
181         /// <exception cref="OverflowException"> Thrown when length of textures list is overflow. </exception>
182         public void BindTextureResources(List<Texture> textures)
183         {
184             unsafe
185             {
186                 if (textures != null)
187                 {
188                     int count = textures.Count;
189                     if (count > 0)
190                     {
191                         IntPtr[] texturesArray = new IntPtr[count];
192                         for (int i = 0; i < count; i++)
193                         {
194                             texturesArray[i] = HandleRef.ToIntPtr(Texture.getCPtr(textures[i]));
195                         }
196                         IntPtr unmanagedPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * count);
197                         Marshal.Copy(texturesArray, 0, unmanagedPointer, count);
198
199                         Interop.GLView.GlViewBindTextureResources(SwigCPtr, unmanagedPointer, count);
200                         Marshal.FreeHGlobal(unmanagedPointer);
201                     }
202                 }
203             }
204         }
205
206         /// <summary>
207         /// Sets graphics configuration for the DirectRenderingGLView
208         /// </summary>
209         /// <param name="depth">The flag of depth buffer. When the value is true, 24bit depth buffer is enabled.</param>
210         /// <param name="stencil">The flag of stencil. When the value is true, 8bit stencil buffer is enabled.</param>
211         /// <param name="msaa">The bit of MSAA</param>
212         /// <param name="version">The GLES version</param>
213         /// <returns>True if the config was successfully set, false otherwise.</returns>
214         public bool SetGraphicsConfig(bool depth, bool stencil, int msaa, GLESVersion version)
215         {
216             bool ret = Interop.GLView.GlViewSetGraphicsConfig(SwigCPtr, depth, stencil, msaa, (int)version);
217             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218             return ret;
219         }
220
221         /// <summary>
222         /// Renders once more, even when paused.
223         /// </summary>
224         public void RenderOnce()
225         {
226             Interop.GLView.GlViewRenderOnce(SwigCPtr);
227             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
228         }
229
230         private int OnRenderFrame(global::System.IntPtr cPtr)
231         {
232             if (glRenderFrameCallback != null)
233             {
234                 Matrix mvp = Matrix.GetMatrixFromPtr(Interop.GLView.GlViewGetRednerCallbackInputMvp(cPtr));
235                 Matrix projection = Matrix.GetMatrixFromPtr(Interop.GLView.GlViewGetRednerCallbackInputProjection(cPtr));
236                 Size2D size = Size2D.GetSize2DFromPtr(Interop.GLView.GlViewGetRednerCallbackInputSize(cPtr));
237                 Rectangle clippingBox = Rectangle.GetRectangleFromPtr(Interop.GLView.GlViewGetRednerCallbackInputClipplingBox(cPtr));
238                 int[] textureBindings = GetTextureBindings(cPtr);
239
240                 RenderCallbackInput input = new RenderCallbackInput(mvp, projection, size, clippingBox, textureBindings);
241
242                 return glRenderFrameCallback(input);
243             }
244             return 0;
245         }
246
247         private static int[] GetTextureBindings(global::System.IntPtr cPtr)
248         {
249             int bindingSize = 0;
250             global::System.IntPtr arrayPtr = Interop.GLView.GlViewGetRednerCallbackInputTextureBindings(cPtr, ref bindingSize);
251             if (bindingSize != 0)
252             {
253                 int[] result = new int[bindingSize];
254                 System.Runtime.InteropServices.Marshal.Copy(arrayPtr, result, 0, bindingSize);
255                 return result;
256             }
257             else
258             {
259                 return Array.Empty<int>();
260             }
261         }
262     }
263 }