summaryrefslogtreecommitdiff
path: root/canvas/source/java/perftest/PerfTest.java
blob: 0534ce3dad0b68dc9e1cf2b209441ccc5cc6779f (plain)
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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: PerfTest.java,v $
 * $Revision: 1.4 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;


public class PerfTest
{
    // the frame object we're generating. TODO: Remove public access.
    private Frame   frame;
    private boolean fullscreen;

    public PerfTest()
    {
        fullscreen = false;

        // create a normal Java frame, and set it into fullscreen mode
        frame = new javax.swing.JFrame( "PerformanceTest" );
        frame.setBounds( new Rectangle(0,0,1600,1200) );
        frame.setUndecorated( true );
        frame.setVisible( true );

        Graphics2D graphics = (Graphics2D)frame.getGraphics();
        if( graphics.getDeviceConfiguration().getDevice().isFullScreenSupported() )
        {
            System.err.println( "entering fullscreen mode" );
            graphics.getDeviceConfiguration().getDevice().setFullScreenWindow( frame );
            fullscreen = true;
        }
        else
        {
            System.err.println( "fullscreen not supported" );
        }

        graphics.dispose();
    }

    //----------------------------------------------------------------------------------

    public void dispose()
    {
        if( fullscreen )
        {
            Graphics2D graphics = (Graphics2D)frame.getGraphics();
            if( graphics.getDeviceConfiguration().getDevice().isFullScreenSupported() )
            {
                System.err.println( "leaving fullscreen mode" );
                graphics.getDeviceConfiguration().getDevice().setFullScreenWindow( null );
            }
            graphics.dispose();
        }

        if( frame != null )
            frame.dispose();
    }

    //----------------------------------------------------------------------------------

    public static void initGraphics( Graphics2D graphics )
    {
        if( graphics != null )
        {
            RenderingHints hints = new RenderingHints(null);
            boolean hq = true;

            if( hq )
            {
                hints.add( new RenderingHints( RenderingHints.KEY_FRACTIONALMETRICS,
                                               RenderingHints.VALUE_FRACTIONALMETRICS_ON ) );
//                 hints.add( new RenderingHints( RenderingHints.KEY_ALPHA_INTERPOLATION,
//                                                         RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY) );
                hints.add( new RenderingHints( RenderingHints.KEY_ALPHA_INTERPOLATION,
                                               RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED) );
//                 hints.add( new RenderingHints( RenderingHints.KEY_INTERPOLATION,
//                                                         RenderingHints.VALUE_INTERPOLATION_BICUBIC) );
                hints.add( new RenderingHints( RenderingHints.KEY_INTERPOLATION,
                                               RenderingHints.VALUE_INTERPOLATION_BILINEAR) );
//                 hints.add( new RenderingHints( RenderingHints.KEY_RENDERING,
//                                                         RenderingHints.VALUE_RENDER_QUALITY) );
                hints.add( new RenderingHints( RenderingHints.KEY_RENDERING,
                                               RenderingHints.VALUE_RENDER_SPEED) );
//                 hints.add( new RenderingHints( RenderingHints.KEY_STROKE_CONTROL,
//                                                         RenderingHints.VALUE_STROKE_NORMALIZE) );
                hints.add( new RenderingHints( RenderingHints.KEY_STROKE_CONTROL,
                                               RenderingHints.VALUE_STROKE_DEFAULT) );
                hints.add( new RenderingHints( RenderingHints.KEY_ANTIALIASING,
                                               RenderingHints.VALUE_ANTIALIAS_ON) );
            }
            else
            {
                hints.add( new RenderingHints( RenderingHints.KEY_ALPHA_INTERPOLATION,
                                               RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED) );
                hints.add( new RenderingHints( RenderingHints.KEY_INTERPOLATION,
                                               RenderingHints.VALUE_INTERPOLATION_BILINEAR) );
                hints.add( new RenderingHints( RenderingHints.KEY_RENDERING,
                                               RenderingHints.VALUE_RENDER_SPEED) );
                hints.add( new RenderingHints( RenderingHints.KEY_STROKE_CONTROL,
                                               RenderingHints.VALUE_STROKE_DEFAULT) );
                hints.add( new RenderingHints( RenderingHints.KEY_ANTIALIASING,
                                               RenderingHints.VALUE_ANTIALIAS_OFF) );
            }

            // the least common denominator standard
            hints.add( new RenderingHints( RenderingHints.KEY_FRACTIONALMETRICS,
                                           RenderingHints.VALUE_FRACTIONALMETRICS_ON) );
            hints.add( new RenderingHints( RenderingHints.KEY_TEXT_ANTIALIASING,
                                           RenderingHints.VALUE_TEXT_ANTIALIAS_ON) );

            graphics.setRenderingHints( hints );
        }
    }

