radeon/r200: drop dirty state from texture object + pp_txoffset
[mesa.git] / src / glx / x11 / dri2_glx.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #ifdef GLX_DIRECT_RENDERING
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/extensions/Xdamage.h>
38 #include "glxclient.h"
39 #include "glcontextmodes.h"
40 #include "xf86dri.h"
41 #include <dlfcn.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include "xf86drm.h"
47 #include "dri2.h"
48 #include "dri_common.h"
49
50 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
51 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
52 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
53
54 struct __GLXDRIdisplayPrivateRec {
55 __GLXDRIdisplay base;
56
57 /*
58 ** XFree86-DRI version information
59 */
60 int driMajor;
61 int driMinor;
62 int driPatch;
63
64 unsigned long configureSeqno;
65 Bool (*oldConfigProc)(Display *, XEvent *, xEvent *);
66 };
67
68 struct __GLXDRIcontextPrivateRec {
69 __GLXDRIcontext base;
70 __DRIcontext *driContext;
71 __GLXscreenConfigs *psc;
72 };
73
74 struct __GLXDRIdrawablePrivateRec {
75 __GLXDRIdrawable base;
76 __DRIbuffer buffers[5];
77 int bufferCount;
78 int width, height;
79 unsigned long configureSeqno;
80 };
81
82 static void dri2DestroyContext(__GLXDRIcontext *context,
83 __GLXscreenConfigs *psc, Display *dpy)
84 {
85 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
86 const __DRIcoreExtension *core = pcp->psc->core;
87
88 (*core->destroyContext)(pcp->driContext);
89
90 Xfree(pcp);
91 }
92
93 static Bool dri2BindContext(__GLXDRIcontext *context,
94 __GLXDRIdrawable *draw, __GLXDRIdrawable *read)
95 {
96 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
97 const __DRIcoreExtension *core = pcp->psc->core;
98
99 return (*core->bindContext)(pcp->driContext,
100 draw->driDrawable,
101 read->driDrawable);
102 }
103
104 static void dri2UnbindContext(__GLXDRIcontext *context)
105 {
106 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
107 const __DRIcoreExtension *core = pcp->psc->core;
108
109 (*core->unbindContext)(pcp->driContext);
110 }
111
112 static __GLXDRIcontext *dri2CreateContext(__GLXscreenConfigs *psc,
113 const __GLcontextModes *mode,
114 GLXContext gc,
115 GLXContext shareList, int renderType)
116 {
117 __GLXDRIcontextPrivate *pcp, *pcp_shared;
118 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
119 __DRIcontext *shared = NULL;
120
121 if (shareList) {
122 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
123 shared = pcp_shared->driContext;
124 }
125
126 pcp = Xmalloc(sizeof *pcp);
127 if (pcp == NULL)
128 return NULL;
129
130 pcp->psc = psc;
131 pcp->driContext =
132 (*psc->dri2->createNewContext)(psc->__driScreen,
133 config->driConfig, shared, pcp);
134 gc->__driContext = pcp->driContext;
135
136 if (pcp->driContext == NULL) {
137 Xfree(pcp);
138 return NULL;
139 }
140
141 pcp->base.destroyContext = dri2DestroyContext;
142 pcp->base.bindContext = dri2BindContext;
143 pcp->base.unbindContext = dri2UnbindContext;
144
145 return &pcp->base;
146 }
147
148 static void dri2DestroyDrawable(__GLXDRIdrawable *pdraw)
149 {
150 const __DRIcoreExtension *core = pdraw->psc->core;
151
152 (*core->destroyDrawable)(pdraw->driDrawable);
153 DRI2DestroyDrawable(pdraw->psc->dpy, pdraw->drawable);
154 Xfree(pdraw);
155 }
156
157 static __GLXDRIdrawable *dri2CreateDrawable(__GLXscreenConfigs *psc,
158 XID xDrawable,
159 GLXDrawable drawable,
160 const __GLcontextModes *modes)
161 {
162 __GLXDRIdrawablePrivate *pdraw;
163 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
164
165 pdraw = Xmalloc(sizeof(*pdraw));
166 if (!pdraw)
167 return NULL;
168
169 pdraw->base.destroyDrawable = dri2DestroyDrawable;
170 pdraw->base.xDrawable = xDrawable;
171 pdraw->base.drawable = drawable;
172 pdraw->base.psc = psc;
173 pdraw->bufferCount = 0;
174 pdraw->configureSeqno = ~0;
175
176 DRI2CreateDrawable(psc->dpy, xDrawable);
177
178 /* Create a new drawable */
179 pdraw->base.driDrawable =
180 (*psc->dri2->createNewDrawable)(psc->__driScreen,
181 config->driConfig, pdraw);
182
183 if (!pdraw->base.driDrawable) {
184 DRI2DestroyDrawable(psc->dpy, drawable);
185 Xfree(pdraw);
186 return NULL;
187 }
188
189 return &pdraw->base;
190 }
191
192 static void dri2CopySubBuffer(__GLXDRIdrawable *pdraw,
193 int x, int y, int width, int height)
194 {
195 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
196 XRectangle xrect;
197 XserverRegion region;
198
199 xrect.x = x;
200 xrect.y = priv->height - y - height;
201 xrect.width = width;
202 xrect.height = height;
203
204 region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
205 DRI2CopyRegion(pdraw->psc->dpy, pdraw->drawable, region,
206 DRI2BufferFrontLeft, DRI2BufferBackLeft);
207 XFixesDestroyRegion(pdraw->psc->dpy, region);
208 }
209
210 static void dri2SwapBuffers(__GLXDRIdrawable *pdraw)
211 {
212 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
213
214 dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
215 }
216
217 static void dri2DestroyScreen(__GLXscreenConfigs *psc)
218 {
219 /* Free the direct rendering per screen data */
220 (*psc->core->destroyScreen)(psc->__driScreen);
221 close(psc->fd);
222 psc->__driScreen = NULL;
223 }
224
225 static __DRIbuffer *
226 dri2GetBuffers(__DRIdrawable *driDrawable,
227 int *width, int *height,
228 unsigned int *attachments, int count,
229 int *out_count, void *loaderPrivate)
230 {
231 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
232 __GLXdisplayPrivate *dpyPriv = __glXInitialize(pdraw->base.psc->dpy);
233 __GLXDRIdisplayPrivate *pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
234 DRI2Buffer *buffers;
235 int i;
236
237 /**
238 * Check if a ConfigureNotify has come in since we last asked for the
239 * buffers associated with this drawable. If not, we can assume that they're
240 * the same set at glViewport time, and save a synchronous round-trip to the
241 * X Server.
242 */
243 if (pdraw->configureSeqno == pdp->configureSeqno &&
244 count == pdraw->bufferCount) {
245 for (i = 0; i < count; i++) {
246 if (pdraw->buffers[i].attachment != attachments[i])
247 break;
248 }
249 if (i == count) {
250 *out_count = pdraw->bufferCount;
251 return pdraw->buffers;
252 }
253 }
254 pdraw->configureSeqno = pdp->configureSeqno;
255
256 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
257 width, height, attachments, count, out_count);
258 if (buffers == NULL)
259 return NULL;
260
261 pdraw->width = *width;
262 pdraw->height = *height;
263 pdraw->bufferCount = *out_count;
264
265 /* This assumes the DRI2 buffer attachment tokens matches the
266 * __DRIbuffer tokens. */
267 for (i = 0; i < *out_count; i++) {
268 pdraw->buffers[i].attachment = buffers[i].attachment;
269 pdraw->buffers[i].name = buffers[i].name;
270 pdraw->buffers[i].pitch = buffers[i].pitch;
271 pdraw->buffers[i].cpp = buffers[i].cpp;
272 pdraw->buffers[i].flags = buffers[i].flags;
273 }
274
275 Xfree(buffers);
276
277 return pdraw->buffers;
278 }
279
280 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
281 { __DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION },
282 dri2GetBuffers,
283 };
284
285 static const __DRIextension *loader_extensions[] = {
286 &dri2LoaderExtension.base,
287 &systemTimeExtension.base,
288 NULL
289 };
290
291 static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
292 __GLXdisplayPrivate *priv)
293 {
294 const __DRIconfig **driver_configs;
295 const __DRIextension **extensions;
296 __GLXDRIscreen *psp;
297 char *driverName, *deviceName;
298 drm_magic_t magic;
299 int i;
300
301 psp = Xmalloc(sizeof *psp);
302 if (psp == NULL)
303 return NULL;
304
305 /* Initialize per screen dynamic client GLX extensions */
306 psc->ext_list_first_time = GL_TRUE;
307
308 if (!DRI2Connect(psc->dpy, RootWindow(psc->dpy, screen),
309 &driverName, &deviceName))
310 return NULL;
311
312 psc->driver = driOpenDriver(driverName);
313 if (psc->driver == NULL) {
314 ErrorMessageF("driver pointer missing\n");
315 goto handle_error;
316 }
317
318 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
319 if (extensions == NULL) {
320 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
321 goto handle_error;
322 }
323
324 for (i = 0; extensions[i]; i++) {
325 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
326 psc->core = (__DRIcoreExtension *) extensions[i];
327 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
328 psc->dri2 = (__DRIdri2Extension *) extensions[i];
329 }
330
331 if (psc->core == NULL || psc->dri2 == NULL) {
332 ErrorMessageF("core dri or dri2 extension not found\n");
333 goto handle_error;
334 }
335
336 psc->fd = open(deviceName, O_RDWR);
337 if (psc->fd < 0) {
338 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
339 return NULL;
340 }
341
342 if (drmGetMagic(psc->fd, &magic)) {
343 ErrorMessageF("failed to get magic\n");
344 return NULL;
345 }
346
347 if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic)) {
348 ErrorMessageF("failed to authenticate magic %d\n", magic);
349 return NULL;
350 }
351
352 psc->__driScreen =
353 psc->dri2->createNewScreen(screen, psc->fd,
354 loader_extensions, &driver_configs, psc);
355 if (psc->__driScreen == NULL) {
356 ErrorMessageF("failed to create dri screen\n");
357 return NULL;
358 }
359
360 driBindExtensions(psc, 1);
361
362 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
363 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
364
365 psp->destroyScreen = dri2DestroyScreen;
366 psp->createContext = dri2CreateContext;
367 psp->createDrawable = dri2CreateDrawable;
368 psp->swapBuffers = dri2SwapBuffers;
369
370 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
371 * available.*/
372 psp->copySubBuffer = dri2CopySubBuffer;
373 __glXEnableDirectExtension(psc, "GLX_MESA_copy_sub_buffer");
374
375 Xfree(driverName);
376 Xfree(deviceName);
377
378 return psp;
379
380 handle_error:
381 Xfree(driverName);
382 Xfree(deviceName);
383
384 /* FIXME: clean up here */
385
386 return NULL;
387 }
388
389 /* Called from __glXFreeDisplayPrivate.
390 */
391 static void dri2DestroyDisplay(__GLXDRIdisplay *dpy)
392 {
393 Xfree(dpy);
394 }
395
396 /**
397 * Makes a note on receiving ConfigureNotify that we need to re-check the
398 * DRI2 buffers, as window sizes may have resulted in reallocation.
399 */
400 static Bool dri2ConfigureNotifyProc(Display *dpy, XEvent *re, xEvent *event)
401 {
402 __GLXdisplayPrivate *dpyPriv = __glXInitialize(dpy);
403 __GLXDRIdisplayPrivate *pdp;
404 Bool ret;
405
406 /* We should always be able to find our pdp, as it only gets torn down
407 * when the Display is torn down.
408 */
409 pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
410
411 ret = pdp->oldConfigProc(dpy, re, event);
412
413 pdp->configureSeqno = re->xconfigure.serial;
414
415 return ret;
416 }
417
418 /*
419 * Allocate, initialize and return a __DRIdisplayPrivate object.
420 * This is called from __glXInitialize() when we are given a new
421 * display pointer.
422 */
423 _X_HIDDEN __GLXDRIdisplay *dri2CreateDisplay(Display *dpy)
424 {
425 __GLXDRIdisplayPrivate *pdp;
426 int eventBase, errorBase;
427
428 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
429 return NULL;
430
431 pdp = Xmalloc(sizeof *pdp);
432 if (pdp == NULL)
433 return NULL;
434
435 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
436 Xfree(pdp);
437 return NULL;
438 }
439
440 pdp->oldConfigProc = XESetWireToEvent(dpy, ConfigureNotify,
441 dri2ConfigureNotifyProc);
442
443 pdp->driPatch = 0;
444 pdp->configureSeqno = 0;
445
446 pdp->base.destroyDisplay = dri2DestroyDisplay;
447 pdp->base.createScreen = dri2CreateScreen;
448
449 return &pdp->base;
450 }
451
452 #endif /* GLX_DIRECT_RENDERING */