summaryrefslogtreecommitdiff
path: root/src/java.desktop/share/classes/sun/java2d/pipe/RegionClipSpanIterator.java
blob: c409ae1b98d18146cf626beeeb24e7e5e107d6e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.java2d.pipe;


/**
 * This class clips a SpanIterator to a Region and outputs the
 * resulting spans as another SpanIterator.
 *
 * Spans are output in the usual y/x order, unless the input span
 * iterator doesn't conform to this order, or the iterator's span
 * straddle more than one band of the Region used for clipping.
 *
 * Principle of operation:
 *
 * The iterator maintains a several cursors onto the RegionIterator
 * in order to avoid having to buffer spans from the SpanIterator.
 * They are:
 *  resetState    The initial state of the RegionIterator
 *  lwm             Low Water Mark, a running start point for
 *                  processing each band. Usually goes down, but
 *                  can be reset to resetState if a span has a lower
 *                  start coordinate than the previous one.
 *  row             The start of the current band of the RegionIterator
 *  box             The current span of the current row
 *
 * The main nextSpan() loop implements a coroutine like structure, with
 * three producers to get the next span, row and box calling each other
 * to iterate through the span iterator and region.
 *
 * REMIND: Needs a native implementation!
 */
public class RegionClipSpanIterator implements SpanIterator {

    // The inputs to the filter
    Region rgn;
    SpanIterator spanIter;

    // The cursors that track the progress through the region
    RegionIterator resetState;
    RegionIterator lwm;
    RegionIterator row;
    RegionIterator box;

    // The bounds of the current span iterator span
    int spanlox, spanhix, spanloy, spanhiy;

    // The extent of the region band marking the low water mark
    int lwmloy, lwmhiy;

    // The bounds of the current region box
    int rgnlox, rgnloy, rgnhix, rgnhiy;

    // The bounding box of the input Region. Used for click
    // rejection of iterator spans
    int rgnbndslox, rgnbndsloy, rgnbndshix, rgnbndshiy;

    // The array used to hold coordinates from the region iterator
    int[] rgnbox = new int[4];

    // The array used to hold coordinates from the span iterator
    int[] spanbox = new int[4];

    // True if the next iterator span should be read on the next
    // iteration of the main nextSpan() loop
    boolean doNextSpan;

    // True if the next region box should be read on the next
    // iteration of the main nextSpan() loop
    boolean doNextBox;

    // True if there are no more spans or the Region is empty
    boolean done = false;

    /*
     * Creates an instance that filters the spans generated by
     * spanIter through the region described by rgn.
     */
    public RegionClipSpanIterator(Region rgn, SpanIterator spanIter) {

        this.spanIter = spanIter;

        resetState = rgn.getIterator();
        lwm = resetState.createCopy();

        if (!lwm.nextYRange(rgnbox)) {
            done = true;
            return;
        }

        rgnloy = lwmloy = rgnbox[1];
        rgnhiy = lwmhiy = rgnbox[3];

        rgn.getBounds(rgnbox);
        rgnbndslox = rgnbox[0];
        rgnbndsloy = rgnbox[1];
        rgnbndshix = rgnbox[2];
        rgnbndshiy = rgnbox[3];
        if (rgnbndslox >= rgnbndshix ||
            rgnbndsloy >= rgnbndshiy) {
            done = true;
            return;
        }

        this.rgn = rgn;


        row = lwm.createCopy();
        box = row.createCopy();
        doNextSpan = true;
        doNextBox = false;
    }

    /*
     * Gets the bbox of the available path segments, clipped to the
     * Region.
     */
    public void getPathBox(int[] pathbox) {
        int[] rgnbox = new int[4];
        rgn.getBounds(rgnbox);
        spanIter.getPathBox(pathbox);

        if (pathbox[0] < rgnbox[0]) {
            pathbox[0] = rgnbox[0];
        }

        if (pathbox[1] < rgnbox[1]) {
            pathbox[1] = rgnbox[1];
        }

        if (pathbox[2] > rgnbox[2]) {
            pathbox[2] = rgnbox[2];
        }

        if (pathbox[3] > rgnbox[3]) {
            pathbox[3] = rgnbox[3];
        }
}

    /*
     * Intersects the path box with the given bbox.
     * Returned spans are clipped to this region, or discarded
     * altogether if they lie outside it.
     */
    public void intersectClipBox(int lox, int loy, int hix, int hiy) {
        spanIter.intersectClipBox(lox, loy, hix, hiy);
    }


