AquaPainter.java 11.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.apple.laf;

import java.awt.*;
import java.awt.image.*;
import java.util.HashMap;

import com.apple.laf.AquaImageFactory.RecyclableSlicedImageControl;
import com.apple.laf.AquaImageFactory.NineSliceMetrics;
import com.apple.laf.AquaImageFactory.SlicedImageControl;

import sun.awt.image.*;
import sun.java2d.*;
import sun.print.*;
import apple.laf.*;
import apple.laf.JRSUIUtils.NineSliceMetricsProvider;
41
import sun.awt.image.ImageCache;
42 43 44

abstract class AquaPainter <T extends JRSUIState> {
    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 57 58 59 60 61 62 63 64 65
    }

    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) {
        return create(state, new NineSliceMetricsProvider() {
            @Override
               public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state) {
                return new NineSliceMetrics(minWidth, minHeight, westCut, eastCut, northCut, southCut, useMiddle, stretchHorizontally, stretchVertically);
            }
        });
    }

    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 72 73

    final Rectangle boundsRect = new Rectangle();
    final JRSUIControl control;
    T state;
74
    AquaPainter(final JRSUIControl control, final T state) {
75 76 77 78
        this.control = control;
        this.state = state;
    }

79 80
    final JRSUIControl getControl() {
        control.set(state = state.derive());
81 82 83
        return control;
    }

84 85
    final void paint(final Graphics g, final Component c, final int x,
                     final int y, final int w, final int h) {
86 87
        boundsRect.setBounds(x, y, w, h);

88
        final T nextState = state.derive();
89
        final Graphics2D g2d = getGraphics2D(g);
90
        if (g2d != null) paint(g2d, nextState);
91 92 93
        state = nextState;
    }

94 95 96 97 98
    private static class AquaNineSlicingImagePainter<T extends JRSUIState>
            extends AquaPainter<T> {

        private final HashMap<T, RecyclableJRSUISlicedImageControl> slicedControlImages;
        private final NineSliceMetricsProvider metricsProvider;
99

100
        AquaNineSlicingImagePainter(final T state) {
101 102 103
            this(state, null);
        }

104
        AquaNineSlicingImagePainter(final T state, final NineSliceMetricsProvider metricsProvider) {
105 106
            super(new JRSUIControl(false), state);
            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 115 116 117 118 119 120
                return;
            }

            RecyclableJRSUISlicedImageControl slicesRef = slicedControlImages.get(stateToPaint);
            if (slicesRef == null) {
                final NineSliceMetrics metrics = metricsProvider.getNineSliceMetricsForState(stateToPaint);
                if (metrics == null) {
121
                    AquaSingleImagePainter.paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
122 123 124 125 126 127 128 129 130 131
                    return;
                }
                slicesRef = new RecyclableJRSUISlicedImageControl(control, stateToPaint, metrics);
                slicedControlImages.put(stateToPaint, slicesRef);
            }
            final SlicedImageControl slices = slicesRef.get();
            slices.paint(g, boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
        }
    }

132 133 134
    private static final class AquaSingleImagePainter<T extends JRSUIState>
            extends AquaPainter<T> {

135
        AquaSingleImagePainter(final T state) {
136 137 138 139
            super(new JRSUIControl(false), state);
        }

        @Override
140 141
        void paint(final Graphics2D g, final T stateToPaint) {
            paintFromSingleCachedImage(g, control, stateToPaint, boundsRect);
142 143
        }

144 145 146 147 148 149
        static void paintFromSingleCachedImage(final Graphics2D g,
                final JRSUIControl control, final JRSUIState controlState,
                final Rectangle bounds) {
            if (bounds.width <= 0 || bounds.height <= 0) {
                return;
            }
150 151 152

            final GraphicsConfiguration config = g.getDeviceConfiguration();
            final ImageCache cache = ImageCache.getInstance();
153 154
            final int width = bounds.width;
            final int height = bounds.height;
155
            AquaPixelsKey key = new AquaPixelsKey(config,
156 157
                    width, height, bounds, controlState);
            Image img = (BufferedImage) cache.getImage(key);
158
            if (img == null) {
159 160 161 162 163 164 165 166

                Image baseImage = createImage(width, height, bounds, control,
                        controlState);

                img = new MultiResolutionBufferedImage(baseImage,
                        (rvWidth, rvHeight) -> createImage(rvWidth, rvHeight,
                                bounds, control, controlState));

167 168 169
                if (!controlState.is(JRSUIConstants.Animating.YES)) {
                    cache.setImage(key, img);
                }
170
            }
171

172
            g.drawImage(img, bounds.x, bounds.y, bounds.width, bounds.height, null);
173
        }
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

        private static Image createImage(int imgW, int imgH, final Rectangle bounds,
                final JRSUIControl control, JRSUIState controlState) {
            BufferedImage img = new BufferedImage(imgW, imgH,
                    BufferedImage.TYPE_INT_ARGB_PRE);

            final WritableRaster raster = img.getRaster();
            final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();

            control.set(controlState);
            control.paint(SunWritableRaster.stealData(buffer, 0),
                    imgW, imgH, 0, 0, bounds.width, bounds.height);
            SunWritableRaster.markDirty(buffer);
            return img;
        }
189 190
    }

