Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ if libtype == 'shared'
conf.set('EPOXY_PUBLIC', '__attribute__((visibility("default"))) extern')
visibility_cflags += [ '-fvisibility=hidden' ]
endif
else
conf.set('EPOXY_STATIC_BUILD', true)
endif

# The inline keyword is available only for C++ in MSVC.
Expand Down
6 changes: 5 additions & 1 deletion src/dispatch_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
*/

#include "config.h"

#ifdef __GNUC__
#define EPOXY_THREADLOCAL __thread
#elif defined (_MSC_VER)
#define EPOXY_THREADLOCAL __declspec(thread)
#endif
#ifdef _WIN32
#define PLATFORM_HAS_EGL ENABLE_EGL
#define PLATFORM_HAS_GLX ENABLE_GLX
Expand Down
70 changes: 4 additions & 66 deletions src/dispatch_wgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include "dispatch_common.h"

static bool first_context_current = false;
static bool already_switched_to_dispatch_table = false;
//static bool first_context_current = false;
// static bool already_switched_to_dispatch_table = false;

/**
* If we can determine the WGL extension support from the current
Expand Down Expand Up @@ -75,70 +75,8 @@ epoxy_has_wgl_extension(HDC hdc, const char *ext)
void
epoxy_handle_external_wglMakeCurrent(void)
{
if (!first_context_current) {
first_context_current = true;
} else {
if (!already_switched_to_dispatch_table) {
already_switched_to_dispatch_table = true;
gl_switch_to_dispatch_table();
wgl_switch_to_dispatch_table();
}

gl_init_dispatch_table();
wgl_init_dispatch_table();
}
}

/**
* This global symbol is apparently looked up by Windows when loading
* a DLL, but it doesn't declare the prototype.
*/
BOOL WINAPI
DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved);

BOOL WINAPI
DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
{
void *data;

switch (reason) {
case DLL_PROCESS_ATTACH:
gl_tls_index = TlsAlloc();
if (gl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;
wgl_tls_index = TlsAlloc();
if (wgl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;

first_context_current = false;

/* FALLTHROUGH */

case DLL_THREAD_ATTACH:
data = LocalAlloc(LPTR, gl_tls_size);
TlsSetValue(gl_tls_index, data);

data = LocalAlloc(LPTR, wgl_tls_size);
TlsSetValue(wgl_tls_index, data);

break;

case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
data = TlsGetValue(gl_tls_index);
LocalFree(data);

data = TlsGetValue(wgl_tls_index);
LocalFree(data);

if (reason == DLL_PROCESS_DETACH) {
TlsFree(gl_tls_index);
TlsFree(wgl_tls_index);
}
break;
}

return TRUE;
gl_init_dispatch_table();
wgl_init_dispatch_table();
}

WRAPPER_VISIBILITY (BOOL)
Expand Down
28 changes: 26 additions & 2 deletions src/gen_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def write_thunks(self, func):
func.args_list))

def write_function_pointer(self, func):
self.outln('{0} epoxy_{1} = epoxy_{1}_global_rewrite_ptr;'.format(func.ptr_type, func.wrapped_name))
self.outln('{0} epoxy_{1} = EPOXY_DISPATCH_PTR(epoxy_{1});'.format(func.ptr_type, func.wrapped_name))
self.outln('')

def write_provider_enums(self):
Expand Down Expand Up @@ -816,14 +816,22 @@ def write_source(self, f):
self.write_thunks(func)
self.outln('')

self.outln('#define EPOXY_DISPATCH_PTR(name) name##_global_rewrite_ptr')

self.outln('#if USING_DISPATCH_TABLE')

self.outln('#undef EPOXY_DISPATCH_PTR')
self.outln('#define EPOXY_DISPATCH_PTR(name) name##_dispatch_table_thunk')

self.outln('static struct dispatch_table resolver_table = {')
for func in self.sorted_functions:
self.outln(' epoxy_{0}_dispatch_table_rewrite_ptr, /* {0} */'.format(func.wrapped_name))
self.outln('};')
self.outln('')
"""
self.outln('#ifdef EPOXY_STATIC_BUILD')

self.outln('#else')
self.outln('uint32_t {0}_tls_index;'.format(self.target))
self.outln('uint32_t {0}_tls_size = sizeof(struct dispatch_table);'.format(self.target))
self.outln('')
Expand All @@ -833,8 +841,21 @@ def write_source(self, f):
self.outln('{')
self.outln(' return TlsGetValue({0}_tls_index);'.format(self.target))
self.outln('}')
self.outln('#endif /* EPOXY_STATIC_BUILD */')
self.outln('')
"""

self.outln('EPOXY_THREADLOCAL struct dispatch_table {0}_tls_data = {{'.format(self.target))
for func in self.sorted_functions:
self.outln(' epoxy_{0}_dispatch_table_rewrite_ptr, /* {0} */'.format(func.wrapped_name))
self.outln('};')
self.outln('static inline struct dispatch_table *')
self.outln('get_dispatch_table(void)')
self.outln('{')
self.outln(' return &{0}_tls_data;'.format(self.target))
self.outln('}')
self.outln('')

self.outln('void')
self.outln('{0}_init_dispatch_table(void)'.format(self.target))
self.outln('{')
Expand All @@ -843,14 +864,17 @@ def write_source(self, f):
self.outln('}')
self.outln('')

"""
self.outln('void')
self.outln('{0}_switch_to_dispatch_table(void)'.format(self.target))
self.outln('{')

for func in self.sorted_functions:
self.outln(' epoxy_{0} = epoxy_{0}_dispatch_table_thunk;'.format(func.wrapped_name))

self.outln('}')
"""

self.outln('')

self.outln('#endif /* !USING_DISPATCH_TABLE */')
Expand Down