diff --git a/platform/platform-api/src/com/intellij/ui/paint/EffectPainter2D.java b/platform/platform-api/src/com/intellij/ui/paint/EffectPainter2D.java index 0abdb8f2c01e..d1057aef626e 100644 --- a/platform/platform-api/src/com/intellij/ui/paint/EffectPainter2D.java +++ b/platform/platform-api/src/com/intellij/ui/paint/EffectPainter2D.java @@ -9,6 +9,7 @@ import com.intellij.util.ui.JBUI; import com.intellij.util.ui.UIUtil; import com.intellij.util.ui.WavePainter2D; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; import java.awt.*; @@ -319,14 +320,24 @@ public enum EffectPainter2D implements RegionPainter2D { } } + @Nullable BufferedImage getImage(Graphics2D g, Color color, double height) { ConcurrentHashMap cache = UIUtil.isJreHiDPI(g) ? myHiDPICache : myNormalCache; - return cache.computeIfAbsent(Objects.hash(color.getRGB(), JBUI.sysScale(g), height), k -> createImage(g, color, height)); + int key = Objects.hash(color.getRGB(), JBUI.sysScale(g), height); + BufferedImage image = cache.get(key); + if (image == null) { + image = createImage(g, color, height); + if (image != null) cache.putIfAbsent(key, image); + } + return image; } + @Nullable BufferedImage createImage(Graphics2D g, Paint paint, double height) { double period = getPeriod(height); int width = (int)period << (paint instanceof Color ? 8 : 1); + if (width <= 0 || height <= 0) return null; + BufferedImage image = UIUtil.createImage(g, width, height, BufferedImage.TYPE_INT_ARGB, RoundingMode.FLOOR); paintImage(image.createGraphics(), paint, width, height, period); return image; @@ -341,6 +352,8 @@ public enum EffectPainter2D implements RegionPainter2D { g.setComposite(AlphaComposite.SrcOver); BufferedImage image = paint instanceof Color ? getImage(g, (Color)paint, height) : createImage(g, paint, height); + if (image == null) return; + int period = image.getWidth(null); if (image instanceof JBHiDPIScaledImage) period /= 2; double offset = (x % period + period) % period; // normalize diff --git a/platform/util/src/com/intellij/ui/paint/PaintUtil.java b/platform/util/src/com/intellij/ui/paint/PaintUtil.java index 8b8a595342ea..d846ebdd0086 100644 --- a/platform/util/src/com/intellij/ui/paint/PaintUtil.java +++ b/platform/util/src/com/intellij/ui/paint/PaintUtil.java @@ -149,6 +149,8 @@ public class PaintUtil { public static double alignToInt(double usrValue, @NotNull ScaleContext ctx, @Nullable RoundingMode rm, @Nullable ParityMode pm) { if (rm == null) rm = ROUND; double scale = getScale(ctx); + if (scale == 0) return 0; + int devValue = devValue(usrValue, scale, pm != null && rm == ROUND ? FLOOR : rm); if (pm != null && ParityMode.of(devValue) != pm) { devValue += (rm == FLOOR) ? -1 : 1; @@ -198,7 +200,11 @@ public class PaintUtil { private static double getScale(ScaleContext ctx) { // exclude the user scale, unless it's zero - return ctx.getScale(USR_SCALE) == 0 ? 0 : ctx.getScale(PIX_SCALE) / ctx.getScale(USR_SCALE); + double scale = ctx.getScale(USR_SCALE) == 0 ? 0 : ctx.getScale(PIX_SCALE) / ctx.getScale(USR_SCALE); + if (scale <= 0) { + Logger.getInstance(PaintUtil.class).warn("bad scale in the context: " + ctx.toString(), new Throwable()); + } + return scale; } /**