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.

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