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.

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