summaryrefslogtreecommitdiff
path: root/x11-dri2.c
blob: 0c6b4dd344aae00f4a58ccec4ead0d0642d8f1b0 (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
/*
 * Copyright © 2008 Kristian Høgsberg
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, 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.
 */

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <xf86drm.h>
#include "libdri2.h"

#include "eagle-internal.h"

typedef struct EGLDisplayX11 *EGLDisplayX11;
struct EGLDisplayX11 {
	struct EGLDisplay base;
	int eventBase;
	int errorBase;
	Window root;
	Display *display;
};

typedef struct EGLSurfaceX11 *EGLSurfaceX11;
struct EGLSurfaceX11 {
	struct EGLSurface	base;
	Drawable		drawable;
};

static void
x11GetBuffers(EGLSurface surface, uint32_t *attachments, int count)
{
	EGLDisplayX11 x11Display = (EGLDisplayX11) surface->display;
	EGLSurfaceX11 x11Surface = (EGLSurfaceX11) surface;
	DRI2Buffer *buffers;
	int i, outCount;

	buffers = DRI2GetBuffers(x11Display->display,
				 x11Surface->drawable,
				 &surface->width,
				 &surface->height,
				 attachments, count, &outCount);

	for (i = 0; i < outCount; i++) {
		surface->buffers[i].attachment = buffers[i].attachment;
		surface->buffers[i].name = buffers[i].name;
		surface->buffers[i].pitch = buffers[i].pitch;
		surface->buffers[i].cpp = buffers[i].cpp;
	}

	surface->count = outCount;

	XFree(buffers);
}

static EGLBoolean
x11SwapBuffers(EGLDisplay display, EGLSurface surface)
{
	EGLDisplayX11 x11Display = (EGLDisplayX11) display;
	EGLSurfaceX11 x11Surface = (EGLSurfaceX11) surface;
	XserverRegion region;
	XRectangle rect;

	rect.x = 0;
	rect.y = 0;
	rect.width = surface->width;
	rect.height = surface->height;
	region = XFixesCreateRegion(x11Display->display, &rect, 1);
	DRI2CopyRegion(x11Display->display, x11Surface->drawable,
		       region, DRI2BufferBackLeft, DRI2BufferFrontLeft);
	XFixesDestroyRegion(x11Display->display, region);

	return EGL_TRUE;
}

static EGLBoolean
x11DestroySurface(EGLDisplay display, EGLSurface surface)
{
	EGLDisplayX11 x11Display = (EGLDisplayX11) display;
	EGLSurfaceX11 x11Surface = (EGLSurfaceX11) surface;

	DRI2DestroyDrawable(x11Display->display, x11Surface->drawable);

	return EGL_TRUE;
}

static const struct EagleBackend x11Backend = {
	x11GetBuffers,
	x11SwapBuffers,
	x11DestroySurface
};

EGLDisplay
eglCreateDisplayX11(Display *display, Window root)
{
	EGLDisplayX11 x11Display;
	int eventBase, errorBase;
	int major, minor;
	drm_magic_t magic;
	char *driverName, *deviceName;

	if (!DRI2QueryExtension(display, &eventBase, &errorBase))
		return NULL;

	if (!DRI2QueryVersion(display, &major, &minor) || major < 1)
		return NULL;

	if (!DRI2Connect(display, root, &driverName, &deviceName))
		return NULL;

	x11Display = malloc(sizeof *x11Display);
	if (x11Display == NULL)
		return NULL;
	
	x11Display->eventBase = eventBase;
	x11Display->errorBase = errorBase;
	x11Display->display = display;

	/* Get ude*/
	if (eglInitDisplay(&x11Display->base, NULL, driverName) < 0) {
		free(x11Display);
		return NULL;
	}

	if (drmGetMagic(x11Display->base.fd, &magic)) {
		free(x11Display);
		return NULL;
	}

	DRI2Authenticate(display, root, magic);

	x11Display->base.backend = &x11Backend;

	return &x11Display->base;
}

EGLSurface
eglCreateSurfaceX11(EGLDisplay display, EGLConfig config,
		    Window window, int width, int height)
{
	EGLSurfaceX11 x11Surface;

	x11Surface = malloc(sizeof *x11Surface);
	if (x11Surface == NULL)
		return NULL;

	x11Surface->drawable = window;

	eglInitSurface(&x11Surface->base, display, config, width, height);

	return &x11Surface->base;
}