Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
11 KiB

  1. /******************************Module*Header*******************************\
  2. *
  3. * *******************
  4. * * GDI SAMPLE CODE *
  5. * *******************
  6. *
  7. * Module Name: palette.c
  8. *
  9. * Palette support.
  10. *
  11. * Copyright (c) 1992-1998 Microsoft Corporation
  12. \**************************************************************************/
  13. #include "precomp.h"
  14. // Global Table defining the 20 Window default colours. For 256 colour
  15. // palettes the first 10 must be put at the beginning of the palette
  16. // and the last 10 at the end of the palette.
  17. PALETTEENTRY gapalBase[20] =
  18. {
  19. { 0, 0, 0, 0 }, // 0
  20. { 0x80,0, 0, 0 }, // 1
  21. { 0, 0x80,0, 0 }, // 2
  22. { 0x80,0x80,0, 0 }, // 3
  23. { 0, 0, 0x80,0 }, // 4
  24. { 0x80,0, 0x80,0 }, // 5
  25. { 0, 0x80,0x80,0 }, // 6
  26. { 0xC0,0xC0,0xC0,0 }, // 7
  27. { 192, 220, 192, 0 }, // 8
  28. { 166, 202, 240, 0 }, // 9
  29. { 255, 251, 240, 0 }, // 10
  30. { 160, 160, 164, 0 }, // 11
  31. { 0x80,0x80,0x80,0 }, // 12
  32. { 0xFF,0, 0 ,0 }, // 13
  33. { 0, 0xFF,0 ,0 }, // 14
  34. { 0xFF,0xFF,0 ,0 }, // 15
  35. { 0 ,0, 0xFF,0 }, // 16
  36. { 0xFF,0, 0xFF,0 }, // 17
  37. { 0, 0xFF,0xFF,0 }, // 18
  38. { 0xFF,0xFF,0xFF,0 }, // 19
  39. };
  40. /******************************Public*Routine******************************\
  41. * BOOL bInitializePalette
  42. *
  43. * Initializes default palette for PDEV.
  44. *
  45. \**************************************************************************/
  46. BOOL bInitializePalette(
  47. PDEV* ppdev,
  48. DEVINFO* pdi)
  49. {
  50. PALETTEENTRY* ppal;
  51. PALETTEENTRY* ppalTmp;
  52. ULONG ulLoop;
  53. HPALETTE hpal;
  54. if (ppdev->iBitmapFormat == BMF_8BPP)
  55. {
  56. // Allocate our palette:
  57. ppal = EngAllocMem(FL_ZERO_MEMORY, sizeof(PALETTEENTRY) * 256, ALLOC_TAG);
  58. if (ppal == NULL)
  59. goto ReturnFalse;
  60. ppdev->pPal = ppal;
  61. // Fill in Windows reserved colours from the WIN 3.0 DDK
  62. // The Window Manager reserved the first and last 10 colours for
  63. // painting windows borders and for non-palette managed applications.
  64. for (ulLoop = 0; ulLoop < 10; ulLoop++)
  65. {
  66. // First 10
  67. ppal[ulLoop] = gapalBase[ulLoop];
  68. // Last 10
  69. ppal[246 + ulLoop] = gapalBase[ulLoop+10];
  70. }
  71. // Create handle for palette.
  72. hpal = EngCreatePalette(PAL_INDEXED, 256, (ULONG*) ppal, 0, 0, 0);
  73. }
  74. else
  75. {
  76. ASSERTDD((ppdev->iBitmapFormat == BMF_16BPP) ||
  77. (ppdev->iBitmapFormat == BMF_24BPP) ||
  78. (ppdev->iBitmapFormat == BMF_32BPP),
  79. "This case handles only 16, 24 or 32bpp");
  80. hpal = EngCreatePalette(PAL_BITFIELDS, 0, NULL,
  81. ppdev->flRed, ppdev->flGreen, ppdev->flBlue);
  82. }
  83. ppdev->hpalDefault = hpal;
  84. pdi->hpalDefault = hpal;
  85. if (hpal == 0)
  86. goto ReturnFalse;
  87. return(TRUE);
  88. ReturnFalse:
  89. DISPDBG((0, "Failed bInitializePalette"));
  90. return(FALSE);
  91. }
  92. /******************************Public*Routine******************************\
  93. * VOID vUninitializePalette
  94. *
  95. * Frees resources allocated by bInitializePalette.
  96. *
  97. * Note: In an error case, this may be called before bInitializePalette.
  98. *
  99. \**************************************************************************/
  100. VOID vUninitializePalette(PDEV* ppdev)
  101. {
  102. // Delete the default palette if we created one:
  103. if (ppdev->hpalDefault != 0)
  104. EngDeletePalette(ppdev->hpalDefault);
  105. if (ppdev->pPal != (PALETTEENTRY*) NULL)
  106. EngFreeMem(ppdev->pPal);
  107. }
  108. /******************************Public*Routine******************************\
  109. * BOOL bEnablePalette
  110. *
  111. * Initialize the hardware's 8bpp palette registers.
  112. *
  113. \**************************************************************************/
  114. BOOL bEnablePalette(PDEV* ppdev)
  115. {
  116. BYTE ajClutSpace[MAX_CLUT_SIZE];
  117. PVIDEO_CLUT pScreenClut;
  118. ULONG ulReturnedDataLength;
  119. ULONG cColors;
  120. PVIDEO_CLUTDATA pScreenClutData;
  121. if (ppdev->iBitmapFormat == BMF_8BPP)
  122. {
  123. // Fill in pScreenClut header info:
  124. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  125. pScreenClut->NumEntries = 256;
  126. pScreenClut->FirstEntry = 0;
  127. // Copy colours in:
  128. cColors = 256;
  129. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  130. while(cColors--)
  131. {
  132. pScreenClutData[cColors].Red = ppdev->pPal[cColors].peRed >>
  133. ppdev->cPaletteShift;
  134. pScreenClutData[cColors].Green = ppdev->pPal[cColors].peGreen >>
  135. ppdev->cPaletteShift;
  136. pScreenClutData[cColors].Blue = ppdev->pPal[cColors].peBlue >>
  137. ppdev->cPaletteShift;
  138. pScreenClutData[cColors].Unused = 0;
  139. }
  140. // Set palette registers:
  141. if (EngDeviceIoControl(ppdev->hDriver,
  142. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  143. pScreenClut,
  144. MAX_CLUT_SIZE,
  145. NULL,
  146. 0,
  147. &ulReturnedDataLength))
  148. {
  149. DISPDBG((0, "Failed bEnablePalette"));
  150. return(FALSE);
  151. }
  152. }
  153. DISPDBG((5, "Passed bEnablePalette"));
  154. return(TRUE);
  155. }
  156. /******************************Public*Routine******************************\
  157. * VOID vDisablePalette
  158. *
  159. * Undoes anything done in bEnablePalette.
  160. *
  161. \**************************************************************************/
  162. VOID vDisablePalette(
  163. PDEV* ppdev)
  164. {
  165. // Nothin' to do
  166. }
  167. /******************************Public*Routine******************************\
  168. * VOID vAssertModePalette
  169. *
  170. * Sets/resets the palette in preparation for full-screen/graphics mode.
  171. *
  172. \**************************************************************************/
  173. VOID vAssertModePalette(
  174. PDEV* ppdev,
  175. BOOL bEnable)
  176. {
  177. // USER immediately calls DrvSetPalette after switching out of
  178. // full-screen, so we don't have to worry about resetting the
  179. // palette here.
  180. }
  181. /******************************Public*Routine******************************\
  182. * BOOL DrvSetPalette
  183. *
  184. * DDI entry point for manipulating the palette.
  185. *
  186. \**************************************************************************/
  187. BOOL DrvSetPalette(
  188. DHPDEV dhpdev,
  189. PALOBJ* ppalo,
  190. FLONG fl,
  191. ULONG iStart,
  192. ULONG cColors)
  193. {
  194. BYTE ajClutSpace[MAX_CLUT_SIZE];
  195. PVIDEO_CLUT pScreenClut;
  196. PVIDEO_CLUTDATA pScreenClutData;
  197. PDEV* ppdev;
  198. UNREFERENCED_PARAMETER(fl);
  199. ppdev = (PDEV*) dhpdev;
  200. // Fill in pScreenClut header info:
  201. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  202. pScreenClut->NumEntries = (USHORT) cColors;
  203. pScreenClut->FirstEntry = (USHORT) iStart;
  204. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  205. if (cColors != PALOBJ_cGetColors(ppalo, iStart, cColors,
  206. (ULONG*) pScreenClutData))
  207. {
  208. DISPDBG((0, "DrvSetPalette failed PALOBJ_cGetColors"));
  209. return (FALSE);
  210. }
  211. // Set the high reserved byte in each palette entry to 0.
  212. // Do the appropriate palette shifting to fit in the DAC.
  213. if (ppdev->cPaletteShift)
  214. {
  215. while(cColors--)
  216. {
  217. pScreenClutData[cColors].Red >>= ppdev->cPaletteShift;
  218. pScreenClutData[cColors].Green >>= ppdev->cPaletteShift;
  219. pScreenClutData[cColors].Blue >>= ppdev->cPaletteShift;
  220. pScreenClutData[cColors].Unused = 0;
  221. }
  222. }
  223. else
  224. {
  225. while(cColors--)
  226. {
  227. pScreenClutData[cColors].Unused = 0;
  228. }
  229. }
  230. // Set palette registers
  231. if (EngDeviceIoControl(ppdev->hDriver,
  232. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  233. pScreenClut,
  234. MAX_CLUT_SIZE,
  235. NULL,
  236. 0,
  237. &cColors))
  238. {
  239. DISPDBG((0, "DrvSetPalette failed SET_COLOR_REGISTERS"));
  240. return (FALSE);
  241. }
  242. return(TRUE);
  243. }
  244. /******************************Public*Routine******************************\
  245. * BOOL DrvIcmSetDeviceGammaRamp
  246. *
  247. * DDI entry point for manipulating the device gamma ramp.
  248. *
  249. * Note that GCAPS2_CHANGEGAMMARAMP has to be set for this to be called.
  250. * Don't set GCAPS2_CHANGEGAMMARAMP when running 8bpp, though!
  251. *
  252. * (Note that we currently aren't hooking this call because none of the
  253. * S3 hardware supports it!)
  254. *
  255. \**************************************************************************/
  256. BOOL DrvIcmSetDeviceGammaRamp(
  257. DHPDEV dhpdev,
  258. ULONG iFormat,
  259. PVOID lpRamp)
  260. {
  261. BYTE ajClutSpace[MAX_CLUT_SIZE];
  262. PVIDEO_CLUT pScreenClut;
  263. PVIDEO_CLUTDATA pScreenClutData;
  264. PGAMMARAMP pGammaRamp;
  265. PDEV* ppdev;
  266. ULONG i;
  267. DWORD cColors;
  268. BYTE* pjIoBase;
  269. BYTE jMode;
  270. BYTE jDacControl;
  271. ppdev = (PDEV*) dhpdev;
  272. pjIoBase = ppdev->pjIoBase;
  273. // Fill in pScreenClut header info:
  274. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  275. pScreenClut->NumEntries = (USHORT) 256;
  276. pScreenClut->FirstEntry = (USHORT) 0;
  277. pScreenClutData = (PVIDEO_CLUTDATA) (&(pScreenClut->LookupTable[0]));
  278. pGammaRamp = lpRamp;
  279. if (iFormat == IGRF_RGB_256WORDS)
  280. {
  281. for (i = 0; i < 256; i++)
  282. {
  283. // The gamma-ramp colors are given to us in an 8.8 fixed
  284. // point format. Since our DAC has only 8 bits of color
  285. // precision, we get rid of the fractional part by simple
  286. // truncation.
  287. pScreenClutData[i].Red = pGammaRamp->Red[i] >> 8;
  288. pScreenClutData[i].Green = pGammaRamp->Green[i] >> 8;
  289. pScreenClutData[i].Blue = pGammaRamp->Blue[i] >> 8;
  290. pScreenClutData[i].Unused = 0;
  291. }
  292. // Set palette registers:
  293. if (EngDeviceIoControl(ppdev->hDriver,
  294. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  295. pScreenClut,
  296. MAX_CLUT_SIZE,
  297. NULL,
  298. 0,
  299. &cColors))
  300. {
  301. DISPDBG((0, "DrvIcmSetDeviceGammaRamp failed SET_COLOR_REGISTERS"));
  302. return (FALSE);
  303. }
  304. }
  305. return(TRUE);
  306. }