[OpenTK] Introduce OpenTK (#336)
[platform/core/csapi/tizenfx.git] / external / src / OpenTK / OpenTK / Graphics / GraphicsBindingsBase.cs
1 //
2 // The Open Toolkit Library License
3 //
4 // Copyright (c) 2006 - 2009 the Open Toolkit library.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights to
9 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 // the Software, and to permit persons to whom the Software is furnished to do
11 // so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 using System;
27 using System.Diagnostics;
28
29 namespace OpenTK.Graphics
30 {
31     /// <summary>
32     /// Implements BindingsBase for the OpenTK.Graphics namespace (OpenGL and OpenGL|ES).
33     /// </summary>
34     public abstract class GraphicsBindingsBase : BindingsBase
35     {
36         internal IntPtr[] _EntryPointsInstance;
37         internal byte[] _EntryPointNamesInstance;
38         internal int[] _EntryPointNameOffsetsInstance;
39
40         /// <summary>
41         /// Retrieves an unmanaged function pointer to the specified function.
42         /// </summary>
43         /// <param name="funcname">
44         /// A <see cref="System.String"/> that defines the name of the function.
45         /// </param>
46         /// <returns>
47         /// A <see cref="IntPtr"/> that contains the address of funcname or IntPtr.Zero,
48         /// if the function is not supported by the drivers.
49         /// </returns>
50         /// <remarks>
51         /// Note: some drivers are known to return non-zero values for unsupported functions.
52         /// Typical values include 1 and 2 - inheritors are advised to check for and ignore these
53         /// values.
54         /// </remarks>
55         protected override IntPtr GetAddress(string funcname)
56         {
57             var context = GraphicsContext.CurrentContext as IGraphicsContextInternal;
58             if (context == null)
59             {
60                 throw new GraphicsContextMissingException();
61             }
62             return context != null ? context.GetAddress(funcname) : IntPtr.Zero;
63         }
64
65         // Loads all available entry points for the current API.
66         // Note: we prefer IGraphicsContextInternal.GetAddress over
67         // this.GetAddress to improve loading performance (less
68         // validation necessary.)
69         internal override void LoadEntryPoints()
70         {
71             Debug.Print("Loading entry points for {0}", GetType().FullName);
72
73             IGraphicsContext context = GraphicsContext.CurrentContext;
74             if (context == null)
75             {
76                 throw new GraphicsContextMissingException();
77             }
78
79             IGraphicsContextInternal context_internal = context as IGraphicsContextInternal;
80             unsafe
81             {
82                 fixed (byte* name = _EntryPointNamesInstance)
83                 {
84                     for (int i = 0; i < _EntryPointsInstance.Length; i++)
85                     {
86                         _EntryPointsInstance[i] = context_internal.GetAddress(
87                             new IntPtr(name + _EntryPointNameOffsetsInstance[i]));
88                     }
89                 }
90             }
91         }
92     }
93 }