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.

1805 lines
61 KiB

  1. /*************************************************************************\
  2. * Module Name: Lines.c
  3. *
  4. * Contains the code for drawing short fractional endpoint lines and
  5. * longer lines with strips. There is also a separate x86 Asm version
  6. * of this code.
  7. *
  8. * Copyright (c) 1990-1994 Microsoft Corporation
  9. * Copyright (c) 1992 Digital Equipment Corporation
  10. \**************************************************************************/
  11. #include "precomp.h"
  12. ///////////////////////////////////////////////////////////////////////
  13. // We have to be careful of arithmetic overflow in a number of places.
  14. // Fortunately, the compiler is guaranteed to natively support 64-bit
  15. // signed LONGLONGs and 64-bit unsigned DWORDLONGs.
  16. //
  17. // UUInt32x32To64(a, b) is a macro defined in 'winnt.h' that multiplies
  18. // two 32-bit ULONGs to produce a 64-bit DWORDLONG result.
  19. //
  20. // UInt64By32To32 is our own macro to divide a 64-bit DWORDLONG by
  21. // a 32-bit ULONG to produce a 32-bit ULONG result.
  22. //
  23. // UInt64Mod32To32 is our own macro to modulus a 64-bit DWORDLONG by
  24. // a 32-bit ULONG to produce a 32-bit ULONG result.
  25. //
  26. // 64 bit divides are usually very expensive. Since it's very rare
  27. // that we'll get lines where the upper 32 bits of the 64 bit result
  28. // are used, we can almost always use 32-bit ULONG divides. We still
  29. // must correctly handle the larger cases:
  30. #define UInt64Div32To32(a, b) \
  31. ((((DWORDLONG)(a)) > ULONG_MAX) ? \
  32. (ULONG)((DWORDLONG)(a) / (ULONG)(b)) : \
  33. (ULONG)((ULONG)(a) / (ULONG)(b)))
  34. #define UInt64Mod32To32(a, b) \
  35. ((((DWORDLONG)(a)) > ULONG_MAX) ? \
  36. (ULONG)((DWORDLONG)(a) % (ULONG)(b)) : \
  37. (ULONG)((ULONG)(a) % (ULONG)(b)))
  38. #define SWAPL(x,y,t) {t = x; x = y; y = t;}
  39. FLONG gaflRound[] = {
  40. FL_H_ROUND_DOWN | FL_V_ROUND_DOWN, // no flips
  41. FL_H_ROUND_DOWN | FL_V_ROUND_DOWN, // FL_FLIP_D
  42. FL_H_ROUND_DOWN, // FL_FLIP_V
  43. FL_V_ROUND_DOWN, // FL_FLIP_V | FL_FLIP_D
  44. FL_V_ROUND_DOWN, // FL_FLIP_SLOPE_ONE
  45. 0xbaadf00d, // FL_FLIP_SLOPE_ONE | FL_FLIP_D
  46. FL_H_ROUND_DOWN, // FL_FLIP_SLOPE_ONE | FL_FLIP_V
  47. 0xbaadf00d // FL_FLIP_SLOPE_ONE | FL_FLIP_V | FL_FLIP_D
  48. };
  49. BOOL bIntegerLine(PDEV*, ULONG, ULONG, ULONG, ULONG);
  50. BOOL bHardwareLine(PDEV*, POINTFIX*, POINTFIX*);
  51. /******************************Public*Routine******************************\
  52. * BOOL bLines(ppdev, pptfxFirst, pptfxBuf, cptfx, pls,
  53. * prclClip, apfn[], flStart)
  54. *
  55. * Computes the DDA for the line and gets ready to draw it. Puts the
  56. * pixel data into an array of strips, and calls a strip routine to
  57. * do the actual drawing.
  58. *
  59. * Doing NT Lines Right
  60. * --------------------
  61. *
  62. * In NT, all lines are given to the device driver in fractional
  63. * coordinates, in a 28.4 fixed point format. The lower 4 bits are
  64. * fractional for sub-pixel positioning.
  65. *
  66. * Note that you CANNOT! just round the coordinates to integers
  67. * and pass the results to your favorite integer Bresenham routine!!
  68. * (Unless, of course, you have such a high resolution device that
  69. * nobody will notice -- not likely for a display device.) The
  70. * fractions give a more accurate rendering of the line -- this is
  71. * important for things like our Bezier curves, which would have 'kinks'
  72. * if the points in its polyline approximation were rounded to integers.
  73. *
  74. * Unfortunately, for fractional lines there is more setup work to do
  75. * a DDA than for integer lines. However, the main loop is exactly
  76. * the same (and can be done entirely with 32 bit math).
  77. *
  78. * If You've Got Hardware That Does Bresenham
  79. * ------------------------------------------
  80. *
  81. * A lot of hardware limits DDA error terms to 'n' bits. With fractional
  82. * coordinates, 4 bits are given to the fractional part, letting
  83. * you draw in hardware only those lines that lie entirely in a 2^(n-4)
  84. * by 2^(n-4) pixel space.
  85. *
  86. * And you still have to correctly draw those lines with coordinates
  87. * outside that space! Remember that the screen is only a viewport
  88. * onto a 28.4 by 28.4 space -- if any part of the line is visible
  89. * you MUST render it precisely, regardless of where the end points lie.
  90. * So even if you do it in software, somewhere you'll have to have a
  91. * 32 bit DDA routine.
  92. *
  93. * Our Implementation
  94. * ------------------
  95. *
  96. * We employ a run length slice algorithm: our DDA calculates the
  97. * number of pixels that are in each row (or 'strip') of pixels.
  98. *
  99. * We've separated the running of the DDA and the drawing of pixels:
  100. * we run the DDA for several iterations and store the results in
  101. * a 'strip' buffer (which are the lengths of consecutive pixel rows of
  102. * the line), then we crank up a 'strip drawer' that will draw all the
  103. * strips in the buffer.
  104. *
  105. * We also employ a 'half-flip' to reduce the number of strip
  106. * iterations we need to do in the DDA and strip drawing loops: when a
  107. * (normalized) line's slope is more than 1/2, we do a final flip
  108. * about the line y = (1/2)x. So now, instead of each strip being
  109. * consecutive horizontal or vertical pixel rows, each strip is composed
  110. * of those pixels aligned in 45 degree rows. So a line like (0, 0) to
  111. * (128, 128) would generate only one strip.
  112. *
  113. * We also always draw only left-to-right.
  114. *
  115. * Styled lines may have arbitrary style patterns. We specially
  116. * optimize the default patterns (and call them 'masked' styles).
  117. *
  118. * The DDA Derivation
  119. * ------------------
  120. *
  121. * Here is how I like to think of the DDA calculation.
  122. *
  123. * We employ Knuth's "diamond rule": rendering a one-pixel-wide line
  124. * can be thought of as dragging a one-pixel-wide by one-pixel-high
  125. * diamond along the true line. Pixel centers lie on the integer
  126. * coordinates, and so we light any pixel whose center gets covered
  127. * by the "drag" region (John D. Hobby, Journal of the Association
  128. * for Computing Machinery, Vol. 36, No. 2, April 1989, pp. 209-229).
  129. *
  130. * We must define which pixel gets lit when the true line falls
  131. * exactly half-way between two pixels. In this case, we follow
  132. * the rule: when two pels are equidistant, the upper or left pel
  133. * is illuminated, unless the slope is exactly one, in which case
  134. * the upper or right pel is illuminated. (So we make the edges
  135. * of the diamond exclusive, except for the top and left vertices,
  136. * which are inclusive, unless we have slope one.)
  137. *
  138. * This metric decides what pixels should be on any line BEFORE it is
  139. * flipped around for our calculation. Having a consistent metric
  140. * this way will let our lines blend nicely with our curves. The
  141. * metric also dictates that we will never have one pixel turned on
  142. * directly above another that's turned on. We will also never have
  143. * a gap; i.e., there will be exactly one pixel turned on for each
  144. * column between the start and end points. All that remains to be
  145. * done is to decide how many pixels should be turned on for each row.
  146. *
  147. * So lines we draw will consist of varying numbers of pixels on
  148. * successive rows, for example:
  149. *
  150. * ******
  151. * *****
  152. * ******
  153. * *****
  154. *
  155. * We'll call each set of pixels on a row a "strip".
  156. *
  157. * (Please remember that our coordinate space has the origin as the
  158. * upper left pixel on the screen; postive y is down and positive x
  159. * is right.)
  160. *
  161. * Device coordinates are specified as fixed point 28.4 numbers,
  162. * where the first 28 bits are the integer coordinate, and the last
  163. * 4 bits are the fraction. So coordinates may be thought of as
  164. * having the form (x, y) = (M/F, N/F) where F is the constant scaling
  165. * factor F = 2^4 = 16, and M and N are 32 bit integers.
  166. *
  167. * Consider the line from (M0/F, N0/F) to (M1/F, N1/F) which runs
  168. * left-to-right and whose slope is in the first octant, and let
  169. * dM = M1 - M0 and dN = N1 - N0. Then dM >= 0, dN >= 0 and dM >= dN.
  170. *
  171. * Since the slope of the line is less than 1, the edges of the
  172. * drag region are created by the top and bottom vertices of the
  173. * diamond. At any given pixel row y of the line, we light those
  174. * pixels whose centers are between the left and right edges.
  175. *
  176. * Let mL(n) denote the line representing the left edge of the drag
  177. * region. On pixel row j, the column of the first pixel to be
  178. * lit is
  179. *
  180. * iL(j) = ceiling( mL(j * F) / F)
  181. *
  182. * Since the line's slope is less than one:
  183. *
  184. * iL(j) = ceiling( mL([j + 1/2] F) / F )
  185. *
  186. * Recall the formula for our line:
  187. *
  188. * n(m) = (dN / dM) (m - M0) + N0
  189. *
  190. * m(n) = (dM / dN) (n - N0) + M0
  191. *
  192. * Since the line's slope is less than one, the line representing
  193. * the left edge of the drag region is the original line offset
  194. * by 1/2 pixel in the y direction:
  195. *
  196. * mL(n) = (dM / dN) (n - F/2 - N0) + M0
  197. *
  198. * From this we can figure out the column of the first pixel that
  199. * will be lit on row j, being careful of rounding (if the left
  200. * edge lands exactly on an integer point, the pixel at that
  201. * point is not lit because of our rounding convention):
  202. *
  203. * iL(j) = floor( mL(j F) / F ) + 1
  204. *
  205. * = floor( ((dM / dN) (j F - F/2 - N0) + M0) / F ) + 1
  206. *
  207. * = floor( F dM j - F/2 dM - N0 dM + dN M0) / F dN ) + 1
  208. *
  209. * F dM j - [ dM (N0 + F/2) - dN M0 ]
  210. * = floor( ---------------------------------- ) + 1
  211. * F dN
  212. *
  213. * dM j - [ dM (N0 + F/2) - dN M0 ] / F
  214. * = floor( ------------------------------------ ) + 1 (1)
  215. * dN
  216. *
  217. * = floor( (dM j + alpha) / dN ) + 1
  218. *
  219. * where
  220. *
  221. * alpha = - [ dM (N0 + F/2) - dN M0 ] / F
  222. *
  223. * We use equation (1) to calculate the DDA: there are iL(j+1) - iL(j)
  224. * pixels in row j. Because we are always calculating iL(j) for
  225. * integer quantities of j, we note that the only fractional term
  226. * is constant, and so we can 'throw away' the fractional bits of
  227. * alpha:
  228. *
  229. * beta = floor( - [ dM (N0 + F/2) - dN M0 ] / F ) (2)
  230. *
  231. * so
  232. *
  233. * iL(j) = floor( (dM j + beta) / dN ) + 1 (3)
  234. *
  235. * for integers j.
  236. *
  237. * Note if iR(j) is the line's rightmost pixel on row j, that
  238. * iR(j) = iL(j + 1) - 1.
  239. *
  240. * Similarly, rewriting equation (1) as a function of column i,
  241. * we can determine, given column i, on which pixel row j is the line
  242. * lit:
  243. *
  244. * dN i + [ dM (N0 + F/2) - dN M0 ] / F
  245. * j(i) = ceiling( ------------------------------------ ) - 1
  246. * dM
  247. *
  248. * Floors are easier to compute, so we can rewrite this:
  249. *
  250. * dN i + [ dM (N0 + F/2) - dN M0 ] / F + dM - 1/F
  251. * j(i) = floor( ----------------------------------------------- ) - 1
  252. * dM
  253. *
  254. * dN i + [ dM (N0 + F/2) - dN M0 ] / F + dM - 1/F - dM
  255. * = floor( ---------------------------------------------------- )
  256. * dM
  257. *
  258. * dN i + [ dM (N0 + F/2) - dN M0 - 1 ] / F
  259. * = floor( ---------------------------------------- )
  260. * dM
  261. *
  262. * We can once again wave our hands and throw away the fractional bits
  263. * of the remainder term:
  264. *
  265. * j(i) = floor( (dN i + gamma) / dM ) (4)
  266. *
  267. * where
  268. *
  269. * gamma = floor( [ dM (N0 + F/2) - dN M0 - 1 ] / F ) (5)
  270. *
  271. * We now note that
  272. *
  273. * beta = -gamma - 1 = ~gamma (6)
  274. *
  275. * To draw the pixels of the line, we could evaluate (3) on every scan
  276. * line to determine where the strip starts. Of course, we don't want
  277. * to do that because that would involve a multiply and divide for every
  278. * scan. So we do everything incrementally.
  279. *
  280. * We would like to easily compute c , the number of pixels on scan j:
  281. * j
  282. *
  283. * c = iL(j + 1) - iL(j)
  284. * j
  285. *
  286. * = floor((dM (j + 1) + beta) / dN) - floor((dM j + beta) / dN) (7)
  287. *
  288. * This may be rewritten as
  289. *
  290. * c = floor(i + r / dN) - floor(i + r / dN) (8)
  291. * j j+1 j+1 j j
  292. *
  293. * where i , i are integers and r < dN, r < dN.
  294. * j j+1 j j+1
  295. *
  296. * Rewriting (7) again:
  297. *
  298. * c = floor(i + r / dN + dM / dN) - floor(i + r / dN)
  299. * j j j j j
  300. *
  301. *
  302. * = floor((r + dM) / dN) - floor(r / dN)
  303. * j j
  304. *
  305. * This may be rewritten as
  306. *
  307. * c = dI + floor((r + dR) / dN) - floor(r / dN)
  308. * j j j
  309. *
  310. * where dI + dR / dN = dM / dN, dI is an integer and dR < dN.
  311. *
  312. * r is the remainder (or "error") term in the DDA loop: r / dN
  313. * j j
  314. * is the exact fraction of a pixel at which the strip ends. To go
  315. * on to the next scan and compute c we need to know r .
  316. * j+1 j+1
  317. *
  318. * So in the main loop of the DDA:
  319. *
  320. * c = dI + floor((r + dR) / dN) and r = (r + dR) % dN
  321. * j j j+1 j
  322. *
  323. * and we know r < dN, r < dN, and dR < dN.
  324. * j j+1
  325. *
  326. * We have derived the DDA only for lines in the first octant; to
  327. * handle other octants we do the common trick of flipping the line
  328. * to the first octant by first making the line left-to-right by
  329. * exchanging the end-points, then flipping about the lines y = 0 and
  330. * y = x, as necessary. We must record the transformation so we can
  331. * undo them later.
  332. *
  333. * We must also be careful of how the flips affect our rounding. If
  334. * to get the line to the first octant we flipped about x = 0, we now
  335. * have to be careful to round a y value of 1/2 up instead of down as
  336. * we would for a line originally in the first octant (recall that
  337. * "In the case where two pels are equidistant, the upper or left
  338. * pel is illuminated...").
  339. *
  340. * To account for this rounding when running the DDA, we shift the line
  341. * (or not) in the y direction by the smallest amount possible. That
  342. * takes care of rounding for the DDA, but we still have to be careful
  343. * about the rounding when determining the first and last pixels to be
  344. * lit in the line.
  345. *
  346. * Determining The First And Last Pixels In The Line
  347. * -------------------------------------------------
  348. *
  349. * Fractional coordinates also make it harder to determine which pixels
  350. * will be the first and last ones in the line. We've already taken
  351. * the fractional coordinates into account in calculating the DDA, but
  352. * the DDA cannot tell us which are the end pixels because it is quite
  353. * happy to calculate pixels on the line from minus infinity to positive
  354. * infinity.
  355. *
  356. * The diamond rule determines the start and end pixels. (Recall that
  357. * the sides are exclusive except for the left and top vertices.)
  358. * This convention can be thought of in another way: there are diamonds
  359. * around the pixels, and wherever the true line crosses a diamond,
  360. * that pel is illuminated.
  361. *
  362. * Consider a line where we've done the flips to the first octant, and the
  363. * floor of the start coordinates is the origin:
  364. *
  365. * +-----------------------> +x
  366. * |
  367. * | 0 1
  368. * | 0123456789abcdef
  369. * |
  370. * | 0 00000000?1111111
  371. * | 1 00000000 1111111
  372. * | 2 0000000 111111
  373. * | 3 000000 11111
  374. * | 4 00000 ** 1111
  375. * | 5 0000 ****1
  376. * | 6 000 1***
  377. * | 7 00 1 ****
  378. * | 8 ? ***
  379. * | 9 22 3 ****
  380. * | a 222 33 ***
  381. * | b 2222 333 ****
  382. * | c 22222 3333 **
  383. * | d 222222 33333
  384. * | e 2222222 333333
  385. * | f 22222222 3333333
  386. * |
  387. * | 2 3
  388. * v
  389. * +y
  390. *
  391. * If the start of the line lands on the diamond around pixel 0 (shown by
  392. * the '0' region here), pixel 0 is the first pel in the line. The same
  393. * is true for the other pels.
  394. *
  395. * A little more work has to be done if the line starts in the
  396. * 'nether-land' between the diamonds (as illustrated by the '*' line):
  397. * the first pel lit is the first diamond crossed by the line (pixel 1 in
  398. * our example). This calculation is determined by the DDA or slope of
  399. * the line.
  400. *
  401. * If the line starts exactly half way between two adjacent pixels
  402. * (denoted here by the '?' spots), the first pixel is determined by our
  403. * round-down convention (and is dependent on the flips done to
  404. * normalize the line).
  405. *
  406. * Last Pel Exclusive
  407. * ------------------
  408. *
  409. * To eliminate repeatedly lit pels between continuous connected lines,
  410. * we employ a last-pel exclusive convention: if the line ends exactly on
  411. * the diamond around a pel, that pel is not lit. (This eliminates the
  412. * checks we had in the old code to see if we were re-lighting pels.)
  413. *
  414. * The Half Flip
  415. * -------------
  416. *
  417. * To make our run length algorithm more efficient, we employ a "half
  418. * flip". If after normalizing to the first octant, the slope is more
  419. * than 1/2, we subtract the y coordinate from the x coordinate. This
  420. * has the effect of reflecting the coordinates through the line of slope
  421. * 1/2. Note that the diagonal gets mapped into the x-axis after a half
  422. * flip.
  423. *
  424. * How Many Bits Do We Need, Anyway?
  425. * ---------------------------------
  426. *
  427. * Note that if the line is visible on your screen, you must light up
  428. * exactly the correct pixels, no matter where in the 28.4 x 28.4 device
  429. * space the end points of the line lie (meaning you must handle 32 bit
  430. * DDAs, you can certainly have optimized cases for lesser DDAs).
  431. *
  432. * We move the origin to (floor(M0 / F), floor(N0 / F)), so when we
  433. * calculate gamma from (5), we know that 0 <= M0, N0 < F. And we
  434. * are in the first octant, so dM >= dN. Then we know that gamma can
  435. * be in the range [(-1/2)dM, (3/2)dM]. The DDI guarantees us that
  436. * valid lines will have dM and dN values at most 31 bits (unsigned)
  437. * of significance. So gamma requires 33 bits of significance (we store
  438. * this as a 64 bit number for convenience).
  439. *
  440. * When running through the DDA loop, r + dR can have a value in the
  441. * j
  442. * range 0 <= r < 2 dN; thus the result must be a 32 bit unsigned value.
  443. * j
  444. *
  445. * Testing Lines
  446. * -------------
  447. *
  448. * To be NT compliant, a display driver must exactly adhere to GIQ,
  449. * which means that for any given line, the driver must light exactly
  450. * the same pels as does GDI. This can be tested using the Guiman tool
  451. * provided elsewhere in the DDK, and 'ZTest', which draws random lines
  452. * on the screen and to a bitmap, and compares the results.
  453. *
  454. * If You've Got Line Hardware
  455. * ---------------------------
  456. *
  457. * If your hardware already adheres to GIQ, you're all set. Otherwise
  458. * you'll want to look at the S3 sample code and read the following:
  459. *
  460. * 1) You'll want to special case integer-only lines, since they require
  461. * less processing time and are more common (CAD programs will probably
  462. * only ever give integer lines). GDI does not provide a flag saying
  463. * that all lines in a path are integer lines; consequently, you will
  464. * have to explicitly check every line.
  465. *
  466. * 2) You are required to correctly draw any line in the 28.4 device
  467. * space that intersects the viewport. If you have less than 32 bits
  468. * of significance in the hardware for the Bresenham terms, extremely
  469. * long lines would overflow the hardware. For such (rare) cases, you
  470. * can fall back to strip-drawing code (or if your display is a frame
  471. * buffer, fall back to the engine).
  472. *
  473. * 3) If you can explicitly set the Bresenham terms in your hardware, you
  474. * can draw non-integer lines using the hardware. If your hardware has
  475. * 'n' bits of precision, you can draw GIQ lines that are up to 2^(n-5)
  476. * pels long (4 bits are required for the fractional part, and one bit is
  477. * used as a sign bit). Note that integer lines don't require the 4
  478. * fractional bits, so if you special case them as in 1), you can do
  479. * integer lines that are up to 2^(n - 1) pels long. See the
  480. * 'bHardwareLine' routine for an example.
  481. *
  482. \**************************************************************************/
  483. BOOL bLines(
  484. PDEV* ppdev,
  485. POINTFIX* pptfxFirst, // Start of first line
  486. POINTFIX* pptfxBuf, // Pointer to buffer of all remaining lines
  487. RUN* prun, // Pointer to runs if doing complex clipping
  488. ULONG cptfx, // Number of points in pptfxBuf or number of runs
  489. // in prun
  490. LINESTATE* pls, // Colour and style info
  491. RECTL* prclClip, // Pointer to clip rectangle if doing simple clipping
  492. PFNSTRIP apfn[], // Array of strip functions
  493. FLONG flStart) // Flags for each line, which is a combination of:
  494. // FL_SIMPLE_CLIP
  495. // FL_COMPLEX_CLIP
  496. // FL_STYLED
  497. // FL_LAST_PEL_INCLUSIVE
  498. // - Should be set only for all integer lines,
  499. // and can't be used with FL_COMPLEX_CLIP
  500. {
  501. ULONG M0;
  502. ULONG dM;
  503. ULONG N0;
  504. ULONG dN;
  505. ULONG dN_Original;
  506. FLONG fl;
  507. LONG x;
  508. LONG y;
  509. LONGLONG llBeta;
  510. LONGLONG llGamma;
  511. LONGLONG dl;
  512. LONGLONG ll;
  513. ULONG ulDelta;
  514. ULONG x0;
  515. ULONG y0;
  516. ULONG x1;
  517. ULONG cStylePels; // Major length of line in pixels for styling
  518. ULONG xStart;
  519. POINTL ptlStart;
  520. STRIP strip;
  521. PFNSTRIP pfn;
  522. LONG cPels;
  523. LONG* plStrip;
  524. LONG* plStripEnd;
  525. LONG cStripsInNextRun;
  526. POINTFIX* pptfxBufEnd = pptfxBuf + cptfx; // Last point in path record
  527. STYLEPOS spThis; // Style pos for this line
  528. BOOL bW32p = (ppdev->ulChipID == W32P);
  529. do {
  530. /***********************************************************************\
  531. * Start the DDA calculations. *
  532. \***********************************************************************/
  533. M0 = (LONG) pptfxFirst->x;
  534. dM = (LONG) pptfxBuf->x;
  535. N0 = (LONG) pptfxFirst->y;
  536. dN = (LONG) pptfxBuf->y;
  537. fl = flStart;
  538. // Check for non-clipped, non-styled integer endpoint lines
  539. if (bW32p)
  540. {
  541. if ((fl & (FL_CLIP | FL_STYLED)) == 0)
  542. {
  543. // Special-case integer end-point lines:
  544. if (((M0 | dM | N0 | dN) & (F - 1)) == 0)
  545. {
  546. if (bIntegerLine(ppdev, M0, N0, dM, dN))
  547. {
  548. goto Next_Line;
  549. }
  550. }
  551. }
  552. }
  553. if ((LONG) M0 > (LONG) dM)
  554. {
  555. // Ensure that we run left-to-right:
  556. register ULONG ulTmp;
  557. SWAPL(M0, dM, ulTmp);
  558. SWAPL(N0, dN, ulTmp);
  559. fl |= FL_FLIP_H;
  560. }
  561. // Compute the delta dx. The DDI says we can never have a valid delta
  562. // with a magnitued more than 2^31 - 1, but GDI never actually checks
  563. // its transforms. So we have to check for this case to avoid overflow:
  564. dM -= M0;
  565. if ((LONG) dM < 0)
  566. {
  567. goto Next_Line;
  568. }
  569. if ((LONG) dN < (LONG) N0)
  570. {
  571. // Line runs from bottom to top, so flip across y = 0:
  572. N0 = -(LONG) N0;
  573. dN = -(LONG) dN;
  574. fl |= FL_FLIP_V;
  575. }
  576. dN -= N0;
  577. if ((LONG) dN < 0)
  578. {
  579. goto Next_Line;
  580. }
  581. // We now have a line running left-to-right, top-to-bottom from (M0, N0)
  582. // to (M0 + dM, N0 + dN):
  583. if (dN >= dM)
  584. {
  585. if (dN == dM)
  586. {
  587. // Have to special case slopes of one:
  588. fl |= FL_FLIP_SLOPE_ONE;
  589. }
  590. else
  591. {
  592. // Since line has slope greater than 1, flip across x = y:
  593. register ULONG ulTmp;
  594. SWAPL(dM, dN, ulTmp);
  595. SWAPL(M0, N0, ulTmp);
  596. fl |= FL_FLIP_D;
  597. }
  598. }
  599. fl |= gaflRound[(fl & FL_ROUND_MASK) >> FL_ROUND_SHIFT];
  600. x = LFLOOR((LONG) M0);
  601. y = LFLOOR((LONG) N0);
  602. M0 = FXFRAC(M0);
  603. N0 = FXFRAC(N0);
  604. // Calculate the remainder term [ dM * (N0 + F/2) - M0 * dN ]:
  605. llGamma = UInt32x32To64(dM, N0 + F/2) - UInt32x32To64(M0, dN);
  606. if (fl & FL_V_ROUND_DOWN) // Adjust so y = 1/2 rounds down
  607. {
  608. llGamma--;
  609. }
  610. llGamma >>= FLOG2;
  611. llBeta = ~llGamma;
  612. /***********************************************************************\
  613. * Figure out which pixels are at the ends of the line. *
  614. \***********************************************************************/
  615. // The toughest part of GIQ is determining the start and end pels.
  616. //
  617. // Our approach here is to calculate x0 and x1 (the inclusive start
  618. // and end columns of the line respectively, relative to our normalized
  619. // origin). Then x1 - x0 + 1 is the number of pels in the line. The
  620. // start point is easily calculated by plugging x0 into our line equation
  621. // (which takes care of whether y = 1/2 rounds up or down in value)
  622. // getting y0, and then undoing the normalizing flips to get back
  623. // into device space.
  624. //
  625. // We look at the fractional parts of the coordinates of the start and
  626. // end points, and call them (M0, N0) and (M1, N1) respectively, where
  627. // 0 <= M0, N0, M1, N1 < 16. We plot (M0, N0) on the following grid
  628. // to determine x0:
  629. //
  630. // +-----------------------> +x
  631. // |
  632. // | 0 1
  633. // | 0123456789abcdef
  634. // |
  635. // | 0 ........?xxxxxxx
  636. // | 1 ..........xxxxxx
  637. // | 2 ...........xxxxx
  638. // | 3 ............xxxx
  639. // | 4 .............xxx
  640. // | 5 ..............xx
  641. // | 6 ...............x
  642. // | 7 ................
  643. // | 8 ................
  644. // | 9 ......**........
  645. // | a ........****...x
  646. // | b ............****
  647. // | c .............xxx****
  648. // | d ............xxxx ****
  649. // | e ...........xxxxx ****
  650. // | f ..........xxxxxx
  651. // |
  652. // | 2 3
  653. // v
  654. //
  655. // +y
  656. //
  657. // This grid accounts for the appropriate rounding of GIQ and last-pel
  658. // exclusion. If (M0, N0) lands on an 'x', x0 = 2. If (M0, N0) lands
  659. // on a '.', x0 = 1. If (M0, N0) lands on a '?', x0 rounds up or down,
  660. // depending on what flips have been done to normalize the line.
  661. //
  662. // For the end point, if (M1, N1) lands on an 'x', x1 =
  663. // floor((M0 + dM) / 16) + 1. If (M1, N1) lands on a '.', x1 =
  664. // floor((M0 + dM)). If (M1, N1) lands on a '?', x1 rounds up or down,
  665. // depending on what flips have been done to normalize the line.
  666. //
  667. // Lines of exactly slope one require a special case for both the start
  668. // and end. For example, if the line ends such that (M1, N1) is (9, 1),
  669. // the line has gone exactly through (8, 0) -- which may be considered
  670. // to be part of 'x' because of rounding! So slopes of exactly slope
  671. // one going through (8, 0) must also be considered as belonging in 'x'.
  672. //
  673. // For lines that go left-to-right, we have the following grid:
  674. //
  675. // +-----------------------> +x
  676. // |
  677. // | 0 1
  678. // | 0123456789abcdef
  679. // |
  680. // | 0 xxxxxxxx?.......
  681. // | 1 xxxxxxx.........
  682. // | 2 xxxxxx..........
  683. // | 3 xxxxx...........
  684. // | 4 xxxx............
  685. // | 5 xxx.............
  686. // | 6 xx..............
  687. // | 7 x...............
  688. // | 8 x...............
  689. // | 9 x.....**........
  690. // | a xx......****....
  691. // | b xxx.........****
  692. // | c xxxx............****
  693. // | d xxxxx........... ****
  694. // | e xxxxxx.......... ****
  695. // | f xxxxxxx.........
  696. // |
  697. // | 2 3
  698. // v
  699. //
  700. // +y
  701. //
  702. // This grid accounts for the appropriate rounding of GIQ and last-pel
  703. // exclusion. If (M0, N0) lands on an 'x', x0 = 0. If (M0, N0) lands
  704. // on a '.', x0 = 1. If (M0, N0) lands on a '?', x0 rounds up or down,
  705. // depending on what flips have been done to normalize the line.
  706. //
  707. // For the end point, if (M1, N1) lands on an 'x', x1 =
  708. // floor((M0 + dM) / 16) - 1. If (M1, N1) lands on a '.', x1 =
  709. // floor((M0 + dM)). If (M1, N1) lands on a '?', x1 rounds up or down,
  710. // depending on what flips have been done to normalize the line.
  711. //
  712. // Lines of exactly slope one must be handled similarly to the right-to-
  713. // left case.
  714. {
  715. // Calculate x0, x1
  716. ULONG N1 = FXFRAC(N0 + dN);
  717. ULONG M1 = FXFRAC(M0 + dM);
  718. x1 = LFLOOR(M0 + dM);
  719. if (fl & FL_LAST_PEL_INCLUSIVE)
  720. {
  721. // It sure is easy to compute the first pel when lines have only
  722. // integer coordinates and are last-pel inclusive:
  723. x0 = 0;
  724. y0 = 0;
  725. // Last-pel inclusive lines that are exactly one pixel long
  726. // have a 'delta-x' and 'delta-y' equal to zero. The problem is
  727. // that our clip code assumes that 'delta-x' is always non-zero
  728. // (since it never happens with last-pel exclusive lines). As
  729. // an inelegant solution, we simply modify 'delta-x' in this
  730. // case -- because the line is exactly one pixel long, changing
  731. // the slope will obviously have no effect on rasterization.
  732. if (x1 == 0)
  733. {
  734. dM = 1;
  735. llGamma = 0;
  736. llBeta = ~llGamma;
  737. }
  738. }
  739. else
  740. {
  741. if (fl & FL_FLIP_H)
  742. {
  743. // ---------------------------------------------------------------
  744. // Line runs right-to-left: <----
  745. // Compute x1:
  746. if (N1 == 0)
  747. {
  748. if (LROUND(M1, fl & FL_H_ROUND_DOWN))
  749. {
  750. x1++;
  751. }
  752. }
  753. else if (abs((LONG) (N1 - F/2)) + M1 > F)
  754. {
  755. x1++;
  756. }
  757. if ((fl & (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  758. == (FL_FLIP_SLOPE_ONE))
  759. {
  760. // Have to special-case diagonal lines going through our
  761. // the point exactly equidistant between two horizontal
  762. // pixels, if we're supposed to round x=1/2 down:
  763. if ((N1 > 0) && (M1 == N1 + 8))
  764. x1++;
  765. // Don't you love special cases? Is this a rhetorical question?
  766. if ((N0 > 0) && (M0 == N0 + 8))
  767. {
  768. x0 = 2;
  769. ulDelta = dN;
  770. goto right_to_left_compute_y0;
  771. }
  772. }
  773. // Compute x0:
  774. x0 = 1;
  775. ulDelta = 0;
  776. if (N0 == 0)
  777. {
  778. if (LROUND(M0, fl & FL_H_ROUND_DOWN))
  779. {
  780. x0 = 2;
  781. ulDelta = dN;
  782. }
  783. }
  784. else if (abs((LONG) (N0 - F/2)) + M0 > F)
  785. {
  786. x0 = 2;
  787. ulDelta = dN;
  788. }
  789. // Compute y0:
  790. right_to_left_compute_y0:
  791. y0 = 0;
  792. ll = llGamma + (LONGLONG) ulDelta;
  793. if (ll >= (LONGLONG) (2 * dM - dN))
  794. y0 = 2;
  795. else if (ll >= (LONGLONG) (dM - dN))
  796. y0 = 1;
  797. }
  798. else
  799. {
  800. // ---------------------------------------------------------------
  801. // Line runs left-to-right: ---->
  802. // Compute x1:
  803. if (!(fl & FL_LAST_PEL_INCLUSIVE))
  804. x1--;
  805. if (M1 > 0)
  806. {
  807. if (N1 == 0)
  808. {
  809. if (LROUND(M1, fl & FL_H_ROUND_DOWN))
  810. x1++;
  811. }
  812. else if (abs((LONG) (N1 - F/2)) <= (LONG) M1)
  813. {
  814. x1++;
  815. }
  816. }
  817. if ((fl & (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  818. == (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  819. {
  820. // Have to special-case diagonal lines going through our
  821. // the point exactly equidistant between two horizontal
  822. // pixels, if we're supposed to round x=1/2 down:
  823. if ((M1 > 0) && (N1 == M1 + 8))
  824. x1--;
  825. if ((M0 > 0) && (N0 == M0 + 8))
  826. {
  827. x0 = 0;
  828. goto left_to_right_compute_y0;
  829. }
  830. }
  831. // Compute x0:
  832. x0 = 0;
  833. if (M0 > 0)
  834. {
  835. if (N0 == 0)
  836. {
  837. if (LROUND(M0, fl & FL_H_ROUND_DOWN))
  838. x0 = 1;
  839. }
  840. else if (abs((LONG) (N0 - F/2)) <= (LONG) M0)
  841. {
  842. x0 = 1;
  843. }
  844. }
  845. // Compute y0:
  846. left_to_right_compute_y0:
  847. y0 = 0;
  848. if (llGamma >= (LONGLONG) (dM - (dN & (-(LONG) x0))))
  849. {
  850. y0 = 1;
  851. }
  852. }
  853. }
  854. }
  855. cStylePels = x1 - x0 + 1;
  856. if ((LONG) cStylePels <= 0)
  857. goto Next_Line;
  858. xStart = x0;
  859. /***********************************************************************\
  860. * Complex clipping. *
  861. \***********************************************************************/
  862. if (fl & FL_COMPLEX_CLIP)
  863. {
  864. dN_Original = dN;
  865. Continue_Complex_Clipping:
  866. if (fl & FL_FLIP_H)
  867. {
  868. // Line runs right-to-left <-----
  869. x0 = xStart + cStylePels - prun->iStop - 1;
  870. x1 = xStart + cStylePels - prun->iStart - 1;
  871. }
  872. else
  873. {
  874. // Line runs left-to-right ----->
  875. x0 = xStart + prun->iStart;
  876. x1 = xStart + prun->iStop;
  877. }
  878. prun++;
  879. // Reset some variables we'll nuke a little later:
  880. dN = dN_Original;
  881. pls->spNext = pls->spComplex;
  882. // No overflow since large integer math is used. Both values
  883. // will be positive:
  884. dl = UInt32x32To64(x0, dN) + llGamma;
  885. // y0 = dl / dM:
  886. y0 = UInt64Div32To32(dl, dM);
  887. ASSERTDD((LONG) y0 >= 0, "y0 weird: Goofed up end pel calc?");
  888. }
  889. /***********************************************************************\
  890. * Simple rectangular clipping. *
  891. \***********************************************************************/
  892. if (fl & FL_SIMPLE_CLIP)
  893. {
  894. ULONG y1;
  895. LONG xRight;
  896. LONG xLeft;
  897. LONG yBottom;
  898. LONG yTop;
  899. // Note that y0 and y1 are actually the lower and upper bounds,
  900. // respectively, of the y coordinates of the line (the line may
  901. // have actually shrunk due to first/last pel clipping).
  902. //
  903. // Also note that x0, y0 are not necessarily zero.
  904. RECTL* prcl = &prclClip[(fl & FL_RECTLCLIP_MASK) >>
  905. FL_RECTLCLIP_SHIFT];
  906. // Normalize to the same point we've normalized for the DDA
  907. // calculations:
  908. xRight = prcl->right - x;
  909. xLeft = prcl->left - x;
  910. yBottom = prcl->bottom - y;
  911. yTop = prcl->top - y;
  912. if (yBottom <= (LONG) y0 ||
  913. xRight <= (LONG) x0 ||
  914. xLeft > (LONG) x1)
  915. {
  916. Totally_Clipped:
  917. if (fl & FL_STYLED)
  918. {
  919. pls->spNext += cStylePels;
  920. if (pls->spNext >= pls->spTotal2)
  921. pls->spNext %= pls->spTotal2;
  922. }
  923. goto Next_Line;
  924. }
  925. if ((LONG) x1 >= xRight)
  926. x1 = xRight - 1;
  927. // We have to know the correct y1, which we haven't bothered to
  928. // calculate up until now. This multiply and divide is quite
  929. // expensive; we could replace it with code similar to that which
  930. // we used for computing y0.
  931. //
  932. // The reason why we need the actual value, and not an upper
  933. // bounds guess like y1 = LFLOOR(dM) + 2 is that we have to be
  934. // careful when calculating x(y) that y0 <= y <= y1, otherwise
  935. // we can overflow on the divide (which, needless to say, is very
  936. // bad).
  937. dl = UInt32x32To64(x1, dN) + llGamma;
  938. // y1 = dl / dM:
  939. y1 = UInt64Div32To32(dl, dM);
  940. if (yTop > (LONG) y1)
  941. goto Totally_Clipped;
  942. if (yBottom <= (LONG) y1)
  943. {
  944. y1 = yBottom;
  945. dl = UInt32x32To64(y1, dM) + llBeta;
  946. // x1 = dl / dN:
  947. x1 = UInt64Div32To32(dl, dN);
  948. }
  949. // At this point, we've taken care of calculating the intercepts
  950. // with the right and bottom edges. Now we work on the left and
  951. // top edges:
  952. if (xLeft > (LONG) x0)
  953. {
  954. x0 = xLeft;
  955. dl = UInt32x32To64(x0, dN) + llGamma;
  956. // y0 = dl / dM;
  957. y0 = UInt64Div32To32(dl, dM);
  958. if (yBottom <= (LONG) y0)
  959. goto Totally_Clipped;
  960. }
  961. if (yTop > (LONG) y0)
  962. {
  963. y0 = yTop;
  964. dl = UInt32x32To64(y0, dM) + llBeta;
  965. // x0 = dl / dN + 1;
  966. x0 = UInt64Div32To32(dl, dN) + 1;
  967. if (xRight <= (LONG) x0)
  968. goto Totally_Clipped;
  969. }
  970. ASSERTDD(x0 <= x1, "Improper rectangle clip");
  971. }
  972. /***********************************************************************\
  973. * Done clipping. Unflip if necessary. *
  974. \***********************************************************************/
  975. ptlStart.x = x + x0;
  976. ptlStart.y = y + y0;
  977. if (fl & FL_FLIP_D)
  978. {
  979. register LONG lTmp;
  980. SWAPL(ptlStart.x, ptlStart.y, lTmp);
  981. }
  982. if (fl & FL_FLIP_V)
  983. {
  984. ptlStart.y = -ptlStart.y;
  985. }
  986. cPels = x1 - x0 + 1;
  987. /***********************************************************************\
  988. * Style calculations. *
  989. \***********************************************************************/
  990. if (fl & FL_STYLED)
  991. {
  992. STYLEPOS sp;
  993. spThis = pls->spNext;
  994. pls->spNext += cStylePels;
  995. {
  996. if (pls->spNext >= pls->spTotal2)
  997. pls->spNext %= pls->spTotal2;
  998. if (fl & FL_FLIP_H)
  999. sp = pls->spNext - x0 + xStart;
  1000. else
  1001. sp = spThis + x0 - xStart;
  1002. ASSERTDD(fl & FL_STYLED, "Oops");
  1003. // Normalize our target style position:
  1004. if ((sp < 0) || (sp >= pls->spTotal2))
  1005. {
  1006. sp %= pls->spTotal2;
  1007. // The modulus of a negative number is not well-defined
  1008. // in C -- if it's negative we'll adjust it so that it's
  1009. // back in the range [0, spTotal2):
  1010. if (sp < 0)
  1011. sp += pls->spTotal2;
  1012. }
  1013. // Since we always draw the line left-to-right, but styling is
  1014. // always done in the direction of the original line, we have
  1015. // to figure out where we are in the style array for the left
  1016. // edge of this line.
  1017. if (fl & FL_FLIP_H)
  1018. {
  1019. // Line originally ran right-to-left:
  1020. sp = -sp;
  1021. if (sp < 0)
  1022. sp += pls->spTotal2;
  1023. pls->ulStyleMask = ~pls->ulStartMask;
  1024. pls->pspStart = &pls->aspRtoL[0];
  1025. pls->pspEnd = &pls->aspRtoL[pls->cStyle - 1];
  1026. }
  1027. else
  1028. {
  1029. // Line originally ran left-to-right:
  1030. pls->ulStyleMask = pls->ulStartMask;
  1031. pls->pspStart = &pls->aspLtoR[0];
  1032. pls->pspEnd = &pls->aspLtoR[pls->cStyle - 1];
  1033. }
  1034. if (sp >= pls->spTotal)
  1035. {
  1036. sp -= pls->spTotal;
  1037. if (pls->cStyle & 1)
  1038. pls->ulStyleMask = ~pls->ulStyleMask;
  1039. }
  1040. pls->psp = pls->pspStart;
  1041. while (sp >= *pls->psp)
  1042. sp -= *pls->psp++;
  1043. ASSERTDD(pls->psp <= pls->pspEnd,
  1044. "Flew off into NeverNeverLand");
  1045. pls->spRemaining = *pls->psp - sp;
  1046. if ((pls->psp - pls->pspStart) & 1)
  1047. pls->ulStyleMask = ~pls->ulStyleMask;
  1048. }
  1049. }
  1050. plStrip = &strip.alStrips[0];
  1051. plStripEnd = &strip.alStrips[STRIP_MAX]; // Is exclusive
  1052. cStripsInNextRun = 0x7fffffff;
  1053. strip.ptlStart = ptlStart;
  1054. if (2 * dN > dM &&
  1055. !(fl & FL_STYLED))
  1056. {
  1057. // Do a half flip! Remember that we may doing this on the
  1058. // same line multiple times for complex clipping (meaning the
  1059. // affected variables should be reset for every clip run):
  1060. fl |= FL_FLIP_HALF;
  1061. llBeta = llGamma - (LONGLONG) ((LONG) dM);
  1062. dN = dM - dN;
  1063. y0 = x0 - y0; // Note this may overflow, but that's okay
  1064. }
  1065. // Now, run the DDA starting at (ptlStart.x, ptlStart.y)!
  1066. strip.flFlips = fl;
  1067. pfn = apfn[(fl & FL_STRIP_MASK) >> FL_STRIP_SHIFT];
  1068. // Now calculate the DDA variables needed to figure out how many pixels
  1069. // go in the very first strip:
  1070. {
  1071. register LONG i;
  1072. register ULONG dI;
  1073. register ULONG dR;
  1074. ULONG r;
  1075. if (dN == 0)
  1076. i = 0x7fffffff;
  1077. else
  1078. {
  1079. dl = UInt32x32To64(y0 + 1, dM) + llBeta;
  1080. ASSERTDD(dl >= 0, "Oops!");
  1081. // i = (dl / dN) - x0 + 1;
  1082. // r = (dl % dN);
  1083. i = UInt64Div32To32(dl, dN);
  1084. r = UInt64Mod32To32(dl, dN);
  1085. i = i - x0 + 1;
  1086. dI = dM / dN;
  1087. dR = dM % dN; // 0 <= dR < dN
  1088. ASSERTDD(dI > 0, "Weird dI");
  1089. }
  1090. ASSERTDD(i > 0 && i <= 0x7fffffff, "Weird initial strip length");
  1091. ASSERTDD(cPels > 0, "Zero pel line");
  1092. /***********************************************************************\
  1093. * Run the DDA! *
  1094. \***********************************************************************/
  1095. while(TRUE)
  1096. {
  1097. cPels -= i;
  1098. if (cPels <= 0)
  1099. break;
  1100. *plStrip++ = i;
  1101. if (plStrip == plStripEnd)
  1102. {
  1103. strip.cStrips = (LONG)(plStrip - &strip.alStrips[0]);
  1104. (*pfn)(ppdev, &strip, pls);
  1105. plStrip = &strip.alStrips[0];
  1106. }
  1107. i = dI;
  1108. r += dR;
  1109. if (r >= dN)
  1110. {
  1111. r -= dN;
  1112. i++;
  1113. }
  1114. }
  1115. *plStrip++ = cPels + i;
  1116. strip.cStrips = (LONG)(plStrip - &strip.alStrips[0]);
  1117. (*pfn)(ppdev, &strip, pls);
  1118. }
  1119. Next_Line:
  1120. if (fl & FL_COMPLEX_CLIP)
  1121. {
  1122. cptfx--;
  1123. if (cptfx != 0)
  1124. goto Continue_Complex_Clipping;
  1125. break;
  1126. }
  1127. else
  1128. {
  1129. pptfxFirst = pptfxBuf;
  1130. pptfxBuf++;
  1131. }
  1132. } while (pptfxBuf < pptfxBufEnd);
  1133. return(TRUE);
  1134. }
  1135. //////////////////////////////////////////////////////////////////////////
  1136. // General defines for bHardwareLine
  1137. #define HW_FLIP_D 0x0001L // Diagonal flip
  1138. #define HW_FLIP_V 0x0002L // Vertical flip
  1139. #define HW_FLIP_H 0x0004L // Horizontal flip
  1140. #define HW_FLIP_SLOPE_ONE 0x0008L // Normalized line has exactly slope one
  1141. #define HW_FLIP_MASK (HW_FLIP_D | HW_FLIP_V | HW_FLIP_H)
  1142. #define HW_X_ROUND_DOWN 0x0100L // x = 1/2 rounds down in value
  1143. #define HW_Y_ROUND_DOWN 0x0200L // y = 1/2 rounds down in value
  1144. LONG gaiDir[] = { 0, 1, 7, 6, 3, 2, 4, 5 };
  1145. FLONG gaflHardwareRound[] = {
  1146. HW_X_ROUND_DOWN | HW_Y_ROUND_DOWN, // | | |
  1147. HW_X_ROUND_DOWN | HW_Y_ROUND_DOWN, // | | | FLIP_D
  1148. HW_X_ROUND_DOWN, // | | FLIP_V |
  1149. HW_Y_ROUND_DOWN, // | | FLIP_V | FLIP_D
  1150. HW_Y_ROUND_DOWN, // | FLIP_H | |
  1151. HW_X_ROUND_DOWN, // | FLIP_H | | FLIP_D
  1152. 0, // | FLIP_H | FLIP_V |
  1153. 0, // | FLIP_H | FLIP_V | FLIP_D
  1154. HW_Y_ROUND_DOWN, // SLOPE_ONE | | |
  1155. 0xffffffff, // SLOPE_ONE | | | FLIP_D
  1156. HW_X_ROUND_DOWN, // SLOPE_ONE | | FLIP_V |
  1157. 0xffffffff, // SLOPE_ONE | | FLIP_V | FLIP_D
  1158. HW_Y_ROUND_DOWN, // SLOPE_ONE | FLIP_H | |
  1159. 0xffffffff, // SLOPE_ONE | FLIP_H | | FLIP_D
  1160. HW_X_ROUND_DOWN, // SLOPE_ONE | FLIP_H | FLIP_V |
  1161. 0xffffffff // SLOPE_ONE | FLIP_H | FLIP_V | FLIP_D
  1162. };
  1163. #if 0
  1164. //////////////////////////////////////////////////////////////////////////
  1165. // S3 specific defines
  1166. #define DEFAULT_DRAW_CMD (DRAW_LINE | DRAW | DIR_TYPE_XY | MULTIPLE_PIXELS | \
  1167. WRITE | LAST_PIXEL_OFF)
  1168. LONG gaiDrawCmd[] = {
  1169. DEFAULT_DRAW_CMD | PLUS_X | PLUS_Y | 0, // Octant 0
  1170. DEFAULT_DRAW_CMD | PLUS_X | PLUS_Y | MAJOR_Y, // Octant 1
  1171. DEFAULT_DRAW_CMD | PLUS_X | 0 | 0, // Octant 7
  1172. DEFAULT_DRAW_CMD | PLUS_X | 0 | MAJOR_Y, // Octant 6
  1173. DEFAULT_DRAW_CMD | 0 | PLUS_Y | 0, // Octant 3
  1174. DEFAULT_DRAW_CMD | 0 | PLUS_Y | MAJOR_Y, // Octant 2
  1175. DEFAULT_DRAW_CMD | 0 | 0 | 0, // Octant 4
  1176. DEFAULT_DRAW_CMD | 0 | 0 | MAJOR_Y, // Octant 5
  1177. };
  1178. #endif
  1179. // The S3's hardware can have 13 bits of significance for the error and
  1180. // step terms:
  1181. #define NUM_DDA_BITS 13
  1182. /******************************Public*Routine******************************\
  1183. * BOOL bHardwareLine(ppdev, pptfxStart, pptfxEnd)
  1184. *
  1185. * This routine is useful for folks who have line drawing hardware where
  1186. * they can explicitly set the Bresenham terms -- they can use this routine
  1187. * to draw fractional coordinate GIQ lines with the hardware.
  1188. *
  1189. * Fractional coordinate lines require an extra 4 bits of precision in the
  1190. * Bresenham terms. For example, if your hardware has 13 bits of precision
  1191. * for the terms, you can only draw GIQ lines up to 255 pels long using this
  1192. * routine.
  1193. *
  1194. * Input:
  1195. * pptfxStart - Points to GIQ coordinate of start of line
  1196. * pptfxEnd - Points to GIQ coordinate of end of line
  1197. * NUM_DDA_BITS- The number of bits of precision your hardware can support.
  1198. *
  1199. * Output:
  1200. * returns - TRUE if the line was drawn.
  1201. * FALSE if the line is too long, and the strips code must be
  1202. * used.
  1203. *
  1204. * DDALINE:
  1205. * iDir - Direction of the line, as an octant numbered as follows:
  1206. *
  1207. * \ 5 | 6 /
  1208. * \ | /
  1209. * 4 \ | / 7
  1210. * \ /
  1211. * -----+-----
  1212. * /|\
  1213. * 3 / | \ 0
  1214. * / | \
  1215. * / 2 | 1 \
  1216. *
  1217. * ptlStart - Start pixel of line.
  1218. * cPels - # of pels in line. *NOTE* You must check if this is <= 0!
  1219. * dMajor - Major axis delta.
  1220. * dMinor - Minor axis delta.
  1221. * lErrorTerm - Error term.
  1222. *
  1223. * What you do with the last 3 terms may be a little tricky. They are
  1224. * actually the terms for the formula of the normalized line
  1225. *
  1226. * dMinor * x + (lErrorTerm + dMajor)
  1227. * y(x) = floor( ---------------------------------- )
  1228. * dMajor
  1229. *
  1230. * where y(x) is the y coordinate of the pixel to be lit as a function of
  1231. * the x-coordinate.
  1232. *
  1233. * Every time the line advances one in the major direction 'x', dMinor
  1234. * gets added to the current error term. If the resulting value is >= 0,
  1235. * we know we have to move one pixel in the minor direction 'y', and
  1236. * dMajor must be subtracted from the current error term.
  1237. *
  1238. * If you're trying to figure out what this means for your hardware, you can
  1239. * think of the DDALINE terms as having been computed equivalently as
  1240. * follows:
  1241. *
  1242. * dMinor = 2 * (minor axis delta)
  1243. * dMajor = 2 * (major axis delta)
  1244. * lErrorTerm = - (major axis delta) - fixup
  1245. *
  1246. * That is, if your documentation tells you that for integer lines, a
  1247. * register is supposed to be initialized with the value
  1248. * '2 * (minor axis delta)', you'll actually use dMinor.
  1249. *
  1250. * Example: Setting up the 8514
  1251. *
  1252. * AXSTPSIGN is supposed to be the axial step constant register, defined
  1253. * as 2 * (minor axis delta). You set:
  1254. *
  1255. * AXSTPSIGN = dMinor
  1256. *
  1257. * DGSTPSIGN is supposed to be the diagonal step constant register,
  1258. * defined as 2 * (minor axis delta) - 2 * (major axis delta). You set:
  1259. *
  1260. * DGSTPSIGN = dMinor - dMajor
  1261. *
  1262. * ERR_TERM is supposed to be the adjusted error term, defined as
  1263. * 2 * (minor axis delta) - (major axis delta) - fixup. You set:
  1264. *
  1265. * ERR_TERM = lErrorTerm + dMinor
  1266. *
  1267. * Implementation:
  1268. *
  1269. * You'll want to special case integer lines before calling this routine
  1270. * (since they're very common, take less time to the computation of line
  1271. * terms, and can handle longer lines than this routine because 4 bits
  1272. * aren't being given to the fraction).
  1273. *
  1274. * If a GIQ line is too long to be handled by this routine, you can just
  1275. * use the slower strip routines for that line. Note that you cannot
  1276. * just fail the call -- you must be able to accurately draw any line
  1277. * in the 28.4 device space when it intersects the viewport.
  1278. *
  1279. * Testing:
  1280. *
  1281. * Use Guiman, or some other test that draws random fractional coordinate
  1282. * lines and compares them to what GDI itself draws to a bitmap.
  1283. *
  1284. \**************************************************************************/
  1285. BOOL bHardwareLine(
  1286. PDEV* ppdev,
  1287. POINTFIX* pptfxStart, // Start of line
  1288. POINTFIX* pptfxEnd) // End of line
  1289. {
  1290. FLONG fl; // Various flags
  1291. ULONG M0; // Normalized fractional unit x start coordinate (0 <= M0 < F)
  1292. ULONG N0; // Normalized fractional unit y start coordinate (0 <= N0 < F)
  1293. ULONG M1; // Normalized fractional unit x end coordinate (0 <= M1 < F)
  1294. ULONG N1; // Normalized fractional unit x end coordinate (0 <= N1 < F)
  1295. ULONG dM; // Normalized fractional unit x-delta (0 <= dM)
  1296. ULONG dN; // Normalized fractional unit y-delta (0 <= dN <= dM)
  1297. LONG x; // Normalized x coordinate of origin
  1298. LONG y; // Normalized y coordinate of origin
  1299. LONG x0; // Normalized x offset from origin to start pixel (inclusive)
  1300. LONG y0; // Normalized y offset from origin to start pixel (inclusive)
  1301. LONG x1; // Normalized x offset from origin to end pixel (inclusive)
  1302. LONG lGamma;// Bresenham error term at origin
  1303. LONG cPels; // Number of pixels in line
  1304. /***********************************************************************\
  1305. * Normalize line to the first octant.
  1306. \***********************************************************************/
  1307. fl = 0;
  1308. M0 = pptfxStart->x;
  1309. dM = pptfxEnd->x;
  1310. if ((LONG) dM < (LONG) M0)
  1311. {
  1312. // Line runs from right to left, so flip across x = 0:
  1313. M0 = -(LONG) M0;
  1314. dM = -(LONG) dM;
  1315. fl |= HW_FLIP_H;
  1316. }
  1317. // Compute the delta. The DDI says we can never have a valid delta
  1318. // with a magnitude more than 2^31 - 1, but the engine never actually
  1319. // checks its transforms. To ensure that we'll never puke on our shoes,
  1320. // we check for that case and simply refuse to draw the line:
  1321. dM -= M0;
  1322. if ((LONG) dM < 0)
  1323. return(FALSE);
  1324. N0 = pptfxStart->y;
  1325. dN = pptfxEnd->y;
  1326. if ((LONG) dN < (LONG) N0)
  1327. {
  1328. // Line runs from bottom to top, so flip across y = 0:
  1329. N0 = -(LONG) N0;
  1330. dN = -(LONG) dN;
  1331. fl |= HW_FLIP_V;
  1332. }
  1333. // Compute another delta:
  1334. dN -= N0;
  1335. if ((LONG) dN < 0)
  1336. return(FALSE);
  1337. if (dN >= dM)
  1338. {
  1339. if (dN == dM)
  1340. {
  1341. // Have to special case slopes of one:
  1342. fl |= HW_FLIP_SLOPE_ONE;
  1343. }
  1344. else
  1345. {
  1346. // Since line has slope greater than 1, flip across x = y:
  1347. register ULONG ulTmp;
  1348. ulTmp = dM; dM = dN; dN = ulTmp;
  1349. ulTmp = M0; M0 = N0; N0 = ulTmp;
  1350. fl |= HW_FLIP_D;
  1351. }
  1352. }
  1353. // Figure out if we can do the line in hardware, given that we have a
  1354. // limited number of bits of precision for the Bresenham terms.
  1355. //
  1356. // Remember that one bit has to be kept as a sign bit:
  1357. if ((LONG) dM >= (1L << (NUM_DDA_BITS - 1)))
  1358. return(FALSE);
  1359. fl |= gaflHardwareRound[fl];
  1360. /***********************************************************************\
  1361. * Calculate the error term at pixel 0.
  1362. \***********************************************************************/
  1363. x = LFLOOR((LONG) M0);
  1364. y = LFLOOR((LONG) N0);
  1365. M0 = FXFRAC(M0);
  1366. N0 = FXFRAC(N0);
  1367. // NOTE NOTE NOTE: If this routine were to handle any line in the 28.4
  1368. // space, it will overflow its math (the following part requires 36 bits
  1369. // of precision)! But we get here for lines that the hardware can handle
  1370. // (see the expression (dM >= (1L << (NUM_DDA_BITS - 1))) above?), so if
  1371. // cBits is less than 28, we're safe.
  1372. //
  1373. // If you're going to use this routine to handle all lines in the 28.4
  1374. // device space, you will HAVE to make sure the math doesn't overflow,
  1375. // otherwise you won't be NT compliant! (See 'bHardwareLine' for an example
  1376. // how to do that. You don't have to worry about this if you simply
  1377. // default to the strips code for long lines, because those routines
  1378. // already do the math correctly.)
  1379. // Calculate the remainder term [ dM * (N0 + F/2) - M0 * dN ]. Note
  1380. // that M0 and N0 have at most 4 bits of significance (and if the
  1381. // arguments are properly ordered, on a 486 each multiply would be no
  1382. // more than 13 cycles):
  1383. lGamma = (N0 + F/2) * dM - M0 * dN;
  1384. if (fl & HW_Y_ROUND_DOWN)
  1385. lGamma--;
  1386. lGamma >>= FLOG2;
  1387. /***********************************************************************\
  1388. * Figure out which pixels are at the ends of the line.
  1389. \***********************************************************************/
  1390. // The toughest part of GIQ is determining the start and end pels.
  1391. //
  1392. // Our approach here is to calculate x0 and x1 (the inclusive start
  1393. // and end columns of the line respectively, relative to our normalized
  1394. // origin). Then x1 - x0 + 1 is the number of pels in the line. The
  1395. // start point is easily calculated by plugging x0 into our line equation
  1396. // (which takes care of whether y = 1/2 rounds up or down in value)
  1397. // getting y0, and then undoing the normalizing flips to get back
  1398. // into device space.
  1399. //
  1400. // We look at the fractional parts of the coordinates of the start and
  1401. // end points, and call them (M0, N0) and (M1, N1) respectively, where
  1402. // 0 <= M0, N0, M1, N1 < 16. We plot (M0, N0) on the following grid
  1403. // to determine x0:
  1404. //
  1405. // +-----------------------> +x
  1406. // |
  1407. // | 0 1
  1408. // | 0123456789abcdef
  1409. // |
  1410. // | 0 ........?xxxxxxx
  1411. // | 1 ..........xxxxxx
  1412. // | 2 ...........xxxxx
  1413. // | 3 ............xxxx
  1414. // | 4 .............xxx
  1415. // | 5 ..............xx
  1416. // | 6 ...............x
  1417. // | 7 ................
  1418. // | 8 ................
  1419. // | 9 ......**........
  1420. // | a ........****...x
  1421. // | b ............****
  1422. // | c .............xxx****
  1423. // | d ............xxxx ****
  1424. // | e ...........xxxxx ****
  1425. // | f ..........xxxxxx
  1426. // |
  1427. // | 2 3
  1428. // v
  1429. //
  1430. // +y
  1431. //
  1432. // This grid accounts for the appropriate rounding of GIQ and last-pel
  1433. // exclusion. If (M0, N0) lands on an 'x', x0 = 2. If (M0, N0) lands
  1434. // on a '.', x0 = 1. If (M0, N0) lands on a '?', x0 rounds up or down,
  1435. // depending on what flips have been done to normalize the line.
  1436. //
  1437. // For the end point, if (M1, N1) lands on an 'x', x1 =
  1438. // floor((M0 + dM) / 16) + 1. If (M1, N1) lands on a '.', x1 =
  1439. // floor((M0 + dM)). If (M1, N1) lands on a '?', x1 rounds up or down,
  1440. // depending on what flips have been done to normalize the line.
  1441. //
  1442. // Lines of exactly slope one require a special case for both the start
  1443. // and end. For example, if the line ends such that (M1, N1) is (9, 1),
  1444. // the line has gone exactly through (8, 0) -- which may be considered
  1445. // to be part of 'x' because of rounding! So slopes of exactly slope
  1446. // one going through (8, 0) must also be considered as belonging in 'x'
  1447. // when an x value of 1/2 is supposed to round up in value.
  1448. // Calculate x0, x1:
  1449. N1 = FXFRAC(N0 + dN);
  1450. M1 = FXFRAC(M0 + dM);
  1451. x1 = LFLOOR(M0 + dM);
  1452. // Line runs left-to-right:
  1453. // Compute x1:
  1454. x1--;
  1455. if (M1 > 0)
  1456. {
  1457. if (N1 == 0)
  1458. {
  1459. if (LROUND(M1, fl & HW_X_ROUND_DOWN))
  1460. x1++;
  1461. }
  1462. else if (abs((LONG) (N1 - F/2)) <= (LONG) M1)
  1463. {
  1464. x1++;
  1465. }
  1466. }
  1467. if ((fl & (HW_FLIP_SLOPE_ONE | HW_X_ROUND_DOWN))
  1468. == (HW_FLIP_SLOPE_ONE | HW_X_ROUND_DOWN))
  1469. {
  1470. // Have to special-case diagonal lines going through our
  1471. // the point exactly equidistant between two horizontal
  1472. // pixels, if we're supposed to round x=1/2 down:
  1473. if ((M1 > 0) && (N1 == M1 + 8))
  1474. x1--;
  1475. if ((M0 > 0) && (N0 == M0 + 8))
  1476. {
  1477. x0 = 0;
  1478. goto left_to_right_compute_y0;
  1479. }
  1480. }
  1481. // Compute x0:
  1482. x0 = 0;
  1483. if (M0 > 0)
  1484. {
  1485. if (N0 == 0)
  1486. {
  1487. if (LROUND(M0, fl & HW_X_ROUND_DOWN))
  1488. x0 = 1;
  1489. }
  1490. else if (abs((LONG) (N0 - F/2)) <= (LONG) M0)
  1491. {
  1492. x0 = 1;
  1493. }
  1494. }
  1495. left_to_right_compute_y0:
  1496. /***********************************************************************\
  1497. * Calculate the start pixel.
  1498. \***********************************************************************/
  1499. // We now compute y0 and adjust the error term. We know x0, and we know
  1500. // the current formula for the pixels to be lit on the line:
  1501. //
  1502. // dN * x + lGamma
  1503. // y(x) = floor( --------------- )
  1504. // dM
  1505. //
  1506. // The remainder of this expression is the new error term at (x0, y0).
  1507. // Since x0 is going to be either 0 or 1, we don't actually have to do a
  1508. // multiply or divide to compute y0. Finally, we subtract dM from the
  1509. // new error term so that it is in the range [-dM, 0).
  1510. y0 = 0;
  1511. lGamma += (dN & (-x0));
  1512. lGamma -= dM;
  1513. if (lGamma >= 0)
  1514. {
  1515. y0 = 1;
  1516. lGamma -= dM;
  1517. }
  1518. // Undo our flips to get the start coordinate:
  1519. x += x0;
  1520. y += y0;
  1521. if (fl & HW_FLIP_D)
  1522. {
  1523. register LONG lTmp;
  1524. lTmp = x; x = y; y = lTmp;
  1525. }
  1526. if (fl & HW_FLIP_V)
  1527. {
  1528. y = -y;
  1529. }
  1530. if (fl & HW_FLIP_H)
  1531. {
  1532. x = -x;
  1533. }
  1534. /***********************************************************************\
  1535. * Return the Bresenham terms:
  1536. \***********************************************************************/
  1537. // iDir = gaiDir[fl & HW_FLIP_MASK];
  1538. // ptlStart.x = x;
  1539. // ptlStart.y = y;
  1540. // cPels = x1 - x0 + 1; // NOTE: You'll have to check if cPels <= 0!
  1541. // dMajor = dM;
  1542. // dMinor = dN;
  1543. // lErrorTerm = lGamma;
  1544. /***********************************************************************\
  1545. * Draw the line. S3 specific code follows:
  1546. \***********************************************************************/
  1547. cPels = x1 - x0 + 1;
  1548. #if 0
  1549. if (cPels > 0)
  1550. {
  1551. IO_FIFO_WAIT(ppdev, 7);
  1552. IO_CUR_X(ppdev, x);
  1553. IO_CUR_Y(ppdev, y);
  1554. IO_MAJ_AXIS_PCNT(ppdev, cPels);
  1555. IO_AXSTP(ppdev, dN);
  1556. IO_DIASTP(ppdev, dN - dM);
  1557. IO_ERR_TERM(ppdev, dN + lGamma);
  1558. IO_CMD(ppdev, gaiDrawCmd[fl & HW_FLIP_MASK]);
  1559. }
  1560. #endif
  1561. return(TRUE);
  1562. }