1/*
2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 *   - Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 *
11 *   - Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 *   - Neither the name of Oracle nor the names of its
16 *     contributors may be used to endorse or promote products derived
17 *     from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This source code is provided to illustrate the usage of a given feature
34 * or technique and has been deliberately simplified. Additional steps
35 * required for a production-quality application, such as security checks,
36 * input validation and proper error handling, might not be present in
37 * this sample code.
38 */
39
40
41package j2dbench;
42
43import java.awt.Canvas;
44import java.awt.Image;
45import java.awt.Graphics;
46import java.awt.Graphics2D;
47import java.awt.Dimension;
48import java.awt.AlphaComposite;
49import java.awt.Color;
50import java.awt.Toolkit;
51import java.util.Hashtable;
52
53import j2dbench.tests.GraphicsTests;
54
55public class TestEnvironment implements Node.Visitor {
56    static Group globaloptroot;
57    static Group envroot;
58
59    static Option.Int outputWidth;
60    static Option.Int outputHeight;
61
62    static Option.Int runCount;
63    static Option.Int repCount;
64    static Option.Int testTime;
65
66    public static void init() {
67        globaloptroot = new Group("global", "Global Options");
68        envroot = new Group(globaloptroot, "env", "Test Environment Options");
69
70        outputWidth =
71            new Option.Int(envroot, "outputwidth",
72                           "Width of Output Window or Image",
73                           1, Integer.MAX_VALUE, 640);
74        outputHeight =
75            new Option.Int(envroot, "outputheight",
76                           "Height of Output Window or Image",
77                           1, Integer.MAX_VALUE, 480);
78
79        runCount =
80            new Option.Int(envroot, "runcount",
81                           "Fixed Number of Test Runs per Benchmark",
82                           1, Integer.MAX_VALUE, 5);
83        repCount =
84            new Option.Int(envroot, "repcount",
85                           "Fixed Number of Reps (0 means calibrate)",
86                           0, Integer.MAX_VALUE, 0);
87        testTime =
88            new Option.Int(envroot, "testtime",
89                           "Target test time to calibrate for",
90                           1, Integer.MAX_VALUE, 2500);
91    }
92
93    public void visit(Node node) {
94        if (node instanceof Test) {
95            ((Test) node).runTest(this);
96        }
97    }
98
99    public void runAllTests() {
100        Group.root.traverse(this);
101    }
102
103    Canvas comp;
104    Image testImage;
105    Image srcImage;
106    boolean stopped;
107    ResultSet results;
108    Hashtable modifiers;
109    Timer timer;
110
111    public TestEnvironment() {
112        results = new ResultSet();
113        modifiers = new Hashtable();
114        timer = Timer.getImpl();
115    }
116
117    public void startTiming() {
118        timer.start();
119    }
120
121    public void stopTiming() {
122        timer.stop();
123    }
124
125    public long getTimeMillis() {
126        return timer.getTimeMillis();
127    }
128
129    public long getTimeNanos() {
130        return timer.getTimeNanos();
131    }
132
133    public Canvas getCanvas() {
134        if (comp == null) {
135            final int w = getWidth();
136            final int h = getHeight();
137            comp = new Canvas() {
138                public Dimension getPreferredSize() {
139                    return new Dimension(w, h);
140                }
141            };
142        }
143        return comp;
144    }
145
146    public Image getSrcImage() {
147        return srcImage;
148    }
149
150    public void stop() {
151        stopped = true;
152    }
153
154    public boolean isStopped() {
155        return stopped;
156    }
157
158    public void setTestImage(Image img) {
159        this.testImage = img;
160    }
161
162    public void setSrcImage(Image img) {
163        this.srcImage = img;
164    }
165
166    public void erase() {
167        Graphics g = getGraphics();
168        if (g != null) {
169            g.setColor(Color.white);
170            g.fillRect(0, 0, getWidth(), getHeight());
171            g.dispose();
172        }
173    }
174
175    public Graphics getGraphics() {
176        if (testImage != null) {
177            return testImage.getGraphics();
178        }
179        if (comp != null) {
180            return comp.getGraphics();
181        }
182        return null;
183    }
184
185    public int getWidth() {
186        return outputWidth.getIntValue();
187    }
188
189    public int getHeight() {
190        return outputHeight.getIntValue();
191    }
192
193    public int getRunCount() {
194        return runCount.getIntValue();
195    }
196
197    public int getRepCount() {
198        return repCount.getIntValue();
199    }
200
201    public long getTestTime() {
202        return testTime.getIntValue();
203    }
204
205    public void sync() {
206        if (comp == null) {
207            Toolkit.getDefaultToolkit().sync();
208        } else {
209            comp.getToolkit().sync();
210        }
211    }
212
213    public boolean idle() {
214        if (!stopped) {
215            sync();
216            System.gc();
217            System.runFinalization();
218            System.gc();
219            sync();
220            try {
221                Thread.sleep(50);
222            } catch (InterruptedException e) {
223                stop();
224            }
225        }
226        return stopped;
227    }
228
229    public void setModifier(Modifier o, Object v) {
230        modifiers.put(o, v);
231    }
232
233    public Object getModifier(Modifier o) {
234        return modifiers.get(o);
235    }
236
237    public boolean isEnabled(Modifier o) {
238        return ((Boolean) modifiers.get(o)).booleanValue();
239    }
240
241    public int getIntValue(Modifier o) {
242        return ((Integer) modifiers.get(o)).intValue();
243    }
244
245    public void removeModifier(Modifier o) {
246        modifiers.remove(o);
247    }
248
249    public Hashtable getModifiers() {
250        return (Hashtable) modifiers.clone();
251    }
252
253    public void record(Result result) {
254        results.record(result);
255    }
256
257    public void flushToScreen() {
258        if (testImage != null && comp != null) {
259            Graphics g = comp.getGraphics();
260            if (GraphicsTests.hasGraphics2D) {
261                ((Graphics2D) g).setComposite(AlphaComposite.Src);
262            }
263            g.drawImage(testImage, 0, 0, null);
264            g.dispose();
265        }
266    }
267
268    public void summarize() {
269        results.summarize();
270    }
271
272    private abstract static class Timer {
273        public static Timer getImpl() {
274            try {
275                System.nanoTime();
276                return new Nanos();
277            } catch (NoSuchMethodError e) {
278                return new Millis();
279            }
280        }
281
282        public abstract void start();
283        public abstract void stop();
284        public abstract long getTimeMillis();
285        public abstract long getTimeNanos();
286
287        private static class Millis extends Timer {
288            private long millis;
289
290            public void start() {
291                millis = System.currentTimeMillis();
292            }
293
294            public void stop() {
295                millis = System.currentTimeMillis() - millis;
296            }
297
298            public long getTimeMillis() {
299                return millis;
300            }
301
302            public long getTimeNanos() {
303                return millis * 1000 * 1000;
304            }
305        }
306
307        private static class Nanos extends Timer {
308            private long nanos;
309
310            public void start() {
311                nanos = System.nanoTime();
312            }
313
314            public void stop() {
315                nanos = System.nanoTime() - nanos;
316            }
317
318            public long getTimeMillis() {
319                return (nanos + (500 * 1000)) / (1000 * 1000);
320            }
321
322            public long getTimeNanos() {
323                return nanos;
324            }
325        }
326    }
327}
328