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.

997 lines
35 KiB

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