887
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
|
887
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
887
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
887
|
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 |
*
|
5506
|
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.
|
887
|
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 sun.java2d.SunGraphics2D;
|
|
37 |
import sun.java2d.SurfaceData;
|
|
38 |
import sun.java2d.loops.CompositeType;
|
|
39 |
import static sun.java2d.d3d.D3DContext.D3DContextCaps.*;
|
|
40 |
|
|
41 |
abstract class D3DPaints {
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Holds all registered implementations, using the corresponding
|
|
45 |
* SunGraphics2D.PAINT_* constant as the hash key.
|
|
46 |
*/
|
|
47 |
private static Map<Integer, D3DPaints> impls =
|
|
48 |
new HashMap<Integer, D3DPaints>(4, 1.0f);
|
|
49 |
|
|
50 |
static {
|
|
51 |
impls.put(SunGraphics2D.PAINT_GRADIENT, new Gradient());
|
|
52 |
impls.put(SunGraphics2D.PAINT_LIN_GRADIENT, new LinearGradient());
|
|
53 |
impls.put(SunGraphics2D.PAINT_RAD_GRADIENT, new RadialGradient());
|
|
54 |
impls.put(SunGraphics2D.PAINT_TEXTURE, new Texture());
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Attempts to locate an implementation corresponding to the paint state
|
|
59 |
* of the provided SunGraphics2D object. If no implementation can be
|
|
60 |
* found, or if the paint cannot be accelerated under the conditions
|
|
61 |
* of the SunGraphics2D, this method returns false; otherwise, returns
|
|
62 |
* true.
|
|
63 |
*/
|
|
64 |
static boolean isValid(SunGraphics2D sg2d) {
|
|
65 |
D3DPaints impl = impls.get(sg2d.paintState);
|
|
66 |
return (impl != null && impl.isPaintValid(sg2d));
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Returns true if this implementation is able to accelerate the
|
|
71 |
* Paint object associated with, and under the conditions of, the
|
|
72 |
* provided SunGraphics2D instance; otherwise returns false.
|
|
73 |
*/
|
|
74 |
abstract boolean isPaintValid(SunGraphics2D sg2d);
|
|
75 |
|
|
76 |
/************************* GradientPaint support ****************************/
|
|
77 |
|
|
78 |
private static class Gradient extends D3DPaints {
|
|
79 |
private Gradient() {}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Returns true if the given GradientPaint instance can be
|
|
83 |
* used by the accelerated D3DPaints.Gradient implementation.
|
|
84 |
* A GradientPaint is considered valid only if the destination
|
|
85 |
* has support for fragment shaders.
|
|
86 |
*/
|
|
87 |
@Override
|
|
88 |
boolean isPaintValid(SunGraphics2D sg2d) {
|
|
89 |
D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData;
|
|
90 |
D3DGraphicsDevice gd = (D3DGraphicsDevice)
|
|
91 |
dstData.getDeviceConfiguration().getDevice();
|
|
92 |
return gd.isCapPresent(CAPS_LCD_SHADER);
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
/************************** TexturePaint support ****************************/
|
|
97 |
|
|
98 |
private static class Texture extends D3DPaints {
|
|
99 |
private Texture() {}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Returns true if the given TexturePaint instance can be used by the
|
|
103 |
* accelerated BufferedPaints.Texture implementation.
|
|
104 |
*
|
|
105 |
* A TexturePaint is considered valid if the following conditions
|
|
106 |
* are met:
|
|
107 |
* - the texture image dimensions are power-of-two
|
|
108 |
* - the texture image can be (or is already) cached in a D3D
|
|
109 |
* texture object
|
|
110 |
*/
|
|
111 |
@Override
|
|
112 |
public boolean isPaintValid(SunGraphics2D sg2d) {
|
|
113 |
TexturePaint paint = (TexturePaint)sg2d.paint;
|
|
114 |
D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData;
|
|
115 |
BufferedImage bi = paint.getImage();
|
|
116 |
|
|
117 |
// verify that the texture image dimensions are pow2
|
|
118 |
D3DGraphicsDevice gd =
|
|
119 |
(D3DGraphicsDevice)dstData.getDeviceConfiguration().getDevice();
|
|
120 |
int imgw = bi.getWidth();
|
|
121 |
int imgh = bi.getHeight();
|
|
122 |
if (!gd.isCapPresent(CAPS_TEXNONPOW2)) {
|
|
123 |
if ((imgw & (imgw - 1)) != 0 || (imgh & (imgh - 1)) != 0) {
|
|
124 |
return false;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
// verify that the texture image is square if it has to be
|
|
128 |
if (!gd.isCapPresent(CAPS_TEXNONSQUARE) && imgw != imgh)
|
|
129 |
{
|
|
130 |
return false;
|
|
131 |
}
|
|
132 |
|
|
133 |
SurfaceData srcData =
|
|
134 |
dstData.getSourceSurfaceData(bi, sg2d.TRANSFORM_ISIDENT,
|
|
135 |
CompositeType.SrcOver, null);
|
|
136 |
if (!(srcData instanceof D3DSurfaceData)) {
|
|
137 |
// REMIND: this is a hack that attempts to cache the system
|
|
138 |
// memory image from the TexturePaint instance into a
|
|
139 |
// D3D texture...
|
|
140 |
srcData =
|
|
141 |
dstData.getSourceSurfaceData(bi, sg2d.TRANSFORM_ISIDENT,
|
|
142 |
CompositeType.SrcOver, null);
|
|
143 |
if (!(srcData instanceof D3DSurfaceData)) {
|
|
144 |
return false;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
// verify that the source surface is actually a texture
|
|
149 |
D3DSurfaceData d3dData = (D3DSurfaceData)srcData;
|
|
150 |
if (d3dData.getType() != D3DSurfaceData.TEXTURE) {
|
|
151 |
return false;
|
|
152 |
}
|
|
153 |
|
|
154 |
return true;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
/****************** Shared MultipleGradientPaint support ********************/
|
|
159 |
|
|
160 |
private static abstract class MultiGradient extends D3DPaints {
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Note that this number is lower than the MULTI_MAX_FRACTIONS
|
|
164 |
* defined in the superclass. The D3D pipeline now uses a
|
|
165 |
* slightly more complicated shader (to avoid the gradient banding
|
|
166 |
* issues), which has a higher instruction count. To ensure that
|
|
167 |
* all versions of the shader can be compiled for PS 2.0 hardware,
|
|
168 |
* we need to cap this maximum value at 8.
|
|
169 |
*/
|
|
170 |
public static final int MULTI_MAX_FRACTIONS_D3D = 8;
|
|
171 |
|
|
172 |
protected MultiGradient() {}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Returns true if the given MultipleGradientPaint instance can be
|
|
176 |
* used by the accelerated D3DPaints.MultiGradient implementation.
|
|
177 |
* A MultipleGradientPaint is considered valid if the following
|
|
178 |
* conditions are met:
|
|
179 |
* - the number of gradient "stops" is <= MAX_FRACTIONS
|
|
180 |
* - the destination has support for fragment shaders
|
|
181 |
*/
|
|
182 |
@Override
|
|
183 |
boolean isPaintValid(SunGraphics2D sg2d) {
|
|
184 |
MultipleGradientPaint paint = (MultipleGradientPaint)sg2d.paint;
|
|
185 |
// REMIND: ugh, this creates garbage; would be nicer if
|
|
186 |
// we had a MultipleGradientPaint.getNumStops() method...
|
|
187 |
if (paint.getFractions().length > MULTI_MAX_FRACTIONS_D3D) {
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
|
|
191 |
D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData;
|
|
192 |
D3DGraphicsDevice gd = (D3DGraphicsDevice)
|
|
193 |
dstData.getDeviceConfiguration().getDevice();
|
|
194 |
if (!gd.isCapPresent(CAPS_LCD_SHADER)) {
|
|
195 |
return false;
|
|
196 |
}
|
|
197 |
return true;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
/********************** LinearGradientPaint support *************************/
|
|
202 |
|
|
203 |
private static class LinearGradient extends MultiGradient {
|
|
204 |
private LinearGradient() {}
|
|
205 |
|
|
206 |
@Override
|
|
207 |
boolean isPaintValid(SunGraphics2D sg2d) {
|
|
208 |
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
|
|
209 |
|
|
210 |
if (paint.getFractions().length == 2 &&
|
|
211 |
paint.getCycleMethod() != CycleMethod.REPEAT &&
|
|
212 |
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
|
|
213 |
{
|
|
214 |
D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData;
|
|
215 |
D3DGraphicsDevice gd = (D3DGraphicsDevice)
|
|
216 |
dstData.getDeviceConfiguration().getDevice();
|
|
217 |
if (gd.isCapPresent(CAPS_LCD_SHADER)) {
|
|
218 |
// we can delegate to the optimized two-color gradient
|
|
219 |
// codepath, which should be faster
|
|
220 |
return true;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
|
224 |
return super.isPaintValid(sg2d);
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
/********************** RadialGradientPaint support *************************/
|
|
229 |
|
|
230 |
private static class RadialGradient extends MultiGradient {
|
|
231 |
private RadialGradient() {}
|
|
232 |
}
|
|
233 |
}
|