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.

593 lines
15 KiB

  1. /******************************************************************************\
  2. *
  3. * $Workfile: palette.c $
  4. *
  5. * Palette support.
  6. *
  7. * Copyright (c) 1992-1995 Microsoft Corporation
  8. * Copyright (c) 1996 Cirrus Logic, Inc.
  9. *
  10. * $Log: S:/projects/drivers/ntsrc/display/palette.c_v $
  11. *
  12. * Rev 1.2 18 Dec 1996 13:44:10 PLCHU
  13. *
  14. *
  15. * Rev 1.1 Oct 10 1996 15:38:38 unknown
  16. *
  17. *
  18. * Rev 1.1 12 Aug 1996 16:54:26 frido
  19. *
  20. * Added NT 3.5x/4.0 auto detection.
  21. *
  22. * chu01 12-16-96 Enable color correction
  23. * myf29 02-12-97 Support Gamma Correction for 755x
  24. *
  25. \******************************************************************************/
  26. #include "precomp.h"
  27. //
  28. // chu01
  29. //
  30. #ifdef GAMMACORRECT
  31. static BOOL bGammaInit = FALSE ;
  32. BOOL bInitGlobalDefPaletteTable = FALSE ;
  33. //
  34. // gamma facter for All, Blue, Green, Red
  35. // contrast facter for All, Blue, Green, Red
  36. //
  37. extern PGAMMA_VALUE GammaFactor ;
  38. extern PCONTRAST_VALUE ContrastFactor ;
  39. #endif // GAMMACORRECT
  40. // Global Table defining the 20 Window default colours. For 256 colour
  41. // palettes the first 10 must be put at the beginning of the palette
  42. // and the last 10 at the end of the palette.
  43. PALETTEENTRY gapalBase[20] =
  44. {
  45. { 0, 0, 0, 0 }, // 0
  46. { 0x80,0, 0, 0 }, // 1
  47. { 0, 0x80,0, 0 }, // 2
  48. { 0x80,0x80,0, 0 }, // 3
  49. { 0, 0, 0x80,0 }, // 4
  50. { 0x80,0, 0x80,0 }, // 5
  51. { 0, 0x80,0x80,0 }, // 6
  52. { 0xC0,0xC0,0xC0,0 }, // 7
  53. { 192, 220, 192, 0 }, // 8
  54. { 166, 202, 240, 0 }, // 9
  55. { 255, 251, 240, 0 }, // 10
  56. { 160, 160, 164, 0 }, // 11
  57. { 0x80,0x80,0x80,0 }, // 12
  58. { 0xFF,0, 0 ,0 }, // 13
  59. { 0, 0xFF,0 ,0 }, // 14
  60. { 0xFF,0xFF,0 ,0 }, // 15
  61. { 0 ,0, 0xFF,0 }, // 16
  62. { 0xFF,0, 0xFF,0 }, // 17
  63. { 0, 0xFF,0xFF,0 }, // 18
  64. { 0xFF,0xFF,0xFF,0 }, // 19
  65. };
  66. /******************************Public*Routine******************************\
  67. * BOOL bInitializePalette
  68. *
  69. * Initializes default palette for PDEV.
  70. *
  71. \**************************************************************************/
  72. BOOL bInitializePalette(
  73. PDEV* ppdev,
  74. DEVINFO* pdi)
  75. {
  76. PALETTEENTRY* ppal;
  77. PALETTEENTRY* ppalTmp;
  78. ULONG ulLoop;
  79. BYTE jRed;
  80. BYTE jGre;
  81. BYTE jBlu;
  82. HPALETTE hpal;
  83. //
  84. // chu01
  85. //
  86. #ifdef GAMMACORRECT
  87. PALETTEENTRY* pGpal;
  88. PALETTEENTRY* ppalFrom;
  89. PALETTEENTRY* ppalTo;
  90. PALETTEENTRY* ppalEnd;
  91. int i;
  92. #endif // GAMMACORRECT
  93. DISPDBG((2, "bInitializePalette"));
  94. //
  95. // chu01 : Sorry VideoPort_xxxx functions do not work here.
  96. //
  97. #ifdef GAMMACORRECT
  98. if ((ppdev->iBitmapFormat == BMF_8BPP) ||
  99. (ppdev->ulChipID == 0x40) || //myf29
  100. (ppdev->ulChipID == 0x4C) || //myf29
  101. (ppdev->ulChipID == 0xBC))
  102. #else
  103. if (ppdev->iBitmapFormat == BMF_8BPP)
  104. #endif // GAMMACORRECT
  105. {
  106. // Allocate our palette:
  107. ppal = (PALETTEENTRY*) ALLOC(sizeof(PALETTEENTRY) * 256);
  108. if (ppal == NULL)
  109. goto ReturnFalse;
  110. ppdev->pPal = ppal;
  111. //
  112. // chu01
  113. //
  114. #ifdef GAMMACORRECT
  115. pGpal = (PALETTEENTRY*) ALLOC(sizeof(PALETTEENTRY) * 256) ;
  116. if (pGpal == NULL)
  117. goto ReturnFalse ;
  118. ppdev->pCurrentPalette = pGpal ;
  119. #endif // GAMMACORRECT
  120. // Generate 256 (8*4*4) RGB combinations to fill the palette
  121. jRed = 0;
  122. jGre = 0;
  123. jBlu = 0;
  124. ppalTmp = ppal;
  125. //
  126. // chu01
  127. //
  128. #ifdef GAMMACORRECT
  129. if ((ppdev->ulChipID == 0xBC) ||
  130. (ppdev->ulChipID == 0x40) || //myf29
  131. (ppdev->ulChipID == 0x4C)) //myf29
  132. {
  133. if ((ppdev->iBitmapFormat == BMF_16BPP) ||
  134. (ppdev->iBitmapFormat == BMF_24BPP))
  135. {
  136. i = 0 ;
  137. for (ulLoop = 256; ulLoop != 0; ulLoop--)
  138. {
  139. ppalTmp->peRed = i ;
  140. ppalTmp->peGreen = i ;
  141. ppalTmp->peBlue = i ;
  142. ppalTmp->peFlags = 0 ;
  143. ppalTmp++;
  144. i++ ;
  145. }
  146. goto bInitPal ;
  147. }
  148. }
  149. #endif // GAMMACORRECT
  150. for (ulLoop = 256; ulLoop != 0; ulLoop--)
  151. {
  152. ppalTmp->peRed = jRed;
  153. ppalTmp->peGreen = jGre;
  154. ppalTmp->peBlue = jBlu;
  155. ppalTmp->peFlags = 0;
  156. ppalTmp++;
  157. if (!(jRed += 32))
  158. if (!(jGre += 32))
  159. jBlu += 64;
  160. }
  161. // Fill in Windows reserved colours from the WIN 3.0 DDK
  162. // The Window Manager reserved the first and last 10 colours for
  163. // painting windows borders and for non-palette managed applications.
  164. for (ulLoop = 0; ulLoop < 10; ulLoop++)
  165. {
  166. // First 10
  167. ppal[ulLoop] = gapalBase[ulLoop];
  168. // Last 10
  169. ppal[246 + ulLoop] = gapalBase[ulLoop+10];
  170. }
  171. //
  172. // chu01
  173. //
  174. #ifdef GAMMACORRECT
  175. bInitPal:
  176. if (!bInitGlobalDefPaletteTable)
  177. {
  178. ppalFrom = (PALETTEENTRY*) ppal ;
  179. ppalTo = ppdev->pCurrentPalette ;
  180. ppalEnd = &ppalTo[256] ;
  181. for (; ppalTo < ppalEnd; ppalFrom++, ppalTo++)
  182. {
  183. ppalTo->peRed = ppalFrom->peRed ;
  184. ppalTo->peGreen = ppalFrom->peGreen ;
  185. ppalTo->peBlue = ppalFrom->peBlue ;
  186. ppalTo->peFlags = 0 ;
  187. }
  188. bInitGlobalDefPaletteTable = TRUE ;
  189. }
  190. #endif // GAMMACORRECT
  191. // Create handle for palette.
  192. #ifndef GAMMACORRECT
  193. hpal = EngCreatePalette(PAL_INDEXED, 256, (ULONG*) ppal, 0, 0, 0);
  194. #else
  195. if (ppdev->iBitmapFormat == BMF_8BPP)
  196. hpal = EngCreatePalette(PAL_INDEXED, 256, (ULONG*) ppal, 0, 0, 0);
  197. else
  198. hpal = EngCreatePalette(PAL_BITFIELDS, 0, NULL,
  199. ppdev->flRed, ppdev->flGreen, ppdev->flBlue);
  200. #endif // !GAMMACORRECT
  201. }
  202. else
  203. {
  204. ASSERTDD((ppdev->iBitmapFormat == BMF_16BPP) ||
  205. (ppdev->iBitmapFormat == BMF_24BPP) ||
  206. (ppdev->iBitmapFormat == BMF_32BPP),
  207. "This case handles only 16, 24 or 32bpp");
  208. hpal = EngCreatePalette(PAL_BITFIELDS, 0, NULL,
  209. ppdev->flRed, ppdev->flGreen, ppdev->flBlue);
  210. }
  211. ppdev->hpalDefault = hpal;
  212. pdi->hpalDefault = hpal;
  213. if (hpal == 0)
  214. goto ReturnFalse;
  215. return(TRUE);
  216. ReturnFalse:
  217. DISPDBG((0, "Failed bInitializePalette"));
  218. return(FALSE);
  219. }
  220. /******************************Public*Routine******************************\
  221. * VOID vUninitializePalette
  222. *
  223. * Frees resources allocated by bInitializePalette.
  224. *
  225. * Note: In an error case, this may be called before bInitializePalette.
  226. *
  227. \**************************************************************************/
  228. VOID vUninitializePalette(PDEV* ppdev)
  229. {
  230. // Delete the default palette if we created one:
  231. if (ppdev->hpalDefault != 0)
  232. EngDeletePalette(ppdev->hpalDefault);
  233. if (ppdev->pPal != (PALETTEENTRY*) NULL)
  234. FREE(ppdev->pPal);
  235. }
  236. /******************************Public*Routine******************************\
  237. * BOOL bEnablePalette
  238. *
  239. * Initialize the hardware's 8bpp palette registers.
  240. *
  241. \**************************************************************************/
  242. BOOL bEnablePalette(PDEV* ppdev)
  243. {
  244. BYTE ajClutSpace[MAX_CLUT_SIZE];
  245. PVIDEO_CLUT pScreenClut;
  246. ULONG ulReturnedDataLength;
  247. PALETTEENTRY* ppalFrom;
  248. PALETTEENTRY* ppalTo;
  249. PALETTEENTRY* ppalEnd;
  250. //
  251. // chu01
  252. //
  253. #ifdef GAMMACORRECT
  254. PALETTEENTRY* ppalSrc;
  255. PALETTEENTRY* ppalDest;
  256. PALETTEENTRY* ppalLength;
  257. BOOL status; //myf29
  258. #endif // GAMMACORRECT
  259. DISPDBG((2, "bEnablePalette"));
  260. //
  261. // chu01
  262. //
  263. #ifdef GAMMACORRECT
  264. if ((ppdev->iBitmapFormat == BMF_8BPP) ||
  265. (ppdev->ulChipID == 0x40) || //myf29
  266. (ppdev->ulChipID == 0x4C) || //myf29
  267. (ppdev->ulChipID == 0xBC))
  268. #else
  269. if (ppdev->iBitmapFormat == BMF_8BPP)
  270. #endif // GAMMACORRECT
  271. {
  272. // Fill in pScreenClut header info:
  273. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  274. pScreenClut->NumEntries = 256;
  275. pScreenClut->FirstEntry = 0;
  276. // Copy Colors in.
  277. ppalFrom = ppdev->pPal;
  278. ppalTo = (PALETTEENTRY*) pScreenClut->LookupTable;
  279. ppalEnd = &ppalTo[256];
  280. for (; ppalTo < ppalEnd; ppalFrom++, ppalTo++)
  281. {
  282. //
  283. // chu01
  284. //
  285. #ifndef GAMMACORRECT
  286. ppalTo->peRed = ppalFrom->peRed >> 2 ;
  287. ppalTo->peGreen = ppalFrom->peGreen >> 2 ;
  288. ppalTo->peBlue = ppalFrom->peBlue >> 2 ;
  289. ppalTo->peFlags = 0 ;
  290. #else
  291. ppalTo->peRed = ppalFrom->peRed ;
  292. ppalTo->peGreen = ppalFrom->peGreen ;
  293. ppalTo->peBlue = ppalFrom->peBlue ;
  294. ppalTo->peFlags = 0 ;
  295. #endif // !GAMMACORRECT
  296. }
  297. //
  298. // chu01
  299. //
  300. #ifdef GAMMACORRECT
  301. if (!bGammaInit)
  302. {
  303. if (ppdev->flCaps & CAPS_GAMMA_CORRECT)
  304. {
  305. //
  306. // Get Gamma factor from registry
  307. //
  308. if (!IOCONTROL(ppdev->hDriver,
  309. IOCTL_CIRRUS_GET_GAMMA_FACTOR,
  310. NULL, ,
  311. 0,
  312. &GammaFactor,
  313. sizeof(GammaFactor),
  314. &ulReturnedDataLength))
  315. {
  316. DISPDBG((0, "Failed to access Gamma factor from registry"));
  317. }
  318. else
  319. {
  320. DISPDBG((0, "G Gamma = %lx", GammaFactor)) ;
  321. }
  322. //
  323. // Get Contrast factor from registry
  324. //
  325. if (!IOCONTROL(ppdev->hDriver,
  326. IOCTL_CIRRUS_GET_CONTRAST_FACTOR,
  327. NULL, ,
  328. 0,
  329. &ContrastFactor,
  330. sizeof(ContrastFactor),
  331. &ulReturnedDataLength))
  332. {
  333. DISPDBG((0, "Failed to access Contrast factor from registry"));
  334. }
  335. else
  336. {
  337. DISPDBG((0, "G Contrast = %lx", ContrastFactor)) ;
  338. }
  339. }
  340. else
  341. {
  342. GammaFactor = 0x7f7f7f7f ;
  343. ContrastFactor = 0x80 ;
  344. DISPDBG((0, "G Gamma = %lx", GammaFactor)) ;
  345. DISPDBG((0, "G Contrast = %lx", ContrastFactor)) ;
  346. }
  347. bGammaInit = TRUE ;
  348. }
  349. //
  350. // Save the new palette values
  351. //
  352. ppalFrom = (PALETTEENTRY*) pScreenClut->LookupTable;
  353. ppalTo = ppdev->pCurrentPalette;
  354. ppalEnd = &ppalTo[256];
  355. for (; ppalTo < ppalEnd; ppalFrom++, ppalTo++)
  356. {
  357. ppalTo->peRed = ppalFrom->peRed ;
  358. ppalTo->peGreen = ppalFrom->peGreen ;
  359. ppalTo->peBlue = ppalFrom->peBlue ;
  360. ppalTo->peFlags = 0 ;
  361. }
  362. //myf29 begin
  363. if (ppdev->ulChipID == 0xBC)
  364. status = bEnableGammaCorrect(ppdev) ;
  365. else if ((ppdev->ulChipID == 0x40) || (ppdev->ulChipID ==0x4C))
  366. status = bEnableGamma755x(ppdev) ;
  367. //myf29 end
  368. CalculateGamma( ppdev, pScreenClut, 256 ) ;
  369. #endif // GAMMACORRECT
  370. // Set palette registers:
  371. if (!IOCONTROL(ppdev->hDriver,
  372. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  373. pScreenClut,
  374. MAX_CLUT_SIZE,
  375. NULL,
  376. 0,
  377. &ulReturnedDataLength))
  378. {
  379. DISPDBG((0, "Failed bEnablePalette"));
  380. return(FALSE);
  381. }
  382. }
  383. DISPDBG((5, "Passed bEnablePalette"));
  384. return(TRUE);
  385. }
  386. /******************************Public*Routine******************************\
  387. * VOID vDisablePalette
  388. *
  389. * Undoes anything done in bEnablePalette.
  390. *
  391. \**************************************************************************/
  392. VOID vDisablePalette(
  393. PDEV* ppdev)
  394. {
  395. // Nothin' to do
  396. }
  397. /******************************Public*Routine******************************\
  398. * VOID vAssertModePalette
  399. *
  400. * Sets/resets the palette in preparation for full-screen/graphics mode.
  401. *
  402. \**************************************************************************/
  403. VOID vAssertModePalette(
  404. PDEV* ppdev,
  405. BOOL bEnable)
  406. {
  407. // USER immediately calls DrvSetPalette after switching out of
  408. // full-screen, so we don't have to worry about resetting the
  409. // palette here.
  410. }
  411. /******************************Public*Routine******************************\
  412. * BOOL DrvSetPalette
  413. *
  414. * DDI entry point for manipulating the palette.
  415. *
  416. \**************************************************************************/
  417. BOOL DrvSetPalette(
  418. DHPDEV dhpdev,
  419. PALOBJ* ppalo,
  420. FLONG fl,
  421. ULONG iStart,
  422. ULONG cColors)
  423. {
  424. BYTE ajClutSpace[MAX_CLUT_SIZE];
  425. PVIDEO_CLUT pScreenClut;
  426. PALETTEENTRY* ppal;
  427. PALETTEENTRY* ppalFrom;
  428. PALETTEENTRY* ppalTo;
  429. PALETTEENTRY* ppalEnd;
  430. BOOL status; //myf29
  431. PDEV* ppdev;
  432. UNREFERENCED_PARAMETER(fl);
  433. DISPDBG((2, "---- DrvSetPalette"));
  434. // Fill in pScreenClut header info:
  435. pScreenClut = (PVIDEO_CLUT) ajClutSpace;
  436. pScreenClut->NumEntries = (USHORT) cColors;
  437. pScreenClut->FirstEntry = (USHORT) iStart;
  438. ppal = (PPALETTEENTRY) (pScreenClut->LookupTable);
  439. if (cColors != PALOBJ_cGetColors(ppalo, iStart, cColors, (ULONG*) ppal))
  440. {
  441. DISPDBG((0, "DrvSetPalette failed PALOBJ_cGetColors\n"));
  442. goto ReturnFalse;
  443. }
  444. // Set the high reserved byte in each palette entry to 0.
  445. //
  446. // chu01
  447. //
  448. #ifndef GAMMACORRECT
  449. for (ppalEnd = &ppal[cColors]; ppal < ppalEnd; ppal++)
  450. {
  451. ppal->peRed >>= 2;
  452. ppal->peGreen >>= 2;
  453. ppal->peBlue >>= 2;
  454. ppal->peFlags = 0;
  455. }
  456. #endif // !GAMMACORRECT
  457. // Set palette registers
  458. ppdev = (PDEV*) dhpdev;
  459. //
  460. // chu01
  461. //
  462. #ifdef GAMMACORRECT
  463. //
  464. // Save the new palette values
  465. //
  466. ppalFrom = (PALETTEENTRY*) pScreenClut->LookupTable;
  467. ppalTo = ppdev->pCurrentPalette;
  468. ppalEnd = &ppalTo[256];
  469. for (; ppalTo < ppalEnd; ppalFrom++, ppalTo++)
  470. {
  471. ppalTo->peRed = ppalFrom->peRed ;
  472. ppalTo->peGreen = ppalFrom->peGreen ;
  473. ppalTo->peBlue = ppalFrom->peBlue ;
  474. ppalTo->peFlags = 0 ;
  475. }
  476. //myf29 begin
  477. if (ppdev->ulChipID == 0xBC)
  478. status = bEnableGammaCorrect(ppdev) ;
  479. else if ((ppdev->ulChipID == 0x40) || (ppdev->ulChipID ==0x4C))
  480. status = bEnableGamma755x(ppdev) ;
  481. //myf29 end
  482. CalculateGamma( ppdev, pScreenClut, 256 ) ;
  483. #endif // GAMMACORRECT
  484. if (!IOCONTROL(ppdev->hDriver,
  485. IOCTL_VIDEO_SET_COLOR_REGISTERS,
  486. pScreenClut,
  487. MAX_CLUT_SIZE,
  488. NULL,
  489. 0,
  490. &cColors))
  491. {
  492. DISPDBG((0, "DrvSetPalette failed EngDeviceIoControl\n"));
  493. goto ReturnFalse;
  494. }
  495. return(TRUE);
  496. ReturnFalse:
  497. return(FALSE);
  498. }