forked from i-rinat/libvdpau-va-gl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx-stack.c
158 lines (136 loc) · 4.43 KB
/
ctx-stack.c
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
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 2013 Rinat Ibragimov
*
* This file is part of libvdpau-va-gl
*
* libvdpau-va-gl distributed under the terms of LGPLv3. See COPYING for details.
*/
/*
* glx context stack
*/
#define _GNU_SOURCE
#include "ctx-stack.h"
#include "globals.h"
#include <assert.h>
#include "vdpau-trace.h"
#include "vdpau-locking.h"
#include <sys/syscall.h>
#include <unistd.h>
static __thread Display *glx_ctx_stack_display;
static __thread Drawable glx_ctx_stack_wnd;
static __thread GLXContext glx_ctx_stack_glc;
static __thread int glx_ctx_stack_same;
static __thread int glx_ctx_stack_element_count = 0;
GHashTable *glc_hash_table = NULL;
int glc_hash_table_ref_count = 0;
GLXContext root_glc;
XVisualInfo *root_vi;
void
glx_context_push_global(Display *dpy, Drawable wnd, GLXContext glc)
{
pthread_mutex_lock(&global.glx_ctx_stack_mutex);
assert(0 == glx_ctx_stack_element_count);
glx_ctx_stack_display = glXGetCurrentDisplay();
glx_ctx_stack_wnd = glXGetCurrentDrawable();
glx_ctx_stack_glc = glXGetCurrentContext();
glx_ctx_stack_same = 0;
glx_ctx_stack_element_count ++;
if (dpy == glx_ctx_stack_display && wnd == glx_ctx_stack_wnd && glc == glx_ctx_stack_glc) {
// Same context. Don't call MakeCurrent.
glx_ctx_stack_same = 1;
} else {
glx_ctx_stack_same = 0;
locked_glXMakeCurrent(dpy, wnd, glc);
}
pthread_mutex_unlock(&global.glx_ctx_stack_mutex);
}
void
glx_context_push_thread_local(VdpDeviceData *deviceData)
{
pthread_mutex_lock(&global.glx_ctx_stack_mutex);
Display *dpy = deviceData->display;
const Window wnd = deviceData->root;
const gint thread_id = (gint) syscall(__NR_gettid);
GLXContext glc = g_hash_table_lookup(glc_hash_table, GINT_TO_POINTER(thread_id));
if (!glc) {
glc = glXCreateContext(dpy, root_vi, root_glc, GL_TRUE);
assert(glc);
g_hash_table_insert(glc_hash_table, GINT_TO_POINTER(thread_id), glc);
}
glx_ctx_stack_display = glXGetCurrentDisplay();
glx_ctx_stack_wnd = glXGetCurrentDrawable();
glx_ctx_stack_glc = glXGetCurrentContext();
glx_ctx_stack_element_count ++;
if (dpy == glx_ctx_stack_display && wnd == glx_ctx_stack_wnd && glc == glx_ctx_stack_glc) {
// Same context. Don't call MakeCurrent.
glx_ctx_stack_same = 1;
} else {
glx_ctx_stack_same = 0;
locked_glXMakeCurrent(dpy, wnd, glc);
}
pthread_mutex_unlock(&global.glx_ctx_stack_mutex);
}
void
glx_context_pop()
{
pthread_mutex_lock(&global.glx_ctx_stack_mutex);
assert(1 == glx_ctx_stack_element_count);
if (!glx_ctx_stack_same) {
if (glx_ctx_stack_display)
locked_glXMakeCurrent(glx_ctx_stack_display, glx_ctx_stack_wnd, glx_ctx_stack_glc);
}
glx_ctx_stack_element_count --;
pthread_mutex_unlock(&global.glx_ctx_stack_mutex);
}
void
glx_context_ref_glc_hash_table(Display *dpy, int screen)
{
pthread_mutex_lock(&global.glx_ctx_stack_mutex);
if (0 == glc_hash_table_ref_count) {
glc_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
glc_hash_table_ref_count = 1;
XLockDisplay(dpy);
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
root_vi = glXChooseVisual(dpy, screen, att);
if (NULL == root_vi) {
traceError("error (glx_context_ref_glc_hash_table): glXChooseVisual failed\n");
XUnlockDisplay(dpy);
return;
}
root_glc = glXCreateContext(dpy, root_vi, NULL, GL_TRUE);
XUnlockDisplay(dpy);
} else {
glc_hash_table_ref_count ++;
}
pthread_mutex_unlock(&global.glx_ctx_stack_mutex);
}
static
void
glc_hash_destroy_func(gpointer key, gpointer value, gpointer user_data)
{
(void)key;
GLXContext glc = value;
Display *dpy = user_data;
glXDestroyContext(dpy, glc);
}
void
glx_context_unref_glc_hash_table(Display *dpy)
{
pthread_mutex_lock(&global.glx_ctx_stack_mutex);
glc_hash_table_ref_count --;
if (0 == glc_hash_table_ref_count) {
XLockDisplay(dpy);
g_hash_table_foreach(glc_hash_table, glc_hash_destroy_func, dpy);
g_hash_table_unref(glc_hash_table);
glc_hash_table = NULL;
glXDestroyContext(dpy, root_glc);
XFree(root_vi);
XUnlockDisplay(dpy);
}
pthread_mutex_unlock(&global.glx_ctx_stack_mutex);
}
GLXContext
glx_context_get_root_context(void)
{
return root_glc;
}