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.Composite;
029: import java.awt.CompositeContext;
030: import java.awt.RenderingHints;
031: import java.awt.image.ColorModel;
032: import java.awt.image.Raster;
033: import java.awt.image.WritableRaster;
034: import java.lang.ref.WeakReference;
035: import sun.java2d.loops.GraphicsPrimitive;
036: import sun.java2d.SurfaceData;
037: import sun.java2d.pipe.Region;
038: import sun.java2d.pipe.SpanIterator;
039:
040: /**
041: * Blit
042: * 1) copies rectangle of pixels from one surface to another
043: * 2) performs compositing of colors based upon a Composite
044: * parameter
045: *
046: * precise behavior is undefined if the source surface
047: * and the destination surface are the same surface
048: * with overlapping regions of pixels
049: */
050:
051: public class Blit extends GraphicsPrimitive {
052: public static final String methodSignature = "Blit(...)".toString();
053:
054: public static final int primTypeID = makePrimTypeID();
055:
056: private static RenderCache blitcache = new RenderCache(20);
057:
058: public static Blit locate(SurfaceType srctype,
059: CompositeType comptype, SurfaceType dsttype) {
060: return (Blit) GraphicsPrimitiveMgr.locate(primTypeID, srctype,
061: comptype, dsttype);
062: }
063:
064: public static Blit getFromCache(SurfaceType src,
065: CompositeType comp, SurfaceType dst) {
066: Object o = blitcache.get(src, comp, dst);
067: if (o != null) {
068: return (Blit) o;
069: }
070:
071: Blit blit = locate(src, comp, dst);
072: if (blit == null) {
073: System.out.println("blit loop not found for:");
074: System.out.println("src: " + src);
075: System.out.println("comp: " + comp);
076: System.out.println("dst: " + dst);
077: } else {
078: blitcache.put(src, comp, dst, blit);
079: }
080: return blit;
081: }
082:
083: protected Blit(SurfaceType srctype, CompositeType comptype,
084: SurfaceType dsttype) {
085: super (methodSignature, primTypeID, srctype, comptype, dsttype);
086: }
087:
088: public Blit(long pNativePrim, SurfaceType srctype,
089: CompositeType comptype, SurfaceType dsttype) {
090: super (pNativePrim, methodSignature, primTypeID, srctype,
091: comptype, dsttype);
092: }
093:
094: /**
095: * All Blit implementors must have this invoker method
096: */
097: public native void Blit(SurfaceData src, SurfaceData dst,
098: Composite comp, Region clip, int srcx, int srcy, int dstx,
099: int dsty, int width, int height);
100:
101: static {
102: GraphicsPrimitiveMgr
103: .registerGeneral(new Blit(null, null, null));
104: }
105:
106: public GraphicsPrimitive makePrimitive(SurfaceType srctype,
107: CompositeType comptype, SurfaceType dsttype) {
108: /*
109: System.out.println("Constructing general blit for:");
110: System.out.println("src: "+srctype);
111: System.out.println("comp: "+comptype);
112: System.out.println("dst: "+dsttype);
113: */
114:
115: if (comptype.isDerivedFrom(CompositeType.Xor)) {
116: GeneralXorBlit gxb = new GeneralXorBlit(srctype, comptype,
117: dsttype);
118: setupGeneralBinaryOp(gxb);
119: return gxb;
120: } else if (comptype.isDerivedFrom(CompositeType.AnyAlpha)) {
121: return new GeneralMaskBlit(srctype, comptype, dsttype);
122: } else {
123: return AnyBlit.instance;
124: }
125: }
126:
127: private static class AnyBlit extends Blit {
128: public static AnyBlit instance = new AnyBlit();
129:
130: public AnyBlit() {
131: super (SurfaceType.Any, CompositeType.Any, SurfaceType.Any);
132: }
133:
134: public void Blit(SurfaceData srcData, SurfaceData dstData,
135: Composite comp, Region clip, int srcx, int srcy,
136: int dstx, int dsty, int width, int height) {
137: ColorModel srcCM = srcData.getColorModel();
138: ColorModel dstCM = dstData.getColorModel();
139: // REMIND: Should get RenderingHints from sg2d
140: CompositeContext ctx = comp.createContext(srcCM, dstCM,
141: new RenderingHints(null));
142: Raster srcRas = srcData
143: .getRaster(srcx, srcy, width, height);
144: WritableRaster dstRas = (WritableRaster) dstData.getRaster(
145: dstx, dsty, width, height);
146:
147: if (clip == null) {
148: clip = Region
149: .getInstanceXYWH(dstx, dsty, width, height);
150: }
151: int span[] = { dstx, dsty, dstx + width, dsty + height };
152: SpanIterator si = clip.getSpanIterator(span);
153: srcx -= dstx;
154: srcy -= dsty;
155: while (si.nextSpan(span)) {
156: int w = span[2] - span[0];
157: int h = span[3] - span[1];
158: srcRas = srcRas.createChild(srcx + span[0], srcy
159: + span[1], w, h, 0, 0, null);
160: dstRas = dstRas.createWritableChild(span[0], span[1],
161: w, h, 0, 0, null);
162: ctx.compose(srcRas, dstRas, dstRas);
163: }
164: ctx.dispose();
165: }
166: }
167:
168: private static class GeneralMaskBlit extends Blit {
169: MaskBlit performop;
170:
171: public GeneralMaskBlit(SurfaceType srctype,
172: CompositeType comptype, SurfaceType dsttype) {
173: super (srctype, comptype, dsttype);
174: performop = MaskBlit.locate(srctype, comptype, dsttype);
175: }
176:
177: public void Blit(SurfaceData srcData, SurfaceData dstData,
178: Composite comp, Region clip, int srcx, int srcy,
179: int dstx, int dsty, int width, int height) {
180: performop.MaskBlit(srcData, dstData, comp, clip, srcx,
181: srcy, dstx, dsty, width, height, null, 0, 0);
182: }
183: }
184:
185: private static class GeneralXorBlit extends Blit implements
186: GeneralBinaryOp {
187: Blit convertsrc;
188: Blit convertdst;
189: Blit performop;
190: Blit convertresult;
191:
192: WeakReference srcTmp;
193: WeakReference dstTmp;
194:
195: public GeneralXorBlit(SurfaceType srctype,
196: CompositeType comptype, SurfaceType dsttype) {
197: super (srctype, comptype, dsttype);
198: }
199:
200: public void setPrimitives(Blit srcconverter, Blit dstconverter,
201: GraphicsPrimitive genericop, Blit resconverter) {
202: this .convertsrc = srcconverter;
203: this .convertdst = dstconverter;
204: this .performop = (Blit) genericop;
205: this .convertresult = resconverter;
206: }
207:
208: public synchronized void Blit(SurfaceData srcData,
209: SurfaceData dstData, Composite comp, Region clip,
210: int srcx, int srcy, int dstx, int dsty, int width,
211: int height) {
212: SurfaceData src, dst;
213: Region opclip;
214: int sx, sy, dx, dy;
215:
216: if (convertsrc == null) {
217: src = srcData;
218: sx = srcx;
219: sy = srcy;
220: } else {
221: SurfaceData cachedSrc = null;
222: if (srcTmp != null) {
223: cachedSrc = (SurfaceData) srcTmp.get();
224: }
225: src = convertFrom(convertsrc, srcData, srcx, srcy,
226: width, height, cachedSrc);
227: sx = 0;
228: sy = 0;
229: if (src != cachedSrc) {
230: srcTmp = new WeakReference(src);
231: }
232: }
233:
234: if (convertdst == null) {
235: dst = dstData;
236: dx = dstx;
237: dy = dsty;
238: opclip = clip;
239: } else {
240: // assert: convertresult != null
241: SurfaceData cachedDst = null;
242: if (dstTmp != null) {
243: cachedDst = (SurfaceData) dstTmp.get();
244: }
245: dst = convertFrom(convertdst, dstData, dstx, dsty,
246: width, height, cachedDst);
247: dx = 0;
248: dy = 0;
249: opclip = null;
250: if (dst != cachedDst) {
251: dstTmp = new WeakReference(dst);
252: }
253: }
254:
255: performop.Blit(src, dst, comp, opclip, sx, sy, dx, dy,
256: width, height);
257:
258: if (convertresult != null) {
259: // assert: convertdst != null
260: convertTo(convertresult, dst, dstData, clip, dstx,
261: dsty, width, height);
262: }
263: }
264: }
265:
266: public GraphicsPrimitive traceWrap() {
267: return new TraceBlit(this );
268: }
269:
270: private static class TraceBlit extends Blit {
271: Blit target;
272:
273: public TraceBlit(Blit target) {
274: super (target.getSourceType(), target.getCompositeType(),
275: target.getDestType());
276: this .target = target;
277: }
278:
279: public GraphicsPrimitive traceWrap() {
280: return this ;
281: }
282:
283: public void Blit(SurfaceData src, SurfaceData dst,
284: Composite comp, Region clip, int srcx, int srcy,
285: int dstx, int dsty, int width, int height) {
286: tracePrimitive(target);
287: target.Blit(src, dst, comp, clip, srcx, srcy, dstx, dsty,
288: width, height);
289: }
290: }
291: }
|