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