aboutsummaryrefslogtreecommitdiff
path: root/test/sun/java2d/pipe/hw/RSLAPITest/RSLAPITest.java
blob: 52ddd3b21960df0b05a983efba4d5b810ce29a48 (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
/*
 * Copyright 2007-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
/*
 * @test
 * @bug 6635805 6653780 6667607
 * @summary Tests that the resource sharing layer API is not broken
 * @author Dmitri.Trembovetski@sun.com: area=Graphics
 * @compile -XDignore.symbol.file=true RSLAPITest.java
 * @run main/othervm RSLAPITest
 * @run main/othervm -Dsun.java2d.noddraw=true RSLAPITest
 * @run main/othervm -Dsun.java2d.opengl=True RSLAPITest
 */

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.VolatileImage;
import java.util.HashSet;
import sun.java2d.DestSurfaceProvider;
import sun.java2d.Surface;
import sun.java2d.pipe.BufferedContext;
import sun.java2d.pipe.RenderQueue;
import sun.java2d.pipe.hw.AccelDeviceEventListener;
import sun.java2d.pipe.hw.AccelGraphicsConfig;
import sun.java2d.pipe.hw.AccelSurface;
import static java.awt.Transparency.*;
import java.lang.reflect.Field;
import static sun.java2d.pipe.hw.AccelSurface.*;
import static sun.java2d.pipe.hw.ContextCapabilities.*;

public class RSLAPITest {
    private static volatile boolean failed = false;

    public static void main(String[] args) {
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        testGC(gc);

        if (failed) {
            throw new RuntimeException("Test FAILED. See err output for more");
        }

        System.out.println("Test PASSED.");
    }

    private static void testInvalidType(AccelSurface surface, int type) {
        long ret = surface.getNativeResource(type);
        System.out.printf("  getNativeResource(%d)=0x%x\n", type, ret);
        if (ret != 0l) {
            System.err.printf(
                    "FAILED: surface.getNativeResource(%d) returned" +
                    " 0x%s. It should have have returned 0L\n",
                    type, ret);
            failed = true;
        }
    }

    private static void testD3DDeviceResourceField(final AccelSurface surface) {
        try {
            Class d3dc = Class.forName("sun.java2d.d3d.D3DSurfaceData");
            if (d3dc.isInstance(surface)) {
                Field f = d3dc.getDeclaredField("D3D_DEVICE_RESOURCE");
                f.setAccessible(true);
                int d3dDR = (Integer)f.get(null);

                System.out.printf(
                        "  getNativeResource(D3D_DEVICE_RESOURCE)=0x%x\n",
                        surface.getNativeResource(d3dDR));
            }
        } catch (ClassNotFoundException e) {}
        catch (IllegalAccessException e) {}
        catch (NoSuchFieldException e) {
            System.err.println("Failed: D3DSurfaceData.D3D_DEVICE_RESOURCE" +
                               " field not found!");
            failed = true;
        }
    }

    private static void printSurface(Surface s) {
        if (s instanceof AccelSurface) {
            final AccelSurface surface = (AccelSurface) s;
            System.out.println(" Accel Surface: ");
            System.out.println("  type=" + surface.getType());
            System.out.println("  bounds=" + surface.getBounds());
            System.out.println("  nativeBounds=" + surface.getNativeBounds());
            System.out.println("  isSurfaceLost=" + surface.isSurfaceLost());
            System.out.println("  isValid=" + surface.isValid());
            RenderQueue rq = surface.getContext().getRenderQueue();
            rq.lock();
            try {
                rq.flushAndInvokeNow(new Runnable() {
                    public void run() {
                        System.out.printf("  getNativeResource(TEXTURE)=0x%x\n",
                            surface.getNativeResource(TEXTURE));
                        System.out.printf("  getNativeResource(RT_TEXTURE)=0x%x\n",
                            surface.getNativeResource(RT_TEXTURE));
                        System.out.printf("  getNativeResource(RT_PLAIN)=0x%x\n",
                            surface.getNativeResource(RT_PLAIN));
                        System.out.printf(
                            "  getNativeResource(FLIP_BACKBUFFER)=0x%x\n",
                            surface.getNativeResource(FLIP_BACKBUFFER));

                        testD3DDeviceResourceField(surface);

                        testInvalidType(surface, -1);
                        testInvalidType(surface, -150);
                        testInvalidType(surface, 300);
                        testInvalidType(surface, Integer.MAX_VALUE);
                        testInvalidType(surface, Integer.MIN_VALUE);
                    }
                });
            } finally {
                rq.unlock();
            }
        } else {
            System.out.println("null accelerated surface");
        }
    }

    private static void printAGC(AccelGraphicsConfig agc) {
        System.out.println("Accelerated Graphics Config: " + agc);
        System.out.println("Capabilities:");
        System.out.printf("AGC caps: 0x%x\n",
                          agc.getContextCapabilities().getCaps());
        System.out.println(agc.getContextCapabilities());
    }

    private static void testGC(GraphicsConfiguration gc) {
        if (!(gc instanceof AccelGraphicsConfig)) {
            System.out.println("Test passed: no hw accelerated configs found.");
            return;
        }
        System.out.println("AccelGraphicsConfig exists, testing.");
        AccelGraphicsConfig agc = (AccelGraphicsConfig) gc;
        printAGC(agc);

        testContext(agc);

        VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);
        vi.validate(gc);
        if (vi instanceof DestSurfaceProvider) {
            System.out.println("Passed: VI is DestSurfaceProvider");
            Surface s = ((DestSurfaceProvider) vi).getDestSurface();
            if (s instanceof AccelSurface) {
                System.out.println("Passed: Obtained Accel Surface");
                printSurface((AccelSurface) s);
            }
            Graphics g = vi.getGraphics();
            if (g instanceof DestSurfaceProvider) {
                System.out.println("Passed: VI graphics is " +
                                   "DestSurfaceProvider");
                printSurface(((DestSurfaceProvider) g).getDestSurface());
            }
        } else {
            System.out.println("VI is not DestSurfaceProvider");
        }
        testVICreation(agc, CAPS_RT_TEXTURE_ALPHA, TRANSLUCENT, RT_TEXTURE);
        testVICreation(agc, CAPS_RT_TEXTURE_OPAQUE, OPAQUE, RT_TEXTURE);
        testVICreation(agc, CAPS_RT_PLAIN_ALPHA, TRANSLUCENT, RT_PLAIN);
        testVICreation(agc, agc.getContextCapabilities().getCaps(), OPAQUE,
                       TEXTURE);
        testForNPEDuringCreation(agc);
    }

    private static void testVICreation(AccelGraphicsConfig agc, int cap,
                                       int transparency, int type)
    {
        int caps = agc.getContextCapabilities().getCaps();
        int w = 11, h = 17;

        VolatileImage vi =
            agc.createCompatibleVolatileImage(w, h, transparency, type);
        if ((cap & caps) != 0) {
            if (vi == null) {
                System.out.printf("Failed: cap=%d is supported but " +
                                  "image wasn't created\n", cap);
                throw new RuntimeException("Failed: image wasn't created " +
                                           "for supported cap");
            } else {
                if (!(vi instanceof DestSurfaceProvider)) {
                    throw new RuntimeException("Failed: created VI is not " +
                                               "DestSurfaceProvider");
                }
                Surface s = ((DestSurfaceProvider) vi).getDestSurface();
                if (s instanceof AccelSurface) {
                    AccelSurface as = (AccelSurface) s;
                    printSurface(as);
                    if (as.getType() != type) {
                        throw new RuntimeException("Failed: returned VI is" +
                                " of incorrect type: " + as.getType() +
                                " requested type=" + type);
                    } else {
                        System.out.printf("Passed: VI of type %d was " +
                                "created for cap=%d\n", type, cap);
                    }
                    if (as.getType() == TEXTURE) {
                        boolean ex = false;
                        try {
                            Graphics g = vi.getGraphics();
                            g.dispose();
                        } catch (UnsupportedOperationException e) {
                            ex = true;
                        }
                        if (!ex) {
                            throw new RuntimeException("Failed: " +
                                "texture.getGraphics() didn't throw exception");
                        } else {
                            System.out.println("Passed: VI.getGraphics()" +
                                    " threw exception for texture-based VI");
                        }
                    }
                } else {
                    System.out.printf("Passed: VI of type %d was " +
                            "created for cap=%d but accel surface is null\n",
                            type, cap);
                }
            }
        } else {
            if (vi != null) {
                throw new RuntimeException("Failed: created VI for " +
                                           "unsupported cap=" + cap);
            }
        }
    }

    private static void testContext(final AccelGraphicsConfig agc) {
        BufferedContext c = agc.getContext();
        final AccelDeviceEventListener l = new AccelDeviceEventListener() {
            public void onDeviceDispose() {
                System.out.println("onDeviceDispose invoked");
                agc.removeDeviceEventListener(this);
            }
            public void onDeviceReset() {
                System.out.println("onDeviceReset invoked");
            }
        };
        agc.addDeviceEventListener(l);

        RenderQueue rq = c.getRenderQueue();
        rq.lock();
        try {
            c.saveState();
            rq.flushNow();
            c.restoreState();
            rq.flushNow();
            System.out.println("Passed: Save/Restore");
        } finally {
            rq.unlock();
        }
    }

    private static void testForNPEDuringCreation(AccelGraphicsConfig agc) {
        int iterations = 100;
        HashSet<VolatileImage> vis = new HashSet<VolatileImage>();
        GraphicsConfiguration gc = (GraphicsConfiguration)agc;
        Rectangle r = gc.getBounds();
        long ram = gc.getDevice().getAvailableAcceleratedMemory();
        if (ram > 0) {
            // guesstimate the number of iterations needed to exhaust vram
            int i = 2 *
                (int)(ram / (r.width * r.height * gc.getColorModel().getPixelSize()/8));
            iterations = Math.max(iterations, i);
            System.err.println("iterations="+iterations);
        }
        for (int i = 0; i < iterations; i++) {
            VolatileImage vi =
                agc.createCompatibleVolatileImage(r.width, r.height,
                                                  Transparency.OPAQUE,
                                                  AccelSurface.RT_PLAIN);
            if (vi == null) {
                break;
            }
            vis.add(vi);
        }
        for (VolatileImage vi : vis) {
            vi.flush();
        }
        vis = null;

        System.out.println("Passed: testing for possible NPEs " +
                           "during VI creation");
    }
}