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.

944 lines
30 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: enable.c
  3. *
  4. * The initialization guts of the portable 16-colour VGA driver.
  5. *
  6. * The drawing guts of a portable 16-colour VGA driver for Windows NT. The
  7. * implementation herein may possibly be the simplest method of bringing
  8. * up a driver whose surface is not directly writable by GDI. One might
  9. * use the phrase "quick and dirty" when describing it.
  10. *
  11. * We create a 4bpp bitmap that is the size of the screen, and simply
  12. * have GDI do all the drawing to it. We update the screen directly
  13. * from the bitmap, based on the bounds of the drawing (basically
  14. * employing "dirty rectangles").
  15. *
  16. * In total, the only hardware-specific code we had to write was the
  17. * initialization code, and a routine for doing aligned srccopy blts
  18. * from a DIB to the screen.
  19. *
  20. * Obvious Note: This approach is definitely not recommended for decent
  21. * driver performance.
  22. *
  23. * Copyright (c) 1994-1995 Microsoft Corporation
  24. \**************************************************************************/
  25. #include "precomp.h"
  26. /******************************Public*Structure****************************\
  27. * DFVFN gadrvfn[]
  28. *
  29. * Build the driver function table gadrvfn with function index/address
  30. * pairs. This table tells GDI which DDI calls we support, and their
  31. * location (GDI does an indirect call through this table to call us).
  32. *
  33. \**************************************************************************/
  34. static DRVFN gadrvfn[] = {
  35. { INDEX_DrvEnablePDEV, (PFN) DrvEnablePDEV },
  36. { INDEX_DrvCompletePDEV, (PFN) DrvCompletePDEV },
  37. { INDEX_DrvDisablePDEV, (PFN) DrvDisablePDEV },
  38. { INDEX_DrvEnableSurface, (PFN) DrvEnableSurface },
  39. { INDEX_DrvDisableSurface, (PFN) DrvDisableSurface },
  40. { INDEX_DrvDitherColor, (PFN) DrvDitherColor },
  41. { INDEX_DrvAssertMode, (PFN) DrvAssertMode },
  42. { INDEX_DrvGetModes, (PFN) DrvGetModes },
  43. { INDEX_DrvBitBlt, (PFN) DrvBitBlt },
  44. { INDEX_DrvTextOut, (PFN) DrvTextOut },
  45. { INDEX_DrvStrokePath, (PFN) DrvStrokePath },
  46. { INDEX_DrvCopyBits, (PFN) DrvCopyBits },
  47. { INDEX_DrvPaint, (PFN) DrvPaint },
  48. };
  49. ULONG gcdrvfn = sizeof(gadrvfn) / sizeof(DRVFN);
  50. /******************************Public*Structure****************************\
  51. * GDIINFO ggdiDefault
  52. *
  53. * This contains the default GDIINFO fields that are passed back to GDI
  54. * during DrvEnablePDEV.
  55. *
  56. * NOTE: This structure defaults to values for a 4bpp non-palette device.
  57. \**************************************************************************/
  58. GDIINFO ggdiDefault = {
  59. GDI_DRIVER_VERSION,
  60. DT_RASDISPLAY, // ulTechnology
  61. 0, // ulHorzSize
  62. 0, // ulVertSize
  63. 0, // ulHorzRes (filled in at initialization)
  64. 0, // ulVertRes (filled in at initialization)
  65. 4, // cBitsPixel
  66. 1, // cPlanes
  67. 16, // ulNumColors
  68. 0, // flRaster (DDI reserved field)
  69. 0, // ulLogPixelsX (filled in at initialization)
  70. 0, // ulLogPixelsY (filled in at initialization)
  71. TC_RA_ABLE, // flTextCaps
  72. 6, // ulDACRed
  73. 6, // ulDACGree
  74. 6, // ulDACBlue
  75. 0x0024, // ulAspectX (one-to-one aspect ratio)
  76. 0x0024, // ulAspectY
  77. 0x0033, // ulAspectXY
  78. 1, // xStyleStep
  79. 1, // yStyleSte;
  80. 3, // denStyleStep
  81. { 0, 0 }, // ptlPhysOffset
  82. { 0, 0 }, // szlPhysSize
  83. 0, // ulNumPalReg (win3.1 16 color drivers say 0 too)
  84. // These fields are for halftone initialization.
  85. { // ciDevice, ColorInfo
  86. { 6700, 3300, 0 }, // Red
  87. { 2100, 7100, 0 }, // Green
  88. { 1400, 800, 0 }, // Blue
  89. { 1750, 3950, 0 }, // Cyan
  90. { 4050, 2050, 0 }, // Magenta
  91. { 4400, 5200, 0 }, // Yellow
  92. { 3127, 3290, 0 }, // AlignmentWhite
  93. 20000, // RedGamma
  94. 20000, // GreenGamma
  95. 20000, // BlueGamma
  96. 0, 0, 0, 0, 0, 0
  97. },
  98. 0, // ulDevicePelsDPI (filled in at initialization)
  99. PRIMARY_ORDER_CBA, // ulPrimaryOrder
  100. HT_PATSIZE_4x4_M, // ulHTPatternSize
  101. HT_FORMAT_4BPP_IRGB, // ulHTOutputFormat
  102. HT_FLAG_ADDITIVE_PRIMS, // flHTFlags
  103. 0, // ulVRefresh
  104. 1, // ulBltAlignment (preferred window alignment
  105. // for fast-text routines)
  106. 0, // ulPanningHorzRes
  107. 0, // ulPanningVertRes
  108. };
  109. /******************************Public*Structure****************************\
  110. * DEVINFO gdevinfoDefault
  111. *
  112. * This contains the default DEVINFO fields that are passed back to GDI
  113. * during DrvEnablePDEV.
  114. *
  115. * NOTE: This structure defaults to values for a 4bpp non-palette device.
  116. \**************************************************************************/
  117. #define SYSTM_LOGFONT {16,7,0,0,700,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS, \
  118. CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, \
  119. VARIABLE_PITCH | FF_DONTCARE,L"System"}
  120. #define HELVE_LOGFONT {12,9,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS, \
  121. CLIP_STROKE_PRECIS,PROOF_QUALITY, \
  122. VARIABLE_PITCH | FF_DONTCARE, L"MS Sans Serif"}
  123. #define COURI_LOGFONT {12,9,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS, \
  124. CLIP_STROKE_PRECIS,PROOF_QUALITY, \
  125. FIXED_PITCH | FF_DONTCARE, L"Courier"}
  126. DEVINFO gdevinfoDefault =
  127. {
  128. (GCAPS_MONO_DITHER | GCAPS_COLOR_DITHER),
  129. // Graphics capabilities
  130. SYSTM_LOGFONT, // Default font description
  131. HELVE_LOGFONT, // ANSI variable font description
  132. COURI_LOGFONT, // ANSI fixed font description
  133. 0, // Count of device fonts
  134. BMF_4BPP, // preferred DIB format
  135. 8, // Width of color dither
  136. 8, // Height of color dither
  137. 0 // Default palette to use for this device
  138. };
  139. /******************************Public*Data*Struct*************************\
  140. * VGALOGPALETTE logPalVGA
  141. *
  142. * This is the palette for the VGA.
  143. *
  144. \**************************************************************************/
  145. typedef struct _VGALOGPALETTE
  146. {
  147. USHORT ident;
  148. USHORT NumEntries;
  149. PALETTEENTRY palPalEntry[16];
  150. } VGALOGPALETTE;
  151. const VGALOGPALETTE logPalVGA =
  152. {
  153. 0x400, // Driver version
  154. 16, // Number of entries
  155. {
  156. { 0, 0, 0, 0 }, // 0
  157. { 0x80,0, 0, 0 }, // 1
  158. { 0, 0x80,0, 0 }, // 2
  159. { 0x80,0x80,0, 0 }, // 3
  160. { 0, 0, 0x80,0 }, // 4
  161. { 0x80,0, 0x80,0 }, // 5
  162. { 0, 0x80,0x80,0 }, // 6
  163. { 0x80,0x80,0x80,0 }, // 7
  164. { 0xC0,0xC0,0xC0,0 }, // 8
  165. { 0xFF,0, 0, 0 }, // 9
  166. { 0, 0xFF,0, 0 }, // 10
  167. { 0xFF,0xFF,0, 0 }, // 11
  168. { 0, 0, 0xFF,0 }, // 12
  169. { 0xFF,0, 0xFF,0 }, // 13
  170. { 0, 0xFF,0xFF,0 }, // 14
  171. { 0xFF,0xFF,0xFF,0 } // 15
  172. }
  173. };
  174. /******************************Public*Routine******************************\
  175. * BOOL DrvEnableDriver
  176. *
  177. * Enables the driver by retrieving the drivers function table and version.
  178. *
  179. \**************************************************************************/
  180. BOOL DrvEnableDriver(
  181. ULONG iEngineVersion,
  182. ULONG cj,
  183. DRVENABLEDATA* pded)
  184. {
  185. // Engine Version is passed down so future drivers can support previous
  186. // engine versions. A next generation driver can support both the old
  187. // and new engine conventions if told what version of engine it is
  188. // working with. For the first version the driver does nothing with it.
  189. // Fill in as much as we can.
  190. if (cj >= sizeof(DRVENABLEDATA))
  191. pded->pdrvfn = gadrvfn;
  192. if (cj >= (sizeof(ULONG) * 2))
  193. pded->c = gcdrvfn;
  194. // DDI version this driver was targeted for is passed back to engine.
  195. // Future graphic's engine may break calls down to old driver format.
  196. if (cj >= sizeof(ULONG))
  197. pded->iDriverVersion = DDI_DRIVER_VERSION_NT4;
  198. return(TRUE);
  199. }
  200. /******************************Public*Routine******************************\
  201. * VOID DrvDisableDriver
  202. *
  203. * Tells the driver it is being disabled. Release any resources allocated in
  204. * DrvEnableDriver.
  205. *
  206. \**************************************************************************/
  207. VOID DrvDisableDriver(VOID)
  208. {
  209. return;
  210. }
  211. /******************************Public*Routine******************************\
  212. * DWORD getAvailableModes
  213. *
  214. * Calls the miniport to get the list of modes supported by the kernel driver,
  215. * and returns the list of modes supported by the diplay driver among those
  216. *
  217. * returns the number of entries in the videomode buffer.
  218. * 0 means no modes are supported by the miniport or that an error occured.
  219. *
  220. * NOTE: the buffer must be freed up by the caller.
  221. *
  222. \**************************************************************************/
  223. DWORD getAvailableModes(
  224. HANDLE hDriver,
  225. PVIDEO_MODE_INFORMATION* modeInformation,
  226. DWORD* cbModeSize)
  227. {
  228. ULONG ulTemp;
  229. VIDEO_NUM_MODES modes;
  230. PVIDEO_MODE_INFORMATION pVideoTemp;
  231. DWORD status;
  232. //
  233. // Get the number of modes supported by the mini-port
  234. //
  235. if (status = EngDeviceIoControl(hDriver,
  236. IOCTL_VIDEO_QUERY_NUM_AVAIL_MODES,
  237. NULL,
  238. 0,
  239. &modes,
  240. sizeof(VIDEO_NUM_MODES),
  241. &ulTemp))
  242. {
  243. DISPDBG((0, "getAvailableModes failed VIDEO_QUERY_NUM_AVAIL_MODES"));
  244. DISPDBG((0, "Win32 Status = %x", status));
  245. return(0);
  246. }
  247. *cbModeSize = modes.ModeInformationLength;
  248. //
  249. // Allocate the buffer for the mini-port to write the modes in.
  250. //
  251. *modeInformation = (PVIDEO_MODE_INFORMATION)
  252. EngAllocMem(FL_ZERO_MEMORY,
  253. modes.NumModes *
  254. modes.ModeInformationLength, ALLOC_TAG);
  255. if (*modeInformation == (PVIDEO_MODE_INFORMATION) NULL)
  256. {
  257. DISPDBG((0, "getAvailableModes failed EngAllocMem"));
  258. return(0);
  259. }
  260. //
  261. // Ask the mini-port to fill in the available modes.
  262. //
  263. if (status = EngDeviceIoControl(hDriver,
  264. IOCTL_VIDEO_QUERY_AVAIL_MODES,
  265. NULL,
  266. 0,
  267. *modeInformation,
  268. modes.NumModes * modes.ModeInformationLength,
  269. &ulTemp))
  270. {
  271. DISPDBG((0, "getAvailableModes failed VIDEO_QUERY_AVAIL_MODES"));
  272. DISPDBG((0, "Win32 Status = %x", status));
  273. EngFreeMem(*modeInformation);
  274. *modeInformation = (PVIDEO_MODE_INFORMATION) NULL;
  275. return(0);
  276. }
  277. //
  278. // Now see which of these modes are supported by the display driver.
  279. // As an internal mechanism, set the length to 0 for the modes we
  280. // DO NOT support.
  281. //
  282. ulTemp = modes.NumModes;
  283. pVideoTemp = *modeInformation;
  284. //
  285. // Mode is rejected if it is not 4 planes, or not graphics, or is not
  286. // one of 1 bits per pel.
  287. //
  288. while (ulTemp--)
  289. {
  290. if ((pVideoTemp->NumberOfPlanes != 4 ) ||
  291. !(pVideoTemp->AttributeFlags & VIDEO_MODE_GRAPHICS) ||
  292. (pVideoTemp->BitsPerPlane != 1) ||
  293. (pVideoTemp->VisScreenWidth > 800))
  294. {
  295. pVideoTemp->Length = 0;
  296. }
  297. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  298. (((PUCHAR)pVideoTemp) + modes.ModeInformationLength);
  299. }
  300. return(modes.NumModes);
  301. }
  302. /******************************Public*Routine******************************\
  303. * BOOL bInitializeModeFields
  304. *
  305. * Initializes a bunch of fields in the pdev, devcaps (aka gdiinfo), and
  306. * devinfo based on the requested mode.
  307. *
  308. \**************************************************************************/
  309. BOOL bInitializeModeFields(
  310. PDEV* ppdev,
  311. GDIINFO* pgdi,
  312. DEVINFO* pdi,
  313. DEVMODEW* pdm)
  314. {
  315. ULONG cModes;
  316. PVIDEO_MODE_INFORMATION pVideoBuffer;
  317. PVIDEO_MODE_INFORMATION pVideoModeSelected;
  318. PVIDEO_MODE_INFORMATION pVideoTemp;
  319. BOOL bSelectDefault;
  320. VIDEO_MODE_INFORMATION VideoModeInformation;
  321. ULONG cbModeSize;
  322. // Call the miniport to get mode information
  323. cModes = getAvailableModes(ppdev->hDriver, &pVideoBuffer, &cbModeSize);
  324. if (cModes == 0)
  325. goto ReturnFalse;
  326. // Now see if the requested mode has a match in that table.
  327. pVideoModeSelected = NULL;
  328. pVideoTemp = pVideoBuffer;
  329. if ((pdm->dmPelsWidth == 0) &&
  330. (pdm->dmPelsHeight == 0) &&
  331. (pdm->dmBitsPerPel == 0) &&
  332. (pdm->dmDisplayFrequency == 0))
  333. {
  334. DISPDBG((1, "Default mode requested"));
  335. bSelectDefault = TRUE;
  336. }
  337. else
  338. {
  339. DISPDBG((1, "Requested mode..."));
  340. DISPDBG((1, " Screen width -- %li", pdm->dmPelsWidth));
  341. DISPDBG((1, " Screen height -- %li", pdm->dmPelsHeight));
  342. DISPDBG((1, " Bits per pel -- %li", pdm->dmBitsPerPel));
  343. DISPDBG((1, " Frequency -- %li", pdm->dmDisplayFrequency));
  344. bSelectDefault = FALSE;
  345. }
  346. while (cModes--)
  347. {
  348. if (pVideoTemp->Length != 0)
  349. {
  350. DISPDBG((2, " Checking against miniport mode:"));
  351. DISPDBG((2, " Screen width -- %li", pVideoTemp->VisScreenWidth));
  352. DISPDBG((2, " Screen height -- %li", pVideoTemp->VisScreenHeight));
  353. DISPDBG((2, " Bits per pel -- %li", pVideoTemp->BitsPerPlane *
  354. pVideoTemp->NumberOfPlanes));
  355. DISPDBG((2, " Frequency -- %li", pVideoTemp->Frequency));
  356. if (bSelectDefault ||
  357. ((pVideoTemp->VisScreenWidth == pdm->dmPelsWidth) &&
  358. (pVideoTemp->VisScreenHeight == pdm->dmPelsHeight) &&
  359. (pVideoTemp->BitsPerPlane *
  360. pVideoTemp->NumberOfPlanes == pdm->dmBitsPerPel) &&
  361. (pVideoTemp->Frequency == pdm->dmDisplayFrequency)))
  362. {
  363. pVideoModeSelected = pVideoTemp;
  364. DISPDBG((1, "...Found a mode match!"));
  365. break;
  366. }
  367. }
  368. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  369. (((PUCHAR)pVideoTemp) + cbModeSize);
  370. }
  371. // If no mode has been found, return an error
  372. if (pVideoModeSelected == NULL)
  373. {
  374. DISPDBG((1, "...Couldn't find a mode match!"));
  375. EngFreeMem(pVideoBuffer);
  376. goto ReturnFalse;
  377. }
  378. // We have chosen the one we want. Save it in a stack buffer and
  379. // get rid of allocated memory before we forget to free it.
  380. VideoModeInformation = *pVideoModeSelected;
  381. EngFreeMem(pVideoBuffer);
  382. // Set up screen information from the mini-port:
  383. ppdev->ulMode = VideoModeInformation.ModeIndex;
  384. ppdev->cxScreen = VideoModeInformation.VisScreenWidth;
  385. ppdev->cyScreen = VideoModeInformation.VisScreenHeight;
  386. ppdev->iBitmapFormat = BMF_4BPP;
  387. DISPDBG((1, "ScreenStride: %lx", VideoModeInformation.ScreenStride));
  388. ppdev->flHooks = (HOOK_BITBLT |
  389. HOOK_TEXTOUT |
  390. HOOK_COPYBITS |
  391. HOOK_STROKEPATH |
  392. HOOK_PAINT);
  393. ppdev->pjBase = INVALID_BASE_ADDRESS;
  394. // Fill in the GDIINFO data structure with the default 4bpp values:
  395. *pgdi = ggdiDefault;
  396. // Now overwrite the defaults with the relevant information returned
  397. // from the kernel driver:
  398. pgdi->ulHorzSize = VideoModeInformation.XMillimeter;
  399. pgdi->ulVertSize = VideoModeInformation.YMillimeter;
  400. pgdi->ulHorzRes = VideoModeInformation.VisScreenWidth;
  401. pgdi->ulVertRes = VideoModeInformation.VisScreenHeight;
  402. pgdi->ulPanningHorzRes = VideoModeInformation.VisScreenWidth;
  403. pgdi->ulPanningVertRes = VideoModeInformation.VisScreenHeight;
  404. pgdi->cBitsPixel = VideoModeInformation.BitsPerPlane;
  405. pgdi->cPlanes = VideoModeInformation.NumberOfPlanes;
  406. pgdi->ulVRefresh = VideoModeInformation.Frequency;
  407. pgdi->ulDACRed = VideoModeInformation.NumberRedBits;
  408. pgdi->ulDACGreen = VideoModeInformation.NumberGreenBits;
  409. pgdi->ulDACBlue = VideoModeInformation.NumberBlueBits;
  410. pgdi->ulLogPixelsX = pdm->dmLogPixels;
  411. pgdi->ulLogPixelsY = pdm->dmLogPixels;
  412. // Fill in the devinfo structure with the default 4bpp values:
  413. *pdi = gdevinfoDefault;
  414. return(TRUE);
  415. ReturnFalse:
  416. DISPDBG((0, "Failed bInitializeModeFields"));
  417. return(FALSE);
  418. }
  419. /******************************Public*Routine******************************\
  420. * BOOL bInitializePalette
  421. *
  422. * Initializes default palette for PDEV.
  423. *
  424. \**************************************************************************/
  425. BOOL bInitializePalette(
  426. PDEV* ppdev,
  427. DEVINFO* pdi)
  428. {
  429. HPALETTE hpal;
  430. hpal = EngCreatePalette(PAL_INDEXED, 16, (ULONG*) (logPalVGA.palPalEntry),
  431. 0, 0, 0);
  432. if (hpal == 0)
  433. goto ReturnFalse;
  434. ppdev->hpalDefault = hpal;
  435. pdi->hpalDefault = hpal;
  436. return(TRUE);
  437. ReturnFalse:
  438. DISPDBG((0, "Failed bInitializePalette"));
  439. return(FALSE);
  440. }
  441. /******************************Public*Routine******************************\
  442. * VOID vUninitializePalette
  443. *
  444. * Frees resources allocated by bInitializePalette.
  445. *
  446. * Note: In an error case, this may be called before bInitializePalette.
  447. *
  448. \**************************************************************************/
  449. VOID vUninitializePalette(PDEV* ppdev)
  450. {
  451. // Delete the default palette if we created one:
  452. if (ppdev->hpalDefault != 0)
  453. EngDeletePalette(ppdev->hpalDefault);
  454. }
  455. /******************************Public*Routine******************************\
  456. * DHPDEV DrvEnablePDEV
  457. *
  458. * Initializes a bunch of fields for GDI, based on the mode we've been asked
  459. * to do. This is the first thing called after DrvEnableDriver, when GDI
  460. * wants to get some information about us.
  461. *
  462. * (This function mostly returns back information; DrvEnableSurface is used
  463. * for initializing the hardware and driver components.)
  464. *
  465. \**************************************************************************/
  466. DHPDEV DrvEnablePDEV(
  467. DEVMODEW* pdm, // Contains data pertaining to requested mode
  468. PWSTR pwszLogAddr, // Logical address
  469. ULONG cPat, // Count of standard patterns
  470. HSURF* phsurfPatterns, // Buffer for standard patterns
  471. ULONG cjCaps, // Size of buffer for device caps 'pdevcaps'
  472. ULONG* pdevcaps, // Buffer for device caps, also known as 'gdiinfo'
  473. ULONG cjDevInfo, // Number of bytes in device info 'pdi'
  474. DEVINFO* pdi, // Device information
  475. HDEV hdev, // HDEV, used for callbacks
  476. PWSTR pwszDeviceName, // Device name
  477. HANDLE hDriver) // Kernel driver handle
  478. {
  479. PDEV* ppdev;
  480. // Future versions of NT had better supply 'devcaps' and 'devinfo'
  481. // structures that are the same size or larger than the current
  482. // structures:
  483. if ((cjCaps < sizeof(GDIINFO)) || (cjDevInfo < sizeof(DEVINFO)))
  484. {
  485. DISPDBG((0, "DrvEnablePDEV - Buffer size too small"));
  486. goto ReturnFailure0;
  487. }
  488. // Allocate a physical device structure. Note that we definitely
  489. // rely on the zero initialization:
  490. ppdev = (PDEV*) EngAllocMem(FL_ZERO_MEMORY, sizeof(PDEV), ALLOC_TAG);
  491. if (ppdev == NULL)
  492. {
  493. DISPDBG((0, "DrvEnablePDEV - Failed EngAllocMem"));
  494. goto ReturnFailure0;
  495. }
  496. ppdev->hDriver = hDriver;
  497. // Get the current screen mode information. Set up device caps and
  498. // devinfo:
  499. if (!bInitializeModeFields(ppdev, (GDIINFO*) pdevcaps, pdi, pdm))
  500. {
  501. DISPDBG((0, "DrvEnablePDEV - Failed bInitializeModeFields"));
  502. goto ReturnFailure1;
  503. }
  504. // Initialize palette information.
  505. if (!bInitializePalette(ppdev, pdi))
  506. {
  507. DISPDBG((0, "DrvEnablePDEV - Failed bInitializePalette"));
  508. goto ReturnFailure1;
  509. }
  510. return((DHPDEV) ppdev);
  511. ReturnFailure1:
  512. DrvDisablePDEV((DHPDEV) ppdev);
  513. ReturnFailure0:
  514. DISPDBG((0, "Failed DrvEnablePDEV"));
  515. return(0);
  516. }
  517. /******************************Public*Routine******************************\
  518. * DrvDisablePDEV
  519. *
  520. * Release the resources allocated in DrvEnablePDEV. If a surface has been
  521. * enabled DrvDisableSurface will have already been called.
  522. *
  523. * Note: In an error, we may call this before DrvEnablePDEV is done.
  524. *
  525. \**************************************************************************/
  526. VOID DrvDisablePDEV(
  527. DHPDEV dhpdev)
  528. {
  529. PDEV* ppdev;
  530. ppdev = (PDEV*) dhpdev;
  531. vUninitializePalette(ppdev);
  532. EngFreeMem(ppdev);
  533. }
  534. /******************************Public*Routine******************************\
  535. * VOID DrvCompletePDEV
  536. *
  537. * Store the HPDEV, the engines handle for this PDEV, in the DHPDEV.
  538. *
  539. \**************************************************************************/
  540. VOID DrvCompletePDEV(
  541. DHPDEV dhpdev,
  542. HDEV hdev)
  543. {
  544. ((PDEV*) dhpdev)->hdevEng = hdev;
  545. }
  546. /******************************Public*Routine******************************\
  547. * HSURF DrvEnableSurface
  548. *
  549. * Creates the drawing surface, initializes the hardware, and initializes
  550. * driver components. This function is called after DrvEnablePDEV, and
  551. * performs the final device initialization.
  552. *
  553. \**************************************************************************/
  554. HSURF DrvEnableSurface(
  555. DHPDEV dhpdev)
  556. {
  557. PDEV* ppdev;
  558. HSURF hsurfShadow;
  559. HSURF hsurfDevice;
  560. SIZEL sizl;
  561. ppdev = (PDEV*) dhpdev;
  562. /////////////////////////////////////////////////////////////////////
  563. // Have GDI create the actual SURFOBJ.
  564. //
  565. // Our drawing surface is going to be 'device-managed', meaning that
  566. // GDI cannot draw on the framebuffer bits directly, and as such we
  567. // create the surface via EngCreateSurface. By doing this, we ensure
  568. // that GDI will only ever access the bitmaps bits via the Drv calls
  569. // that we've HOOKed.
  570. sizl.cx = ppdev->cxScreen;
  571. sizl.cy = ppdev->cyScreen;
  572. hsurfDevice = EngCreateDeviceSurface(NULL, sizl, ppdev->iBitmapFormat);
  573. if (hsurfDevice == 0)
  574. {
  575. DISPDBG((0, "DrvEnableSurface - Failed EngCreateSurface"));
  576. return(0);
  577. }
  578. ppdev->hsurfScreen = hsurfDevice; // Remember it for clean-up
  579. /////////////////////////////////////////////////////////////////////
  580. // Now associate the surface and the PDEV.
  581. //
  582. // We have to associate the surface we just created with our physical
  583. // device so that GDI can get information related to the PDEV when
  584. // it's drawing to the surface (such as, for example, the length of
  585. // styles on the device when simulating styled lines).
  586. //
  587. if (!EngAssociateSurface(hsurfDevice, ppdev->hdevEng, ppdev->flHooks))
  588. {
  589. DISPDBG((0, "DrvEnableSurface - Failed EngAssociateSurface"));
  590. goto ReturnFailure;
  591. }
  592. // Create the 4bpp DIB on which we'll have GDI do all the drawing.
  593. // We'll merely occasionally blt portions to the screen to update.
  594. sizl.cx = ppdev->cxScreen;
  595. sizl.cy = ppdev->cyScreen;
  596. hsurfShadow = (HSURF) EngCreateBitmap(sizl, 0, ppdev->iBitmapFormat, 0,
  597. NULL);
  598. if (hsurfShadow == 0)
  599. goto ReturnFailure;
  600. if (!EngAssociateSurface(hsurfShadow, ppdev->hdevEng, 0))
  601. {
  602. DISPDBG((0, "DrvEnableSurface - Failed second EngAssociateSurface"));
  603. goto ReturnFailure;
  604. }
  605. ppdev->pso = EngLockSurface(hsurfShadow);
  606. if (ppdev->pso == NULL)
  607. goto ReturnFailure;
  608. /////////////////////////////////////////////////////////////////////
  609. // Now enable all the subcomponents.
  610. //
  611. // Note that the order in which these 'Enable' functions are called
  612. // may be significant in low off-screen memory conditions, because
  613. // the off-screen heap manager may fail some of the later
  614. // allocations...
  615. if (!bEnableHardware(ppdev))
  616. goto ReturnFailure;
  617. DISPDBG((5, "Passed DrvEnableSurface"));
  618. return(hsurfDevice);
  619. ReturnFailure:
  620. DrvDisableSurface((DHPDEV) ppdev);
  621. DISPDBG((0, "Failed DrvEnableSurface"));
  622. return(0);
  623. }
  624. /******************************Public*Routine******************************\
  625. * VOID DrvDisableSurface
  626. *
  627. * Free resources allocated by DrvEnableSurface. Release the surface.
  628. *
  629. * Note: In an error case, we may call this before DrvEnableSurface is
  630. * completely done.
  631. *
  632. \**************************************************************************/
  633. VOID DrvDisableSurface(
  634. DHPDEV dhpdev)
  635. {
  636. PDEV* ppdev;
  637. HSURF hsurf;
  638. ppdev = (PDEV*) dhpdev;
  639. // Note: In an error case, some of the following relies on the
  640. // fact that the PDEV is zero-initialized, so fields like
  641. // 'hsurfScreen' will be zero unless the surface has been
  642. // sucessfully initialized, and makes the assumption that
  643. // EngDeleteSurface can take '0' as a parameter.
  644. vDisableHardware(ppdev);
  645. if (ppdev->pso) {
  646. hsurf = ppdev->pso->hsurf;
  647. EngUnlockSurface(ppdev->pso);
  648. EngDeleteSurface(hsurf);
  649. }
  650. EngDeleteSurface(ppdev->hsurfScreen);
  651. }
  652. /******************************Public*Routine******************************\
  653. * VOID DrvAssertMode
  654. *
  655. * This asks the device to reset itself to the mode of the pdev passed in.
  656. *
  657. \**************************************************************************/
  658. BOOL DrvAssertMode(
  659. DHPDEV dhpdev,
  660. BOOL bEnable)
  661. {
  662. PDEV* ppdev;
  663. ppdev = (PDEV*) dhpdev;
  664. if (!bEnable)
  665. {
  666. //////////////////////////////////////////////////////////////
  667. // Disable - Switch to full-screen mode
  668. return(bAssertModeHardware(ppdev, FALSE));
  669. }
  670. else
  671. {
  672. //////////////////////////////////////////////////////////////
  673. // Enable - Switch back to graphics mode
  674. // We have to enable every subcomponent in the reverse order
  675. // in which it was disabled:
  676. return(bAssertModeHardware(ppdev, TRUE));
  677. }
  678. }
  679. /******************************Public*Routine******************************\
  680. * ULONG DrvGetModes
  681. *
  682. * Returns the list of available modes for the device.
  683. *
  684. \**************************************************************************/
  685. ULONG DrvGetModes(
  686. HANDLE hDriver,
  687. ULONG cjSize,
  688. DEVMODEW* pdm)
  689. {
  690. DWORD cModes;
  691. DWORD cbOutputSize;
  692. PVIDEO_MODE_INFORMATION pVideoModeInformation;
  693. PVIDEO_MODE_INFORMATION pVideoTemp;
  694. DWORD cOutputModes = cjSize / (sizeof(DEVMODEW) + DRIVER_EXTRA_SIZE);
  695. DWORD cbModeSize;
  696. cModes = getAvailableModes(hDriver,
  697. (PVIDEO_MODE_INFORMATION *) &pVideoModeInformation,
  698. &cbModeSize);
  699. if (cModes == 0)
  700. {
  701. DISPDBG((0, "DrvGetModes failed to get mode information"));
  702. return(0);
  703. }
  704. if (pdm == NULL)
  705. {
  706. cbOutputSize = cModes * (sizeof(DEVMODEW) + DRIVER_EXTRA_SIZE);
  707. }
  708. else
  709. {
  710. //
  711. // Now copy the information for the supported modes back into the
  712. // output buffer
  713. //
  714. cbOutputSize = 0;
  715. pVideoTemp = pVideoModeInformation;
  716. do
  717. {
  718. if (pVideoTemp->Length != 0)
  719. {
  720. if (cOutputModes == 0)
  721. {
  722. break;
  723. }
  724. //
  725. // Zero the entire structure to start off with.
  726. //
  727. memset(pdm, 0, sizeof(DEVMODEW));
  728. //
  729. // Set the name of the device to the name of the DLL.
  730. //
  731. memcpy(pdm->dmDeviceName, DLL_NAME, sizeof(DLL_NAME));
  732. pdm->dmSpecVersion = DM_SPECVERSION;
  733. pdm->dmDriverVersion = DM_SPECVERSION;
  734. pdm->dmSize = sizeof(DEVMODEW);
  735. pdm->dmDriverExtra = DRIVER_EXTRA_SIZE;
  736. pdm->dmBitsPerPel = pVideoTemp->NumberOfPlanes *
  737. pVideoTemp->BitsPerPlane;
  738. pdm->dmPelsWidth = pVideoTemp->VisScreenWidth;
  739. pdm->dmPelsHeight = pVideoTemp->VisScreenHeight;
  740. pdm->dmDisplayFrequency = pVideoTemp->Frequency;
  741. pdm->dmDisplayFlags = 0;
  742. pdm->dmFields = DM_BITSPERPEL |
  743. DM_PELSWIDTH |
  744. DM_PELSHEIGHT |
  745. DM_DISPLAYFREQUENCY |
  746. DM_DISPLAYFLAGS ;
  747. //
  748. // Go to the next DEVMODE entry in the buffer.
  749. //
  750. cOutputModes--;
  751. pdm = (LPDEVMODEW) ( ((ULONG_PTR)pdm) + sizeof(DEVMODEW) +
  752. DRIVER_EXTRA_SIZE);
  753. cbOutputSize += (sizeof(DEVMODEW) + DRIVER_EXTRA_SIZE);
  754. }
  755. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  756. (((PUCHAR)pVideoTemp) + cbModeSize);
  757. } while (--cModes);
  758. }
  759. EngFreeMem(pVideoModeInformation);
  760. return(cbOutputSize);
  761. }