a2a7f8e9b30424df6902032d0f4d3fbb73e99632
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Window / GLWindow.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 extern alias TizenSystemInformation;
19
20 using System;
21 using System.ComponentModel;
22 using System.Collections.Generic;
23 using System.Runtime.InteropServices;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// The GLWindow class is to draw with native GLES.<br />
29     /// This class is the special window. It is for native GLES application.<br />
30     /// So, some special functions and type are supported.<br />
31     /// In addition, basic window's functions are supported, too.<br />
32     /// </summary>
33     [EditorBrowsable(EditorBrowsableState.Never)]
34     public partial class GLWindow : BaseHandle
35     {
36         internal GLWindow(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
37         {
38
39         }
40
41         /// <summary>
42         /// Creates an initialized handle to a new GLWindow.<br />
43         /// This creates an GLWindow with default options.!--<br />
44         /// </summary>
45         /// <returns>A new GLWindow.</returns>
46         [EditorBrowsable(EditorBrowsableState.Never)]
47         public GLWindow() : this(Interop.GLWindow.GlWindowNew(), true)
48         {
49             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
50         }
51
52         /// <summary>
53         /// Creates an initialized handle to a new GLWindow.<br />
54         /// This API can create GLWindow with specific option.<br />
55         /// </summary>
56         /// <param name="name">The name for GL window. </param>
57         /// <param name="windowPosition">The position and size of the Window.</param>
58         /// <param name="isTranslucent">Whether Window is translucent.</param>
59         /// <returns>A new Window.</returns>
60         [EditorBrowsable(EditorBrowsableState.Never)]
61         public GLWindow(string name, Rectangle windowPosition = null, bool isTranslucent = false) : this(Interop.GLWindow.GlWindowNew(Rectangle.getCPtr(windowPosition), name, "", isTranslucent), true)
62         {
63             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
64         }
65
66         /// <summary>
67         /// Enumeration for orientation of the window is the way in which a rectangular page is oriented for normal viewing.
68         /// </summary>
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public enum GLWindowOrientation
71         {
72             /// <summary>
73             /// Portrait orientation. The height of the display area is greater than the width.
74             /// </summary>
75             [EditorBrowsable(EditorBrowsableState.Never)]
76             Portrait = 0,
77
78             /// <summary>
79             /// Landscape orientation. A wide view area is needed.
80             /// </summary>
81             [EditorBrowsable(EditorBrowsableState.Never)]
82             Landscape = 90,
83
84             /// <summary>
85             /// Portrait inverse orientation.
86             /// </summary>
87             [EditorBrowsable(EditorBrowsableState.Never)]
88             PortraitInverse = 180,
89
90             /// <summary>
91             /// Landscape inverse orientation.
92             /// </summary>
93             [EditorBrowsable(EditorBrowsableState.Never)]
94             LandscapeInverse = 270,
95
96             /// <summary>
97             /// No orientation. It is for the preferred orientation
98             /// Especially, NoOrientationPreference only has the effect for the preferred orientation.
99             /// It is used to unset the preferred orientation with SetPreferredOrientation.
100             /// </summary>
101             [EditorBrowsable(EditorBrowsableState.Never)]
102             NoOrientationPreference = -1
103         }
104
105         /// <summary>
106         /// Gets or sets a size of the window.
107         /// </summary>
108         /// <exception cref="ArgumentNullException"> Thrown when value is null. </exception>
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         public Size2D WindowSize
111         {
112             get
113             {
114                 global::System.IntPtr intPtr = Interop.GLWindow.GlWindowGetPositionSize(SwigCPtr);
115                 var val = new Rectangle(intPtr, true);
116                 Size2D ret = new Size2D(val.Width, val.Height);
117                 val.Dispose();
118
119                 return ret;
120             }
121             set
122             {
123                 if (null == value)
124                 {
125                     throw new ArgumentNullException(nameof(value));
126                 }
127
128                 global::System.IntPtr intPtr = Interop.GLWindow.GlWindowGetPositionSize(SwigCPtr);
129                 Rectangle val = new Rectangle(intPtr, true);
130                 Rectangle ret = new Rectangle(val.X, val.Y, value.Width, value.Height);
131                 val.Dispose();
132
133                 Interop.GLWindow.GlWindowSetPositionSize(SwigCPtr, Rectangle.getCPtr(ret));
134                 ret.Dispose();
135             }
136         }
137
138         /// <summary>
139         /// Sets egl configuration for GLWindow
140         /// </summary>
141         /// <param name="depth">The flag of depth buffer. If true is set, 24bit depth buffer is enabled.</param>
142         /// <param name="stencil">The flag of stencil. it true is set, 8bit stencil buffer is enabled.</param>
143         /// <param name="msaa">The bit of msaa.</param>
144         /// <param name="version">The GLES version.</param>
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         public void SetEglConfig(bool depth, bool stencil, int msaa, GLESVersion version)
147         {
148             Interop.GLWindow.GlWindowSetEglConfig(SwigCPtr, depth, stencil, msaa, (int)version);
149             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150         }
151
152         /// <summary>
153         /// Shows the GLWindow if it is hidden.
154         /// </summary>
155         [EditorBrowsable(EditorBrowsableState.Never)]
156         public void Show()
157         {
158             Interop.GLWindow.GlWindowShow(SwigCPtr);
159             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
160         }
161
162         /// <summary>
163         /// Hides the GLWindow if it is showing.
164         /// </summary>
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public void Hide()
167         {
168             Interop.GLWindow.GlWindowHide(SwigCPtr);
169             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170         }
171
172         /// <summary>
173         /// Raises the window to the top of the window stack.
174         /// </summary>
175         [EditorBrowsable(EditorBrowsableState.Never)]
176         public void Raise()
177         {
178             Interop.GLWindow.GlWindowRaise(SwigCPtr);
179             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
180         }
181
182         /// <summary>
183         /// Lowers the window to the bottom of the window stack.
184         /// </summary>
185         [EditorBrowsable(EditorBrowsableState.Never)]
186         public void Lower()
187         {
188             Interop.GLWindow.GlWindowLower(SwigCPtr);
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190         }
191
192         /// <summary>
193         /// Activates the window to the top of the window stack even it is iconified.
194         /// </summary>
195         [EditorBrowsable(EditorBrowsableState.Never)]
196         public void Activate()
197         {
198             Interop.GLWindow.GlWindowActivate(SwigCPtr);
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200         }
201
202         /// <summary>
203         /// Sets position and size of the window. This API guarantees that
204         /// both moving and resizing of window will appear on the screen at once.
205         /// </summary>
206         [EditorBrowsable(EditorBrowsableState.Never)]
207         public Rectangle WindowPositionSize
208         {
209             get
210             {
211                 global::System.IntPtr intPtr = Interop.GLWindow.GlWindowGetPositionSize(SwigCPtr);
212                 Rectangle val = new Rectangle(intPtr, true);
213                 Rectangle ret = new Rectangle(val.X, val.Y, val.Width, val.Height);
214                 val.Dispose();
215
216                 return ret;
217             }
218             set
219             {
220                 Interop.GLWindow.GlWindowSetPositionSize(SwigCPtr, Rectangle.getCPtr(value));
221             }
222         }
223
224         /// <summary>
225         /// Gets the count of supported auxiliary hints of the GLWindow.
226         /// </summary>
227         /// <returns>The number of supported auxiliary hints.</returns>
228         [EditorBrowsable(EditorBrowsableState.Never)]
229         public uint GetSupportedAuxiliaryHintCount()
230         {
231             uint ret = Interop.GLWindow.GlWindowGetSupportedAuxiliaryHintCount(SwigCPtr);
232             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
233             return ret;
234         }
235
236         /// <summary>
237         /// Gets the supported auxiliary hint string of the GLWindow.
238         /// </summary>
239         /// <param name="index">The index of the supported auxiliary hint lists.</param>
240         /// <returns>The auxiliary hint string of the index.</returns>
241         [EditorBrowsable(EditorBrowsableState.Never)]
242         public string GetSupportedAuxiliaryHint(uint index)
243         {
244             string ret = Interop.GLWindow.GlWindowGetSupportedAuxiliaryHint(SwigCPtr, index);
245             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
246             return ret;
247         }
248
249         /// <summary>
250         /// Creates an auxiliary hint of the GLWindow.
251         /// </summary>
252         /// <param name="hint">The auxiliary hint string.</param>
253         /// <param name="value">The value string.</param>
254         /// <returns>The ID of created auxiliary hint, or 0 on failure.</returns>
255         [EditorBrowsable(EditorBrowsableState.Never)]
256         public uint AddAuxiliaryHint(string hint, string value)
257         {
258             uint ret = Interop.GLWindow.GlWindowAddAuxiliaryHint(SwigCPtr, hint, value);
259             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
260             return ret;
261         }
262
263         /// <summary>
264         /// Removes an auxiliary hint of the GLWindow.
265         /// </summary>
266         /// <param name="id">The ID of the auxiliary hint.</param>
267         /// <returns>True if no error occurred, false otherwise.</returns>
268         [EditorBrowsable(EditorBrowsableState.Never)]
269         public bool RemoveAuxiliaryHint(uint id)
270         {
271             bool ret = Interop.GLWindow.GlWindowRemoveAuxiliaryHint(SwigCPtr, id);
272             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
273             return ret;
274         }
275
276         /// <summary>
277         /// Changes a value of the auxiliary hint.
278         /// </summary>
279         /// <param name="id">The auxiliary hint ID.</param>
280         /// <param name="value">The value string to be set.</param>
281         /// <returns>True if no error occurred, false otherwise.</returns>
282         [EditorBrowsable(EditorBrowsableState.Never)]
283         public bool SetAuxiliaryHintValue(uint id, string value)
284         {
285             bool ret = Interop.GLWindow.GlWindowSetAuxiliaryHintValue(SwigCPtr, id, value);
286             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
287             return ret;
288         }
289
290         /// <summary>
291         /// Gets a value of the auxiliary hint.
292         /// </summary>
293         /// <param name="id">The auxiliary hint ID.</param>
294         /// <returns>The string value of the auxiliary hint ID, or an empty string if none exists.</returns>
295         [EditorBrowsable(EditorBrowsableState.Never)]
296         public string GetAuxiliaryHintValue(uint id)
297         {
298             string ret = Interop.GLWindow.GlWindowGetAuxiliaryHintValue(SwigCPtr, id);
299             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
300             return ret;
301         }
302
303         /// <summary>
304         /// Gets an ID of the auxiliary hint string.
305         /// </summary>
306         /// <param name="hint">The auxiliary hint string.</param>
307         /// <returns>The ID of auxiliary hint string, or 0 on failure.</returns>
308         [EditorBrowsable(EditorBrowsableState.Never)]
309         public uint GetAuxiliaryHintId(string hint)
310         {
311             uint ret = Interop.GLWindow.GlWindowGetAuxiliaryHintId(SwigCPtr, hint);
312             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
313             return ret;
314         }
315
316         /// <summary>
317         /// Sets a region to accept input events.
318         /// </summary>
319         /// <param name="inputRegion">The region to accept input events.</param>
320         [EditorBrowsable(EditorBrowsableState.Never)]
321         public void SetInputRegion(Rectangle inputRegion)
322         {
323             Interop.GLWindow.GlWindowSetInputRegion(SwigCPtr, Rectangle.getCPtr(inputRegion));
324             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
325         }
326
327         /// <summary>
328         /// Sets a transparent window's visual state to opaque. <br />
329         /// If a visual state of a transparent window is opaque, <br />
330         /// then the window manager could handle it as an opaque window when calculating visibility.
331         /// </summary>
332         /// <param name="opaque">Whether the window's visual state is opaque.</param>
333         /// <remarks>This will have no effect on an opaque window. <br />
334         /// It doesn't change transparent window to opaque window but lets the window manager know the visual state of the window.
335         /// </remarks>
336         [EditorBrowsable(EditorBrowsableState.Never)]
337         public void SetOpaqueState(bool opaque)
338         {
339             Interop.GLWindow.GlWindowSetOpaqueState(SwigCPtr, opaque);
340             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
341         }
342
343         /// <summary>
344         /// Returns whether a transparent window's visual state is opaque or not.
345         /// </summary>
346         /// <returns>True if the window's visual state is opaque, false otherwise.</returns>
347         /// <remarks> The return value has no meaning on an opaque window. </remarks>
348         [EditorBrowsable(EditorBrowsableState.Never)]
349         public bool IsOpaqueState()
350         {
351             bool ret = Interop.GLWindow.GlWindowIsOpaqueState(SwigCPtr);
352             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
353             return ret;
354         }
355
356         /// <summary>
357         /// Sets a preferred orientation.
358         /// </summary>
359         /// <param name="orientation">The preferred orientation.</param>
360         [EditorBrowsable(EditorBrowsableState.Never)]
361         public void SetPreferredOrientation(GLWindow.GLWindowOrientation orientation)
362         {
363             Interop.GLWindow.GlWindowSetPreferredOrientation(SwigCPtr, (int)orientation);
364             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
365         }
366
367         /// <summary>
368         /// Gets current orientation of the window.
369         /// </summary>
370         /// <returns>The current window orientation if previously set, or none.</returns>
371         [EditorBrowsable(EditorBrowsableState.Never)]
372         public GLWindow.GLWindowOrientation GetCurrentOrientation()
373         {
374             GLWindow.GLWindowOrientation ret = (GLWindow.GLWindowOrientation)Interop.GLWindow.GlWindowGetCurrentOrientation(SwigCPtr);
375             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376             return ret;
377         }
378
379         /// <summary>
380         /// Sets available orientations of the window.
381         /// This API is for setting several orientations one time.
382         /// </summary>
383         /// <param name="orientations">The list of orientations.</param>
384         /// <exception cref="ArgumentNullException"> Thrown when orientations is null. </exception>
385         [EditorBrowsable(EditorBrowsableState.Never)]
386         public void SetAvailableOrientations(List<GLWindow.GLWindowOrientation> orientations)
387         {
388             if (null == orientations)
389             {
390                 throw new ArgumentNullException(nameof(orientations));
391             }
392
393             PropertyArray orientationArray = new PropertyArray();
394             for (int i = 0; i < orientations.Count; i++)
395             {
396                 PropertyValue val = new PropertyValue((int)orientations[i]);
397                 orientationArray.PushBack(val);
398                 val.Dispose();
399             }
400
401             Interop.GLWindow.GlWindowSetAvailableOrientations(SwigCPtr, PropertyArray.getCPtr(orientationArray), orientations.Count);
402             orientationArray.Dispose();
403             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
404         }
405
406         /// <summary>
407         /// Allows at least one more render, even when paused.
408         /// The window should be shown, not minimised.
409         /// </summary>
410         [EditorBrowsable(EditorBrowsableState.Never)]
411         public void RenderOnce()
412         {
413             Interop.GLWindow.GlWindowRenderOnce(SwigCPtr);
414             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415         }
416
417         /// <summary>
418         /// Type of callback to initialize native GL code.
419         /// </summary>
420         [EditorBrowsable(EditorBrowsableState.Never)]
421         public delegate void GLInitializeCallbackType();
422
423         GLInitializeCallbackType GLInitializeCallback;
424         HandleRef InitHandleRef;
425
426         /// <summary>
427         /// Type of callback to render to frame to use native GL code.
428         /// </summary>
429         [EditorBrowsable(EditorBrowsableState.Never)]
430         public delegate int GLRenderFrameCallbackType();
431
432         GLRenderFrameCallbackType GLRenderFrameCallback;
433         HandleRef RenderHandlerRef;
434
435
436         /// <summary>
437         /// Type of callback to cleanup native GL resource.
438         /// </summary>
439         [EditorBrowsable(EditorBrowsableState.Never)]
440         public delegate void GLTerminateCallbackType();
441
442         GLTerminateCallbackType GLTerminateCallback;
443         HandleRef TerminateHandlerRef;
444
445         /// <summary>
446         /// Registers a GL callback function for application.
447         /// </summary>
448         /// <param name="glInit">The callback function for application initialize</param>
449         /// <param name="glRenderFrame">The callback function to render to the frame</param>
450         /// <param name="glTerminate">The callback function to clean-up application GL resource</param>
451         [EditorBrowsable(EditorBrowsableState.Never)]
452         public void RegisterGlCallback(GLInitializeCallbackType glInit, GLRenderFrameCallbackType glRenderFrame, GLTerminateCallbackType glTerminate)
453         {
454             GLInitializeCallback = glInit;
455             InitHandleRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(GLInitializeCallback));
456
457             GLRenderFrameCallback = glRenderFrame;
458             RenderHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(GLRenderFrameCallback));
459
460             GLTerminateCallback = glTerminate;
461             TerminateHandlerRef = new HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(GLTerminateCallback));
462
463             Interop.GLWindow.GlWindowRegisterGlCallback(SwigCPtr, InitHandleRef, RenderHandlerRef, TerminateHandlerRef);
464
465             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
466         }
467
468         /// <summary>
469         /// Destroy the window immediately.
470         /// </summary>
471         [EditorBrowsable(EditorBrowsableState.Never)]
472         public void Destroy()
473         {
474             this.Dispose();
475         }
476
477         /// <summary>
478         /// Gets or sets a Rendeirng Mode of GLWindow.
479         /// </summary>
480         [EditorBrowsable(EditorBrowsableState.Never)]
481         public GLRenderingMode RenderingMode
482         {
483             [EditorBrowsable(EditorBrowsableState.Never)]
484             get
485             {
486                 GLRenderingMode mode = (GLRenderingMode)Interop.GLWindow.GlWindowGetRenderingMode(SwigCPtr);
487                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
488                 return mode;
489             }
490             [EditorBrowsable(EditorBrowsableState.Never)]
491             set
492             {
493                 Interop.GLWindow.GlWindowSetRenderingMode(SwigCPtr, (int)value);
494                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
495             }
496         }
497
498         /// <summary>
499         /// Dispose for Window
500         /// </summary>
501         protected override void Dispose(DisposeTypes type)
502         {
503             if (disposed)
504             {
505                 return;
506             }
507
508             this.DisconnectNativeSignals();
509
510             base.Dispose(type);
511         }
512
513         /// This will not be public opened.
514         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
515         {
516             Interop.Window.DeleteWindow(swigCPtr);
517         }
518     }
519 }