1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package marlin;
  24 
  25 import java.awt.BasicStroke;
  26 import java.awt.Color;
  27 import java.awt.Graphics2D;
  28 import java.awt.RenderingHints;
  29 import java.awt.geom.Path2D;
  30 import static java.awt.geom.Path2D.WIND_NON_ZERO;
  31 import java.awt.image.BufferedImage;
  32 import java.io.File;
  33 import java.io.IOException;
  34 import javax.imageio.ImageIO;
  35 import sun.java2d.pipe.RenderingEngine;
  36 
  37 /**
  38  * Simple crash rendering test using huge GeneralPaths with marlin renderer
  39  *
  40  * run it with large heap (2g):
  41  * java -Dsun.java2d.renderer=sun.java2d.marlin.MarlinRenderingEngine marlin.CrashTest
  42  *
  43  * @author bourgesl
  44  */
  45 public class CrashTest {
  46 
  47     static final boolean SAVE_IMAGE = false;
  48     static boolean USE_ROUND_CAPS_AND_JOINS = true;
  49 
  50     public static void main(String[] args) {
  51         // try insane image sizes:
  52 
  53         // subpixel coords may overflow:
  54 //        testHugeImage((Integer.MAX_VALUE >> 3) + 1, 6);
  55         // larger than 23 bits: (RLE)
  56         testHugeImage(8388608 + 1, 10);
  57 
  58         test(0.1f, false, 0);
  59         test(0.1f, true, 7f);
  60 
  61         // Exceed 2Gb OffHeap buffer for edges:
  62         try {
  63             USE_ROUND_CAPS_AND_JOINS = true;
  64             test(0.1f, true, 0.1f);
  65             System.out.println("Exception MISSING.");
  66         }
  67         catch (Throwable th) {
  68             if (th instanceof ArrayIndexOutOfBoundsException) {
  69                 System.out.println("ArrayIndexOutOfBoundsException expected.");
  70             } else {
  71                 System.out.println("Exception occured:");
  72                 th.printStackTrace();
  73             }
  74         }
  75 
  76     }
  77 
  78     private static void test(final float lineStroke,
  79                              final boolean useDashes,
  80                              final float dashMinLen)
  81     throws ArrayIndexOutOfBoundsException
  82     {
  83         System.out.println("---\n" + "test: "
  84             + "lineStroke=" + lineStroke
  85             + ", useDashes=" + useDashes
  86             +", dashMinLen=" + dashMinLen
  87         );
  88 
  89         final String renderer = RenderingEngine.getInstance().getClass().getSimpleName();
  90         System.out.println("Testing renderer = " + renderer);
  91 
  92         final BasicStroke stroke = createStroke(lineStroke, useDashes, dashMinLen);
  93 
  94         // TODO: test Dasher.firstSegmentsBuffer resizing ?
  95 // array.dasher.firstSegmentsBuffer.d_float[2] sum: 6 avg: 3.0 [3 | 3]
  96         /*
  97          // Marlin growable arrays:
  98          = new StatLong("array.dasher.firstSegmentsBuffer.d_float");
  99          = new StatLong("array.stroker.polystack.curves.d_float");
 100          = new StatLong("array.stroker.polystack.curveTypes.d_byte");
 101          = new StatLong("array.marlincache.rowAAChunk.d_byte");
 102          = new StatLong("array.marlincache.touchedTile.int");
 103          = new StatLong("array.renderer.alphaline.int");
 104          = new StatLong("array.renderer.crossings.int");
 105          = new StatLong("array.renderer.aux_crossings.int");
 106          = new StatLong("array.renderer.edgeBuckets.int");
 107          = new StatLong("array.renderer.edgeBucketCounts.int");
 108          = new StatLong("array.renderer.edgePtrs.int");
 109          = new StatLong("array.renderer.aux_edgePtrs.int");
 110          */
 111         // size > 8192 (exceed both tile and buckets arrays)
 112         final int size = 9000;
 113         System.out.println("image size = " + size);
 114 
 115         final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
 116 
 117         final Graphics2D g2d = (Graphics2D) image.getGraphics();
 118         try {
 119             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 120             g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 121 
 122             g2d.setClip(0, 0, size, size);
 123             g2d.setBackground(Color.WHITE);
 124             g2d.clearRect(0, 0, size, size);
 125 
 126             g2d.setStroke(stroke);
 127             g2d.setColor(Color.BLACK);
 128 
 129             final long start = System.nanoTime();
 130 
 131             paint(g2d, size - 10f);
 132 
 133             final long time = System.nanoTime() - start;
 134 
 135             System.out.println("paint: duration= " + (1e-6 * time) + " ms.");
 136 
 137             if (SAVE_IMAGE) {
 138                 try {
 139                     final File file = new File("CrashTest-" + renderer + "-dash-" + useDashes + ".bmp");
 140 
 141                     System.out.println("Writing file: " + file.getAbsolutePath());
 142                     ImageIO.write(image, "BMP", file);
 143                 } catch (IOException ex) {
 144                     System.out.println("Writing file failure:");
 145                     ex.printStackTrace();
 146                 }
 147             }
 148         } finally {
 149             g2d.dispose();
 150         }
 151     }
 152 
 153     private static void testHugeImage(final int width, final int height)
 154     throws ArrayIndexOutOfBoundsException
 155     {
 156         System.out.println("---\n" + "testHugeImage: "
 157             + "width=" + width
 158             + ", height=" + height
 159         );
 160 
 161         final String renderer = RenderingEngine.getInstance().getClass().getSimpleName();
 162         System.out.println("Testing renderer = " + renderer);
 163 
 164         final BasicStroke stroke = createStroke(2.5f, false, 0);
 165 
 166         // size > 24bits (exceed both tile and buckets arrays)
 167         System.out.println("image size = " + width + " x "+height);
 168 
 169         final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
 170 
 171         final Graphics2D g2d = (Graphics2D) image.getGraphics();
 172         try {
 173             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 174             g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 175 
 176             g2d.setBackground(Color.WHITE);
 177             g2d.clearRect(0, 0, width, height);
 178 
 179             g2d.setStroke(stroke);
 180             g2d.setColor(Color.BLACK);
 181 
 182             final Path2D.Float path = new Path2D.Float(WIND_NON_ZERO, 32);
 183             path.moveTo(0, 0);
 184             path.lineTo(width, 0);
 185             path.lineTo(width, height);
 186             path.lineTo(0, height);
 187             path.lineTo(0, 0);
 188 
 189             final long start = System.nanoTime();
 190 
 191             g2d.draw(path);
 192 
 193             final long time = System.nanoTime() - start;
 194 
 195             System.out.println("paint: duration= " + (1e-6 * time) + " ms.");
 196 
 197             if (SAVE_IMAGE) {
 198                 try {
 199                     final File file = new File("CrashTest-" + renderer +
 200                                                "-huge-" + width + "x" +height + ".bmp");
 201 
 202                     System.out.println("Writing file: " + file.getAbsolutePath());
 203                     ImageIO.write(image, "BMP", file);
 204                 } catch (IOException ex) {
 205                     System.out.println("Writing file failure:");
 206                     ex.printStackTrace();
 207                 }
 208             }
 209         } finally {
 210             g2d.dispose();
 211         }
 212     }
 213 
 214     private static void paint(final Graphics2D g2d, final float size) {
 215         final double halfSize = size / 2.0;
 216 
 217         final Path2D.Float path = new Path2D.Float(WIND_NON_ZERO, 32 * 1024);
 218 
 219         // show cross:
 220         path.moveTo(0, 0);
 221         path.lineTo(size, size);
 222 
 223         path.moveTo(size, 0);
 224         path.lineTo(0, size);
 225 
 226         path.moveTo(0, 0);
 227         path.lineTo(size, 0);
 228 
 229         path.moveTo(0, 0);
 230         path.lineTo(0, size);
 231 
 232         path.moveTo(0, 0);
 233 
 234         double r = size;
 235 
 236         final int ratio = 100;
 237         int repeats = 1;
 238 
 239         int n = 0;
 240 
 241         while (r > 1.0) {
 242             repeats *= ratio;
 243 
 244             if (repeats > 10000) {
 245                 repeats = 10000;
 246             }
 247 
 248             for (int i = 0; i < repeats; i++) {
 249                 path.lineTo(halfSize - 0.5 * r + i * r / repeats,
 250                             halfSize - 0.5 * r);
 251                 n++;
 252                 path.lineTo(halfSize - 0.5 * r + i * r / repeats + 0.1,
 253                             halfSize + 0.5 * r);
 254                 n++;
 255             }
 256 
 257             r -= halfSize;
 258         }
 259         System.out.println("draw : " + n + " lines.");
 260         g2d.draw(path);
 261     }
 262 
 263     private static BasicStroke createStroke(final float width,
 264                                             final boolean useDashes,
 265                                             final float dashMinLen) {
 266         final float[] dashes;
 267 
 268         if (useDashes) {
 269             // huge dash array (exceed Dasher.INITIAL_ARRAY)
 270             dashes = new float[512];
 271 
 272             float cur = dashMinLen;
 273             float step = 0.01f;
 274 
 275             for (int i = 0; i < dashes.length; i += 2) {
 276                 dashes[i] = cur;
 277                 dashes[i + 1] = cur;
 278                 cur += step;
 279             }
 280         } else {
 281             dashes = null;
 282         }
 283 
 284         if (USE_ROUND_CAPS_AND_JOINS) {
 285             // Use both round Caps & Joins:
 286             return new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 100.0f, dashes, 0.0f);
 287         }
 288         return new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 100.0f, dashes, 0.0f);
 289     }
 290 }