aboutsummaryrefslogtreecommitdiff
path: root/pugl/pugl/detail/x11_gl.c
blob: 85c18b4876308e9956c529180288e74399796622 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
  Copyright 2012-2019 David Robillard <http://drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/**
   @file x11_gl.c OpenGL graphics backend for X11.
*/

#include "pugl/detail/types.h"
#include "pugl/detail/x11.h"
#include "pugl/pugl.h"
#include "pugl/pugl_gl_backend.h"

#include <GL/gl.h>
#include <GL/glx.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
	GLXFBConfig fb_config;
	GLXContext  ctx;
	int         double_buffered;
} PuglX11GlSurface;

static int
puglX11GlHintValue(const int value)
{
	return value == PUGL_DONT_CARE ? (int)GLX_DONT_CARE : value;
}

static int
puglX11GlGetAttrib(Display* const    display,
                   const GLXFBConfig fb_config,
                   const int         attrib)
{
	int value = 0;
	glXGetFBConfigAttrib(display, fb_config, attrib, &value);
	return value;
}

static int
puglX11GlConfigure(PuglView* view)
{
	PuglInternals* const impl    = view->impl;
	const int            screen  = impl->screen;
	Display* const       display = impl->display;

	PuglX11GlSurface* const surface =
		(PuglX11GlSurface*)calloc(1, sizeof(PuglX11GlSurface));
	impl->surface = surface;

	const int attrs[] = {
		GLX_X_RENDERABLE,  True,
		GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
		GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
		GLX_RENDER_TYPE,   GLX_RGBA_BIT,
		GLX_SAMPLES,       view->hints.samples,
		GLX_RED_SIZE,      puglX11GlHintValue(view->hints.red_bits),
		GLX_GREEN_SIZE,    puglX11GlHintValue(view->hints.green_bits),
		GLX_BLUE_SIZE,     puglX11GlHintValue(view->hints.blue_bits),
		GLX_ALPHA_SIZE,    puglX11GlHintValue(view->hints.alpha_bits),
		GLX_DEPTH_SIZE,    puglX11GlHintValue(view->hints.depth_bits),
		GLX_STENCIL_SIZE,  puglX11GlHintValue(view->hints.stencil_bits),
		GLX_DOUBLEBUFFER,  puglX11GlHintValue(view->hints.double_buffer),
		None
	};

	int          n_fbc = 0;
	GLXFBConfig* fbc   = glXChooseFBConfig(display, screen, attrs, &n_fbc);
	if (n_fbc <= 0) {
		fprintf(stderr, "error: Failed to create GL context\n");
		return 1;
	}

	surface->fb_config = fbc[0];
	impl->vi           = glXGetVisualFromFBConfig(impl->display, fbc[0]);

	printf("Using visual 0x%lX: R=%d G=%d B=%d A=%d D=%d"
	       " DOUBLE=%d SAMPLES=%d\n",
	       impl->vi->visualid,
	       puglX11GlGetAttrib(display, fbc[0], GLX_RED_SIZE),
	       puglX11GlGetAttrib(display, fbc[0], GLX_GREEN_SIZE),
	       puglX11GlGetAttrib(display, fbc[0], GLX_BLUE_SIZE),
	       puglX11GlGetAttrib(display, fbc[0], GLX_ALPHA_SIZE),
	       puglX11GlGetAttrib(display, fbc[0], GLX_DEPTH_SIZE),
	       puglX11GlGetAttrib(display, fbc[0], GLX_DOUBLEBUFFER),
	       puglX11GlGetAttrib(display, fbc[0], GLX_SAMPLES));

	XFree(fbc);

	return 0;
}

static int
puglX11GlCreate(PuglView* view)
{
	PuglInternals* const    impl      = view->impl;
	PuglX11GlSurface* const surface   = (PuglX11GlSurface*)impl->surface;
	Display* const          display   = impl->display;
	const GLXFBConfig       fb_config = surface->fb_config;

	const int ctx_attrs[] = {
		GLX_CONTEXT_MAJOR_VERSION_ARB, view->hints.context_version_major,
		GLX_CONTEXT_MINOR_VERSION_ARB, view->hints.context_version_minor,
		GLX_CONTEXT_PROFILE_MASK_ARB, (view->hints.use_compat_profile
		                               ? GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
		                               : GLX_CONTEXT_CORE_PROFILE_BIT_ARB),
		0};

	typedef GLXContext (*CreateContextAttribs)(
		Display*, GLXFBConfig, GLXContext, Bool, const int*);

	CreateContextAttribs create_context =
		(CreateContextAttribs)glXGetProcAddress(
			(const GLubyte*)"glXCreateContextAttribsARB");

	impl->surface = surface;
	surface->ctx  = create_context(display, fb_config, 0, GL_TRUE, ctx_attrs);
	if (!surface->ctx) {
		surface->ctx =
			glXCreateNewContext(display, fb_config, GLX_RGBA_TYPE, 0, True);
	}

	glXGetConfig(impl->display,
	             impl->vi,
	             GLX_DOUBLEBUFFER,
	             &surface->double_buffered);

	return 0;
}

static int
puglX11GlDestroy(PuglView* view)
{
	PuglX11GlSurface* surface = (PuglX11GlSurface*)view->impl->surface;
	if (surface) {
		glXDestroyContext(view->impl->display, surface->ctx);
		free(surface);
		view->impl->surface = NULL;
	}
	return 0;
}

static int
puglX11GlEnter(PuglView* view, bool PUGL_UNUSED(drawing))
{
	PuglX11GlSurface* surface = (PuglX11GlSurface*)view->impl->surface;
	glXMakeCurrent(view->impl->display, view->impl->win, surface->ctx);
	return 0;
}

static int
puglX11GlLeave(PuglView* view, bool drawing)
{
	PuglX11GlSurface* surface = (PuglX11GlSurface*)view->impl->surface;

	if (drawing && surface->double_buffered) {
		glXSwapBuffers(view->impl->display, view->impl->win);
	} else if (drawing) {
		glFlush();
	}

	glXMakeCurrent(view->impl->display, None, NULL);

	return 0;
}

static int
puglX11GlResize(PuglView* PUGL_UNUSED(view),
                int       PUGL_UNUSED(width),
                int       PUGL_UNUSED(height))
{
	return 0;
}

static void*
puglX11GlGetContext(PuglView* PUGL_UNUSED(view))
{
	return NULL;
}

PuglGlFunc
puglGetProcAddress(const char* name)
{
	return glXGetProcAddress((const GLubyte*)name);
}

const PuglBackend* puglGlBackend(void)
{
	static const PuglBackend backend = {
		puglX11GlConfigure,
		puglX11GlCreate,
		puglX11GlDestroy,
		puglX11GlEnter,
		puglX11GlLeave,
		puglX11GlResize,
		puglX11GlGetContext
	};

	return &backend;
}