1 /* 2 * Copyright (c) 2007, 2008, 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 javax.tools.annotation.GenerateNativeHeader; 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, sg2d.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, sg2d.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 /* No native methods here, but the constants are needed in the supporting JNI code */ 162 @GenerateNativeHeader 163 private static abstract class MultiGradient extends D3DPaints { 164 165 /** 166 * Note that this number is lower than the MULTI_MAX_FRACTIONS 167 * defined in the superclass. The D3D pipeline now uses a 168 * slightly more complicated shader (to avoid the gradient banding 169 * issues), which has a higher instruction count. To ensure that 170 * all versions of the shader can be compiled for PS 2.0 hardware, 171 * we need to cap this maximum value at 8. 172 */ 173 public static final int MULTI_MAX_FRACTIONS_D3D = 8; 174 175 protected MultiGradient() {} 176 177 /** 178 * Returns true if the given MultipleGradientPaint instance can be 179 * used by the accelerated D3DPaints.MultiGradient implementation. 180 * A MultipleGradientPaint is considered valid if the following 181 * conditions are met: 182 * - the number of gradient "stops" is <= MAX_FRACTIONS 183 * - the destination has support for fragment shaders 184 */ 185 @Override 186 boolean isPaintValid(SunGraphics2D sg2d) { 187 MultipleGradientPaint paint = (MultipleGradientPaint)sg2d.paint; 188 // REMIND: ugh, this creates garbage; would be nicer if 189 // we had a MultipleGradientPaint.getNumStops() method... 190 if (paint.getFractions().length > MULTI_MAX_FRACTIONS_D3D) { 191 return false; 192 } 193 194 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 195 D3DGraphicsDevice gd = (D3DGraphicsDevice) 196 dstData.getDeviceConfiguration().getDevice(); 197 if (!gd.isCapPresent(CAPS_LCD_SHADER)) { 198 return false; 199 } 200 return true; 201 } 202 } 203 204 /********************** LinearGradientPaint support *************************/ 205 206 private static class LinearGradient extends MultiGradient { 207 private LinearGradient() {} 208 209 @Override 210 boolean isPaintValid(SunGraphics2D sg2d) { 211 LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint; 212 213 if (paint.getFractions().length == 2 && 214 paint.getCycleMethod() != CycleMethod.REPEAT && 215 paint.getColorSpace() != ColorSpaceType.LINEAR_RGB) 216 { 217 D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData; 218 D3DGraphicsDevice gd = (D3DGraphicsDevice) 219 dstData.getDeviceConfiguration().getDevice(); 220 if (gd.isCapPresent(CAPS_LCD_SHADER)) { 221 // we can delegate to the optimized two-color gradient 222 // codepath, which should be faster 223 return true; 224 } 225 } 226 227 return super.isPaintValid(sg2d); 228 } 229 } 230 231 /********************** RadialGradientPaint support *************************/ 232 233 private static class RadialGradient extends MultiGradient { 234 private RadialGradient() {} 235 } 236 }