8379778: SunGraphics2D improvements

Reviewed-by: avu, prr
This commit is contained in:
Daniel Gredler 2026-03-12 14:49:35 +00:00
parent 34a0235ed3
commit 8444fdae4a

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -250,11 +250,6 @@ public final class SunGraphics2D
private FontInfo glyphVectorFontInfo;
private FontRenderContext glyphVectorFRC;
private static final int slowTextTransformMask =
AffineTransform.TYPE_GENERAL_TRANSFORM
| AffineTransform.TYPE_MASK_ROTATION
| AffineTransform.TYPE_FLIP;
static {
if (PerformanceLogger.loggingEnabled()) {
PerformanceLogger.setTime("SunGraphics2D static initialization");
@ -453,13 +448,13 @@ public final class SunGraphics2D
* or whether that shape must be "kept" unmodified.
*/
Shape intersectShapes(Shape s1, Shape s2, boolean keep1, boolean keep2) {
if (s1 instanceof Rectangle && s2 instanceof Rectangle) {
return ((Rectangle) s1).intersection((Rectangle) s2);
if (s1 instanceof Rectangle r1 && s2 instanceof Rectangle r2) {
return r1.intersection(r2);
}
if (s1 instanceof Rectangle2D) {
return intersectRectShape((Rectangle2D) s1, s2, keep1, keep2);
} else if (s2 instanceof Rectangle2D) {
return intersectRectShape((Rectangle2D) s2, s1, keep2, keep1);
if (s1 instanceof Rectangle2D r1) {
return intersectRectShape(r1, s2, keep1, keep2);
} else if (s2 instanceof Rectangle2D r2) {
return intersectRectShape(r2, s1, keep2, keep1);
}
return intersectByArea(s1, s2, keep1, keep2);
}
@ -473,8 +468,7 @@ public final class SunGraphics2D
*/
Shape intersectRectShape(Rectangle2D r, Shape s,
boolean keep1, boolean keep2) {
if (s instanceof Rectangle2D) {
Rectangle2D r2 = (Rectangle2D) s;
if (s instanceof Rectangle2D r2) {
Rectangle2D outrect;
if (!keep1) {
outrect = r;
@ -596,12 +590,10 @@ public final class SunGraphics2D
}
float ptSize = font.getSize2D();
int txFontType;
AffineTransform devAt, textAt=null;
if (font.isTransformed()) {
textAt = font.getTransform();
textAt.scale(ptSize, ptSize);
txFontType = textAt.getType();
info.originX = (float)textAt.getTranslateX();
info.originY = (float)textAt.getTranslateY();
textAt.translate(-info.originX, -info.originY);
@ -621,7 +613,6 @@ public final class SunGraphics2D
}
info.pixelHeight = (int)(Math.abs(scaley)+0.5);
} else {
txFontType = AffineTransform.TYPE_IDENTITY;
info.originX = info.originY = 0;
if (transformState >= TRANSFORM_TRANSLATESCALE) {
transform.getMatrix(info.devTx = new double[4]);
@ -783,18 +774,6 @@ public final class SunGraphics2D
return info;
}
public static boolean isRotated(double [] mtx) {
if ((mtx[0] == mtx[3]) &&
(mtx[1] == 0.0) &&
(mtx[2] == 0.0) &&
(mtx[0] > 0.0))
{
return false;
}
return true;
}
public void setFont(Font font) {
/* replacing the reference equality test font != this.font with
* !font.equals(this.font) did not yield any measurable difference
@ -944,8 +923,7 @@ public final class SunGraphics2D
}
int newCompState;
CompositeType newCompType;
if (comp instanceof AlphaComposite) {
AlphaComposite alphacomp = (AlphaComposite) comp;
if (comp instanceof AlphaComposite alphacomp) {
newCompType = CompositeType.forAlphaComposite(alphacomp);
if (newCompType == CompositeType.SrcOverNoEa) {
if (paintState == PAINT_OPAQUECOLOR ||
@ -1000,8 +978,8 @@ public final class SunGraphics2D
* @see TexturePaint
*/
public void setPaint(Paint paint) {
if (paint instanceof Color) {
setColor((Color) paint);
if (paint instanceof Color c) {
setColor(c);
return;
}
if (paint == null || this.paint == paint) {
@ -1162,8 +1140,8 @@ public final class SunGraphics2D
}
int saveStrokeState = strokeState;
stroke = s;
if (s instanceof BasicStroke) {
validateBasicStroke((BasicStroke) s);
if (s instanceof BasicStroke bs) {
validateBasicStroke(bs);
} else {
strokeState = STROKE_CUSTOM;
}
@ -1193,11 +1171,10 @@ public final class SunGraphics2D
throw new IllegalArgumentException
(hintValue+" is not compatible with "+hintKey);
}
if (hintKey instanceof SunHints.Key) {
if (hintKey instanceof SunHints.Key sunKey) {
boolean stateChanged;
boolean textStateChanged = false;
boolean recognized = true;
SunHints.Key sunKey = (SunHints.Key) hintKey;
int newHint;
if (sunKey == SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST) {
newHint = ((Integer)hintValue).intValue();
@ -1297,7 +1274,6 @@ public final class SunGraphics2D
hints.put(hintKey, hintValue);
}
/**
* Returns the preferences for the rendering algorithms.
* @param hintKey The category of hint to be set. The strings
@ -1310,10 +1286,10 @@ public final class SunGraphics2D
if (hints != null) {
return hints.get(hintKey);
}
if (!(hintKey instanceof SunHints.Key)) {
if (!(hintKey instanceof SunHints.Key shk)) {
return null;
}
int keyindex = ((SunHints.Key)hintKey).getIndex();
int keyindex = shk.getIndex();
switch (keyindex) {
case SunHints.INTKEY_RENDERING:
return SunHints.Value.get(SunHints.INTKEY_RENDERING,
@ -1822,8 +1798,8 @@ public final class SunGraphics2D
public Rectangle getClipBounds(Rectangle r) {
if (clipState != CLIP_DEVICE) {
if (transformState <= TRANSFORM_INT_TRANSLATE) {
if (usrClip instanceof Rectangle) {
r.setBounds((Rectangle) usrClip);
if (usrClip instanceof Rectangle usrClipRect) {
r.setBounds(usrClipRect);
} else {
r.setFrame(usrClip.getBounds2D());
}
@ -1970,8 +1946,7 @@ public final class SunGraphics2D
r.translate(tx, ty);
return r;
}
if (s instanceof Rectangle2D) {
Rectangle2D rect = (Rectangle2D) s;
if (s instanceof Rectangle2D rect) {
return new Rectangle2D.Double(rect.getX() + tx,
rect.getY() + ty,
rect.getWidth(),
@ -1991,10 +1966,9 @@ public final class SunGraphics2D
return null;
}
if (clip instanceof Rectangle2D &&
if (clip instanceof Rectangle2D rect &&
(tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
{
Rectangle2D rect = (Rectangle2D) clip;
double[] matrix = new double[4];
matrix[0] = rect.getX();
matrix[1] = rect.getY();
@ -2180,65 +2154,6 @@ public final class SunGraphics2D
}
}
/*
public void XcopyArea(int x, int y, int w, int h, int dx, int dy) {
Rectangle rect = new Rectangle(x, y, w, h);
rect = transformBounds(rect, transform);
Point2D point = new Point2D.Float(dx, dy);
Point2D root = new Point2D.Float(0, 0);
point = transform.transform(point, point);
root = transform.transform(root, root);
int fdx = (int)(point.getX()-root.getX());
int fdy = (int)(point.getY()-root.getY());
Rectangle r = getCompBounds().intersection(rect.getBounds());
if (r.isEmpty()) {
return;
}
// Begin Rasterizer for Clip Shape
boolean skipClip = true;
byte[] clipAlpha = null;
if (clipState == CLIP_SHAPE) {
int box[] = new int[4];
clipRegion.getBounds(box);
Rectangle devR = new Rectangle(box[0], box[1],
box[2] - box[0],
box[3] - box[1]);
if (!devR.isEmpty()) {
OutputManager mgr = getOutputManager();
RegionIterator ri = clipRegion.getIterator();
while (ri.nextYRange(box)) {
int spany = box[1];
int spanh = box[3] - spany;
while (ri.nextXBand(box)) {
int spanx = box[0];
int spanw = box[2] - spanx;
mgr.copyArea(this, null,
spanw, 0,
spanx, spany,
spanw, spanh,
fdx, fdy,
null);
}
}
}
return;
}
// End Rasterizer for Clip Shape
getOutputManager().copyArea(this, null,
r.width, 0,
r.x, r.y, r.width,
r.height, fdx, fdy,
null);
}
*/
public void drawLine(int x1, int y1, int x2, int y2) {
try {
drawpipe.drawLine(this, x1, y1, x2, y2);
@ -2465,8 +2380,8 @@ public final class SunGraphics2D
if (paintState <= PAINT_ALPHACOLOR) {
validateColor();
}
if (composite instanceof XORComposite) {
Color c = ((XORComposite) composite).getXorColor();
if (composite instanceof XORComposite xorComp) {
Color c = xorComp.getXorColor();
setComposite(new XORComposite(c, surfaceData));
}
validatePipe();
@ -2668,8 +2583,7 @@ public final class SunGraphics2D
}
// BufferedImage case: use a simple drawImage call
if (img instanceof BufferedImage) {
BufferedImage bufImg = (BufferedImage)img;
if (img instanceof BufferedImage bufImg) {
drawImage(bufImg,xform,null);
return;
}
@ -2905,21 +2819,6 @@ public final class SunGraphics2D
drawRenderedImage(rendering,reverseTransform);
}
/*
* Transform the bounding box of the BufferedImage
*/
protected Rectangle transformBounds(Rectangle rect,
AffineTransform tx) {
if (tx.isIdentity()) {
return rect;
}
Shape s = transformShape(tx, rect);
return s.getBounds();
}
// text rendering methods
public void drawString(String str, int x, int y) {
if (str == null) {
@ -3130,13 +3029,12 @@ public final class SunGraphics2D
invalidateTransform();
}
return result;
} else if (img instanceof MultiResolutionImage) {
} else if (img instanceof MultiResolutionImage mrImage) {
// get scaled destination image size
int width = img.getWidth(observer);
int height = img.getHeight(observer);
MultiResolutionImage mrImage = (MultiResolutionImage) img;
Image resolutionVariant = getResolutionVariant(mrImage, width, height,
dx1, dy1, dx2, dy2,
sx1, sy1, sx2, sy2,
@ -3311,8 +3209,7 @@ public final class SunGraphics2D
Image resolutionVariant
= img.getResolutionVariant(destImageWidth, destImageHeight);
if (resolutionVariant instanceof ToolkitImage
&& ((ToolkitImage) resolutionVariant).hasError()) {
if (resolutionVariant instanceof ToolkitImage tki && tki.hasError()) {
return null;
}