File: MTGraphicsAccessTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (362 lines) | stat: -rw-r--r-- 12,022 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2010, 2011, 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.
 *
 * 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.
 */

/*
  @test
  @bug 5089429 6982632 8145808
  @summary Checks that we don't crash if rendering operations and state
  changes are performed on a graphics context from different threads.

  @author Dmitri.Trembovetski@sun.com area=Graphics
  @run main MTGraphicsAccessTest
 */

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.util.concurrent.atomic.AtomicInteger;

public class MTGraphicsAccessTest {

    // in seconds
    static final long STANDALONE_RUN_TIME = 20;
    static final long JTREG_RUN_TIME = 7;

    static boolean standaloneMode;
    static boolean allowExceptions = true;
    static long testRunTime;

    volatile boolean done;
    AtomicInteger stillRunning = new AtomicInteger(0);
    volatile int numexceptions;

    Graphics2D sharedGraphics;
    BufferedImage sharedBI =
            new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);

    static final Paint colors[] = {
        Color.red,
        new Color(0x7f, 0xff, 0x00, 0x7f),
        new GradientPaint(0,  0, Color.red,
                          50, 50, new Color(0x7f, 0xff, 0x00, 0x7f)),
    };
    static final Font fonts[] = {
        new Font("Dialog", Font.PLAIN, 12),
        new Font("Dialog", Font.BOLD, 16),
        new Font("Dialog", Font.ITALIC, 18),
    };
    static final AlphaComposite comps[] = {
        AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f),
        AlphaComposite.Src,
        AlphaComposite.Xor,
        AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f),
        null,
    };
    static final Stroke strokes[] = {
        new BasicStroke(),
        new BasicStroke(0.0f),
        new BasicStroke(2.0f),
        new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
                        BasicStroke.JOIN_BEVEL),
        new BasicStroke(5.0f, BasicStroke.CAP_SQUARE,
                        BasicStroke.JOIN_ROUND),
        new BasicStroke(0.0f, BasicStroke.CAP_ROUND,
                        BasicStroke.JOIN_ROUND, 0,
                        new float[]{0,6,0,6}, 0),
    };
    static final AffineTransform transforms[] = {
        new AffineTransform(),
        AffineTransform.getRotateInstance(10.0),
        AffineTransform.getShearInstance(10.0, 4.0),
        AffineTransform.getScaleInstance(1.1, 1.2),
        AffineTransform.getScaleInstance(3.0, 2.0),
    };

    public MTGraphicsAccessTest() {
        BufferedImage bi =
            new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
        sharedGraphics = (Graphics2D)bi.getGraphics();

        done = false;
        numexceptions = 0;

        for (int i = 0; i < (standaloneMode ? stateChangers.length : 3); i++) {
            (new TesterThread(stateChangers[i])).start();
        }
        for (int i = 0; i < (standaloneMode ? renderTests.length : 5); i++) {
            (new TesterThread(renderTests[i])).start();
        }

        mysleep(testRunTime);
        done = true;
        while (stillRunning.get() > 0) { mysleep(500); }

        if (numexceptions == 0) {
            System.err.println("Test passed");
        } else if (!allowExceptions) {
            throw new RuntimeException("Test failed with "+
                                       numexceptions+" exceptions");
        } else {
            System.err.println("Test finished with "+
                               numexceptions+" exceptions");
        }
    }

    private void mysleep(long time) {
        try {
            // add +/-5ms variance to increase randomness
            Thread.sleep(time + (long)(5 - Math.random()*10));
        } catch (InterruptedException e) {};
    }

    public static void usage(String message) {
        if (message != null) {
            System.err.println(message);
        }
        System.err.println("Usage: MTGraphicsAccessTest [-full] "+
            "[-time N/forever] [-help]");
        System.err.println(" -full: run full suite of tests "+
            "(default: limited number of tests is run)");
        System.err.println(" -time N: test duration in seconds/forever"+
            " (default: "+JTREG_RUN_TIME+"s for the short suite, "+
            STANDALONE_RUN_TIME+"s for the full suite)");
        System.err.println(" -help: print this help page");
        System.exit(1);
    }

    public static void main(String[] args) {
        boolean testRunSet = false;
        for (int i = 0; i < args.length; i++) {
            if ("-full".equals(args[i])) {
                standaloneMode = true;
                System.err.println("Running complete list of tests");
            } else if ("-noexc".equals(args[i])) {
                allowExceptions = false;
            } else if ("-time".equals(args[i])) {
                try {
                    String time = args[++i];
                    if ("forever".equals(time)) {
                        testRunTime = (Long.MAX_VALUE - 20)/1000;
                    } else {
                        testRunTime = 1000*Integer.parseInt(time);
                    }
                    testRunSet = true;
                } catch (NumberFormatException e) {
                    usage("Can't parse number of seconds: " + args[i]);
                } catch (ArrayIndexOutOfBoundsException e1) {
                    usage("Missing the 'seconds' argument for -time parameter");
                }
            } else if ("-help".equals(args[i])) {
                usage(null);
            } else {
                usage("Unknown argument:" + args[i]);
            }
        }

        if (!testRunSet) {
            testRunTime = 1000 *
                (standaloneMode ? STANDALONE_RUN_TIME : JTREG_RUN_TIME);
        }

        System.err.println("Approximate test run time: "+
             testRunTime/1000+" seconds");

        new MTGraphicsAccessTest();
    }

    class TesterThread extends Thread {
        Runnable testRunnable;

        public TesterThread(Runnable testRunnable) {
            stillRunning.incrementAndGet();
            this.testRunnable = testRunnable;
        }

        public void run() {
            try {
                while (!done) {
                    try {
                        testRunnable.run();
                        yield();
                    } catch (Throwable t) {
                        numexceptions++;
                        t.printStackTrace();
                    }
                }
            } finally {
                stillRunning.decrementAndGet();
            }
        }
    }

    final Runnable stateChangers[] = {
        new Runnable() {
            public void run() {
                sharedGraphics.setClip(10, 10, 30, 30);
                mysleep(10);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.setClip(10, 10, 30, 30);
                mysleep(10);
            }
        },
        new Runnable() {
            int c = 0;
            public void run() {
                sharedGraphics.setPaint(colors[c++ % colors.length]);
                mysleep(10);
            }
        },
        new Runnable() {
            boolean AA = false;
            public void run() {
                if (AA) {
                    sharedGraphics.setRenderingHint(
                        RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                } else {
                    sharedGraphics.setRenderingHint(
                        RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_OFF);
                }
                AA = !AA;
                mysleep(10);
            }
        },
        new Runnable() {
            int t = 0;
            public void run() {
                sharedGraphics.setTransform(
                    transforms[t++ % transforms.length]);
                mysleep(10);
            }
        },
        new Runnable() {
            int c = 0;
            public void run() {
                AlphaComposite comp = comps[c++ % comps.length];
                if (comp == null) {
                    sharedGraphics.setXORMode(Color.green);
                } else {
                    sharedGraphics.setComposite(comp);
                }
                mysleep(10);
            }
        },
        new Runnable() {
            int s = 0;
            public void run() {
                sharedGraphics.setStroke(strokes[s++ % strokes.length]);
                mysleep(10);
            }
        },
        new Runnable() {
            int f = 0;
            public void run() {
                sharedGraphics.setFont(fonts[f++ % fonts.length]);
                mysleep(10);
            }
        },
    };

    final Runnable renderTests[] = {
        new Runnable() {
            public void run() {
                sharedGraphics.drawLine(10, 10, 30, 30);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawLine(10, 10, 30, 30);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawRect(10, 10, 30, 30);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.fillRect(10, 10, 30, 30);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawString("Stuff", 10, 10);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.draw3DRect(10, 10, 30, 30, true);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawImage(sharedBI, 10, 10, null);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.fill3DRect(10, 10, 30, 30, false);
            }
        },
        // REMIND: copyArea doesn't work when transform is set..
        //          new Runnable() {
        //              public void run() {
        //                  sharedGraphics.copyArea(10, 10, 30, 30, 20, 20);
        //              }
        //          },
        new Runnable() {
            public void run() {
                sharedGraphics.drawRoundRect(10, 10, 30, 30, 20, 20);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.fillRoundRect(10, 10, 30, 30, 20, 20);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawArc(10, 10, 30, 30, 0, 90);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.fillArc(10, 10, 30, 30, 0, 90);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.drawOval(10, 10, 30, 30);
            }
        },
        new Runnable() {
            public void run() {
                sharedGraphics.fillOval(10, 10, 30, 30);
            }
        }
    };
}