Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8041895

[macosx] Slow performance drawing a BufferedImage using IndexColorModel

    Details

    • Subcomponent:
      2d
    • OS:
      os_x

      Description

      A DESCRIPTION OF THE REQUEST :
      System: Mac OS X 10.9
      Java: 1.7.0_45

      Note: Mac is running in HiDPI mode (scale factor is 2.0).

      Running my sample application to draw a buffed image (with IndexColorModel) has a poor performance in the OpenGL pipeline.
      Here is command and output of test one:

      java DrawTest1 true 800 600 10
      overall time: 1982 ms
      single-draw time: 198 ms

      A buffered image using a DirectColorModel can be drawn very fast instead.
      Here is command and output of test two:

      java DrawTest1 false 800 600 10
      overall time: 19 ms
      single-draw time: 1 ms


      JUSTIFICATION :
      Drawing a single image of 800 x 600 pixels takes approximately 200 milliseconds. Rendering an index based image using the OpenGL pipeline seems to be implemented by scaling and converting the image to an ARGB-premultiplied image that can be handled by the Java OpenGL bridge.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should somehow be possible to use a native OpenGL blit using GL_COLOR_INDEX for that scenario. This avoids additional copying of pixels in the Java code.

      ACTUAL -
      Drawing a single image of 800 x 600 pixels takes approximately 200 milliseconds.

      ---------- BEGIN SOURCE ----------
      /**
       * Sample application to test the performance of Graphics.drawImage
       * using either an index based buffer or a direct pixel based buffer.
       */
      import java.awt.Frame;
      import java.awt.Graphics;
      import java.awt.Image;
      import java.awt.color.ColorSpace;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import java.awt.image.BufferedImage;
      import java.awt.image.ColorModel;
      import java.awt.image.DataBuffer;
      import java.awt.image.DataBufferByte;
      import java.awt.image.DataBufferInt;
      import java.awt.image.DirectColorModel;
      import java.awt.image.IndexColorModel;
      import java.awt.image.Raster;
      import java.awt.image.WritableRaster;
      import java.util.Arrays;

      public class DrawTest1 {
      private static final int[] IMR_WIN_COLORMAP = {
         0xff000000, 0xff800000, 0xff008000, 0xff808000, 0xff000080, 0xff800080,
         0xff008080, 0xffc0c0c0, 0xffc0dcc0, 0xffd4d0c8, 0xff040404, 0xff080808,
         0xff0c0c0c, 0xff111111, 0xff161616, 0xff1c1c1c, 0xff222222, 0xff292929,
         0xff555555, 0xff4d4d4d, 0xff424242, 0xff393939, 0xffff7c80, 0xffff5050,
         0xffd60093, 0xffccecff, 0xffefd6c6, 0xffe7e7d6, 0xffada990, 0xff330000,
         0xff660000, 0xff990000, 0xffcc0000, 0xff003300, 0xff333300, 0xff663300,
         0xff993300, 0xffcc3300, 0xffff3300, 0xff006600, 0xff336600, 0xff666600,
         0xff996600, 0xffcc6600, 0xffff6600, 0xff009900, 0xff339900, 0xff669900,
         0xff999900, 0xffcc9900, 0xffff9900, 0xff00cc00, 0xff33cc00, 0xff66cc00,
         0xff99cc00, 0xffcccc00, 0xffffcc00, 0xff66ff00, 0xff99ff00, 0xffccff00,
         0xff000033, 0xff330033, 0xff660033, 0xff990033, 0xffcc0033, 0xffff0033,
         0xff003333, 0xff333333, 0xff663333, 0xff993333, 0xffcc3333, 0xffff3333,
         0xff006633, 0xff336633, 0xff666633, 0xff996633, 0xffcc6633, 0xffff6633,
         0xff009933, 0xff339933, 0xff669933, 0xff999933, 0xffcc9933, 0xffff9933,
         0xff00cc33, 0xff33cc33, 0xff66cc33, 0xff99cc33, 0xffcccc33, 0xffffcc33,
         0xff33ff33, 0xff66ff33, 0xff99ff33, 0xffccff33, 0xffffff33, 0xff000066,
         0xff330066, 0xff660066, 0xff990066, 0xffcc0066, 0xffff0066, 0xff003366,
         0xff333366, 0xff663366, 0xff993366, 0xffcc3366, 0xffff3366, 0xff006666,
         0xff336666, 0xff666666, 0xff996666, 0xffcc6666, 0xff009966, 0xff339966,
         0xff669966, 0xff999966, 0xffcc9966, 0xffff9966, 0xff00cc66, 0xff33cc66,
         0xff99cc66, 0xffcccc66, 0xffffcc66, 0xff00ff66, 0xff33ff66, 0xff99ff66,
         0xffccff66, 0xffff00cc, 0xffcc00ff, 0xff009999, 0xff993399, 0xff990099,
         0xffcc0099, 0xff000099, 0xff333399, 0xff660099, 0xffcc3399, 0xffff0099,
         0xff006699, 0xff336699, 0xff663399, 0xff996699, 0xffcc6699, 0xffff3399,
         0xff339999, 0xff669999, 0xff999999, 0xffcc9999, 0xffff9999, 0xff00cc99,
         0xff33cc99, 0xff66cc66, 0xff99cc99, 0xffcccc99, 0xffffcc99, 0xff00ff99,
         0xff33ff99, 0xff66cc99, 0xff99ff99, 0xffccff99, 0xffffff99, 0xff0000cc,
         0xff330099, 0xff6600cc, 0xff9900cc, 0xffcc00cc, 0xff003399, 0xff3333cc,
         0xff6633cc, 0xff9933cc, 0xffcc33cc, 0xffff33cc, 0xff0066cc, 0xff3366cc,
         0xff666699, 0xff9966cc, 0xffcc66cc, 0xffff6699, 0xff0099cc, 0xff3399cc,
         0xff6699cc, 0xff9999cc, 0xffcc99cc, 0xffff99cc, 0xff00cccc, 0xff33cccc,
         0xff66cccc, 0xff99cccc, 0xffcccccc, 0xffffcccc, 0xff00ffcc, 0xff33ffcc,
         0xff66ff99, 0xff99ffcc, 0xffccffcc, 0xffffffcc, 0xff3300cc, 0xff6600ff,
         0xff9900ff, 0xff0033cc, 0xff3333ff, 0xff6633ff, 0xff9933ff, 0xffcc33ff,
         0xffff33ff, 0xff0066ff, 0xff3366ff, 0xff6666cc, 0xff9966ff, 0xffcc66ff,
         0xffff66cc, 0xff0099ff, 0xff3399ff, 0xff6699ff, 0xff9999ff, 0xffcc99ff,
         0xffff99ff, 0xff00ccff, 0xff33ccff, 0xff66ccff, 0xff99ccff, 0xffccccff,
         0xffffccff, 0xff33ffff, 0xff66ffcc, 0xff99ffff, 0xffccffff, 0xffff6666,
         0xff66ff66, 0xffffff66, 0xff6666ff, 0xffff66ff, 0xff66ffff, 0xffa50021,
         0xff5f5f5f, 0xff777777, 0xff868686, 0xff969696, 0xffcbcbcb, 0xffb2b2b2,
         0xffd7d7d7, 0xffdddddd, 0xffe3e3e3, 0xffeaeaea, 0xfff1f1f1, 0xfff8f8f8,
         0xfffffbf0, 0xff3a6ea5, 0xff808080, 0xffff0000, 0xff00ff00, 0xffffff00,
         0xff0000ff, 0xffff00ff, 0xff00ffff, 0xffffffff
      };

      public static void main(String[] args) {
      if(args.length < 4) {
      System.out.println("Program arguments: INDEX_MODE WIDTH HEIGHT REPEAT");
      System.out.println();
      System.out.println("INDEX_MODE: specify 'true' to use a BufferedImage with IndexColorModel");
      System.out.println(" specify 'false' to use a BufferedImage with DirectColorModel");
      System.out.println("WIDTH: width of the image in pixels");
      System.out.println("HEIGHT: height of the image in pixels");
      System.out.println("REPEAT: number of repeated draw image operations");
      return;
      }
      boolean bo_use_index_mode = Boolean.parseBoolean(args[0]);
      int im_image_width = Math.max(Integer.parseInt(args[1]), 1);
      int im_image_height = Math.max(Integer.parseInt(args[2]), 1);
      int im_num_draws = Math.max(Integer.parseInt(args[3]), 1);

      Frame ds_f = new Frame();
      ds_f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent ds_e) {
      System.exit(0);
      }
      });
      ds_f.setBounds(0, 0, im_image_width, im_image_height);
      ds_f.setVisible(true);
      Graphics ds_g = ds_f.getGraphics();
      Image ds_image;
      if(bo_use_index_mode) {
      byte[] byr_buf = new byte[im_image_width*im_image_height];
      Arrays.fill(byr_buf, (byte)250);
      ColorModel ds_cml = new IndexColorModel(
      8, IMR_WIN_COLORMAP.length, IMR_WIN_COLORMAP, 0, true, -1, DataBuffer.TYPE_BYTE);
      WritableRaster ds_wr = Raster.createInterleavedRaster(new DataBufferByte(byr_buf, byr_buf.length),
      im_image_width, im_image_height, im_image_width, 1, new int[] { 0 }, null);
      ds_image = new BufferedImage(ds_cml, ds_wr, false, null);
      }
      else {
      int[] byr_buf = new int[im_image_width*im_image_height];
      Arrays.fill(byr_buf, 0xff00ff00);
      DirectColorModel ds_cml = new DirectColorModel(
      ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0xff0000, 0x00ff00, 0x0000ff, 0xff000000, true, DataBuffer.TYPE_INT);
      int[] imr_bandmasks = new int[4];
      imr_bandmasks[0] = ds_cml.getRedMask();
      imr_bandmasks[1] = ds_cml.getGreenMask();
      imr_bandmasks[2] = ds_cml.getBlueMask();
      imr_bandmasks[3] = ds_cml.getAlphaMask();
      WritableRaster ds_wr = Raster.createPackedRaster(new DataBufferInt(byr_buf, byr_buf.length),
      im_image_width, im_image_height, im_image_width, imr_bandmasks, null);
      ds_image = new BufferedImage(ds_cml, ds_wr, true, null);
      }
      long il_time1 = System.currentTimeMillis();
      for(int iml_i=0; iml_i<im_num_draws; iml_i++) {
      //System.out.println("LOOP-B");
      ds_g.drawImage(ds_image, 0, 0, null);
      //System.out.println("LOOP-E");
      }
      long il_time2 = System.currentTimeMillis();
      ds_g.dispose();
      System.out.println("overall time: " + (il_time2-il_time1) + " ms");
      System.out.println("single-draw time: " + ((il_time2-il_time1)/im_num_draws) + " ms");
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Copy the pixels of the index based pixel buffer to a direct pixel buffer and draw the direct pixel buffer with "Graphics.drawImage". However, this not a perfect solution a long as I have to draw the image a hundreds of times per second.

        Attachments

          Activity

            People

            • Assignee:
              serb Sergey Bylokhov
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: