Source code of Windows XP (NT5)
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.

383 lines
11 KiB

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