diff options
author | Graydon Hoare <graydon@redhat.com> | 2004-12-07 00:30:12 +0000 |
---|---|---|
committer | Graydon Hoare <graydon@redhat.com> | 2004-12-07 00:30:12 +0000 |
commit | 2b8605d9de3bb80418bae27f26006e392b7e7b08 (patch) | |
tree | 84b275e3f0172d66a86f89fa70f5dc5b3ae0138e /libjava/gnu | |
parent | 8773443d862224162c26f7baf2c9daeb00247e35 (diff) |
2004-12-06 Graydon Hoare <graydon@redhat.com>
* gnu/java/awt/peer/gtk/GdkGraphics2D.java:
Rework painting into BufferedImages
* gnu/java/awt/peer/gtk/GdkPixbufDecoder.java:
Construct BufferedImage with alpha only when alpha is
present in colormodel.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics2D.c:
Rework painting into client-side jint arrays.
* jni/gtk-peer/gtkcairopeer.h:
Likewise.
git-svn-id: https://gcc.gnu.org/svn/gcc/branches/java-gui-branch@91800 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/gnu')
-rw-r--r-- | libjava/gnu/java/awt/peer/gtk/GdkGraphics2D.java | 201 | ||||
-rw-r--r-- | libjava/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java | 47 |
2 files changed, 133 insertions, 115 deletions
diff --git a/libjava/gnu/java/awt/peer/gtk/GdkGraphics2D.java b/libjava/gnu/java/awt/peer/gtk/GdkGraphics2D.java index 82de03d5aec..0d18f173e84 100644 --- a/libjava/gnu/java/awt/peer/gtk/GdkGraphics2D.java +++ b/libjava/gnu/java/awt/peer/gtk/GdkGraphics2D.java @@ -78,10 +78,12 @@ import java.awt.image.ColorModel; import java.awt.image.CropImageFilter; import java.awt.image.DataBuffer; import java.awt.image.DataBufferInt; +import java.awt.image.DirectColorModel; import java.awt.image.FilteredImageSource; import java.awt.image.ImageConsumer; import java.awt.image.ImageObserver; import java.awt.image.ImagingOpException; +import java.awt.image.MultiPixelPackedSampleModel; import java.awt.image.SampleModel; import java.awt.image.Raster; import java.awt.image.RenderedImage; @@ -123,6 +125,8 @@ public class GdkGraphics2D extends Graphics2D private Font font; private RenderingHints hints; private BufferedImage bimage; + private boolean pixelConversionRequired; + private int [] pixelBuffer; private Composite comp; @@ -130,9 +134,9 @@ public class GdkGraphics2D extends Graphics2D native private void initState (GtkComponentPeer component); native private void initState (int width, int height); + native private void initState (int[] pixes, int width, int height); native private void copyState (GdkGraphics2D g); native public void dispose (); - native private int[] getImagePixels(); native private void cairoSurfaceSetFilter(int filter); native void connectSignals (GtkComponentPeer component); @@ -236,10 +240,24 @@ public class GdkGraphics2D extends Graphics2D GdkGraphics2D (BufferedImage bimage) { - this.bimage = bimage; - initState (bimage.getWidth(), bimage.getHeight()); + this.bimage = bimage; + this.pixelBuffer = findSimpleIntegerArray (bimage.getColorModel(), bimage.getRaster()); + if (this.pixelBuffer == null) + { + this.pixelBuffer = new int [bimage.getRaster().getWidth() * + bimage.getRaster().getHeight()]; + this.pixelConversionRequired = true; + } + else + { + this.pixelConversionRequired = false; + } - setColor(Color.black); + initState (this.pixelBuffer, + bimage.getWidth(), + bimage.getHeight()); + + setColor (Color.black); setBackground (Color.black); setPaint (getColor()); setFont (new Font("SansSerif", Font.PLAIN, 12)); @@ -250,9 +268,9 @@ public class GdkGraphics2D extends Graphics2D stateStack = new Stack(); // draw current buffered image to the pixmap associated - // with it. - - drawImage (bimage, new AffineTransform (1,0,0,1,0,0), bg, null); + // with it, if the image is not equal to our paint buffer. + if (pixelConversionRequired) + drawImage (bimage, new AffineTransform (1,0,0,1,0,0), bg, null); } @@ -450,52 +468,56 @@ public class GdkGraphics2D extends Graphics2D } - private final void updateBufferedImage() + private final int[] findSimpleIntegerArray (ColorModel cm, Raster raster) { - int[] pixels = getImagePixels(); - updateImagePixels(pixels); - } + if (cm == null || raster == null) + return null; - - private final boolean isBufferedImageGraphics () - { - return bimage != null; - } - - private final void updateImagePixels (int[] pixels) - { + if (! cm.getColorSpace().isCS_sRGB()) + return null; - // This function can only be used if - // this graphics object is used to draw into - // buffered image - - if (! isBufferedImageGraphics ()) - return; + if (! (cm instanceof DirectColorModel)) + return null; - WritableRaster raster = bimage.getRaster(); - DataBuffer db = raster.getDataBuffer (); + DirectColorModel dcm = (DirectColorModel) cm; - // update pixels in the bufferedImage + if (dcm.getRedMask() != 0x00FF0000 + || dcm.getGreenMask() != 0x0000FF00 + || dcm.getBlueMask() != 0x000000FF) + return null; - if (raster.getSampleModel ().getDataType () == DataBuffer.TYPE_INT - && db instanceof DataBufferInt - && db.getNumBanks () == 1) - { + if (! (raster instanceof WritableRaster)) + return null; - // single bank, ARGB-ints buffer in sRGB space - DataBufferInt dbi = (DataBufferInt) raster.getDataBuffer (); + if (raster.getSampleModel().getDataType() != DataBuffer.TYPE_INT) + return null; - for (int i=0; i < pixels.length; i++) - dbi.setElem(i, pixels[i]); - - } - else - { - bimage.getRaster().setPixels (0, 0, raster.getWidth (), - raster.getHeight (), pixels); - } + if (! (raster.getDataBuffer() instanceof DataBufferInt)) + return null; + + DataBufferInt db = (DataBufferInt) raster.getDataBuffer(); + + if (db.getNumBanks() != 1) + return null; + + // Finally, we have determined that this is a single bank, [A]RGB-int + // buffer in sRGB space. It's worth checking all this, because it means + // that cairo can paint directly into the data buffer, which is very + // fast compared to all the normal copying and converting. + + return db.getData(); } + private final void updateBufferedImage() + { + if (bimage != null && pixelConversionRequired) + { + bimage.getRaster().setPixels (0, 0, + bimage.getRaster().getWidth (), + bimage.getRaster().getHeight (), + pixelBuffer); + } + } private final boolean drawImage(Image img, AffineTransform xform, @@ -517,8 +539,7 @@ public class GdkGraphics2D extends Graphics2D GdkGraphics2D g2 = (GdkGraphics2D) img.getGraphics (); gdkDrawDrawable (g2, (int)xform.getTranslateX(), (int)xform.getTranslateY()); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); return true; } @@ -542,7 +563,7 @@ public class GdkGraphics2D extends Graphics2D { // draw an image which has actually been loaded // into memory fully - + BufferedImage b = (BufferedImage) img; return drawRaster (b.getColorModel (), b.getData (), @@ -591,8 +612,7 @@ public class GdkGraphics2D extends Graphics2D walkPath (s.getPathIterator (null), shiftDrawCalls); cairoStroke (); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } public void fill (Shape s) @@ -607,8 +627,7 @@ public class GdkGraphics2D extends Graphics2D walkPath (s.getPathIterator (null), false); cairoFill (); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } @@ -841,7 +860,7 @@ public class GdkGraphics2D extends Graphics2D public Shape getClip () { - return getClipInDevSpace (); + return clip.getBounds2D(); //getClipInDevSpace (); } public Rectangle getClipBounds () @@ -904,8 +923,7 @@ public class GdkGraphics2D extends Graphics2D setStroke(draw3DRectStroke); super.draw3DRect(x, y, width, height, raised); setStroke(tmp); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } public void fill3DRect(int x, int y, int width, @@ -915,8 +933,7 @@ public class GdkGraphics2D extends Graphics2D setStroke(draw3DRectStroke); super.fill3DRect(x, y, width, height, raised); setStroke(tmp); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } @@ -943,8 +960,7 @@ public class GdkGraphics2D extends Graphics2D cairoFill (); setColor (fg); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } @@ -1040,45 +1056,47 @@ public class GdkGraphics2D extends Graphics2D i2u[4] = 0; i2u[5] = 0; } - int pixels[] = null; + int pixels[] = findSimpleIntegerArray(cm, r); - if (sm.getDataType () == DataBuffer.TYPE_INT && - db instanceof DataBufferInt && - db.getNumBanks () == 1) - { - // single bank, ARGB-ints buffer in sRGB space - DataBufferInt dbi = (DataBufferInt)db; - pixels = dbi.getData (); - } - else - pixels = r.getPixels (0, 0, r.getWidth (), r.getHeight (), pixels); - - ColorSpace cs = cm.getColorSpace (); - if (cs != null && - cs.getType () != ColorSpace.CS_sRGB) + if (pixels == null) { - int pixels2[] = new int[pixels.length]; - for (int i = 0; i < pixels2.length; i++) - pixels2[i] = cm.getRGB (pixels[i]); - pixels = pixels2; + // FIXME: I don't think this code will work correctly with a non-RGB + // MultiPixelPackedSampleModel. Although this entire method should + // probably be rewritten to better utilize Cairo's different supported + // data formats. + if (sm instanceof MultiPixelPackedSampleModel) + { + pixels = r.getPixels(0, 0, r.getWidth(), r.getHeight(), pixels); + for (int i = 0; i < pixels.length; i++) + pixels[i] = cm.getRGB(pixels[i]); + } + else + { + pixels = new int[r.getWidth() * r.getHeight()]; + for (int i = 0; i < pixels.length; i++) + pixels[i] = cm.getRGB(db.getElem(i)); + } } - // change all transparent pixels in the image to the - // specified bgcolor - - if (bgcolor != null) + // Change all transparent pixels in the image to the specified bgcolor, + // or (if there's no alpha) fill in an alpha channel so that it paints + // correctly. + + if (cm.hasAlpha()) { + if (bgcolor != null) + for (int i = 0; i < pixels.length; i++) + { + if (cm.getAlpha (pixels[i]) == 0) + pixels[i] = bgcolor.getRGB (); + } + } else for (int i = 0; i < pixels.length; i++) - { - if (cm.getAlpha (pixels[i]) == 0) - pixels[i] = bgcolor.getRGB (); - } - } + pixels[i] |= 0xFF000000; drawPixels (pixels, r.getWidth (), r.getHeight (), r.getWidth (), i2u); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); return true; } @@ -1389,22 +1407,19 @@ public class GdkGraphics2D extends Graphics2D public void drawGdkGlyphVector(GdkGlyphVector gv, float x, float y) { cairoDrawGdkGlyphVector(getFontPeer(), gv, x, y); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } public void drawGdkTextLayout(GdkTextLayout gl, float x, float y) { cairoDrawGdkTextLayout(getFontPeer(), gl, x, y); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } public void drawString (String str, float x, float y) { cairoDrawString(getFontPeer(), str, x, y); - if (isBufferedImageGraphics ()) - updateBufferedImage(); + updateBufferedImage(); } public void drawString (String str, int x, int y) diff --git a/libjava/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java b/libjava/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java index a7b93e5b9ad..c70c62db108 100644 --- a/libjava/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java +++ b/libjava/gnu/java/awt/peer/gtk/GdkPixbufDecoder.java @@ -159,15 +159,18 @@ public class GdkPixbufDecoder extends gnu.java.awt.image.ImageDecoder { BufferedImage bufferedImage; ColorModel defaultModel; + int width; + int height; public BufferedImage getBufferedImage() { return bufferedImage; } - public void setDimensions(int width, int height) + public void setDimensions(int w, int h) { - bufferedImage = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); + width = w; + height = h; } public void setProperties(Hashtable props) {} @@ -189,28 +192,28 @@ public class GdkPixbufDecoder extends gnu.java.awt.image.ImageDecoder ColorModel model, int[] pixels, int offset, int scansize) { - if (bufferedImage != null) + if (model == null) + model = defaultModel; + + if (bufferedImage == null) + bufferedImage = new BufferedImage (width, height, (model != null && model.hasAlpha() ? + BufferedImage.TYPE_INT_ARGB + : BufferedImage.TYPE_INT_RGB)); + int pixels2[]; + if (model != null) { - - if (model == null) - model = defaultModel; - - int pixels2[]; - if (model != null) - { - pixels2 = new int[pixels.length]; - for (int yy = 0; yy < h; yy++) - for (int xx = 0; xx < w; xx++) - { - int i = yy * scansize + xx; - pixels2[i] = model.getRGB (pixels[i]); - } - } - else - pixels2 = pixels; - - bufferedImage.setRGB (x, y, w, h, pixels2, offset, scansize); + pixels2 = new int[pixels.length]; + for (int yy = 0; yy < h; yy++) + for (int xx = 0; xx < w; xx++) + { + int i = yy * scansize + xx; + pixels2[i] = model.getRGB (pixels[i]); + } } + else + pixels2 = pixels; + + bufferedImage.setRGB (x, y, w, h, pixels2, offset, scansize); } public void imageComplete(int status) {} |