# HG changeset patch # User Anton Epple # Date 1369391398 -7200 # Node ID 69c81bdaf193bf691629ba7476714388f2921511 # Parent 64f6ae8682edb4de597d311561bee9571c8b70a7 added caching to style diff -r 64f6ae8682ed -r 69c81bdaf193 javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Fri May 24 07:50:07 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Fri May 24 12:29:58 2013 +0200 @@ -144,18 +144,6 @@ //// graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height); //// } - public void setFillStyle(String style){ - graphicsEnvironmentImpl.setFillStyle(style); - } - - public String getFillStyle(){ - return graphicsEnvironmentImpl.getFillStyle(); - } - - public void setStrokeStyle(String style){ - graphicsEnvironmentImpl.setStrokeStyle(style); - } - public void setShadowColor(String color){ graphicsEnvironmentImpl.setShadowColor(color); } @@ -172,10 +160,6 @@ graphicsEnvironmentImpl.setShadowOffsetY(y); } - public String getStrokeStyle(){ - return graphicsEnvironmentImpl.getStrokeStyle(); - } - public String getShadowColor(){ return graphicsEnvironmentImpl.getShadowColor(); } @@ -223,6 +207,11 @@ public void setMiterLimit(double limit){ graphicsEnvironmentImpl.setMiterLimit(limit); } + + public void setFillStyle(Style style){ + Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached()); + style.cache(nativeFillStyle); + } public String getFont(){ return graphicsEnvironmentImpl.getFont(); @@ -231,6 +220,11 @@ public void setFont(String font){ graphicsEnvironmentImpl.setFont(font); } + + public void setStrokeStyle(Style style){ + Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached()); + style.cache(nativeStrokeStyle); + } public String getTextAlign(){ return graphicsEnvironmentImpl.getTextAlign(); diff -r 64f6ae8682ed -r 69c81bdaf193 javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java Fri May 24 07:50:07 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java Fri May 24 12:29:58 2013 +0200 @@ -18,6 +18,7 @@ package net.java.html.canvas; import java.util.HashMap; +import java.util.Objects; /** * @@ -82,5 +83,43 @@ public void setY1(double y1) { this.y1 = y1; } + + @Override + public int hashCode() { + int hash = 7; + hash = 29 * hash + Objects.hashCode(this.stops); + hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32)); + hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32)); + hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32)); + hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32)); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final LinearGradient other = (LinearGradient) obj; + if (!Objects.equals(this.stops, other.stops)) { + return false; + } + if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) { + return false; + } + if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) { + return false; + } + if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) { + return false; + } + if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) { + return false; + } + return true; + } } diff -r 64f6ae8682ed -r 69c81bdaf193 javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java Fri May 24 07:50:07 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java Fri May 24 12:29:58 2013 +0200 @@ -21,15 +21,17 @@ * * @author antonepple */ -public final class RadialGradient extends LinearGradient{ - double r0, r1; +public final class RadialGradient extends LinearGradient { - RadialGradient( double x0, double y0,double r0, double x1, double y1, double r1) { + + private double r0, r1; + + RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) { super(x0, y0, x1, y1); this.r0 = r0; this.r1 = r1; } - + public double getR0() { return r0; } @@ -45,7 +47,34 @@ public void setR1(double r1) { this.r1 = r1; } - - - + + @Override + public int hashCode() { + int hash = super.hashCode(); + hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32)); + hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32)); + + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + if (!super.equals(obj)) { + return false; + } + final RadialGradient other = (RadialGradient) obj; + if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) { + return false; + } + if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) { + return false; + } + return true; + } } diff -r 64f6ae8682ed -r 69c81bdaf193 javaquery/canvas/src/main/java/net/java/html/canvas/Style.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/Style.java Fri May 24 07:50:07 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/Style.java Fri May 24 12:29:58 2013 +0200 @@ -23,21 +23,34 @@ */ public class Style { + private Object cached; + private int cacheHash; + Style() { } - - public static final RadialGradient createRadialGradient( double x0, double y0,double r0, double x1, double y1, double r1){ + + public static final RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) { return new RadialGradient(x0, y0, r0, x1, y1, r1); } - - public static final LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){ + + public static final LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) { return new LinearGradient(x0, y0, x1, y1); } - - public static final Pattern createPattern(ImageData imageData, String repeat){ + + public static final Pattern createPattern(ImageData imageData, String repeat) { return new Pattern(imageData, repeat); } - - - + + void cache(Object toCache) { + cacheHash = hashCode(); + this.cached = toCache; + } + + private boolean isCached() { + return cacheHash == hashCode(); + } + + Object getCached() { + return isCached() ? cached : null; + } } diff -r 64f6ae8682ed -r 69c81bdaf193 javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java Fri May 24 07:50:07 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java Fri May 24 12:29:58 2013 +0200 @@ -22,6 +22,7 @@ /** * Provider API for Canvas. Implement this to add support for your platform. + * * @author antonepple */ public interface GraphicsEnvironment { @@ -87,15 +88,32 @@ //// //// public void drawImage(ImageData image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height); // - public void setFillStyle(String style); - public String getFillStyle(); + /** + * When implementing you can return an Object of your choice to enable + * caching. Returning null means no caching. When caching is enabled, and + * the cache hasn't been invalidated, the Object you returned will be passed + * as a parameter. + * + * @param style The style object you should use to create your native style + * @param nativeStyle your native object if cached, null otherwise + * @return return native Object for caching + * + */ + public Object setFillStyle(Style style, Object nativeStyle); - public void setFillStyle(Style style); - - public void setStrokeStyle(String style); - - public void setStrokeStyle(Style style); + /** + * When implementing you can return an Object of your choice to enable + * caching. Returning null means no caching. When caching is enabled, and + * the cache hasn't been invalidated, the Object you returned will be passed + * as a parameter. + * + * @param style The style object you should use to create your native style + * @param nativeStyle your native object if cached, null otherwise + * @return return native Object for caching + * + */ + public Object setStrokeStyle(Style style, Object nativeStyle); public void setShadowColor(String color); @@ -105,8 +123,6 @@ public void setShadowOffsetY(double y); - public String getStrokeStyle(); - public String getShadowColor(); public double getShadowBlur(); @@ -162,7 +178,6 @@ //// public void putImageData(ImageData imageData, double x, double y); //// //// public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight); - public void setGlobalAlpha(double alpha); public double getGlobalAlpha(); @@ -172,7 +187,6 @@ public String getGlobalCompositeOperation(); //// public ImageData getImageForPath(String path); - public int getHeight(); public int getWidth();