From 80d145ce62818700c7c70c304a204b5379d50cd3 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Wed, 4 Oct 2017 14:22:06 +0200
Subject: [PATCH] Grid AWT: cache last used BasicStroke

---
 .../fastrGrid/device/awt/Graphics2DDevice.java    | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/Graphics2DDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/Graphics2DDevice.java
index b9834220b6..39a10e7c51 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/Graphics2DDevice.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/awt/Graphics2DDevice.java
@@ -72,6 +72,7 @@ public class Graphics2DDevice implements GridDevice {
     private Graphics2D graphics;
     private final boolean graphicsIsExclusive;
     private DrawingContext cachedContext;
+    private BasicStroke stokeCache;
 
     /**
      * @param graphics Object that should be used for the drawing.
@@ -329,7 +330,7 @@ public class Graphics2DDevice implements GridDevice {
         return new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
     }
 
-    private static BasicStroke getStrokeFromCtx(DrawingContext ctx) {
+    private BasicStroke getStrokeFromCtx(DrawingContext ctx) {
         byte[] type = ctx.getLineType();
         double width = ctx.getLineWidth();
         int lineJoin = fromGridLineJoin(ctx.getLineJoin());
@@ -338,7 +339,10 @@ public class Graphics2DDevice implements GridDevice {
         if (type == DrawingContext.GRID_LINE_BLANK) {
             return blankStroke;
         } else if (type == DrawingContext.GRID_LINE_SOLID) {
-            return new BasicStroke((float) (width), endCap, lineJoin, lineMitre);
+            if (stokeCache == null || !areEqual(stokeCache, (float) width, endCap, lineJoin, lineMitre)) {
+                stokeCache = new BasicStroke((float) (width), endCap, lineJoin, lineMitre);
+            }
+            return stokeCache;
         }
         float[] pattern = new float[type.length];
         for (int i = 0; i < pattern.length; i++) {
@@ -383,4 +387,11 @@ public class Graphics2DDevice implements GridDevice {
     private static int iround(double val) {
         return (int) Math.round(val);
     }
+
+    private static boolean areEqual(BasicStroke s, float width, int endCap, int lineJoin, float lineMitre) {
+        return s.getLineWidth() == width &&
+                        s.getEndCap() == endCap &&
+                        s.getLineJoin() == lineJoin &&
+                        s.getMiterLimit() == lineMitre;
+    }
 }
-- 
GitLab