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.xr;
27
28 import java.awt.*;
29 import java.awt.geom.*;
30
31 import java.security.AccessController;
32 import java.security.PrivilegedAction;
33
34 import sun.font.*;
35 import sun.java2d.*;
36 import sun.java2d.loops.*;
37
38 /**
39 * Manages per-application resources, e.g. the 1x1 pixmap used for solid color
40 * fill as well as per-application state e.g. the currently set source picture
41 * used for composition .
42 *
43 * @author Clemens Eisserer
44 */
45
46 public class XRCompositeManager {
47 private static boolean enableGradCache = true;
48 private static XRCompositeManager instance;
49
50 private static final int SOLID = 0;
51 private static final int TEXTURE = 1;
52 private static final int GRADIENT = 2;
53
54 int srcType;
55 XRSolidSrcPict solidSrc32;
56 XRSurfaceData texture;
57 XRSurfaceData gradient;
58 int alphaMask = XRUtils.None;
59
60 XRColor solidColor = new XRColor();
61 float extraAlpha = 1.0f;
62 byte compRule = XRUtils.PictOpOver;
63 XRColor alphaColor = new XRColor();
64
65 XRSurfaceData solidSrcPict;
66 int alphaMaskPict;
67 int gradCachePixmap;
68 int gradCachePicture;
69
70 boolean xorEnabled = false;
71 int validatedPixel = 0;
72 Composite validatedComp;
73 Paint validatedPaint;
74 float validatedExtraAlpha = 1.0f;
75
76 XRBackend con;
77 MaskTileManager maskBuffer;
78 XRTextRenderer textRenderer;
79 XRMaskImage maskImage;
80
81 public static synchronized XRCompositeManager getInstance(
82 XRSurfaceData surface) {
83 if (instance == null) {
84 instance = new XRCompositeManager(surface);
85 }
86 return instance;
87 }
88
89 private XRCompositeManager(XRSurfaceData surface) {
90 con = new XRBackendNative();
91
92 String gradProp =
93 AccessController.doPrivileged(new PrivilegedAction<String>() {
94 public String run() {
95 return System.getProperty("sun.java2d.xrgradcache");
96 }
97 });
98
99 enableGradCache = gradProp == null ||
100 !(gradProp.equalsIgnoreCase("false") ||
101 gradProp.equalsIgnoreCase("f"));
102
103 XRPaints.register(this);
104
105 initResources(surface);
106
107 maskBuffer = new MaskTileManager(this, surface.getXid());
108 textRenderer = new XRTextRenderer(this);
109 maskImage = new XRMaskImage(this, surface.getXid());
110 }
111
112 public void initResources(XRSurfaceData surface) {
113 int parentXid = surface.getXid();
114
115 solidSrc32 = new XRSolidSrcPict(con, parentXid);
116 setForeground(0);
117
118 int extraAlphaMask = con.createPixmap(parentXid, 8, 1, 1);
119 alphaMaskPict = con.createPicture(extraAlphaMask,
120 XRUtils.PictStandardA8);
121 con.setPictureRepeat(alphaMaskPict, XRUtils.RepeatNormal);
122 con.renderRectangle(alphaMaskPict, XRUtils.PictOpClear,
123 XRColor.NO_ALPHA, 0, 0, 1, 1);
124
125 if (enableGradCache) {
126 gradCachePixmap = con.createPixmap(parentXid, 32,
127 MaskTileManager.MASK_SIZE, MaskTileManager.MASK_SIZE);
128 gradCachePicture = con.createPicture(gradCachePixmap,
129 XRUtils.PictStandardARGB32);
130 }
131 }
132
133 public void setForeground(int pixel) {
134 solidColor.setColorValues(pixel);
135 }
136
137 public void setGradientPaint(XRSurfaceData gradient) {
138 if (this.gradient != null) {
139 con.freePicture(this.gradient.picture);
140 }
141 this.gradient = gradient;
142 srcType = GRADIENT;
143 }
144
145 public void setTexturePaint(XRSurfaceData texture) {
146 this.texture = texture;
147 this.srcType = TEXTURE;
148 }
149
150 public void XRResetPaint() {
218 xorEnabled = false;
219 } else if (comp instanceof XORComposite) {
220 /* XOR composite validation is handled in XRSurfaceData */
221 xorEnabled = true;
222 } else {
223 throw new InternalError(
224 "Composite accaleration not implemented for: "
225 + comp.getClass().getName());
226 }
227 }
228
229 public boolean maskRequired() {
230 return (!xorEnabled)
231 && ((srcType != SOLID)
232 || (srcType == SOLID && (solidColor.alpha != 0xffff) || (extraAlpha != 1.0f)));
233 }
234
235 public void XRComposite(int src, int mask, int dst, int srcX, int srcY,
236 int maskX, int maskY, int dstX, int dstY, int width, int height) {
237 int cachedSrc = (src == XRUtils.None) ? getCurrentSource().picture : src;
238 int cachedX = srcX;
239 int cachedY = srcY;
240
241 if (enableGradCache && gradient != null
242 && cachedSrc == gradient.picture) {
243 con.renderComposite(XRUtils.PictOpSrc, gradient.picture,
244 XRUtils.None, gradCachePicture, srcX, srcY, 0, 0, 0, 0,
245 width, height);
246 cachedX = 0;
247 cachedY = 0;
248 cachedSrc = gradCachePicture;
249 }
250
251 con.renderComposite(compRule, cachedSrc, mask, dst, cachedX, cachedY,
252 maskX, maskY, dstX, dstY, width, height);
253 }
254
255 public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {
256 if (xorEnabled) {
257 con.GCRectangles(dst.getXid(), dst.getGC(), rects);
258 } else {
259 if (rects.getSize() == 1) {
260 con.renderRectangle(dst.getPicture(), compRule, solidColor,
261 rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));
262 } else {
263 con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);
264 }
265 }
266 }
267
268 public void XRCompositeRectangles(XRSurfaceData dst, GrowableRectArray rects) {
269 int srcPict = getCurrentSource().picture;
270
271 for(int i=0; i < rects.getSize(); i++) {
272 int x = rects.getX(i);
327 public XRColor getAlphaColor() {
328 return alphaColor;
329 }
330
331 public XRBackend getBackend() {
332 return con;
333 }
334
335 public float getExtraAlpha() {
336 return validatedExtraAlpha;
337 }
338
339 public byte getCompRule() {
340 return compRule;
341 }
342
343 public XRTextRenderer getTextRenderer() {
344 return textRenderer;
345 }
346
347 public MaskTileManager getMaskBuffer() {
348 return maskBuffer;
349 }
350
351 public XRMaskImage getMaskImage() {
352 return maskImage;
353 }
354 }
|
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.xr;
27
28 import java.awt.*;
29 import java.awt.geom.*;
30
31 import sun.font.*;
32 import sun.java2d.*;
33 import sun.java2d.loops.*;
34
35 /**
36 * Manages per-application resources, e.g. the 1x1 pixmap used for solid color
37 * fill as well as per-application state e.g. the currently set source picture
38 * used for composition .
39 *
40 * @author Clemens Eisserer
41 */
42
43 public class XRCompositeManager {
44 private static XRCompositeManager instance;
45
46 private static final int SOLID = 0;
47 private static final int TEXTURE = 1;
48 private static final int GRADIENT = 2;
49
50 int srcType;
51 XRSolidSrcPict solidSrc32;
52 XRSurfaceData texture;
53 XRSurfaceData gradient;
54 int alphaMask = XRUtils.None;
55
56 XRColor solidColor = new XRColor();
57 float extraAlpha = 1.0f;
58 byte compRule = XRUtils.PictOpOver;
59 XRColor alphaColor = new XRColor();
60
61 XRSurfaceData solidSrcPict;
62 int alphaMaskPict;
63
64 boolean xorEnabled = false;
65 int validatedPixel = 0;
66 Composite validatedComp;
67 Paint validatedPaint;
68 float validatedExtraAlpha = 1.0f;
69
70 XRBackend con;
71 RectTileManager maskBuffer;
72 XRTextRenderer textRenderer;
73 XRMaskImage maskImage;
74
75 public static synchronized XRCompositeManager getInstance(
76 XRSurfaceData surface) {
77 if (instance == null) {
78 instance = new XRCompositeManager(surface);
79 }
80 return instance;
81 }
82
83 private XRCompositeManager(XRSurfaceData surface) {
84 String defProp = System.getProperty("sun.java2d.xr.deferred");
85
86 if(defProp != null && defProp.length() > 0
87 && Character.toLowerCase(defProp.charAt(0)) == 't') {
88 con = new XRBackendDeferred();
89 } else {
90 con = new XRBackendNative();
91 }
92
93 con.initResources(surface.getXid());
94
95 XRPaints.register(this);
96
97 initResources(surface);
98
99 maskBuffer = new RectTileManager(this, surface.getXid());
100 textRenderer = new XRTextRenderer(this);
101 maskImage = new XRMaskImage(this, surface.getXid());
102 }
103
104 public void initResources(XRSurfaceData surface) {
105 int parentXid = surface.getXid();
106
107 solidSrc32 = new XRSolidSrcPict(con, parentXid);
108 setForeground(0);
109
110 int extraAlphaMask = con.createPixmap(parentXid, 8, 1, 1);
111 alphaMaskPict = con.createPicture(extraAlphaMask,
112 XRUtils.PictStandardA8);
113 con.setPictureRepeat(alphaMaskPict, XRUtils.RepeatNormal);
114 con.renderRectangle(alphaMaskPict, XRUtils.PictOpClear,
115 XRColor.NO_ALPHA, 0, 0, 1, 1);
116 }
117
118 public void setForeground(int pixel) {
119 solidColor.setColorValues(pixel);
120 }
121
122 public void setGradientPaint(XRSurfaceData gradient) {
123 if (this.gradient != null) {
124 con.freePicture(this.gradient.picture);
125 }
126 this.gradient = gradient;
127 srcType = GRADIENT;
128 }
129
130 public void setTexturePaint(XRSurfaceData texture) {
131 this.texture = texture;
132 this.srcType = TEXTURE;
133 }
134
135 public void XRResetPaint() {
203 xorEnabled = false;
204 } else if (comp instanceof XORComposite) {
205 /* XOR composite validation is handled in XRSurfaceData */
206 xorEnabled = true;
207 } else {
208 throw new InternalError(
209 "Composite accaleration not implemented for: "
210 + comp.getClass().getName());
211 }
212 }
213
214 public boolean maskRequired() {
215 return (!xorEnabled)
216 && ((srcType != SOLID)
217 || (srcType == SOLID && (solidColor.alpha != 0xffff) || (extraAlpha != 1.0f)));
218 }
219
220 public void XRComposite(int src, int mask, int dst, int srcX, int srcY,
221 int maskX, int maskY, int dstX, int dstY, int width, int height) {
222 int cachedSrc = (src == XRUtils.None) ? getCurrentSource().picture : src;
223
224 con.renderComposite(compRule, cachedSrc, mask, dst, srcX, srcY,
225 maskX, maskY, dstX, dstY, width, height);
226 }
227
228 public void XRMaskedComposite(int src, int dst,
229 int srcX, int srcY, int dstX, int dstY, int width,
230 int height, int maskScan, int maskOff, byte[] mask) {
231
232 src = (src == XRUtils.None) ? getCurrentSource().picture : src;
233
234 float maskAlpha = 1.0f;
235 int maskXid = XRUtils.None;
236
237 if (mask != null) {
238 maskAlpha = isTexturePaintActive() ? getExtraAlpha() : 1.0f;
239 } else if (isTexturePaintActive()) {
240 maskXid = getExtraAlphaMask();
241 }
242
243 con.maskedComposite(compRule, src, maskXid, dst,
244 srcX, srcY, dstX, dstY, width,
245 height, maskScan, maskOff, mask, maskAlpha);
246 }
247
248 public void XRRenderRectangles(XRSurfaceData dst, GrowableRectArray rects) {
249 if (xorEnabled) {
250 con.GCRectangles(dst.getXid(), dst.getGC(), rects);
251 } else {
252 if (rects.getSize() == 1) {
253 con.renderRectangle(dst.getPicture(), compRule, solidColor,
254 rects.getX(0), rects.getY(0), rects.getWidth(0), rects.getHeight(0));
255 } else {
256 con.renderRectangles(dst.getPicture(), compRule, solidColor, rects);
257 }
258 }
259 }
260
261 public void XRCompositeRectangles(XRSurfaceData dst, GrowableRectArray rects) {
262 int srcPict = getCurrentSource().picture;
263
264 for(int i=0; i < rects.getSize(); i++) {
265 int x = rects.getX(i);
320 public XRColor getAlphaColor() {
321 return alphaColor;
322 }
323
324 public XRBackend getBackend() {
325 return con;
326 }
327
328 public float getExtraAlpha() {
329 return validatedExtraAlpha;
330 }
331
332 public byte getCompRule() {
333 return compRule;
334 }
335
336 public XRTextRenderer getTextRenderer() {
337 return textRenderer;
338 }
339
340 public RectTileManager getMaskBuffer() {
341 return maskBuffer;
342 }
343
344 public XRMaskImage getMaskImage() {
345 return maskImage;
346 }
347 }
|