aboutsummaryrefslogtreecommitdiff
path: root/src/solaris/classes/sun/java2d/xr/XRPaints.java
blob: 42dec990ffdc1b6d89e1dcf3f9cf64e6eb4760d3 (plain)
1
2
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
 * 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.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.java2d.xr;

import java.awt.*;
import java.awt.MultipleGradientPaint.*;
import java.awt.geom.*;
import java.awt.image.*;

import sun.java2d.*;
import sun.java2d.loops.*;
import sun.java2d.pipe.*;

abstract class XRPaints {
    static XRCompositeManager xrCompMan;

    static final XRGradient xrGradient = new XRGradient();
    static final XRLinearGradient xrLinearGradient = new XRLinearGradient();
    static final XRRadialGradient xrRadialGradient = new XRRadialGradient();
    static final XRTexture xrTexture = new XRTexture();

    public static void register(XRCompositeManager xrComp) {
        xrCompMan = xrComp;
    }

    private static XRPaints getXRPaint(SunGraphics2D sg2d) {
        switch (sg2d.paintState) {
        case SunGraphics2D.PAINT_GRADIENT:
            return xrGradient;

        case SunGraphics2D.PAINT_LIN_GRADIENT:
            return xrLinearGradient;

        case SunGraphics2D.PAINT_RAD_GRADIENT:
            return xrRadialGradient;

        case SunGraphics2D.PAINT_TEXTURE:
            return xrTexture;

        default:
            return null;
        }
    }

    /**
     * Attempts to locate an implementation corresponding to the paint state of
     * the provided SunGraphics2D object. If no implementation can be found, or
     * if the paint cannot be accelerated under the conditions of the
     * SunGraphics2D, this method returns false; otherwise, returns true.
     */
    static boolean isValid(SunGraphics2D sg2d) {
        XRPaints impl = getXRPaint(sg2d);
        return (impl != null && impl.isPaintValid(sg2d));
    }

    static void setPaint(SunGraphics2D sg2d, Paint paint) {
        XRPaints impl = getXRPaint(sg2d);
        if (impl != null) {
            impl.setXRPaint(sg2d, paint);
        }
    }

    /**
     * Returns true if this implementation is able to accelerate the Paint
     * object associated with, and under the conditions of, the provided
     * SunGraphics2D instance; otherwise returns false.
     */
    abstract boolean isPaintValid(SunGraphics2D sg2d);

    abstract void setXRPaint(SunGraphics2D sg2d, Paint paint);

    private static class XRGradient extends XRPaints {
        private XRGradient() {
        }

        /**
         * There are no restrictions for accelerating GradientPaint, so this
         * method always returns true.
         */
        @Override
        boolean isPaintValid(SunGraphics2D sg2d) {
            return true;
        }

        void setXRPaint(SunGraphics2D sg2d, Paint pt) {
            GradientPaint paint = (GradientPaint) pt;

            int[] pixels = convertToIntArgbPixels(new Color[] { paint.getColor1(), paint.getColor2() }, false);

            float fractions[] = new float[2];
            fractions[0] = 0;
            fractions[1] = 1;

            Point2D pt1 = paint.getPoint1();
            Point2D pt2 = paint.getPoint2();

            AffineTransform at = (AffineTransform) sg2d.transform.clone();
            try {
                at.invert();
            } catch (NoninvertibleTransformException ex) {
                at.setToIdentity();
            }

            int repeat = paint.isCyclic() ? XRUtils.RepeatReflect : XRUtils.RepeatPad;

            XRBackend con = xrCompMan.getBackend();
            int gradient = con.createLinearGradient(pt1, pt2, fractions, pixels, repeat, at);
            xrCompMan.setGradientPaint(new XRSurfaceData.XRInternalSurfaceData(con, gradient, at));
        }
    }

    public int getGradientLength(Point2D pt1, Point2D pt2) {
           double xDiff = Math.max(pt1.getX(), pt2.getX()) - Math.min(pt1.getX(), pt2.getX());
           double yDiff = Math.max(pt1.getY(), pt2.getY()) - Math.min(pt1.getY(), pt2.getY());
           return (int) Math.ceil(Math.sqrt(xDiff*xDiff + yDiff*yDiff));
    }

    private static class XRLinearGradient extends XRPaints {

        @Override
        boolean isPaintValid(SunGraphics2D sg2d) {
            return true;
        }

        @Override
        void setXRPaint(SunGraphics2D sg2d, Paint pt) {
            LinearGradientPaint paint = (LinearGradientPaint) pt;
            boolean linear = (paint.getColorSpace() == ColorSpaceType.LINEAR_RGB);

            Color[] colors = paint.getColors();
            Point2D pt1 = paint.getStartPoint();
            Point2D pt2 = paint.getEndPoint();


            AffineTransform at = paint.getTransform();
            at.preConcatenate(sg2d.transform);

            int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());
            float[] fractions = paint.getFractions();
            int[] pixels = convertToIntArgbPixels(colors, linear);

            try {
                at.invert();
            } catch (NoninvertibleTransformException ex) {
                ex.printStackTrace();
            }

