jdk/src/share/classes/sun/java2d/pipe/BufferedMaskBlit.java
author neugens
Fri, 30 Oct 2009 19:19:35 +0100
changeset 4250 e88776b21913
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6896068: SunGraphics2D exposes a reference to itself while non fully initialised. Summary: Introduce a new Interface to mark the Loops based pipes and initialise the loops accordingly. Reviewed-by: flar, rkennke
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 2007 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.Composite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.java2d.loops.Blit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.java2d.loops.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.java2d.loops.MaskBlit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.java2d.loops.SurfaceType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static sun.java2d.pipe.BufferedOpCodes.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The MaskBlit operation is expressed as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   dst = ((src <MODE> dst) * pathA) + (dst * (1 - pathA))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The OGL/D3D implementation of the MaskBlit operation differs from the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * equation because it is not possible to perform such a complex operation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * OpenGL/Direct3D (without the use of advanced techniques like fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * shaders and multitexturing).  Therefore, the BufferedMaskBlit operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * is expressed as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   dst = (src * pathA) <SrcOver> dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This simplified formula is only equivalent to the "true" MaskBlit equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * in the following situations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   - <MODE> is SrcOver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   - <MODE> is Src, extra alpha == 1.0, and the source surface is opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Therefore, we register BufferedMaskBlit primitives for only the SurfaceType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and CompositeType restrictions mentioned above.  In addition for the Src
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * case, we must override the composite with a SrcOver (no extra alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * instance, so that we set up the OpenGL/Direct3D blending mode to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * BufferedMaskBlit equation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class BufferedMaskBlit extends MaskBlit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int ST_INT_ARGB     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int ST_INT_ARGB_PRE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int ST_INT_RGB      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int ST_INT_BGR      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final RenderQueue rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final int srcTypeVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Blit blitop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected BufferedMaskBlit(RenderQueue rq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                               SurfaceType srcType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                               CompositeType compType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                               SurfaceType dstType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(srcType, compType, dstType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.rq = rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (srcType == SurfaceType.IntArgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            this.srcTypeVal = ST_INT_ARGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else if (srcType == SurfaceType.IntArgbPre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            this.srcTypeVal = ST_INT_ARGB_PRE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else if (srcType == SurfaceType.IntRgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            this.srcTypeVal = ST_INT_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else if (srcType == SurfaceType.IntBgr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.srcTypeVal = ST_INT_BGR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new InternalError("unrecognized source surface type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void MaskBlit(SurfaceData src, SurfaceData dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                         Composite comp, Region clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                         int srcx, int srcy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                         int dstx, int dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                         int width, int height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                         byte[] mask, int maskoff, int maskscan)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (width <= 0 || height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (mask == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // no mask involved; delegate to regular blit loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (blitop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                blitop = Blit.getFromCache(src.getSurfaceType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                           CompositeType.AnyAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                           this.getDestType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            blitop.Blit(src, dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        comp, clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        srcx, srcy, dstx, dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        AlphaComposite acomp = (AlphaComposite)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (acomp.getRule() != AlphaComposite.SRC_OVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            comp = AlphaComposite.SrcOver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        rq.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            validateContext(dst, comp, clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int totalBytesRequired = 20 + (width * height * 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * REMIND: we should fix this so that it works with tiles that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             *         are larger than the entire buffer, but the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             *         OGL/D3DMaskBlit isn't even prepared for tiles larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             *         than 32x32 pixels, so there's no urgency here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            rq.ensureCapacity(totalBytesRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // enqueue parameters and tile pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int newpos = enqueueTile(buf.getAddress(), buf.position(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                     src, src.getNativeOps(), srcTypeVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                     mask, mask.length, maskoff, maskscan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                     srcx, srcy, dstx, dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                     width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            buf.position(newpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            rq.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private native int enqueueTile(long buf, int bpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                   SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                   long pSrcOps, int srcType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                   byte[] mask, int masklen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                   int maskoff, int maskscan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                   int srcx, int srcy, int dstx, int dsty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                   int width, int height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Validates the context state using the given destination surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * and composite/clip values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected abstract void validateContext(SurfaceData dstData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                            Composite comp, Region clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}