| 1 |
#include <dlfcn.h> |
| 2 |
#include <pthread.h> |
| 3 |
#include <semaphore.h> |
| 4 |
#include <time.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <sys/socket.h> |
| 7 |
#include <sys/un.h> |
| 8 |
#include <errno.h> |
| 9 |
#include <cstdlib> |
| 10 |
#include <cstring> |
| 11 |
#include <cstdio> |
| 12 |
#include <cassert> |
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
#include <X11/Xatom.h> |
| 16 |
#pragma GCC visibility push(default) |
| 17 |
#define GLX_GLXEXT_PROTOTYPES |
| 18 |
#define GL_GLEXT_PROTOTYPES |
| 19 |
#include <GL/glx.h> |
| 20 |
#pragma GCC visibility pop |
| 21 |
|
| 22 |
#define primus_print(c, ...) do { if (c) fprintf(stderr, "primus: " __VA_ARGS__); } while (0) |
| 23 |
|
| 24 |
#define die_if(cond, ...) do {if (cond) {primus_print(true, "fatal: " __VA_ARGS__); exit(1);} } while (0) |
| 25 |
#define primus_warn(...) primus_print(primus.loglevel >= 1, "warning: " __VA_ARGS__) |
| 26 |
#define primus_perf(...) primus_print(primus.loglevel >= 2, "profiling: " __VA_ARGS__) |
| 27 |
|
| 28 |
// Try to load any of the colon-separated libraries |
| 29 |
static void *mdlopen(const char *paths, int flag) |
| 30 |
{ |
| 31 |
char *p = strdupa(paths); |
| 32 |
char errors[1024], *errors_ptr = errors, *errors_end = errors + 1024; |
| 33 |
for (char *c = p; c; p = c + 1) |
| 34 |
{ |
| 35 |
if ((c = strchr(p, ':'))) |
| 36 |
*c = 0; |
| 37 |
die_if(p[0] != '/', "need absolute library path: %s\n", p); |
| 38 |
void *handle = dlopen(p, flag); |
| 39 |
if (handle) |
| 40 |
return handle; |
| 41 |
errors_ptr += snprintf(errors_ptr, errors_end - errors_ptr, "%s\n", dlerror()); |
| 42 |
} |
| 43 |
die_if(true, "failed to load any of the libraries: %s\n%s", paths, errors); |
| 44 |
} |
| 45 |
|
| 46 |
static void *real_dlsym(void *handle, const char *symbol) |
| 47 |
{ |
| 48 |
typedef void* (*dlsym_fn)(void *, const char*); |
| 49 |
static dlsym_fn pdlsym = (dlsym_fn) dlsym(dlopen("libdl.so.2", RTLD_LAZY), "dlsym"); |
| 50 |
return pdlsym(handle, symbol); |
| 51 |
} |
| 52 |
|
| 53 |
// Pointers to implemented/forwarded GLX and OpenGL functions |
| 54 |
struct CapturedFns { |
| 55 |
void *handle; |
| 56 |
// Declare functions as fields of the struct |
| 57 |
#define DEF_GLX_PROTO(ret, name, args, ...) ret (*name) args; |
| 58 |
#include "glx-reimpl.def" |
| 59 |
#include "glx-dpyredir.def" |
| 60 |
#include "glxext-reimpl.def" |
| 61 |
#include "gl-passthru.def" |
| 62 |
#include "gl-needed.def" |
| 63 |
#undef DEF_GLX_PROTO |
| 64 |
CapturedFns(const char *lib) |
| 65 |
{ |
| 66 |
handle = mdlopen(lib, RTLD_LAZY); |
| 67 |
#define DEF_GLX_PROTO(ret, name, args, ...) name = (ret (*) args)real_dlsym(handle, #name); |
| 68 |
#include "glx-reimpl.def" |
| 69 |
#include "glx-dpyredir.def" |
| 70 |
#undef DEF_GLX_PROTO |
| 71 |
#define DEF_GLX_PROTO(ret, name, args, ...) name = (ret (*) args)this->glXGetProcAddress((GLubyte*)#name); |
| 72 |
#include "glxext-reimpl.def" |
| 73 |
#include "gl-passthru.def" |
| 74 |
#include "gl-needed.def" |
| 75 |
#undef DEF_GLX_PROTO |
| 76 |
} |
| 77 |
~CapturedFns() |
| 78 |
{ |
| 79 |
dlclose(handle); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
// Drawable tracking info |
| 84 |
struct DrawableInfo { |
| 85 |
// Only XWindow is not explicitely created via GLX |
| 86 |
enum {XWindow, Window, Pixmap, Pbuffer} kind; |
| 87 |
GLXFBConfig fbconfig; |
| 88 |
GLXPbuffer pbuffer; |
| 89 |
Drawable window; |
| 90 |
int width, height; |
| 91 |
enum ReinitTodo {NONE, RESIZE, SHUTDOWN} reinit; |
| 92 |
GLvoid *pixeldata; |
| 93 |
GLsync sync; |
| 94 |
GLXContext actx; |
| 95 |
|
| 96 |
struct { |
| 97 |
pthread_t worker; |
| 98 |
sem_t acqsem, relsem; |
| 99 |
ReinitTodo reinit; |
| 100 |
|
| 101 |
void spawn_worker(GLXDrawable draw, void* (*work)(void*)) |
| 102 |
{ |
| 103 |
reinit = RESIZE; |
| 104 |
sem_init(&acqsem, 0, 0); |
| 105 |
sem_init(&relsem, 0, 0); |
| 106 |
pthread_create(&worker, NULL, work, (void*)draw); |
| 107 |
} |
| 108 |
void reap_worker() |
| 109 |
{ |
| 110 |
//pthread_cancel(worker); |
| 111 |
pthread_join(worker, NULL); |
| 112 |
sem_destroy(&relsem); |
| 113 |
sem_destroy(&acqsem); |
| 114 |
worker = 0; |
| 115 |
} |
| 116 |
} r, d; |
| 117 |
void reap_workers() |
| 118 |
{ |
| 119 |
if (r.worker) |
| 120 |
{ |
| 121 |
r.reinit = SHUTDOWN; |
| 122 |
sem_post(&r.acqsem); |
| 123 |
sem_wait(&r.relsem); |
| 124 |
r.reap_worker(); |
| 125 |
d.reap_worker(); |
| 126 |
} |
| 127 |
} |
| 128 |
void update_geometry(int width, int height) |
| 129 |
{ |
| 130 |
if (this->width == width && this->height == height) |
| 131 |
return; |
| 132 |
this->width = width; this->height = height; |
| 133 |
__sync_synchronize(); |
| 134 |
reinit = RESIZE; |
| 135 |
} |
| 136 |
~DrawableInfo(); |
| 137 |
}; |
| 138 |
|
| 139 |
struct DrawablesInfo: public std::map<GLXDrawable, DrawableInfo> { |
| 140 |
bool known(GLXDrawable draw) |
| 141 |
{ |
| 142 |
return this->find(draw) != this->end(); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
struct ContextInfo { |
| 147 |
GLXFBConfig fbconfig; |
| 148 |
int sharegroup; |
| 149 |
}; |
| 150 |
|
| 151 |
struct ContextsInfo: public std::map<GLXContext, ContextInfo> { |
| 152 |
void record(GLXContext ctx, GLXFBConfig config, GLXContext share) |
| 153 |
{ |
| 154 |
static int nsharegroups; |
| 155 |
int sharegroup = share ? (*this)[share].sharegroup : nsharegroups++; |
| 156 |
(*this)[ctx] = (ContextInfo){config, sharegroup}; |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
// Shorthand for obtaining compile-time configurable value that can be |
| 161 |
// overridden by environment |
| 162 |
#define getconf(V) (getenv(#V) ? getenv(#V) : V) |
| 163 |
|
| 164 |
// Runs before all other initialization takes place |
| 165 |
struct EarlyInitializer { |
| 166 |
EarlyInitializer(const char **adpy_strp, const char **libgla_strp) |
| 167 |
{ |
| 168 |
// Allow multiple glvnd libGL.so.1 instances to be loaded into a process |
| 169 |
// and OpenGL calls dispatched to them depending on context. |
| 170 |
setenv("__GLVND_DISALLOW_PATCHING", "1", 0); |
| 171 |
#ifdef BUMBLEBEE_SOCKET |
| 172 |
// Signal the Bumblebee daemon to bring up secondary X |
| 173 |
int sock = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 174 |
struct sockaddr_un addr; |
| 175 |
addr.sun_family = AF_UNIX; |
| 176 |
strncpy(addr.sun_path, getconf(BUMBLEBEE_SOCKET), sizeof(addr.sun_path)); |
| 177 |
die_if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0, |
| 178 |
"failed to connect to Bumblebee daemon: %s\n", strerror(errno)); |
| 179 |
static char c[256]; |
| 180 |
if (!getenv("PRIMUS_DISPLAY")) |
| 181 |
{ |
| 182 |
send(sock, "Q VirtualDisplay", strlen("Q VirtualDisplay") + 1, 0); |
| 183 |
recv(sock, &c, 255, 0); |
| 184 |
die_if(memcmp(c, "Value: ", strlen("Value: ")), "unexpected query response\n"); |
| 185 |
*strchrnul(c, '\n') = 0; |
| 186 |
*adpy_strp = strdup(c + 7); |
| 187 |
} |
| 188 |
if (!getenv("PRIMUS_libGLa")) |
| 189 |
{ |
| 190 |
send(sock, "Q LibraryPath", strlen("Q LibraryPath") + 1, 0); |
| 191 |
recv(sock, &c, 255, 0); |
| 192 |
die_if(memcmp(c, "Value: ", strlen("Value: ")), "unexpected query response\n"); |
| 193 |
*strchrnul(c, '\n') = 0; |
| 194 |
int npaths = 0; |
| 195 |
for (char *p = c + 7; *p; npaths++, p = strchrnul(p + 1, ':')); |
| 196 |
if (npaths) |
| 197 |
{ |
| 198 |
char *bblibs = new char[strlen(c + 7) + npaths * strlen("/libGL.so.1") + 1], *b = bblibs, *n, *p; |
| 199 |
for (p = c + 7; *p; p = n) |
| 200 |
{ |
| 201 |
n = strchrnul(p + 1, ':'); |
| 202 |
b += sprintf(b, "%.*s/libGL.so.1", (int)(n - p), p); |
| 203 |
} |
| 204 |
*libgla_strp = bblibs; |
| 205 |
} |
| 206 |
} |
| 207 |
send(sock, "C", 1, 0); |
| 208 |
recv(sock, &c, 255, 0); |
| 209 |
die_if(c[0] == 'N', "Bumblebee daemon reported: %s\n", c + 5); |
| 210 |
die_if(c[0] != 'Y', "failure contacting Bumblebee daemon\n"); |
| 211 |
// the socket will be closed when the application quits, then bumblebee will shut down the secondary X |
| 212 |
#else |
| 213 |
#warning Building without Bumblebee daemon support |
| 214 |
#endif |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
// Process-wide data |
| 219 |
static struct PrimusInfo { |
| 220 |
const char *adpy_str, *libgla_str; |
| 221 |
EarlyInitializer ei; |
| 222 |
// Readback-display synchronization method |
| 223 |
// 0: no sync, 1: D lags behind one frame, 2: fully synced |
| 224 |
int sync; |
| 225 |
// 0: only errors, 1: warnings, 2: profiling |
| 226 |
int loglevel; |
| 227 |
// 0: autodetect, 1: texture, 2: PBO glDrawPixels |
| 228 |
int dispmethod; |
| 229 |
// sleep ratio in readback thread, percent |
| 230 |
int autosleep; |
| 231 |
// The "accelerating" X display |
| 232 |
Display *adpy; |
| 233 |
// The "displaying" X display. The same as the application is using, but |
| 234 |
// primus opens its own connection. |
| 235 |
Display *ddpy; |
| 236 |
CapturedFns afns; |
| 237 |
CapturedFns dfns; |
| 238 |
// FIXME: there are race conditions in accesses to these |
| 239 |
DrawablesInfo drawables; |
| 240 |
ContextsInfo contexts; |
| 241 |
|
| 242 |
PrimusInfo(): |
| 243 |
adpy_str(getconf(PRIMUS_DISPLAY)), |
| 244 |
libgla_str(getconf(PRIMUS_libGLa)), |
| 245 |
ei(&adpy_str, &libgla_str), |
| 246 |
sync(atoi(getconf(PRIMUS_SYNC))), |
| 247 |
loglevel(atoi(getconf(PRIMUS_VERBOSE))), |
| 248 |
dispmethod(atoi(getconf(PRIMUS_UPLOAD))), |
| 249 |
autosleep(atoi(getconf(PRIMUS_SLEEP))), |
| 250 |
adpy(XOpenDisplay(adpy_str)), |
| 251 |
ddpy(XOpenDisplay(NULL)), |
| 252 |
afns(libgla_str), |
| 253 |
dfns(getconf(PRIMUS_libGLd)) |
| 254 |
{ |
| 255 |
die_if(!adpy, "failed to open secondary X display\n"); |
| 256 |
die_if(!ddpy, "failed to open main X display\n"); |
| 257 |
} |
| 258 |
} primus; |
| 259 |
|
| 260 |
// Thread-specific data |
| 261 |
static __thread struct { |
| 262 |
Display *dpy; |
| 263 |
GLXDrawable drawable, read_drawable; |
| 264 |
void make_current(Display *dpy, GLXDrawable draw, GLXDrawable read) |
| 265 |
{ |
| 266 |
this->dpy = dpy; |
| 267 |
this->drawable = draw; |
| 268 |
this->read_drawable = read; |
| 269 |
} |
| 270 |
} tsdata; |
| 271 |
|
| 272 |
// Profiler |
| 273 |
struct Profiler { |
| 274 |
const char *name; |
| 275 |
const char * const *state_names; |
| 276 |
|
| 277 |
double state_time[6], prev_timestamp, print_timestamp; |
| 278 |
int state, nframes, width, height; |
| 279 |
|
| 280 |
static double get_timestamp() |
| 281 |
{ |
| 282 |
struct timespec tp; |
| 283 |
clock_gettime(CLOCK_MONOTONIC, &tp); |
| 284 |
return tp.tv_sec + 1e-9 * tp.tv_nsec; |
| 285 |
} |
| 286 |
|
| 287 |
Profiler(const char *name, const char * const *state_names): |
| 288 |
name(name), |
| 289 |
state_names(state_names), |
| 290 |
state(0), nframes(0), width(0), height(0) |
| 291 |
{ |
| 292 |
memset(state_time, 0, sizeof(state_time)); |
| 293 |
prev_timestamp = print_timestamp = get_timestamp(); |
| 294 |
} |
| 295 |
|
| 296 |
void tick(bool state_reset = false) |
| 297 |
{ |
| 298 |
if (primus.loglevel < 2) |
| 299 |
return; |
| 300 |
double timestamp = get_timestamp(); |
| 301 |
assert(state_reset || state_names[state]); |
| 302 |
if (state_reset) |
| 303 |
state = 0; |
| 304 |
assert(state * sizeof(state_time[0]) < sizeof(state_time)); |
| 305 |
state_time[state++] += timestamp - prev_timestamp; |
| 306 |
prev_timestamp = timestamp; |
| 307 |
if (state_names[state]) |
| 308 |
return; |
| 309 |
nframes++; |
| 310 |
// check if it's time to print again |
| 311 |
double period = timestamp - print_timestamp; // time since we printed |
| 312 |
if (period < 5) |
| 313 |
return; |
| 314 |
// construct output |
| 315 |
char buf[128], *cbuf = buf, *end = buf+128; |
| 316 |
for (int i = 0; i < state; i++) |
| 317 |
cbuf += snprintf(cbuf, end - cbuf, ", %.1f%% %s", 100 * state_time[i] / period, state_names[i]); |
| 318 |
primus_perf("%s: %dx%d, %.1f fps%s\n", name, width, height, nframes / period, buf); |
| 319 |
// start counting again |
| 320 |
print_timestamp = timestamp; |
| 321 |
nframes = 0; |
| 322 |
memset(state_time, 0, sizeof(state_time)); |
| 323 |
} |
| 324 |
}; |
| 325 |
|
| 326 |
// Find out the dimensions of the window |
| 327 |
static void note_geometry(Display *dpy, Drawable draw, int *width, int *height) |
| 328 |
{ |
| 329 |
Window root; |
| 330 |
int x, y; |
| 331 |
unsigned bw, d; |
| 332 |
XGetGeometry(dpy, draw, &root, &x, &y, (unsigned *)width, (unsigned *)height, &bw, &d); |
| 333 |
} |
| 334 |
|
| 335 |
static GLXFBConfig* get_dconfigs(Display *dpy) |
| 336 |
{ |
| 337 |
static const int attrs[] = {GLX_DOUBLEBUFFER, GL_TRUE, None}; |
| 338 |
int ncfg; |
| 339 |
GLXFBConfig *dconfigs = primus.dfns.glXChooseFBConfig(dpy, 0, attrs, &ncfg); |
| 340 |
die_if(!dconfigs, "broken GLX on main X display\n"); |
| 341 |
return dconfigs; |
| 342 |
} |
| 343 |
|
| 344 |
static bool test_drawpixels_fast(Display *dpy, GLXContext ctx, GLXFBConfig dconfig) |
| 345 |
{ |
| 346 |
int width = 1920, height = 1080; |
| 347 |
int pbattrs[] = {GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height, GLX_PRESERVED_CONTENTS, True, None}; |
| 348 |
GLXPbuffer pbuffer = primus.dfns.glXCreatePbuffer(dpy, dconfig, pbattrs); |
| 349 |
primus.dfns.glXMakeCurrent(dpy, pbuffer, ctx); |
| 350 |
GLuint pbo; |
| 351 |
primus.dfns.glGenBuffers(1, &pbo); |
| 352 |
primus.dfns.glBindBuffer(GL_PIXEL_UNPACK_BUFFER_EXT, pbo); |
| 353 |
primus.dfns.glBufferData(GL_PIXEL_UNPACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_DRAW); |
| 354 |
void *pixeldata = malloc(width*height*4); |
| 355 |
|
| 356 |
double end = 0.2 + Profiler::get_timestamp(); |
| 357 |
int iters = 0; |
| 358 |
do { |
| 359 |
primus.dfns.glBufferSubData(GL_PIXEL_UNPACK_BUFFER_EXT, 0, width*height*4, pixeldata); |
| 360 |
primus.dfns.glDrawPixels(width, height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); |
| 361 |
primus.dfns.glXSwapBuffers(dpy, pbuffer); |
| 362 |
iters++; |
| 363 |
} while (end > Profiler::get_timestamp()); |
| 364 |
|
| 365 |
free(pixeldata); |
| 366 |
primus.dfns.glDeleteBuffers(1, &pbo); |
| 367 |
primus.dfns.glXDestroyPbuffer(dpy, pbuffer); |
| 368 |
|
| 369 |
bool is_fast = iters >= 12; |
| 370 |
primus_perf("upload autodetection: will use %s path (%d iters)\n", is_fast ? "PBO" : "texture", iters); |
| 371 |
return is_fast; |
| 372 |
} |
| 373 |
|
| 374 |
static void* display_work(void *vd) |
| 375 |
{ |
| 376 |
GLXDrawable drawable = (GLXDrawable)vd; |
| 377 |
DrawableInfo &di = primus.drawables[drawable]; |
| 378 |
int width, height; |
| 379 |
static const float quad_vertex_coords[] = {-1, -1, -1, 1, 1, 1, 1, -1}; |
| 380 |
static const float quad_texture_coords[] = { 0, 0, 0, 1, 1, 1, 1, 0}; |
| 381 |
GLuint textures[2] = {0}, pbos[2] = {0}; |
| 382 |
int ctex = 0; |
| 383 |
static const char *state_names[] = {"wait", "upload", "draw+swap", NULL}; |
| 384 |
Profiler profiler("display", state_names); |
| 385 |
Display *ddpy = XOpenDisplay(NULL); |
| 386 |
if (!ddpy) // Chromium sandbox prevents opening new connections |
| 387 |
{ |
| 388 |
ddpy = primus.ddpy; |
| 389 |
primus_warn("reusing initial X connection for display thread\n"); |
| 390 |
} |
| 391 |
assert(di.kind == di.XWindow || di.kind == di.Window); |
| 392 |
XSelectInput(ddpy, di.window, StructureNotifyMask); |
| 393 |
note_geometry(ddpy, di.window, &width, &height); |
| 394 |
di.update_geometry(width, height); |
| 395 |
GLXFBConfig *dconfigs = get_dconfigs(ddpy); |
| 396 |
GLXContext context = primus.dfns.glXCreateNewContext(ddpy, *dconfigs, GLX_RGBA_TYPE, NULL, True); |
| 397 |
die_if(!primus.dfns.glXIsDirect(ddpy, context), |
| 398 |
"failed to acquire direct rendering context for display thread\n"); |
| 399 |
if (!primus.dispmethod) |
| 400 |
primus.dispmethod = test_drawpixels_fast(ddpy, context, *dconfigs) ? 2 : 1; |
| 401 |
XFree(dconfigs); |
| 402 |
primus.dfns.glXMakeCurrent(ddpy, di.window, context); |
| 403 |
bool use_textures = (primus.dispmethod == 1); |
| 404 |
if (use_textures) |
| 405 |
{ |
| 406 |
primus.dfns.glVertexPointer (2, GL_FLOAT, 0, quad_vertex_coords); |
| 407 |
primus.dfns.glTexCoordPointer(2, GL_FLOAT, 0, quad_texture_coords); |
| 408 |
primus.dfns.glEnableClientState(GL_VERTEX_ARRAY); |
| 409 |
primus.dfns.glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 410 |
primus.dfns.glGenTextures(2, textures); |
| 411 |
primus.dfns.glEnable(GL_TEXTURE_2D); |
| 412 |
} |
| 413 |
else |
| 414 |
primus.dfns.glGenBuffers(2, pbos); |
| 415 |
for (;;) |
| 416 |
{ |
| 417 |
sem_wait(&di.d.acqsem); |
| 418 |
profiler.tick(true); |
| 419 |
if (di.d.reinit) |
| 420 |
{ |
| 421 |
if (di.d.reinit == di.SHUTDOWN) |
| 422 |
{ |
| 423 |
if (use_textures) |
| 424 |
primus.dfns.glDeleteTextures(2, textures); |
| 425 |
else |
| 426 |
primus.dfns.glDeleteBuffers(2, pbos); |
| 427 |
primus.dfns.glXMakeCurrent(ddpy, 0, NULL); |
| 428 |
primus.dfns.glXDestroyContext(ddpy, context); |
| 429 |
if (ddpy != primus.ddpy) |
| 430 |
XCloseDisplay(ddpy); |
| 431 |
sem_post(&di.d.relsem); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
di.d.reinit = di.NONE; |
| 435 |
profiler.width = width = di.width; |
| 436 |
profiler.height = height = di.height; |
| 437 |
primus.dfns.glViewport(0, 0, width, height); |
| 438 |
if (use_textures) |
| 439 |
{ |
| 440 |
primus.dfns.glBindTexture(GL_TEXTURE_2D, textures[ctex ^ 1]); |
| 441 |
primus.dfns.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 442 |
primus.dfns.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); |
| 443 |
primus.dfns.glBindTexture(GL_TEXTURE_2D, textures[ctex]); |
| 444 |
primus.dfns.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 445 |
primus.dfns.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
primus.dfns.glBindBuffer(GL_PIXEL_UNPACK_BUFFER_EXT, pbos[ctex ^ 1]); |
| 450 |
primus.dfns.glBufferData(GL_PIXEL_UNPACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_DRAW); |
| 451 |
primus.dfns.glBindBuffer(GL_PIXEL_UNPACK_BUFFER_EXT, pbos[ctex]); |
| 452 |
primus.dfns.glBufferData(GL_PIXEL_UNPACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_DRAW); |
| 453 |
} |
| 454 |
sem_post(&di.d.relsem); |
| 455 |
continue; |
| 456 |
} |
| 457 |
if (use_textures) |
| 458 |
primus.dfns.glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, di.pixeldata); |
| 459 |
else |
| 460 |
primus.dfns.glBufferSubData(GL_PIXEL_UNPACK_BUFFER_EXT, 0, width*height*4, di.pixeldata); |
| 461 |
if (!primus.sync) |
| 462 |
sem_post(&di.d.relsem); // Unlock as soon as possible |
| 463 |
profiler.tick(); |
| 464 |
if (use_textures) |
| 465 |
{ |
| 466 |
primus.dfns.glDrawArrays(GL_QUADS, 0, 4); |
| 467 |
primus.dfns.glBindTexture(GL_TEXTURE_2D, textures[ctex ^= 1]); |
| 468 |
} |
| 469 |
else |
| 470 |
{ |
| 471 |
primus.dfns.glDrawPixels(width, height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); |
| 472 |
primus.dfns.glBindBuffer(GL_PIXEL_UNPACK_BUFFER_EXT, pbos[ctex ^= 1]); |
| 473 |
} |
| 474 |
primus.dfns.glXSwapBuffers(ddpy, di.window); |
| 475 |
for (int pending = XPending(ddpy); pending > 0; pending--) |
| 476 |
{ |
| 477 |
XEvent event; |
| 478 |
XNextEvent(ddpy, &event); |
| 479 |
if (event.type == ConfigureNotify) |
| 480 |
di.update_geometry(event.xconfigure.width, event.xconfigure.height); |
| 481 |
} |
| 482 |
if (primus.sync) |
| 483 |
sem_post(&di.d.relsem); // Unlock only after drawing |
| 484 |
profiler.tick(); |
| 485 |
} |
| 486 |
return NULL; |
| 487 |
} |
| 488 |
|
| 489 |
static void* readback_work(void *vd) |
| 490 |
{ |
| 491 |
GLXDrawable drawable = (GLXDrawable)vd; |
| 492 |
DrawableInfo &di = primus.drawables[drawable]; |
| 493 |
int width, height; |
| 494 |
GLuint pbos[2] = {0}; |
| 495 |
int cbuf = 0; |
| 496 |
unsigned sleep_usec = 0; |
| 497 |
static const char *state_names[] = {"app", "sleep", "map", "wait", NULL}; |
| 498 |
Profiler profiler("readback", state_names); |
| 499 |
struct timespec tp; |
| 500 |
if (!primus.sync) |
| 501 |
sem_post(&di.d.relsem); // No PBO is mapped initially |
| 502 |
GLXContext context = primus.afns.glXCreateNewContext(primus.adpy, di.fbconfig, GLX_RGBA_TYPE, di.actx, True); |
| 503 |
die_if(!primus.afns.glXIsDirect(primus.adpy, context), |
| 504 |
"failed to acquire direct rendering context for readback thread\n"); |
| 505 |
primus.afns.glXMakeCurrent(primus.adpy, di.pbuffer, context); |
| 506 |
primus.afns.glGenBuffers(2, &pbos[0]); |
| 507 |
primus.afns.glReadBuffer(GL_FRONT); |
| 508 |
for (;;) |
| 509 |
{ |
| 510 |
sem_wait(&di.r.acqsem); |
| 511 |
profiler.tick(true); |
| 512 |
if (di.r.reinit) |
| 513 |
{ |
| 514 |
clock_gettime(CLOCK_REALTIME, &tp); |
| 515 |
tp.tv_sec += 1; |
| 516 |
// Wait for D worker, if active |
| 517 |
if (!primus.sync && sem_timedwait(&di.d.relsem, &tp)) |
| 518 |
{ |
| 519 |
pthread_cancel(di.d.worker); |
| 520 |
sem_post(&di.d.relsem); // Pretend that D worker completed reinit |
| 521 |
primus_warn("timeout waiting for display worker\n"); |
| 522 |
die_if(di.r.reinit != di.SHUTDOWN, "killed worker on resize\n"); |
| 523 |
} |
| 524 |
di.d.reinit = di.r.reinit; |
| 525 |
sem_post(&di.d.acqsem); // Signal D worker to reinit |
| 526 |
sem_wait(&di.d.relsem); // Wait until reinit was completed |
| 527 |
if (!primus.sync) |
| 528 |
sem_post(&di.d.relsem); // Unlock as no PBO is currently mapped |
| 529 |
if (di.r.reinit == di.SHUTDOWN) |
| 530 |
{ |
| 531 |
primus.afns.glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, pbos[cbuf ^ 1]); |
| 532 |
primus.afns.glUnmapBuffer(GL_PIXEL_PACK_BUFFER_EXT); |
| 533 |
primus.afns.glDeleteBuffers(2, &pbos[0]); |
| 534 |
primus.afns.glXMakeCurrent(primus.adpy, 0, NULL); |
| 535 |
primus.afns.glXDestroyContext(primus.adpy, context); |
| 536 |
sem_post(&di.r.relsem); |
| 537 |
return NULL; |
| 538 |
} |
| 539 |
di.r.reinit = di.NONE; |
| 540 |
profiler.width = width = di.width; |
| 541 |
profiler.height = height = di.height; |
| 542 |
primus.afns.glXMakeCurrent(primus.adpy, di.pbuffer, context); |
| 543 |
primus.afns.glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, pbos[cbuf ^ 1]); |
| 544 |
primus.afns.glBufferData(GL_PIXEL_PACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_READ); |
| 545 |
primus.afns.glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, pbos[cbuf]); |
| 546 |
primus.afns.glBufferData(GL_PIXEL_PACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_READ); |
| 547 |
} |
| 548 |
primus.afns.glWaitSync(di.sync, 0, GL_TIMEOUT_IGNORED); |
| 549 |
primus.afns.glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); |
| 550 |
if (!primus.sync) |
| 551 |
sem_post(&di.r.relsem); // Unblock main thread as soon as possible |
| 552 |
usleep(sleep_usec); |
| 553 |
profiler.tick(); |
| 554 |
if (primus.sync == 1) // Get the previous framebuffer |
| 555 |
primus.afns.glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, pbos[cbuf ^ 1]); |
| 556 |
double map_time = Profiler::get_timestamp(); |
| 557 |
GLvoid *pixeldata = primus.afns.glMapBuffer(GL_PIXEL_PACK_BUFFER_EXT, GL_READ_ONLY); |
| 558 |
map_time = Profiler::get_timestamp() - map_time; |
| 559 |
sleep_usec = (map_time * 1e6 + sleep_usec) * primus.autosleep / 100; |
| 560 |
profiler.tick(); |
| 561 |
clock_gettime(CLOCK_REALTIME, &tp); |
| 562 |
tp.tv_sec += 1; |
| 563 |
if (!primus.sync && sem_timedwait(&di.d.relsem, &tp)) |
| 564 |
primus_warn("dropping a frame to avoid deadlock\n"); |
| 565 |
else |
| 566 |
{ |
| 567 |
di.pixeldata = pixeldata; |
| 568 |
sem_post(&di.d.acqsem); |
| 569 |
if (primus.sync) |
| 570 |
{ |
| 571 |
sem_wait(&di.d.relsem); |
| 572 |
sem_post(&di.r.relsem); // Unblock main thread only after D::work has completed |
| 573 |
} |
| 574 |
cbuf ^= 1; |
| 575 |
primus.afns.glBindBuffer(GL_PIXEL_PACK_BUFFER_EXT, pbos[cbuf]); |
| 576 |
} |
| 577 |
primus.afns.glUnmapBuffer(GL_PIXEL_PACK_BUFFER_EXT); |
| 578 |
profiler.tick(); |
| 579 |
} |
| 580 |
return NULL; |
| 581 |
} |
| 582 |
|
| 583 |
// Find appropriate FBConfigs on adpy for a given Visual on ddpy |
| 584 |
static GLXFBConfig* match_fbconfig(Display *dpy, XVisualInfo *vis) |
| 585 |
{ |
| 586 |
int ncfg, attrs[] = { |
| 587 |
GLX_DOUBLEBUFFER, 0, GLX_STEREO, 0, GLX_AUX_BUFFERS, 0, |
| 588 |
GLX_RED_SIZE, 0, GLX_GREEN_SIZE, 0, GLX_BLUE_SIZE, 0, |
| 589 |
GLX_ALPHA_SIZE, 0, GLX_DEPTH_SIZE, 0, GLX_STENCIL_SIZE, 0, |
| 590 |
GLX_ACCUM_RED_SIZE, 0, GLX_ACCUM_GREEN_SIZE, 0, GLX_ACCUM_BLUE_SIZE, 0, GLX_ACCUM_ALPHA_SIZE, 0, |
| 591 |
GLX_SAMPLE_BUFFERS, 0, GLX_SAMPLES, 0, None |
| 592 |
}; |
| 593 |
for (int i = 0; attrs[i] != None; i += 2) |
| 594 |
primus.dfns.glXGetConfig(dpy, vis, attrs[i], &attrs[i+1]); |
| 595 |
return primus.afns.glXChooseFBConfig(primus.adpy, 0, attrs, &ncfg); |
| 596 |
} |
| 597 |
|
| 598 |
GLXContext glXCreateContext(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct) |
| 599 |
{ |
| 600 |
GLXFBConfig *acfgs = match_fbconfig(dpy, vis); |
| 601 |
GLXContext actx = primus.afns.glXCreateNewContext(primus.adpy, *acfgs, GLX_RGBA_TYPE, shareList, direct); |
| 602 |
primus.contexts.record(actx, *acfgs, shareList); |
| 603 |
return actx; |
| 604 |
} |
| 605 |
|
| 606 |
GLXContext glXCreateNewContext(Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct) |
| 607 |
{ |
| 608 |
GLXContext actx = primus.afns.glXCreateNewContext(primus.adpy, config, renderType, shareList, direct); |
| 609 |
primus.contexts.record(actx, config, shareList); |
| 610 |
return actx; |
| 611 |
} |
| 612 |
|
| 613 |
GLXContext glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config, GLXContext shareList, Bool direct, const int *attrib_list) |
| 614 |
{ |
| 615 |
GLXContext actx = primus.afns.glXCreateContextAttribsARB(primus.adpy, config, shareList, direct, attrib_list); |
| 616 |
primus.contexts.record(actx, config, shareList); |
| 617 |
return actx; |
| 618 |
} |
| 619 |
|
| 620 |
void glXDestroyContext(Display *dpy, GLXContext ctx) |
| 621 |
{ |
| 622 |
primus.contexts.erase(ctx); |
| 623 |
// kludge: reap background tasks when deleting the last context |
| 624 |
// otherwise something will deadlock during unloading the library |
| 625 |
if (primus.contexts.empty()) |
| 626 |
for (DrawablesInfo::iterator i = primus.drawables.begin(); i != primus.drawables.end(); i++) |
| 627 |
i->second.reap_workers(); |
| 628 |
primus.afns.glXDestroyContext(primus.adpy, ctx); |
| 629 |
} |
| 630 |
|
| 631 |
static GLXPbuffer create_pbuffer(DrawableInfo &di) |
| 632 |
{ |
| 633 |
int pbattrs[] = {GLX_PBUFFER_WIDTH, di.width, GLX_PBUFFER_HEIGHT, di.height, GLX_PRESERVED_CONTENTS, True, None}; |
| 634 |
return primus.afns.glXCreatePbuffer(primus.adpy, di.fbconfig, pbattrs); |
| 635 |
} |
| 636 |
|
| 637 |
// Create or recall backing Pbuffer for the drawable |
| 638 |
static GLXPbuffer lookup_pbuffer(Display *dpy, GLXDrawable draw, GLXContext ctx) |
| 639 |
{ |
| 640 |
if (!draw) |
| 641 |
return 0; |
| 642 |
bool known = primus.drawables.known(draw); |
| 643 |
DrawableInfo &di = primus.drawables[draw]; |
| 644 |
if (!known) |
| 645 |
{ |
| 646 |
// Drawable is a plain X Window. Get the FBConfig from the context |
| 647 |
if (ctx) |
| 648 |
di.fbconfig = primus.contexts[ctx].fbconfig; |
| 649 |
else |
| 650 |
{ |
| 651 |
XWindowAttributes attrs; |
| 652 |
die_if(!XGetWindowAttributes(dpy, draw, &attrs), "failed to query attributes"); |
| 653 |
int nvis; |
| 654 |
XVisualInfo tmpl = {0}, *vis; |
| 655 |
tmpl.visualid = XVisualIDFromVisual(attrs.visual); |
| 656 |
die_if(!(vis = XGetVisualInfo(dpy, VisualIDMask, &tmpl, &nvis)), "no visuals"); |
| 657 |
di.fbconfig = *match_fbconfig(dpy, vis); |
| 658 |
XFree(vis); |
| 659 |
} |
| 660 |
di.kind = di.XWindow; |
| 661 |
di.window = draw; |
| 662 |
note_geometry(dpy, draw, &di.width, &di.height); |
| 663 |
} |
| 664 |
else if (ctx && di.fbconfig != primus.contexts[ctx].fbconfig) |
| 665 |
{ |
| 666 |
if (di.pbuffer) |
| 667 |
{ |
| 668 |
primus_warn("recreating incompatible pbuffer\n"); |
| 669 |
di.reap_workers(); |
| 670 |
primus.afns.glXDestroyPbuffer(primus.adpy, di.pbuffer); |
| 671 |
di.pbuffer = 0; |
| 672 |
} |
| 673 |
di.fbconfig = primus.contexts[ctx].fbconfig; |
| 674 |
} |
| 675 |
if (!di.pbuffer) |
| 676 |
di.pbuffer = create_pbuffer(di); |
| 677 |
return di.pbuffer; |
| 678 |
} |
| 679 |
|
| 680 |
Bool glXMakeCurrent(Display *dpy, GLXDrawable drawable, GLXContext ctx) |
| 681 |
{ |
| 682 |
GLXPbuffer pbuffer = lookup_pbuffer(dpy, drawable, ctx); |
| 683 |
tsdata.make_current(dpy, drawable, drawable); |
| 684 |
return primus.afns.glXMakeCurrent(primus.adpy, pbuffer, ctx); |
| 685 |
} |
| 686 |
|
| 687 |
Bool glXMakeContextCurrent(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx) |
| 688 |
{ |
| 689 |
if (draw == read) |
| 690 |
return glXMakeCurrent(dpy, draw, ctx); |
| 691 |
GLXPbuffer pbuffer = lookup_pbuffer(dpy, draw, ctx); |
| 692 |
GLXPbuffer pb_read = lookup_pbuffer(dpy, read, ctx); |
| 693 |
tsdata.make_current(dpy, draw, read); |
| 694 |
return primus.afns.glXMakeContextCurrent(primus.adpy, pbuffer, pb_read, ctx); |
| 695 |
} |
| 696 |
|
| 697 |
void glXSwapBuffers(Display *dpy, GLXDrawable drawable) |
| 698 |
{ |
| 699 |
XFlush(dpy); |
| 700 |
assert(primus.drawables.known(drawable)); |
| 701 |
DrawableInfo &di = primus.drawables[drawable]; |
| 702 |
primus.afns.glXSwapBuffers(primus.adpy, di.pbuffer); |
| 703 |
if (di.kind == di.Pbuffer || di.kind == di.Pixmap) |
| 704 |
return; |
| 705 |
GLXContext ctx = glXGetCurrentContext(); |
| 706 |
if (!ctx) |
| 707 |
primus_warn("glXSwapBuffers: no current context\n"); |
| 708 |
else if (drawable != tsdata.drawable) |
| 709 |
primus_warn("glXSwapBuffers: drawable not current\n"); |
| 710 |
if (di.r.worker && ctx && (!di.actx || primus.contexts[di.actx].sharegroup != primus.contexts[ctx].sharegroup)) |
| 711 |
{ |
| 712 |
primus_warn("glXSwapBuffers: respawning threads after context change\n"); |
| 713 |
di.reap_workers(); |
| 714 |
} |
| 715 |
if (!di.r.worker) |
| 716 |
{ |
| 717 |
// Need to create a sharing context to use GL sync objects |
| 718 |
di.actx = ctx; |
| 719 |
di.d.spawn_worker(drawable, display_work); |
| 720 |
di.r.spawn_worker(drawable, readback_work); |
| 721 |
} |
| 722 |
// Readback thread needs a sync object to avoid reading an incomplete frame |
| 723 |
di.sync = primus.afns.glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 724 |
sem_post(&di.r.acqsem); // Signal the readback worker thread |
| 725 |
sem_wait(&di.r.relsem); // Wait until it has issued glReadBuffer |
| 726 |
primus.afns.glDeleteSync(di.sync); |
| 727 |
if (di.reinit == di.RESIZE) |
| 728 |
{ |
| 729 |
__sync_synchronize(); |
| 730 |
primus.afns.glXDestroyPbuffer(primus.adpy, di.pbuffer); |
| 731 |
di.pbuffer = create_pbuffer(di); |
| 732 |
if (ctx) // FIXME: drawable can be current in other threads |
| 733 |
glXMakeContextCurrent(dpy, tsdata.drawable, tsdata.read_drawable, ctx); |
| 734 |
di.r.reinit = di.reinit; |
| 735 |
di.reinit = di.NONE; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
GLXWindow glXCreateWindow(Display *dpy, GLXFBConfig config, Window win, const int *attribList) |
| 740 |
{ |
| 741 |
GLXFBConfig *dconfigs = get_dconfigs(dpy); |
| 742 |
GLXWindow glxwin = primus.dfns.glXCreateWindow(dpy, *dconfigs, win, attribList); |
| 743 |
XFree(dconfigs); |
| 744 |
DrawableInfo &di = primus.drawables[glxwin]; |
| 745 |
di.kind = di.Window; |
| 746 |
di.fbconfig = config; |
| 747 |
di.window = win; |
| 748 |
note_geometry(dpy, win, &di.width, &di.height); |
| 749 |
return glxwin; |
| 750 |
} |
| 751 |
|
| 752 |
DrawableInfo::~DrawableInfo() |
| 753 |
{ |
| 754 |
reap_workers(); |
| 755 |
if (pbuffer) |
| 756 |
primus.afns.glXDestroyPbuffer(primus.adpy, pbuffer); |
| 757 |
} |
| 758 |
|
| 759 |
void glXDestroyWindow(Display *dpy, GLXWindow window) |
| 760 |
{ |
| 761 |
assert(primus.drawables.known(window)); |
| 762 |
primus.drawables.erase(window); |
| 763 |
primus.dfns.glXDestroyWindow(dpy, window); |
| 764 |
} |
| 765 |
|
| 766 |
GLXPbuffer glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList) |
| 767 |
{ |
| 768 |
GLXFBConfig *dconfigs = get_dconfigs(dpy); |
| 769 |
GLXPbuffer pbuffer = primus.dfns.glXCreatePbuffer(dpy, *dconfigs, attribList); |
| 770 |
XFree(dconfigs); |
| 771 |
DrawableInfo &di = primus.drawables[pbuffer]; |
| 772 |
di.kind = di.Pbuffer; |
| 773 |
di.fbconfig = config; |
| 774 |
for (int i = 0; attribList[i] != None; i++) |
| 775 |
if (attribList[i] == GLX_PBUFFER_WIDTH) |
| 776 |
di.width = attribList[i+1]; |
| 777 |
else if (attribList[i] == GLX_PBUFFER_HEIGHT) |
| 778 |
di.height = attribList[i+1]; |
| 779 |
return pbuffer; |
| 780 |
} |
| 781 |
|
| 782 |
void glXDestroyPbuffer(Display *dpy, GLXPbuffer pbuf) |
| 783 |
{ |
| 784 |
assert(primus.drawables.known(pbuf)); |
| 785 |
primus.drawables.erase(pbuf); |
| 786 |
primus.dfns.glXDestroyPbuffer(dpy, pbuf); |
| 787 |
} |
| 788 |
|
| 789 |
GLXPixmap glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attribList) |
| 790 |
{ |
| 791 |
GLXFBConfig *dconfigs = get_dconfigs(dpy); |
| 792 |
GLXPixmap glxpix = primus.dfns.glXCreatePixmap(dpy, *dconfigs, pixmap, attribList); |
| 793 |
XFree(dconfigs); |
| 794 |
DrawableInfo &di = primus.drawables[glxpix]; |
| 795 |
di.kind = di.Pixmap; |
| 796 |
di.fbconfig = config; |
| 797 |
note_geometry(dpy, pixmap, &di.width, &di.height); |
| 798 |
return glxpix; |
| 799 |
} |
| 800 |
|
| 801 |
void glXDestroyPixmap(Display *dpy, GLXPixmap pixmap) |
| 802 |
{ |
| 803 |
assert(primus.drawables.known(pixmap)); |
| 804 |
primus.drawables.erase(pixmap); |
| 805 |
primus.dfns.glXDestroyPixmap(dpy, pixmap); |
| 806 |
} |
| 807 |
|
| 808 |
GLXPixmap glXCreateGLXPixmap(Display *dpy, XVisualInfo *visual, Pixmap pixmap) |
| 809 |
{ |
| 810 |
GLXPixmap glxpix = primus.dfns.glXCreateGLXPixmap(dpy, visual, pixmap); |
| 811 |
DrawableInfo &di = primus.drawables[glxpix]; |
| 812 |
di.kind = di.Pixmap; |
| 813 |
note_geometry(dpy, pixmap, &di.width, &di.height); |
| 814 |
GLXFBConfig *acfgs = match_fbconfig(dpy, visual); |
| 815 |
di.fbconfig = *acfgs; |
| 816 |
return glxpix; |
| 817 |
} |
| 818 |
|
| 819 |
void glXDestroyGLXPixmap(Display *dpy, GLXPixmap pixmap) |
| 820 |
{ |
| 821 |
glXDestroyPixmap(dpy, pixmap); |
| 822 |
} |
| 823 |
|
| 824 |
static XVisualInfo *match_visual(Display *dpy, int attrs[]) |
| 825 |
{ |
| 826 |
XVisualInfo *vis = glXChooseVisual(dpy, 0, attrs); |
| 827 |
for (int i = 2; attrs[i] != None && vis; i += 2) |
| 828 |
{ |
| 829 |
int tmp = attrs[i+1]; |
| 830 |
primus.dfns.glXGetConfig(dpy, vis, attrs[i], &attrs[i+1]); |
| 831 |
if (tmp != attrs[i+1]) |
| 832 |
vis = NULL; |
| 833 |
} |
| 834 |
return vis; |
| 835 |
} |
| 836 |
|
| 837 |
XVisualInfo *glXGetVisualFromFBConfig(Display *dpy, GLXFBConfig config) |
| 838 |
{ |
| 839 |
if (!primus.afns.glXGetVisualFromFBConfig(primus.adpy, config)) |
| 840 |
return NULL; |
| 841 |
int i, attrs[] = { |
| 842 |
GLX_RGBA, GLX_DOUBLEBUFFER, |
| 843 |
GLX_RED_SIZE, 0, GLX_GREEN_SIZE, 0, GLX_BLUE_SIZE, 0, |
| 844 |
GLX_ALPHA_SIZE, 0, GLX_DEPTH_SIZE, 0, GLX_STENCIL_SIZE, 0, |
| 845 |
GLX_SAMPLE_BUFFERS, 0, GLX_SAMPLES, 0, None |
| 846 |
}; |
| 847 |
for (i = 2; attrs[i] != None; i += 2) |
| 848 |
primus.afns.glXGetFBConfigAttrib(primus.adpy, config, attrs[i], &attrs[i+1]); |
| 849 |
XVisualInfo *vis = NULL; |
| 850 |
for (i -= 2; i >= 0 && !vis; i -= 2) |
| 851 |
{ |
| 852 |
vis = match_visual(dpy, attrs); |
| 853 |
attrs[i] = None; |
| 854 |
} |
| 855 |
return vis; |
| 856 |
} |
| 857 |
|
| 858 |
int glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config, int attribute, int *value) |
| 859 |
{ |
| 860 |
int r = primus.afns.glXGetFBConfigAttrib(primus.adpy, config, attribute, value); |
| 861 |
if (attribute == GLX_VISUAL_ID && *value) |
| 862 |
return primus.dfns.glXGetConfig(dpy, glXGetVisualFromFBConfig(dpy, config), attribute, value); |
| 863 |
return r; |
| 864 |
} |
| 865 |
|
| 866 |
void glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute, unsigned int *value) |
| 867 |
{ |
| 868 |
primus.afns.glXQueryDrawable(primus.adpy, lookup_pbuffer(dpy, draw, NULL), attribute, value); |
| 869 |
} |
| 870 |
|
| 871 |
void glXUseXFont(Font font, int first, int count, int list) |
| 872 |
{ |
| 873 |
unsigned long prop; |
| 874 |
Display *dpy = glXGetCurrentDisplay(); |
| 875 |
XFontStruct *fs = XQueryFont(dpy, font); |
| 876 |
XGetFontProperty(fs, XA_FONT, &prop); |
| 877 |
char *xlfd = XGetAtomName(dpy, prop); |
| 878 |
Font afont = XLoadFont(primus.adpy, xlfd); |
| 879 |
primus.afns.glXUseXFont(afont, first, count, list); |
| 880 |
XUnloadFont(primus.adpy, afont); |
| 881 |
XFree(xlfd); |
| 882 |
XFreeFontInfo(NULL, fs, 1); |
| 883 |
} |
| 884 |
|
| 885 |
GLXContext glXGetCurrentContext(void) |
| 886 |
{ |
| 887 |
return primus.afns.glXGetCurrentContext(); |
| 888 |
} |
| 889 |
|
| 890 |
GLXDrawable glXGetCurrentDrawable(void) |
| 891 |
{ |
| 892 |
return tsdata.drawable; |
| 893 |
} |
| 894 |
|
| 895 |
void glXWaitGL(void) |
| 896 |
{ |
| 897 |
} |
| 898 |
|
| 899 |
void glXWaitX(void) |
| 900 |
{ |
| 901 |
} |
| 902 |
|
| 903 |
Display *glXGetCurrentDisplay(void) |
| 904 |
{ |
| 905 |
return tsdata.dpy; |
| 906 |
} |
| 907 |
|
| 908 |
GLXDrawable glXGetCurrentReadDrawable(void) |
| 909 |
{ |
| 910 |
return tsdata.read_drawable; |
| 911 |
} |
| 912 |
|
| 913 |
// Application sees ddpy-side Visuals, but adpy-side FBConfigs and Contexts |
| 914 |
XVisualInfo* glXChooseVisual(Display *dpy, int screen, int *attribList) |
| 915 |
{ |
| 916 |
return primus.dfns.glXChooseVisual(dpy, screen, attribList); |
| 917 |
} |
| 918 |
|
| 919 |
int glXGetConfig(Display *dpy, XVisualInfo *visual, int attrib, int *value) |
| 920 |
{ |
| 921 |
return primus.dfns.glXGetConfig(dpy, visual, attrib, value); |
| 922 |
} |
| 923 |
|
| 924 |
// GLX forwarders that reroute to adpy |
| 925 |
#define DEF_GLX_PROTO(ret, name, par, ...) \ |
| 926 |
ret name par \ |
| 927 |
{ return primus.afns.name(primus.adpy, __VA_ARGS__); } |
| 928 |
#include "glx-dpyredir.def" |
| 929 |
#undef DEF_GLX_PROTO |
| 930 |
|
| 931 |
// OpenGL forwarders |
| 932 |
#define DEF_GLX_PROTO(ret, name, par, ...) \ |
| 933 |
void ifunc_##name(void) asm(#name) __attribute__((visibility("default"),ifunc("i" #name))); \ |
| 934 |
extern "C" { \ |
| 935 |
static ret l##name par \ |
| 936 |
{ return primus.afns.name(__VA_ARGS__); } \ |
| 937 |
static void *i##name(void) \ |
| 938 |
{ return primus.afns.handle ? real_dlsym(primus.afns.handle, #name) : (void*)l##name; } } |
| 939 |
#include "gl-passthru.def" |
| 940 |
#undef DEF_GLX_PROTO |
| 941 |
|
| 942 |
// GLX extensions |
| 943 |
|
| 944 |
int glXSwapIntervalSGI(int interval) |
| 945 |
{ |
| 946 |
return 1; // Indicate failure to set swapinterval |
| 947 |
} |
| 948 |
|
| 949 |
__GLXextFuncPtr glXGetProcAddress(const GLubyte *procName) |
| 950 |
{ |
| 951 |
static const char * const redefined_names[] = { |
| 952 |
#define DEF_GLX_PROTO(ret, name, args, ...) #name, |
| 953 |
#include "glx-reimpl.def" |
| 954 |
#include "glxext-reimpl.def" |
| 955 |
#include "glx-dpyredir.def" |
| 956 |
#undef DEF_GLX_PROTO |
| 957 |
}; |
| 958 |
static const __GLXextFuncPtr redefined_fns[] = { |
| 959 |
#define DEF_GLX_PROTO(ret, name, args, ...) (__GLXextFuncPtr)name, |
| 960 |
#include "glx-reimpl.def" |
| 961 |
#include "glxext-reimpl.def" |
| 962 |
#include "glx-dpyredir.def" |
| 963 |
#undef DEF_GLX_PROTO |
| 964 |
}; |
| 965 |
enum {n_redefined = sizeof(redefined_fns) / sizeof(redefined_fns[0])}; |
| 966 |
// Non-GLX functions are forwarded to the accelerating libGL |
| 967 |
if (memcmp(procName, "glX", 3)) |
| 968 |
return primus.afns.glXGetProcAddress(procName); |
| 969 |
// All GLX functions are either implemented in primus or not available |
| 970 |
for (int i = 0; i < n_redefined; i++) |
| 971 |
if (!strcmp((const char *)procName, redefined_names[i])) |
| 972 |
return redefined_fns[i]; |
| 973 |
return NULL; |
| 974 |
} |
| 975 |
|
| 976 |
__GLXextFuncPtr glXGetProcAddressARB(const GLubyte *procName) |
| 977 |
{ |
| 978 |
return glXGetProcAddress(procName); |
| 979 |
} |
| 980 |
|
| 981 |
static const char glxext_clientside[] = "GLX_ARB_get_proc_address "; |
| 982 |
static const char glxext_adpy[] = "GLX_ARB_create_context GLX_ARB_create_context_profile "; |
| 983 |
static const char glxext_ddpy[] = ""; |
| 984 |
|
| 985 |
const char *glXGetClientString(Display *dpy, int name) |
| 986 |
{ |
| 987 |
static std::string exts(std::string(glxext_clientside) + glxext_adpy + glxext_ddpy); |
| 988 |
switch (name) |
| 989 |
{ |
| 990 |
case GLX_VENDOR: return "primus"; |
| 991 |
case GLX_VERSION: return "1.4"; |
| 992 |
case GLX_EXTENSIONS: return exts.c_str(); |
| 993 |
default: return NULL; |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
static std::string intersect_exts(const char *set1, const char *set2) |
| 998 |
{ |
| 999 |
std::string r; |
| 1000 |
for (const char *p; *set1; set1 = p + 1) |
| 1001 |
{ |
| 1002 |
p = strchr(set1, ' '); |
| 1003 |
if (memmem(set2, strlen(set2), set1, p - set1)) |
| 1004 |
r.append(set1, p - set1 + 1); |
| 1005 |
} |
| 1006 |
return r; |
| 1007 |
} |
| 1008 |
|
| 1009 |
const char *glXQueryExtensionsString(Display *dpy, int screen) |
| 1010 |
{ |
| 1011 |
static std::string exts |
| 1012 |
(std::string(glxext_clientside) |
| 1013 |
+ intersect_exts(glxext_adpy, primus.afns.glXQueryExtensionsString(primus.adpy, 0)) |
| 1014 |
+ intersect_exts(glxext_ddpy, primus.dfns.glXQueryExtensionsString(dpy, 0))); |
| 1015 |
return exts.c_str(); |
| 1016 |
} |
| 1017 |
|
| 1018 |
// OpenGL ABI specifies that anything above OpenGL 1.2 + ARB_multitexture must |
| 1019 |
// be obtained via glXGetProcAddress, but some applications link against |
| 1020 |
// extension functions, and Mesa and vendor libraries let them |
| 1021 |
#ifndef PRIMUS_STRICT |
| 1022 |
#warning Enabled workarounds for applications demanding more than promised by the OpenGL ABI |
| 1023 |
|
| 1024 |
// OpenGL extension forwarders |
| 1025 |
#define P(name) \ |
| 1026 |
void ifunc_##name(void) asm(#name) __attribute__((visibility("default"),ifunc("i" #name))); \ |
| 1027 |
extern "C" { static void *i##name(void) \ |
| 1028 |
{ return primus.afns.handle ? real_dlsym(primus.afns.handle, #name) : NULL; } } |
| 1029 |
#include "glext-passthru.def" |
| 1030 |
#undef P |
| 1031 |
#endif |