2 * DirectDraw DGA2 interface
4 * Copyright 2001 TransGaming Technologies, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_LIBXXF86DGA2
26 #include <X11/extensions/xf86dga.h>
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
41 LPDDHALMODEINFO xf86dga2_modes;
42 unsigned xf86dga2_mode_count;
43 static XDGAMode* modes;
44 static int dga_event, dga_error;
46 static void convert_mode(XDGAMode *mode, LPDDHALMODEINFO info)
48 info->dwWidth = mode->viewportWidth;
49 info->dwHeight = mode->viewportHeight;
50 info->wRefreshRate = mode->verticalRefresh;
51 info->lPitch = mode->bytesPerScanline;
52 info->dwBPP = (mode->depth < 24) ? mode->depth : mode->bitsPerPixel;
53 info->wFlags = (mode->depth == 8) ? DDMODEINFO_PALETTIZED : 0;
54 info->dwRBitMask = mode->redMask;
55 info->dwGBitMask = mode->greenMask;
56 info->dwBBitMask = mode->blueMask;
57 info->dwAlphaBitMask = 0;
58 TRACE(" width=%ld, height=%ld, bpp=%ld, refresh=%d\n",
59 info->dwWidth, info->dwHeight, info->dwBPP, info->wRefreshRate);
62 static int DGA2ErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
67 void X11DRV_XF86DGA2_Init(void)
69 int nmodes, major, minor, i;
72 if (xf86dga2_modes) return; /* already initialized? */
74 /* if in desktop mode, don't use DGA */
75 if (root_window != DefaultRootWindow(gdi_display)) return;
80 ok = XDGAQueryExtension(gdi_display, &dga_event, &dga_error);
83 X11DRV_expect_error(gdi_display, DGA2ErrorHandler, NULL);
84 ok = XDGAQueryVersion(gdi_display, &major, &minor);
85 if (X11DRV_check_error()) ok = FALSE;
90 if (major < 2) return; /* only bother with DGA 2+ */
92 /* test that it works */
94 if ((ok = XDGAOpenFramebuffer(gdi_display, DefaultScreen(gdi_display))))
96 XDGACloseFramebuffer(gdi_display, DefaultScreen(gdi_display));
98 modes = XDGAQueryModes(gdi_display, DefaultScreen(gdi_display), &nmodes);
99 if (!modes) ok = FALSE;
101 else WARN("disabling XF86DGA2 (insufficient permissions?)\n");
105 TRACE("DGA modes: count=%d\n", nmodes);
107 xf86dga2_mode_count = nmodes+1;
108 xf86dga2_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * (nmodes+1));
110 /* make dummy mode for exiting DGA */
111 memset(&xf86dga2_modes[0], 0, sizeof(xf86dga2_modes[0]));
113 /* convert modes to DDHALMODEINFO format */
114 for (i=0; i<nmodes; i++)
115 convert_mode(&modes[i], &xf86dga2_modes[i+1]);
117 TRACE("Enabling XF86DGA2 mode\n");
120 void X11DRV_XF86DGA2_Cleanup(void)
122 if (modes) TSXFree(modes);
125 static XDGADevice *dga_dev;
127 static VIDMEM dga_mem = {
128 VIDMEM_ISRECTANGULAR | VIDMEM_ISHEAP
131 static DWORD PASCAL X11DRV_XF86DGA2_SetMode(LPDDHAL_SETMODEDATA data)
133 LPDDRAWI_DIRECTDRAW_LCL ddlocal = data->lpDD->lpExclusiveOwner;
135 Display *display = gdi_display;
137 data->ddRVal = DD_OK;
139 if (data->dwModeIndex) {
141 XDGADevice *new_dev = NULL;
142 if (dga_dev || XDGAOpenFramebuffer(display, DefaultScreen(display)))
143 new_dev = XDGASetMode(display, DefaultScreen(display), modes[data->dwModeIndex-1].num);
145 XDGASetViewport(display, DefaultScreen(display), 0, 0, XDGAFlipImmediate);
147 VirtualFree(dga_dev->data, 0, MEM_RELEASE);
150 XDGASelectInput(display, DefaultScreen(display),
151 KeyPressMask|KeyReleaseMask|
152 ButtonPressMask|ButtonReleaseMask|
154 X11DRV_EVENT_SetDGAStatus((HWND)ddlocal->hWnd, dga_event);
155 X11DRV_EVENT_SetInputMethod(X11DRV_INPUT_RELATIVE);
158 vram = dga_dev->mode.bytesPerScanline * dga_dev->mode.imageHeight;
159 VirtualAlloc(dga_dev->data, vram, MEM_RESERVE|MEM_SYSTEM, PAGE_READWRITE);
160 dga_mem.fpStart = (FLATPTR)dga_dev->data;
161 dga_mem.u1.dwWidth = dga_dev->mode.bytesPerScanline;
162 dga_mem.u2.dwHeight = dga_dev->mode.imageHeight;
163 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, dga_dev->data, &dga_mem);
164 X11DRV_DD_IsDirect = TRUE;
168 if (!dga_dev) XDGACloseFramebuffer(display, DefaultScreen(display));
169 data->ddRVal = DDERR_GENERIC;
174 X11DRV_DD_IsDirect = FALSE;
175 X11DRV_DDHAL_SwitchMode(0, NULL, NULL);
176 XDGASetMode(display, DefaultScreen(display), 0);
177 VirtualFree(dga_dev->data, 0, MEM_RELEASE);
178 X11DRV_EVENT_SetInputMethod(X11DRV_INPUT_ABSOLUTE);
179 X11DRV_EVENT_SetDGAStatus(0, -1);
181 XDGACloseFramebuffer(display, DefaultScreen(display));
185 return DDHAL_DRIVER_HANDLED;
188 static LPDDHAL_CREATESURFACE X11DRV_XF86DGA2_old_create_surface;
190 static DWORD PASCAL X11DRV_XF86DGA2_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
192 LPDDRAWI_DDRAWSURFACE_LCL lcl = *data->lplpSList;
193 LPDDRAWI_DDRAWSURFACE_GBL gbl = lcl->lpGbl;
194 LPDDSURFACEDESC2 desc = (LPDDSURFACEDESC2)data->lpDDSurfaceDesc;
195 HRESULT hr = DDHAL_DRIVER_NOTHANDLED;
197 if (X11DRV_XF86DGA2_old_create_surface)
198 hr = X11DRV_XF86DGA2_old_create_surface(data);
200 if (desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)) {
201 gbl->fpVidMem = 0; /* tell ddraw to allocate the memory */
202 hr = DDHAL_DRIVER_HANDLED;
207 static DWORD PASCAL X11DRV_XF86DGA2_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
209 Display *display = gdi_display;
211 data->lpDDPalette->u1.dwReserved1 = XDGACreateColormap(display, DefaultScreen(display),
214 if (data->lpColorTable)
215 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1, 0, 256,
217 data->ddRVal = DD_OK;
218 return DDHAL_DRIVER_HANDLED;
221 static DWORD PASCAL X11DRV_XF86DGA2_Flip(LPDDHAL_FLIPDATA data)
223 Display *display = gdi_display;
224 if (data->lpSurfCurr == X11DRV_DD_Primary) {
225 DWORD ofs = data->lpSurfCurr->lpGbl->fpVidMem - dga_mem.fpStart;
227 XDGASetViewport(display, DefaultScreen(display),
228 (ofs % dga_dev->mode.bytesPerScanline)*8/dga_dev->mode.bitsPerPixel,
229 ofs / dga_dev->mode.bytesPerScanline,
233 data->ddRVal = DD_OK;
234 return DDHAL_DRIVER_HANDLED;
237 static DWORD PASCAL X11DRV_XF86DGA2_SetPalette(LPDDHAL_SETPALETTEDATA data)
239 Display *display = gdi_display;
240 if ((data->lpDDSurface == X11DRV_DD_Primary) &&
241 data->lpDDPalette && data->lpDDPalette->u1.dwReserved1)
244 XDGAInstallColormap(display, DefaultScreen(display), data->lpDDPalette->u1.dwReserved1);
247 data->ddRVal = DD_OK;
248 return DDHAL_DRIVER_HANDLED;
251 int X11DRV_XF86DGA2_CreateDriver(LPDDHALINFO info)
253 if (!xf86dga2_mode_count) return 0; /* no DGA */
255 info->dwNumModes = xf86dga2_mode_count;
256 info->lpModeInfo = xf86dga2_modes;
257 info->dwModeIndex = 0;
259 X11DRV_XF86DGA2_old_create_surface = info->lpDDCallbacks->CreateSurface;
260 info->lpDDCallbacks->SetMode = X11DRV_XF86DGA2_SetMode;
261 info->lpDDCallbacks->CreateSurface = X11DRV_XF86DGA2_CreateSurface;
262 info->lpDDCallbacks->CreatePalette = X11DRV_XF86DGA2_CreatePalette;
263 info->lpDDSurfaceCallbacks->Flip = X11DRV_XF86DGA2_Flip;
264 info->lpDDSurfaceCallbacks->SetPalette = X11DRV_XF86DGA2_SetPalette;
268 #endif /* HAVE_LIBXXF86DGA2 */