    /*
     * Fetches the next span that needs to be operated on.
     * If the return value is false then there are no more spans.
     */
    public boolean nextSpan(int[] resultbox) {
        if (done) {
            return false;
        }

        int resultlox, resultloy, resulthix, resulthiy;
        boolean doNextRow = false;

        // REMIND: Cache the coordinate inst vars used in this loop
        // in locals vars.
        while (true) {
            // We've exhausted the current span so get the next one
            if (doNextSpan) {
                if (!spanIter.nextSpan(spanbox)) {
                    done = true;
                    return false;
                } else {
                    spanlox = spanbox[0];
                    // Clip out spans that lie outside of the rgn's bounds
                    if (spanlox >= rgnbndshix) {
                        continue;
                    }

                    spanloy = spanbox[1];
                    if (spanloy >= rgnbndshiy) {
                        continue;
                    }

                    spanhix = spanbox[2];
                    if (spanhix <= rgnbndslox) {
                        continue;
                    }

                    spanhiy = spanbox[3];
                    if (spanhiy <= rgnbndsloy) {
                        continue;
                    }
                }
                // If the span starts higher up than the low-water mark,
                // reset the lwm. This can only happen if spans aren't
                // returned in strict y/x order, or the first time through.
                if (lwmloy > spanloy) {
                    lwm.copyStateFrom(resetState);
                    lwm.nextYRange(rgnbox);
                    lwmloy = rgnbox[1];
                    lwmhiy = rgnbox[3];
                }
                // Skip to the first rgn row whose bottom edge is
                // below the top of the current span. This will only
                // execute >0 times when the current span starts in a
                // lower region row than the previous one, or possibly the
                // first time through.
                while (lwmhiy <= spanloy) {
                    if (!lwm.nextYRange(rgnbox))
                        break;
                    lwmloy = rgnbox[1];
                    lwmhiy = rgnbox[3];
                }
                // If the row overlaps the span, process it, otherwise
                // fetch another span
                if (lwmhiy > spanloy && lwmloy < spanhiy) {
                    // Update the current row if it's different from the
                    // new lwm
                    if (rgnloy != lwmloy) {
                        row.copyStateFrom(lwm);
                        rgnloy = lwmloy;
                        rgnhiy = lwmhiy;
                    }
                    box.copyStateFrom(row);
                    doNextBox = true;
                    doNextSpan = false;
                }
                continue;
            }

            // The current row's spans are exhausted, do the next one
            if (doNextRow) {
                // Next time we either do the next span or the next box
                doNextRow = false;
                // Get the next row
                boolean ok = row.nextYRange(rgnbox);
                // If there was one, update the bounds
                if (ok) {
                    rgnloy = rgnbox[1];
                    rgnhiy = rgnbox[3];
                }
                if (!ok || rgnloy >= spanhiy) {
                    // If we've exhausted the rows or this one is below the span,
                    // go onto the next span
                    doNextSpan = true;
                }
                else {
                    // Otherwise get the first box on this row
                    box.copyStateFrom(row);
                    doNextBox = true;
                }
                continue;
            }

            // Process the next box in the current row
            if (doNextBox) {
                boolean ok = box.nextXBand(rgnbox);
                if (ok) {
                    rgnlox = rgnbox[0];
                    rgnhix = rgnbox[2];
                }
                if (!ok || rgnlox >= spanhix) {
                    // If there was no next rgn span or it's beyond the
                    // source span, go onto the next row or span
                    doNextBox = false;
                    if (rgnhiy >= spanhiy) {
                        // If the current row totally overlaps the span,
                        // go onto the next span
                        doNextSpan = true;
                    } else {
                        // otherwise go onto the next rgn row
                        doNextRow = true;
                    }
                } else {
                    // Otherwise, if the new rgn span overlaps the
                    // spanbox, no need to get another box
                    doNextBox = rgnhix <= spanlox;
                }
                continue;
            }

            // Prepare to do the next box either on this call or
            // or the subsequent one
            doNextBox = true;

            // Clip the current span against the current box
            if (spanlox > rgnlox) {
                resultlox = spanlox;
            }
            else {
                resultlox = rgnlox;
            }

            if (spanloy > rgnloy) {
                resultloy = spanloy;
            }
            else {
                resultloy = rgnloy;
            }

            if (spanhix < rgnhix) {
                resulthix = spanhix;
            }
            else {
                resulthix = rgnhix;
            }

            if (spanhiy < rgnhiy) {
                resulthiy = spanhiy;
            }
            else {
                resulthiy = rgnhiy;
            }

            // If the result is empty, try then next box
            // otherwise return the box.
            // REMIND: I think by definition it's non-empty
            // if we're here. Need to think about this some more.
            if (resultlox >= resulthix ||
                resultloy >= resulthiy) {
                    continue;
            }
            else {
                    break;
            }
        }

        resultbox[0] = resultlox;
        resultbox[1] = resultloy;
        resultbox[2] = resulthix;
        resultbox[3] = resulthiy;
        return true;

    }


    /**
     * This method tells the iterator that it may skip all spans
     * whose Y range is completely above the indicated Y coordinate.
     */
    public void skipDownTo(int y) {
        spanIter.skipDownTo(y);
    }

    /**
     * This method returns a native pointer to a function block that
     * can be used by a native method to perform the same iteration
     * cycle that the above methods provide while avoiding upcalls to
     * the Java object.
     * The definition of the structure whose pointer is returned by
     * this method is defined in:
     * <pre>
     *     src/share/native/sun/java2d/pipe/SpanIterator.h
     * </pre>
     */
    public long getNativeIterator() {
        return 0;
    }
}