1 /* 2 * Copyright (c) 2002, 2020, 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 41 package j2dbench; 42 43 import java.awt.Component; 44 import java.awt.Frame; 45 import java.awt.GraphicsConfiguration; 46 import java.awt.Image; 47 import java.awt.Polygon; 48 import java.awt.Transparency; 49 import java.awt.color.ColorSpace; 50 import java.awt.image.BufferedImage; 51 import java.awt.image.ComponentColorModel; 52 import java.awt.image.DataBuffer; 53 import java.awt.image.WritableRaster; 54 55 import javax.swing.SwingUtilities; 56 57 import j2dbench.tests.GraphicsTests; 58 import j2dbench.tests.ImageTests; 59 60 public abstract class Destinations extends Option.Enable { 61 public static Group.EnableSet destroot; 62 public static Group frameroot; 63 public static Group bufimgdestroot; 64 public static Group compatimgdestroot; 65 public static Group volimgdestroot; 66 67 public static void init() { 68 destroot = new Group.EnableSet(TestEnvironment.globaloptroot, 69 "dest", "Output Destination Options"); 70 71 new OffScreen(); 72 73 frameroot = new Group.EnableSet(destroot, "frame", "Output to Frame"); 74 frameroot.setHorizontal(); 75 76 new Screen(false, false); 77 if (ImageTests.hasOpacityWindow) { 78 new Screen(true, false); 79 } 80 if (ImageTests.hasShapedWindow) { 81 new Screen(false, true); 82 } 83 if (ImageTests.hasShapedWindow && ImageTests.hasOpacityWindow) { 84 new Screen(true, true); 85 } 86 87 if (GraphicsTests.hasGraphics2D) { 88 if (ImageTests.hasCompatImage) { 89 compatimgdestroot = 90 new Group.EnableSet(destroot, "compatimg", 91 "Compatible Image Destinations"); 92 compatimgdestroot.setHorizontal(); 93 94 new CompatImg(); 95 new CompatImg(Transparency.OPAQUE); 96 new CompatImg(Transparency.BITMASK); 97 new CompatImg(Transparency.TRANSLUCENT); 98 } 99 100 if (ImageTests.hasVolatileImage) { 101 volimgdestroot = new Group.EnableSet(destroot, "volimg", 102 "Output to Volatile Image"); 103 104 volimgdestroot.setHorizontal(); 105 new VolatileImg(); 106 if (ImageTests.hasTransparentVolatileImage) { 107 new VolatileImg(Transparency.OPAQUE); 108 new VolatileImg(Transparency.BITMASK); 109 new VolatileImg(Transparency.TRANSLUCENT); 110 } 111 } 112 113 bufimgdestroot = new Group.EnableSet(destroot, "bufimg", 114 "BufferedImage Destinations"); 115 116 new BufImg(BufferedImage.TYPE_INT_RGB); 117 new BufImg(BufferedImage.TYPE_INT_ARGB); 118 new BufImg(BufferedImage.TYPE_INT_ARGB_PRE); 119 new BufImg(BufferedImage.TYPE_3BYTE_BGR); 120 new BufImg(BufferedImage.TYPE_BYTE_INDEXED); 121 new BufImg(BufferedImage.TYPE_BYTE_GRAY); 122 new BufImg(BufferedImage.TYPE_4BYTE_ABGR); 123 new BufImg(BufferedImage.TYPE_4BYTE_ABGR_PRE); 124 new CustomImg(); 125 } 126 } 127 128 public Destinations(Group parent, 129 String nodename, String description, 130 boolean defenabled) 131 { 132 super(parent, nodename, description, defenabled); 133 } 134 135 public void modifyTest(TestEnvironment env) { 136 setDestination(env); 137 } 138 139 public void restoreTest(TestEnvironment env) { 140 env.setTestImage(null); 141 } 142 143 public String getAbbreviatedModifierDescription(Object val) { 144 return "to "+getModifierValueName(val); 145 } 146 147 public abstract void setDestination(TestEnvironment env); 148 149 public static class Screen extends Destinations { 150 151 private boolean opacity; 152 private boolean shaped; 153 154 public Screen(boolean opacity, boolean shaped) { 155 super(frameroot, getDescription(opacity,shaped), 156 getLongDescription(opacity,shaped), false); 157 this.opacity = opacity; 158 this.shaped = shaped; 159 } 160 161 private static String getDescription(boolean opacity, boolean shaped){ 162 if (opacity && shaped) { 163 return "shapedtransframe"; 164 } 165 if (shaped) { 166 return "shapedframe"; 167 } 168 if (opacity) { 169 return "transframe"; 170 } 171 return "defaultframe"; 172 } 173 174 private static String getLongDescription(boolean opacity, boolean shaped){ 175 if (opacity && shaped) { 176 return "Translucent and Shaped"; 177 } 178 if (shaped) { 179 return "Shaped"; 180 } 181 if (opacity) { 182 return "Translucent"; 183 } 184 return "Default"; 185 } 186 187 public String getModifierValueName(Object val) { 188 if (opacity && shaped) { 189 return "Translucent and Shaped Frame"; 190 } 191 if (shaped) { 192 return "Shaped Frame"; 193 } 194 if (opacity) { 195 return "Translucent Frame"; 196 } 197 return "Default Frame"; 198 } 199 200 public void setDestination(TestEnvironment env) { 201 env.setTestImage(null); 202 } 203 204 public void modifyTest(TestEnvironment env) { 205 setDestination(env); 206 Frame frame = (Frame) SwingUtilities.getWindowAncestor(env.comp); 207 if (frame != null && (opacity || shaped)) { 208 frame.dispose(); 209 frame.setUndecorated(true); 210 int w = frame.getWidth(); 211 int h = frame.getHeight(); 212 if (shaped) { 213 Polygon p = new Polygon(); 214 p.addPoint(0, 0); 215 p.addPoint(w, 0); 216 p.addPoint(0, h); 217 p.addPoint(w, h); 218 p.addPoint(0, 0); 219 frame.setShape(p); 220 } 221 if (opacity) { 222 frame.setOpacity(0.5f); 223 } 224 frame.setVisible(true); 225 } 226 } 227 228 public void restoreTest(TestEnvironment env) { 229 env.setTestImage(null); 230 Frame frame = (Frame) SwingUtilities.getWindowAncestor(env.comp); 231 if (frame != null && (opacity || shaped)) { 232 frame.dispose(); 233 frame.setShape(null); 234 frame.setOpacity(1); 235 frame.setUndecorated(false); 236 frame.setVisible(true); 237 } 238 } 239 } 240 241 public static class OffScreen extends Destinations { 242 public OffScreen() { 243 super(destroot, "offscreen", "Output to OffScreen Image", false); 244 } 245 246 public String getModifierValueName(Object val) { 247 return "OffScreen"; 248 } 249 250 public void setDestination(TestEnvironment env) { 251 Component c = env.getCanvas(); 252 env.setTestImage(c.createImage(env.getWidth(), env.getHeight())); 253 } 254 } 255 256 public static class CompatImg extends Destinations { 257 int transparency; 258 259 public static String[] ShortNames = { 260 "compatimg", 261 "opqcompatimg", 262 "bmcompatimg", 263 "transcompatimg", 264 }; 265 266 public static String[] ShortDescriptions = { 267 "Default", 268 "Opaque", 269 "Bitmask", 270 "Translucent", 271 }; 272 273 public static String[] LongDescriptions = { 274 "Default Compatible Image", 275 "Opaque Compatible Image", 276 "Bitmask Compatible Image", 277 "Translucent Compatible Image", 278 }; 279 280 public static String[] ModifierNames = { 281 "CompatImage()", 282 "CompatImage(Opaque)", 283 "CompatImage(Bitmask)", 284 "CompatImage(Translucent)", 285 }; 286 287 public CompatImg() { 288 this(0); 289 } 290 291 public CompatImg(int transparency) { 292 super(compatimgdestroot, 293 ShortNames[transparency], 294 ShortDescriptions[transparency], 295 false); 296 this.transparency = transparency; 297 } 298 299 public String getModifierValueName(Object val) { 300 return ModifierNames[transparency]; 301 } 302 303 public void setDestination(TestEnvironment env) { 304 Component c = env.getCanvas(); 305 GraphicsConfiguration gc = c.getGraphicsConfiguration(); 306 int w = env.getWidth(); 307 int h = env.getHeight(); 308 if (transparency == 0) { 309 env.setTestImage(gc.createCompatibleImage(w, h)); 310 } else { 311 env.setTestImage(gc.createCompatibleImage(w, h, transparency)); 312 } 313 } 314 } 315 316 public static class VolatileImg extends Destinations { 317 private final int transparency; 318 319 public static final String[] ShortNames = { 320 "volimg", 321 "opqvolimg", 322 "bmvolimg", 323 "transvolimg", 324 }; 325 326 public static final String[] ShortDescriptions = { 327 "Default", 328 "Opaque", 329 "Bitmask", 330 "Translucent", 331 }; 332 333 public static final String[] LongDescriptions = { 334 "Default VolatileImg Image", 335 "Opaque VolatileImg Image", 336 "Bitmask VolatileImg Image", 337 "Translucent VolatileImg Image", 338 }; 339 340 public static final String[] ModifierNames = { 341 "VolatileImg()", 342 "VolatileImg(Opaque)", 343 "VolatileImg(Bitmask)", 344 "VolatileImg(Translucent)", 345 }; 346 347 public VolatileImg() { 348 this(0); 349 } 350 351 public VolatileImg(final int transparency) { 352 super(volimgdestroot, 353 ShortNames[transparency], 354 ShortDescriptions[transparency], 355 false); 356 this.transparency = transparency; 357 } 358 359 public String getModifierValueName(final Object val) { 360 return ModifierNames[transparency]; 361 } 362 363 public void setDestination(final TestEnvironment env) { 364 Component c = env.getCanvas(); 365 GraphicsConfiguration gc = c.getGraphicsConfiguration(); 366 int w = env.getWidth(); 367 int h = env.getHeight(); 368 if (transparency == 0) { 369 env.setTestImage(gc.createCompatibleVolatileImage(w, h)); 370 } else { 371 env.setTestImage(gc.createCompatibleVolatileImage(w, h, transparency)); 372 } 373 } 374 } 375 376 377 public static class BufImg extends Destinations { 378 int type; 379 Image img; 380 381 public static String[] ShortNames = { 382 "custom", 383 "IntXrgb", 384 "IntArgb", 385 "IntArgbPre", 386 "IntXbgr", 387 "3ByteBgr", 388 "4ByteAbgr", 389 "4ByteAbgrPre", 390 "Short565", 391 "Short555", 392 "ByteGray", 393 "ShortGray", 394 "ByteBinary", 395 "ByteIndexed", 396 }; 397 398 public static String[] Descriptions = { 399 "Custom Image", 400 "32-bit XRGB Packed Image", 401 "32-bit ARGB Packed Image", 402 "32-bit ARGB Alpha Premultiplied Packed Image", 403 "32-bit XBGR Packed Image", 404 "3-byte BGR Component Image", 405 "4-byte ABGR Component Image", 406 "4-byte ABGR Alpha Premultiplied Component Image", 407 "16-bit 565 RGB Packed Image", 408 "15-bit 555 RGB Packed Image", 409 "8-bit Grayscale Image", 410 "16-bit Grayscale Image", 411 "1-bit Binary Image", 412 "8-bit Indexed Image", 413 }; 414 415 public BufImg(int type) { 416 super(bufimgdestroot, ShortNames[type], Descriptions[type], false); 417 this.type = type; 418 } 419 420 public String getModifierValueName(Object val) { 421 return "BufImg("+getNodeName()+")"; 422 } 423 424 public void setDestination(TestEnvironment env) { 425 if (img == null) { 426 img = new BufferedImage(env.getWidth(), env.getHeight(), type); 427 } 428 env.setTestImage(img); 429 } 430 } 431 432 public static class CustomImg extends Destinations { 433 private Image img; 434 435 public CustomImg() { 436 super(bufimgdestroot, 437 "custom", 438 "Custom (3-float RGB) Image", 439 false); 440 } 441 442 public String getModifierValueName(Object val) { 443 return "CustomImg"; 444 } 445 446 public void setDestination(TestEnvironment env) { 447 if (img == null) { 448 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 449 ComponentColorModel cm = 450 new ComponentColorModel(cs, false, false, 451 Transparency.OPAQUE, 452 DataBuffer.TYPE_FLOAT); 453 WritableRaster raster = 454 cm.createCompatibleWritableRaster(env.getWidth(), 455 env.getHeight()); 456 img = new BufferedImage(cm, raster, false, null); 457 } 458 env.setTestImage(img); 459 } 460 } 461 }