2 using System.ComponentModel;
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Diagnostics.CodeAnalysis;
8 namespace Tizen.NUI.BaseComponents
11 /// DirectRenderingGLView allows drawing with OpenGL. You can render to a Window directly.
12 /// DirectRenderingGLView creates a context.
14 /// <since_tizen> 11 </since_tizen>
15 [EditorBrowsable(EditorBrowsableState.Never)]
16 public class DirectRenderingGLView : View
19 /// The parameter of the RenderFrame Callback.
20 /// It has data to render directly.
22 [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
23 public class RenderCallbackInput
28 Rectangle clippingBox;
29 ReadOnlyCollection<int> textureBindings;
31 public RenderCallbackInput(Matrix mvp, Matrix projection, Size2D size, Rectangle clippingBox, int[] textureBindings)
34 this.projection = projection;
36 this.clippingBox = clippingBox;
37 this.textureBindings = new ReadOnlyCollection<int>(textureBindings);
51 public Matrix Projection
53 get { return projection; }
57 /// The size of the DirectRenderingGLView
65 /// The area of DirectRenderingGLView. You can use this for glScissor()
67 public Rectangle ClippingBox
69 get { return clippingBox; }
75 public ReadOnlyCollection<int> TextureBindings
77 get { return textureBindings; }
81 private GLInitializeDelegate glInitializeCallback;
82 private GLRenderFrameDelegate glRenderFrameCallback;
83 private GLTerminateDelegate glTerminateCallback;
84 private InternalGLRenderFrameDelegate internalRenderFrameCallback;
87 /// Type of callback to initialize OpenGLES.
89 public delegate void GLInitializeDelegate();
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.
95 /// <returns>The return value is not 0, the eglSwapBuffers() will be called.</returns>
96 public delegate int GLRenderFrameDelegate(in RenderCallbackInput input);
98 private delegate int InternalGLRenderFrameDelegate(global::System.IntPtr cPtr);
101 /// Type of callback to clean up GL resource.
103 public delegate void GLTerminateDelegate();
105 internal DirectRenderingGLView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
110 /// Creates an initialized DirectRenderingGLView.
112 /// <param name="colorFormat">The format of the color buffer</param>
113 public DirectRenderingGLView(ColorFormat colorFormat) : this(Interop.GLView.New(0, (int)colorFormat), true)
115 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
119 /// Enumeration for the color format of the color buffer
121 public enum ColorFormat
124 /// 8 red bits, 8 green bits, 8 blue bits
129 /// 8 red bits, 8 green bits, 8 blue bits, alpha 8 bits
135 /// Gets or sets the rendering mode of the DirectRenderingGLView.
137 public GLRenderingMode RenderingMode
139 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "SWIG boilerplate, no exceptions are expected")]
142 GLRenderingMode ret = (GLRenderingMode)Interop.GLView.GlViewGetRenderingMode(SwigCPtr);
143 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
148 Interop.GLView.GlViewSetRenderingMode(SwigCPtr, (int)value);
149 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154 /// Registers GL callback functions to render with OpenGL ES
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)
161 glInitializeCallback = glInit;
162 HandleRef InitHandleRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(glInitializeCallback));
164 glRenderFrameCallback = glRenderFrame;
165 internalRenderFrameCallback = OnRenderFrame;
166 HandleRef RenderHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(internalRenderFrameCallback));
168 glTerminateCallback = glTerminate;
169 HandleRef TerminateHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(glTerminateCallback));
171 Interop.GLView.GlViewRegisterGlCallbacks(SwigCPtr, InitHandleRef, RenderHandlerRef, TerminateHandlerRef);
173 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
177 /// Binds textures to own context.
178 /// You can get the bind IDs in RenderCallbackInput in the glRenderFrame callback.
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)
186 if (textures != null)
188 int count = textures.Count;
191 IntPtr[] texturesArray = new IntPtr[count];
192 for (int i = 0; i < count; i++)
194 texturesArray[i] = HandleRef.ToIntPtr(Texture.getCPtr(textures[i]));
196 IntPtr unmanagedPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * count);
197 Marshal.Copy(texturesArray, 0, unmanagedPointer, count);
199 Interop.GLView.GlViewBindTextureResources(SwigCPtr, unmanagedPointer, count);
200 Marshal.FreeHGlobal(unmanagedPointer);
207 /// Sets graphics configuration for the DirectRenderingGLView
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)
216 bool ret = Interop.GLView.GlViewSetGraphicsConfig(SwigCPtr, depth, stencil, msaa, (int)version);
217 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
222 /// Renders once more, even when paused.
224 public void RenderOnce()
226 Interop.GLView.GlViewRenderOnce(SwigCPtr);
227 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230 private int OnRenderFrame(global::System.IntPtr cPtr)
232 if (glRenderFrameCallback != null)
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);
240 RenderCallbackInput input = new RenderCallbackInput(mvp, projection, size, clippingBox, textureBindings);
242 return glRenderFrameCallback(input);
247 private static int[] GetTextureBindings(global::System.IntPtr cPtr)
250 global::System.IntPtr arrayPtr = Interop.GLView.GlViewGetRednerCallbackInputTextureBindings(cPtr, ref bindingSize);
251 if (bindingSize != 0)
253 int[] result = new int[bindingSize];
254 System.Runtime.InteropServices.Marshal.Copy(arrayPtr, result, 0, bindingSize);
259 return Array.Empty<int>();