1 /*
   2  * Copyright (c) 2007, 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 
  41 package j2dbench.tests;
  42 
  43 import j2dbench.Destinations;
  44 import j2dbench.Group;
  45 import j2dbench.Modifier;
  46 import j2dbench.Option;
  47 import j2dbench.Test;
  48 import j2dbench.Result;
  49 import j2dbench.TestEnvironment;
  50 import java.awt.Graphics2D;
  51 import java.awt.Color;
  52 import java.awt.GraphicsConfiguration;
  53 import java.awt.GraphicsEnvironment;
  54 import java.awt.image.VolatileImage;
  55 import java.awt.image.BufferedImage;
  56 import java.awt.image.DataBuffer;
  57 import java.awt.image.DataBufferByte;
  58 import java.awt.image.DataBufferShort;
  59 import java.awt.image.DataBufferInt;
  60 import java.awt.image.Raster;
  61 import java.awt.image.WritableRaster;
  62 import java.awt.image.IndexColorModel;
  63 
  64 public abstract class PixelTests extends Test {
  65     static Group pixelroot;
  66 
  67     static Group pixeloptroot;
  68     static Group.EnableSet bufimgsrcroot;
  69 
  70     static Option doRenderTo;
  71     static Option doRenderFrom;
  72 
  73     static Group bufimgtestroot;
  74     static Group rastertestroot;
  75     static Group dbtestroot;
  76 
  77     public static void init() {
  78         pixelroot = new Group("pixel", "Pixel Access Benchmarks");
  79 
  80         pixeloptroot = new Group(pixelroot, "opts", "Pixel Access Options");
  81         doRenderTo = new Option.Toggle(pixeloptroot, "renderto",
  82                                        "Render to Image before test",
  83                                        Option.Toggle.Off);
  84         doRenderFrom = new Option.Toggle(pixeloptroot, "renderfrom",
  85                                          "Render from Image before test",
  86                                          Option.Toggle.Off);
  87 
  88         // BufferedImage Sources
  89         bufimgsrcroot = new Group.EnableSet(pixelroot, "src",
  90                                             "BufferedImage Sources");
  91         new BufImg(BufferedImage.TYPE_BYTE_BINARY, 1);
  92         new BufImg(BufferedImage.TYPE_BYTE_BINARY, 2);
  93         new BufImg(BufferedImage.TYPE_BYTE_BINARY, 4);
  94         new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
  95         new BufImg(BufferedImage.TYPE_BYTE_GRAY);
  96         new BufImg(BufferedImage.TYPE_USHORT_555_RGB);
  97         new BufImg(BufferedImage.TYPE_USHORT_565_RGB);
  98         new BufImg(BufferedImage.TYPE_USHORT_GRAY);
  99         new BufImg(BufferedImage.TYPE_3BYTE_BGR);
 100         new BufImg(BufferedImage.TYPE_4BYTE_ABGR);
 101         new BufImg(BufferedImage.TYPE_INT_RGB);
 102         new BufImg(BufferedImage.TYPE_INT_BGR);
 103         new BufImg(BufferedImage.TYPE_INT_ARGB);
 104 
 105         // BufferedImage Tests
 106         bufimgtestroot = new Group(pixelroot, "bimgtests",
 107                                    "BufferedImage Tests");
 108         new BufImgTest.GetRGB();
 109         new BufImgTest.SetRGB();
 110 
 111         // Raster Tests
 112         rastertestroot = new Group(pixelroot, "rastests",
 113                                    "Raster Tests");
 114         new RasTest.GetDataElements();
 115         new RasTest.SetDataElements();
 116         new RasTest.GetPixel();
 117         new RasTest.SetPixel();
 118 
 119         // DataBuffer Tests
 120         dbtestroot = new Group(pixelroot, "dbtests",
 121                                "DataBuffer Tests");
 122         new DataBufTest.GetElem();
 123         new DataBufTest.SetElem();
 124     }
 125 
 126     public PixelTests(Group root, String nodeName, String description) {
 127         super(root, nodeName, description);
 128         addDependency(bufimgsrcroot);
 129         addDependencies(pixeloptroot, false);
 130     }
 131 
 132     public static class Context {
 133         BufferedImage bimg;
 134         WritableRaster ras;
 135         DataBuffer db;
 136         int pixeldata[];
 137         Object elemdata;
 138     }
 139 
 140     public Object initTest(TestEnvironment env, Result result) {
 141         Context ctx = new Context();
 142         ctx.bimg = ((BufImg) env.getModifier(bufimgsrcroot)).getImage();
 143         if (env.isEnabled(doRenderTo)) {
 144             Graphics2D g2d = ctx.bimg.createGraphics();
 145             g2d.setColor(Color.white);
 146             g2d.fillRect(3, 0, 1, 1);
 147             g2d.dispose();
 148         }
 149         if (env.isEnabled(doRenderFrom)) {
 150             GraphicsConfiguration cfg =
 151                 GraphicsEnvironment
 152                 .getLocalGraphicsEnvironment()
 153                 .getDefaultScreenDevice()
 154                 .getDefaultConfiguration();
 155             VolatileImage vimg = cfg.createCompatibleVolatileImage(8, 1);
 156             vimg.validate(cfg);
 157             Graphics2D g2d = vimg.createGraphics();
 158             for (int i = 0; i < 100; i++) {
 159                 g2d.drawImage(ctx.bimg, 0, 0, null);
 160             }
 161             g2d.dispose();
 162             vimg.flush();
 163         }
 164         result.setUnits(1);
 165         result.setUnitName(getUnitName());
 166         return ctx;
 167     }
 168 
 169     public abstract String getUnitName();
 170 
 171     public void cleanupTest(TestEnvironment env, Object context) {
 172     }
 173 
 174     public static class BufImg extends Option.Enable {
 175         public static int rgbvals[] = {
 176             0x00000000,
 177             0xff0000ff,
 178             0x8000ff00,
 179             0xffffffff
 180         };
 181 
 182         static int cmap[] = {
 183             0xff000000,  // 0: opaque black
 184             0xffffffff,  // 1: opaque white
 185 
 186             0x00000000,  // 2: transparent black
 187             0x80ffffff,  // 3: translucent white
 188 
 189             0x00ffffff,  // 4: transparent white
 190             0x80000000,  // 5: translucent black
 191             0xff333333,  // 6: opaque dark gray
 192             0xff666666,  // 7: opaque medium gray
 193             0xff999999,  // 8: opaque gray
 194             0xffcccccc,  // 9: opaque light gray
 195             0xff0000ff,  // A: opaque blue
 196             0xff00ff00,  // B: opaque green
 197             0xff00ffff,  // C: opaque cyan
 198             0xffff0000,  // D: opaque red
 199             0xffff00ff,  // E: opaque magenta
 200             0xffffff00,  // F: opaque yellow
 201         };
 202 
 203         int type;
 204         int nbits;
 205 
 206         public BufImg(int type) {
 207             super(bufimgsrcroot,
 208                   Destinations.BufImg.ShortNames[type],
 209                   Destinations.BufImg.Descriptions[type], false);
 210             this.type = type;
 211             this.nbits = 0;
 212         }
 213 
 214         public BufImg(int type, int nbits) {
 215             super(bufimgsrcroot,
 216                   nbits+"BitBinary",
 217                   nbits+"-bit Binary Image", false);
 218             this.type = type;
 219             this.nbits = nbits;
 220         }
 221 
 222         public String getModifierValueName(Object val) {
 223             return "BufImg("+getNodeName()+")";
 224         }
 225 
 226         public BufferedImage getImage() {
 227             BufferedImage bimg;
 228             if (nbits == 0) {
 229                 bimg = new BufferedImage(8, 1, type);
 230             } else {
 231                 IndexColorModel icm =
 232                     new IndexColorModel(nbits, (1 << nbits),
 233                                         cmap, 0, (nbits > 1), -1,
 234                                         DataBuffer.TYPE_BYTE);
 235                 // Note that this constructor has bugs pre 1.4...
 236                 // bimg = new BufferedImage(64/nbits, 1, type, icm);
 237                 WritableRaster wr =
 238                     icm.createCompatibleWritableRaster(64/nbits, 1);
 239                 bimg = new BufferedImage(icm, wr, false, null);
 240             }
 241             for (int i = 0; i < bimg.getWidth(); i++) {
 242                 bimg.setRGB(i, 0, rgbvals[i&3]);
 243             }
 244             return bimg;
 245         }
 246     }
 247 
 248     public static abstract class BufImgTest extends PixelTests {
 249         public BufImgTest(String nodeName, String description) {
 250             super(bufimgtestroot, nodeName, description);
 251         }
 252 
 253         public String getUnitName() {
 254             return "pixel";
 255         }
 256 
 257         public static class GetRGB extends BufImgTest {
 258             public GetRGB() {
 259                 super("getrgb", "BufferedImage.getRGB(x, y)");
 260             }
 261 
 262             public void runTest(Object context, int numReps) {
 263                 BufferedImage bimg = ((Context) context).bimg;
 264                 do {
 265                     bimg.getRGB(numReps&7, 0);
 266                 } while (--numReps > 0);
 267             }
 268         }
 269 
 270         public static class SetRGB extends BufImgTest {
 271             public SetRGB() {
 272                 super("setrgb", "BufferedImage.setRGB(x, y, rgb)");
 273             }
 274 
 275             public void runTest(Object context, int numReps) {
 276                 BufferedImage bimg = ((Context) context).bimg;
 277                 do {
 278                     bimg.setRGB(numReps&7, 0, BufImg.rgbvals[numReps&3]);
 279                 } while (--numReps > 0);
 280             }
 281         }
 282     }
 283 
 284     public static abstract class RasTest extends PixelTests {
 285         public RasTest(String nodeName, String description) {
 286             super(rastertestroot, nodeName, description);
 287         }
 288 
 289         public String getUnitName() {
 290             return "pixel";
 291         }
 292 
 293         public Object initTest(TestEnvironment env, Result result) {
 294             Context ctx = (Context) super.initTest(env, result);
 295             ctx.ras = ctx.bimg.getRaster();
 296             ctx.pixeldata = ctx.ras.getPixel(0, 0, (int[]) null);
 297             ctx.elemdata = ctx.ras.getDataElements(0, 0, null);
 298             return ctx;
 299         }
 300 
 301         public static class GetDataElements extends RasTest {
 302             public GetDataElements() {
 303                 super("getdataelem", "Raster.getDataElements(x, y, o)");
 304             }
 305 
 306             public void runTest(Object context, int numReps) {
 307                 Raster ras = ((Context) context).ras;
 308                 Object elemdata = ((Context) context).elemdata;
 309                 do {
 310                     ras.getDataElements(numReps&7, 0, elemdata);
 311                 } while (--numReps > 0);
 312             }
 313         }
 314 
 315         public static class SetDataElements extends RasTest {
 316             public SetDataElements() {
 317                 super("setdataelem", "WritableRaster.setDataElements(x, y, o)");
 318             }
 319 
 320             public void runTest(Object context, int numReps) {
 321                 WritableRaster ras = ((Context) context).ras;
 322                 Object elemdata = ((Context) context).elemdata;
 323                 do {
 324                     ras.setDataElements(numReps&7, 0, elemdata);
 325                 } while (--numReps > 0);
 326             }
 327         }
 328 
 329         public static class GetPixel extends RasTest {
 330             public GetPixel() {
 331                 super("getpixel", "Raster.getPixel(x, y, v[])");
 332             }
 333 
 334             public void runTest(Object context, int numReps) {
 335                 Raster ras = ((Context) context).ras;
 336                 int pixeldata[] = ((Context) context).pixeldata;
 337                 do {
 338                     ras.getPixel(numReps&7, 0, pixeldata);
 339                 } while (--numReps > 0);
 340             }
 341         }
 342 
 343         public static class SetPixel extends RasTest {
 344             public SetPixel() {
 345                 super("setpixel", "WritableRaster.setPixel(x, y, v[])");
 346             }
 347 
 348             public void runTest(Object context, int numReps) {
 349                 WritableRaster ras = ((Context) context).ras;
 350                 int pixeldata[] = ((Context) context).pixeldata;
 351                 do {
 352                     ras.setPixel(numReps&7, 0, pixeldata);
 353                 } while (--numReps > 0);
 354             }
 355         }
 356     }
 357 
 358     public static abstract class DataBufTest extends PixelTests {
 359         public DataBufTest(String nodeName, String description) {
 360             super(dbtestroot, nodeName, description);
 361         }
 362 
 363         public String getUnitName() {
 364             return "element";
 365         }
 366 
 367         public Object initTest(TestEnvironment env, Result result) {
 368             Context ctx = (Context) super.initTest(env, result);
 369             ctx.db = ctx.bimg.getRaster().getDataBuffer();
 370             return ctx;
 371         }
 372 
 373         public static class GetElem extends DataBufTest {
 374             public GetElem() {
 375                 super("getelem", "DataBuffer.getElem(i)");
 376             }
 377 
 378             public void runTest(Object context, int numReps) {
 379                 DataBuffer db = ((Context) context).db;
 380                 do {
 381                     db.getElem(numReps&7);
 382                 } while (--numReps > 0);
 383             }
 384         }
 385 
 386         public static class SetElem extends DataBufTest {
 387             public SetElem() {
 388                 super("setelem", "DataBuffer.setElem(i, v)");
 389             }
 390 
 391             public void runTest(Object context, int numReps) {
 392                 DataBuffer db = ((Context) context).db;
 393                 do {
 394                     db.setElem(numReps&7, 0);
 395                 } while (--numReps > 0);
 396             }
 397         }
 398     }
 399 }