[NUI] Fix some svace issues.
[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         public void BindTextureResources(List<Texture> textures)
182         {
183             unsafe
184             {
185                 if (textures != null && textures.Count > 0)
186                 {
187                     IntPtr unmanagedPointer = Marshal.AllocHGlobal(sizeof(IntPtr) * textures.Count);
188                     IntPtr[] texturesArray = new IntPtr[textures.Count];
189                     for (int i = 0; i < textures.Count; i++)
190                     {
191                         texturesArray[i] = HandleRef.ToIntPtr(Texture.getCPtr(textures[i]));
192                     }
193                     System.Runtime.InteropServices.Marshal.Copy(texturesArray, 0, unmanagedPointer, textures.Count);
194
195                     Interop.GLView.GlViewBindTextureResources(SwigCPtr, unmanagedPointer, textures.Count);
196                     Marshal.FreeHGlobal(unmanagedPointer);
197                 }
198             }
199         }
200
201         /// <summary>
202         /// Sets graphics configuration for the DirectRenderingGLView
203         /// </summary>
204         /// <param name="depth">The flag of depth buffer. When the value is true, 24bit depth buffer is enabled.</param>
205         /// <param name="stencil">The flag of stencil. When the value is true, 8bit stencil buffer is enabled.</param>
206         /// <param name="msaa">The bit of MSAA</param>
207         /// <param name="version">The GLES version</param>
208         /// <returns>True if the config was successfully set, false otherwise.</returns>
209         public bool SetGraphicsConfig(bool depth, bool stencil, int msaa, GLESVersion version)
210         {
211             bool ret = Interop.GLView.GlViewSetGraphicsConfig(SwigCPtr, depth, stencil, msaa, (int)version);
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213             return ret;
214         }
215
216         /// <summary>
217         /// Renders once more, even when paused.
218         /// </summary>
219         public void RenderOnce()
220         {
221             Interop.GLView.GlViewRenderOnce(SwigCPtr);
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223         }
224
225         private int OnRenderFrame(global::System.IntPtr cPtr)
226         {
227             if (glRenderFrameCallback != null)
228             {
229                 Matrix mvp = Matrix.GetMatrixFromPtr(Interop.GLView.GlViewGetRednerCallbackInputMvp(cPtr));
230                 Matrix projection = Matrix.GetMatrixFromPtr(Interop.GLView.GlViewGetRednerCallbackInputProjection(cPtr));
231                 Size2D size = Size2D.GetSize2DFromPtr(Interop.GLView.GlViewGetRednerCallbackInputSize(cPtr));
232                 Rectangle clippingBox = Rectangle.GetRectangleFromPtr(Interop.GLView.GlViewGetRednerCallbackInputClipplingBox(cPtr));
233                 int[] textureBindings = GetTextureBindings(cPtr);
234
235                 RenderCallbackInput input = new RenderCallbackInput(mvp, projection, size, clippingBox, textureBindings);
236
237                 return glRenderFrameCallback(input);
238             }
239             return 0;
240         }
241
242         private static int[] GetTextureBindings(global::System.IntPtr cPtr)
243         {
244             int bindingSize = 0;
245             global::System.IntPtr arrayPtr = Interop.GLView.GlViewGetRednerCallbackInputTextureBindings(cPtr, ref bindingSize);
246             if (bindingSize != 0)
247             {
248                 int[] result = new int[bindingSize];
249                 System.Runtime.InteropServices.Marshal.Copy(arrayPtr, result, 0, bindingSize);
250                 return result;
251             }
252             else
253             {
254                 return Array.Empty<int>();
255             }
256         }
257     }
258 }