    public void run()
    {
        frame.createBufferStrategy(2);
        BufferStrategy bufferStrategy = frame.getBufferStrategy();

        if( bufferStrategy.getCapabilities().isPageFlipping() )
            System.err.println( "double buffering is using page flipping" );
        else
            System.err.println( "double buffering is using blitting" );

        int width = 1600;
        int height = 1200;
        Graphics2D graphics             = (Graphics2D)frame.getGraphics();
        VolatileImage backBuffer        = graphics.getDeviceConfiguration().createCompatibleVolatileImage(width,height); // TODO: size dynamic
        BufferedImage buffer            = graphics.getDeviceConfiguration().createCompatibleImage(width,height,
                                                                                                  Transparency.TRANSLUCENT);
        BufferedImage buffer2           = graphics.getDeviceConfiguration().createCompatibleImage(width,height,
                                                                                                  Transparency.TRANSLUCENT);
        BufferedImage buffer3           = graphics.getDeviceConfiguration().createCompatibleImage(width,height,
                                                                                                  Transparency.TRANSLUCENT);

        GraphicsDevice device = graphics.getDeviceConfiguration().getDevice();
        System.err.println( "Images generated. Available vram: " + device.getAvailableAcceleratedMemory() );

        Graphics2D bufferGraphics       = (Graphics2D)buffer.getGraphics();
        Graphics2D buffer2Graphics      = (Graphics2D)buffer2.getGraphics();
        Graphics2D buffer3Graphics      = (Graphics2D)buffer3.getGraphics();
        Graphics2D backBufGraphics      = (Graphics2D)backBuffer.getGraphics();

        System.err.println( "Image graphics generated. Available vram: " + device.getAvailableAcceleratedMemory() );

        // init Graphics
        initGraphics( graphics );
        initGraphics( bufferGraphics );
        initGraphics( buffer2Graphics );
        initGraphics( backBufGraphics );

        // init content
        Font font = new Font( "Times", Font.PLAIN, 100 );
        Font fpsFont = new Font( "Times", Font.PLAIN, 20 );

        bufferGraphics.setComposite( AlphaComposite.getInstance(AlphaComposite.CLEAR));
        bufferGraphics.fillRect( 0,0,width,height );

        bufferGraphics.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        bufferGraphics.setColor( Color.red );
        bufferGraphics.fillRect( 0,0,width/2,height/2 );
        bufferGraphics.setColor( Color.green );
        bufferGraphics.fillRect( width/2,0,width,height/2 );
        bufferGraphics.setColor( Color.blue );
        bufferGraphics.fillRect( 0,height/2,width/2,height );

        buffer2Graphics.setColor( Color.red );
        buffer2Graphics.fillRect( 0,0,width/2,height/2 );
        buffer2Graphics.setColor( Color.green );
        buffer2Graphics.fillRect( width/2,0,width,height/2 );
        buffer2Graphics.setColor( Color.blue );
        buffer2Graphics.fillRect( 0,height/2,width/2,height );

        buffer3Graphics.setColor( Color.blue );
        buffer3Graphics.fillRect( 0,0,width/2,height/2 );
        buffer3Graphics.setColor( Color.white );
        buffer3Graphics.fillRect( width/2,0,width,height/2 );
        buffer3Graphics.setColor( Color.gray );
        buffer3Graphics.fillRect( 0,height/2,width/2,height );

        backBufGraphics.setColor( Color.white );
        backBufGraphics.fillRect(0,0,width,height);
        backBufGraphics.setColor( Color.red );
        backBufGraphics.setFont( font );
        int i, turns=15;
        for(i=0; i<turns; ++i)
        {
            backBufGraphics.drawString( "Crossfade test", width*i/turns, height*i/turns );
        }

        System.err.println( "Images filled with content. Available vram: " + device.getAvailableAcceleratedMemory() );

        long lastTime = System.currentTimeMillis();
        int  turn, numTurns = 100;
        for(turn=0; turn<numTurns; ++turn)
        {
            // fetch the Graphics object to draw into (we're doing double
            // buffering here, the content is later shown via
            // bufferStrategy.show().
            Graphics2D graph = null;

            try
            {
                graph = (Graphics2D)bufferStrategy.getDrawGraphics();

                try
                {
                    // repaint background
                    graph.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER) );
                    graph.drawImage(backBuffer, 0, 0, null);

                    // alpha-composite foreground on top of that
                    float alpha = turn/(float)numTurns;
                    graph.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha) );

                    graph.drawImage(buffer, 0, 0, null);
                    buffer.flush();
                    graph.drawImage(buffer2, 100, 100, null);
                    buffer2.flush();

                    long currTime = System.currentTimeMillis();
                    graph.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER) );
                    graph.setFont( fpsFont );
                    graph.setColor( Color.black );
                    String fps = new String( String.valueOf(1000.0/(currTime-lastTime)) );
                    System.err.println( "Images composited. Framerate: " + fps + " fps" );
                    graph.drawString( fps.substring(0,5) + " fps", 0, 20);
                    lastTime = currTime;

                    System.err.println( "Available vram: " + device.getAvailableAcceleratedMemory() );
                }
                catch( Exception e )
                {
                }

                System.err.println( "Turn: " + turn );
            }
            finally
            {
                if( graph != null )
                    graph.dispose();
            }

            bufferStrategy.show();
        }

        try
        {
            Thread.sleep(2000);
        }
        catch( Exception e )
        {
        }
    }

    //----------------------------------------------------------------------------------

    public static void main(String[] args)
    {
        // now, we're getting slightly system dependent here.
        String os = (String) System.getProperty("os.name");

        System.err.println( "System detected: " + os );

        // tweak some speed knobs...
        if( os.startsWith("Windows") )
        {
            System.setProperty("sun.java2d.translaccel", "true");
            System.setProperty("sun.java2d.ddforcevram", "true");
            //System.setProperty("sun.java2d.accthreshold", "0");

            System.err.println( "Optimizing for Windows" );
        }
        else
        {
            System.setProperty("sun.java2d.opengl", "true");

            System.err.println( "Optimizing for Unix" );
        }

        PerfTest test = new PerfTest();

        test.run();

        test.dispose();
    }

    //----------------------------------------------------------------------------------

}