            XRBackend con = xrCompMan.getBackend();
            int gradient = con.createLinearGradient(pt1, pt2, fractions, pixels, repeat, at);
            xrCompMan.setGradientPaint(new XRSurfaceData.XRInternalSurfaceData(con, gradient, at));
        }
    }

    private static class XRRadialGradient extends XRPaints {

        @Override
        boolean isPaintValid(SunGraphics2D sg2d) {
            RadialGradientPaint grad = (RadialGradientPaint) sg2d.paint;
            return grad.getFocusPoint().equals(grad.getCenterPoint());
        }

        @Override
        void setXRPaint(SunGraphics2D sg2d, Paint pt) {
            RadialGradientPaint paint = (RadialGradientPaint) pt;
            boolean linear = (paint.getColorSpace() == ColorSpaceType.LINEAR_RGB);
            Color[] colors = paint.getColors();
            Point2D center = paint.getCenterPoint();
            Point2D focus = paint.getFocusPoint();

            int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());
            float[] fractions = paint.getFractions();
            int[] pixels = convertToIntArgbPixels(colors, linear);
            float radius = paint.getRadius();

            // save original (untransformed) center and focus points
            double cx = center.getX();
            double cy = center.getY();
            double fx = focus.getX();
            double fy = focus.getY();

            AffineTransform at = paint.getTransform();
            at.preConcatenate(sg2d.transform);
            focus = at.transform(focus, focus);

            // transform unit circle to gradient coords; we start with the
            // unit circle (center=(0,0), focus on positive x-axis, radius=1)
            // and then transform into gradient space
            at.translate(cx, cy);
            at.rotate(fx - cx, fy - cy);
            // at.scale(radius, radius);

            // invert to get mapping from device coords to unit circle
            try {
                at.invert();
            } catch (Exception e) {
                at.setToScale(0.0, 0.0);
            }
            focus = at.transform(focus, focus);

            // clamp the focus point so that it does not rest on, or outside
            // of, the circumference of the gradient circle
            fx = Math.min(focus.getX(), 0.99);

            XRBackend con = xrCompMan.getBackend();
            int gradient = con.createRadialGradient(new Point2D.Float(0, 0), new Point2D.Float(0, 0), 0, radius, fractions, pixels, repeat, at);
            xrCompMan.setGradientPaint(new XRSurfaceData.XRInternalSurfaceData(con, gradient, at));
        }
    }

    private static class XRTexture extends XRPaints {

        @Override
        boolean isPaintValid(SunGraphics2D sg2d) {
            TexturePaint paint = (TexturePaint) sg2d.paint;
            BufferedImage bi = paint.getImage();
            XRSurfaceData dstData = (XRSurfaceData) sg2d.getDestSurface();

            SurfaceData srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);
            if (!(srcData instanceof XRSurfaceData)) {
                // REMIND: this is a hack that attempts to cache the system
                // memory image from the TexturePaint instance into an
                // OpenGL texture...
                srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);
                if (!(srcData instanceof XRSurfaceData)) {
                    return false;
                }
            }

            return true;
        }

        @Override
        void setXRPaint(SunGraphics2D sg2d, Paint pt) {
            TexturePaint paint = (TexturePaint) pt;

            BufferedImage bi = paint.getImage();
            SurfaceData dstData = sg2d.surfaceData;
            SurfaceData srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);

            // REMIND: this hack tries to ensure that we have a cached texture
            if (!(srcData instanceof XRSurfaceData)) {
                srcData = dstData.getSourceSurfaceData(paint.getImage(), SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);
                if (!(srcData instanceof XRSurfaceData)) {
                    throw new InternalError("Surface not cachable");
                }
            }

            XRSurfaceData x11SrcData = (XRSurfaceData) srcData;

            AffineTransform at = (AffineTransform) sg2d.transform.clone();
            Rectangle2D anchor = paint.getAnchorRect();
            at.translate(anchor.getX(), anchor.getY());
            at.scale(anchor.getWidth() / ((double) bi.getWidth()), anchor.getHeight() / ((double) bi.getHeight()));

            try {
                at.invert();
            } catch (NoninvertibleTransformException ex) {
                at.setToIdentity(); /* TODO: Right thing to do in this case? */
            }

            x11SrcData.validateAsSource(at, XRUtils.RepeatNormal, XRUtils.ATransOpToXRQuality(sg2d.interpolationType));
            xrCompMan.setTexturePaint(((XRSurfaceData) srcData));
        }
    }

    public int[] convertToIntArgbPixels(Color[] colors, boolean linear) {
        int[] pixels = new int[colors.length];
        for (int i = 0; i < colors.length; i++) {
            pixels[i] = colorToIntArgbPixel(colors[i], linear);
        }
        return pixels;
    }

    public int colorToIntArgbPixel(Color c, boolean linear) {
        int rgb = c.getRGB();

        int a = rgb >>> 24;
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = (rgb) & 0xff;
        if (linear) {
            r = BufferedPaints.convertSRGBtoLinearRGB(r);
            g = BufferedPaints.convertSRGBtoLinearRGB(g);
            b = BufferedPaints.convertSRGBtoLinearRGB(b);
        }

        a *= xrCompMan.getExtraAlpha();

        return ((a << 24) | (r << 16) | (g << 8) | (b));
    }
}