001: /*
002: * Copyright 1997-2002 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.pipe;
027:
028: import java.awt.AlphaComposite;
029: import java.awt.CompositeContext;
030: import java.awt.PaintContext;
031: import java.awt.Rectangle;
032: import java.awt.Shape;
033: import java.awt.RenderingHints;
034: import java.awt.image.ColorModel;
035: import java.awt.image.BufferedImage;
036: import java.awt.image.Raster;
037: import java.awt.image.WritableRaster;
038: import sun.awt.image.BufImgSurfaceData;
039: import sun.java2d.SunGraphics2D;
040: import sun.java2d.SurfaceData;
041: import sun.java2d.loops.Blit;
042: import sun.java2d.loops.MaskBlit;
043: import sun.java2d.loops.CompositeType;
044:
045: public class GeneralCompositePipe implements CompositePipe {
046: class TileContext {
047: SunGraphics2D sunG2D;
048: PaintContext paintCtxt;
049: CompositeContext compCtxt;
050: ColorModel compModel;
051: Object pipeState;
052:
053: public TileContext(SunGraphics2D sg, PaintContext pCtx,
054: CompositeContext cCtx, ColorModel cModel) {
055: sunG2D = sg;
056: paintCtxt = pCtx;
057: compCtxt = cCtx;
058: compModel = cModel;
059: }
060: }
061:
062: public Object startSequence(SunGraphics2D sg, Shape s,
063: Rectangle devR, int[] abox) {
064: RenderingHints hints = sg.getRenderingHints();
065: ColorModel model = sg.getDeviceColorModel();
066: PaintContext paintContext = sg.paint.createContext(model, devR,
067: s.getBounds2D(), sg.cloneTransform(), hints);
068: CompositeContext compositeContext = sg.composite.createContext(
069: paintContext.getColorModel(), model, hints);
070: return new TileContext(sg, paintContext, compositeContext,
071: model);
072: }
073:
074: public boolean needTile(Object ctx, int x, int y, int w, int h) {
075: return true;
076: }
077:
078: /**
079: * GeneralCompositePipe.renderPathTile works with custom composite operator
080: * provided by an application
081: */
082: public void renderPathTile(Object ctx, byte[] atile, int offset,
083: int tilesize, int x, int y, int w, int h) {
084: TileContext context = (TileContext) ctx;
085: PaintContext paintCtxt = context.paintCtxt;
086: CompositeContext compCtxt = context.compCtxt;
087: SunGraphics2D sg = context.sunG2D;
088:
089: Raster srcRaster = paintCtxt.getRaster(x, y, w, h);
090: ColorModel paintModel = paintCtxt.getColorModel();
091:
092: Raster dstRaster;
093: Raster dstIn;
094: WritableRaster dstOut;
095:
096: SurfaceData sd = sg.getSurfaceData();
097: dstRaster = sd.getRaster(x, y, w, h);
098: if (dstRaster instanceof WritableRaster && atile == null) {
099: dstOut = (WritableRaster) dstRaster;
100: dstOut = dstOut.createWritableChild(x, y, w, h, 0, 0, null);
101: dstIn = dstOut;
102: } else {
103: dstIn = dstRaster.createChild(x, y, w, h, 0, 0, null);
104: dstOut = dstIn.createCompatibleWritableRaster();
105: }
106:
107: compCtxt.compose(srcRaster, dstIn, dstOut);
108:
109: if (dstRaster != dstOut && dstOut.getParent() != dstRaster) {
110: if (dstRaster instanceof WritableRaster && atile == null) {
111: ((WritableRaster) dstRaster).setDataElements(x, y,
112: dstOut);
113: } else {
114: ColorModel cm = sg.getDeviceColorModel();
115: BufferedImage resImg = new BufferedImage(cm, dstOut, cm
116: .isAlphaPremultiplied(), null);
117: SurfaceData resData = BufImgSurfaceData
118: .createData(resImg);
119: if (atile == null) {
120: Blit blit = Blit.getFromCache(resData
121: .getSurfaceType(), CompositeType.SrcNoEa,
122: sd.getSurfaceType());
123: blit.Blit(resData, sd, AlphaComposite.Src, null, 0,
124: 0, x, y, w, h);
125: } else {
126: MaskBlit blit = MaskBlit.getFromCache(resData
127: .getSurfaceType(), CompositeType.SrcNoEa,
128: sd.getSurfaceType());
129: blit.MaskBlit(resData, sd, AlphaComposite.Src,
130: null, 0, 0, x, y, w, h, atile, offset,
131: tilesize);
132: }
133: }
134: }
135: }
136:
137: public void skipTile(Object ctx, int x, int y) {
138: return;
139: }
140:
141: public void endSequence(Object ctx) {
142: TileContext context = (TileContext) ctx;
143: if (context.paintCtxt != null) {
144: context.paintCtxt.dispose();
145: }
146: if (context.compCtxt != null) {
147: context.compCtxt.dispose();
148: }
149: }
150:
151: }
|