191 192 193 194 195 196 197 198 199
    private static class AquaPixelsKey implements ImageCache.PixelsKey {

        private final int pixelCount;
        private final int hash;

        // key parts
        private final GraphicsConfiguration config;
        private final int w;
        private final int h;
200
        private final Rectangle bounds;
201 202 203
        private final JRSUIState state;

        AquaPixelsKey(final GraphicsConfiguration config,
204
                final int w, final int h, final Rectangle bounds,
205 206 207 208 209
                final JRSUIState state) {
            this.pixelCount = w * h;
            this.config = config;
            this.w = w;
            this.h = h;
210
            this.bounds = bounds;
211 212 213 214 215 216 217 218 219 220 221 222
            this.state = state;
            this.hash = hash();
        }

        public int getPixelCount() {
            return pixelCount;
        }

        private int hash() {
            int hash = config != null ? config.hashCode() : 0;
            hash = 31 * hash + w;
            hash = 31 * hash + h;
223
            hash = 31 * hash + bounds.hashCode();
224 225 226 227 228 229 230 231 232 233 234 235 236 237
            hash = 31 * hash + state.hashCode();
            return hash;
        }

        @Override
        public int hashCode() {
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof AquaPixelsKey) {
                AquaPixelsKey key = (AquaPixelsKey) obj;
                return config == key.config && w == key.w && h == key.h
238
                        && bounds.equals(key.bounds) && state.equals(key.state);
239 240 241 242 243
            }
            return false;
        }
    }

244 245 246 247 248
    private static class RecyclableJRSUISlicedImageControl
            extends RecyclableSlicedImageControl {

        private final JRSUIControl control;
        private final JRSUIState state;
249

250
        RecyclableJRSUISlicedImageControl(final JRSUIControl control, final JRSUIState state, final NineSliceMetrics metrics) {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            super(metrics);
            this.control = control;
            this.state = state;
        }

        @Override
        protected Image createTemplateImage(int width, int height) {
            BufferedImage image = new BufferedImage(metrics.minW, metrics.minH, BufferedImage.TYPE_INT_ARGB_PRE);

            final WritableRaster raster = image.getRaster();
            final DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();

            control.set(state);
            control.paint(SunWritableRaster.stealData(buffer, 0), metrics.minW, metrics.minH, 0, 0, metrics.minW, metrics.minH);

            SunWritableRaster.markDirty(buffer);

            return image;
        }
    }

272
    private Graphics2D getGraphics2D(final Graphics g) {
273 274
        try {
            return (SunGraphics2D)g; // doing a blind try is faster than checking instanceof
275
        } catch (Exception ignored) {
276 277 278 279 280 281
            if (g instanceof PeekGraphics) {
                // if it is a peek just dirty the region
                g.fillRect(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
            } else if (g instanceof ProxyGraphics2D) {
                final ProxyGraphics2D pg = (ProxyGraphics2D)g;
                final Graphics2D g2d = pg.getDelegate();
282 283 284
                if (g2d instanceof SunGraphics2D) {
                    return g2d;
                }
285 286 287 288 289 290 291 292
            } else if (g instanceof Graphics2D) {
                return (Graphics2D) g;
            }
        }

        return null;
    }
}