src/java.desktop/macosx/classes/sun/java2d/metal/MTLBufImgOps.java
author jdv
Fri, 21 Jun 2019 12:08:37 +0530
branchmetal-prototype-branch
changeset 57416 e153174dba06
permissions -rw-r--r--
Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch Reviewed-by: avu, prr, kcr Contributed-by: avu(Jetbrains), aghaisas, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57416
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     1
/*
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     4
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    10
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    15
 * accompanied this code).
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    16
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    20
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    23
 * questions.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    24
 */
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    25
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    26
package sun.java2d.metal;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    27
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    28
import sun.java2d.SunGraphics2D;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    29
import sun.java2d.SurfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    30
import sun.java2d.loops.CompositeType;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    31
import sun.java2d.pipe.BufferedBufImgOps;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    32
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    33
import java.awt.image.*;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    34
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    35
import static sun.java2d.metal.MTLContext.MTLContextCaps.CAPS_EXT_BIOP_SHADER;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    36
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    37
class MTLBufImgOps extends BufferedBufImgOps {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    38
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    39
    /**
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    40
     * This method is called from MTLDrawImage.transformImage() only.  It
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    41
     * validates the provided BufferedImageOp to determine whether the op
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    42
     * is one that can be accelerated by the MTL pipeline.  If the operation
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    43
     * cannot be completed for any reason, this method returns false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    44
     * otherwise, the given BufferedImage is rendered to the destination
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    45
     * using the provided BufferedImageOp and this method returns true.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    46
     */
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    47
    static boolean renderImageWithOp(SunGraphics2D sg, BufferedImage img,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    48
                                     BufferedImageOp biop, int x, int y)
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    49
    {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    50
        // Validate the provided BufferedImage (make sure it is one that
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    51
        // is supported, and that its properties are acceleratable)
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    52
        if (biop instanceof ConvolveOp) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    53
            if (!isConvolveOpValid((ConvolveOp)biop)) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    54
                return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    55
            }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    56
        } else if (biop instanceof RescaleOp) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    57
            if (!isRescaleOpValid((RescaleOp)biop, img)) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    58
                return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    59
            }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    60
        } else if (biop instanceof LookupOp) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    61
            if (!isLookupOpValid((LookupOp)biop, img)) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    62
                return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    63
            }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    64
        } else {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    65
            // No acceleration for other BufferedImageOps (yet)
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    66
            return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    67
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    68
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    69
        SurfaceData dstData = sg.surfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    70
        if (!(dstData instanceof MTLSurfaceData) ||
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    71
                (sg.interpolationType == AffineTransformOp.TYPE_BICUBIC) ||
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    72
                (sg.compositeState > SunGraphics2D.COMP_ALPHA))
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    73
        {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    74
            return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    75
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    76
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    77
        SurfaceData srcData =
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    78
                dstData.getSourceSurfaceData(img, SunGraphics2D.TRANSFORM_ISIDENT,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    79
                        CompositeType.SrcOver, null);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    80
        if (!(srcData instanceof MTLSurfaceData)) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    81
            // REMIND: this hack tries to ensure that we have a cached texture
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    82
            srcData =
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    83
                    dstData.getSourceSurfaceData(img, SunGraphics2D.TRANSFORM_ISIDENT,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    84
                            CompositeType.SrcOver, null);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    85
            if (!(srcData instanceof MTLSurfaceData)) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    86
                return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    87
            }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    88
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    89
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    90
        // Verify that the source surface is actually a texture and
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    91
        // that the operation is supported
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    92
        MTLSurfaceData mtlSrc = (MTLSurfaceData)srcData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    93
        MTLGraphicsConfig gc = mtlSrc.getMTLGraphicsConfig();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    94
        if (mtlSrc.getType() != MTLSurfaceData.TEXTURE ||
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    95
                !gc.isCapPresent(CAPS_EXT_BIOP_SHADER))
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    96
        {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    97
            return false;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    98
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    99
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   100
        int sw = img.getWidth();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   101
        int sh = img.getHeight();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   102
        MTLBlitLoops.IsoBlit(srcData, dstData,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   103
                img, biop,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   104
                sg.composite, sg.getCompClip(),
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   105
                sg.transform, sg.interpolationType,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   106
                0, 0, sw, sh,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   107
                x, y, x+sw, y+sh,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   108
                true);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   109
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   110
        return true;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   111
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   112
}