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.

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