001: /*
002: * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.loops;
027:
028: import java.awt.Font;
029: import java.awt.Color;
030: import java.awt.Composite;
031: import java.awt.AlphaComposite;
032: import java.awt.Transparency;
033: import java.awt.image.ColorModel;
034: import java.awt.image.WritableRaster;
035: import java.awt.image.BufferedImage;
036: import sun.awt.image.BufImgSurfaceData;
037: import sun.java2d.loops.GraphicsPrimitive;
038: import sun.java2d.SurfaceData;
039: import sun.java2d.SunGraphics2D;
040: import sun.java2d.pipe.Region;
041:
042: /**
043: * BlitBg
044: * 1) copies rectangle of pixels from one surface to another
045: * 2) performs compositing of colors based upon a Composite
046: * parameter
047: * 3) assumes that non-opaque pixels are to be blended with
048: * the indicated Bg color before compositing with the
049: * destination
050: *
051: * precise behavior is undefined if the source surface
052: * and the destination surface are the same surface
053: * with overlapping regions of pixels
054: */
055: public class BlitBg extends GraphicsPrimitive {
056: public static final String methodSignature = "BlitBg(...)"
057: .toString();
058:
059: public static final int primTypeID = makePrimTypeID();
060:
061: private static RenderCache blitcache = new RenderCache(20);
062:
063: public static BlitBg locate(SurfaceType srctype,
064: CompositeType comptype, SurfaceType dsttype) {
065: return (BlitBg) GraphicsPrimitiveMgr.locate(primTypeID,
066: srctype, comptype, dsttype);
067: }
068:
069: public static BlitBg getFromCache(SurfaceType src,
070: CompositeType comp, SurfaceType dst) {
071: Object o = blitcache.get(src, comp, dst);
072: if (o != null) {
073: return (BlitBg) o;
074: }
075: BlitBg blit = locate(src, comp, dst);
076: if (blit == null) {
077: System.out.println("blitbg loop not found for:");
078: System.out.println("src: " + src);
079: System.out.println("comp: " + comp);
080: System.out.println("dst: " + dst);
081: } else {
082: blitcache.put(src, comp, dst, blit);
083: }
084: return blit;
085: }
086:
087: protected BlitBg(SurfaceType srctype, CompositeType comptype,
088: SurfaceType dsttype) {
089: super (methodSignature, primTypeID, srctype, comptype, dsttype);
090: }
091:
092: public BlitBg(long pNativePrim, SurfaceType srctype,
093: CompositeType comptype, SurfaceType dsttype) {
094: super (pNativePrim, methodSignature, primTypeID, srctype,
095: comptype, dsttype);
096: }
097:
098: /**
099: * All BlitBg implementors must have this invoker method
100: */
101: public native void BlitBg(SurfaceData src, SurfaceData dst,
102: Composite comp, Region clip, Color bgColor, int srcx,
103: int srcy, int dstx, int dsty, int width, int height);
104:
105: static {
106: GraphicsPrimitiveMgr.registerGeneral(new BlitBg(null, null,
107: null));
108: }
109:
110: public GraphicsPrimitive makePrimitive(SurfaceType srctype,
111: CompositeType comptype, SurfaceType dsttype) {
112: /*
113: System.out.println("Constructing general blitbg for:");
114: System.out.println("src: "+srctype);
115: System.out.println("comp: "+comptype);
116: System.out.println("dst: "+dsttype);
117: */
118: return new General(srctype, comptype, dsttype);
119: }
120:
121: private static class General extends BlitBg {
122: CompositeType compositeType;
123:
124: public General(SurfaceType srctype, CompositeType comptype,
125: SurfaceType dsttype) {
126: super (srctype, comptype, dsttype);
127: compositeType = comptype;
128: }
129:
130: public void BlitBg(SurfaceData srcData, SurfaceData dstData,
131: Composite comp, Region clip, Color bgColor, int srcx,
132: int srcy, int dstx, int dsty, int width, int height) {
133: ColorModel dstModel = dstData.getColorModel();
134: if (!dstModel.hasAlpha()
135: && bgColor.getTransparency() != Transparency.OPAQUE) {
136: dstModel = ColorModel.getRGBdefault();
137: }
138: WritableRaster wr = dstModel
139: .createCompatibleWritableRaster(width, height);
140: boolean isPremult = dstModel.isAlphaPremultiplied();
141: BufferedImage bimg = new BufferedImage(dstModel, wr,
142: isPremult, null);
143: SurfaceData tmpData = BufImgSurfaceData.createData(bimg);
144: SunGraphics2D sg2d = new SunGraphics2D(tmpData, bgColor,
145: bgColor, defaultFont);
146: FillRect fillop = FillRect.locate(SurfaceType.AnyColor,
147: CompositeType.SrcNoEa, tmpData.getSurfaceType());
148: Blit combineop = Blit
149: .getFromCache(srcData.getSurfaceType(),
150: CompositeType.SrcOverNoEa, tmpData
151: .getSurfaceType());
152: Blit blitop = Blit.getFromCache(tmpData.getSurfaceType(),
153: compositeType, dstData.getSurfaceType());
154: fillop.FillRect(sg2d, tmpData, 0, 0, width, height);
155: combineop.Blit(srcData, tmpData, AlphaComposite.SrcOver,
156: null, srcx, srcy, 0, 0, width, height);
157: blitop.Blit(tmpData, dstData, comp, clip, 0, 0, dstx, dsty,
158: width, height);
159: }
160:
161: private static Font defaultFont = new Font("Dialog",
162: Font.PLAIN, 12);
163: }
164:
165: public GraphicsPrimitive traceWrap() {
166: return new TraceBlitBg(this );
167: }
168:
169: private static class TraceBlitBg extends BlitBg {
170: BlitBg target;
171:
172: public TraceBlitBg(BlitBg target) {
173: super (target.getSourceType(), target.getCompositeType(),
174: target.getDestType());
175: this .target = target;
176: }
177:
178: public GraphicsPrimitive traceWrap() {
179: return this ;
180: }
181:
182: public void BlitBg(SurfaceData src, SurfaceData dst,
183: Composite comp, Region clip, Color bgColor, int srcx,
184: int srcy, int dstx, int dsty, int width, int height) {
185: tracePrimitive(target);
186: target.BlitBg(src, dst, comp, clip, bgColor, srcx, srcy,
187: dstx, dsty, width, height);
188: }
189: }
190: }
|