1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.image.*;
  30 import java.util.HashMap;
  31 
  32 import com.apple.laf.AquaImageFactory.RecyclableSlicedImageControl;
  33 import com.apple.laf.AquaImageFactory.NineSliceMetrics;
  34 import com.apple.laf.AquaImageFactory.SlicedImageControl;
  35 
  36 import sun.awt.image.*;
  37 import sun.java2d.*;
  38 import sun.print.*;
  39 import apple.laf.*;
  40 import apple.laf.JRSUIUtils.NineSliceMetricsProvider;
  41 import sun.awt.image.ImageCache;
  42 
  43 abstract class AquaPainter <T extends JRSUIState> {
  44     static <T extends JRSUIState> AquaPainter<T> create(final T state) {
  45         return new AquaSingleImagePainter<>(state);
  46     }
  47 
  48     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut) {
  49         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, true);
  50     }
  51 
  52     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle) {
  53         return create(state, minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, true, true);
  54     }
  55 
  56     static <T extends JRSUIState> AquaPainter<T> create(final T state, final int minWidth, final int minHeight, final int westCut, final int eastCut, final int northCut, final int southCut, final boolean useMiddle, final boolean stretchHorizontally, final boolean stretchVertically) {
  57         return create(state, new NineSliceMetricsProvider() {
  58             @Override
  59                public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state) {
  60                 return new NineSliceMetrics(minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically);
  61             }
  62         });
  63     }
  64 
  65     static <T extends JRSUIState> AquaPainter<T> create(final T state, final NineSliceMetricsProvider metricsProvider) {
  66         return new AquaNineSlicingImagePainter<>(state, metricsProvider);
  67     }
  68 
  69     abstract void paint(Graphics2D g, T stateToPaint);
  70 
  71     final Rectangle boundsRect = new Rectangle();
  72     final JRSUIControl control;
  73     T state;
  74     AquaPainter(final JRSUIControl control, final T state) {
  75         this.control = control;
  76         this.state = state;
  77     }
  78 
  79     final JRSUIControl getControl() {
  80         control.set(state = state.derive());
  81         return control;
  82     }
  83 
  84     final void paint(final Graphics g, final Component c, final int x,
  85                      final int y, final int w, final int h) {
  86         boundsRect.setBounds(x, y, w, h);
  87 
  88         final T nextState = state.derive();
  89         final Graphics2D g2d = getGraphics2D(g);
  90         if (g2d != null) paint(g2d, nextState);
  91         state = nextState;
  92     }
  93 
  94     private static class AquaNineSlicingImagePainter<T extends JRSUIState>
  95             extends AquaPainter<T> {
  96 
  97         private final HashMap<T, RecyclableJRSUISlicedImageControl> slicedControlImages;
  98         private final NineSliceMetricsProvider metricsProvider;
  99 
 100         AquaNineSlicingImagePainter(final T state) {
 101             this(state, null);
 102         }
 103 
 104         AquaNineSlicingImagePainter(final T state, final NineSliceMetricsProvider metricsProvider) {
 105             super(new JRSUIControl(false), state);
 106             this.metricsProvider = metricsProvider;
 107             slicedControlImages = new HashMap<>();
 108         }
 109 
 110         @Override
 111         void paint(final Graphics2D g, final T stateToPaint) {
 112             if (metricsProvider == null) {
 113                 AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 114                 return;
 115             }
 116 
 117             RecyclableJRSUISlicedImageControl slicesRef = slicedControlImages.get(stateToPaint);
 118             if (slicesRef == null) {
 119                 final NineSliceMetrics metrics = metricsProvider.getNineSliceMetricsForState(stateToPaint);
 120                 if (metrics == null) {
 121                     AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 122                     return;
 123                 }
 124                 slicesRef = new RecyclableJRSUISlicedImageControl(control, stateToPaint, metrics);
 125                 slicedControlImages.put(stateToPaint, slicesRef);
 126             }
 127             final SlicedImageControl slices = slicesRef.get();
 128             slices.paint(g, boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 129         }
 130     }
 131 
 132     private static final class AquaSingleImagePainter<T extends JRSUIState>
 133             extends AquaPainter<T> {
 134 
 135         AquaSingleImagePainter(final T state) {
 136             super(new JRSUIControl(false), state);
 137         }
 138 
 139         @Override
 140         void paint(final Graphics2D g, final T stateToPaint) {
 141             paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
 142         }
 143 
 144         static void paintFromSingleCachedImage(final Graphics2D g,
 145                 final JRSUIControl control, final JRSUIState controlState,
 146                 final Rectangle bounds) {
 147             if (bounds.width <= 0 || bounds.height <= 0) {
 148                 return;
 149             }
 150 
 151             int scale = 1;
 152             if (g instanceof SunGraphics2D) {
 153                 scale = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 154             }
 155             final GraphicsConfiguration config = g.getDeviceConfiguration();
 156             final ImageCache cache = ImageCache.getInstance();
 157             final int imgW = bounds.width * scale;
 158             final int imgH = bounds.height * scale;
 159             AquaPixelsKey key = new AquaPixelsKey(config,
 160                     imgW, imgH, scale, controlState);
 161             BufferedImage img = (BufferedImage) cache.getImage(key);
 162             if (img == null) {
 163                 img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB_PRE);
 164                 cache.setImage(key, img);
 165                 final WritableRaster raster = img.getRaster();
 166                 final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
 167 
 168                 control.set(controlState);
 169                 control.paint(SunWritableRaster.stealData(buffer, 0),
 170                         imgW, imgH, 0, 0, bounds.width, bounds.height);
 171                 SunWritableRaster.markDirty(buffer);
 172             }
 173 
 174             g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height, null);
 175         }
 176     }
 177 
 178     private static class AquaPixelsKey implements ImageCache.PixelsKey {
 179 
 180         private final int pixelCount;
 181         private final int hash;
 182 
 183         // key parts
 184         private final GraphicsConfiguration config;
 185         private final int w;
 186         private final int h;
 187         private final int scale;
 188         private final JRSUIState state;
 189 
 190         AquaPixelsKey(final GraphicsConfiguration config,
 191                 final int w, final int h, final int scale,
 192                 final JRSUIState state) {
 193             this.pixelCount = w * h;
 194             this.config = config;
 195             this.w = w;
 196             this.h = h;
 197             this.scale = scale;
 198             this.state = state;
 199             this.hash = hash();
 200         }
 201 
 202         public int getPixelCount() {
 203             return pixelCount;
 204         }
 205 
 206         private int hash() {
 207             int hash = config != null ? config.hashCode() : 0;
 208             hash = 31 * hash + w;
 209             hash = 31 * hash + h;
 210             hash = 31 * hash + scale;
 211             hash = 31 * hash + state.hashCode();
 212             return hash;
 213         }
 214 
 215         @Override
 216         public int hashCode() {
 217             return hash;
 218         }
 219 
 220         @Override
 221         public boolean equals(Object obj) {
 222             if (obj instanceof AquaPixelsKey) {
 223                 AquaPixelsKey key = (AquaPixelsKey) obj;
 224                 return config == key.config && w == key.w && h == key.h
 225                         && scale == key.scale && state.equals(key.state);
 226             }
 227             return false;
 228         }
 229     }
 230 
 231     private static class RecyclableJRSUISlicedImageControl
 232             extends RecyclableSlicedImageControl {
 233 
 234         private final JRSUIControl control;
 235         private final JRSUIState state;
 236 
 237         RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
 238             super(metrics);
 239             this.control = control;
 240             this.state = state;
 241         }
 242 
 243         @Override
 244         protected Image createTemplateImage(int width, int height) {
 245             BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);
 246 
 247             final WritableRaster raster = image.getRaster();
 248             final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
 249 
 250             control.set(state);
 251             control.paint(SunWritableRaster.stealData(buffer, 0), metrics.minW, metrics.minH, 0, 0, metrics.minW, metrics.minH);
 252 
 253             SunWritableRaster.markDirty(buffer);
 254 
 255             return image;
 256         }
 257     }
 258 
 259     private Graphics2D getGraphics2D(final Graphics g) {
 260         try {
 261             return (SunGraphics2D)g; // doing a blind try is faster than checking instanceof
 262         } catch (Exception ignored) {
 263             if (g instanceof PeekGraphics) {
 264                 // if it is a peek just dirty the region
 265                 g.fillRect(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
 266             } else if (g instanceof ProxyGraphics2D) {
 267                 final ProxyGraphics2D pg = (ProxyGraphics2D)g;
 268                 final Graphics2D g2d = pg.getDelegate();
 269                 if (g2d instanceof SunGraphics2D) {
 270                     return g2d;
 271                 }
 272             } else if (g instanceof Graphics2D) {
 273                 return (Graphics2D) g;
 274             }
 275         }
 276 
 277         return null;
 278     }
 279 }