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

1002 lines
36 KiB

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