jdk/src/share/classes/sun/java2d/pipe/BufferedMaskFill.java
author flar
Mon, 06 Dec 2010 21:45:48 -0800
changeset 7487 9b031d062ede
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
6775317: Improve performance of non-AA transformed rectangles and single wide lines in software pipelines Reviewed-by: jgodinez, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.SunGraphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.java2d.SurfaceData;
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.MaskFill;
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 MaskFill 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 MaskFill 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 BufferedMaskFill 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" MaskFill 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 paint is opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Therefore, we register BufferedMaskFill primitives for only the SurfaceType
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and CompositeType restrictions mentioned above.  In addition, for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * SrcNoEa case we must override the incoming composite with a SrcOver (no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * extra alpha) instance, so that we set up the OpenGL/Direct3D blending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * mode to match the BufferedMaskFill equation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class BufferedMaskFill extends MaskFill {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected final RenderQueue rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected BufferedMaskFill(RenderQueue rq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               SurfaceType srcType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                               CompositeType compType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                               SurfaceType dstType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        super(srcType, compType, dstType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.rq = rq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void MaskFill(SunGraphics2D sg2d, SurfaceData sData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                         Composite comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                         final int x, final int y, final int w, final int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                         final byte[] mask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                         final int maskoff, final int maskscan)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        AlphaComposite acomp = (AlphaComposite)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (acomp.getRule() != AlphaComposite.SRC_OVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            comp = AlphaComposite.SrcOver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        rq.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            validateContext(sg2d, comp, BufferedContext.USE_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // we adjust the mask length so that the mask ends on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // 4-byte boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            int maskBytesRequired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (mask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                // we adjust the mask length so that the mask ends on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // 4-byte boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                maskBytesRequired = (mask.length + 3) & (~3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // mask not needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                maskBytesRequired = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            int totalBytesRequired = 32 + maskBytesRequired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (totalBytesRequired <= buf.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (totalBytesRequired > buf.remaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    // process the queue first and then enqueue the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    rq.flushNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                buf.putInt(MASK_FILL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                // enqueue parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                buf.putInt(x).putInt(y).putInt(w).putInt(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                buf.putInt(maskoff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                buf.putInt(maskscan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                buf.putInt(maskBytesRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (mask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    // enqueue the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    int padding = maskBytesRequired - mask.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    buf.put(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    if (padding != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        buf.position(buf.position() + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // queue is too small to accomodate entire mask; perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // the operation directly on the queue flushing thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                rq.flushAndInvokeNow(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        maskFill(x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                 maskoff, maskscan, mask.length, mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            rq.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
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
     * Called as a separate Runnable when the operation is too large to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * on the RenderQueue.  The OGL/D3D pipelines each have their own (small)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * native implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected abstract void maskFill(int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                     int maskoff, int maskscan, int masklen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                     byte[] mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Validates the state in the provided SunGraphics2D object and sets up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * any special resources for this operation (e.g. enabling gradient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * shading).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected abstract void validateContext(SunGraphics2D sg2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                            Composite comp, int ctxflags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}