aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xwin/glx/glwrap.c
blob: 5190d36f7c8ff25c797383cdb2f0c8d9b1f90caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * File: glwrap.c
 * Purpose: Wrapper functions for Win32 OpenGL functions
 *
 * Authors: Alexander Gottwald
 *          Jon TURNEY
 *
 * Copyright (c) Jon TURNEY 2009
 * Copyright (c) Alexander Gottwald 2004
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

// define USE_OPENGL32 makes gl.h declare gl*() function prototypes with stdcall linkage,
// so our generated wrappers will correctly link with the functions in opengl32.dll
#define USE_OPENGL32

#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif

#include <X11/Xwindows.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <glx/glxserver.h>
#include <glx/glxext.h>
#include <glx/glapi.h>
#include <glx/dispatch.h>
#include <glwindows.h>

static unsigned int glWinIndirectProcCalls = 0;
static unsigned int glWinDirectProcCalls = 0;

void
glWinCallDelta(void)
{
  static unsigned int glWinIndirectProcCallsLast = 0;
  static unsigned int glWinDirectProcCallsLast = 0;
  if ((glWinIndirectProcCalls != glWinIndirectProcCallsLast) ||
      (glWinDirectProcCalls != glWinDirectProcCallsLast))
    {
      if (glxWinDebugSettings.enableTrace)
        {
          ErrorF("after %d direct and %d indirect GL calls\n",
                 glWinDirectProcCalls - glWinDirectProcCallsLast,
                 glWinIndirectProcCalls - glWinIndirectProcCallsLast);
        }
      glWinDirectProcCallsLast = glWinDirectProcCalls;
      glWinIndirectProcCallsLast = glWinIndirectProcCalls;
    }
}

static PROC
glWinResolveHelper(PROC *cache, char *symbol)
{
  PROC proc = NULL;

  /* If not yet cached, call wglGetProcAddress */
  if ((*cache) == NULL)
    {
      proc = wglGetProcAddress(symbol);
      if (proc == NULL)
        {
          ErrorF("glwrap: Can't resolve \"%s\"\n", symbol);
          (*cache) = (PROC)-1;
        }
      else
        {
          ErrorF("glwrap: Resolved \"%s\"\n", symbol);
          (*cache) = proc;
        }
    }
  /* Cached wglGetProcAddress failure */
  else if ((*cache) == (PROC)-1)
    {
      proc = 0;
    }
  /* Cached wglGetProcAddress result */
  else
    {
      proc = (*cache);
    }

  return proc;
}

#define RESOLVE_RET(proctype, symbol, retval) \
    static PROC cache = NULL; \
    __stdcall proctype proc = (proctype)glWinResolveHelper(&cache, symbol); \
    if (proc == NULL) { \
        __glXErrorCallBack(0); \
        return retval; \
    } \
    glWinIndirectProcCalls++;

#define RESOLVE(proctype, symbol) RESOLVE_RET(proctype, symbol,)

#define RESOLVED_PROC(proctype) proc

/*
  Include generated cdecl wrappers for stdcall gl*() functions in opengl32.dll

  OpenGL 1.2 and upward is treated as extensions, function address must
  found using wglGetProcAddress(), but also stdcall so still need wrappers...

  Include generated dispatch table setup function
*/

#include "generated_gl_wrappers.c"

/*
  Special non-static wrapper for glGetString for debug output
*/

const GLubyte* glGetStringWrapperNonstatic(GLenum name)
{
  return glGetString(name);
}

/*
  Special non-static wrapper for glAddSwapHintRectWIN for copySubBuffers
*/

typedef void (__stdcall *PFNGLADDSWAPHINTRECTWIN)(GLint x, GLint y, GLsizei width, GLsizei height);

void glAddSwapHintRectWINWrapperNonstatic(GLint x, GLint y, GLsizei width, GLsizei height)
{
  RESOLVE(PFNGLADDSWAPHINTRECTWIN, "glAddSwapHintRectWIN");
  proc(x, y, width, height);
}