Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/xr/XRPaints.java
32288 views
/*1* Copyright (c) 2010, 2013, 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/**98* There are no restrictions for accelerating GradientPaint, so this99* method always returns true.100*/101@Override102boolean isPaintValid(SunGraphics2D sg2d) {103return true;104}105106void 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) {132return ((LinearGradientPaint) sg2d.getPaint()).getColorSpace() == ColorSpaceType.SRGB;133}134135@Override136void setXRPaint(SunGraphics2D sg2d, Paint pt) {137LinearGradientPaint paint = (LinearGradientPaint) pt;138139Color[] colors = paint.getColors();140Point2D pt1 = paint.getStartPoint();141Point2D pt2 = paint.getEndPoint();142143int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());144float[] fractions = paint.getFractions();145int[] pixels = convertToIntArgbPixels(colors);146147AffineTransform at = paint.getTransform();148try {149at.invert();150} catch (NoninvertibleTransformException ex) {151ex.printStackTrace();152}153154XRBackend con = xrCompMan.getBackend();155int gradient = con.createLinearGradient(pt1, pt2, fractions, pixels, repeat);156XRInternalSurfaceData x11sd = new XRSurfaceData.XRInternalSurfaceData(con, gradient);157x11sd.setStaticSrcTx(at);158xrCompMan.setGradientPaint(x11sd);159}160}161162private static class XRRadialGradient extends XRPaints {163164@Override165boolean isPaintValid(SunGraphics2D sg2d) {166RadialGradientPaint grad = (RadialGradientPaint) sg2d.paint;167return grad.getFocusPoint().equals(grad.getCenterPoint())168&& grad.getColorSpace() == ColorSpaceType.SRGB;169}170171@Override172void setXRPaint(SunGraphics2D sg2d, Paint pt) {173RadialGradientPaint paint = (RadialGradientPaint) pt;174Color[] colors = paint.getColors();175Point2D center = paint.getCenterPoint();176177int repeat = XRUtils.getRepeatForCycleMethod(paint.getCycleMethod());178float[] fractions = paint.getFractions();179int[] pixels = convertToIntArgbPixels(colors);180float radius = paint.getRadius();181182float cx = (float) center.getX();183float cy = (float) center.getY();184185AffineTransform at = paint.getTransform();186try {187at.invert();188} catch (NoninvertibleTransformException ex) {189ex.printStackTrace();190}191192XRBackend con = xrCompMan.getBackend();193int gradient = con.createRadialGradient(cx, cy, 0, radius, fractions, pixels, repeat);194XRInternalSurfaceData x11sd = new XRSurfaceData.XRInternalSurfaceData(con, gradient);195x11sd.setStaticSrcTx(at);196xrCompMan.setGradientPaint(x11sd);197}198}199200private static class XRTexture extends XRPaints {201202private XRSurfaceData getAccSrcSurface(XRSurfaceData dstData, BufferedImage bi) {203// REMIND: this is a hack that attempts to cache the system204// memory image from the TexturePaint instance into an205// XRender pixmap...206SurfaceData srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);207if (!(srcData instanceof XRSurfaceData)) {208srcData = dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, CompositeType.SrcOver, null);209if (!(srcData instanceof XRSurfaceData)) {210throw new InternalError("Surface not cachable");211}212}213214return (XRSurfaceData) srcData;215}216217@Override218boolean isPaintValid(SunGraphics2D sg2d) {219TexturePaint paint = (TexturePaint) sg2d.paint;220BufferedImage bi = paint.getImage();221XRSurfaceData dstData = (XRSurfaceData) sg2d.getDestSurface();222223return getAccSrcSurface(dstData, bi) != null;224}225226@Override227void setXRPaint(SunGraphics2D sg2d, Paint pt) {228TexturePaint paint = (TexturePaint) pt;229BufferedImage bi = paint.getImage();230Rectangle2D anchor = paint.getAnchorRect();231232XRSurfaceData dstData = (XRSurfaceData) sg2d.surfaceData;233XRSurfaceData srcData = (XRSurfaceData) getAccSrcSurface(dstData, bi);234235AffineTransform at = new AffineTransform();236at.translate(anchor.getX(), anchor.getY());237at.scale(anchor.getWidth() / ((double) bi.getWidth()), anchor.getHeight() / ((double) bi.getHeight()));238239try {240at.invert();241} catch (NoninvertibleTransformException ex) {242at.setToIdentity();243}244srcData.setStaticSrcTx(at);245246srcData.validateAsSource(at, XRUtils.RepeatNormal, XRUtils.ATransOpToXRQuality(sg2d.interpolationType));247xrCompMan.setTexturePaint(srcData);248}249}250251public int[] convertToIntArgbPixels(Color[] colors) {252int[] pixels = new int[colors.length];253for (int i = 0; i < colors.length; i++) {254pixels[i] = colorToIntArgbPixel(colors[i]);255}256return pixels;257}258259public int colorToIntArgbPixel(Color c) {260int rgb = c.getRGB();261int a = (int) Math.round(xrCompMan.getExtraAlpha() * (rgb >>> 24));262return ((a << 24) | (rgb & 0x00FFFFFF));263}264}265266267