xserver-multidpi/hw/xfree86/common/xf86cmap.c
Dave Airlie 1f0e8bd5eb api: rework the X server driver API to avoid global arrays.
This is a squash merge containing all the API changes, as
well as the video ABI bump.

Its been squashed to make bisection easier.

Full patch log below:

commit b202738bbf0c5a1c1172767119c2c71f1e7f8070
Author: Aaron Plattner <aplattner@nvidia.com>
Date:   Mon May 14 15:16:11 2012 -0700

    xfree86: Bump video ABI to 13.0

    The ABI was broken by changes to convert from screen index numbers to ScreenPtr
    / ScrnInfoPtr in various structures and function signatures.

    Signed-off-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Dave Airlie <airlied@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3d5f7d9f8d408bcad3f83277d255f25d3b0edbf3
Author: Dave Airlie <airlied@redhat.com>
Date:   Thu May 24 10:56:57 2012 +0100

    xf86: xf86ClearEntityListForScreen should take a pScrn

    When adding GPU screens this make life easier.

    (also fix comment, as pointed out by Alan)

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit afee8b5ab4501597ecc1ade34124d7ca227ab055
Author: Dave Airlie <airlied@redhat.com>
Date:   Thu May 24 07:07:32 2012 +0100

    xf86i2c: add pscrn for drivers to use

    This just adds a pScrn pointer into the struct for the drivers to use
    instead of scrnIndex. Mostly scrnIndex is used for logging, but some
    drivers use it to lookup xf86Screens, so let them stash a pScrn instead.

    Removing the scrnIndex is a bit more involved and I'm not sure its worth
    the effort. Doing i2c in the X server is legacy code as far as I'm concerned.

    Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit ea5092f1f679691d187f1eee9427e6057beec56e
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 19:25:20 2012 +0100

    dix/gc: consolidate GC object creation in one place

    The standard GC create and scratch GC create were 90% the same really,
    and I have a need in the future for creating GC objects without the
    other bits, so wanted to avoid a third copy.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3d91482ea9b4883e64e496f2768168e0ffa21ba1
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 10:24:06 2012 +0100

    xf86: add a define to denote the new non-index interfaces are being used

    This can be used by drivers to provide compatible APIs.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 37c3ae3e6cd4f3dedc72f371096d6743f8f99df3
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 15:09:12 2012 +0100

    dix: make Create/Free scratch pixmaps take a ScreenPtr

    While technically an API/ABI change I doubt anyone uses it,
    but it helps in splitting screens up.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 75f2062a3fe94f04764ecc7d2ff2fbbeccb9da60
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:57:55 2012 +0100

    xf86/xv: remove scrnIndexfrom xf86FindXvOptions.

    Move this interface to taking an ScrnInfoPtr.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit f80c2374f40ea7b2ee0556e2e76cc07406f3d843
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:53:59 2012 +0100

    xf86: make xf86DeleteScreen take a ScrnInfoPtr (v2)

    stop passing indices into this function.

    v2: drop flags argument.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 58824e414f35682435f15bfe6c4b656bd90b9235
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:48:09 2012 +0100

    xf86: fix xf86IsScreenPrimary interface to take a pScrn (API/ABI)

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 6b4fc1f9d391bcdf7ca288766e49bce60f4635cd
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:18:59 2012 +0100

    xserver: convert block/wakeup handlers to passing ScreenPtr (ABI/API) (v2)

    Instead of passing an index, pass the actual ScreenPtr. This allows
    more moving towards not abusing xf86Screens + screenInfo.

    v2: drop the blockData/wakeupData args as per ajax's suggestion.,
    fix docs.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 790d003de20fb47674420a24dadd92412d78620d
