fbscreen.c revision 4642e01f
105b261ecSmrg/*
205b261ecSmrg * Copyright © 1998 Keith Packard
305b261ecSmrg *
405b261ecSmrg * Permission to use, copy, modify, distribute, and sell this software and its
505b261ecSmrg * documentation for any purpose is hereby granted without fee, provided that
605b261ecSmrg * the above copyright notice appear in all copies and that both that
705b261ecSmrg * copyright notice and this permission notice appear in supporting
805b261ecSmrg * documentation, and that the name of Keith Packard not be used in
905b261ecSmrg * advertising or publicity pertaining to distribution of the software without
1005b261ecSmrg * specific, written prior permission.  Keith Packard makes no
1105b261ecSmrg * representations about the suitability of this software for any purpose.  It
1205b261ecSmrg * is provided "as is" without express or implied warranty.
1305b261ecSmrg *
1405b261ecSmrg * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1505b261ecSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1605b261ecSmrg * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1705b261ecSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1805b261ecSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1905b261ecSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2005b261ecSmrg * PERFORMANCE OF THIS SOFTWARE.
2105b261ecSmrg */
2205b261ecSmrg
2305b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
2405b261ecSmrg#include <dix-config.h>
2505b261ecSmrg#endif
2605b261ecSmrg
2705b261ecSmrg#include "fb.h"
2805b261ecSmrg
2905b261ecSmrgBool
3005b261ecSmrgfbCloseScreen (int index, ScreenPtr pScreen)
3105b261ecSmrg{
3205b261ecSmrg    int	    d;
3305b261ecSmrg    DepthPtr	depths = pScreen->allowedDepths;
3405b261ecSmrg
3505b261ecSmrg    for (d = 0; d < pScreen->numDepths; d++)
3605b261ecSmrg	xfree (depths[d].vids);
3705b261ecSmrg    xfree (depths);
3805b261ecSmrg    xfree (pScreen->visuals);
3905b261ecSmrg    xfree (pScreen->devPrivate);
4005b261ecSmrg#ifdef FB_SCREEN_PRIVATE
414642e01fSmrg    xfree (dixLookupPrivate(&pScreen->devPrivates, fbGetScreenPrivateKey()));
4205b261ecSmrg#endif
4305b261ecSmrg    return TRUE;
4405b261ecSmrg}
4505b261ecSmrg
4605b261ecSmrgBool
4705b261ecSmrgfbRealizeFont(ScreenPtr pScreen, FontPtr pFont)
4805b261ecSmrg{
4905b261ecSmrg    return (TRUE);
5005b261ecSmrg}
5105b261ecSmrg
5205b261ecSmrgBool
5305b261ecSmrgfbUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
5405b261ecSmrg{
5505b261ecSmrg    return (TRUE);
5605b261ecSmrg}
5705b261ecSmrg
5805b261ecSmrgvoid
5905b261ecSmrgfbQueryBestSize (int class,
6005b261ecSmrg		 unsigned short *width, unsigned short *height,
6105b261ecSmrg		 ScreenPtr pScreen)
6205b261ecSmrg{
6305b261ecSmrg    unsigned short  w;
6405b261ecSmrg
6505b261ecSmrg    switch (class) {
6605b261ecSmrg    case CursorShape:
6705b261ecSmrg	if (*width > pScreen->width)
6805b261ecSmrg	    *width = pScreen->width;
6905b261ecSmrg	if (*height > pScreen->height)
7005b261ecSmrg	    *height = pScreen->height;
7105b261ecSmrg	break;
7205b261ecSmrg    case TileShape:
7305b261ecSmrg    case StippleShape:
7405b261ecSmrg	w = *width;
7505b261ecSmrg	if ((w & (w - 1)) && w < FB_UNIT)
7605b261ecSmrg	{
7705b261ecSmrg	    for (w = 1; w < *width; w <<= 1)
7805b261ecSmrg		;
7905b261ecSmrg	    *width = w;
8005b261ecSmrg	}
8105b261ecSmrg    }
8205b261ecSmrg}
8305b261ecSmrg
8405b261ecSmrgPixmapPtr
8505b261ecSmrg_fbGetWindowPixmap (WindowPtr pWindow)
8605b261ecSmrg{
8705b261ecSmrg    return fbGetWindowPixmap (pWindow);
8805b261ecSmrg}
8905b261ecSmrg
9005b261ecSmrgvoid
9105b261ecSmrg_fbSetWindowPixmap (WindowPtr pWindow, PixmapPtr pPixmap)
9205b261ecSmrg{
934642e01fSmrg    dixSetPrivate(&pWindow->devPrivates, fbGetWinPrivateKey(), pPixmap);
9405b261ecSmrg}
9505b261ecSmrg
9605b261ecSmrgBool
9705b261ecSmrgfbSetupScreen(ScreenPtr	pScreen,
9805b261ecSmrg	      pointer	pbits,		/* pointer to screen bitmap */
9905b261ecSmrg	      int	xsize, 		/* in pixels */
10005b261ecSmrg	      int	ysize,
10105b261ecSmrg	      int	dpix,		/* dots per inch */
10205b261ecSmrg	      int	dpiy,
10305b261ecSmrg	      int	width,		/* pixel width of frame buffer */
10405b261ecSmrg	      int	bpp)		/* bits per pixel for screen */
10505b261ecSmrg{
1064642e01fSmrg    if (!fbAllocatePrivates(pScreen, NULL))
10705b261ecSmrg	return FALSE;
10805b261ecSmrg    pScreen->defColormap = FakeClientID(0);
10905b261ecSmrg    /* let CreateDefColormap do whatever it wants for pixels */
11005b261ecSmrg    pScreen->blackPixel = pScreen->whitePixel = (Pixel) 0;
11105b261ecSmrg    pScreen->QueryBestSize = fbQueryBestSize;
11205b261ecSmrg    /* SaveScreen */
11305b261ecSmrg    pScreen->GetImage = fbGetImage;
11405b261ecSmrg    pScreen->GetSpans = fbGetSpans;
11505b261ecSmrg    pScreen->CreateWindow = fbCreateWindow;
11605b261ecSmrg    pScreen->DestroyWindow = fbDestroyWindow;
11705b261ecSmrg    pScreen->PositionWindow = fbPositionWindow;
11805b261ecSmrg    pScreen->ChangeWindowAttributes = fbChangeWindowAttributes;
11905b261ecSmrg    pScreen->RealizeWindow = fbMapWindow;
12005b261ecSmrg    pScreen->UnrealizeWindow = fbUnmapWindow;
12105b261ecSmrg    pScreen->CopyWindow = fbCopyWindow;
12205b261ecSmrg    pScreen->CreatePixmap = fbCreatePixmap;
12305b261ecSmrg    pScreen->DestroyPixmap = fbDestroyPixmap;
12405b261ecSmrg    pScreen->RealizeFont = fbRealizeFont;
12505b261ecSmrg    pScreen->UnrealizeFont = fbUnrealizeFont;
12605b261ecSmrg    pScreen->CreateGC = fbCreateGC;
12705b261ecSmrg    pScreen->CreateColormap = fbInitializeColormap;
12805b261ecSmrg    pScreen->DestroyColormap = (void (*)(ColormapPtr))NoopDDA;
12905b261ecSmrg    pScreen->InstallColormap = fbInstallColormap;
13005b261ecSmrg    pScreen->UninstallColormap = fbUninstallColormap;
13105b261ecSmrg    pScreen->ListInstalledColormaps = fbListInstalledColormaps;
13205b261ecSmrg    pScreen->StoreColors = (void (*)(ColormapPtr, int, xColorItem *))NoopDDA;
13305b261ecSmrg    pScreen->ResolveColor = fbResolveColor;
13405b261ecSmrg    pScreen->BitmapToRegion = fbPixmapToRegion;
13505b261ecSmrg
13605b261ecSmrg    pScreen->GetWindowPixmap = _fbGetWindowPixmap;
13705b261ecSmrg    pScreen->SetWindowPixmap = _fbSetWindowPixmap;
13805b261ecSmrg
13905b261ecSmrg    return TRUE;
14005b261ecSmrg}
14105b261ecSmrg
14205b261ecSmrg#ifdef FB_ACCESS_WRAPPER
14305b261ecSmrgBool
14405b261ecSmrgwfbFinishScreenInit(ScreenPtr		pScreen,
14505b261ecSmrg		    pointer		pbits,
14605b261ecSmrg		    int			xsize,
14705b261ecSmrg		    int			ysize,
14805b261ecSmrg		    int			dpix,
14905b261ecSmrg		    int			dpiy,
15005b261ecSmrg		    int			width,
15105b261ecSmrg		    int			bpp,
15205b261ecSmrg		    SetupWrapProcPtr	setupWrap,
15305b261ecSmrg		    FinishWrapProcPtr	finishWrap)
15405b261ecSmrg#else
15505b261ecSmrgBool
15605b261ecSmrgfbFinishScreenInit(ScreenPtr	pScreen,
15705b261ecSmrg		   pointer	pbits,
15805b261ecSmrg		   int		xsize,
15905b261ecSmrg		   int		ysize,
16005b261ecSmrg		   int		dpix,
16105b261ecSmrg		   int		dpiy,
16205b261ecSmrg		   int		width,
16305b261ecSmrg		   int		bpp)
16405b261ecSmrg#endif
16505b261ecSmrg{
16605b261ecSmrg    VisualPtr	visuals;
16705b261ecSmrg    DepthPtr	depths;
16805b261ecSmrg    int		nvisuals;
16905b261ecSmrg    int		ndepths;
17005b261ecSmrg    int		rootdepth;
17105b261ecSmrg    VisualID	defaultVisual;
17205b261ecSmrg    int		imagebpp = bpp;
17305b261ecSmrg
17405b261ecSmrg#ifdef FB_DEBUG
17505b261ecSmrg    int	stride;
17605b261ecSmrg
17705b261ecSmrg    ysize -= 2;
17805b261ecSmrg    stride = (width * bpp) / 8;
17905b261ecSmrg    fbSetBits ((FbStip *) pbits,
18005b261ecSmrg	       stride / sizeof (FbStip), FB_HEAD_BITS);
18105b261ecSmrg    pbits = (void *) ((char *) pbits + stride);
18205b261ecSmrg    fbSetBits ((FbStip *) ((char *) pbits + stride * ysize),
18305b261ecSmrg			   stride / sizeof (FbStip), FB_TAIL_BITS);
18405b261ecSmrg#endif
18505b261ecSmrg    /*
18605b261ecSmrg     * By default, a 24bpp screen will use 32bpp images, this avoids
18705b261ecSmrg     * problems with many applications which just can't handle packed
18805b261ecSmrg     * pixels.  If you want real 24bit images, include a 24bpp
18905b261ecSmrg     * format in the pixmap formats
19005b261ecSmrg     */
19105b261ecSmrg#ifdef FB_24_32BIT
19205b261ecSmrg    if (bpp == 24)
19305b261ecSmrg    {
19405b261ecSmrg	int	f;
19505b261ecSmrg
19605b261ecSmrg	imagebpp = 32;
19705b261ecSmrg	/*
19805b261ecSmrg	 * Check to see if we're advertising a 24bpp image format,
19905b261ecSmrg	 * in which case windows will use it in preference to a 32 bit
20005b261ecSmrg	 * format.
20105b261ecSmrg	 */
20205b261ecSmrg	for (f = 0; f < screenInfo.numPixmapFormats; f++)
20305b261ecSmrg	{
20405b261ecSmrg	    if (screenInfo.formats[f].bitsPerPixel == 24)
20505b261ecSmrg	    {
20605b261ecSmrg		imagebpp = 24;
20705b261ecSmrg		break;
20805b261ecSmrg	    }
20905b261ecSmrg	}
21005b261ecSmrg    }
21105b261ecSmrg#endif
21205b261ecSmrg#ifdef FB_SCREEN_PRIVATE
21305b261ecSmrg    if (imagebpp == 32)
21405b261ecSmrg    {
21505b261ecSmrg	fbGetScreenPrivate(pScreen)->win32bpp = bpp;
21605b261ecSmrg	fbGetScreenPrivate(pScreen)->pix32bpp = bpp;
21705b261ecSmrg    }
21805b261ecSmrg    else
21905b261ecSmrg    {
22005b261ecSmrg	fbGetScreenPrivate(pScreen)->win32bpp = 32;
22105b261ecSmrg	fbGetScreenPrivate(pScreen)->pix32bpp = 32;
22205b261ecSmrg    }
22305b261ecSmrg#ifdef FB_ACCESS_WRAPPER
22405b261ecSmrg    fbGetScreenPrivate(pScreen)->setupWrap = setupWrap;
22505b261ecSmrg    fbGetScreenPrivate(pScreen)->finishWrap = finishWrap;
22605b261ecSmrg#endif
22705b261ecSmrg#endif
22805b261ecSmrg    rootdepth = 0;
22905b261ecSmrg    if (!fbInitVisuals (&visuals, &depths, &nvisuals, &ndepths, &rootdepth,
23005b261ecSmrg			&defaultVisual,((unsigned long)1<<(imagebpp-1)), 8))
23105b261ecSmrg	return FALSE;
23205b261ecSmrg    if (! miScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy, width,
23305b261ecSmrg			rootdepth, ndepths, depths,
23405b261ecSmrg			defaultVisual, nvisuals, visuals))
23505b261ecSmrg	return FALSE;
23605b261ecSmrg    /* overwrite miCloseScreen with our own */
23705b261ecSmrg    pScreen->CloseScreen = fbCloseScreen;
23805b261ecSmrg#ifdef FB_24_32BIT
23905b261ecSmrg    if (bpp == 24 && imagebpp == 32)
24005b261ecSmrg    {
24105b261ecSmrg	pScreen->ModifyPixmapHeader = fb24_32ModifyPixmapHeader;
24205b261ecSmrg	pScreen->CreateScreenResources = fb24_32CreateScreenResources;
24305b261ecSmrg    }
24405b261ecSmrg#endif
24505b261ecSmrg#if 0
24605b261ecSmrg    /* leave backing store initialization to the enclosing code so
24705b261ecSmrg     * it can choose the correct order of wrappers
24805b261ecSmrg     */
24905b261ecSmrg    /* init backing store here so we can overwrite CloseScreen without stepping
25005b261ecSmrg     * on the backing store wrapped version */
25105b261ecSmrg    fbInitializeBackingStore (pScreen);
25205b261ecSmrg#endif
25305b261ecSmrg    return TRUE;
25405b261ecSmrg}
25505b261ecSmrg
25605b261ecSmrg/* dts * (inch/dot) * (25.4 mm / inch) = mm */
25705b261ecSmrg#ifdef FB_ACCESS_WRAPPER
25805b261ecSmrgBool
25905b261ecSmrgwfbScreenInit(ScreenPtr		pScreen,
26005b261ecSmrg	      pointer		pbits,
26105b261ecSmrg	      int		xsize,
26205b261ecSmrg	      int		ysize,
26305b261ecSmrg	      int		dpix,
26405b261ecSmrg	      int		dpiy,
26505b261ecSmrg	      int		width,
26605b261ecSmrg	      int		bpp,
26705b261ecSmrg	      SetupWrapProcPtr	setupWrap,
26805b261ecSmrg	      FinishWrapProcPtr	finishWrap)
26905b261ecSmrg{
27005b261ecSmrg    if (!fbSetupScreen(pScreen, pbits, xsize, ysize, dpix, dpiy, width, bpp))
27105b261ecSmrg	return FALSE;
27205b261ecSmrg    if (!wfbFinishScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy,
27305b261ecSmrg			     width, bpp, setupWrap, finishWrap))
27405b261ecSmrg	return FALSE;
27505b261ecSmrg    return TRUE;
27605b261ecSmrg}
27705b261ecSmrg#else
27805b261ecSmrgBool
27905b261ecSmrgfbScreenInit(ScreenPtr	pScreen,
28005b261ecSmrg	     pointer	pbits,
28105b261ecSmrg	     int	xsize,
28205b261ecSmrg	     int	ysize,
28305b261ecSmrg	     int	dpix,
28405b261ecSmrg	     int	dpiy,
28505b261ecSmrg	     int	width,
28605b261ecSmrg	     int	bpp)
28705b261ecSmrg{
28805b261ecSmrg    if (!fbSetupScreen(pScreen, pbits, xsize, ysize, dpix, dpiy, width, bpp))
28905b261ecSmrg	return FALSE;
29005b261ecSmrg    if (!fbFinishScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy,
29105b261ecSmrg			    width, bpp))
29205b261ecSmrg	return FALSE;
29305b261ecSmrg    return TRUE;
29405b261ecSmrg}
29505b261ecSmrg#endif
296