1 /* 2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.java2d.d3d; 27 28 import java.awt.LinearGradientPaint; 29 import java.awt.MultipleGradientPaint; 30 import java.awt.MultipleGradientPaint.ColorSpaceType; 31 import java.awt.MultipleGradientPaint.CycleMethod; 32 import java.awt.TexturePaint; 33 import java.awt.image.BufferedImage; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.lang.annotation.Native; 37 import sun.java2d.SunGraphics2D; 38 import sun.java2d.SurfaceData; 39 import sun.java2d.loops.CompositeType; 40 import static sun.java2d.d3d.D3DContext.D3DContextCaps.*; 41 42 abstract class D3DPaints { 43 44 /** 45 * Holds all registered implementations, using the corresponding 46 * SunGraphics2D.PAINT_* constant as the hash key. 47 */ 48 private static Map<Integer, D3DPaints> impls = 49 new HashMap<Integer, D3DPaints>(4, 1.0f); 50 51 static { 52 impls.put(SunGraphics2D.PAINT_GRADIENT, new Gradient()); 53 impls.put(SunGraphics2D.PAINT_LIN_GRADIENT, new LinearGradient()); 54 impls.put(SunGraphics2D.PAINT_RAD_GRADIENT, new RadialGradient()); 55 impls.put(SunGraphics2D.PAINT_TEXTURE, new Texture()); 56 } 57 58 /** 59 * Attempts to locate an implementation corresponding to the paint state 60 * of the provided SunGraphics2D object. If no implementation can be 61 * found, or if the paint cannot be accelerated under the conditions 62 * of the SunGraphics2D, this method returns false; otherwise, returns 63 * true. 64 */ 65 static boolean isValid(SunGraphics2D sg2d) { 66 D3DPaints impl = impls.get(sg2d.paintState); 67 return (impl != null && impl.isPaintValid(sg2d)); 68 } 69 70 /** 71 * Returns true if this implementation is able to accelerate the 72 * Paint object associated with, and under the conditions of, the 73 * provided SunGraphics2D instance; otherwise returns false. 74 */ 75 abstract boolean isPaintValid(SunGraphics2D sg2d); 76 77 /************************* GradientPaint support ****************************/ 78 79 private static class Gradient extends D3DPaints { 80 private Gradient() {} 81 82 /** 83 * Returns true if the given GradientPaint instance can be 84 * used by the accelerated D3DPaints.Gradient implementation. 85 * A GradientPaint is considered valid only if the destination 86 * has support for fragment shaders. 87 */ 88 @Override 89 boolean isPaintValid(SunGraphics2D sg2d) { 90 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 91 D3DGraphicsDevice gd = (D3DGraphicsDevice) 92 dstData.getDeviceConfiguration().getDevice(); 93 return gd.isCapPresent(CAPS_LCD_SHADER); 94 } 95 } 96 97 /************************** TexturePaint support ****************************/ 98 99 private static class Texture extends D3DPaints { 100 private Texture() {} 101 102 /** 103 * Returns true if the given TexturePaint instance can be used by the 104 * accelerated BufferedPaints.Texture implementation. 105 * 106 * A TexturePaint is considered valid if the following conditions 107 * are met: 108 * - the texture image dimensions are power-of-two 109 * - the texture image can be (or is already) cached in a D3D 110 * texture object 111 */ 112 @Override 113 public boolean isPaintValid(SunGraphics2D sg2d) { 114 TexturePaint paint = (TexturePaint)sg2d.paint; 115 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 116 BufferedImage bi = paint.getImage(); 117 118 // verify that the texture image dimensions are pow2 119 D3DGraphicsDevice gd = 120 (D3DGraphicsDevice)dstData.getDeviceConfiguration().getDevice(); 121 int imgw = bi.getWidth(); 122 int imgh = bi.getHeight(); 123 if (!gd.isCapPresent(CAPS_TEXNONPOW2)) { 124 if ((imgw & (imgw - 1)) != 0 || (imgh & (imgh - 1)) != 0) { 125 return false; 126 } 127 } 128 // verify that the texture image is square if it has to be 129 if (!gd.isCapPresent(CAPS_TEXNONSQUARE) && imgw != imgh) 130 { 131 return false; 132 } 133 134 SurfaceData srcData = 135 dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, 136 CompositeType.SrcOver, null); 137 if (!(srcData instanceof D3DSurfaceData)) { 138 // REMIND: this is a hack that attempts to cache the system 139 // memory image from the TexturePaint instance into a 140 // D3D texture... 141 srcData = 142 dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT, 143 CompositeType.SrcOver, null); 144 if (!(srcData instanceof D3DSurfaceData)) { 145 return false; 146 } 147 } 148 149 // verify that the source surface is actually a texture 150 D3DSurfaceData d3dData = (D3DSurfaceData)srcData; 151 if (d3dData.getType() != D3DSurfaceData.TEXTURE) { 152 return false; 153 } 154 155 return true; 156 } 157 } 158 159 /****************** Shared MultipleGradientPaint support ********************/ 160 161 private static abstract class MultiGradient extends D3DPaints { 162 163 /** 164 * Note that this number is lower than the MULTI_MAX_FRACTIONS 165 * defined in the superclass. The D3D pipeline now uses a 166 * slightly more complicated shader (to avoid the gradient banding 167 * issues), which has a higher instruction count. To ensure that 168 * all versions of the shader can be compiled for PS 2.0 hardware, 169 * we need to cap this maximum value at 8. 170 */ 171 @Native public static final int MULTI_MAX_FRACTIONS_D3D = 8; 172 173 protected MultiGradient() {} 174 175 /** 176 * Returns true if the given MultipleGradientPaint instance can be 177 * used by the accelerated D3DPaints.MultiGradient implementation. 178 * A MultipleGradientPaint is considered valid if the following 179 * conditions are met: 180 * - the number of gradient "stops" is <= MAX_FRACTIONS 181 * - the destination has support for fragment shaders 182 */ 183 @Override 184 boolean isPaintValid(SunGraphics2D sg2d) { 185 MultipleGradientPaint paint = (MultipleGradientPaint)sg2d.paint; 186 // REMIND: ugh, this creates garbage; would be nicer if 187 // we had a MultipleGradientPaint.getNumStops() method... 188 if (paint.getFractions().length > MULTI_MAX_FRACTIONS_D3D) { 189 return false; 190 } 191 192 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 193 D3DGraphicsDevice gd = (D3DGraphicsDevice) 194 dstData.getDeviceConfiguration().getDevice(); 195 if (!gd.isCapPresent(CAPS_LCD_SHADER)) { 196 return false; 197 } 198 return true; 199 } 200 } 201 202 /********************** LinearGradientPaint support *************************/ 203 204 private static class LinearGradient extends MultiGradient { 205 private LinearGradient() {} 206 207 @Override 208 boolean isPaintValid(SunGraphics2D sg2d) { 209 LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint; 210 211 if (paint.getFractions().length == 2 && 212 paint.getCycleMethod() != CycleMethod.REPEAT && 213 paint.getColorSpace() != ColorSpaceType.LINEAR_RGB) 214 { 215 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 216 D3DGraphicsDevice gd = (D3DGraphicsDevice) 217 dstData.getDeviceConfiguration().getDevice(); 218 if (gd.isCapPresent(CAPS_LCD_SHADER)) { 219 // we can delegate to the optimized two-color gradient 220 // codepath, which should be faster 221 return true; 222 } 223 } 224 225 return super.isPaintValid(sg2d); 226 } 227 } 228 229 /********************** RadialGradientPaint support *************************/ 230 231 private static class RadialGradient extends MultiGradient { 232 private RadialGradient() {} 233 } 234 }