Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRPaints.java
41159 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.awt.*;28import java.awt.MultipleGradientPaint.*;29import java.awt.geom.*;30import java.awt.image.*;31import sun.java2d.*;32import sun.java2d.loops.*;33import sun.java2d.xr.XRSurfaceData.XRInternalSurfaceData;3435abstract class XRPaints {36static XRCompositeManager xrCompMan;3738static final XRGradient xrGradient = new XRGradient();39static final XRLinearGradient xrLinearGradient = new XRLinearGradient();40static final XRRadialGradient xrRadialGradient = new XRRadialGradient();41static final XRTexture xrTexture = new XRTexture();4243public static void register(XRCompositeManager xrComp) {44xrCompMan = xrComp;45}4647private static XRPaints getXRPaint(SunGraphics2D sg2d) {48switch (sg2d.paintState) {49case SunGraphics2D.PAINT_GRADIENT:50return xrGradient;5152case SunGraphics2D.PAINT_LIN_GRADIENT:53return xrLinearGradient;5455case SunGraphics2D.PAINT_RAD_GRADIENT:56return xrRadialGradient;5758case SunGraphics2D.PAINT_TEXTURE:59return xrTexture;6061default:62return null;63}64}6566/**67* Attempts to locate an implementation corresponding to the paint state of68* the provided SunGraphics2D object. If no implementation can be found, or69* if the paint cannot be accelerated under the conditions of the70* SunGraphics2D, this method returns false; otherwise, returns true.71*/72static boolean isValid(SunGraphics2D sg2d) {73XRPaints impl = getXRPaint(sg2d);74return (impl != null && impl.isPaintValid(sg2d));75}7677static void setPaint(SunGraphics2D sg2d, Paint paint) {78XRPaints impl = getXRPaint(sg2d);79if (impl != null) {80impl.setXRPaint(sg2d, paint);81}82}8384/**85* Returns true if this implementation is able to accelerate the Paint86* object associated with, and under the conditions of, the provided87* SunGraphics2D instance; otherwise returns false.88*/89abstract boolean isPaintValid(SunGraphics2D sg2d);9091abstract void setXRPaint(SunGraphics2D sg2d, Paint paint);9293private static class XRGradient extends XRPaints {94private XRGradient() {95}9697@Override98boolean isPaintValid(SunGraphics2D sg2d) {99GradientPaint paint = (GradientPaint) sg2d.paint;100101return XRUtils.isPointCoordInShortRange(paint.getPoint1())102&& XRUtils.isPointCoordInShortRange(paint.getPoint2());103}104105@Override106void setXRPaint(SunGraphics2D sg2d, Paint pt) {107GradientPaint paint = (GradientPaint) pt;108109int repeat = paint.isCyclic() ? XRUtils.RepeatReflect : XRUtils.RepeatPad;110float[] fractions = {0, 1};111int[] pixels = convertToIntArgbPixels(new Color[] { paint.getColor1(), paint.getColor2() });112113Point2D pt1 = paint.getPoint1();114Point2D pt2 = paint.getPoint2();115116XRBackend con = xrCompMan.getBackend();117int gradient = con.createLinearGradient(pt1, pt2, fractions, pixels, repeat);118xrCompMan.setGradientPaint(new XRSurfaceData.XRInternalSurfaceData(con, gradient));119}120}121122public int getGradientLength(Point2D pt1, Point2D pt2) {123double xDiff = Math.max(pt1.getX(), pt2.getX()) - Math.min(pt1.getX(), pt2.getX());124double yDiff = Math.max(pt1.getY(), pt2.getY()) - Math.min(pt1.getY(), pt2.getY());125return (int) Math.ceil(Math.sqrt(xDiff*xDiff + yDiff*yDiff));126}127128private static class XRLinearGradient extends XRPaints {129130@Override131boolean isPaintValid(SunGraphics2D sg2d) {132LinearGradientPaint paint = (LinearGradientPaint) sg2d.getPaint();133134return paint.getColorSpace() == ColorSpaceType.SRGB135&& XRUtils.isPointCoordInShortRange(paint.getStartPoint())136&& XRUtils.isPointCoordInShortRange(paint.getEndPoint())137&& paint.getTransform().getDeterminant() != 0.0;138}139140@Override141void setXRPaint(SunGraphics2D sg2d, Paint pt) {142LinearGradientPaint paint = (LinearGradientPaint) pt;143Color[] colors = paint.getColors();144Point2D pt1 = paint.getStartPoint();145Point2D pt2 = paint.getEndPoint();146int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());147float[] fractions = paint.getFractions();148int[] pixels = convertToIntArgbPixels(colors);149AffineTransform at = paint.getTransform();150151try {152at.invert();153} catch (NoninvertibleTransformException ex) {154ex.printStackTrace();155}156157XRBackend con = xrCompMan.getBackend();158int gradient = con.createLinearGradient(pt1, pt2, fractions, pixels, repeat);159XRInternalSurfaceData x11sd = new XRSurfaceData.XRInternalSurfaceData(con, gradient);160x11sd.setStaticSrcTx(at);161xrCompMan.setGradientPaint(x11sd);162}163}164165private static class XRRadialGradient extends XRPaints {166167@Override168boolean isPaintValid(SunGraphics2D sg2d) {169RadialGradientPaint grad = (RadialGradientPaint) sg2d.paint;170171return grad.getColorSpace() == ColorSpaceType.SRGB172&& grad.getFocusPoint().equals(grad.getCenterPoint())173&& XRUtils.isPointCoordInShortRange(grad.getCenterPoint())174&& grad.getRadius() <= Short.MAX_VALUE175&& grad.getTransform().getDeterminant() != 0.0;176}177178@Override179void setXRPaint(SunGraphics2D sg2d, Paint pt) {180RadialGradientPaint paint = (RadialGradientPaint) pt;181Color[] colors = paint.getColors();182Point2D center = paint.getCenterPoint();183float cx = (float) center.getX();184float cy = (float) center.getY();185186AffineTransform at = paint.getTransform();187int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());188float[] fractions = paint.getFractions();189int[] pixels = convertToIntArgbPixels(colors);190float radius = paint.getRadius();191192try {193at.invert();194} catch (NoninvertibleTransformException ex) {195ex.printStackTrace();196}197198XRBackend con = xrCompMan.getBackend();199int gradient = con.createRadialGradient(cx, cy, 0, radius, fractions, pixels, repeat);200XRInternalSurfaceData x11sd = new XRSurfaceData.XRInternalSurfaceData(con, gradient);201x11sd.setStaticSrcTx(at);202xrCompMan.setGradientPaint(x11sd);203}204}205206private static class XRTexture extends XRPaints {207208private XRSurfaceData getAccSrcSurface(XRSurfaceData dstData, BufferedImage bi) {209// REMIND: this is a hack that attempts to cache the system210// memory image from the TexturePaint instance into an211// XRender pixmap...212SurfaceData srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);213if (!(srcData instanceof XRSurfaceData)) {214srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);215if (!(srcData instanceof XRSurfaceData)) {216throw new InternalError("Surface not cachable");217}218}219220return (XRSurfaceData) srcData;221}222223@Override224boolean isPaintValid(SunGraphics2D sg2d) {225TexturePaint paint = (TexturePaint) sg2d.paint;226BufferedImage bi = paint.getImage();227XRSurfaceData dstData = (XRSurfaceData) sg2d.getDestSurface();228229return getAccSrcSurface(dstData, bi) != null;230}231232@Override233void setXRPaint(SunGraphics2D sg2d, Paint pt) {234TexturePaint paint = (TexturePaint) pt;235BufferedImage bi = paint.getImage();236Rectangle2D anchor = paint.getAnchorRect();237238XRSurfaceData dstData = (XRSurfaceData) sg2d.surfaceData;239XRSurfaceData srcData = getAccSrcSurface(dstData, bi);240241AffineTransform at = new AffineTransform();242at.translate(anchor.getX(), anchor.getY());243at.scale(anchor.getWidth() / ((double) bi.getWidth()), anchor.getHeight() / ((double) bi.getHeight()));244245try {246at.invert();247} catch (NoninvertibleTransformException ex) {248at.setToIdentity();249}250srcData.setStaticSrcTx(at);251252srcData.validateAsSource(at, XRUtils.RepeatNormal, XRUtils.ATransOpToXRQuality(sg2d.interpolationType));253xrCompMan.setTexturePaint(srcData);254}255}256257public int[] convertToIntArgbPixels(Color[] colors) {258int[] pixels = new int[colors.length];259for (int i = 0; i < colors.length; i++) {260pixels[i] = colorToIntArgbPixel(colors[i]);261}262return pixels;263}264265public int colorToIntArgbPixel(Color c) {266int rgb = c.getRGB();267int a = Math.round(xrCompMan.getExtraAlpha() * (rgb >>> 24));268return ((a << 24) | (rgb & 0x00FFFFFF));269}270}271272273