Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1018 lines
36 KiB

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