PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/uppdev/plugin/pixman/lib/pixman-region.c

http://upp-mirror.googlecode.com/
C | 2129 lines | 1367 code | 197 blank | 565 comment | 393 complexity | b1e408008c2e2bba58ca1656d741a76d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-2-Clause, BSD-3-Clause, LGPL-3.0, GPL-3.0

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

Large files files are truncated, but you can click here to view the full file