1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
11 * A general-purpose data structure for holding a list of rectangular
12 * regions that need to be repainted, with intelligent coalescing.
14 * DirtyList will unify two regions A and B if the smallest rectangle
15 * enclosing both A and B occupies no more than epsilon + Area_A +
16 * Area_B. Failing this, if two corners of A fall within B, A will be
17 * shrunk to exclude the union of A and B.
19 public class DirtyList {
21 /** The dirty regions (each one is an int[4]). */
22 private int[][] dirties = new int[10][];
24 /** The number of dirty regions */
25 private int numdirties = 0;
27 /** See class comment */
28 private static final int epsilon = 50 * 50;
30 public int num() { return numdirties; }
32 /** grows the array */
34 int[][] newdirties = new int[dirties.length * 2][];
35 System.arraycopy(dirties, 0, newdirties, 0, numdirties);
39 /** Add a new rectangle to the dirty list; returns false if the
40 * region fell completely within an existing rectangle or set of
41 * rectangles (ie did not expand the dirty area)
43 public synchronized boolean dirty(int x, int y, int w, int h) {
44 if (numdirties == dirties.length) grow();
46 // we attempt the "lossless" combinations first
47 for(int i=0; i<numdirties; i++) {
48 int[] cur = dirties[i];
50 // new region falls completely within existing region
51 if (x >= cur[0] && y >= cur[1] && x + w <= cur[0] + cur[2] && y + h <= cur[1] + cur[3]) {
54 // existing region falls completely within new region
55 } else if (x <= cur[0] && y <= cur[1] && x + w >= cur[0] + cur[2] && y + h >= cur[1] + cur[3]) {
59 // left end of new region falls within existing region
60 } else if (x >= cur[0] && x < cur[0] + cur[2] && y >= cur[1] && y + h <= cur[1] + cur[3]) {
61 w = x + w - (cur[0] + cur[2]);
65 // right end of new region falls within existing region
66 } else if (x + w > cur[0] && x + w <= cur[0] + cur[2] && y >= cur[1] && y + h <= cur[1] + cur[3]) {
70 // top end of new region falls within existing region
71 } else if (x >= cur[0] && x + w <= cur[0] + cur[2] && y >= cur[1] && y < cur[1] + cur[3]) {
72 h = y + h - (cur[1] + cur[3]);
76 // bottom end of new region falls within existing region
77 } else if (x >= cur[0] && x + w <= cur[0] + cur[2] && y + h > cur[1] && y + h <= cur[1] + cur[3]) {
81 // left end of existing region falls within new region
82 } else if (dirties[i][0] >= x && dirties[i][0] < x + w && dirties[i][1] >= y && dirties[i][1] + dirties[i][3] <= y + h) {
83 dirties[i][2] = dirties[i][2] - (x + w - dirties[i][0]);
84 dirties[i][0] = x + w;
87 // right end of existing region falls within new region
88 } else if (dirties[i][0] + dirties[i][2] > x && dirties[i][0] + dirties[i][2] <= x + w &&
89 dirties[i][1] >= y && dirties[i][1] + dirties[i][3] <= y + h) {
90 dirties[i][2] = x - dirties[i][0];
93 // top end of existing region falls within new region
94 } else if (dirties[i][0] >= x && dirties[i][0] + dirties[i][2] <= x + w && dirties[i][1] >= y && dirties[i][1] < y + h) {
95 dirties[i][3] = dirties[i][3] - (y + h - dirties[i][1]);
96 dirties[i][1] = y + h;
99 // bottom end of existing region falls within new region
100 } else if (dirties[i][0] >= x && dirties[i][0] + dirties[i][2] <= x + w &&
101 dirties[i][1] + dirties[i][3] > y && dirties[i][1] + dirties[i][3] <= y + h) {
102 dirties[i][3] = y - dirties[i][1];
108 // then we attempt the "lossy" combinations
109 for(int i=0; i<numdirties; i++) {
110 int[] cur = dirties[i];
111 if (w > 0 && h > 0 && cur[2] > 0 && cur[3] > 0 &&
112 ((max(x + w, cur[0] + cur[2]) - min(x, cur[0])) *
113 (max(y + h, cur[1] + cur[3]) - min(y, cur[1])) <
114 w * h + cur[2] * cur[3] + epsilon)) {
115 int a = min(cur[0], x);
116 int b = min(cur[1], y);
117 int c = max(x + w, cur[0] + cur[2]) - min(cur[0], x);
118 int d = max(y + h, cur[1] + cur[3]) - min(cur[1], y);
121 return dirty(a, b, c, d);
125 dirties[numdirties++] = new int[] { x, y, w, h };
129 /** Returns true if there are no regions that need repainting */
130 public boolean empty() { return (numdirties == 0); }
133 * Atomically returns the list of dirty rectangles as an array of
134 * four-int arrays and clears the internal dirty-rectangle
135 * list. Note that some of the regions returned may be null, or
136 * may have zero height or zero width, and do not need to be
139 public synchronized int[][] flush() {
140 if (numdirties == 0) return null;
141 int[][] ret = dirties;
142 for(int i=numdirties; i<ret.length; i++) ret[i] = null;
143 dirties = new int[dirties.length][];
148 /** included here so that it can be inlined */
149 private static final int min(int a, int b) {
154 /** included here so that it can be inlined */
155 private static final int max(int a, int b) {
160 /** included here so that it can be inlined */
161 private static final int min(int a, int b, int c) {
162 if (a<=b && a<=c) return a;
163 else if (b<=c && b<=a) return b;
167 /** included here so that it can be inlined */
168 private static final int max(int a, int b, int c) {
169 if (a>=b && a>=c) return a;
170 else if (b>=c && b>=a) return b;
174 /** included here so that it can be inlined */
175 private static final int bound(int a, int b, int c) {