RFR: 8355078: java.awt.Color.createContext() uses unnecessary synchronization
Chen Liang
liach at openjdk.org
Sat May 3 03:36:37 UTC 2025
On Sun, 20 Apr 2025 08:15:38 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:
> At some point, java.awt.Color.createContext() was marked as synchronized to support caching of a ColorPaintContext instance. The cache was later removed, but the synchronized modifier remained. Since there is no shared mutable state anymore, the synchronization is no longer necessary and can be safely removed.
>
>
> Note: This code path is rarely executed because a special optimization was introduced in [SunGraphics2D.setPaint](https://github.com/openjdk/jdk/blob/c514f135ccf08c3be016a32ae8f2c055fb941857/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java#L1003). As a result, unless a custom wrapper around the Color class is used, the Color.createContext() method is typically bypassed during rendering.
>
> Two tests are added:
> - ColorPaintContextBasicTest: Checks if different image types (BufferedImage and VolatileImage) produce the same results when using different ways to fill the image (setColor, setPaint, and custom Paint). This test intentionally uses a custom Paint implementation to bypass the optimization and ensure that Color.createContext() is invoked verifying its correct behavior.
> - ColorPaintContextStateTrackerTest: Checks that the raster used in ColorPaintContext.getRaster() can be properly cached in video memory and we do not need to call icr.markDirty() in ColorPaintContext.getRaster()
Is a CSR requested for the access flags change? Some method flags, notably `native` or `synchronized`, are not part of the API specificaion and not rendered, and thus can be changed without CSR reviews. For example, the [javadoc for this particular method](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/java/awt/Color.html#createContext(java.awt.image.ColorModel,java.awt.Rectangle,java.awt.geom.Rectangle2D,java.awt.geom.AffineTransform,java.awt.RenderingHints)) does not include the `synchronized` modifier.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24771#issuecomment-2817252429
More information about the client-libs-dev
mailing list