Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/xr/XRUtils.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.AffineTransform;30import java.awt.image.*;31import sun.java2d.loops.*;32import static java.awt.AlphaComposite.*;3334/**35* XRender constants and utility methods.36*37* @author Clemens Eisserer38*/3940public class XRUtils {41public static final int None = 0;4243/* Composition Operators */44public static final byte PictOpClear = 0;45public static final byte PictOpSrc = 1;46public static final byte PictOpDst = 2;47public static final byte PictOpOver = 3;48public static final byte PictOpOverReverse = 4;49public static final byte PictOpIn = 5;50public static final byte PictOpInReverse = 6;51public static final byte PictOpOut = 7;52public static final byte PictOpOutReverse = 8;53public static final byte PictOpAtop = 9;54public static final byte PictOpAtopReverse = 10;55public static final byte PictOpXor = 11;56public static final byte PictOpAdd = 12;57public static final byte PictOpSaturate = 13;5859/* Repeats */60public static final int RepeatNone = 0;61public static final int RepeatNormal = 1;62public static final int RepeatPad = 2;63public static final int RepeatReflect = 3;6465/* Interpolation qualities */66public static final int FAST = 0;67public static final int GOOD = 1;68public static final int BEST = 2;69public static final byte[] FAST_NAME = "fast".getBytes();70public static final byte[] GOOD_NAME = "good".getBytes();71public static final byte[] BEST_NAME = "best".getBytes();7273/* PictFormats */74public static final int PictStandardARGB32 = 0;75public static final int PictStandardRGB24 = 1;76public static final int PictStandardA8 = 2;77public static final int PictStandardA4 = 3;78public static final int PictStandardA1 = 4;7980/**81* Maps the specified affineTransformOp to the corresponding XRender image82* filter.83*/84public static int ATransOpToXRQuality(int affineTranformOp) {8586switch (affineTranformOp) {87case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:88return FAST;8990case AffineTransformOp.TYPE_BILINEAR:91return GOOD;9293case AffineTransformOp.TYPE_BICUBIC:94return BEST;95}9697return -1;98}99100/**101* Maps the specified affineTransformOp to the corresponding XRender image102* filter.103*/104public static byte[] ATransOpToXRQualityName(int affineTranformOp) {105106switch (affineTranformOp) {107case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:108return FAST_NAME;109110case AffineTransformOp.TYPE_BILINEAR:111return GOOD_NAME;112113case AffineTransformOp.TYPE_BICUBIC:114return BEST_NAME;115}116117return null;118}119120121public static byte[] getFilterName(int filterType) {122switch (filterType) {123case FAST:124return FAST_NAME;125case GOOD:126return GOOD_NAME;127case BEST:128return BEST_NAME;129}130131return null;132}133134135/**136* Returns the XRender picture Format which is required to fullfill the137* Java2D transparency requirement.138*/139public static int getPictureFormatForTransparency(int transparency) {140switch (transparency) {141case Transparency.OPAQUE:142return PictStandardRGB24;143144case Transparency.BITMASK:145case Transparency.TRANSLUCENT:146return PictStandardARGB32;147}148149return -1;150}151152153public static SurfaceType getXRSurfaceTypeForTransparency(int transparency) {154if (transparency == Transparency.OPAQUE) {155return SurfaceType.IntRgb;156}else {157return SurfaceType.IntArgbPre;158}159}160161/**162* Maps Java2D CycleMethod to XRender's Repeat property.163*/164public static int getRepeatForCycleMethod(CycleMethod cycleMethod) {165if (cycleMethod.equals(CycleMethod.NO_CYCLE)) {166return RepeatPad;167} else if (cycleMethod.equals(CycleMethod.REFLECT)) {168return RepeatReflect;169} else if (cycleMethod.equals(CycleMethod.REPEAT)) {170return RepeatNormal;171}172173return RepeatNone;174}175176/**177* Converts a double into an XFixed.178*/179public static int XDoubleToFixed(double dbl) {180return (int) (dbl * 65536);181}182183public static double XFixedToDouble(int fixed) {184return ((double) fixed) / 65536;185}186187public static int[] convertFloatsToFixed(float[] values) {188int[] fixed = new int[values.length];189190for (int i = 0; i < values.length; i++) {191fixed[i] = XDoubleToFixed(values[i]);192}193194return fixed;195}196197public static long intToULong(int signed) {198if (signed < 0) {199return ((long) signed) + (((long) Integer.MAX_VALUE) -200((long) Integer.MIN_VALUE) + 1);201}202203return signed;204}205206/**207* Maps the specified Java2D composition rule, to the corresponding XRender208* composition rule.209*/210public static byte j2dAlphaCompToXR(int j2dRule) {211switch (j2dRule) {212case CLEAR:213return PictOpClear;214215case SRC:216return PictOpSrc;217218case DST:219return PictOpDst;220221case SRC_OVER:222return PictOpOver;223224case DST_OVER:225return PictOpOverReverse;226227case SRC_IN:228return PictOpIn;229230case DST_IN:231return PictOpInReverse;232233case SRC_OUT:234return PictOpOut;235236case DST_OUT:237return PictOpOutReverse;238239case SRC_ATOP:240return PictOpAtop;241242case DST_ATOP:243return PictOpAtopReverse;244245case XOR:246return PictOpXor;247}248249throw new InternalError("No XRender equivalent available for requested java2d composition rule: "+j2dRule);250}251252public static short clampToShort(int x) {253return (short) (x > Short.MAX_VALUE254? Short.MAX_VALUE255: (x < Short.MIN_VALUE ? Short.MIN_VALUE : x));256}257258public static int clampToUShort(int x) {259return (x > 65535 ? 65535 : (x < 0) ? 0 : x);260}261262public static boolean isTransformQuadrantRotated(AffineTransform tr) {263return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |264AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);265}266267public static boolean isMaskEvaluated(byte xrCompRule) {268switch (xrCompRule) {269case PictOpOver:270case PictOpOverReverse:271case PictOpAtop:272case PictOpXor:273return true;274}275276return false;277}278}279280281