summaryrefslogtreecommitdiff
path: root/test/dri2test.c
blob: e7b3b83b7cd2302ab02a91aceb7136991dd914d4 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright © 2011 Texas Instruments, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Soft-
 * ware"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, provided that the above copyright
 * notice(s) and this permission notice appear in all copies of the Soft-
 * ware and that both the above copyright notice(s) and this permission
 * notice appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
 * QUENTIAL 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 PERFOR-
 * MANCE OF THIS SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder shall
 * not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization of
 * the copyright holder.
 *
 * Authors:
 *   Rob Clark (rob@ti.com)
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "dri2test.h"

static Bool WireToEvent(Display *dpy, XExtDisplayInfo *info,
		XEvent *event, xEvent *wire)
{
	switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {

	case DRI2_BufferSwapComplete:
	{
//		xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
		MSG("BufferSwapComplete");
		return True;
	}
	case DRI2_InvalidateBuffers:
	{
//		xDRI2InvalidateBuffers *awire = (xDRI2InvalidateBuffers *)wire;
		MSG("InvalidateBuffers");
//		dri2InvalidateBuffers(dpy, awire->drawable);
		return False;
	}
	default:
		/* client doesn't support server event */
		break;
	}

	return False;
}

static Status EventToWire(Display *dpy, XExtDisplayInfo *info,
		XEvent *event, xEvent *wire)
{
   switch (event->type) {
   default:
      /* client doesn't support server event */
      break;
   }

   return Success;
}

static const DRI2EventOps ops = {
		.WireToEvent = WireToEvent,
		.EventToWire = EventToWire,
};

static int dri2_connect(Display *dpy, char **driver)
{
	int eventBase, errorBase, major, minor;
	char *device;
	drm_magic_t magic;
	Window root;
	int fd;

	if (!DRI2InitDisplay(dpy, &ops)) {
		ERROR_MSG("DRI2InitDisplay failed");
		return -1;
	}

	if (!DRI2QueryExtension(dpy, &eventBase, &errorBase)) {
		ERROR_MSG("DRI2QueryExtension failed");
		return -1;
	}

	MSG("DRI2QueryExtension: eventBase=%d, errorBase=%d", eventBase, errorBase);

	if (!DRI2QueryVersion(dpy, &major, &minor)) {
		ERROR_MSG("DRI2QueryVersion failed");
		return -1;
	}

	MSG("DRI2QueryVersion: major=%d, minor=%d", major, minor);

	root = RootWindow(dpy, DefaultScreen(dpy));

	if (!DRI2Connect(dpy, root, driver, &device)) {
		ERROR_MSG("DRI2Connect failed");
		return -1;
	}

	MSG("DRI2Connect: driver=%s, device=%s", *driver, device);

	fd = open(device, O_RDWR);
	if (fd < 0) {
		ERROR_MSG("open failed");
		return fd;
	}

	if (drmGetMagic(fd, &magic)) {
		ERROR_MSG("drmGetMagic failed");
		return -1;
	}

	if (!DRI2Authenticate(dpy, root, magic)) {
		ERROR_MSG("DRI2Authenticate failed");
		return -1;
	}

	return fd;
}

#ifdef HAVE_NOUVEAU
extern Backend nouveau_backend;
#endif

#ifdef HAVE_OMAP
extern Backend omap_backend;
#endif

/* stolen from modetest.c */
static void fill(char *virtual, int n, int width, int height, int stride)
{
	int i, j;
    /* paint the buffer with colored tiles */
    for (j = 0; j < height; j++) {
            uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
            for (i = 0; i < width; i++) {
                    div_t d = div(n+i, width);
                    fb_ptr[i] =
                            0x00130502 * (d.quot >> 6) +
                            0x000a1120 * (d.rem >> 6);
            }
    }
}


int main(int argc, char **argv)
{
	static unsigned attachments[] = {
			DRI2BufferFrontLeft,
			DRI2BufferBackLeft,
	};
	Display *dpy;
	Window win;
	Backend *backend = NULL;
	DRI2Buffer *dri2bufs;
	Buffer *bufs;
	char *driver;
	int fd, nbufs, i, w, h;

	dpy = XOpenDisplay(NULL);
	win = XCreateSimpleWindow(dpy, RootWindow(dpy, 0),
			1, 1, WIDTH, HEIGHT, 0, BlackPixel (dpy, 0), BlackPixel(dpy, 0));
	XMapWindow(dpy, win);
	XFlush(dpy);

	if ((fd = dri2_connect(dpy, &driver)) < 0) {
		return -1;
	}

#ifdef HAVE_NOUVEAU
	if (!strcmp(driver, "nouveau")) {
		backend = &nouveau_backend;
	}
#endif

#ifdef HAVE_OMAP
	if (!strcmp(driver, "omap")) {
		backend = &omap_backend;
	}
#endif

	if (!backend) {
		ERROR_MSG("no suitable backend DRM driver found");
		return -1;
	}

	backend->setup(fd);

	DRI2CreateDrawable(dpy, win);

	dri2bufs = DRI2GetBuffers(dpy, win, &w, &h, attachments, 2, &nbufs);
	if (!dri2bufs) {
		ERROR_MSG("DRI2GetBuffers failed");
		return -1;
	}

	MSG("DRI2GetBuffers: w=%d, h=%d, nbufs=%d", w, h, nbufs);

	bufs = calloc(nbufs, sizeof(Buffer));

	for (i = 0; i < nbufs; i++) {
		bufs[i].dri2buf = &dri2bufs[i];
		bufs[i].hdl = backend->init(bufs[i].dri2buf);
	}

	for (i = 0; i < NFRAMES; i++) {
		CARD64 count;

		char *buf = backend->prep(bufs[i % nbufs].hdl);
		fill(buf, i, w, h, bufs[i % nbufs].dri2buf->pitch);
		backend->fini(bufs[i % nbufs].hdl);
		DRI2SwapBuffers(dpy, win, 0, 0, 0, &count);
		MSG("DRI2SwapBuffers: count=%lu", count);
		if (i > 0) {
			/* XXX wait.. */
		}
	}

	return 0;
}