jdk/src/share/classes/sun/java2d/loops/BlitBg.java
author tdv
Fri, 18 Jul 2008 10:48:44 -0700
changeset 887 0aab8d3fa11a
parent 2 90ce3da70b43
child 888 c7009cf0001f
permissions -rw-r--r--
6725214: D3D: forward-port the new pipeline from 6u10 Summary: Forward port of the new Direct3D 9 rendering pipeline from 6u10. Also includes fixes for 6690659 6689025 6658398 6596234. Reviewed-by: campbell, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems Microsystems, Inc.  All Rights Reserved.
2
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.loops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Composite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.AlphaComposite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.image.BufImgSurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.java2d.loops.GraphicsPrimitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.java2d.SunGraphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.java2d.pipe.Region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * BlitBg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 1) copies rectangle of pixels from one surface to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 2) performs compositing of colors based upon a Composite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * 3) assumes that non-opaque pixels are to be blended with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *    the indicated Bg color before compositing with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * precise behavior is undefined if the source surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * and the destination surface are the same surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * with overlapping regions of pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class BlitBg extends GraphicsPrimitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final String methodSignature = "BlitBg(...)".toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int primTypeID = makePrimTypeID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static RenderCache blitcache = new RenderCache(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static BlitBg locate(SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return (BlitBg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            GraphicsPrimitiveMgr.locate(primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                        srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static BlitBg getFromCache(SurfaceType src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                      CompositeType comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                      SurfaceType dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Object o = blitcache.get(src, comp, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return (BlitBg) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        BlitBg blit = locate(src, comp, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (blit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println("blitbg loop not found for:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println("src:  "+src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println("comp: "+comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println("dst:  "+dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            blitcache.put(src, comp, dst, blit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return blit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected BlitBg(SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                     CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                     SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super(methodSignature, primTypeID, srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public BlitBg(long pNativePrim,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                  SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                  CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                  SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super(pNativePrim, methodSignature, primTypeID, srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * All BlitBg implementors must have this invoker method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public native void BlitBg(SurfaceData src, SurfaceData dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                              Composite comp, Region clip,
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   112
                              int bgColor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                              int srcx, int srcy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                              int dstx, int dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                              int width, int height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        GraphicsPrimitiveMgr.registerGeneral(new BlitBg(null, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public GraphicsPrimitive makePrimitive(SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                           CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                           SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println("Constructing general blitbg for:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("src:  "+srctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println("comp: "+comptype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.out.println("dst:  "+dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return new General(srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static class General extends BlitBg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        CompositeType compositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public General(SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                       CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                       SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            super(srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            compositeType = comptype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   145
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public void BlitBg(SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                           SurfaceData dstData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                           Composite comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                           Region clip,
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   150
                           int bgArgb,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                           int srcx, int srcy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                           int dstx, int dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ColorModel dstModel = dstData.getColorModel();
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   156
            boolean bgHasAlpha = (bgArgb >>> 24) != 0xff;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   157
            if (!dstModel.hasAlpha() && bgHasAlpha) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                dstModel = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            WritableRaster wr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                dstModel.createCompatibleWritableRaster(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            boolean isPremult = dstModel.isAlphaPremultiplied();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            BufferedImage bimg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                new BufferedImage(dstModel, wr, isPremult, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            SurfaceData tmpData = BufImgSurfaceData.createData(bimg);
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   166
            Color bgColor = new Color(bgArgb, bgHasAlpha);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            SunGraphics2D sg2d = new SunGraphics2D(tmpData, bgColor, bgColor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                                   defaultFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            FillRect fillop = FillRect.locate(SurfaceType.AnyColor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                              CompositeType.SrcNoEa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                              tmpData.getSurfaceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Blit combineop = Blit.getFromCache(srcData.getSurfaceType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                               CompositeType.SrcOverNoEa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                               tmpData.getSurfaceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            Blit blitop = Blit.getFromCache(tmpData.getSurfaceType(), compositeType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                            dstData.getSurfaceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            fillop.FillRect(sg2d, tmpData, 0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            combineop.Blit(srcData, tmpData, AlphaComposite.SrcOver, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                           srcx, srcy, 0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            blitop.Blit(tmpData, dstData, comp, clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        0, 0, dstx, dsty, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        private static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public GraphicsPrimitive traceWrap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return new TraceBlitBg(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static class TraceBlitBg extends BlitBg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        BlitBg target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        public TraceBlitBg(BlitBg target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            super(target.getSourceType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                  target.getCompositeType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                  target.getDestType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public GraphicsPrimitive traceWrap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   205
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public void BlitBg(SurfaceData src, SurfaceData dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                           Composite comp, Region clip,
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   208
                           int bgColor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                           int srcx, int srcy, int dstx, int dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                           int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            tracePrimitive(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            target.BlitBg(src, dst, comp, clip, bgColor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                          srcx, srcy, dstx, dsty, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}