Author: Dave Airlie <airlied@gmail.com>
Date:   Wed Apr 11 09:53:14 2012 +0100

    xf86/common: remove some more pScrn->pScreen uses

    remove some more conversions that appeared after api cleanups.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit aac85e18d1dd093f2cad6bd29375e40bd7af0b8f
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:34:53 2012 +0100

    ddc: change API to take ScrnInfoPtr (v2)

    This removes all xf86Screens usage from ddc code,
    it modifies the API for some functions to avoid taking indices.

    v2: address Alan's comments about dropping DDC2Init parameter.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit fe3f57b6eaf6860a33876a54f9439f69578f03a5
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:31:26 2012 +0100

    vbe: don't use index for VBEInterpretPanelID (API)

    Remove use of xf86screens from vbe module.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit abf1965f4ed91529036d3fdb470d6a3ce6f29675
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:25:11 2012 +0100

    int10/vbe: don't use xf86Screens. (ABI) (v3)

    Pass the ScrnInfoPtr instead of the index in the int10 struct.

    This saves us using it to dereference xf86Screens.

    v2: address Alan's comment to fix struct alignment.

    v3: squash in all the int10 fixes, test the vm86 code builds,
    after comments by Keith.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 23cca612b4fb5efc33683c7624b803b457387e3d
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:30:18 2012 +0100

    xserver: drop index argument to ScreenInit (ABI/API) (v2)

    This drops the index argument, its the same as pScreen->myNum,
    and its the last major index abuse I can find.

    v2: address Alan's review - update docs, fix xwin/xnest/darwin

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 40d360e2d7e832407f3ed64e3a02c27ecc89a960
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:23:01 2012 +0100

    xf86: migrate PointerMoved from index to ScrnInfoPtr (ABI/API)

    This migrates PointerMoved from an index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit aa60a2f38679d0eeb979a9c2648c9bc771409bf9
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:20:46 2012 +0100

    xf86: migrate PMEvent to a ScrnInfoPtr (ABI/API)

    This migrates the PMEvent from index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit d3f28ef44371ed4a039ffc5dd7eb6408d1269ba2
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:18:30 2012 +0100

    xf86: migrate SetDGAMode from index to ScrnInfoPtr (ABI/API)

    This migrates the SetDGAMode callback from an index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit baf5e4818a74f2b68c3dfdcc56f54322351039a0
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:14:11 2012 +0100

    xf86: migrate ChangeGamma from index to ScrnInfoPtr (ABI/API) (v2)

    This migrates the ChangeGamma interface to avoid passing a index.

    v2: fix xf86RandR12.c + xf86cmap.c call

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 51e5f90ada929d6b23176090badbb42fdb3fa550
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:11:09 2012 +0100

    xf86/exa: migrate index to screen types for EnableDisableFBAccess (ABI/API)

    The EXA interface migrates to ScreenPtr,
    and the xf86 interface migrated to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 94f1f21d17e86f96d4a54292a399160950087675
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:02:11 2012 +0100

    xf86: migrate ValidMode callback to ScrnInfoPtr (ABI/API)

    This migrates the ValidMode to passing a ScrnInfoPtr instead
    of an index.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3f8f18198fed4f39ec805b508a3482e91eea26b2
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:59:46 2012 +0100

    xf86: migrate SwitchMode to taking ScrnInfoPtr (ABI/API) (v2)

    This migrate the SwitchMode interface to take a ScrnInfoPtr
    instead of an index.

    v2: drop flags.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit d06a038a5c49328ab3a8d969d24f9fcd22c63202
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:50:37 2012 +0100

    xf86: move AdjustFrame to passing ScrnInfoPtr (ABI/API) (v2)

    This converts AdjustFrame code paths to passing a ScrnInfoPtr
    instead of an integer index.

    v2: drop flags args.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 53d2f8608ffd4090d08e7d5cf2e92fb954959b90
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:41:27 2012 +0100

    xf86: modify FreeScreen callback to take pScrn instead of index. (ABI/API) (v2)

    Another index->pScrn conversion.

    v2: drop flags arg.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 60db37c0b247052e0f5c54b1921fe58a3609c2e3
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:35:41 2012 +0100

    xf86: change EnterVT/LeaveVT to take a ScrnInfoPtr (ABI/API break) (v2)

    This modifies the EnterVT/LeaveVT interfaces to take a ScrnInfoPtr
    instead of an index into xf86Screens. This allows dropping more
    public dereferences of the xf86Screens and screenInfo.

    v2: drop flags args as suggested by Keith, fix docs.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 06729dbbc804a20242e6499f446acb5d94023c3c
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:04:59 2012 +0100

    xserver: remove index from CloseScreen (API/ABI breakage)

    This drops the index from the CloseScreen callback,
    its always been useless really, since the pScreen contains it.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>
2012-06-05 13:22:18 +01:00

