PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mugshot-dependencies/trunk/cairo/pixman/src/pixregion.c

https://gitlab.com/manoj-makkuboy/magnetism
C | 1656 lines | 1044 code | 150 blank | 462 comment | 316 complexity | 032338eb39724d224d7ff1a76036d77b MD5 | raw file
  1. /***********************************************************
  2. Copyright 1987, 1988, 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987, 1988, 1989 by
  20. Digital Equipment Corporation, Maynard, Massachusetts.
  21. All Rights Reserved
  22. Permission to use, copy, modify, and distribute this software and its
  23. documentation for any purpose and without fee is hereby granted,
  24. provided that the above copyright notice appear in all copies and that
  25. both that copyright notice and this permission notice appear in
  26. supporting documentation, and that the name of Digital not be
  27. used in advertising or publicity pertaining to distribution of the
  28. software without specific, written prior permission.
  29. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  30. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  31. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  32. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  33. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  34. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  35. SOFTWARE.
  36. ******************************************************************/
  37. #include <stdlib.h>
  38. #include <limits.h>
  39. #include <string.h>
  40. #include "pixregionint.h"
  41. #if defined (__GNUC__) && !defined (NO_INLINES)
  42. #define INLINE __inline
  43. #else
  44. #define INLINE
  45. #endif
  46. #undef assert
  47. #ifdef DEBUG_PIXREGION
  48. #define assert(expr) {if (!(expr)) \
  49. FatalError("Assertion failed file %s, line %d: expr\n", \
  50. __FILE__, __LINE__); }
  51. #else
  52. #define assert(expr)
  53. #endif
  54. #define good(reg) assert(pixman_region16_valid(reg))
  55. #undef MIN
  56. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  57. #undef MAX
  58. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  59. static pixman_box16_t pixman_region_emptyBox = {0, 0, 0, 0};
  60. static pixman_region16_data_t pixman_region_emptyData = {0, 0};
  61. static pixman_region16_data_t pixman_brokendata = {0, 0};
  62. static pixman_region16_t pixman_brokenregion = { { 0, 0, 0, 0 }, &pixman_brokendata };
  63. static pixman_region_status_t
  64. pixman_break (pixman_region16_t *pReg);
  65. static void
  66. pixman_init (pixman_region16_t *region, pixman_box16_t *rect);
  67. static void
  68. pixman_uninit (pixman_region16_t *region);
  69. /*
  70. * The functions in this file implement the Region abstraction used extensively
  71. * throughout the X11 sample server. A Region is simply a set of disjoint
  72. * (non-overlapping) rectangles, plus an "extent" rectangle which is the
  73. * smallest single rectangle that contains all the non-overlapping rectangles.
  74. *
  75. * A Region is implemented as a "y-x-banded" array of rectangles. This array
  76. * imposes two degrees of order. First, all rectangles are sorted by top side
  77. * y coordinate first (y1), and then by left side x coordinate (x1).
  78. *
  79. * Furthermore, the rectangles are grouped into "bands". Each rectangle in a
  80. * band has the same top y coordinate (y1), and each has the same bottom y
  81. * coordinate (y2). Thus all rectangles in a band differ only in their left
  82. * and right side (x1 and x2). Bands are implicit in the array of rectangles:
  83. * there is no separate list of band start pointers.
  84. *
  85. * The y-x band representation does not minimize rectangles. In particular,
  86. * if a rectangle vertically crosses a band (the rectangle has scanlines in
  87. * the y1 to y2 area spanned by the band), then the rectangle may be broken
  88. * down into two or more smaller rectangles stacked one atop the other.
  89. *
  90. * ----------- -----------
  91. * | | | | band 0
  92. * | | -------- ----------- --------
  93. * | | | | in y-x banded | | | | band 1
  94. * | | | | form is | | | |
  95. * ----------- | | ----------- --------
  96. * | | | | band 2
  97. * -------- --------
  98. *
  99. * An added constraint on the rectangles is that they must cover as much
  100. * horizontal area as possible: no two rectangles within a band are allowed
  101. * to touch.
  102. *
  103. * Whenever possible, bands will be merged together to cover a greater vertical
  104. * distance (and thus reduce the number of rectangles). Two bands can be merged
  105. * only if the bottom of one touches the top of the other and they have
  106. * rectangles in the same places (of the same width, of course).
  107. *
  108. * Adam de Boor wrote most of the original region code. Joel McCormack
  109. * substantially modified or rewrote most of the core arithmetic routines, and
  110. * added pixman_region_validate in order to support several speed improvements to
  111. * pixman_region_validateTree. Bob Scheifler changed the representation to be more
  112. * compact when empty or a single rectangle, and did a bunch of gratuitous
  113. * reformatting. Carl Worth did further gratuitous reformatting while re-merging
  114. * the server and client region code into libpixregion.
  115. */
  116. /* true iff two Boxes overlap */
  117. #define EXTENTCHECK(r1,r2) \
  118. (!( ((r1)->x2 <= (r2)->x1) || \
  119. ((r1)->x1 >= (r2)->x2) || \
  120. ((r1)->y2 <= (r2)->y1) || \
  121. ((r1)->y1 >= (r2)->y2) ) )
  122. /* true iff (x,y) is in Box */
  123. #define INBOX(r,x,y) \
  124. ( ((r)->x2 > x) && \
  125. ((r)->x1 <= x) && \
  126. ((r)->y2 > y) && \
  127. ((r)->y1 <= y) )
  128. /* true iff Box r1 contains Box r2 */
  129. #define SUBSUMES(r1,r2) \
  130. ( ((r1)->x1 <= (r2)->x1) && \
  131. ((r1)->x2 >= (r2)->x2) && \
  132. ((r1)->y1 <= (r2)->y1) && \
  133. ((r1)->y2 >= (r2)->y2) )
  134. #define allocData(n) malloc(PIXREGION_SZOF(n))
  135. #define freeData(reg) if ((reg)->data && (reg)->data->size) free((reg)->data)
  136. #define RECTALLOC_BAIL(pReg,n,bail) \
  137. if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
  138. if (!pixman_rect_alloc(pReg, n)) { goto bail; }
  139. #define RECTALLOC(pReg,n) \
  140. if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
  141. if (!pixman_rect_alloc(pReg, n)) { return PIXMAN_REGION_STATUS_FAILURE; }
  142. #define ADDRECT(pNextRect,nx1,ny1,nx2,ny2) \
  143. { \
  144. pNextRect->x1 = nx1; \
  145. pNextRect->y1 = ny1; \
  146. pNextRect->x2 = nx2; \
  147. pNextRect->y2 = ny2; \
  148. pNextRect++; \
  149. }
  150. #define NEWRECT(pReg,pNextRect,nx1,ny1,nx2,ny2) \
  151. { \
  152. if (!(pReg)->data || ((pReg)->data->numRects == (pReg)->data->size))\
  153. { \
  154. if (!pixman_rect_alloc(pReg, 1)) \
  155. return PIXMAN_REGION_STATUS_FAILURE; \
  156. pNextRect = PIXREGION_TOP(pReg); \
  157. } \
  158. ADDRECT(pNextRect,nx1,ny1,nx2,ny2); \
  159. pReg->data->numRects++; \
  160. assert(pReg->data->numRects<=pReg->data->size); \
  161. }
  162. #define DOWNSIZE(reg,numRects) \
  163. if (((numRects) < ((reg)->data->size >> 1)) && ((reg)->data->size > 50)) \
  164. { \
  165. pixman_region16_data_t * NewData; \
  166. NewData = (pixman_region16_data_t *)realloc((reg)->data, PIXREGION_SZOF(numRects)); \
  167. if (NewData) \
  168. { \
  169. NewData->size = (numRects); \
  170. (reg)->data = NewData; \
  171. } \
  172. }
  173. #ifdef DEBUG_PIXREGION
  174. int
  175. pixman_region16_print(rgn)
  176. pixman_region16_t * rgn;
  177. {
  178. int num, size;
  179. int i;
  180. pixman_box16_t * rects;
  181. num = PIXREGION_NUM_RECTS(rgn);
  182. size = PIXREGION_SIZE(rgn);
  183. rects = PIXREGION_RECTS(rgn);
  184. ErrorF("num: %d size: %d\n", num, size);
  185. ErrorF("extents: %d %d %d %d\n",
  186. rgn->extents.x1, rgn->extents.y1, rgn->extents.x2, rgn->extents.y2);
  187. for (i = 0; i < num; i++)
  188. ErrorF("%d %d %d %d \n",
  189. rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2);
  190. ErrorF("\n");
  191. return(num);
  192. }
  193. pixman_region_status_t
  194. pixman_region16_tsEqual(reg1, reg2)
  195. pixman_region16_t * reg1;
  196. pixman_region16_t * reg2;
  197. {
  198. int i;
  199. pixman_box16_t * rects1, rects2;
  200. if (reg1->extents.x1 != reg2->extents.x1) return PIXMAN_REGION_STATUS_FAILURE;
  201. if (reg1->extents.x2 != reg2->extents.x2) return PIXMAN_REGION_STATUS_FAILURE;
  202. if (reg1->extents.y1 != reg2->extents.y1) return PIXMAN_REGION_STATUS_FAILURE;
  203. if (reg1->extents.y2 != reg2->extents.y2) return PIXMAN_REGION_STATUS_FAILURE;
  204. if (PIXREGION_NUM_RECTS(reg1) != PIXREGION_NUM_RECTS(reg2)) return PIXMAN_REGION_STATUS_FAILURE;
  205. rects1 = PIXREGION_RECTS(reg1);
  206. rects2 = PIXREGION_RECTS(reg2);
  207. for (i = 0; i != PIXREGION_NUM_RECTS(reg1); i++) {
  208. if (rects1[i].x1 != rects2[i].x1) return PIXMAN_REGION_STATUS_FAILURE;
  209. if (rects1[i].x2 != rects2[i].x2) return PIXMAN_REGION_STATUS_FAILURE;
  210. if (rects1[i].y1 != rects2[i].y1) return PIXMAN_REGION_STATUS_FAILURE;
  211. if (rects1[i].y2 != rects2[i].y2) return PIXMAN_REGION_STATUS_FAILURE;
  212. }
  213. return PIXMAN_REGION_STATUS_SUCCESS;
  214. }
  215. pixman_region_status_t
  216. pixman_region16_valid(reg)
  217. pixman_region16_t * reg;
  218. {
  219. int i, numRects;
  220. if ((reg->extents.x1 > reg->extents.x2) ||
  221. (reg->extents.y1 > reg->extents.y2))
  222. return PIXMAN_REGION_STATUS_FAILURE;
  223. numRects = PIXREGION_NUM_RECTS(reg);
  224. if (!numRects)
  225. return ((reg->extents.x1 == reg->extents.x2) &&
  226. (reg->extents.y1 == reg->extents.y2) &&
  227. (reg->data->size || (reg->data == &pixman_region_emptyData)));
  228. else if (numRects == 1)
  229. return (!reg->data);
  230. else
  231. {
  232. pixman_box16_t * pboxP, pboxN;
  233. pixman_box16_t box;
  234. pboxP = PIXREGION_RECTS(reg);
  235. box = *pboxP;
  236. box.y2 = pboxP[numRects-1].y2;
  237. pboxN = pboxP + 1;
  238. for (i = numRects; --i > 0; pboxP++, pboxN++)
  239. {
  240. if ((pboxN->x1 >= pboxN->x2) ||
  241. (pboxN->y1 >= pboxN->y2))
  242. return PIXMAN_REGION_STATUS_FAILURE;
  243. if (pboxN->x1 < box.x1)
  244. box.x1 = pboxN->x1;
  245. if (pboxN->x2 > box.x2)
  246. box.x2 = pboxN->x2;
  247. if ((pboxN->y1 < pboxP->y1) ||
  248. ((pboxN->y1 == pboxP->y1) &&
  249. ((pboxN->x1 < pboxP->x2) || (pboxN->y2 != pboxP->y2))))
  250. return PIXMAN_REGION_STATUS_FAILURE;
  251. }
  252. return ((box.x1 == reg->extents.x1) &&
  253. (box.x2 == reg->extents.x2) &&
  254. (box.y1 == reg->extents.y1) &&
  255. (box.y2 == reg->extents.y2));
  256. }
  257. }
  258. #endif /* DEBUG_PIXREGION */
  259. /* Create a new empty region */
  260. pixman_region16_t *
  261. pixman_region_create (void)
  262. {
  263. return pixman_region_create_simple (NULL);
  264. }
  265. /*****************************************************************
  266. * pixman_region_create_simple (extents)
  267. * This routine creates a pixman_region16_t for a simple
  268. * rectangular region.
  269. *****************************************************************/
  270. pixman_region16_t *
  271. pixman_region_create_simple (pixman_box16_t *extents)
  272. {
  273. pixman_region16_t *region;
  274. region = malloc (sizeof (pixman_region16_t));
  275. if (region == NULL)
  276. return &pixman_brokenregion;
  277. pixman_init (region, extents);
  278. return region;
  279. }
  280. /*****************************************************************
  281. * RegionInit(pReg, rect, size)
  282. * Outer region rect is statically allocated.
  283. *****************************************************************/
  284. static void
  285. pixman_init(pixman_region16_t *region, pixman_box16_t *extents)
  286. {
  287. if (extents)
  288. {
  289. region->extents = *extents;
  290. region->data = NULL;
  291. }
  292. else
  293. {
  294. region->extents = pixman_region_emptyBox;
  295. region->data = &pixman_region_emptyData;
  296. }
  297. }
  298. static void
  299. pixman_uninit (pixman_region16_t *region)
  300. {
  301. good (region);
  302. freeData (region);
  303. }
  304. void
  305. pixman_region_destroy (pixman_region16_t *region)
  306. {
  307. pixman_uninit (region);
  308. if (region != &pixman_brokenregion)
  309. free (region);
  310. }
  311. int
  312. pixman_region_num_rects (pixman_region16_t *region)
  313. {
  314. return PIXREGION_NUM_RECTS (region);
  315. }
  316. pixman_box16_t *
  317. pixman_region_rects (pixman_region16_t *region)
  318. {
  319. return PIXREGION_RECTS (region);
  320. }
  321. static pixman_region_status_t
  322. pixman_break (pixman_region16_t *region)
  323. {
  324. freeData (region);
  325. region->extents = pixman_region_emptyBox;
  326. region->data = &pixman_brokendata;
  327. return PIXMAN_REGION_STATUS_FAILURE;
  328. }
  329. static pixman_region_status_t
  330. pixman_rect_alloc(pixman_region16_t * region, int n)
  331. {
  332. pixman_region16_data_t *data;
  333. if (!region->data)
  334. {
  335. n++;
  336. region->data = allocData(n);
  337. if (!region->data)
  338. return pixman_break (region);
  339. region->data->numRects = 1;
  340. *PIXREGION_BOXPTR(region) = region->extents;
  341. }
  342. else if (!region->data->size)
  343. {
  344. region->data = allocData(n);
  345. if (!region->data)
  346. return pixman_break (region);
  347. region->data->numRects = 0;
  348. }
  349. else
  350. {
  351. if (n == 1)
  352. {
  353. n = region->data->numRects;
  354. if (n > 500) /* XXX pick numbers out of a hat */
  355. n = 250;
  356. }
  357. n += region->data->numRects;
  358. data = (pixman_region16_data_t *)realloc(region->data, PIXREGION_SZOF(n));
  359. if (!data)
  360. return pixman_break (region);
  361. region->data = data;
  362. }
  363. region->data->size = n;
  364. return PIXMAN_REGION_STATUS_SUCCESS;
  365. }
  366. pixman_region_status_t
  367. pixman_region_copy(pixman_region16_t *dst, pixman_region16_t *src)
  368. {
  369. good(dst);
  370. good(src);
  371. if (dst == src)
  372. return PIXMAN_REGION_STATUS_SUCCESS;
  373. dst->extents = src->extents;
  374. if (!src->data || !src->data->size)
  375. {
  376. freeData(dst);
  377. dst->data = src->data;
  378. return PIXMAN_REGION_STATUS_SUCCESS;
  379. }
  380. if (!dst->data || (dst->data->size < src->data->numRects))
  381. {
  382. freeData(dst);
  383. dst->data = allocData(src->data->numRects);
  384. if (!dst->data)
  385. return pixman_break (dst);
  386. dst->data->size = src->data->numRects;
  387. }
  388. dst->data->numRects = src->data->numRects;
  389. memmove((char *)PIXREGION_BOXPTR(dst),(char *)PIXREGION_BOXPTR(src),
  390. dst->data->numRects * sizeof(pixman_box16_t));
  391. return PIXMAN_REGION_STATUS_SUCCESS;
  392. }
  393. /*======================================================================
  394. * Generic Region Operator
  395. *====================================================================*/
  396. /*-
  397. *-----------------------------------------------------------------------
  398. * pixman_coalesce --
  399. * Attempt to merge the boxes in the current band with those in the
  400. * previous one. We are guaranteed that the current band extends to
  401. * the end of the rects array. Used only by pixman_op.
  402. *
  403. * Results:
  404. * The new index for the previous band.
  405. *
  406. * Side Effects:
  407. * If coalescing takes place:
  408. * - rectangles in the previous band will have their y2 fields
  409. * altered.
  410. * - region->data->numRects will be decreased.
  411. *
  412. *-----------------------------------------------------------------------
  413. */
  414. INLINE static int
  415. pixman_coalesce (
  416. pixman_region16_t * region, /* Region to coalesce */
  417. int prevStart, /* Index of start of previous band */
  418. int curStart) /* Index of start of current band */
  419. {
  420. pixman_box16_t * pPrevBox; /* Current box in previous band */
  421. pixman_box16_t * pCurBox; /* Current box in current band */
  422. int numRects; /* Number rectangles in both bands */
  423. int y2; /* Bottom of current band */
  424. /*
  425. * Figure out how many rectangles are in the band.
  426. */
  427. numRects = curStart - prevStart;
  428. assert(numRects == region->data->numRects - curStart);
  429. if (!numRects) return curStart;
  430. /*
  431. * The bands may only be coalesced if the bottom of the previous
  432. * matches the top scanline of the current.
  433. */
  434. pPrevBox = PIXREGION_BOX(region, prevStart);
  435. pCurBox = PIXREGION_BOX(region, curStart);
  436. if (pPrevBox->y2 != pCurBox->y1) return curStart;
  437. /*
  438. * Make sure the bands have boxes in the same places. This
  439. * assumes that boxes have been added in such a way that they
  440. * cover the most area possible. I.e. two boxes in a band must
  441. * have some horizontal space between them.
  442. */
  443. y2 = pCurBox->y2;
  444. do {
  445. if ((pPrevBox->x1 != pCurBox->x1) || (pPrevBox->x2 != pCurBox->x2)) {
  446. return (curStart);
  447. }
  448. pPrevBox++;
  449. pCurBox++;
  450. numRects--;
  451. } while (numRects);
  452. /*
  453. * The bands may be merged, so set the bottom y of each box
  454. * in the previous band to the bottom y of the current band.
  455. */
  456. numRects = curStart - prevStart;
  457. region->data->numRects -= numRects;
  458. do {
  459. pPrevBox--;
  460. pPrevBox->y2 = y2;
  461. numRects--;
  462. } while (numRects);
  463. return prevStart;
  464. }
  465. /* Quicky macro to avoid trivial reject procedure calls to pixman_coalesce */
  466. #define Coalesce(newReg, prevBand, curBand) \
  467. if (curBand - prevBand == newReg->data->numRects - curBand) { \
  468. prevBand = pixman_coalesce(newReg, prevBand, curBand); \
  469. } else { \
  470. prevBand = curBand; \
  471. }
  472. /*-
  473. *-----------------------------------------------------------------------
  474. * pixman_region_appendNonO --
  475. * Handle a non-overlapping band for the union and subtract operations.
  476. * Just adds the (top/bottom-clipped) rectangles into the region.
  477. * Doesn't have to check for subsumption or anything.
  478. *
  479. * Results:
  480. * None.
  481. *
  482. * Side Effects:
  483. * region->data->numRects is incremented and the rectangles overwritten
  484. * with the rectangles we're passed.
  485. *
  486. *-----------------------------------------------------------------------
  487. */
  488. INLINE static pixman_region_status_t
  489. pixman_region_appendNonO (
  490. pixman_region16_t * region,
  491. pixman_box16_t * r,
  492. pixman_box16_t * rEnd,
  493. int y1,
  494. int y2)
  495. {
  496. pixman_box16_t * pNextRect;
  497. int newRects;
  498. newRects = rEnd - r;
  499. assert(y1 < y2);
  500. assert(newRects != 0);
  501. /* Make sure we have enough space for all rectangles to be added */
  502. RECTALLOC(region, newRects);
  503. pNextRect = PIXREGION_TOP(region);
  504. region->data->numRects += newRects;
  505. do {
  506. assert(r->x1 < r->x2);
  507. ADDRECT(pNextRect, r->x1, y1, r->x2, y2);
  508. r++;
  509. } while (r != rEnd);
  510. return PIXMAN_REGION_STATUS_SUCCESS;
  511. }
  512. #define FindBand(r, rBandEnd, rEnd, ry1) \
  513. { \
  514. ry1 = r->y1; \
  515. rBandEnd = r+1; \
  516. while ((rBandEnd != rEnd) && (rBandEnd->y1 == ry1)) { \
  517. rBandEnd++; \
  518. } \
  519. }
  520. #define AppendRegions(newReg, r, rEnd) \
  521. { \
  522. int newRects; \
  523. if ((newRects = rEnd - r)) { \
  524. RECTALLOC(newReg, newRects); \
  525. memmove((char *)PIXREGION_TOP(newReg),(char *)r, \
  526. newRects * sizeof(pixman_box16_t)); \
  527. newReg->data->numRects += newRects; \
  528. } \
  529. }
  530. /*-
  531. *-----------------------------------------------------------------------
  532. * pixman_op --
  533. * Apply an operation to two regions. Called by pixman_region_union, pixman_region_inverse,
  534. * pixman_region_subtract, pixman_region_intersect.... Both regions MUST have at least one
  535. * rectangle, and cannot be the same object.
  536. *
  537. * Results:
  538. * PIXMAN_REGION_STATUS_SUCCESS if successful.
  539. *
  540. * Side Effects:
  541. * The new region is overwritten.
  542. * pOverlap set to PIXMAN_REGION_STATUS_SUCCESS if overlapFunc ever returns PIXMAN_REGION_STATUS_SUCCESS.
  543. *
  544. * Notes:
  545. * The idea behind this function is to view the two regions as sets.
  546. * Together they cover a rectangle of area that this function divides
  547. * into horizontal bands where points are covered only by one region
  548. * or by both. For the first case, the nonOverlapFunc is called with
  549. * each the band and the band's upper and lower extents. For the
  550. * second, the overlapFunc is called to process the entire band. It
  551. * is responsible for clipping the rectangles in the band, though
  552. * this function provides the boundaries.
  553. * At the end of each band, the new region is coalesced, if possible,
  554. * to reduce the number of rectangles in the region.
  555. *
  556. *-----------------------------------------------------------------------
  557. */
  558. typedef pixman_region_status_t (*OverlapProcPtr)(
  559. pixman_region16_t *region,
  560. pixman_box16_t *r1,
  561. pixman_box16_t *r1End,
  562. pixman_box16_t *r2,
  563. pixman_box16_t *r2End,
  564. short y1,
  565. short y2,
  566. int *pOverlap);
  567. static pixman_region_status_t
  568. pixman_op(
  569. pixman_region16_t * newReg, /* Place to store result */
  570. pixman_region16_t * reg1, /* First region in operation */
  571. pixman_region16_t * reg2, /* 2d region in operation */
  572. OverlapProcPtr overlapFunc, /* Function to call for over-
  573. * lapping bands */
  574. int appendNon1, /* Append non-overlapping bands */
  575. /* in region 1 ? */
  576. int appendNon2, /* Append non-overlapping bands */
  577. /* in region 2 ? */
  578. int *pOverlap)
  579. {
  580. pixman_box16_t * r1; /* Pointer into first region */
  581. pixman_box16_t * r2; /* Pointer into 2d region */
  582. pixman_box16_t * r1End; /* End of 1st region */
  583. pixman_box16_t * r2End; /* End of 2d region */
  584. short ybot; /* Bottom of intersection */
  585. short ytop; /* Top of intersection */
  586. pixman_region16_data_t * oldData; /* Old data for newReg */
  587. int prevBand; /* Index of start of
  588. * previous band in newReg */
  589. int curBand; /* Index of start of current
  590. * band in newReg */
  591. pixman_box16_t * r1BandEnd; /* End of current band in r1 */
  592. pixman_box16_t * r2BandEnd; /* End of current band in r2 */
  593. short top; /* Top of non-overlapping band */
  594. short bot; /* Bottom of non-overlapping band*/
  595. int r1y1; /* Temps for r1->y1 and r2->y1 */
  596. int r2y1;
  597. int newSize;
  598. int numRects;
  599. /*
  600. * Break any region computed from a broken region
  601. */
  602. if (PIXREGION_NAR (reg1) || PIXREGION_NAR(reg2))
  603. return pixman_break (newReg);
  604. /*
  605. * Initialization:
  606. * set r1, r2, r1End and r2End appropriately, save the rectangles
  607. * of the destination region until the end in case it's one of
  608. * the two source regions, then mark the "new" region empty, allocating
  609. * another array of rectangles for it to use.
  610. */
  611. r1 = PIXREGION_RECTS(reg1);
  612. newSize = PIXREGION_NUM_RECTS(reg1);
  613. r1End = r1 + newSize;
  614. numRects = PIXREGION_NUM_RECTS(reg2);
  615. r2 = PIXREGION_RECTS(reg2);
  616. r2End = r2 + numRects;
  617. assert(r1 != r1End);
  618. assert(r2 != r2End);
  619. oldData = (pixman_region16_data_t *)NULL;
  620. if (((newReg == reg1) && (newSize > 1)) ||
  621. ((newReg == reg2) && (numRects > 1)))
  622. {
  623. oldData = newReg->data;
  624. newReg->data = &pixman_region_emptyData;
  625. }
  626. /* guess at new size */
  627. if (numRects > newSize)
  628. newSize = numRects;
  629. newSize <<= 1;
  630. if (!newReg->data)
  631. newReg->data = &pixman_region_emptyData;
  632. else if (newReg->data->size)
  633. newReg->data->numRects = 0;
  634. if (newSize > newReg->data->size)
  635. if (!pixman_rect_alloc(newReg, newSize))
  636. return PIXMAN_REGION_STATUS_FAILURE;
  637. /*
  638. * Initialize ybot.
  639. * In the upcoming loop, ybot and ytop serve different functions depending
  640. * on whether the band being handled is an overlapping or non-overlapping
  641. * band.
  642. * In the case of a non-overlapping band (only one of the regions
  643. * has points in the band), ybot is the bottom of the most recent
  644. * intersection and thus clips the top of the rectangles in that band.
  645. * ytop is the top of the next intersection between the two regions and
  646. * serves to clip the bottom of the rectangles in the current band.
  647. * For an overlapping band (where the two regions intersect), ytop clips
  648. * the top of the rectangles of both regions and ybot clips the bottoms.
  649. */
  650. ybot = MIN(r1->y1, r2->y1);
  651. /*
  652. * prevBand serves to mark the start of the previous band so rectangles
  653. * can be coalesced into larger rectangles. qv. pixman_coalesce, above.
  654. * In the beginning, there is no previous band, so prevBand == curBand
  655. * (curBand is set later on, of course, but the first band will always
  656. * start at index 0). prevBand and curBand must be indices because of
  657. * the possible expansion, and resultant moving, of the new region's
  658. * array of rectangles.
  659. */
  660. prevBand = 0;
  661. do {
  662. /*
  663. * This algorithm proceeds one source-band (as opposed to a
  664. * destination band, which is determined by where the two regions
  665. * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
  666. * rectangle after the last one in the current band for their
  667. * respective regions.
  668. */
  669. assert(r1 != r1End);
  670. assert(r2 != r2End);
  671. FindBand(r1, r1BandEnd, r1End, r1y1);
  672. FindBand(r2, r2BandEnd, r2End, r2y1);
  673. /*
  674. * First handle the band that doesn't intersect, if any.
  675. *
  676. * Note that attention is restricted to one band in the
  677. * non-intersecting region at once, so if a region has n
  678. * bands between the current position and the next place it overlaps
  679. * the other, this entire loop will be passed through n times.
  680. */
  681. if (r1y1 < r2y1) {
  682. if (appendNon1) {
  683. top = MAX(r1y1, ybot);
  684. bot = MIN(r1->y2, r2y1);
  685. if (top != bot) {
  686. curBand = newReg->data->numRects;
  687. pixman_region_appendNonO(newReg, r1, r1BandEnd, top, bot);
  688. Coalesce(newReg, prevBand, curBand);
  689. }
  690. }
  691. ytop = r2y1;
  692. } else if (r2y1 < r1y1) {
  693. if (appendNon2) {
  694. top = MAX(r2y1, ybot);
  695. bot = MIN(r2->y2, r1y1);
  696. if (top != bot) {
  697. curBand = newReg->data->numRects;
  698. pixman_region_appendNonO(newReg, r2, r2BandEnd, top, bot);
  699. Coalesce(newReg, prevBand, curBand);
  700. }
  701. }
  702. ytop = r1y1;
  703. } else {
  704. ytop = r1y1;
  705. }
  706. /*
  707. * Now see if we've hit an intersecting band. The two bands only
  708. * intersect if ybot > ytop
  709. */
  710. ybot = MIN(r1->y2, r2->y2);
  711. if (ybot > ytop) {
  712. curBand = newReg->data->numRects;
  713. (* overlapFunc)(newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot,
  714. pOverlap);
  715. Coalesce(newReg, prevBand, curBand);
  716. }
  717. /*
  718. * If we've finished with a band (y2 == ybot) we skip forward
  719. * in the region to the next band.
  720. */
  721. if (r1->y2 == ybot) r1 = r1BandEnd;
  722. if (r2->y2 == ybot) r2 = r2BandEnd;
  723. } while (r1 != r1End && r2 != r2End);
  724. /*
  725. * Deal with whichever region (if any) still has rectangles left.
  726. *
  727. * We only need to worry about banding and coalescing for the very first
  728. * band left. After that, we can just group all remaining boxes,
  729. * regardless of how many bands, into one final append to the list.
  730. */
  731. if ((r1 != r1End) && appendNon1) {
  732. /* Do first nonOverlap1Func call, which may be able to coalesce */
  733. FindBand(r1, r1BandEnd, r1End, r1y1);
  734. curBand = newReg->data->numRects;
  735. pixman_region_appendNonO(newReg, r1, r1BandEnd, MAX(r1y1, ybot), r1->y2);
  736. Coalesce(newReg, prevBand, curBand);
  737. /* Just append the rest of the boxes */
  738. AppendRegions(newReg, r1BandEnd, r1End);
  739. } else if ((r2 != r2End) && appendNon2) {
  740. /* Do first nonOverlap2Func call, which may be able to coalesce */
  741. FindBand(r2, r2BandEnd, r2End, r2y1);
  742. curBand = newReg->data->numRects;
  743. pixman_region_appendNonO(newReg, r2, r2BandEnd, MAX(r2y1, ybot), r2->y2);
  744. Coalesce(newReg, prevBand, curBand);
  745. /* Append rest of boxes */
  746. AppendRegions(newReg, r2BandEnd, r2End);
  747. }
  748. if (oldData)
  749. free(oldData);
  750. if (!(numRects = newReg->data->numRects))
  751. {
  752. freeData(newReg);
  753. newReg->data = &pixman_region_emptyData;
  754. }
  755. else if (numRects == 1)
  756. {
  757. newReg->extents = *PIXREGION_BOXPTR(newReg);
  758. freeData(newReg);
  759. newReg->data = (pixman_region16_data_t *)NULL;
  760. }
  761. else
  762. {
  763. DOWNSIZE(newReg, numRects);
  764. }
  765. return PIXMAN_REGION_STATUS_SUCCESS;
  766. }
  767. /*-
  768. *-----------------------------------------------------------------------
  769. * pixman_set_extents --
  770. * Reset the extents of a region to what they should be. Called by
  771. * pixman_region_subtract and pixman_region_intersect as they can't figure it out along the
  772. * way or do so easily, as pixman_region_union can.
  773. *
  774. * Results:
  775. * None.
  776. *
  777. * Side Effects:
  778. * The region's 'extents' structure is overwritten.
  779. *
  780. *-----------------------------------------------------------------------
  781. */
  782. static void
  783. pixman_set_extents (pixman_region16_t *region)
  784. {
  785. pixman_box16_t *box, *boxEnd;
  786. if (!region->data)
  787. return;
  788. if (!region->data->size)
  789. {
  790. region->extents.x2 = region->extents.x1;
  791. region->extents.y2 = region->extents.y1;
  792. return;
  793. }
  794. box = PIXREGION_BOXPTR(region);
  795. boxEnd = PIXREGION_END(region);
  796. /*
  797. * Since box is the first rectangle in the region, it must have the
  798. * smallest y1 and since boxEnd is the last rectangle in the region,
  799. * it must have the largest y2, because of banding. Initialize x1 and
  800. * x2 from box and boxEnd, resp., as good things to initialize them
  801. * to...
  802. */
  803. region->extents.x1 = box->x1;
  804. region->extents.y1 = box->y1;
  805. region->extents.x2 = boxEnd->x2;
  806. region->extents.y2 = boxEnd->y2;
  807. assert(region->extents.y1 < region->extents.y2);
  808. while (box <= boxEnd) {
  809. if (box->x1 < region->extents.x1)
  810. region->extents.x1 = box->x1;
  811. if (box->x2 > region->extents.x2)
  812. region->extents.x2 = box->x2;
  813. box++;
  814. };
  815. assert(region->extents.x1 < region->extents.x2);
  816. }
  817. /*======================================================================
  818. * Region Intersection
  819. *====================================================================*/
  820. /*-
  821. *-----------------------------------------------------------------------
  822. * pixman_region_intersectO --
  823. * Handle an overlapping band for pixman_region_intersect.
  824. *
  825. * Results:
  826. * PIXMAN_REGION_STATUS_SUCCESS if successful.
  827. *
  828. * Side Effects:
  829. * Rectangles may be added to the region.
  830. *
  831. *-----------------------------------------------------------------------
  832. */
  833. /*ARGSUSED*/
  834. static pixman_region_status_t
  835. pixman_region_intersectO (
  836. pixman_region16_t * region,
  837. pixman_box16_t * r1,
  838. pixman_box16_t * r1End,
  839. pixman_box16_t * r2,
  840. pixman_box16_t * r2End,
  841. short y1,
  842. short y2,
  843. int *pOverlap)
  844. {
  845. int x1;
  846. int x2;
  847. pixman_box16_t * pNextRect;
  848. pNextRect = PIXREGION_TOP(region);
  849. assert(y1 < y2);
  850. assert(r1 != r1End && r2 != r2End);
  851. do {
  852. x1 = MAX(r1->x1, r2->x1);
  853. x2 = MIN(r1->x2, r2->x2);
  854. /*
  855. * If there's any overlap between the two rectangles, add that
  856. * overlap to the new region.
  857. */
  858. if (x1 < x2)
  859. NEWRECT(region, pNextRect, x1, y1, x2, y2);
  860. /*
  861. * Advance the pointer(s) with the leftmost right side, since the next
  862. * rectangle on that list may still overlap the other region's
  863. * current rectangle.
  864. */
  865. if (r1->x2 == x2) {
  866. r1++;
  867. }
  868. if (r2->x2 == x2) {
  869. r2++;
  870. }
  871. } while ((r1 != r1End) && (r2 != r2End));
  872. return PIXMAN_REGION_STATUS_SUCCESS;
  873. }
  874. pixman_region_status_t
  875. pixman_region_intersect(pixman_region16_t * newReg,
  876. pixman_region16_t * reg1,
  877. pixman_region16_t * reg2)
  878. {
  879. good(reg1);
  880. good(reg2);
  881. good(newReg);
  882. /* check for trivial reject */
  883. if (PIXREGION_NIL(reg1) || PIXREGION_NIL(reg2) ||
  884. !EXTENTCHECK(&reg1->extents, &reg2->extents))
  885. {
  886. /* Covers about 20% of all cases */
  887. freeData(newReg);
  888. newReg->extents.x2 = newReg->extents.x1;
  889. newReg->extents.y2 = newReg->extents.y1;
  890. if (PIXREGION_NAR(reg1) || PIXREGION_NAR(reg2))
  891. {
  892. newReg->data = &pixman_brokendata;
  893. return PIXMAN_REGION_STATUS_FAILURE;
  894. }
  895. else
  896. newReg->data = &pixman_region_emptyData;
  897. }
  898. else if (!reg1->data && !reg2->data)
  899. {
  900. /* Covers about 80% of cases that aren't trivially rejected */
  901. newReg->extents.x1 = MAX(reg1->extents.x1, reg2->extents.x1);
  902. newReg->extents.y1 = MAX(reg1->extents.y1, reg2->extents.y1);
  903. newReg->extents.x2 = MIN(reg1->extents.x2, reg2->extents.x2);
  904. newReg->extents.y2 = MIN(reg1->extents.y2, reg2->extents.y2);
  905. freeData(newReg);
  906. newReg->data = (pixman_region16_data_t *)NULL;
  907. }
  908. else if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
  909. {
  910. return pixman_region_copy(newReg, reg1);
  911. }
  912. else if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
  913. {
  914. return pixman_region_copy(newReg, reg2);
  915. }
  916. else if (reg1 == reg2)
  917. {
  918. return pixman_region_copy(newReg, reg1);
  919. }
  920. else
  921. {
  922. /* General purpose intersection */
  923. int overlap; /* result ignored */
  924. if (!pixman_op(newReg, reg1, reg2, pixman_region_intersectO, PIXMAN_REGION_STATUS_FAILURE, PIXMAN_REGION_STATUS_FAILURE,
  925. &overlap))
  926. return PIXMAN_REGION_STATUS_FAILURE;
  927. pixman_set_extents(newReg);
  928. }
  929. good(newReg);
  930. return(PIXMAN_REGION_STATUS_SUCCESS);
  931. }
  932. #define MERGERECT(r) \
  933. { \
  934. if (r->x1 <= x2) { \
  935. /* Merge with current rectangle */ \
  936. if (r->x1 < x2) *pOverlap = PIXMAN_REGION_STATUS_SUCCESS; \
  937. if (x2 < r->x2) x2 = r->x2; \
  938. } else { \
  939. /* Add current rectangle, start new one */ \
  940. NEWRECT(region, pNextRect, x1, y1, x2, y2); \
  941. x1 = r->x1; \
  942. x2 = r->x2; \
  943. } \
  944. r++; \
  945. }
  946. /*======================================================================
  947. * Region Union
  948. *====================================================================*/
  949. /*-
  950. *-----------------------------------------------------------------------
  951. * pixman_region_unionO --
  952. * Handle an overlapping band for the union operation. Picks the
  953. * left-most rectangle each time and merges it into the region.
  954. *
  955. * Results:
  956. * PIXMAN_REGION_STATUS_SUCCESS if successful.
  957. *
  958. * Side Effects:
  959. * region is overwritten.
  960. * pOverlap is set to PIXMAN_REGION_STATUS_SUCCESS if any boxes overlap.
  961. *
  962. *-----------------------------------------------------------------------
  963. */
  964. static pixman_region_status_t
  965. pixman_region_unionO (
  966. pixman_region16_t *region,
  967. pixman_box16_t *r1,
  968. pixman_box16_t *r1End,
  969. pixman_box16_t *r2,
  970. pixman_box16_t *r2End,
  971. short y1,
  972. short y2,
  973. int *pOverlap)
  974. {
  975. pixman_box16_t * pNextRect;
  976. int x1; /* left and right side of current union */
  977. int x2;
  978. assert (y1 < y2);
  979. assert(r1 != r1End && r2 != r2End);
  980. pNextRect = PIXREGION_TOP(region);
  981. /* Start off current rectangle */
  982. if (r1->x1 < r2->x1)
  983. {
  984. x1 = r1->x1;
  985. x2 = r1->x2;
  986. r1++;
  987. }
  988. else
  989. {
  990. x1 = r2->x1;
  991. x2 = r2->x2;
  992. r2++;
  993. }
  994. while (r1 != r1End && r2 != r2End)
  995. {
  996. if (r1->x1 < r2->x1) MERGERECT(r1) else MERGERECT(r2);
  997. }
  998. /* Finish off whoever (if any) is left */
  999. if (r1 != r1End)
  1000. {
  1001. do
  1002. {
  1003. MERGERECT(r1);
  1004. } while (r1 != r1End);
  1005. }
  1006. else if (r2 != r2End)
  1007. {
  1008. do
  1009. {
  1010. MERGERECT(r2);
  1011. } while (r2 != r2End);
  1012. }
  1013. /* Add current rectangle */
  1014. NEWRECT(region, pNextRect, x1, y1, x2, y2);
  1015. return PIXMAN_REGION_STATUS_SUCCESS;
  1016. }
  1017. /* Convenience function for performing union of region with a single rectangle */
  1018. pixman_region_status_t
  1019. pixman_region_union_rect(pixman_region16_t *dest, pixman_region16_t *source,
  1020. int x, int y, unsigned int width, unsigned int height)
  1021. {
  1022. pixman_region16_t region;
  1023. if (!width || !height)
  1024. return pixman_region_copy (dest, source);
  1025. region.data = NULL;
  1026. region.extents.x1 = x;
  1027. region.extents.y1 = y;
  1028. region.extents.x2 = x + width;
  1029. region.extents.y2 = y + height;
  1030. return pixman_region_union (dest, source, &region);
  1031. }
  1032. pixman_region_status_t
  1033. pixman_region_union(pixman_region16_t *newReg, pixman_region16_t *reg1, pixman_region16_t *reg2)
  1034. {
  1035. int overlap; /* result ignored */
  1036. /* Return PIXMAN_REGION_STATUS_SUCCESS if some overlap between reg1, reg2 */
  1037. good(reg1);
  1038. good(reg2);
  1039. good(newReg);
  1040. /* checks all the simple cases */
  1041. /*
  1042. * Region 1 and 2 are the same
  1043. */
  1044. if (reg1 == reg2)
  1045. {
  1046. return pixman_region_copy(newReg, reg1);
  1047. }
  1048. /*
  1049. * Region 1 is empty
  1050. */
  1051. if (PIXREGION_NIL(reg1))
  1052. {
  1053. if (PIXREGION_NAR(reg1))
  1054. return pixman_break (newReg);
  1055. if (newReg != reg2)
  1056. return pixman_region_copy(newReg, reg2);
  1057. return PIXMAN_REGION_STATUS_SUCCESS;
  1058. }
  1059. /*
  1060. * Region 2 is empty
  1061. */
  1062. if (PIXREGION_NIL(reg2))
  1063. {
  1064. if (PIXREGION_NAR(reg2))
  1065. return pixman_break (newReg);
  1066. if (newReg != reg1)
  1067. return pixman_region_copy(newReg, reg1);
  1068. return PIXMAN_REGION_STATUS_SUCCESS;
  1069. }
  1070. /*
  1071. * Region 1 completely subsumes region 2
  1072. */
  1073. if (!reg1->data && SUBSUMES(&reg1->extents, &reg2->extents))
  1074. {
  1075. if (newReg != reg1)
  1076. return pixman_region_copy(newReg, reg1);
  1077. return PIXMAN_REGION_STATUS_SUCCESS;
  1078. }
  1079. /*
  1080. * Region 2 completely subsumes region 1
  1081. */
  1082. if (!reg2->data && SUBSUMES(&reg2->extents, &reg1->extents))
  1083. {
  1084. if (newReg != reg2)
  1085. return pixman_region_copy(newReg, reg2);
  1086. return PIXMAN_REGION_STATUS_SUCCESS;
  1087. }
  1088. if (!pixman_op(newReg, reg1, reg2, pixman_region_unionO, PIXMAN_REGION_STATUS_SUCCESS, PIXMAN_REGION_STATUS_SUCCESS, &overlap))
  1089. return PIXMAN_REGION_STATUS_FAILURE;
  1090. newReg->extents.x1 = MIN(reg1->extents.x1, reg2->extents.x1);
  1091. newReg->extents.y1 = MIN(reg1->extents.y1, reg2->extents.y1);
  1092. newReg->extents.x2 = MAX(reg1->extents.x2, reg2->extents.x2);
  1093. newReg->extents.y2 = MAX(reg1->extents.y2, reg2->extents.y2);
  1094. good(newReg);
  1095. return PIXMAN_REGION_STATUS_SUCCESS;
  1096. }
  1097. /*======================================================================
  1098. * Batch Rectangle Union
  1099. *====================================================================*/
  1100. /*-
  1101. *-----------------------------------------------------------------------
  1102. * pixman_region_append --
  1103. *
  1104. * "Append" the rgn rectangles onto the end of dstrgn, maintaining
  1105. * knowledge of YX-banding when it's easy. Otherwise, dstrgn just
  1106. * becomes a non-y-x-banded random collection of rectangles, and not
  1107. * yet a true region. After a sequence of appends, the caller must
  1108. * call pixman_region_validate to ensure that a valid region is constructed.
  1109. *
  1110. * Results:
  1111. * PIXMAN_REGION_STATUS_SUCCESS if successful.
  1112. *
  1113. * Side Effects:
  1114. * dstrgn is modified if rgn has rectangles.
  1115. *
  1116. */
  1117. pixman_region_status_t
  1118. pixman_region_append(pixman_region16_t * dstrgn,
  1119. pixman_region16_t * rgn)
  1120. {
  1121. int numRects, dnumRects, size;
  1122. pixman_box16_t *new, *old;
  1123. int prepend;
  1124. if (PIXREGION_NAR(rgn))
  1125. return pixman_break (dstrgn);
  1126. if (!rgn->data && (dstrgn->data == &pixman_region_emptyData))
  1127. {
  1128. dstrgn->extents = rgn->extents;
  1129. dstrgn->data = (pixman_region16_data_t *)NULL;
  1130. return PIXMAN_REGION_STATUS_SUCCESS;
  1131. }
  1132. numRects = PIXREGION_NUM_RECTS(rgn);
  1133. if (!numRects)
  1134. return PIXMAN_REGION_STATUS_SUCCESS;
  1135. prepend = PIXMAN_REGION_STATUS_FAILURE;
  1136. size = numRects;
  1137. dnumRects = PIXREGION_NUM_RECTS(dstrgn);
  1138. if (!dnumRects && (size < 200))
  1139. size = 200; /* XXX pick numbers out of a hat */
  1140. RECTALLOC(dstrgn, size);
  1141. old = PIXREGION_RECTS(rgn);
  1142. if (!dnumRects)
  1143. dstrgn->extents = rgn->extents;
  1144. else if (dstrgn->extents.x2 > dstrgn->extents.x1)
  1145. {
  1146. pixman_box16_t *first, *last;
  1147. first = old;
  1148. last = PIXREGION_BOXPTR(dstrgn) + (dnumRects - 1);
  1149. if ((first->y1 > last->y2) ||
  1150. ((first->y1 == last->y1) && (first->y2 == last->y2) &&
  1151. (first->x1 > last->x2)))
  1152. {
  1153. if (rgn->extents.x1 < dstrgn->extents.x1)
  1154. dstrgn->extents.x1 = rgn->extents.x1;
  1155. if (rgn->extents.x2 > dstrgn->extents.x2)
  1156. dstrgn->extents.x2 = rgn->extents.x2;
  1157. dstrgn->extents.y2 = rgn->extents.y2;
  1158. }
  1159. else
  1160. {
  1161. first = PIXREGION_BOXPTR(dstrgn);
  1162. last = old + (numRects - 1);
  1163. if ((first->y1 > last->y2) ||
  1164. ((first->y1 == last->y1) && (first->y2 == last->y2) &&
  1165. (first->x1 > last->x2)))
  1166. {
  1167. prepend = PIXMAN_REGION_STATUS_SUCCESS;
  1168. if (rgn->extents.x1 < dstrgn->extents.x1)
  1169. dstrgn->extents.x1 = rgn->extents.x1;
  1170. if (rgn->extents.x2 > dstrgn->extents.x2)
  1171. dstrgn->extents.x2 = rgn->extents.x2;
  1172. dstrgn->extents.y1 = rgn->extents.y1;
  1173. }
  1174. else
  1175. dstrgn->extents.x2 = dstrgn->extents.x1;
  1176. }
  1177. }
  1178. if (prepend)
  1179. {
  1180. new = PIXREGION_BOX(dstrgn, numRects);
  1181. if (dnumRects == 1)
  1182. *new = *PIXREGION_BOXPTR(dstrgn);
  1183. else
  1184. memmove((char *)new,(char *)PIXREGION_BOXPTR(dstrgn),
  1185. dnumRects * sizeof(pixman_box16_t));
  1186. new = PIXREGION_BOXPTR(dstrgn);
  1187. }
  1188. else
  1189. new = PIXREGION_BOXPTR(dstrgn) + dnumRects;
  1190. if (numRects == 1)
  1191. *new = *old;
  1192. else
  1193. memmove((char *)new, (char *)old, numRects * sizeof(pixman_box16_t));
  1194. dstrgn->data->numRects += numRects;
  1195. return PIXMAN_REGION_STATUS_SUCCESS;
  1196. }
  1197. #define ExchangeRects(a, b) \
  1198. { \
  1199. pixman_box16_t t; \
  1200. t = rects[a]; \
  1201. rects[a] = rects[b]; \
  1202. rects[b] = t; \
  1203. }
  1204. static void
  1205. QuickSortRects(
  1206. pixman_box16_t rects[],
  1207. int numRects)
  1208. {
  1209. int y1;
  1210. int x1;
  1211. int i, j;
  1212. pixman_box16_t *r;
  1213. /* Always called with numRects > 1 */
  1214. do
  1215. {
  1216. if (numRects == 2)
  1217. {
  1218. if (rects[0].y1 > rects[1].y1 ||
  1219. (rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1))
  1220. ExchangeRects(0, 1);
  1221. return;
  1222. }
  1223. /* Choose partition element, stick in location 0 */
  1224. ExchangeRects(0, numRects >> 1);
  1225. y1 = rects[0].y1;
  1226. x1 = rects[0].x1;
  1227. /* Partition array */
  1228. i = 0;
  1229. j = numRects;
  1230. do
  1231. {
  1232. r = &(rects[i]);
  1233. do
  1234. {
  1235. r++;
  1236. i++;
  1237. } while (i != numRects &&
  1238. (r->y1 < y1 || (r->y1 == y1 && r->x1 < x1)));
  1239. r = &(rects[j]);
  1240. do
  1241. {
  1242. r--;
  1243. j--;
  1244. } while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1));
  1245. if (i < j)
  1246. ExchangeRects(i, j);
  1247. } while (i < j);
  1248. /* Move partition element back to middle */
  1249. ExchangeRects(0, j);
  1250. /* Recurse */
  1251. if (numRects-j-1 > 1)
  1252. QuickSortRects(&rects[j+1], numRects-j-1);
  1253. numRects = j;
  1254. } while (numRects > 1);
  1255. }
  1256. /*-
  1257. *-----------------------------------------------------------------------
  1258. * pixman_region_validate --
  1259. *
  1260. * Take a ``region'' which is a non-y-x-banded random collection of
  1261. * rectangles, and compute a nice region which is the union of all the
  1262. * rectangles.
  1263. *
  1264. * Results:
  1265. * PIXMAN_REGION_STATUS_SUCCESS if successful.
  1266. *
  1267. * Side Effects:
  1268. * The passed-in ``region'' may be modified.
  1269. * pOverlap set to PIXMAN_REGION_STATUS_SUCCESS if any retangles overlapped, else PIXMAN_REGION_STATUS_FAILURE;
  1270. *
  1271. * Strategy:
  1272. * Step 1. Sort the rectangles into ascending order with primary key y1
  1273. * and secondary key x1.
  1274. *
  1275. * Step 2. Split the rectangles into the minimum number of proper y-x
  1276. * banded regions. This may require horizontally merging
  1277. * rectangles, and vertically coalescing bands. With any luck,
  1278. * this step in an identity tranformation (ala the Box widget),
  1279. * or a coalescing into 1 box (ala Menus).
  1280. *
  1281. * Step 3. Merge the separate regions down to a single region by calling
  1282. * pixman_region_union. Maximize the work each pixman_region_union call does by using
  1283. * a binary merge.
  1284. *
  1285. *-----------------------------------------------------------------------
  1286. */
  1287. pixman_region_status_t
  1288. pixman_region_validate(pixman_region16_t * badreg,
  1289. int *pOverlap)
  1290. {
  1291. /* Descriptor for regions under construction in Step 2. */
  1292. typedef struct {
  1293. pixman_region16_t reg;
  1294. int prevBand;
  1295. int curBand;
  1296. } RegionInfo;
  1297. int numRects; /* Original numRects for badreg */
  1298. RegionInfo *ri; /* Array of current regions */
  1299. int numRI; /* Number of entries used in ri */
  1300. int sizeRI; /* Number of entries available in ri */
  1301. int i; /* Index into rects */
  1302. int j; /* Index into ri */
  1303. RegionInfo *rit; /* &ri[j] */
  1304. pixman_region16_t * reg; /* ri[j].reg */
  1305. pixman_box16_t * box; /* Current box in rects */
  1306. pixman_box16_t * riBox; /* Last box in ri[j].reg */
  1307. pixman_region16_t * hreg; /* ri[j_half].reg */
  1308. pixman_region_status_t ret = PIXMAN_REGION_STATUS_SUCCESS;
  1309. *pOverlap = PIXMAN_REGION_STATUS_FAILURE;
  1310. if (!badreg->data)
  1311. {
  1312. good(badreg);
  1313. return PIXMAN_REGION_STATUS_SUCCESS;
  1314. }
  1315. numRects = badreg->data->numRects;
  1316. if (!numRects)
  1317. {
  1318. if (PIXREGION_NAR(badreg))
  1319. return PIXMAN_REGION_STATUS_FAILURE;
  1320. good(badreg);
  1321. return PIXMAN_REGION_STATUS_SUCCESS;
  1322. }
  1323. if (badreg->extents.x1 < badreg->extents.x2)
  1324. {
  1325. if ((numRects) == 1)
  1326. {
  1327. freeData(badreg);
  1328. badreg->data = (pixman_region16_data_t *) NULL;
  1329. }
  1330. else
  1331. {
  1332. DOWNSIZE(badreg, numRects);
  1333. }
  1334. good(badreg);
  1335. return PIXMAN_REGION_STATUS_SUCCESS;
  1336. }
  1337. /* Step 1: Sort the rects array into ascending (y1, x1) order */
  1338. QuickSortRects(PIXREGION_BOXPTR(badreg), numRects);
  1339. /* Step 2: Scatter the sorted array into the minimum number of regions */
  1340. /* Set up the first region to be the first rectangle in badreg */
  1341. /* Note that step 2 code will never overflow the ri[0].reg rects array */
  1342. ri = (RegionInfo *) malloc(4 * sizeof(RegionInfo));
  1343. if (!ri)
  1344. return pixman_break (badreg);
  1345. sizeRI = 4;
  1346. numRI = 1;
  1347. ri[0].prevBand = 0;
  1348. ri[0].curBand = 0;
  1349. ri[0].reg = *badreg;
  1350. box = PIXREGION_BOXPTR(&ri[0].reg);
  1351. ri[0].reg.extents = *box;
  1352. ri[0].reg.data->numRects = 1;
  1353. /* Now scatter rectangles into the minimum set of valid regions. If the
  1354. next rectangle to be added to a region would force an existing rectangle
  1355. in the region to be split up in order to maintain y-x banding, just
  1356. forget it. Try the next region. If it doesn't fit cleanly into any
  1357. region, make a new one. */
  1358. for (i = numRects; --i > 0;)
  1359. {
  1360. box++;
  1361. /* Look for a region to append box to */
  1362. for (j = numRI, rit = ri; --j >= 0; rit++)
  1363. {
  1364. reg = &rit->reg;
  1365. riBox = PIXREGION_END(reg);
  1366. if (box->y1 == riBox->y1 && box->y2 == riBox->y2)
  1367. {
  1368. /* box is in same band as riBox. Merge or append it */
  1369. if (box->x1 <= riBox->x2)
  1370. {
  1371. /* Merge it with riBox */
  1372. if (box->x1 < riBox->x2) *pOverlap = PIXMAN_REGION_STATUS_SUCCESS;
  1373. if (box->x2 > riBox->x2) riBox->x2 = box->x2;
  1374. }
  1375. else
  1376. {
  1377. RECTALLOC_BAIL(reg, 1, bail);
  1378. *PIXREGION_TOP(reg) = *box;
  1379. reg->data->numRects++;
  1380. }
  1381. goto NextRect; /* So sue me */
  1382. }
  1383. else if (box->y1 >= riBox->y2)
  1384. {
  1385. /* Put box into new band */
  1386. if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
  1387. if (reg->extents.x1 > box->x1) reg->extents.x1 = box->x1;
  1388. Coalesce(reg, rit->prevBand, rit->curBand);
  1389. rit->curBand = reg->data->numRects;
  1390. RECTALLOC_BAIL(reg, 1, bail);
  1391. *PIXREGION_TOP(reg) = *box;
  1392. reg->data->numRects++;
  1393. goto NextRect;
  1394. }
  1395. /* Well, this region was inappropriate. Try the next one. */
  1396. } /* for j */
  1397. /* Uh-oh. No regions were appropriate. Create a new one. */
  1398. if (sizeRI == numRI)
  1399. {
  1400. /* Oops, allocate space for new region information */
  1401. sizeRI <<= 1;
  1402. rit = (RegionInfo *) realloc(ri, sizeRI * sizeof(RegionInfo));
  1403. if (!rit)
  1404. goto bail;
  1405. ri = rit;
  1406. rit = &ri[numRI];
  1407. }
  1408. numRI++;
  1409. rit->prevBand = 0;
  1410. rit->curBand = 0;
  1411. rit->reg.extents = *box;
  1412. rit->reg.data = (pixman_region16_data_t *)NULL;
  1413. if (!pixman_rect_alloc(&rit->reg, (i+numRI) / numRI)) /* MUST force allocation */
  1414. goto bail;
  1415. NextRect: ;
  1416. } /* for i */
  1417. /* Make a final pass over each region in order to Coalesce and set
  1418. extents.x2 and extents.y2 */
  1419. for (j = numRI, rit = ri; --j >= 0; rit++)
  1420. {
  1421. reg = &rit->reg;
  1422. riBox = PIXREGION_END(reg);
  1423. reg->extents.y2 = riBox->y2;
  1424. if (reg->extents.x2 < riBox->x2) reg->extents.x2 = riBox->x2;
  1425. Coalesce(reg, rit->prevBand, rit->curBand);
  1426. if (reg->data->numRects == 1) /* keep unions happy below */
  1427. {
  1428. freeData(reg);
  1429. reg->data = (pixman_region16_data_t *)NULL;
  1430. }
  1431. }
  1432. /* Step 3: Union all regions into a single region */
  1433. while (numRI > 1)
  1434. {
  1435. int half = numRI/2;
  1436. for (j = numRI & 1; j < (half + (numRI & 1)); j++)
  1437. {
  1438. reg = &ri[j].reg;
  1439. hreg = &ri[j+half].reg;
  1440. if (!pixman_op(reg, reg, hreg, pixman_region_unionO, PIXMAN_REGION_STATUS_SUCCESS, PIXMAN_REGION_STATUS_SUCCESS, pOverlap))
  1441. ret = PIXMAN_REGION_STATUS_FAILURE;
  1442. if (hreg->extents.x1 < reg->extents.x1)
  1443. reg->extents.x1 = hreg->extents.x1;
  1444. if (hreg->extents.y1 < reg->extents.y1)
  1445. reg->extents.y1 = hreg->extents.y1;
  1446. if (hreg->extents.x2 > reg->extents.x2)
  1447. reg->extents.x2 = hreg->extents.x2;
  1448. if (hreg->extents.y2 > reg->extents.y2)
  1449. reg->extents.y2 = hreg->extents.y2;
  1450. freeData(hreg);
  1451. }
  1452. numRI -= half;
  1453. }
  1454. *badreg = ri[0].reg;
  1455. free(ri);
  1456. good(badreg);
  1457. return ret;
  1458. bail:
  1459. for (i = 0; i < numRI; i++)
  1460. freeData(&ri[i].reg);
  1461. free (ri);
  1462. return pixman_break (badreg);
  1463. }
  1464. /* XXX: Need to fix this to not use any X data structure
  1465. pixman_region16_t *
  1466. pixman_region_rectsToRegion(nrects, prect, ctype)
  1467. int nrects;
  1468. xRectangle *prect;
  1469. int ctype;
  1470. {
  1471. pixman_region16_t * region;
  1472. pixman_region16_data_t * pData;
  1473. pixman_box16_t * box;
  1474. int i;
  1475. int x1, y1, x2, y2;
  1476. region = pixman_region_create(NullBox, 0);
  1477. if (PIXREGION_NAR (region))
  1478. return region;
  1479. if (!nrects)
  1480. return region;
  1481. if (nrects == 1)
  1482. {
  1483. x1 = prect->x;
  1484. y1 = prect->y;
  1485. if ((x2 = x1 + (int) prect->width) > SHRT_MAX)
  1486. x2 = SHRT_MAX;
  1487. if ((y2 = y1 + (int) prect->height) > SHRT_MAX)
  1488. y2 = SHRT_MAX;
  1489. if (x1 != x2 && y1 != y2)
  1490. {
  1491. region->extents.x1 = x1;
  1492. region->extents.y1 = y1;
  1493. region->extents.x2 = x2;
  1494. region->extents.y2 = y2;
  1495. region->data = (pixman_region16_