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.

1733 lines
59 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: enable.c
  3. *
  4. * This module contains the functions that enable and disable the
  5. * driver, the pdev, and the surface.
  6. *
  7. * Copyright (c) 1992-1995 Microsoft Corporation
  8. \**************************************************************************/
  9. #include "precomp.h"
  10. #if defined(ALPHA)
  11. /**************************************************************************\
  12. * BOOL isDense
  13. *
  14. * This global is used to distinguish dense space from sparse space on the
  15. * DEC Alpha in order to use the appropriate method of register access.
  16. *
  17. \**************************************************************************/
  18. BOOL isDense = TRUE;
  19. #endif
  20. /******************************Public*Structure****************************\
  21. * GDIINFO ggdiDefault
  22. *
  23. * This contains the default GDIINFO fields that are passed back to GDI
  24. * during DrvEnablePDEV.
  25. *
  26. * NOTE: This structure defaults to values for an 8bpp palette device.
  27. * Some fields are overwritten for different colour depths.
  28. \**************************************************************************/
  29. GDIINFO ggdiDefault = {
  30. GDI_DRIVER_VERSION, // ulVersion
  31. DT_RASDISPLAY, // ulTechnology
  32. 0, // ulHorzSize (filled in later)
  33. 0, // ulVertSize (filled in later)
  34. 0, // ulHorzRes (filled in later)
  35. 0, // ulVertRes (filled in later)
  36. 0, // cBitsPixel (filled in later)
  37. 0, // cPlanes (filled in later)
  38. 20, // ulNumColors (palette managed)
  39. 0, // flRaster (DDI reserved field)
  40. 0, // ulLogPixelsX (filled in later)
  41. 0, // ulLogPixelsY (filled in later)
  42. TC_RA_ABLE, // flTextCaps -- If we had wanted console windows
  43. // to scroll by repainting the entire window,
  44. // instead of doing a screen-to-screen blt, we
  45. // would have set TC_SCROLLBLT (yes, the flag is
  46. // bass-ackwards).
  47. 0, // ulDACRed (filled in later)
  48. 0, // ulDACGreen (filled in later)
  49. 0, // ulDACBlue (filled in later)
  50. 0x0024, // ulAspectX
  51. 0x0024, // ulAspectY
  52. 0x0033, // ulAspectXY (one-to-one aspect ratio)
  53. 1, // xStyleStep
  54. 1, // yStyleSte;
  55. 3, // denStyleStep -- Styles have a one-to-one aspect
  56. // ratio, and every 'dot' is 3 pixels long
  57. { 0, 0 }, // ptlPhysOffset
  58. { 0, 0 }, // szlPhysSize
  59. 256, // ulNumPalReg
  60. // These fields are for halftone initialization. The actual values are
  61. // a bit magic, but seem to work well on our display.
  62. { // ciDevice
  63. { 6700, 3300, 0 }, // Red
  64. { 2100, 7100, 0 }, // Green
  65. { 1400, 800, 0 }, // Blue
  66. { 1750, 3950, 0 }, // Cyan
  67. { 4050, 2050, 0 }, // Magenta
  68. { 4400, 5200, 0 }, // Yellow
  69. { 3127, 3290, 0 }, // AlignmentWhite
  70. 20000, // RedGamma
  71. 20000, // GreenGamma
  72. 20000, // BlueGamma
  73. 0, 0, 0, 0, 0, 0 // No dye correction for raster displays
  74. },
  75. 0, // ulDevicePelsDPI (for printers only)
  76. PRIMARY_ORDER_CBA, // ulPrimaryOrder
  77. HT_PATSIZE_4x4_M, // ulHTPatternSize
  78. HT_FORMAT_8BPP, // ulHTOutputFormat
  79. HT_FLAG_ADDITIVE_PRIMS, // flHTFlags
  80. 0, // ulVRefresh
  81. 0, // ulPanningHorzRes
  82. 0, // ulPanningVertRes
  83. 0, // ulBltAlignment
  84. };
  85. /******************************Public*Structure****************************\
  86. * DEVINFO gdevinfoDefault
  87. *
  88. * This contains the default DEVINFO fields that are passed back to GDI
  89. * during DrvEnablePDEV.
  90. *
  91. * NOTE: This structure defaults to values for an 8bpp palette device.
  92. * Some fields are overwritten for different colour depths.
  93. \**************************************************************************/
  94. #define SYSTM_LOGFONT {16,7,0,0,700,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,\
  95. CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,\
  96. VARIABLE_PITCH | FF_DONTCARE,L"System"}
  97. #define HELVE_LOGFONT {12,9,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,\
  98. CLIP_STROKE_PRECIS,PROOF_QUALITY,\
  99. VARIABLE_PITCH | FF_DONTCARE,L"MS Sans Serif"}
  100. #define COURI_LOGFONT {12,9,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,\
  101. CLIP_STROKE_PRECIS,PROOF_QUALITY,\
  102. FIXED_PITCH | FF_DONTCARE, L"Courier"}
  103. DEVINFO gdevinfoDefault = {
  104. (GCAPS_OPAQUERECT |
  105. GCAPS_DITHERONREALIZE |
  106. GCAPS_PALMANAGED |
  107. GCAPS_ALTERNATEFILL |
  108. GCAPS_WINDINGFILL |
  109. #if TARGET_BUILD > 351
  110. GCAPS_DIRECTDRAW |
  111. #endif
  112. GCAPS_MONO_DITHER |
  113. GCAPS_COLOR_DITHER |
  114. GCAPS_ASYNCMOVE), // NOTE: Only enable ASYNCMOVE if your code
  115. // and hardware can handle DrvMovePointer
  116. // calls at any time, even while another
  117. // thread is in the middle of a drawing
  118. // call such as DrvBitBlt.
  119. // flGraphicsFlags
  120. SYSTM_LOGFONT, // lfDefaultFont
  121. HELVE_LOGFONT, // lfAnsiVarFont
  122. COURI_LOGFONT, // lfAnsiFixFont
  123. 0, // cFonts
  124. BMF_8BPP, // iDitherFormat
  125. 8, // cxDither
  126. 8, // cyDither
  127. 0 // hpalDefault (filled in later)
  128. };
  129. /******************************Public*Structure****************************\
  130. * DFVFN gadrvfn[]
  131. *
  132. * Build the driver function table gadrvfn with function index/address
  133. * pairs. This table tells GDI which DDI calls we support, and their
  134. * location (GDI does an indirect call through this table to call us).
  135. *
  136. * Why haven't we implemented DrvSaveScreenBits? To save code.
  137. *
  138. * When the driver doesn't hook DrvSaveScreenBits, USER simulates on-
  139. * the-fly by creating a temporary device-format-bitmap, and explicitly
  140. * calling DrvCopyBits to save/restore the bits. Since we already hook
  141. * DrvCreateDeviceBitmap, we'll end up using off-screen memory to store
  142. * the bits anyway (which would have been the main reason for implementing
  143. * DrvSaveScreenBits). So we may as well save some working set.
  144. \**************************************************************************/
  145. #if MULTI_BOARDS
  146. // Multi-board support has its own thunks...
  147. DRVFN gadrvfn[] = {
  148. { INDEX_DrvEnablePDEV, (PFN) MulEnablePDEV },
  149. { INDEX_DrvCompletePDEV, (PFN) MulCompletePDEV },
  150. { INDEX_DrvDisablePDEV, (PFN) MulDisablePDEV },
  151. { INDEX_DrvEnableSurface, (PFN) MulEnableSurface },
  152. { INDEX_DrvDisableSurface, (PFN) MulDisableSurface },
  153. { INDEX_DrvAssertMode, (PFN) MulAssertMode },
  154. { INDEX_DrvMovePointer, (PFN) MulMovePointer },
  155. { INDEX_DrvSetPointerShape, (PFN) MulSetPointerShape },
  156. { INDEX_DrvDitherColor, (PFN) MulDitherColor },
  157. { INDEX_DrvSetPalette, (PFN) MulSetPalette },
  158. { INDEX_DrvCopyBits, (PFN) MulCopyBits },
  159. { INDEX_DrvBitBlt, (PFN) MulBitBlt },
  160. { INDEX_DrvTextOut, (PFN) MulTextOut },
  161. { INDEX_DrvGetModes, (PFN) MulGetModes },
  162. { INDEX_DrvStrokePath, (PFN) MulStrokePath },
  163. { INDEX_DrvFillPath, (PFN) MulFillPath },
  164. { INDEX_DrvPaint, (PFN) MulPaint },
  165. { INDEX_DrvRealizeBrush, (PFN) MulRealizeBrush },
  166. { INDEX_DrvDestroyFont, (PFN) MulDestroyFont },
  167. #if TARGET_BUILD > 351
  168. { INDEX_DrvGetDirectDrawInfo, (PFN) DrvGetDirectDrawInfo },
  169. { INDEX_DrvEnableDirectDraw, (PFN) DrvEnableDirectDraw },
  170. { INDEX_DrvDisableDirectDraw, (PFN) DrvDisableDirectDraw },
  171. #endif
  172. // Note that we don't support DrvCreateDeviceBitmap for multi-boards
  173. // Note that we don't support DrvDeleteDeviceBitmap for multi-boards
  174. // Note that we don't support DrvStretchBlt for multi-boards
  175. // Note that we don't support DrvLineTo for multi-boards
  176. // Note that we don't support DrvEscape for multi-boards
  177. };
  178. #elif DBG
  179. // On Checked builds, thunk everything through Dbg calls...
  180. DRVFN gadrvfn[] = {
  181. { INDEX_DrvEnablePDEV, (PFN) DbgEnablePDEV },
  182. { INDEX_DrvCompletePDEV, (PFN) DbgCompletePDEV },
  183. { INDEX_DrvDisablePDEV, (PFN) DbgDisablePDEV },
  184. { INDEX_DrvEnableSurface, (PFN) DbgEnableSurface },
  185. { INDEX_DrvDisableSurface, (PFN) DbgDisableSurface },
  186. { INDEX_DrvAssertMode, (PFN) DbgAssertMode },
  187. { INDEX_DrvMovePointer, (PFN) DbgMovePointer },
  188. { INDEX_DrvSetPointerShape, (PFN) DbgSetPointerShape },
  189. { INDEX_DrvDitherColor, (PFN) DbgDitherColor },
  190. { INDEX_DrvSetPalette, (PFN) DbgSetPalette },
  191. { INDEX_DrvCopyBits, (PFN) DbgCopyBits },
  192. { INDEX_DrvBitBlt, (PFN) DbgBitBlt },
  193. { INDEX_DrvTextOut, (PFN) DbgTextOut },
  194. { INDEX_DrvGetModes, (PFN) DbgGetModes },
  195. { INDEX_DrvStrokePath, (PFN) DbgStrokePath },
  196. #if TARGET_BUILD > 351
  197. { INDEX_DrvLineTo, (PFN) DbgLineTo },
  198. #endif
  199. { INDEX_DrvFillPath, (PFN) DbgFillPath },
  200. { INDEX_DrvPaint, (PFN) DbgPaint },
  201. { INDEX_DrvStretchBlt, (PFN) DbgStretchBlt },
  202. { INDEX_DrvRealizeBrush, (PFN) DbgRealizeBrush },
  203. { INDEX_DrvCreateDeviceBitmap, (PFN) DbgCreateDeviceBitmap },
  204. { INDEX_DrvDeleteDeviceBitmap, (PFN) DbgDeleteDeviceBitmap },
  205. { INDEX_DrvDestroyFont, (PFN) DbgDestroyFont },
  206. { INDEX_DrvEscape, (PFN) DbgEscape },
  207. #if TARGET_BUILD > 351
  208. { INDEX_DrvGetDirectDrawInfo, (PFN) DrvGetDirectDrawInfo },
  209. { INDEX_DrvEnableDirectDraw, (PFN) DrvEnableDirectDraw },
  210. { INDEX_DrvDisableDirectDraw, (PFN) DrvDisableDirectDraw },
  211. #endif
  212. };
  213. #else
  214. // On Free builds, directly call the appropriate functions...
  215. DRVFN gadrvfn[] = {
  216. { INDEX_DrvEnablePDEV, (PFN) DrvEnablePDEV },
  217. { INDEX_DrvCompletePDEV, (PFN) DrvCompletePDEV },
  218. { INDEX_DrvDisablePDEV, (PFN) DrvDisablePDEV },
  219. { INDEX_DrvEnableSurface, (PFN) DrvEnableSurface },
  220. { INDEX_DrvDisableSurface, (PFN) DrvDisableSurface },
  221. { INDEX_DrvAssertMode, (PFN) DrvAssertMode },
  222. { INDEX_DrvMovePointer, (PFN) DrvMovePointer },
  223. { INDEX_DrvSetPointerShape, (PFN) DrvSetPointerShape },
  224. { INDEX_DrvDitherColor, (PFN) DrvDitherColor },
  225. { INDEX_DrvSetPalette, (PFN) DrvSetPalette },
  226. { INDEX_DrvCopyBits, (PFN) DrvCopyBits },
  227. { INDEX_DrvBitBlt, (PFN) DrvBitBlt },
  228. { INDEX_DrvTextOut, (PFN) DrvTextOut },
  229. { INDEX_DrvGetModes, (PFN) DrvGetModes },
  230. { INDEX_DrvStrokePath, (PFN) DrvStrokePath },
  231. #if TARGET_BUILD > 351
  232. { INDEX_DrvLineTo, (PFN) DrvLineTo },
  233. #endif
  234. { INDEX_DrvFillPath, (PFN) DrvFillPath },
  235. { INDEX_DrvPaint, (PFN) DrvPaint },
  236. { INDEX_DrvStretchBlt, (PFN) DrvStretchBlt },
  237. { INDEX_DrvRealizeBrush, (PFN) DrvRealizeBrush },
  238. { INDEX_DrvCreateDeviceBitmap, (PFN) DrvCreateDeviceBitmap },
  239. { INDEX_DrvDeleteDeviceBitmap, (PFN) DrvDeleteDeviceBitmap },
  240. { INDEX_DrvDestroyFont, (PFN) DrvDestroyFont },
  241. { INDEX_DrvEscape, (PFN) DrvEscape },
  242. #if TARGET_BUILD > 351
  243. { INDEX_DrvGetDirectDrawInfo, (PFN) DrvGetDirectDrawInfo },
  244. { INDEX_DrvEnableDirectDraw, (PFN) DrvEnableDirectDraw },
  245. { INDEX_DrvDisableDirectDraw, (PFN) DrvDisableDirectDraw },
  246. #endif
  247. };
  248. #endif
  249. ULONG gcdrvfn = sizeof(gadrvfn) / sizeof(DRVFN);
  250. /******************************Public*Routine******************************\
  251. * BOOL DrvEnableDriver
  252. *
  253. * Enables the driver by retrieving the drivers function table and version.
  254. *
  255. \**************************************************************************/
  256. BOOL DrvEnableDriver(
  257. ULONG iEngineVersion,
  258. ULONG cj,
  259. DRVENABLEDATA* pded)
  260. {
  261. // Engine Version is passed down so future drivers can support previous
  262. // engine versions. A next generation driver can support both the old
  263. // and new engine conventions if told what version of engine it is
  264. // working with. For the first version the driver does nothing with it.
  265. // Fill in as much as we can.
  266. if (cj >= sizeof(DRVENABLEDATA))
  267. pded->pdrvfn = gadrvfn;
  268. if (cj >= (sizeof(ULONG) * 2))
  269. pded->c = gcdrvfn;
  270. // DDI version this driver was targeted for is passed back to engine.
  271. // Future graphic's engine may break calls down to old driver format.
  272. if (cj >= sizeof(ULONG))
  273. pded->iDriverVersion = DDI_DRIVER_VERSION_NT4;
  274. return(TRUE);
  275. }
  276. /******************************Public*Routine******************************\
  277. * VOID DrvDisableDriver
  278. *
  279. * Tells the driver it is being disabled. Release any resources allocated in
  280. * DrvEnableDriver.
  281. *
  282. \**************************************************************************/
  283. VOID DrvDisableDriver(VOID)
  284. {
  285. return;
  286. }
  287. /******************************Public*Routine******************************\
  288. * BOOL bInitializeATI
  289. *
  290. * Initializes some private ATI info.
  291. *
  292. \**************************************************************************/
  293. BOOL bInitializeATI(PDEV* ppdev)
  294. {
  295. ENH_VERSION_NT info;
  296. ULONG ReturnedDataLength;
  297. info.FeatureFlags = 0;
  298. info.StructureVersion = 0;
  299. info.InterfaceVersion = 0; // Miniport needs these to be zero
  300. // Get some adapter information via a private IOCTL call:
  301. if (!AtiDeviceIoControl(ppdev->hDriver,
  302. IOCTL_VIDEO_ATI_GET_VERSION,
  303. &info,
  304. sizeof(ENH_VERSION_NT),
  305. &info,
  306. sizeof(ENH_VERSION_NT),
  307. &ReturnedDataLength))
  308. {
  309. DISPDBG((0, "bInitializeATI - Failed ATI_GET_VERSION"));
  310. goto ReturnFalse;
  311. }
  312. ppdev->FeatureFlags = info.FeatureFlags;
  313. ppdev->iAsic = info.ChipIndex;
  314. ppdev->iAperture = info.ApertureType;
  315. #if defined(ALPHA)
  316. if (!(ppdev->FeatureFlags & EVN_DENSE_CAPABLE))
  317. {
  318. // Can't use a sparse linear aperture.
  319. // Banked aperture is always sparse.
  320. // In either case, we execute no-aperture code...
  321. ppdev->iAperture = APERTURE_NONE;
  322. isDense = FALSE;
  323. }
  324. #endif
  325. if (info.ChipIndex == ASIC_88800GX)
  326. {
  327. ppdev->iMachType = MACH_MM_64;
  328. }
  329. else if (info.BusFlag & FL_MM_REGS)
  330. {
  331. ppdev->iMachType = MACH_MM_32;
  332. }
  333. else
  334. {
  335. ppdev->iMachType = MACH_IO_32;
  336. }
  337. return(TRUE);
  338. ReturnFalse:
  339. return(FALSE);
  340. }
  341. /******************************Public*Routine******************************\
  342. * DHPDEV DrvEnablePDEV
  343. *
  344. * Initializes a bunch of fields for GDI, based on the mode we've been asked
  345. * to do. This is the first thing called after DrvEnableDriver, when GDI
  346. * wants to get some information about us.
  347. *
  348. * (This function mostly returns back information; DrvEnableSurface is used
  349. * for initializing the hardware and driver components.)
  350. *
  351. \**************************************************************************/
  352. DHPDEV DrvEnablePDEV(
  353. DEVMODEW* pdm, // Contains data pertaining to requested mode
  354. PWSTR pwszLogAddr, // Logical address
  355. ULONG cPat, // Count of standard patterns
  356. HSURF* phsurfPatterns, // Buffer for standard patterns
  357. ULONG cjCaps, // Size of buffer for device caps 'pdevcaps'
  358. ULONG* pdevcaps, // Buffer for device caps, also known as 'gdiinfo'
  359. ULONG cjDevInfo, // Number of bytes in device info 'pdi'
  360. DEVINFO* pdi, // Device information
  361. #if TARGET_BUILD > 351
  362. HDEV hdev, // Used for callbacks
  363. #else
  364. PWSTR pwszDataFile,
  365. #endif
  366. PWSTR pwszDeviceName, // Device name
  367. HANDLE hDriver) // Kernel driver handle
  368. {
  369. PDEV* ppdev;
  370. // Future versions of NT had better supply 'devcaps' and 'devinfo'
  371. // structures that are the same size or larger than the current
  372. // structures:
  373. if ((cjCaps < sizeof(GDIINFO)) || (cjDevInfo < sizeof(DEVINFO)))
  374. {
  375. DISPDBG((0, "DrvEnablePDEV - Buffer size too small"));
  376. goto ReturnFailure0;
  377. }
  378. // Allocate a physical device structure. Note that we definitely
  379. // rely on the zero initialization:
  380. ppdev = AtiAllocMem(LPTR, FL_ZERO_MEMORY, sizeof(PDEV));
  381. if (ppdev == NULL)
  382. {
  383. DISPDBG((0, "DrvEnablePDEV - Failed AtiAllocMem"));
  384. goto ReturnFailure0;
  385. }
  386. ppdev->hDriver = hDriver;
  387. // Do some private ATI-specific initialization:
  388. if (!bInitializeATI(ppdev))
  389. {
  390. DISPDBG((0, "DrvEnablePDEV - Failed bInitializeATI"));
  391. goto ReturnFailure1;
  392. }
  393. // Get the current screen mode information. Set up device caps and
  394. // devinfo:
  395. if (!bInitializeModeFields(ppdev, (GDIINFO*) pdevcaps, pdi, pdm))
  396. {
  397. DISPDBG((0, "DrvEnablePDEV - Failed bInitializeModeFields"));
  398. goto ReturnFailure1;
  399. }
  400. // Initialize palette information.
  401. if (!bInitializePalette(ppdev, pdi))
  402. {
  403. DISPDBG((0, "DrvEnablePDEV - Failed bInitializePalette"));
  404. goto ReturnFailure1;
  405. }
  406. return((DHPDEV) ppdev);
  407. ReturnFailure1:
  408. DrvDisablePDEV((DHPDEV) ppdev);
  409. ReturnFailure0:
  410. DISPDBG((0, "Failed DrvEnablePDEV"));
  411. return(0);
  412. }
  413. /******************************Public*Routine******************************\
  414. * DrvDisablePDEV
  415. *
  416. * Release the resources allocated in DrvEnablePDEV. If a surface has been
  417. * enabled DrvDisableSurface will have already been called.
  418. *
  419. * Note that this function will be called when previewing modes in the
  420. * Display Applet, but not at system shutdown. If you need to reset the
  421. * hardware at shutdown, you can do it in the miniport by providing a
  422. * 'HwResetHw' entry point in the VIDEO_HW_INITIALIZATION_DATA structure.
  423. *
  424. * Note: In an error, we may call this before DrvEnablePDEV is done.
  425. *
  426. \**************************************************************************/
  427. VOID DrvDisablePDEV(
  428. DHPDEV dhpdev)
  429. {
  430. PDEV* ppdev;
  431. ppdev = (PDEV*) dhpdev;
  432. vUninitializePalette(ppdev);
  433. AtiFreeMem(ppdev);
  434. }
  435. /******************************Public*Routine******************************\
  436. * VOID DrvCompletePDEV
  437. *
  438. * Store the HPDEV, the engines handle for this PDEV, in the DHPDEV.
  439. *
  440. \**************************************************************************/
  441. VOID DrvCompletePDEV(
  442. DHPDEV dhpdev,
  443. HDEV hdev)
  444. {
  445. ((PDEV*) dhpdev)->hdevEng = hdev;
  446. }
  447. /******************************Public*Routine******************************\
  448. * HSURF DrvEnableSurface
  449. *
  450. * Creates the drawing surface, initializes the hardware, and initializes
  451. * driver components. This function is called after DrvEnablePDEV, and
  452. * performs the final device initialization.
  453. *
  454. \**************************************************************************/
  455. HSURF DrvEnableSurface(
  456. DHPDEV dhpdev)
  457. {
  458. PDEV* ppdev;
  459. HSURF hsurf;
  460. SIZEL sizl;
  461. DSURF* pdsurf;
  462. VOID* pvTmpBuffer;
  463. ppdev = (PDEV*) dhpdev;
  464. /////////////////////////////////////////////////////////////////////
  465. // First enable all the subcomponents.
  466. //
  467. // Note that the order in which these 'Enable' functions are called
  468. // may be significant in low off-screen memory conditions, because
  469. // the off-screen heap manager may fail some of the later
  470. // allocations...
  471. if (!bEnableHardware(ppdev))
  472. goto ReturnFailure;
  473. if (!bEnableBanking(ppdev))
  474. goto ReturnFailure;
  475. if (!bEnableOffscreenHeap(ppdev))
  476. goto ReturnFailure;
  477. if (!bEnablePointer(ppdev))
  478. goto ReturnFailure;
  479. if (!bEnableText(ppdev))
  480. goto ReturnFailure;
  481. if (!bEnableBrushCache(ppdev))
  482. goto ReturnFailure;
  483. if (!bEnablePalette(ppdev))
  484. goto ReturnFailure;
  485. if (!bEnableDirectDraw(ppdev))
  486. goto ReturnFailure;
  487. /////////////////////////////////////////////////////////////////////
  488. // Now create our private surface structure.
  489. //
  490. // Whenever we get a call to draw directly to the screen, we'll get
  491. // passed a pointer to a SURFOBJ whose 'dhpdev' field will point
  492. // to our PDEV structure, and whose 'dhsurf' field will point to the
  493. // following DSURF structure.
  494. //
  495. // Every device bitmap we create in DrvCreateDeviceBitmap will also
  496. // have its own unique DSURF structure allocated (but will share the
  497. // same PDEV). To make our code more polymorphic for handling drawing
  498. // to either the screen or an off-screen bitmap, we have the same
  499. // structure for both.
  500. pdsurf = AtiAllocMem(LPTR, FL_ZERO_MEMORY, sizeof(DSURF));
  501. if (pdsurf == NULL)
  502. {
  503. DISPDBG((0, "DrvEnableSurface - Failed pdsurf AtiAllocMem"));
  504. goto ReturnFailure;
  505. }
  506. ppdev->pdsurfScreen = pdsurf; // Remember it for clean-up
  507. pdsurf->poh = ppdev->pohScreen; // The screen is a surface, too
  508. pdsurf->dt = DT_SCREEN; // Not to be confused with a DIB
  509. pdsurf->sizl.cx = ppdev->cxScreen;
  510. pdsurf->sizl.cy = ppdev->cyScreen;
  511. pdsurf->ppdev = ppdev;
  512. /////////////////////////////////////////////////////////////////////
  513. // Next, have GDI create the actual SURFOBJ.
  514. //
  515. // Our drawing surface is going to be 'device-managed', meaning that
  516. // GDI cannot draw on the framebuffer bits directly, and as such we
  517. // create the surface via EngCreateDeviceSurface. By doing this, we ensure
  518. // that GDI will only ever access the bitmaps bits via the Drv calls
  519. // that we've HOOKed.
  520. //
  521. // If we could map the entire framebuffer linearly into main memory
  522. // (i.e., we didn't have to go through a 64k aperture), it would be
  523. // beneficial to create the surface via EngCreateBitmap, giving GDI a
  524. // pointer to the framebuffer bits. When we pass a call on to GDI
  525. // where it can't directly read/write to the surface bits because the
  526. // surface is device managed, it has to create a temporary bitmap and
  527. // call our DrvCopyBits routine to get/set a copy of the affected bits.
  528. // Fer example, the OpenGL component prefers to be able to write on the
  529. // framebuffer bits directly.
  530. sizl.cx = ppdev->cxScreen;
  531. sizl.cy = ppdev->cyScreen;
  532. // Let GDI manage 24bpp mach32 with linear aperture.
  533. if (ppdev->iBitmapFormat == BMF_24BPP &&
  534. ppdev->iAsic != ASIC_88800GX && ppdev->iAperture == APERTURE_FULL)
  535. {
  536. hsurf= ppdev->hsurfPunt;
  537. //
  538. // Also tell GDI that we don't want to be called back.
  539. //
  540. if (!EngAssociateSurface(hsurf, ppdev->hdevEng, 0))
  541. {
  542. DISPDBG((0, "DrvEnableSurface - Failed EngAssociateSurface"));
  543. goto ReturnFailure;
  544. }
  545. }
  546. else
  547. {
  548. hsurf = EngCreateDeviceSurface((DHSURF) pdsurf, sizl, ppdev->iBitmapFormat);
  549. if (hsurf == 0)
  550. {
  551. DISPDBG((0, "DrvEnableSurface - Failed EngCreateDeviceSurface"));
  552. goto ReturnFailure;
  553. }
  554. ppdev->hsurfScreen = hsurf; // Remember it for clean-up
  555. ppdev->bEnabled = TRUE; // We'll soon be in graphics mode
  556. /////////////////////////////////////////////////////////////////////
  557. // Now associate the surface and the PDEV.
  558. //
  559. // We have to associate the surface we just created with our physical
  560. // device so that GDI can get information related to the PDEV when
  561. // it's drawing to the surface (such as, for example, the length of
  562. // styles on the device when simulating styled lines).
  563. //
  564. if (!EngAssociateSurface(hsurf, ppdev->hdevEng, ppdev->flHooks))
  565. {
  566. DISPDBG((0, "DrvEnableSurface - Failed EngAssociateSurface"));
  567. goto ReturnFailure;
  568. }
  569. }
  570. // Create our generic temporary buffer, which may be used by any
  571. // component.
  572. pvTmpBuffer = AtiAllocMem(LMEM_FIXED, 0, TMP_BUFFER_SIZE);
  573. if (pvTmpBuffer == NULL)
  574. {
  575. DISPDBG((0, "DrvEnableSurface - Failed VirtualAlloc"));
  576. goto ReturnFailure;
  577. }
  578. ppdev->pvTmpBuffer = pvTmpBuffer;
  579. DISPDBG((5, "Passed DrvEnableSurface"));
  580. return(hsurf);
  581. ReturnFailure:
  582. DrvDisableSurface((DHPDEV) ppdev);
  583. DISPDBG((0, "Failed DrvEnableSurface"));
  584. return(0);
  585. }
  586. /******************************Public*Routine******************************\
  587. * VOID DrvDisableSurface
  588. *
  589. * Free resources allocated by DrvEnableSurface. Release the surface.
  590. *
  591. * Note that this function will be called when previewing modes in the
  592. * Display Applet, but not at system shutdown. If you need to reset the
  593. * hardware at shutdown, you can do it in the miniport by providing a
  594. * 'HwResetHw' entry point in the VIDEO_HW_INITIALIZATION_DATA structure.
  595. *
  596. * Note: In an error case, we may call this before DrvEnableSurface is
  597. * completely done.
  598. *
  599. \**************************************************************************/
  600. VOID DrvDisableSurface(
  601. DHPDEV dhpdev)
  602. {
  603. PDEV* ppdev;
  604. ppdev = (PDEV*) dhpdev;
  605. // Note: In an error case, some of the following relies on the
  606. // fact that the PDEV is zero-initialized, so fields like
  607. // 'hsurfScreen' will be zero unless the surface has been
  608. // sucessfully initialized, and makes the assumption that
  609. // EngDeleteSurface can take '0' as a parameter.
  610. vDisableDirectDraw(ppdev);
  611. vDisablePalette(ppdev);
  612. vDisableBrushCache(ppdev);
  613. vDisableText(ppdev);
  614. vDisablePointer(ppdev);
  615. vDisableOffscreenHeap(ppdev);
  616. vDisableBanking(ppdev);
  617. vDisableHardware(ppdev);
  618. AtiFreeMem(ppdev->pvTmpBuffer);
  619. EngDeleteSurface(ppdev->hsurfScreen);
  620. AtiFreeMem(ppdev->pdsurfScreen);
  621. }
  622. /******************************Public*Routine******************************\
  623. * VOID DrvAssertMode
  624. *
  625. * This asks the device to reset itself to the mode of the pdev passed in.
  626. *
  627. \**************************************************************************/
  628. #if TARGET_BUILD > 351
  629. BOOL DrvAssertMode(
  630. #else
  631. VOID DrvAssertMode(
  632. #endif
  633. DHPDEV dhpdev,
  634. BOOL bEnable)
  635. {
  636. PDEV* ppdev;
  637. ppdev = (PDEV*) dhpdev;
  638. if (!bEnable)
  639. {
  640. //////////////////////////////////////////////////////////////
  641. // Disable - Switch to full-screen mode
  642. vAssertModeDirectDraw(ppdev, FALSE);
  643. vAssertModePalette(ppdev, FALSE);
  644. vAssertModeBrushCache(ppdev, FALSE);
  645. vAssertModeText(ppdev, FALSE);
  646. vAssertModePointer(ppdev, FALSE);
  647. if (bAssertModeOffscreenHeap(ppdev, FALSE))
  648. {
  649. vAssertModeBanking(ppdev, FALSE);
  650. if (bAssertModeHardware(ppdev, FALSE))
  651. {
  652. ppdev->bEnabled = FALSE;
  653. #if TARGET_BUILD > 351
  654. return(TRUE);
  655. #else
  656. return;
  657. #endif
  658. }
  659. //////////////////////////////////////////////////////////
  660. // We failed to switch to full-screen. So undo everything:
  661. vAssertModeBanking(ppdev, TRUE);
  662. bAssertModeOffscreenHeap(ppdev, TRUE); // We don't need to check
  663. } // return code with TRUE
  664. vAssertModePointer(ppdev, TRUE);
  665. vAssertModeText(ppdev, TRUE);
  666. vAssertModeBrushCache(ppdev, TRUE);
  667. vAssertModePalette(ppdev, TRUE);
  668. vAssertModeDirectDraw(ppdev, TRUE);
  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. #if TARGET_BUILD > 351
  677. if (!bAssertModeHardware(ppdev, TRUE))
  678. {
  679. return FALSE;
  680. }
  681. #else
  682. bAssertModeHardware(ppdev, TRUE);
  683. #endif
  684. vAssertModeBanking(ppdev, TRUE);
  685. bAssertModeOffscreenHeap(ppdev, TRUE); // don't need the return
  686. vAssertModePointer(ppdev, TRUE);
  687. vAssertModeText(ppdev, TRUE);
  688. vAssertModeBrushCache(ppdev, TRUE);
  689. vAssertModePalette(ppdev, TRUE);
  690. vAssertModeDirectDraw(ppdev, TRUE);
  691. ppdev->bEnabled = TRUE;
  692. #if TARGET_BUILD > 351
  693. return TRUE;
  694. #endif
  695. }
  696. #if TARGET_BUILD > 351
  697. return FALSE; // If we get here, we've failed!
  698. #endif
  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. //
  756. // We currently do not support Extra information in the driver
  757. //
  758. pdm->dmDriverExtra = DRIVER_EXTRA_SIZE;
  759. pdm->dmSize = sizeof(DEVMODEW);
  760. pdm->dmBitsPerPel = pVideoTemp->NumberOfPlanes *
  761. pVideoTemp->BitsPerPlane;
  762. pdm->dmPelsWidth = pVideoTemp->VisScreenWidth;
  763. pdm->dmPelsHeight = pVideoTemp->VisScreenHeight;
  764. pdm->dmDisplayFrequency = pVideoTemp->Frequency;
  765. #if TARGET_BUILD > 351
  766. pdm->dmDisplayFlags = 0;
  767. pdm->dmFields = DM_BITSPERPEL |
  768. DM_PELSWIDTH |
  769. DM_PELSHEIGHT |
  770. DM_DISPLAYFREQUENCY |
  771. DM_DISPLAYFLAGS ;
  772. #else
  773. if (pVideoTemp->AttributeFlags & VIDEO_MODE_INTERLACED)
  774. {
  775. pdm->dmDisplayFlags |= DM_INTERLACED;
  776. }
  777. #endif
  778. //DISPDBG((0, "pdm: %4li bpp, %4li x %4li, %4li Hz",
  779. //pdm->dmBitsPerPel, pdm->dmPelsWidth, pdm->dmPelsHeight, pdm->dmDisplayFrequency ));
  780. //
  781. // Go to the next DEVMODE entry in the buffer.
  782. //
  783. cOutputModes--;
  784. pdm = (LPDEVMODEW) ( ((ULONG_PTR)pdm) + sizeof(DEVMODEW) +
  785. DRIVER_EXTRA_SIZE);
  786. cbOutputSize += (sizeof(DEVMODEW) + DRIVER_EXTRA_SIZE);
  787. }
  788. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  789. (((PUCHAR)pVideoTemp) + cbModeSize);
  790. } while (--cModes);
  791. }
  792. AtiFreeMem(pVideoModeInformation);
  793. return(cbOutputSize);
  794. }
  795. /******************************Public*Routine******************************\
  796. * BOOL bAssertModeHardware
  797. *
  798. * Sets the appropriate hardware state for graphics mode or full-screen.
  799. *
  800. \**************************************************************************/
  801. BOOL bAssertModeHardware(
  802. PDEV* ppdev,
  803. BOOL bEnable)
  804. {
  805. DWORD ReturnedDataLength;
  806. ULONG ulReturn;
  807. if (bEnable)
  808. {
  809. // Call the miniport via an IOCTL to set the graphics mode.
  810. if (!AtiDeviceIoControl(ppdev->hDriver,
  811. IOCTL_VIDEO_SET_CURRENT_MODE,
  812. &ppdev->ulMode, // input buffer
  813. sizeof(DWORD),
  814. NULL,
  815. 0,
  816. &ReturnedDataLength))
  817. {
  818. DISPDBG((0, "bAssertModeHardware - Failed set IOCTL"));
  819. return(FALSE);
  820. }
  821. vResetClipping(ppdev);
  822. // Set some Mach64 defaults:
  823. if (ppdev->iMachType == MACH_MM_64)
  824. {
  825. BYTE* pjMmBase;
  826. pjMmBase = ppdev->pjMmBase;
  827. M64_CHECK_FIFO_SPACE(ppdev, pjMmBase, 1);
  828. M64_OD(pjMmBase, DP_PIX_WIDTH, ppdev->ulMonoPixelWidth);
  829. vSetDefaultContext(ppdev);
  830. }
  831. }
  832. else
  833. {
  834. // Call the kernel driver to reset the device to a known state.
  835. // NTVDM will take things from there:
  836. if (!AtiDeviceIoControl(ppdev->hDriver,
  837. IOCTL_VIDEO_RESET_DEVICE,
  838. NULL,
  839. 0,
  840. NULL,
  841. 0,
  842. &ulReturn))
  843. {
  844. DISPDBG((0, "bAssertModeHardware - Failed reset IOCTL"));
  845. return(FALSE);
  846. }
  847. }
  848. DISPDBG((5, "Passed bAssertModeHardware"));
  849. return(TRUE);
  850. }
  851. /******************************Public*Routine******************************\
  852. * BOOL bEnableHardware
  853. *
  854. * Puts the hardware in the requested mode and initializes it.
  855. *
  856. * Note: Should be called before any access is done to the hardware from
  857. * the display driver.
  858. *
  859. \**************************************************************************/
  860. BOOL bEnableHardware(
  861. PDEV* ppdev)
  862. {
  863. BYTE* pjIoBase;
  864. VIDEO_PUBLIC_ACCESS_RANGES VideoAccessRange[2];
  865. VIDEO_MEMORY VideoMemory;
  866. VIDEO_MEMORY_INFORMATION VideoMemoryInfo;
  867. DWORD ReturnedDataLength;
  868. ppdev->pjIoBase = NULL;
  869. ppdev->pjMmBase = NULL;
  870. // Map io ports into virtual memory:
  871. if (!AtiDeviceIoControl(ppdev->hDriver,
  872. IOCTL_VIDEO_QUERY_PUBLIC_ACCESS_RANGES,
  873. NULL, // input buffer
  874. 0,
  875. &VideoAccessRange, // output buffer
  876. sizeof(VideoAccessRange),
  877. &ReturnedDataLength))
  878. {
  879. DISPDBG((0, "bEnableHardware - Initialization error mapping IO port base"));
  880. goto ReturnFalse;
  881. }
  882. ppdev->pjIoBase = (UCHAR*) VideoAccessRange[0].VirtualAddress;
  883. ppdev->pjMmBase_Ext = (BYTE*) VideoAccessRange[1].VirtualAddress;
  884. // ------------------------- ATI-specific ----------------------------
  885. // Call the miniport via an IOCTL to set the graphics mode.
  886. // Because of a hardware quirk, 4 BPP causes the mach64 to alter its
  887. // video memory size when you do a SET_CURRENT_MODE, so we do it here
  888. // first so that MAP_VIDEO_MEMORY maps the correct amount of memory.
  889. if (!AtiDeviceIoControl(ppdev->hDriver,
  890. IOCTL_VIDEO_SET_CURRENT_MODE,
  891. &ppdev->ulMode, // input buffer
  892. sizeof(DWORD),
  893. NULL,
  894. 0,
  895. &ReturnedDataLength))
  896. {
  897. DISPDBG((0, "bEnableHardware - Failed set IOCTL"));
  898. goto ReturnFalse;
  899. }
  900. ppdev->pModeInfo = AtiAllocMem( LPTR, FL_ZERO_MEMORY, sizeof (ATI_MODE_INFO) );
  901. if( ppdev->pModeInfo == NULL )
  902. {
  903. DISPDBG((0, "bEnableHardware - Failed memory allocation" ));
  904. goto ReturnFalse;
  905. }
  906. if( !AtiDeviceIoControl( ppdev->hDriver,
  907. IOCTL_VIDEO_ATI_GET_MODE_INFORMATION,
  908. ppdev->pModeInfo,
  909. sizeof (ATI_MODE_INFO),
  910. ppdev->pModeInfo,
  911. sizeof (ATI_MODE_INFO),
  912. &ReturnedDataLength
  913. ) )
  914. {
  915. DISPDBG((0, "bEnableHardware - Failed to get ATI-specific mode information" ));
  916. goto ReturnFalse;
  917. }
  918. // Get the linear memory address range.
  919. VideoMemory.RequestedVirtualAddress = NULL;
  920. if (!AtiDeviceIoControl(ppdev->hDriver,
  921. IOCTL_VIDEO_MAP_VIDEO_MEMORY,
  922. &VideoMemory, // input buffer
  923. sizeof(VIDEO_MEMORY),
  924. &VideoMemoryInfo, // output buffer
  925. sizeof(VideoMemoryInfo),
  926. &ReturnedDataLength))
  927. {
  928. DISPDBG((0, "bEnableHardware - Error mapping buffer address"));
  929. goto ReturnFalse;
  930. }
  931. // Record the Frame Buffer Linear Address.
  932. ppdev->cjBank = VideoMemoryInfo.FrameBufferLength;
  933. // 128K VGA Aperture?
  934. if (ppdev->cjBank == 0x20000)
  935. {
  936. ppdev->cjBank = 0x10000; // true banksize is 64K
  937. }
  938. ppdev->pjScreen = (BYTE*) VideoMemoryInfo.FrameBufferBase;
  939. // So we can free it later in vDisableHardware...
  940. ppdev->VideoRamBase = (BYTE*) VideoMemoryInfo.VideoRamBase;
  941. if (ppdev->iMachType == MACH_MM_64)
  942. {
  943. ppdev->pjMmBase = (BYTE*) VideoMemoryInfo.VideoRamBase
  944. + VideoMemoryInfo.FrameBufferLength
  945. - 0x400;
  946. }
  947. else
  948. {
  949. ppdev->pjMmBase = (BYTE*) VideoMemoryInfo.FrameBufferBase;
  950. }
  951. pjIoBase = ppdev->pjIoBase;
  952. // We finally have enough information to calculate the dimensions
  953. // of on-screen and off-screen memory:
  954. ppdev->cxMemory = ppdev->lDelta / ppdev->cjPelSize;
  955. ppdev->cyMemory = VideoMemoryInfo.VideoRamLength / ppdev->lDelta;
  956. if (VideoMemoryInfo.VideoRamLength <= VideoMemoryInfo.FrameBufferLength)
  957. {
  958. ppdev->flCaps |= CAPS_LINEAR_FRAMEBUFFER;
  959. }
  960. ppdev->ulTearOffset = (ULONG)(ppdev->pjScreen - ppdev->VideoRamBase);
  961. ppdev->ulVramOffset = ppdev->ulTearOffset/8;
  962. if (ppdev->iBitmapFormat != BMF_24BPP)
  963. ppdev->ulScreenOffsetAndPitch = PACKPAIR(ppdev->ulVramOffset,
  964. ppdev->cxMemory * 8);
  965. else
  966. ppdev->ulScreenOffsetAndPitch = PACKPAIR(ppdev->ulVramOffset,
  967. (ppdev->cxMemory * 3) * 8);
  968. // The default pixel width is setup to have monochrome as the
  969. // host data path pixel width:
  970. switch (ppdev->iBitmapFormat)
  971. {
  972. case BMF_4BPP: ppdev->ulMonoPixelWidth = 0x00000101; break;
  973. case BMF_8BPP: ppdev->ulMonoPixelWidth = 0x00000202; break;
  974. case BMF_16BPP: ppdev->ulMonoPixelWidth = 0x00000404; break;
  975. case BMF_24BPP: ppdev->ulMonoPixelWidth = 0x01000202; break;
  976. case BMF_32BPP: ppdev->ulMonoPixelWidth = 0x00000606; break;
  977. }
  978. DISPDBG((1, "RamLength = %lxH, lDelta = %li",
  979. VideoMemoryInfo.VideoRamLength,
  980. ppdev->lDelta));
  981. if ((ppdev->iMachType == MACH_IO_32) || (ppdev->iMachType == MACH_MM_32))
  982. {
  983. // The Mach32 and Mach8 can't handle coordinates larger than 1535:
  984. ppdev->cyMemory = min(ppdev->cyMemory, 1535);
  985. }
  986. if (ppdev->iMachType == MACH_MM_32)
  987. {
  988. // Can do memory-mapped IO:
  989. ppdev->pfnFillSolid = vM32FillSolid;
  990. ppdev->pfnFillPatColor = vM32FillPatColor;
  991. ppdev->pfnFillPatMonochrome = vM32FillPatMonochrome;
  992. ppdev->pfnXfer1bpp = vM32Xfer1bpp;
  993. ppdev->pfnXfer4bpp = vM32Xfer4bpp;
  994. ppdev->pfnXfer8bpp = vM32Xfer8bpp;
  995. ppdev->pfnXferNative = vM32XferNative;
  996. ppdev->pfnCopyBlt = vM32CopyBlt;
  997. ppdev->pfnLineToTrivial = vM32LineToTrivial;
  998. if (ppdev->iAsic == ASIC_68800AX) // Timing problem.
  999. ppdev->pfnTextOut = bI32TextOut;
  1000. else
  1001. ppdev->pfnTextOut = bM32TextOut;
  1002. ppdev->pfnStretchDIB = bM32StretchDIB;
  1003. }
  1004. else if (ppdev->iMachType == MACH_IO_32)
  1005. {
  1006. ppdev->pfnFillSolid = vI32FillSolid;
  1007. ppdev->pfnFillPatColor = vI32FillPatColor;
  1008. ppdev->pfnFillPatMonochrome = vI32FillPatMonochrome;
  1009. ppdev->pfnXfer1bpp = vI32Xfer1bpp;
  1010. ppdev->pfnXfer4bpp = vI32Xfer4bpp;
  1011. ppdev->pfnXfer8bpp = vI32Xfer8bpp;
  1012. ppdev->pfnXferNative = vI32XferNative;
  1013. ppdev->pfnCopyBlt = vI32CopyBlt;
  1014. ppdev->pfnLineToTrivial = vI32LineToTrivial;
  1015. ppdev->pfnTextOut = bI32TextOut;
  1016. ppdev->pfnStretchDIB = bI32StretchDIB;
  1017. }
  1018. else
  1019. {
  1020. // ppdev->iMachType == MACH_MM_64
  1021. ppdev->pfnFillSolid = vM64FillSolid;
  1022. ppdev->pfnFillPatColor = vM64FillPatColor;
  1023. ppdev->pfnFillPatMonochrome = vM64FillPatMonochrome;
  1024. ppdev->pfnXfer1bpp = vM64Xfer1bpp;
  1025. ppdev->pfnXfer4bpp = vM64Xfer4bpp;
  1026. ppdev->pfnXfer8bpp = vM64Xfer8bpp;
  1027. ppdev->pfnXferNative = vM64XferNative;
  1028. if (!(ppdev->FeatureFlags & EVN_SDRAM_1M))
  1029. {
  1030. ppdev->pfnCopyBlt = vM64CopyBlt;
  1031. }
  1032. else
  1033. {
  1034. // Special version to fix screen source FIFO bug in VT-A4
  1035. // with 1 MB of SDRAM.
  1036. ppdev->pfnCopyBlt = vM64CopyBlt_VTA4;
  1037. }
  1038. ppdev->pfnLineToTrivial = vM64LineToTrivial;
  1039. ppdev->pfnTextOut = bM64TextOut;
  1040. ppdev->pfnStretchDIB = bM64StretchDIB;
  1041. if (ppdev->iBitmapFormat == BMF_24BPP)
  1042. {
  1043. ppdev->pfnFillSolid = vM64FillSolid24;
  1044. ppdev->pfnFillPatColor = vM64FillPatColor24;
  1045. ppdev->pfnFillPatMonochrome = vM64FillPatMonochrome24;
  1046. ppdev->pfnXferNative = vM64XferNative24;
  1047. if (!(ppdev->FeatureFlags & EVN_SDRAM_1M))
  1048. {
  1049. ppdev->pfnCopyBlt = vM64CopyBlt24;
  1050. }
  1051. else
  1052. {
  1053. // Special version to fix screen source FIFO bug in VT-A4
  1054. // with 1 MB of SDRAM.
  1055. ppdev->pfnCopyBlt = vM64CopyBlt24_VTA4;
  1056. }
  1057. ppdev->pfnLineToTrivial = vM64LineToTrivial24;
  1058. ppdev->pfnTextOut = bM64TextOut24;
  1059. }
  1060. vEnableContexts(ppdev);
  1061. }
  1062. if ((ppdev->iAsic != ASIC_38800_1) &&
  1063. ((ppdev->iAperture != APERTURE_NONE)||(ppdev->iMachType == MACH_MM_64)))
  1064. {
  1065. ppdev->pfnGetBits = vGetBits;
  1066. ppdev->pfnPutBits = vPutBits;
  1067. }
  1068. else
  1069. {
  1070. ppdev->pfnGetBits = vI32GetBits;
  1071. ppdev->pfnPutBits = vI32PutBits;
  1072. }
  1073. // Now we can set the mode, unlock the accelerator, and reset the
  1074. // clipping:
  1075. if (!bAssertModeHardware(ppdev, TRUE))
  1076. goto ReturnFalse;
  1077. DISPDBG((0, "%li bpp, %li x %li, pjScreen = %lx, cjBank = %lxH, pjMmBase = %lx",
  1078. ppdev->cBitsPerPel, ppdev->cxMemory, ppdev->cyMemory, ppdev->pjScreen, ppdev->cjBank, ppdev->pjMmBase));
  1079. DISPDBG((5, "Passed bEnableHardware"));
  1080. return(TRUE);
  1081. ReturnFalse:
  1082. DISPDBG((0, "Failed bEnableHardware"));
  1083. return(FALSE);
  1084. }
  1085. /******************************Public*Routine******************************\
  1086. * VOID vDisableHardware
  1087. *
  1088. * Undoes anything done in bEnableHardware.
  1089. *
  1090. * Note: In an error case, we may call this before bEnableHardware is
  1091. * completely done.
  1092. *
  1093. \**************************************************************************/
  1094. VOID vDisableHardware(
  1095. PDEV* ppdev)
  1096. {
  1097. DWORD ReturnedDataLength;
  1098. VIDEO_MEMORY VideoMemory[2];
  1099. VideoMemory[0].RequestedVirtualAddress = ppdev->VideoRamBase;
  1100. if (!AtiDeviceIoControl(ppdev->hDriver,
  1101. IOCTL_VIDEO_UNMAP_VIDEO_MEMORY,
  1102. VideoMemory,
  1103. sizeof(VIDEO_MEMORY),
  1104. NULL,
  1105. 0,
  1106. &ReturnedDataLength))
  1107. {
  1108. DISPDBG((0, "vDisableHardware failed IOCTL_VIDEO_UNMAP_VIDEO"));
  1109. }
  1110. VideoMemory[0].RequestedVirtualAddress = ppdev->pjIoBase;
  1111. VideoMemory[1].RequestedVirtualAddress = ppdev->VideoRamBase;
  1112. if (!AtiDeviceIoControl(ppdev->hDriver,
  1113. IOCTL_VIDEO_FREE_PUBLIC_ACCESS_RANGES,
  1114. VideoMemory,
  1115. sizeof(VideoMemory),
  1116. NULL,
  1117. 0,
  1118. &ReturnedDataLength))
  1119. {
  1120. DISPDBG((0, "vDisableHardware failed IOCTL_VIDEO_FREE_PUBLIC_ACCESS"));
  1121. }
  1122. }
  1123. /******************************Public*Routine******************************\
  1124. * BOOL bInitializeModeFields
  1125. *
  1126. * Initializes a bunch of fields in the pdev, devcaps (aka gdiinfo), and
  1127. * devinfo based on the requested mode.
  1128. *
  1129. \**************************************************************************/
  1130. BOOL bInitializeModeFields(
  1131. PDEV* ppdev,
  1132. GDIINFO* pgdi,
  1133. DEVINFO* pdi,
  1134. DEVMODEW* pdm)
  1135. {
  1136. ULONG cModes;
  1137. PVIDEO_MODE_INFORMATION pVideoBuffer;
  1138. PVIDEO_MODE_INFORMATION pVideoModeSelected;
  1139. PVIDEO_MODE_INFORMATION pVideoTemp;
  1140. BOOL bSelectDefault;
  1141. VIDEO_MODE_INFORMATION VideoModeInformation;
  1142. ULONG cbModeSize;
  1143. // Call the miniport to get mode information
  1144. cModes = getAvailableModes(ppdev->hDriver, &pVideoBuffer, &cbModeSize);
  1145. if (cModes == 0)
  1146. goto ReturnFalse;
  1147. // Now see if the requested mode has a match in that table.
  1148. pVideoModeSelected = NULL;
  1149. pVideoTemp = pVideoBuffer;
  1150. if ((pdm->dmPelsWidth == 0) &&
  1151. (pdm->dmPelsHeight == 0) &&
  1152. (pdm->dmBitsPerPel == 0) &&
  1153. (pdm->dmDisplayFrequency == 0))
  1154. {
  1155. DISPDBG((1, "Default mode requested"));
  1156. bSelectDefault = TRUE;
  1157. }
  1158. else
  1159. {
  1160. DISPDBG((1, "Requested mode..."));
  1161. DISPDBG((1, " Screen width -- %li", pdm->dmPelsWidth));
  1162. DISPDBG((1, " Screen height -- %li", pdm->dmPelsHeight));
  1163. DISPDBG((1, " Bits per pel -- %li", pdm->dmBitsPerPel));
  1164. DISPDBG((1, " Frequency -- %li", pdm->dmDisplayFrequency));
  1165. bSelectDefault = FALSE;
  1166. }
  1167. while (cModes--)
  1168. {
  1169. if (pVideoTemp->Length != 0)
  1170. {
  1171. DISPDBG((8, " Checking against miniport mode:"));
  1172. DISPDBG((8, " Screen width -- %li", pVideoTemp->VisScreenWidth));
  1173. DISPDBG((8, " Screen height -- %li", pVideoTemp->VisScreenHeight));
  1174. DISPDBG((8, " Bits per pel -- %li", pVideoTemp->BitsPerPlane *
  1175. pVideoTemp->NumberOfPlanes));
  1176. DISPDBG((8, " Frequency -- %li", pVideoTemp->Frequency));
  1177. if (bSelectDefault ||
  1178. ((pVideoTemp->VisScreenWidth == pdm->dmPelsWidth) &&
  1179. (pVideoTemp->VisScreenHeight == pdm->dmPelsHeight) &&
  1180. (pVideoTemp->BitsPerPlane *
  1181. pVideoTemp->NumberOfPlanes == pdm->dmBitsPerPel) &&
  1182. (pVideoTemp->Frequency == pdm->dmDisplayFrequency)))
  1183. {
  1184. pVideoModeSelected = pVideoTemp;
  1185. DISPDBG((1, "...Found a mode match!"));
  1186. break;
  1187. }
  1188. }
  1189. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  1190. (((PUCHAR)pVideoTemp) + cbModeSize);
  1191. }
  1192. // If no mode has been found, return an error
  1193. if (pVideoModeSelected == NULL)
  1194. {
  1195. DISPDBG((1, "...Couldn't find a mode match!"));
  1196. AtiFreeMem(pVideoBuffer);
  1197. goto ReturnFalse;
  1198. }
  1199. // Punt all rev 3 VLB cards in 8bpp to the 8514/A driver.
  1200. // Timing problems on "MIO" cards are impossible to deal with.
  1201. // They only show up on 5% of all mach32 cards.
  1202. if ((ppdev->FeatureFlags & EVN_MIO_BUG) && pVideoModeSelected->BitsPerPlane == 8)
  1203. {
  1204. AtiFreeMem(pVideoBuffer);
  1205. goto ReturnFalse;
  1206. }
  1207. // We won't support 24bpp without a linear frame buffer.
  1208. if (pVideoModeSelected->BitsPerPlane == 24 && ppdev->iAperture != APERTURE_FULL)
  1209. {
  1210. AtiFreeMem(pVideoBuffer);
  1211. goto ReturnFalse;
  1212. }
  1213. // We have chosen the one we want. Save it in a stack buffer and
  1214. // get rid of allocated memory before we forget to free it.
  1215. VideoModeInformation = *pVideoModeSelected;
  1216. AtiFreeMem(pVideoBuffer);
  1217. #if DEBUG_HEAP
  1218. VideoModeInformation.VisScreenWidth = 640;
  1219. VideoModeInformation.VisScreenHeight = 480;
  1220. #endif
  1221. // Set up screen information from the mini-port:
  1222. ppdev->ulMode = VideoModeInformation.ModeIndex;
  1223. ppdev->cxScreen = VideoModeInformation.VisScreenWidth;
  1224. ppdev->cyScreen = VideoModeInformation.VisScreenHeight;
  1225. ppdev->lDelta = VideoModeInformation.ScreenStride;
  1226. ppdev->cBitsPerPel = VideoModeInformation.BitsPerPlane;
  1227. DISPDBG((1, "ScreenStride: %lx", VideoModeInformation.ScreenStride));
  1228. ppdev->flHooks = (HOOK_BITBLT |
  1229. HOOK_TEXTOUT |
  1230. HOOK_FILLPATH |
  1231. HOOK_COPYBITS |
  1232. HOOK_STROKEPATH |
  1233. HOOK_STRETCHBLT |
  1234. #if TARGET_BUILD > 351
  1235. HOOK_LINETO |
  1236. #endif
  1237. HOOK_PAINT);
  1238. // Fill in the GDIINFO data structure with the default 8bpp values:
  1239. *pgdi = ggdiDefault;
  1240. // Now overwrite the defaults with the relevant information returned
  1241. // from the kernel driver:
  1242. pgdi->ulHorzSize = VideoModeInformation.XMillimeter;
  1243. pgdi->ulVertSize = VideoModeInformation.YMillimeter;
  1244. pgdi->ulHorzRes = VideoModeInformation.VisScreenWidth;
  1245. pgdi->ulVertRes = VideoModeInformation.VisScreenHeight;
  1246. #if TARGET_BUILD > 351
  1247. pgdi->ulPanningHorzRes = VideoModeInformation.VisScreenWidth;
  1248. pgdi->ulPanningVertRes = VideoModeInformation.VisScreenHeight;
  1249. #else
  1250. pgdi->ulDesktopHorzRes = VideoModeInformation.VisScreenWidth;
  1251. pgdi->ulDesktopVertRes = VideoModeInformation.VisScreenHeight;
  1252. #endif
  1253. pgdi->cBitsPixel = VideoModeInformation.BitsPerPlane;
  1254. pgdi->cPlanes = VideoModeInformation.NumberOfPlanes;
  1255. pgdi->ulVRefresh = VideoModeInformation.Frequency;
  1256. pgdi->ulDACRed = VideoModeInformation.NumberRedBits;
  1257. pgdi->ulDACGreen = VideoModeInformation.NumberGreenBits;
  1258. pgdi->ulDACBlue = VideoModeInformation.NumberBlueBits;
  1259. pgdi->ulLogPixelsX = pdm->dmLogPixels;
  1260. pgdi->ulLogPixelsY = pdm->dmLogPixels;
  1261. // Fill in the devinfo structure with the default 8bpp values:
  1262. *pdi = gdevinfoDefault;
  1263. if (VideoModeInformation.BitsPerPlane == 8)
  1264. {
  1265. ppdev->cPelSize = 0;
  1266. ppdev->cjPelSize = 1;
  1267. ppdev->iBitmapFormat = BMF_8BPP;
  1268. ppdev->ulWhite = 0xff;
  1269. // Assuming palette is orthogonal - all colors are same size.
  1270. ppdev->cPaletteShift = 8 - pgdi->ulDACRed;
  1271. DISPDBG((3, "palette shift = %d\n", ppdev->cPaletteShift));
  1272. }
  1273. else if ((VideoModeInformation.BitsPerPlane == 16) ||
  1274. (VideoModeInformation.BitsPerPlane == 15))
  1275. {
  1276. ppdev->cPelSize = 1;
  1277. ppdev->cjPelSize = 2;
  1278. ppdev->iBitmapFormat = BMF_16BPP;
  1279. ppdev->ulWhite = 0xffff;
  1280. ppdev->flRed = VideoModeInformation.RedMask;
  1281. ppdev->flGreen = VideoModeInformation.GreenMask;
  1282. ppdev->flBlue = VideoModeInformation.BlueMask;
  1283. pgdi->ulNumColors = (ULONG) -1;
  1284. pgdi->ulNumPalReg = 0;
  1285. pgdi->ulHTOutputFormat = HT_FORMAT_16BPP;
  1286. pdi->iDitherFormat = BMF_16BPP;
  1287. pdi->flGraphicsCaps &= ~(GCAPS_PALMANAGED | GCAPS_COLOR_DITHER);
  1288. }
  1289. else if (VideoModeInformation.BitsPerPlane == 24)
  1290. {
  1291. ppdev->cPelSize = 0; // not used?!
  1292. ppdev->cjPelSize = 3;
  1293. ppdev->iBitmapFormat = BMF_24BPP;
  1294. ppdev->ulWhite = 0xffffff;
  1295. ppdev->flRed = VideoModeInformation.RedMask;
  1296. ppdev->flGreen = VideoModeInformation.GreenMask;
  1297. ppdev->flBlue = VideoModeInformation.BlueMask;
  1298. pgdi->ulNumColors = (ULONG) -1;
  1299. pgdi->ulNumPalReg = 0;
  1300. pgdi->ulHTOutputFormat = HT_FORMAT_24BPP;
  1301. pdi->iDitherFormat = BMF_24BPP;
  1302. pdi->flGraphicsCaps &= ~(GCAPS_PALMANAGED | GCAPS_COLOR_DITHER);
  1303. }
  1304. else
  1305. {
  1306. ASSERTDD(VideoModeInformation.BitsPerPlane == 32,
  1307. "This driver supports only 8, 16, 24 and 32bpp");
  1308. ppdev->cPelSize = 2;
  1309. ppdev->cjPelSize = 4;
  1310. ppdev->iBitmapFormat = BMF_32BPP;
  1311. ppdev->ulWhite = 0xffffffff;
  1312. ppdev->flRed = VideoModeInformation.RedMask;
  1313. ppdev->flGreen = VideoModeInformation.GreenMask;
  1314. ppdev->flBlue = VideoModeInformation.BlueMask;
  1315. pgdi->ulNumColors = (ULONG) -1;
  1316. pgdi->ulNumPalReg = 0;
  1317. pgdi->ulHTOutputFormat = HT_FORMAT_32BPP;
  1318. pdi->iDitherFormat = BMF_32BPP;
  1319. pdi->flGraphicsCaps &= ~(GCAPS_PALMANAGED | GCAPS_COLOR_DITHER);
  1320. }
  1321. DISPDBG((5, "Passed bInitializeModeFields"));
  1322. return(TRUE);
  1323. ReturnFalse:
  1324. DISPDBG((0, "Failed bInitializeModeFields"));
  1325. return(FALSE);
  1326. }
  1327. /******************************Public*Routine******************************\
  1328. * DWORD getAvailableModes
  1329. *
  1330. * Calls the miniport to get the list of modes supported by the kernel driver,
  1331. * and returns the list of modes supported by the diplay driver among those
  1332. *
  1333. * returns the number of entries in the videomode buffer.
  1334. * 0 means no modes are supported by the miniport or that an error occured.
  1335. *
  1336. * NOTE: the buffer must be freed up by the caller.
  1337. *
  1338. \**************************************************************************/
  1339. DWORD getAvailableModes(
  1340. HANDLE hDriver,
  1341. PVIDEO_MODE_INFORMATION* modeInformation, // Must be freed by caller
  1342. DWORD* cbModeSize)
  1343. {
  1344. ULONG ulTemp;
  1345. VIDEO_NUM_MODES modes;
  1346. PVIDEO_MODE_INFORMATION pVideoTemp;
  1347. //
  1348. // Get the number of modes supported by the mini-port
  1349. //
  1350. if (!AtiDeviceIoControl(hDriver,
  1351. IOCTL_VIDEO_QUERY_NUM_AVAIL_MODES,
  1352. NULL,
  1353. 0,
  1354. &modes,
  1355. sizeof(VIDEO_NUM_MODES),
  1356. &ulTemp))
  1357. {
  1358. DISPDBG((0, "getAvailableModes - Failed VIDEO_QUERY_NUM_AVAIL_MODES"));
  1359. return(0);
  1360. }
  1361. *cbModeSize = modes.ModeInformationLength;
  1362. //
  1363. // Allocate the buffer for the mini-port to write the modes in.
  1364. //
  1365. *modeInformation = AtiAllocMem(LPTR, FL_ZERO_MEMORY,
  1366. modes.NumModes * modes.ModeInformationLength,
  1367. );
  1368. if (*modeInformation == (PVIDEO_MODE_INFORMATION) NULL)
  1369. {
  1370. DISPDBG((0, "getAvailableModes - Failed AtiAllocMem"));
  1371. return 0;
  1372. }
  1373. //
  1374. // Ask the mini-port to fill in the available modes.
  1375. //
  1376. if (!AtiDeviceIoControl(hDriver,
  1377. IOCTL_VIDEO_QUERY_AVAIL_MODES,
  1378. NULL,
  1379. 0,
  1380. *modeInformation,
  1381. modes.NumModes * modes.ModeInformationLength,
  1382. &ulTemp))
  1383. {
  1384. DISPDBG((0, "getAvailableModes - Failed VIDEO_QUERY_AVAIL_MODES"));
  1385. AtiFreeMem(*modeInformation);
  1386. *modeInformation = (PVIDEO_MODE_INFORMATION) NULL;
  1387. return(0);
  1388. }
  1389. //
  1390. // Now see which of these modes are supported by the display driver.
  1391. // As an internal mechanism, set the length to 0 for the modes we
  1392. // DO NOT support.
  1393. //
  1394. ulTemp = modes.NumModes;
  1395. pVideoTemp = *modeInformation;
  1396. //
  1397. // Mode is rejected if it is not one plane, or not graphics, or is not
  1398. // one of 8, 15, 16, 24 or 32 bits per pel.
  1399. //
  1400. while (ulTemp--)
  1401. {
  1402. //DISPDBG((0, "pVideoTemp: %4li bpp, %4li x %4li, %4li Hz",
  1403. //pVideoTemp->BitsPerPlane * pVideoTemp->NumberOfPlanes,
  1404. //pVideoTemp->VisScreenWidth, pVideoTemp->VisScreenHeight,
  1405. //pVideoTemp->Frequency ));
  1406. if ((pVideoTemp->NumberOfPlanes != 1 ) ||
  1407. !(pVideoTemp->AttributeFlags & VIDEO_MODE_GRAPHICS) ||
  1408. ((pVideoTemp->BitsPerPlane != 8) &&
  1409. (pVideoTemp->BitsPerPlane != 15) &&
  1410. (pVideoTemp->BitsPerPlane != 16) &&
  1411. (pVideoTemp->BitsPerPlane != 24) &&
  1412. (pVideoTemp->BitsPerPlane != 32)))
  1413. {
  1414. DISPDBG((2, "Rejecting miniport mode:"));
  1415. DISPDBG((2, " Screen width -- %li", pVideoTemp->VisScreenWidth));
  1416. DISPDBG((2, " Screen height -- %li", pVideoTemp->VisScreenHeight));
  1417. DISPDBG((2, " Bits per pel -- %li", pVideoTemp->BitsPerPlane *
  1418. pVideoTemp->NumberOfPlanes));
  1419. DISPDBG((2, " Frequency -- %li", pVideoTemp->Frequency));
  1420. pVideoTemp->Length = 0;
  1421. }
  1422. pVideoTemp = (PVIDEO_MODE_INFORMATION)
  1423. (((PUCHAR)pVideoTemp) + modes.ModeInformationLength);
  1424. }
  1425. return(modes.NumModes);
  1426. }