added caching to style canvas
authorAnton Epple <toni.epple@eppleton.de>
Fri, 24 May 2013 12:29:58 +0200
branchcanvas
changeset 114169c81bdaf193
parent 1140 64f6ae8682ed
child 1142 7db01893aaf8
added caching to style
javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java
javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java
javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java
javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java
     1.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java	Fri May 24 07:50:07 2013 +0200
     1.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java	Fri May 24 12:29:58 2013 +0200
     1.3 @@ -144,18 +144,6 @@
     1.4  ////        graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height);
     1.5  ////    }
     1.6  
     1.7 -    public void setFillStyle(String style){
     1.8 -        graphicsEnvironmentImpl.setFillStyle(style);
     1.9 -    }
    1.10 -
    1.11 -    public String getFillStyle(){
    1.12 -        return graphicsEnvironmentImpl.getFillStyle();
    1.13 -    }
    1.14 -
    1.15 -    public void setStrokeStyle(String style){
    1.16 -        graphicsEnvironmentImpl.setStrokeStyle(style);
    1.17 -    }
    1.18 -
    1.19      public void setShadowColor(String color){
    1.20          graphicsEnvironmentImpl.setShadowColor(color);
    1.21      }
    1.22 @@ -172,10 +160,6 @@
    1.23          graphicsEnvironmentImpl.setShadowOffsetY(y);
    1.24      }
    1.25  
    1.26 -    public String getStrokeStyle(){
    1.27 -        return graphicsEnvironmentImpl.getStrokeStyle();
    1.28 -    }
    1.29 -
    1.30      public String getShadowColor(){
    1.31          return graphicsEnvironmentImpl.getShadowColor();
    1.32      }
    1.33 @@ -223,6 +207,11 @@
    1.34      public void setMiterLimit(double limit){
    1.35          graphicsEnvironmentImpl.setMiterLimit(limit);
    1.36      }
    1.37 +    
    1.38 +    public void setFillStyle(Style style){
    1.39 +        Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
    1.40 +        style.cache(nativeFillStyle);
    1.41 +    }
    1.42  
    1.43      public String getFont(){
    1.44          return graphicsEnvironmentImpl.getFont();
    1.45 @@ -231,6 +220,11 @@
    1.46      public void setFont(String font){
    1.47          graphicsEnvironmentImpl.setFont(font);
    1.48      }
    1.49 +    
    1.50 +    public void setStrokeStyle(Style style){
    1.51 +        Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
    1.52 +        style.cache(nativeStrokeStyle);
    1.53 +    }
    1.54  
    1.55      public String getTextAlign(){
    1.56          return graphicsEnvironmentImpl.getTextAlign();
     2.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java	Fri May 24 07:50:07 2013 +0200
     2.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/LinearGradient.java	Fri May 24 12:29:58 2013 +0200
     2.3 @@ -18,6 +18,7 @@
     2.4  package net.java.html.canvas;
     2.5  
     2.6  import java.util.HashMap;
     2.7 +import java.util.Objects;
     2.8  
     2.9  /**
    2.10   *
    2.11 @@ -82,5 +83,43 @@
    2.12      public void setY1(double y1) {
    2.13          this.y1 = y1;
    2.14      }
    2.15 +
    2.16 +    @Override
    2.17 +    public int hashCode() {
    2.18 +        int hash = 7;
    2.19 +        hash = 29 * hash + Objects.hashCode(this.stops);
    2.20 +        hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
    2.21 +        hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
    2.22 +        hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
    2.23 +        hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
    2.24 +        return hash;
    2.25 +    }
    2.26 +
    2.27 +    @Override
    2.28 +    public boolean equals(Object obj) {
    2.29 +        if (obj == null) {
    2.30 +            return false;
    2.31 +        }
    2.32 +        if (getClass() != obj.getClass()) {
    2.33 +            return false;
    2.34 +        }
    2.35 +        final LinearGradient other = (LinearGradient) obj;
    2.36 +        if (!Objects.equals(this.stops, other.stops)) {
    2.37 +            return false;
    2.38 +        }
    2.39 +        if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
    2.40 +            return false;
    2.41 +        }
    2.42 +        if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
    2.43 +            return false;
    2.44 +        }
    2.45 +        if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
    2.46 +            return false;
    2.47 +        }
    2.48 +        if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
    2.49 +            return false;
    2.50 +        }
    2.51 +        return true;
    2.52 +    }
    2.53      
    2.54  }
     3.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java	Fri May 24 07:50:07 2013 +0200
     3.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/RadialGradient.java	Fri May 24 12:29:58 2013 +0200
     3.3 @@ -21,15 +21,17 @@
     3.4   *
     3.5   * @author antonepple
     3.6   */
     3.7 -public final class RadialGradient extends LinearGradient{
     3.8 -    double r0, r1;
     3.9 +public final class RadialGradient extends LinearGradient {
    3.10  
    3.11 -    RadialGradient( double x0, double y0,double r0, double x1, double y1, double r1) {
    3.12 +    
    3.13 +    private double r0, r1;
    3.14 +
    3.15 +    RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
    3.16          super(x0, y0, x1, y1);
    3.17          this.r0 = r0;
    3.18          this.r1 = r1;
    3.19      }
    3.20 -    
    3.21 +
    3.22      public double getR0() {
    3.23          return r0;
    3.24      }
    3.25 @@ -45,7 +47,34 @@
    3.26      public void setR1(double r1) {
    3.27          this.r1 = r1;
    3.28      }
    3.29 -    
    3.30 - 
    3.31 -    
    3.32 +
    3.33 +    @Override
    3.34 +    public int hashCode() {
    3.35 +        int hash = super.hashCode();
    3.36 +        hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
    3.37 +        hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
    3.38 +
    3.39 +        return hash;
    3.40 +    }
    3.41 +
    3.42 +    @Override
    3.43 +    public boolean equals(Object obj) {
    3.44 +        if (obj == null) {
    3.45 +            return false;
    3.46 +        }
    3.47 +        if (getClass() != obj.getClass()) {
    3.48 +            return false;
    3.49 +        }
    3.50 +        if (!super.equals(obj)) {
    3.51 +            return false;
    3.52 +        }
    3.53 +        final RadialGradient other = (RadialGradient) obj;
    3.54 +        if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
    3.55 +            return false;
    3.56 +        }
    3.57 +        if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
    3.58 +            return false;
    3.59 +        }
    3.60 +        return true;
    3.61 +    }
    3.62  }
     4.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/Style.java	Fri May 24 07:50:07 2013 +0200
     4.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/Style.java	Fri May 24 12:29:58 2013 +0200
     4.3 @@ -23,21 +23,34 @@
     4.4   */
     4.5  public class Style {
     4.6  
     4.7 +    private Object cached;
     4.8 +    private int cacheHash;
     4.9 +
    4.10      Style() {
    4.11      }
    4.12 -    
    4.13 -    public static final RadialGradient createRadialGradient( double x0, double y0,double r0, double x1, double y1, double r1){
    4.14 +
    4.15 +    public static final RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
    4.16          return new RadialGradient(x0, y0, r0, x1, y1, r1);
    4.17      }
    4.18 -    
    4.19 -    public static final LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){
    4.20 +
    4.21 +    public static final LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
    4.22          return new LinearGradient(x0, y0, x1, y1);
    4.23      }
    4.24 -    
    4.25 -    public static final Pattern createPattern(ImageData imageData, String repeat){
    4.26 +
    4.27 +    public static final Pattern createPattern(ImageData imageData, String repeat) {
    4.28          return new Pattern(imageData, repeat);
    4.29      }
    4.30 -    
    4.31 -    
    4.32 -    
    4.33 +
    4.34 +    void cache(Object toCache) {
    4.35 +        cacheHash = hashCode();
    4.36 +        this.cached = toCache;
    4.37 +    }
    4.38 +
    4.39 +    private boolean isCached() {
    4.40 +        return cacheHash == hashCode();
    4.41 +    }
    4.42 +
    4.43 +    Object getCached() {
    4.44 +        return isCached() ? cached : null;
    4.45 +    }
    4.46  }
     5.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java	Fri May 24 07:50:07 2013 +0200
     5.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java	Fri May 24 12:29:58 2013 +0200
     5.3 @@ -22,6 +22,7 @@
     5.4  
     5.5  /**
     5.6   * Provider API for Canvas. Implement this to add support for your platform.
     5.7 + *
     5.8   * @author antonepple
     5.9   */
    5.10  public interface GraphicsEnvironment {
    5.11 @@ -87,15 +88,32 @@
    5.12  ////
    5.13  ////    public void drawImage(ImageData image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height);
    5.14  //
    5.15 -    public void setFillStyle(String style);
    5.16  
    5.17 -    public String getFillStyle();
    5.18 +    /**
    5.19 +     * When implementing you can return an Object of your choice to enable
    5.20 +     * caching. Returning null means no caching. When caching is enabled, and
    5.21 +     * the cache hasn't been invalidated, the Object you returned will be passed
    5.22 +     * as a parameter.
    5.23 +     *
    5.24 +     * @param style The style object you should use to create your native style
    5.25 +     * @param nativeStyle your native object if cached, null otherwise
    5.26 +     * @return return native Object for caching
    5.27 +     *
    5.28 +     */
    5.29 +    public Object setFillStyle(Style style, Object nativeStyle);
    5.30  
    5.31 -    public void setFillStyle(Style style);
    5.32 -
    5.33 -    public void setStrokeStyle(String style);
    5.34 -
    5.35 -    public void setStrokeStyle(Style style);
    5.36 +    /**
    5.37 +     * When implementing you can return an Object of your choice to enable
    5.38 +     * caching. Returning null means no caching. When caching is enabled, and
    5.39 +     * the cache hasn't been invalidated, the Object you returned will be passed
    5.40 +     * as a parameter.
    5.41 +     *
    5.42 +     * @param style The style object you should use to create your native style
    5.43 +     * @param nativeStyle your native object if cached, null otherwise
    5.44 +     * @return return native Object for caching
    5.45 +     *
    5.46 +     */
    5.47 +    public Object setStrokeStyle(Style style, Object nativeStyle);
    5.48  
    5.49      public void setShadowColor(String color);
    5.50  
    5.51 @@ -105,8 +123,6 @@
    5.52  
    5.53      public void setShadowOffsetY(double y);
    5.54  
    5.55 -    public String getStrokeStyle();
    5.56 -
    5.57      public String getShadowColor();
    5.58  
    5.59      public double getShadowBlur();
    5.60 @@ -162,7 +178,6 @@
    5.61  ////    public void putImageData(ImageData imageData, double x, double y);
    5.62  ////
    5.63  ////    public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
    5.64 -
    5.65      public void setGlobalAlpha(double alpha);
    5.66  
    5.67      public double getGlobalAlpha();
    5.68 @@ -172,7 +187,6 @@
    5.69      public String getGlobalCompositeOperation();
    5.70  
    5.71  ////    public ImageData getImageForPath(String path);
    5.72 -
    5.73      public int getHeight();
    5.74  
    5.75      public int getWidth();