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.

968 lines
31 KiB

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