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.

1014 lines
36 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: fillpath.c
  3. *
  4. * Contains the DrvFillPath routine, which is used for drawing polygons.
  5. *
  6. * Copyright (c) 1992-1996 Microsoft Corporation
  7. * Copyright (c) 1993-1996 Matrox Electronic Systems, Ltd.
  8. \**************************************************************************/
  9. #include "precomp.h"
  10. // We have to be careful of arithmetic overflow in a number of places.
  11. // Fortunately, the compiler is guaranteed to natively support 64-bit
  12. // signed LONGLONGs and 64-bit unsigned DWORDLONGs.
  13. //
  14. // Int32x32To64(a, b) is a macro defined in 'winnt.h' that multiplies
  15. // two 32-bit LONGs to produce a 64-bit LONGLONG result.
  16. // I use it because it is much faster than 64x64 multiplies.
  17. #define UInt64Div32To32(a, b) \
  18. ((((DWORDLONG)(a)) > ULONG_MAX) ? \
  19. (ULONG)((DWORDLONG)(a) / (ULONG)(b)) : \
  20. (ULONG)((ULONG)(a) / (ULONG)(b)))
  21. #define NUM_BUFFER_POINTS 96 // Maximum number of points in a path
  22. // for which we'll attempt to join
  23. // all the path records so that the
  24. // path may still be drawn by FastFill
  25. // Describe a single non-horizontal edge of a path to fill.
  26. typedef struct _EDGE {
  27. PVOID pNext;
  28. INT iScansLeft;
  29. INT X;
  30. INT Y;
  31. INT iErrorTerm;
  32. INT iErrorAdjustUp;
  33. INT iErrorAdjustDown;
  34. INT iXWhole;
  35. INT iXDirection;
  36. INT iWindingDirection;
  37. } EDGE, *PEDGE;
  38. // Maximum number of rects we'll fill per call to
  39. // the fill code
  40. #define MAX_PATH_RECTS 50
  41. #define RECT_BYTES (MAX_PATH_RECTS * sizeof(RECTL))
  42. #define EDGE_BYTES (TMP_BUFFER_SIZE - RECT_BYTES)
  43. #define MAX_EDGES (EDGE_BYTES/sizeof(EDGE))
  44. VOID AdvanceAETEdges(EDGE *pAETHead);
  45. VOID XSortAETEdges(EDGE *pAETHead);
  46. VOID MoveNewEdges(EDGE *pGETHead, EDGE *pAETHead, INT iCurrentY);
  47. EDGE * AddEdgeToGET(EDGE *pGETHead, EDGE *pFreeEdge, POINTFIX *ppfxEdgeStart,
  48. POINTFIX *ppfxEdgeEnd, RECTL *pClipRect);
  49. BOOL ConstructGET(EDGE *pGETHead, EDGE *pFreeEdges, PATHOBJ *ppo,
  50. PATHDATA *pd, BOOL bMore, RECTL *pClipRect);
  51. void AdjustErrorTerm(INT *pErrorTerm, INT iErrorAdjustUp, INT iErrorAdjustDown,
  52. INT yJump, INT *pXStart, INT iXDirection);
  53. /******************************Public*Routine******************************\
  54. * DrvFillPath
  55. *
  56. * Fill the specified path with the specified brush and ROP. This routine
  57. * detects single convex polygons, and will call to separate faster convex
  58. * polygon code for those cases. This routine also detects polygons that
  59. * are really rectangles, and handles those separately as well.
  60. *
  61. * Note: Multiple polygons in a path cannot be treated as being disjoint;
  62. * the fill must consider all the points in the path. That is, if the
  63. * path contains multiple polygons, you cannot simply draw one polygon
  64. * after the other (unless they don't overlap).
  65. *
  66. * Note: This function is optional, but is recommended for good performance.
  67. * To get GDI to call this function, not only do you have to
  68. * HOOK_FILLPATH, you have to set GCAPS_ALTERNATEFILL and/or
  69. * GCAPS_WINDINGFILL.
  70. *
  71. \**************************************************************************/
  72. BOOL DrvFillPath(
  73. SURFOBJ* pso,
  74. PATHOBJ* ppo,
  75. CLIPOBJ* pco,
  76. BRUSHOBJ* pbo,
  77. POINTL* pptlBrush,
  78. MIX mix,
  79. FLONG flOptions)
  80. {
  81. BYTE jClipping; // clipping type
  82. EDGE *pCurrentEdge;
  83. EDGE AETHead; // dummy head/tail node & sentinel for Active Edge Table
  84. EDGE *pAETHead; // pointer to AETHead
  85. EDGE GETHead; // dummy head/tail node & sentinel for Global Edge Table
  86. EDGE *pGETHead; // pointer to GETHead
  87. EDGE *pFreeEdges; // pointer to memory free for use to store edges
  88. ULONG ulNumRects; // # of rectangles to draw currently in rectangle list
  89. RECTL *prclRects; // pointer to start of rectangle draw list
  90. INT iCurrentY; // scan line for which we're currently scanning out the
  91. // fill
  92. ULONG rop4; // Hardware mix value
  93. RBRUSH_COLOR rbc; // Realized brush or solid color
  94. ULONG iSolidColor; // Copy of pbo->iSolidColor
  95. FNFILL *pfnFill; // Points to appropriate fill routine
  96. BOOL bMore;
  97. PATHDATA pd;
  98. RECTL ClipRect;
  99. PDEV *ppdev;
  100. DSURF *pdsurf;
  101. RECTL* prclClip;
  102. BOOL bRetVal=FALSE; // FALSE until proven TRUE
  103. BOOL bMemAlloced=FALSE; // FALSE until proven TRUE
  104. FLONG flFirstRecord;
  105. POINTFIX* pptfxTmp;
  106. ULONG cptfxTmp;
  107. RECTFX rcfxBounds;
  108. POINTFIX aptfxBuf[NUM_BUFFER_POINTS];
  109. // Set up the clipping
  110. if (pco == (CLIPOBJ *) NULL) {
  111. // No CLIPOBJ provided, so we don't have to worry about clipping
  112. jClipping = DC_TRIVIAL;
  113. } else {
  114. // Use the CLIPOBJ-provided clipping
  115. jClipping = pco->iDComplexity;
  116. }
  117. if (jClipping != DC_TRIVIAL) {
  118. if (jClipping != DC_RECT) {
  119. goto ReturnFalse; // there is complex clipping; let GDI fill the path
  120. }
  121. // Clip to the clip rectangle
  122. ClipRect = pco->rclBounds;
  123. } else {
  124. // So the y-clipping code doesn't do any clipping
  125. // /16 so we don't blow the values out when we scale up to GIQ
  126. ClipRect.top = (LONG_MIN + 1) / 16; // +1 to avoid compiler problem
  127. ClipRect.bottom = LONG_MAX / 16;
  128. }
  129. // Pass the surface off to GDI if it's a device bitmap that we've
  130. // converted to a DIB:
  131. pdsurf = (DSURF*) pso->dhsurf;
  132. if (pdsurf->dt == DT_DIB)
  133. {
  134. return(EngFillPath(pdsurf->pso, ppo, pco, pbo, pptlBrush, mix,
  135. flOptions));
  136. }
  137. // We'll be drawing to the screen or an off-screen DFB; copy the surface's
  138. // offset now so that we won't need to refer to the DSURF again:
  139. ppdev = (PDEV*) pso->dhpdev;
  140. ppdev->xOffset = pdsurf->poh->x;
  141. ppdev->yOffset = pdsurf->poh->y;
  142. pfnFill = ppdev->pfnFillSolid;
  143. iSolidColor = 0; // Assume we won't need a pattern
  144. rop4 = (gaRop3FromMix[mix >> 8] << 8) | gaRop3FromMix[mix & 0xff];
  145. if ((((rop4 & 0xff00) >> 8) != (rop4 & 0x00ff)) ||
  146. ((((rop4 >> 4) ^ (rop4)) & 0xf0f) != 0)) // Only do if we need a pattern
  147. {
  148. iSolidColor = pbo->iSolidColor;
  149. rbc.iSolidColor = iSolidColor;
  150. if (iSolidColor == -1)
  151. {
  152. rbc.prb = pbo->pvRbrush;
  153. if (rbc.prb == NULL)
  154. {
  155. rbc.prb = BRUSHOBJ_pvGetRbrush(pbo);
  156. if (rbc.prb == NULL)
  157. return(FALSE);
  158. }
  159. pfnFill = rbc.prb->pfnFillPat;
  160. }
  161. }
  162. // Enumerate path here first time to check for special
  163. // cases (rectangles and monotone polygons)
  164. // It is too difficult to determine interaction between
  165. // multiple paths, if there is more than one, skip this
  166. bMore = PATHOBJ_bEnum(ppo, &pd);
  167. {
  168. prclClip = NULL;
  169. if (jClipping == DC_RECT)
  170. {
  171. prclClip = &ClipRect;
  172. // Our FastFill routine does cross products and intersection
  173. // calculations assuming it can use 32 bit math and not
  174. // overflow. As such, we have to ensure that the bounds of
  175. // the polygon fit in a 15 bit space, including the 4 bit fix
  176. // point fraction. Note that we don't have to do this check
  177. // for trivial clipping, because we'll assume the screen
  178. // dimensions are 2048 x 2048 or smaller:
  179. PATHOBJ_vGetBounds(ppo, &rcfxBounds);
  180. if (((rcfxBounds.xRight - rcfxBounds.xLeft) > 0x7fff) ||
  181. ((rcfxBounds.yBottom - rcfxBounds.yTop) > 0x7fff))
  182. goto SkipFastFill;
  183. }
  184. if (iSolidColor == (ULONG) -1)
  185. {
  186. // Fastfill handles patterns only when running at 8bpp,
  187. // because hardware bugs make it difficult to write
  188. // efficient trapezoid routines at 16bpp and higher:
  189. if (ppdev->iBitmapFormat != BMF_8BPP)
  190. goto SkipFastFill;
  191. // Fastfill doesn't handle non-patcopy rops at 8bpp because
  192. // of a hardware bug:
  193. if (mix != 0x0d0d)
  194. goto SkipFastFill;
  195. }
  196. if (bMore)
  197. {
  198. // FastFill only knows how to take a single contiguous buffer
  199. // of points. Unfortunately, GDI sometimes hands us paths
  200. // that are split over multiple path data records. Convex
  201. // figures such as Ellipses, Pies and RoundRects are almost
  202. // always given in multiple records. Since probably 90% of
  203. // multiple record paths could still be done by FastFill, for
  204. // those cases we simply copy the points into a contiguous
  205. // buffer...
  206. // First make sure that the entire path would fit in the
  207. // temporary buffer, and make sure the path isn't comprised
  208. // of more than one subpath:
  209. if ((ppo->cCurves >= NUM_BUFFER_POINTS) ||
  210. (pd.flags & PD_ENDSUBPATH))
  211. goto SkipFastFill;
  212. pptfxTmp = &aptfxBuf[0];
  213. RtlCopyMemory(pptfxTmp, pd.pptfx, sizeof(POINTFIX) * pd.count);
  214. pptfxTmp += pd.count;
  215. cptfxTmp = pd.count;
  216. flFirstRecord = pd.flags; // Remember PD_BEGINSUBPATH flag
  217. do {
  218. bMore = PATHOBJ_bEnum(ppo, &pd);
  219. RtlCopyMemory(pptfxTmp, pd.pptfx, sizeof(POINTFIX) * pd.count);
  220. cptfxTmp += pd.count;
  221. pptfxTmp += pd.count;
  222. } while (!(pd.flags & PD_ENDSUBPATH));
  223. // Fake up the path data record:
  224. pd.pptfx = &aptfxBuf[0];
  225. pd.count = cptfxTmp;
  226. pd.flags |= flFirstRecord;
  227. // If there's more than one subpath, we can't call FastFill:
  228. if (bMore)
  229. goto SkipFastFill;
  230. }
  231. if (bFastFill(ppdev, pd.count, pd.pptfx, rop4, iSolidColor,
  232. rbc.prb, pptlBrush, prclClip))
  233. {
  234. return(TRUE);
  235. }
  236. }
  237. // There's nothing to do if there are only one or two points
  238. if (ppo->cCurves <= 2) {
  239. goto ReturnTrue;
  240. }
  241. SkipFastFill:
  242. // Set up working storage in the temporary buffer
  243. prclRects = (RECTL*) ppdev->pvTmpBuffer; // storage for list of rectangles to draw
  244. if (!bMore) {
  245. RECTL *rectangle;
  246. INT cPoints = pd.count;
  247. // The count can't be less than three, because we got all the edges
  248. // in this subpath, and above we checked that there were at least
  249. // three edges
  250. // If the count is four, check to see if the polygon is really a
  251. // rectangle since we can really speed that up. We'll also check for
  252. // five with the first and last points the same, because under Win 3.1,
  253. // it was required to close polygons
  254. if ((cPoints == 4) ||
  255. ((cPoints == 5) &&
  256. (pd.pptfx[0].x == pd.pptfx[4].x) &&
  257. (pd.pptfx[0].y == pd.pptfx[4].y))) {
  258. rectangle = prclRects;
  259. /* we have to start somewhere so assume that most
  260. applications specify the top left point first
  261. we want to check that the first two points are
  262. either vertically or horizontally aligned. if
  263. they are then we check that the last point [3]
  264. is either horizontally or vertically aligned,
  265. and finally that the 3rd point [2] is aligned
  266. with both the first point and the last point */
  267. #define FIX_SHIFT 4L
  268. #define FIX_MASK (- (1 << FIX_SHIFT))
  269. rectangle->top = pd.pptfx[0].y - 1 & FIX_MASK;
  270. rectangle->left = pd.pptfx[0].x - 1 & FIX_MASK;
  271. rectangle->right = pd.pptfx[1].x - 1 & FIX_MASK;
  272. if (rectangle->left ^ rectangle->right) {
  273. if (rectangle->top ^ (pd.pptfx[1].y - 1 & FIX_MASK))
  274. goto not_rectangle;
  275. if (rectangle->left ^ (pd.pptfx[3].x - 1 & FIX_MASK))
  276. goto not_rectangle;
  277. if (rectangle->right ^ (pd.pptfx[2].x - 1 & FIX_MASK))
  278. goto not_rectangle;
  279. rectangle->bottom = pd.pptfx[2].y - 1 & FIX_MASK;
  280. if (rectangle->bottom ^ (pd.pptfx[3].y - 1 & FIX_MASK))
  281. goto not_rectangle;
  282. }
  283. else {
  284. if (rectangle->top ^ (pd.pptfx[3].y - 1 & FIX_MASK))
  285. goto not_rectangle;
  286. rectangle->bottom = pd.pptfx[1].y - 1 & FIX_MASK;
  287. if (rectangle->bottom ^ (pd.pptfx[2].y - 1 & FIX_MASK))
  288. goto not_rectangle;
  289. rectangle->right = pd.pptfx[2].x - 1 & FIX_MASK;
  290. if (rectangle->right ^ (pd.pptfx[3].x - 1 & FIX_MASK))
  291. goto not_rectangle;
  292. }
  293. /* if the left is greater than the right then
  294. swap them so the blt code doesn't wig out */
  295. if (rectangle->left > rectangle->right) {
  296. FIX temp;
  297. temp = rectangle->left;
  298. rectangle->left = rectangle->right;
  299. rectangle->right = temp;
  300. }
  301. else {
  302. /* if left == right there's nothing to draw */
  303. if (rectangle->left == rectangle->right) {
  304. goto ReturnTrue;
  305. }
  306. }
  307. /* shift the values to get pixel coordinates */
  308. rectangle->left = (rectangle->left >> FIX_SHIFT) + 1;
  309. rectangle->right = (rectangle->right >> FIX_SHIFT) + 1;
  310. if (rectangle->top > rectangle->bottom) {
  311. FIX temp;
  312. temp = rectangle->top;
  313. rectangle->top = rectangle->bottom;
  314. rectangle->bottom = temp;
  315. }
  316. else {
  317. if (rectangle->top == rectangle->bottom) {
  318. goto ReturnTrue;
  319. }
  320. }
  321. /* shift the values to get pixel coordinates */
  322. rectangle->top = (rectangle->top >> FIX_SHIFT) + 1;
  323. rectangle->bottom = (rectangle->bottom >> FIX_SHIFT) + 1;
  324. // Finally, check for clipping
  325. if (jClipping == DC_RECT) {
  326. // Clip to the clip rectangle
  327. if (!bIntersect(rectangle, &ClipRect, rectangle)) {
  328. // Totally clipped, nothing to do
  329. goto ReturnTrue;
  330. }
  331. }
  332. /* if we get here then the polygon is a rectangle,
  333. set count to 1 and goto bottom to draw it */
  334. ulNumRects = 1;
  335. goto draw_remaining_rectangles;
  336. }
  337. not_rectangle:
  338. ;
  339. }
  340. // Do we have enough memory for all the edges?
  341. // LATER does cCurves include closure?
  342. if (ppo->cCurves > MAX_EDGES) {
  343. //
  344. // try to allocate enough memory
  345. //
  346. pFreeEdges = (EDGE *) EngAllocMem(0, (ppo->cCurves * sizeof(EDGE)), ALLOC_TAG);
  347. if (pFreeEdges == NULL)
  348. {
  349. goto ReturnFalse; // too many edges; let GDI fill the path
  350. }
  351. else
  352. {
  353. bMemAlloced = TRUE;
  354. }
  355. }
  356. else {
  357. pFreeEdges = (EDGE*) ((BYTE*) ppdev->pvTmpBuffer + RECT_BYTES);
  358. // use our handy temporary buffer (it's big enough)
  359. }
  360. // Initialize an empty list of rectangles to fill
  361. ulNumRects = 0;
  362. // Enumerate the path edges and build a Global Edge Table (GET) from them
  363. // in YX-sorted order.
  364. pGETHead = &GETHead;
  365. if (!ConstructGET(pGETHead, pFreeEdges, ppo, &pd, bMore, &ClipRect)) {
  366. goto ReturnFalse; // outside GDI's 2**27 range
  367. }
  368. // Create an empty AET with the head node also a tail sentinel
  369. pAETHead = &AETHead;
  370. AETHead.pNext = pAETHead; // mark that the AET is empty
  371. AETHead.X = 0x7FFFFFFF; // this is greater than any valid X value, so
  372. // searches will always terminate
  373. // Top scan of polygon is the top of the first edge we come to
  374. iCurrentY = ((EDGE *)GETHead.pNext)->Y;
  375. // Loop through all the scans in the polygon, adding edges from the GET to
  376. // the Active Edge Table (AET) as we come to their starts, and scanning out
  377. // the AET at each scan into a rectangle list. Each time it fills up, the
  378. // rectangle list is passed to the filling routine, and then once again at
  379. // the end if any rectangles remain undrawn. We continue so long as there
  380. // are edges to be scanned out
  381. while (1) {
  382. // Advance the edges in the AET one scan, discarding any that have
  383. // reached the end (if there are any edges in the AET)
  384. if (AETHead.pNext != pAETHead) {
  385. AdvanceAETEdges(pAETHead);
  386. }
  387. // If the AET is empty, done if the GET is empty, else jump ahead to
  388. // the next edge in the GET; if the AET isn't empty, re-sort the AET
  389. if (AETHead.pNext == pAETHead) {
  390. if (GETHead.pNext == pGETHead) {
  391. // Done if there are no edges in either the AET or the GET
  392. break;
  393. }
  394. // There are no edges in the AET, so jump ahead to the next edge in
  395. // the GET
  396. iCurrentY = ((EDGE *)GETHead.pNext)->Y;
  397. } else {
  398. // Re-sort the edges in the AET by X coordinate, if there are at
  399. // least two edges in the AET (there could be one edge if the
  400. // balancing edge hasn't yet been added from the GET)
  401. if (((EDGE *)AETHead.pNext)->pNext != pAETHead) {
  402. XSortAETEdges(pAETHead);
  403. }
  404. }
  405. // Move any new edges that start on this scan from the GET to the AET;
  406. // bother calling only if there's at least one edge to add
  407. if (((EDGE *)GETHead.pNext)->Y == iCurrentY) {
  408. MoveNewEdges(pGETHead, pAETHead, iCurrentY);
  409. }
  410. // Scan the AET into rectangles to fill (there's always at least one
  411. // edge pair in the AET)
  412. pCurrentEdge = AETHead.pNext; // point to the first edge
  413. do {
  414. INT iLeftEdge;
  415. // The left edge of any given edge pair is easy to find; it's just
  416. // wherever we happen to be currently
  417. iLeftEdge = pCurrentEdge->X;
  418. // Find the matching right edge according to the current fill rule
  419. if ((flOptions & FP_WINDINGMODE) != 0) {
  420. INT iWindingCount;
  421. // Do winding fill; scan across until we've found equal numbers
  422. // of up and down edges
  423. iWindingCount = pCurrentEdge->iWindingDirection;
  424. do {
  425. pCurrentEdge = pCurrentEdge->pNext;
  426. iWindingCount += pCurrentEdge->iWindingDirection;
  427. } while (iWindingCount != 0);
  428. } else {
  429. // Odd-even fill; the next edge is the matching right edge
  430. pCurrentEdge = pCurrentEdge->pNext;
  431. }
  432. // See if the resulting span encompasses at least one pixel, and
  433. // add it to the list of rectangles to draw if so
  434. if (iLeftEdge < pCurrentEdge->X) {
  435. // We've got an edge pair to add to the list to be filled; see
  436. // if there's room for one more rectangle
  437. if (ulNumRects >= MAX_PATH_RECTS) {
  438. // No more room; draw the rectangles in the list and reset
  439. // it to empty
  440. (*pfnFill)(ppdev, ulNumRects, prclRects, rop4,
  441. rbc, pptlBrush);
  442. // Reset the list to empty
  443. ulNumRects = 0;
  444. }
  445. // Add the rectangle representing the current edge pair
  446. if (jClipping == DC_RECT) {
  447. // Clipped
  448. // Clip to left
  449. prclRects[ulNumRects].left = max(iLeftEdge, ClipRect.left);
  450. // Clip to right
  451. prclRects[ulNumRects].right =
  452. min(pCurrentEdge->X, ClipRect.right);
  453. // Draw only if not fully clipped
  454. if (prclRects[ulNumRects].left <
  455. prclRects[ulNumRects].right) {
  456. prclRects[ulNumRects].top = iCurrentY;
  457. prclRects[ulNumRects].bottom = iCurrentY+1;
  458. ulNumRects++;
  459. }
  460. }
  461. else
  462. {
  463. // Unclipped
  464. prclRects[ulNumRects].top = iCurrentY;
  465. prclRects[ulNumRects].bottom = iCurrentY+1;
  466. prclRects[ulNumRects].left = iLeftEdge;
  467. prclRects[ulNumRects].right = pCurrentEdge->X;
  468. ulNumRects++;
  469. }
  470. }
  471. } while ((pCurrentEdge = pCurrentEdge->pNext) != pAETHead);
  472. iCurrentY++; // next scan
  473. }
  474. /* draw the remaining rectangles, if there are any */
  475. draw_remaining_rectangles:
  476. if (ulNumRects > 0) {
  477. (*pfnFill)(ppdev, ulNumRects, prclRects, rop4, rbc, pptlBrush);
  478. }
  479. ReturnTrue:
  480. bRetVal = TRUE; // done successfully
  481. ReturnFalse:
  482. // bRetVal is originally false. If you jumped to ReturnFalse from somewhere,
  483. // then it will remain false, and be returned.
  484. if (bMemAlloced)
  485. {
  486. //
  487. // we did allocate memory, so release it
  488. //
  489. EngFreeMem (pFreeEdges);
  490. }
  491. return(bRetVal);
  492. }
  493. // Advance the edges in the AET to the next scan, dropping any for which we've
  494. // done all scans. Assumes there is at least one edge in the AET.
  495. VOID AdvanceAETEdges(EDGE *pAETHead)
  496. {
  497. EDGE *pLastEdge, *pCurrentEdge;
  498. pLastEdge = pAETHead;
  499. pCurrentEdge = pLastEdge->pNext;
  500. do {
  501. // Count down this edge's remaining scans
  502. if (--pCurrentEdge->iScansLeft == 0) {
  503. // We've done all scans for this edge; drop this edge from the AET
  504. pLastEdge->pNext = pCurrentEdge->pNext;
  505. } else {
  506. // Advance the edge's X coordinate for a 1-scan Y advance
  507. // Advance by the minimum amount
  508. pCurrentEdge->X += pCurrentEdge->iXWhole;
  509. // Advance the error term and see if we got one extra pixel this
  510. // time
  511. pCurrentEdge->iErrorTerm += pCurrentEdge->iErrorAdjustUp;
  512. if (pCurrentEdge->iErrorTerm >= 0) {
  513. // The error term turned over, so adjust the error term and
  514. // advance the extra pixel
  515. pCurrentEdge->iErrorTerm -= pCurrentEdge->iErrorAdjustDown;
  516. pCurrentEdge->X += pCurrentEdge->iXDirection;
  517. }
  518. pLastEdge = pCurrentEdge;
  519. }
  520. } while ((pCurrentEdge = pLastEdge->pNext) != pAETHead);
  521. }
  522. // X-sort the AET, because the edges may have moved around relative to
  523. // one another when we advanced them. We'll use a multipass bubble
  524. // sort, which is actually okay for this application because edges
  525. // rarely move relative to one another, so we usually do just one pass.
  526. // Also, this makes it easy to keep just a singly-linked list. Assumes there
  527. // are at least two edges in the AET.
  528. VOID XSortAETEdges(EDGE *pAETHead)
  529. {
  530. BOOL bEdgesSwapped;
  531. EDGE *pLastEdge, *pCurrentEdge, *pNextEdge;
  532. do {
  533. bEdgesSwapped = FALSE;
  534. pLastEdge = pAETHead;
  535. pCurrentEdge = pLastEdge->pNext;
  536. pNextEdge = pCurrentEdge->pNext;
  537. do {
  538. if (pNextEdge->X < pCurrentEdge->X) {
  539. // Next edge is to the left of the current edge; swap them
  540. pLastEdge->pNext = pNextEdge;
  541. pCurrentEdge->pNext = pNextEdge->pNext;
  542. pNextEdge->pNext = pCurrentEdge;
  543. bEdgesSwapped = TRUE;
  544. pCurrentEdge = pNextEdge; // continue sorting before the edge
  545. // we just swapped; it might move
  546. // farther yet
  547. }
  548. pLastEdge = pCurrentEdge;
  549. pCurrentEdge = pLastEdge->pNext;
  550. } while ((pNextEdge = pCurrentEdge->pNext) != pAETHead);
  551. } while (bEdgesSwapped);
  552. }
  553. // Moves all edges that start on the current scan from the GET to the AET in
  554. // X-sorted order. Parameters are pointer to head of GET and pointer to dummy
  555. // edge at head of AET, plus current scan line. Assumes there's at least one
  556. // edge to be moved.
  557. VOID MoveNewEdges(EDGE *pGETHead, EDGE *pAETHead, INT iCurrentY)
  558. {
  559. EDGE *pCurrentEdge = pAETHead;
  560. EDGE *pGETNext = pGETHead->pNext;
  561. do {
  562. // Scan through the AET until the X-sorted insertion point for this
  563. // edge is found. We can continue from where the last search left
  564. // off because the edges in the GET are in X sorted order, as is
  565. // the AET. The search always terminates because the AET sentinel
  566. // is greater than any valid X
  567. while (pGETNext->X > ((EDGE *)pCurrentEdge->pNext)->X) {
  568. pCurrentEdge = pCurrentEdge->pNext;
  569. }
  570. // We've found the insertion point; add the GET edge to the AET, and
  571. // remove it from the GET
  572. pGETHead->pNext = pGETNext->pNext;
  573. pGETNext->pNext = pCurrentEdge->pNext;
  574. pCurrentEdge->pNext = pGETNext;
  575. pCurrentEdge = pGETNext; // continue insertion search for the next
  576. // GET edge after the edge we just added
  577. pGETNext = pGETHead->pNext;
  578. } while (pGETNext->Y == iCurrentY);
  579. }
  580. // Build the Global Edge Table from the path. There must be enough memory in
  581. // the free edge area to hold all edges. The GET is constructed in Y-X order,
  582. // and has a head/tail/sentinel node at pGETHead.
  583. BOOL ConstructGET(
  584. EDGE *pGETHead,
  585. EDGE *pFreeEdges,
  586. PATHOBJ *ppo,
  587. PATHDATA *pd,
  588. BOOL bMore,
  589. RECTL *pClipRect)
  590. {
  591. POINTFIX pfxPathStart; // point that started the current subpath
  592. POINTFIX pfxPathPrevious; // point before the current point in a subpath;
  593. // starts the current edge
  594. /* Create an empty GET with the head node also a tail sentinel */
  595. pGETHead->pNext = pGETHead; // mark that the GET is empty
  596. pGETHead->Y = 0x7FFFFFFF; // this is greater than any valid Y value, so
  597. // searches will always terminate
  598. /* PATHOBJ_vEnumStart is implicitly performed by engine
  599. already and first path is enumerated by the caller */
  600. next_subpath:
  601. /* Make sure the PATHDATA is not empty (is this necessary) */
  602. if (pd->count != 0) {
  603. /* If first point starts a subpath, remember it as such
  604. and go on to the next point, so we can get an edge */
  605. if (pd->flags & PD_BEGINSUBPATH) {
  606. /* the first point starts the subpath; remember it */
  607. pfxPathStart = *pd->pptfx; /* the subpath starts here */
  608. pfxPathPrevious = *pd->pptfx; /* this points starts the next edge */
  609. pd->pptfx++; /* advance to the next point */
  610. pd->count--; /* count off this point */
  611. }
  612. /* add edges in PATHDATA to GET, in Y-X sorted order */
  613. while (pd->count--) {
  614. if ((pFreeEdges =
  615. AddEdgeToGET(pGETHead, pFreeEdges, &pfxPathPrevious, pd->pptfx,
  616. pClipRect)) == NULL) {
  617. goto ReturnFalse;
  618. }
  619. pfxPathPrevious = *pd->pptfx; /* current point becomes previous */
  620. pd->pptfx++; /* advance to the next point */
  621. }
  622. /* If last point ends the subpath, insert the edge that
  623. connects to first point (is this built in already?) */
  624. if (pd->flags & PD_ENDSUBPATH) {
  625. if ((pFreeEdges = AddEdgeToGET(pGETHead, pFreeEdges, &pfxPathPrevious,
  626. &pfxPathStart, pClipRect)) == NULL) {
  627. goto ReturnFalse;
  628. }
  629. }
  630. }
  631. /* the initial loop conditions preclude a do, while or for */
  632. if (bMore) {
  633. bMore = PATHOBJ_bEnum(ppo, pd);
  634. goto next_subpath;
  635. }
  636. return(TRUE); // done successfully
  637. ReturnFalse:
  638. return(FALSE); // failed
  639. }
  640. // Adds the edge described by the two passed-in points to the Global Edge
  641. // Table, if the edge spans at least one pixel vertically.
  642. EDGE * AddEdgeToGET(EDGE *pGETHead, EDGE *pFreeEdge,
  643. POINTFIX *ppfxEdgeStart, POINTFIX *ppfxEdgeEnd, RECTL *pClipRect)
  644. {
  645. INT iYStart, iYEnd, iXStart, iXEnd, iYHeight, iXWidth;
  646. INT yJump, yTop;
  647. // Set the winding-rule direction of the edge, and put the endpoints in
  648. // top-to-bottom order
  649. iYHeight = ppfxEdgeEnd->y - ppfxEdgeStart->y;
  650. if (iYHeight == 0) {
  651. return(pFreeEdge); // zero height; ignore this edge
  652. } else if (iYHeight >= 0) {
  653. iXStart = ppfxEdgeStart->x;
  654. iYStart = ppfxEdgeStart->y;
  655. iXEnd = ppfxEdgeEnd->x;
  656. iYEnd = ppfxEdgeEnd->y;
  657. pFreeEdge->iWindingDirection = 1;
  658. } else {
  659. iYHeight = -iYHeight;
  660. iXEnd = ppfxEdgeStart->x;
  661. iYEnd = ppfxEdgeStart->y;
  662. iXStart = ppfxEdgeEnd->x;
  663. iYStart = ppfxEdgeEnd->y;
  664. pFreeEdge->iWindingDirection = -1;
  665. }
  666. if (iYHeight & 0x80000000) {
  667. return(NULL); // too large; outside 2**27 GDI range
  668. }
  669. // Set the error term and adjustment factors, all in GIQ coordinates for
  670. // now
  671. iXWidth = iXEnd - iXStart;
  672. if (iXWidth >= 0) {
  673. // Left to right, so we change X as soon as we move at all
  674. pFreeEdge->iXDirection = 1;
  675. pFreeEdge->iErrorTerm = -1;
  676. } else {
  677. // Right to left, so we don't change X until we've moved a full GIQ
  678. // coordinate
  679. iXWidth = -iXWidth;
  680. pFreeEdge->iXDirection = -1;
  681. pFreeEdge->iErrorTerm = -iYHeight;
  682. }
  683. if (iXWidth & 0x80000000) {
  684. return(NULL); // too large; outside 2**27 GDI range
  685. }
  686. if (iXWidth >= iYHeight) {
  687. // Calculate base run length (minimum distance advanced in X for a 1-
  688. // scan advance in Y)
  689. pFreeEdge->iXWhole = iXWidth / iYHeight;
  690. // Add sign back into base run length if going right to left
  691. if (pFreeEdge->iXDirection == -1) {
  692. pFreeEdge->iXWhole = -pFreeEdge->iXWhole;
  693. }
  694. pFreeEdge->iErrorAdjustUp = iXWidth % iYHeight;
  695. } else {
  696. // Base run length is 0, because line is closer to vertical than
  697. // horizontal
  698. pFreeEdge->iXWhole = 0;
  699. pFreeEdge->iErrorAdjustUp = iXWidth;
  700. }
  701. pFreeEdge->iErrorAdjustDown = iYHeight;
  702. // Calculate the number of pixels spanned by this edge, accounting for
  703. // clipping
  704. // Top true pixel scan in GIQ coordinates
  705. // Shifting to divide and multiply by 16 is okay because the clip rect
  706. // always contains positive numbers
  707. yTop = max(pClipRect->top << 4, (iYStart + 15) & ~0x0F);
  708. pFreeEdge->Y = yTop >> 4; // initial scan line on which to fill edge
  709. // Calculate # of scans to actually fill, accounting for clipping
  710. if ((pFreeEdge->iScansLeft = min(pClipRect->bottom, ((iYEnd + 15) >> 4))
  711. - pFreeEdge->Y) <= 0) {
  712. return(pFreeEdge); // no pixels at all are spanned, so we can
  713. // ignore this edge
  714. }
  715. // If the edge doesn't start on a pixel scan (that is, it starts at a
  716. // fractional GIQ coordinate), advance it to the first pixel scan it
  717. // intersects. Ditto if there's top clipping. Also clip to the bottom if
  718. // needed
  719. if (iYStart != yTop) {
  720. // Jump ahead by the Y distance in GIQ coordinates to the first pixel
  721. // to draw
  722. yJump = yTop - iYStart;
  723. // Advance x the minimum amount for the number of scans traversed
  724. iXStart += pFreeEdge->iXWhole * yJump;
  725. AdjustErrorTerm(&pFreeEdge->iErrorTerm, pFreeEdge->iErrorAdjustUp,
  726. pFreeEdge->iErrorAdjustDown, yJump, &iXStart,
  727. pFreeEdge->iXDirection);
  728. }
  729. // Turn the calculations into pixel rather than GIQ calculations
  730. // Move the X coordinate to the nearest pixel, and adjust the error term
  731. // accordingly
  732. // Dividing by 16 with a shift is okay because X is always positive
  733. pFreeEdge->X = (iXStart + 15) >> 4; // convert from GIQ to pixel coordinates
  734. // LATER adjust only if needed (if prestepped above)?
  735. if (pFreeEdge->iXDirection == 1) {
  736. // Left to right
  737. pFreeEdge->iErrorTerm -= pFreeEdge->iErrorAdjustDown *
  738. (((iXStart + 15) & ~0x0F) - iXStart);
  739. } else {
  740. // Right to left
  741. pFreeEdge->iErrorTerm -= pFreeEdge->iErrorAdjustDown *
  742. ((iXStart - 1) & 0x0F);
  743. }
  744. // Scale the error term down 16 times to switch from GIQ to pixels.
  745. // Shifts work to do the multiplying because these values are always
  746. // non-negative
  747. pFreeEdge->iErrorTerm >>= 4;
  748. // Insert the edge into the GET in YX-sorted order. The search always ends
  749. // because the GET has a sentinel with a greater-than-possible Y value
  750. while ((pFreeEdge->Y > ((EDGE *)pGETHead->pNext)->Y) ||
  751. ((pFreeEdge->Y == ((EDGE *)pGETHead->pNext)->Y) &&
  752. (pFreeEdge->X > ((EDGE *)pGETHead->pNext)->X))) {
  753. pGETHead = pGETHead->pNext;
  754. }
  755. pFreeEdge->pNext = pGETHead->pNext; // link the edge into the GET
  756. pGETHead->pNext = pFreeEdge;
  757. return(++pFreeEdge); // point to the next edge storage location for next
  758. // time
  759. }
  760. // Adjust the error term for a skip ahead in y. This is in ASM because there's
  761. // a multiply/divide that may involve a larger than 32-bit value.
  762. void AdjustErrorTerm(INT *pErrorTerm, INT iErrorAdjustUp, INT iErrorAdjustDown,
  763. INT yJump, INT *pXStart, INT iXDirection)
  764. {
  765. #if defined(_X86_) || defined(i386)
  766. // Adjust the error term up by the number of y coordinates we'll skip
  767. //*pErrorTerm += iErrorAdjustUp * yJump;
  768. _asm mov ebx,pErrorTerm
  769. _asm mov eax,iErrorAdjustUp
  770. _asm mul yJump
  771. _asm add eax,[ebx]
  772. _asm adc edx,-1 // the error term starts out negative
  773. // See if the error term turned over even once while skipping
  774. //if (*pErrorTerm >= 0) {
  775. _asm js short NoErrorTurnover
  776. // # of times we'll turn over the error term and step an extra x
  777. // coordinate while skipping
  778. // NumAdjustDowns = (*pErrorTerm / iErrorAdjustDown) + 1;
  779. _asm div iErrorAdjustDown
  780. _asm inc eax
  781. // Note that EDX is the remainder; (EDX - iErrorAdjustDown) is where
  782. // the error term ends up ultimately
  783. // Advance x appropriately for the # of times the error term
  784. // turned over
  785. // if (iXDirection == 1) {
  786. // *pXStart += NumAdjustDowns;
  787. // } else {
  788. // *pXStart -= NumAdjustDowns;
  789. // }
  790. _asm mov ecx,pXStart
  791. _asm cmp iXDirection,1
  792. _asm jz short GoingRight
  793. _asm neg eax
  794. GoingRight:
  795. _asm add [ecx],eax
  796. // Adjust the error term down to its proper post-skip value
  797. // *pErrorTerm -= iErrorAdjustDown * NumAdjustDowns;
  798. _asm sub edx,iErrorAdjustDown
  799. _asm mov eax,edx // put into EAX for storing to pErrorTerm next
  800. // }
  801. NoErrorTurnover:
  802. _asm mov [ebx],eax
  803. #else
  804. LONGLONG llErrorTerm;
  805. INT NumAdjustDowns;
  806. llErrorTerm = *pErrorTerm;
  807. // Adjust the error term up by the number of y coordinates we'll skip
  808. llErrorTerm += Int32x32To64(iErrorAdjustUp,yJump);
  809. // See if the error term turned over even once while skipping
  810. if (llErrorTerm >= 0) {
  811. // # of times we'll turn over the error term and step an extra x
  812. // coordinate while skipping
  813. NumAdjustDowns = (UInt64Div32To32(llErrorTerm,iErrorAdjustDown)) + 1;
  814. // Advance x appropriately for the # of times the error term
  815. // turned over
  816. if (iXDirection == 1) {
  817. *pXStart += NumAdjustDowns;
  818. } else {
  819. *pXStart -= NumAdjustDowns;
  820. }
  821. // Adjust the error term down to its proper post-skip value
  822. llErrorTerm -= iErrorAdjustDown * NumAdjustDowns;
  823. }
  824. *pErrorTerm = (INT) llErrorTerm;
  825. #endif
  826. }