1180 lines
37 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 1998-2001 by The XFree86 Project, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of the copyright holder(s)
* and author(s) shall not be used in advertising or otherwise to promote
* the sale, use or other dealings in this Software without prior written
* authorization from the copyright holder(s) and author(s).
*/
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#if defined(_XOPEN_SOURCE) || defined(sun) && defined(__SVR4)
#include <math.h>
#else
#define _XOPEN_SOURCE /* to get prototype for pow on some systems */
#include <math.h>
#undef _XOPEN_SOURCE
#endif
#include <X11/X.h>
#include "misc.h"
#include <X11/Xproto.h>
#include "colormapst.h"
#include "scrnintstr.h"
#include "resource.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86str.h"
#include "micmap.h"
#include "xf86Crtc.h"
#ifdef XFreeXDGA
#include <X11/extensions/xf86dgaproto.h>
#include "dgaproc.h"
#endif
#include "xf86cmap.h"
#define SCREEN_PROLOGUE(pScreen, field) ((pScreen)->field = \
((CMapScreenPtr)dixLookupPrivate(&(pScreen)->devPrivates, CMapScreenKey))->field)
#define SCREEN_EPILOGUE(pScreen, field, wrapper)\
((pScreen)->field = wrapper)
#define LOAD_PALETTE(pmap) \
((pmap == GetInstalledmiColormap(pmap->pScreen)) && \
((pScreenPriv->flags & CMAP_LOAD_EVEN_IF_OFFSCREEN) || \
xf86ScreenToScrn(pmap->pScreen)->vtSema || pScreenPriv->isDGAmode))
typedef struct _CMapLink {
ColormapPtr cmap;
struct _CMapLink *next;
} CMapLink, *CMapLinkPtr;
typedef struct {
ScrnInfoPtr pScrn;
CloseScreenProcPtr CloseScreen;
CreateColormapProcPtr CreateColormap;
DestroyColormapProcPtr DestroyColormap;
InstallColormapProcPtr InstallColormap;
StoreColorsProcPtr StoreColors;
Bool (*EnterVT) (ScrnInfoPtr);
Bool (*SwitchMode) (ScrnInfoPtr, DisplayModePtr);
int (*SetDGAMode) (ScrnInfoPtr, int, DGADevicePtr);
xf86ChangeGammaProc *ChangeGamma;
int maxColors;
int sigRGBbits;
int gammaElements;
LOCO *gamma;
int *PreAllocIndices;
CMapLinkPtr maps;
unsigned int flags;
Bool isDGAmode;
} CMapScreenRec, *CMapScreenPtr;
typedef struct {
int numColors;
LOCO *colors;
Bool recalculate;
int overscan;
} CMapColormapRec, *CMapColormapPtr;
static DevPrivateKeyRec CMapScreenKeyRec;
#define CMapScreenKeyRegistered dixPrivateKeyRegistered(&CMapScreenKeyRec)
#define CMapScreenKey (&CMapScreenKeyRec)
static DevPrivateKeyRec CMapColormapKeyRec;
#define CMapColormapKey (&CMapColormapKeyRec)
static void CMapInstallColormap(ColormapPtr);
static void CMapStoreColors(ColormapPtr, int, xColorItem *);
static Bool CMapCloseScreen(ScreenPtr);
static Bool CMapCreateColormap(ColormapPtr);
static void CMapDestroyColormap(ColormapPtr);
static Bool CMapEnterVT(ScrnInfoPtr);
static Bool CMapSwitchMode(ScrnInfoPtr, DisplayModePtr);
#ifdef XFreeXDGA
static int CMapSetDGAMode(ScrnInfoPtr, int, DGADevicePtr);
#endif
static int CMapChangeGamma(ScrnInfoPtr, Gamma);
static void ComputeGamma(CMapScreenPtr);
static Bool CMapAllocateColormapPrivate(ColormapPtr);
static void CMapRefreshColors(ColormapPtr, int, int *);
static void CMapSetOverscan(ColormapPtr, int, int *);
static void CMapReinstallMap(ColormapPtr);
static void CMapUnwrapScreen(ScreenPtr pScreen);
Bool
xf86ColormapAllocatePrivates(ScrnInfoPtr pScrn)
{
/* If we support a better colormap system, then pretend we succeeded. */
if (xf86_crtc_supports_gamma(pScrn))
return TRUE;
if (!dixRegisterPrivateKey(&CMapScreenKeyRec, PRIVATE_SCREEN, 0))
return FALSE;
if (!dixRegisterPrivateKey(&CMapColormapKeyRec, PRIVATE_COLORMAP, 0))
return FALSE;
return TRUE;
}
Bool
xf86HandleColormaps(ScreenPtr pScreen,
int maxColors,
int sigRGBbits,
xf86LoadPaletteProc * loadPalette,
xf86SetOverscanProc * setOverscan, unsigned int flags)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
ColormapPtr pDefMap = NULL;
CMapScreenPtr pScreenPriv;
LOCO *gamma;
int *indices;
int elements;
/* If we support a better colormap system, then pretend we succeeded. */
if (xf86_crtc_supports_gamma(pScrn))
return TRUE;
if (!maxColors || !sigRGBbits || !loadPalette)
return FALSE;
elements = 1 << sigRGBbits;
if (!(gamma = malloc(elements * sizeof(LOCO))))
return FALSE;
if (!(indices = malloc(maxColors * sizeof(int)))) {
free(gamma);
return FALSE;
}
if (!(pScreenPriv = malloc(sizeof(CMapScreenRec)))) {
free(gamma);
free(indices);
return FALSE;
}
dixSetPrivate(&pScreen->devPrivates, &CMapScreenKeyRec, pScreenPriv);
pScreenPriv->CloseScreen = pScreen->CloseScreen;
pScreenPriv->CreateColormap = pScreen->CreateColormap;
pScreenPriv->DestroyColormap = pScreen->DestroyColormap;
pScreenPriv->InstallColormap = pScreen->InstallColormap;
pScreenPriv->StoreColors = pScreen->StoreColors;
pScreen->CloseScreen = CMapCloseScreen;
pScreen->CreateColormap = CMapCreateColormap;
pScreen->DestroyColormap = CMapDestroyColormap;
pScreen->InstallColormap = CMapInstallColormap;
pScreen->StoreColors = CMapStoreColors;
pScreenPriv->pScrn = pScrn;
pScrn->LoadPalette = loadPalette;
pScrn->SetOverscan = setOverscan;
pScreenPriv->maxColors = maxColors;
pScreenPriv->sigRGBbits = sigRGBbits;
pScreenPriv->gammaElements = elements;
pScreenPriv->gamma = gamma;
pScreenPriv->PreAllocIndices = indices;
pScreenPriv->maps = NULL;
pScreenPriv->flags = flags;
pScreenPriv->isDGAmode = FALSE;
pScreenPriv->EnterVT = pScrn->EnterVT;
pScreenPriv->SwitchMode = pScrn->SwitchMode;
pScreenPriv->SetDGAMode = pScrn->SetDGAMode;
pScreenPriv->ChangeGamma = pScrn->ChangeGamma;
if (!(flags & CMAP_LOAD_EVEN_IF_OFFSCREEN)) {
pScrn->EnterVT = CMapEnterVT;
if ((flags & CMAP_RELOAD_ON_MODE_SWITCH) && pScrn->SwitchMode)
pScrn->SwitchMode = CMapSwitchMode;
}
#ifdef XFreeXDGA
pScrn->SetDGAMode = CMapSetDGAMode;
#endif
pScrn->ChangeGamma = CMapChangeGamma;
ComputeGamma(pScreenPriv);
/* get the default map */
dixLookupResourceByType((pointer *) &pDefMap, pScreen->defColormap,
RT_COLORMAP, serverClient, DixInstallAccess);
if (!CMapAllocateColormapPrivate(pDefMap)) {
CMapUnwrapScreen(pScreen);
return FALSE;
}
/* Force the initial map to be loaded */
SetInstalledmiColormap(pScreen, NULL);
CMapInstallColormap(pDefMap);
return TRUE;
}
/**** Screen functions ****/
static Bool
CMapCloseScreen(ScreenPtr pScreen)
{
CMapUnwrapScreen(pScreen);
return (*pScreen->CloseScreen) (pScreen);
}
static Bool
CMapColormapUseMax(VisualPtr pVisual, CMapScreenPtr pScreenPriv)
{
if (pVisual->nplanes > 16)
return TRUE;
return ((1 << pVisual->nplanes) > pScreenPriv->maxColors);
}
static Bool
CMapAllocateColormapPrivate(ColormapPtr pmap)
{
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pmap->pScreen->devPrivates,
CMapScreenKey);
CMapColormapPtr pColPriv;
CMapLinkPtr pLink;
int numColors;
LOCO *colors;
if (CMapColormapUseMax(pmap->pVisual, pScreenPriv))
numColors = pmap->pVisual->ColormapEntries;
else
numColors = 1 << pmap->pVisual->nplanes;
if (!(colors = malloc(numColors * sizeof(LOCO))))
return FALSE;
if (!(pColPriv = malloc(sizeof(CMapColormapRec)))) {
free(colors);
return FALSE;
}
dixSetPrivate(&pmap->devPrivates, CMapColormapKey, pColPriv);
pColPriv->numColors = numColors;
pColPriv->colors = colors;
pColPriv->recalculate = TRUE;
pColPriv->overscan = -1;
/* add map to list */
pLink = malloc(sizeof(CMapLink));
if (pLink) {
pLink->cmap = pmap;
pLink->next = pScreenPriv->maps;
pScreenPriv->maps = pLink;
}
return TRUE;
}
static Bool
CMapCreateColormap(ColormapPtr pmap)
{
ScreenPtr pScreen = pmap->pScreen;
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
Bool ret = FALSE;
pScreen->CreateColormap = pScreenPriv->CreateColormap;
if ((*pScreen->CreateColormap) (pmap)) {
if (CMapAllocateColormapPrivate(pmap))
ret = TRUE;
}
pScreen->CreateColormap = CMapCreateColormap;
return ret;
}
static void
CMapDestroyColormap(ColormapPtr cmap)
{
ScreenPtr pScreen = cmap->pScreen;
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
CMapColormapPtr pColPriv =
(CMapColormapPtr) dixLookupPrivate(&cmap->devPrivates, CMapColormapKey);
CMapLinkPtr prevLink = NULL, pLink = pScreenPriv->maps;
if (pColPriv) {
free(pColPriv->colors);
free(pColPriv);
}
/* remove map from list */
while (pLink) {
if (pLink->cmap == cmap) {
if (prevLink)
prevLink->next = pLink->next;
else
pScreenPriv->maps = pLink->next;
free(pLink);
break;
}
prevLink = pLink;
pLink = pLink->next;
}
if (pScreenPriv->DestroyColormap) {
pScreen->DestroyColormap = pScreenPriv->DestroyColormap;
(*pScreen->DestroyColormap) (cmap);
pScreen->DestroyColormap = CMapDestroyColormap;
}
}
static void
CMapStoreColors(ColormapPtr pmap, int ndef, xColorItem * pdefs)
{
ScreenPtr pScreen = pmap->pScreen;
VisualPtr pVisual = pmap->pVisual;
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
int *indices = pScreenPriv->PreAllocIndices;
int num = ndef;
/* At the moment this isn't necessary since there's nobody below us */
pScreen->StoreColors = pScreenPriv->StoreColors;
(*pScreen->StoreColors) (pmap, ndef, pdefs);
pScreen->StoreColors = CMapStoreColors;
/* should never get here for these */
if ((pVisual->class == TrueColor) ||
(pVisual->class == StaticColor) || (pVisual->class == StaticGray))
return;
if (pVisual->class == DirectColor) {
CMapColormapPtr pColPriv =
(CMapColormapPtr) dixLookupPrivate(&pmap->devPrivates,
CMapColormapKey);
int i;
if (CMapColormapUseMax(pVisual, pScreenPriv)) {
int index;
num = 0;
while (ndef--) {
if (pdefs[ndef].flags & DoRed) {
index = (pdefs[ndef].pixel & pVisual->redMask) >>
pVisual->offsetRed;
i = num;
while (i--)
if (indices[i] == index)
break;
if (i == -1)
indices[num++] = index;
}
if (pdefs[ndef].flags & DoGreen) {
index = (pdefs[ndef].pixel & pVisual->greenMask) >>
pVisual->offsetGreen;
i = num;
while (i--)
if (indices[i] == index)
break;
if (i == -1)
indices[num++] = index;
}
if (pdefs[ndef].flags & DoBlue) {
index = (pdefs[ndef].pixel & pVisual->blueMask) >>
pVisual->offsetBlue;
i = num;
while (i--)
if (indices[i] == index)
break;
if (i == -1)
indices[num++] = index;
}
}
}
else {
/* not really as overkill as it seems */
num = pColPriv->numColors;
for (i = 0; i < pColPriv->numColors; i++)
indices[i] = i;
}
}
else {
while (ndef--)
indices[ndef] = pdefs[ndef].pixel;
}
CMapRefreshColors(pmap, num, indices);
}
static void
CMapInstallColormap(ColormapPtr pmap)
{
ScreenPtr pScreen = pmap->pScreen;
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
if (pmap == GetInstalledmiColormap(pmap->pScreen))
return;
pScreen->InstallColormap = pScreenPriv->InstallColormap;
(*pScreen->InstallColormap) (pmap);
pScreen->InstallColormap = CMapInstallColormap;
/* Important. We let the lower layers, namely DGA,
overwrite the choice of Colormap to install */
if (GetInstalledmiColormap(pmap->pScreen))
pmap = GetInstalledmiColormap(pmap->pScreen);
if (!(pScreenPriv->flags & CMAP_PALETTED_TRUECOLOR) &&
(pmap->pVisual->class == TrueColor) &&
CMapColormapUseMax(pmap->pVisual, pScreenPriv))
return;
if (LOAD_PALETTE(pmap))
CMapReinstallMap(pmap);
}
/**** ScrnInfoRec functions ****/
static Bool
CMapEnterVT(ScrnInfoPtr pScrn)
{
ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
Bool ret;
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
pScrn->EnterVT = pScreenPriv->EnterVT;
ret = (*pScreenPriv->EnterVT) (pScrn);
pScreenPriv->EnterVT = pScrn->EnterVT;
pScrn->EnterVT = CMapEnterVT;
if (ret) {
if (GetInstalledmiColormap(pScreen))
CMapReinstallMap(GetInstalledmiColormap(pScreen));
return TRUE;
}
return FALSE;
}
static Bool
CMapSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
if ((*pScreenPriv->SwitchMode) (pScrn, mode)) {
if (GetInstalledmiColormap(pScreen))
CMapReinstallMap(GetInstalledmiColormap(pScreen));
return TRUE;
}
return FALSE;
}
#ifdef XFreeXDGA
static int
CMapSetDGAMode(ScrnInfoPtr pScrn, int num, DGADevicePtr dev)
{
ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
int ret;
ret = (*pScreenPriv->SetDGAMode) (pScrn, num, dev);
pScreenPriv->isDGAmode = DGAActive(pScrn->scrnIndex);
if (!pScreenPriv->isDGAmode && GetInstalledmiColormap(pScreen)
&& xf86ScreenToScrn(pScreen)->vtSema)
CMapReinstallMap(GetInstalledmiColormap(pScreen));
return ret;
}
#endif
/**** Utilities ****/
static void
CMapReinstallMap(ColormapPtr pmap)
{
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pmap->pScreen->devPrivates,
CMapScreenKey);
CMapColormapPtr cmapPriv =
(CMapColormapPtr) dixLookupPrivate(&pmap->devPrivates, CMapColormapKey);
ScrnInfoPtr pScrn = xf86ScreenToScrn(pmap->pScreen);
int i = cmapPriv->numColors;
int *indices = pScreenPriv->PreAllocIndices;
while (i--)
indices[i] = i;
if (cmapPriv->recalculate)
CMapRefreshColors(pmap, cmapPriv->numColors, indices);
else {
(*pScrn->LoadPalette) (pScrn, cmapPriv->numColors,
indices, cmapPriv->colors, pmap->pVisual);
if (pScrn->SetOverscan) {
#ifdef DEBUGOVERSCAN
ErrorF("SetOverscan() called from CMapReinstallMap\n");
#endif
pScrn->SetOverscan(pScrn, cmapPriv->overscan);
}
}
cmapPriv->recalculate = FALSE;
}
static void
CMapRefreshColors(ColormapPtr pmap, int defs, int *indices)
{
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pmap->pScreen->devPrivates,
CMapScreenKey);
CMapColormapPtr pColPriv =
(CMapColormapPtr) dixLookupPrivate(&pmap->devPrivates, CMapColormapKey);
VisualPtr pVisual = pmap->pVisual;
ScrnInfoPtr pScrn = xf86ScreenToScrn(pmap->pScreen);
int numColors, i;
LOCO *gamma, *colors;
EntryPtr entry;
int reds, greens, blues, maxValue, index, shift;
numColors = pColPriv->numColors;
shift = 16 - pScreenPriv->sigRGBbits;
maxValue = (1 << pScreenPriv->sigRGBbits) - 1;
gamma = pScreenPriv->gamma;
colors = pColPriv->colors;
reds = pVisual->redMask >> pVisual->offsetRed;
greens = pVisual->greenMask >> pVisual->offsetGreen;
blues = pVisual->blueMask >> pVisual->offsetBlue;
switch (pVisual->class) {
case StaticGray:
for (i = 0; i < numColors; i++) {
index = (i + 1) * maxValue / numColors;
colors[i].red = gamma[index].red;
colors[i].green = gamma[index].green;
colors[i].blue = gamma[index].blue;
}
break;
case TrueColor:
if (CMapColormapUseMax(pVisual, pScreenPriv)) {
for (i = 0; i <= reds; i++)
colors[i].red = gamma[i * maxValue / reds].red;
for (i = 0; i <= greens; i++)
colors[i].green = gamma[i * maxValue / greens].green;
for (i = 0; i <= blues; i++)
colors[i].blue = gamma[i * maxValue / blues].blue;
break;
}
for (i = 0; i < numColors; i++) {
colors[i].red = gamma[((i >> pVisual->offsetRed) & reds) *
maxValue / reds].red;
colors[i].green = gamma[((i >> pVisual->offsetGreen) & greens) *
maxValue / greens].green;
colors[i].blue = gamma[((i >> pVisual->offsetBlue) & blues) *
maxValue / blues].blue;
}
break;
case StaticColor:
case PseudoColor:
case GrayScale:
for (i = 0; i < defs; i++) {
index = indices[i];
entry = (EntryPtr) & pmap->red[index];
if (entry->fShared) {
colors[index].red =
gamma[entry->co.shco.red->color >> shift].red;
colors[index].green =
gamma[entry->co.shco.green->color >> shift].green;
colors[index].blue =
gamma[entry->co.shco.blue->color >> shift].blue;
}
else {
colors[index].red = gamma[entry->co.local.red >> shift].red;
colors[index].green =
gamma[entry->co.local.green >> shift].green;
colors[index].blue = gamma[entry->co.local.blue >> shift].blue;
}
}
break;
case DirectColor:
if (CMapColormapUseMax(pVisual, pScreenPriv)) {
for (i = 0; i < defs; i++) {
index = indices[i];
if (index <= reds)
colors[index].red =
gamma[pmap->red[index].co.local.red >> shift].red;
if (index <= greens)
colors[index].green =
gamma[pmap->green[index].co.local.green >> shift].green;
if (index <= blues)
colors[index].blue =
gamma[pmap->blue[index].co.local.blue >> shift].blue;
}
break;
}
for (i = 0; i < defs; i++) {
index = indices[i];
colors[index].red = gamma[pmap->red[(index >> pVisual->
offsetRed) & reds].co.local.
red >> shift].red;
colors[index].green =
gamma[pmap->green[(index >> pVisual->offsetGreen) & greens].co.
local.green >> shift].green;
colors[index].blue =
gamma[pmap->blue[(index >> pVisual->offsetBlue) & blues].co.
local.blue >> shift].blue;
}
break;
}
if (LOAD_PALETTE(pmap))
(*pScrn->LoadPalette) (pScreenPriv->pScrn, defs, indices,
colors, pmap->pVisual);
if (pScrn->SetOverscan)
CMapSetOverscan(pmap, defs, indices);
}
static Bool
CMapCompareColors(LOCO * color1, LOCO * color2)
{
/* return TRUE if the color1 is "closer" to black than color2 */
#ifdef DEBUGOVERSCAN
ErrorF("#%02x%02x%02x vs #%02x%02x%02x (%d vs %d)\n",
color1->red, color1->green, color1->blue,
color2->red, color2->green, color2->blue,
color1->red + color1->green + color1->blue,
color2->red + color2->green + color2->blue);
#endif
return (color1->red + color1->green + color1->blue <
color2->red + color2->green + color2->blue);
}
static void
CMapSetOverscan(ColormapPtr pmap, int defs, int *indices)
{
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pmap->pScreen->devPrivates,
CMapScreenKey);
CMapColormapPtr pColPriv =
(CMapColormapPtr) dixLookupPrivate(&pmap->devPrivates, CMapColormapKey);
ScrnInfoPtr pScrn = xf86ScreenToScrn(pmap->pScreen);
VisualPtr pVisual = pmap->pVisual;
int i;
LOCO *colors;
int index;
Bool newOverscan = FALSE;
int overscan, tmpOverscan;
colors = pColPriv->colors;
overscan = pColPriv->overscan;
/*
* Search for a new overscan index in the following cases:
*
* - The index hasn't yet been initialised.  In this case search
* for an index that is black or a close match to black.
*
* - The colour of the old index is changed. In this case search
* all indices for a black or close match to black.
*
* - The colour of the old index wasn't black. In this case only
* search the indices that were changed for a better match to black.
*/
switch (pVisual->class) {
case StaticGray:
case TrueColor:
/* Should only come here once. Initialise the overscan index to 0 */
overscan = 0;
newOverscan = TRUE;
break;
case StaticColor:
/*
* Only come here once, but search for the overscan in the same way
* as for the other cases.
*/
case DirectColor:
case PseudoColor:
case GrayScale:
if (overscan < 0 || overscan > pScreenPriv->maxColors - 1) {
/* Uninitialised */
newOverscan = TRUE;
}
else {
/* Check if the overscan was changed */
for (i = 0; i < defs; i++) {
index = indices[i];
if (index == overscan) {
newOverscan = TRUE;
break;
}
}
}
if (newOverscan) {
/* The overscan is either uninitialised or it has been changed */
if (overscan < 0 || overscan > pScreenPriv->maxColors - 1)
tmpOverscan = pScreenPriv->maxColors - 1;
else
tmpOverscan = overscan;
/* search all entries for a close match to black */
for (i = pScreenPriv->maxColors - 1; i >= 0; i--) {
if (colors[i].red == 0 && colors[i].green == 0 &&
colors[i].blue == 0) {
overscan = i;
#ifdef DEBUGOVERSCAN
ErrorF("Black found at index 0x%02x\n", i);
#endif
break;
}
else {
#ifdef DEBUGOVERSCAN
ErrorF("0x%02x: ", i);
#endif
if (CMapCompareColors(&colors[i], &colors[tmpOverscan])) {
tmpOverscan = i;
#ifdef DEBUGOVERSCAN
ErrorF("possible \"Black\" at index 0x%02x\n", i);
#endif
}
}
}
if (i < 0)
overscan = tmpOverscan;
}
else {
/* Check of the old overscan wasn't black */
if (colors[overscan].red != 0 || colors[overscan].green != 0 ||
colors[overscan].blue != 0) {
int oldOverscan = tmpOverscan = overscan;
/* See of there is now a better match */
for (i = 0; i < defs; i++) {
index = indices[i];
if (colors[index].red == 0 && colors[index].green == 0 &&
colors[index].blue == 0) {
overscan = index;
#ifdef DEBUGOVERSCAN
ErrorF("Black found at index 0x%02x\n", index);
#endif
break;
}
else {
#ifdef DEBUGOVERSCAN
ErrorF("0x%02x: ", index);
#endif
if (CMapCompareColors(&colors[index],
&colors[tmpOverscan])) {
tmpOverscan = index;
#ifdef DEBUGOVERSCAN
ErrorF("possible \"Black\" at index 0x%02x\n",
index);
#endif
}
}
}
if (i == defs)
overscan = tmpOverscan;
if (overscan != oldOverscan)
newOverscan = TRUE;
}
}
break;
}
if (newOverscan) {
pColPriv->overscan = overscan;
if (LOAD_PALETTE(pmap)) {
#ifdef DEBUGOVERSCAN
ErrorF("SetOverscan() called from CmapSetOverscan\n");
#endif
pScrn->SetOverscan(pScreenPriv->pScrn, overscan);
}
}
}
static void
CMapUnwrapScreen(ScreenPtr pScreen)
{
CMapScreenPtr pScreenPriv =
(CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates, CMapScreenKey);
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
pScreen->CloseScreen = pScreenPriv->CloseScreen;
pScreen->CreateColormap = pScreenPriv->CreateColormap;
pScreen->DestroyColormap = pScreenPriv->DestroyColormap;
pScreen->InstallColormap = pScreenPriv->InstallColormap;
pScreen->StoreColors = pScreenPriv->StoreColors;
pScrn->EnterVT = pScreenPriv->EnterVT;
pScrn->SwitchMode = pScreenPriv->SwitchMode;
pScrn->SetDGAMode = pScreenPriv->SetDGAMode;
pScrn->ChangeGamma = pScreenPriv->ChangeGamma;
free(pScreenPriv->gamma);
free(pScreenPriv->PreAllocIndices);
free(pScreenPriv);
}
static void
ComputeGamma(CMapScreenPtr priv)
{
int elements = priv->gammaElements - 1;
double RedGamma, GreenGamma, BlueGamma;
int i;
#ifndef DONT_CHECK_GAMMA
/* This check is to catch drivers that are not initialising pScrn->gamma */
if (priv->pScrn->gamma.red < GAMMA_MIN ||
priv->pScrn->gamma.red > GAMMA_MAX ||
priv->pScrn->gamma.green < GAMMA_MIN ||
priv->pScrn->gamma.green > GAMMA_MAX ||
priv->pScrn->gamma.blue < GAMMA_MIN ||
priv->pScrn->gamma.blue > GAMMA_MAX) {
xf86DrvMsgVerb(priv->pScrn->scrnIndex, X_WARNING, 0,
"The %s driver didn't call xf86SetGamma() to initialise\n"
"\tthe gamma values.\n", priv->pScrn->driverName);
xf86DrvMsgVerb(priv->pScrn->scrnIndex, X_WARNING, 0,
"PLEASE FIX THE `%s' DRIVER!\n",
priv->pScrn->driverName);
priv->pScrn->gamma.red = 1.0;
priv->pScrn->gamma.green = 1.0;
priv->pScrn->gamma.blue = 1.0;
}
#endif
RedGamma = 1.0 / (double) priv->pScrn->gamma.red;
GreenGamma = 1.0 / (double) priv->pScrn->gamma.green;
BlueGamma = 1.0 / (double) priv->pScrn->gamma.blue;
for (i = 0; i <= elements; i++) {
if (RedGamma == 1.0)
priv->gamma[i].red = i;
else
priv->gamma[i].red = (CARD16) (pow((double) i / (double) elements,
RedGamma) * (double) elements +
0.5);
if (GreenGamma == 1.0)
priv->gamma[i].green = i;
else
priv->gamma[i].green = (CARD16) (pow((double) i / (double) elements,
GreenGamma) *
(double) elements + 0.5);
if (BlueGamma == 1.0)
priv->gamma[i].blue = i;
else
priv->gamma[i].blue = (CARD16) (pow((double) i / (double) elements,
BlueGamma) * (double) elements +
0.5);
}
}
int
CMapChangeGamma(ScrnInfoPtr pScrn, Gamma gamma)
{
int ret = Success;
ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
CMapColormapPtr pColPriv;
CMapScreenPtr pScreenPriv;
CMapLinkPtr pLink;
/* Is this sufficient checking ? */
if (!CMapScreenKeyRegistered)
return BadImplementation;
pScreenPriv = (CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates,
CMapScreenKey);
if (!pScreenPriv)
return BadImplementation;
if (gamma.red < GAMMA_MIN || gamma.red > GAMMA_MAX ||
gamma.green < GAMMA_MIN || gamma.green > GAMMA_MAX ||
gamma.blue < GAMMA_MIN || gamma.blue > GAMMA_MAX)
return BadValue;
pScrn->gamma.red = gamma.red;
pScrn->gamma.green = gamma.green;
pScrn->gamma.blue = gamma.blue;
ComputeGamma(pScreenPriv);
/* mark all colormaps on this screen */
pLink = pScreenPriv->maps;
while (pLink) {
pColPriv = (CMapColormapPtr) dixLookupPrivate(&pLink->cmap->devPrivates,
CMapColormapKey);
pColPriv->recalculate = TRUE;
pLink = pLink->next;
}
if (GetInstalledmiColormap(pScreen) &&
((pScreenPriv->flags & CMAP_LOAD_EVEN_IF_OFFSCREEN) ||
pScrn->vtSema || pScreenPriv->isDGAmode)) {
ColormapPtr pMap = GetInstalledmiColormap(pScreen);
if (!(pScreenPriv->flags & CMAP_PALETTED_TRUECOLOR) &&
(pMap->pVisual->class == TrueColor) &&
CMapColormapUseMax(pMap->pVisual, pScreenPriv)) {
/* if the current map doesn't have a palette look
for another map to change the gamma on. */
pLink = pScreenPriv->maps;
while (pLink) {
if (pLink->cmap->pVisual->class == PseudoColor)
break;
pLink = pLink->next;
}
if (pLink) {
/* need to trick CMapRefreshColors() into thinking
this is the currently installed map */
SetInstalledmiColormap(pScreen, pLink->cmap);
CMapReinstallMap(pLink->cmap);
SetInstalledmiColormap(pScreen, pMap);
}
}
else
CMapReinstallMap(pMap);
}
pScrn->ChangeGamma = pScreenPriv->ChangeGamma;
if (pScrn->ChangeGamma)
ret = pScrn->ChangeGamma(pScrn, gamma);
pScrn->ChangeGamma = CMapChangeGamma;
return ret;
}
static void
ComputeGammaRamp(CMapScreenPtr priv,
unsigned short *red,
unsigned short *green, unsigned short *blue)
{
int elements = priv->gammaElements;
LOCO *entry = priv->gamma;
int shift = 16 - priv->sigRGBbits;
while (elements--) {
entry->red = *(red++) >> shift;
entry->green = *(green++) >> shift;
entry->blue = *(blue++) >> shift;
entry++;
}
}
int
xf86ChangeGammaRamp(ScreenPtr pScreen,
int size,
unsigned short *red,
unsigned short *green, unsigned short *blue)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
CMapColormapPtr pColPriv;
CMapScreenPtr pScreenPriv;
CMapLinkPtr pLink;
if (xf86_crtc_supports_gamma(pScrn)) {
RRCrtcPtr crtc = xf86CompatRRCrtc(pScrn);
if (crtc) {
if (crtc->gammaSize != size)
return BadValue;
RRCrtcGammaSet(crtc, red, green, blue);
return Success;
}
}
if (!CMapScreenKeyRegistered)
return BadImplementation;
pScreenPriv = (CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates,
CMapScreenKey);
if (!pScreenPriv)
return BadImplementation;
if (pScreenPriv->gammaElements != size)
return BadValue;
ComputeGammaRamp(pScreenPriv, red, green, blue);
/* mark all colormaps on this screen */
pLink = pScreenPriv->maps;
while (pLink) {
pColPriv = (CMapColormapPtr) dixLookupPrivate(&pLink->cmap->devPrivates,
CMapColormapKey);
pColPriv->recalculate = TRUE;
pLink = pLink->next;
}
if (GetInstalledmiColormap(pScreen) &&
((pScreenPriv->flags & CMAP_LOAD_EVEN_IF_OFFSCREEN) ||
pScrn->vtSema || pScreenPriv->isDGAmode)) {
ColormapPtr pMap = GetInstalledmiColormap(pScreen);
if (!(pScreenPriv->flags & CMAP_PALETTED_TRUECOLOR) &&
(pMap->pVisual->class == TrueColor) &&
CMapColormapUseMax(pMap->pVisual, pScreenPriv)) {
/* if the current map doesn't have a palette look
for another map to change the gamma on. */
pLink = pScreenPriv->maps;
while (pLink) {
if (pLink->cmap->pVisual->class == PseudoColor)
break;
pLink = pLink->next;
}
if (pLink) {
/* need to trick CMapRefreshColors() into thinking
this is the currently installed map */
SetInstalledmiColormap(pScreen, pLink->cmap);
CMapReinstallMap(pLink->cmap);
SetInstalledmiColormap(pScreen, pMap);
}
}
else
CMapReinstallMap(pMap);
}
return Success;
}
int
xf86GetGammaRampSize(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
CMapScreenPtr pScreenPriv;
if (xf86_crtc_supports_gamma(pScrn)) {
RRCrtcPtr crtc = xf86CompatRRCrtc(pScrn);
if (crtc)
return crtc->gammaSize;
}
if (!CMapScreenKeyRegistered)
return 0;
pScreenPriv = (CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates,
CMapScreenKey);
if (!pScreenPriv)
return 0;
return pScreenPriv->gammaElements;
}
int
xf86GetGammaRamp(ScreenPtr pScreen,
int size,
unsigned short *red,
unsigned short *green, unsigned short *blue)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
CMapScreenPtr pScreenPriv;
LOCO *entry;
int shift, sigbits;
if (xf86_crtc_supports_gamma(pScrn)) {
RRCrtcPtr crtc = xf86CompatRRCrtc(pScrn);
if (crtc) {
if (crtc->gammaSize < size)
return BadValue;
if (!RRCrtcGammaGet(crtc))
return BadImplementation;
memcpy(red, crtc->gammaRed, size * sizeof(*red));
memcpy(green, crtc->gammaGreen, size * sizeof(*green));
memcpy(blue, crtc->gammaBlue, size * sizeof(*blue));
return Success;
}
}
if (!CMapScreenKeyRegistered)
return BadImplementation;
pScreenPriv = (CMapScreenPtr) dixLookupPrivate(&pScreen->devPrivates,
CMapScreenKey);
if (!pScreenPriv)
return BadImplementation;
if (size > pScreenPriv->gammaElements)
return BadValue;
entry = pScreenPriv->gamma;
sigbits = pScreenPriv->sigRGBbits;
while (size--) {
*red = entry->red << (16 - sigbits);
*green = entry->green << (16 - sigbits);
*blue = entry->blue << (16 - sigbits);
shift = sigbits;
while (shift < 16) {
*red |= *red >> shift;
*green |= *green >> shift;
*blue |= *blue >> shift;
shift += sigbits;
}
red++;
green++;
blue++;
entry++;
}
return Success;
}
int
xf86ChangeGamma(ScreenPtr pScreen, Gamma gamma)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
if (pScrn->ChangeGamma)
return (*pScrn->ChangeGamma) (pScrn, gamma);
return BadImplementation;
}