jdk/src/share/classes/sun/java2d/pipe/GeneralCompositePipe.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2002 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.java2d.pipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AlphaComposite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.CompositeContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.PaintContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.image.BufImgSurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.java2d.SunGraphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.java2d.loops.Blit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.java2d.loops.MaskBlit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.java2d.loops.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class GeneralCompositePipe implements CompositePipe {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    class TileContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        SunGraphics2D sunG2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        PaintContext paintCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        CompositeContext compCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        ColorModel compModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Object pipeState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public TileContext(SunGraphics2D sg, PaintContext pCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                           CompositeContext cCtx, ColorModel cModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            sunG2D = sg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            paintCtxt = pCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            compCtxt = cCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            compModel = cModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public Object startSequence(SunGraphics2D sg, Shape s, Rectangle devR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                int[] abox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        RenderingHints hints = sg.getRenderingHints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ColorModel model = sg.getDeviceColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        PaintContext paintContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            sg.paint.createContext(model, devR, s.getBounds2D(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                   sg.cloneTransform(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                   hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        CompositeContext compositeContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            sg.composite.createContext(paintContext.getColorModel(), model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                       hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new TileContext(sg, paintContext, compositeContext, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public boolean needTile(Object ctx, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    * GeneralCompositePipe.renderPathTile works with custom composite operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    * provided by an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void renderPathTile(Object ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                               byte[] atile, int offset, int tilesize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                               int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        TileContext context = (TileContext) ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        PaintContext paintCtxt = context.paintCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        CompositeContext compCtxt = context.compCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SunGraphics2D sg = context.sunG2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Raster srcRaster = paintCtxt.getRaster(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ColorModel paintModel = paintCtxt.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Raster dstRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Raster dstIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        WritableRaster dstOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        SurfaceData sd = sg.getSurfaceData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        dstRaster = sd.getRaster(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (dstRaster instanceof WritableRaster && atile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            dstOut = (WritableRaster) dstRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            dstOut = dstOut.createWritableChild(x, y, w, h, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            dstIn = dstOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            dstIn = dstRaster.createChild(x, y, w, h, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            dstOut = dstIn.createCompatibleWritableRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        compCtxt.compose(srcRaster, dstIn, dstOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (dstRaster != dstOut && dstOut.getParent() != dstRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (dstRaster instanceof WritableRaster && atile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                ((WritableRaster) dstRaster).setDataElements(x, y, dstOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                ColorModel cm = sg.getDeviceColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                BufferedImage resImg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    new BufferedImage(cm, dstOut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                      cm.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                      null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                SurfaceData resData = BufImgSurfaceData.createData(resImg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (atile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    Blit blit = Blit.getFromCache(resData.getSurfaceType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                  CompositeType.SrcNoEa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                  sd.getSurfaceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    blit.Blit(resData, sd, AlphaComposite.Src, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                              0, 0, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    MaskBlit blit = MaskBlit.getFromCache(resData.getSurfaceType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                          CompositeType.SrcNoEa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                          sd.getSurfaceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    blit.MaskBlit(resData, sd, AlphaComposite.Src, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                  0, 0, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                  atile, offset, tilesize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void skipTile(Object ctx, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void endSequence(Object ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        TileContext context = (TileContext) ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (context.paintCtxt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            context.paintCtxt.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (context.compCtxt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            context.compCtxt.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}