1 /*
   2  * Copyright (c) 2019, 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 java.awt.Transparency;
  29 import java.awt.geom.Path2D;
  30 import sun.java2d.InvalidPipeException;
  31 import sun.java2d.SunGraphics2D;
  32 import sun.java2d.loops.GraphicsPrimitive;
  33 import sun.java2d.pipe.BufferedRenderPipe;
  34 import sun.java2d.pipe.ParallelogramPipe;
  35 import sun.java2d.pipe.RenderQueue;
  36 import sun.java2d.pipe.SpanIterator;
  37 import static sun.java2d.pipe.BufferedOpCodes.*;
  38 
  39 class MetalRenderer extends BufferedRenderPipe {
  40 
  41     MetalRenderer(RenderQueue rq) {
  42         super(rq);
  43     }
  44 
  45     @Override
  46     protected void validateContext(SunGraphics2D sg2d) {
  47         int ctxflags =
  48             sg2d.paint.getTransparency() == Transparency.OPAQUE ?
  49                 MetalContext.SRC_IS_OPAQUE : MetalContext.NO_CONTEXT_FLAGS;
  50         MetalSurfaceData dstData;
  51         try {
  52             dstData = (MetalSurfaceData)sg2d.surfaceData;
  53         } catch (ClassCastException e) {
  54             throw new InvalidPipeException("wrong surface data type: " + sg2d.surfaceData);
  55         }
  56         MetalContext.validateContext(dstData, dstData,
  57                                    sg2d.getCompClip(), sg2d.composite,
  58                                    null, sg2d.paint, sg2d, ctxflags);
  59     }
  60 
  61     @Override
  62     protected void validateContextAA(SunGraphics2D sg2d) {
  63         int ctxflags = MetalContext.NO_CONTEXT_FLAGS;
  64         MetalSurfaceData dstData;
  65         try {
  66             dstData = (MetalSurfaceData)sg2d.surfaceData;
  67         } catch (ClassCastException e) {
  68             throw new InvalidPipeException("wrong surface data type: " + sg2d.surfaceData);
  69         }
  70         MetalContext.validateContext(dstData, dstData,
  71                                    sg2d.getCompClip(), sg2d.composite,
  72                                    null, sg2d.paint, sg2d, ctxflags);
  73     }
  74 
  75     void copyArea(SunGraphics2D sg2d,
  76                   int x, int y, int w, int h, int dx, int dy)
  77     {
  78         rq.lock();
  79         try {
  80             int ctxflags =
  81                 sg2d.surfaceData.getTransparency() == Transparency.OPAQUE ?
  82                     MetalContext.SRC_IS_OPAQUE : MetalContext.NO_CONTEXT_FLAGS;
  83             MetalSurfaceData dstData;
  84             try {
  85                 dstData = (MetalSurfaceData)sg2d.surfaceData;
  86             } catch (ClassCastException e) {
  87                 throw new InvalidPipeException("wrong surface data type: " + sg2d.surfaceData);
  88             }
  89             MetalContext.validateContext(dstData, dstData,
  90                                        sg2d.getCompClip(), sg2d.composite,
  91                                        null, null, null, ctxflags);
  92 
  93             rq.ensureCapacity(28);
  94             buf.putInt(COPY_AREA);
  95             buf.putInt(x).putInt(y).putInt(w).putInt(h);
  96             buf.putInt(dx).putInt(dy);
  97         } finally {
  98             rq.unlock();
  99         }
 100     }
 101 
 102     @Override
 103     protected native void drawPoly(int[] xPoints, int[] yPoints,
 104                                    int nPoints, boolean isClosed,
 105                                    int transX, int transY);
 106 
 107     MetalRenderer traceWrap() {
 108         return new Tracer(this);
 109     }
 110 
 111     private class Tracer extends MetalRenderer {
 112         private MetalRenderer mtlr;
 113         Tracer(MetalRenderer mtlr) {
 114             super(mtlr.rq);
 115             this.mtlr = mtlr;
 116         }
 117         public ParallelogramPipe getAAParallelogramPipe() {
 118             final ParallelogramPipe realpipe = mtlr.getAAParallelogramPipe();
 119             return new ParallelogramPipe() {
 120                 public void fillParallelogram(SunGraphics2D sg2d,
 121                                               double ux1, double uy1,
 122                                               double ux2, double uy2,
 123                                               double x, double y,
 124                                               double dx1, double dy1,
 125                                               double dx2, double dy2)
 126                 {
 127                     GraphicsPrimitive.tracePrimitive("OGLFillAAParallelogram");
 128                     realpipe.fillParallelogram(sg2d,
 129                                                ux1, uy1, ux2, uy2,
 130                                                x, y, dx1, dy1, dx2, dy2);
 131                 }
 132                 public void drawParallelogram(SunGraphics2D sg2d,
 133                                               double ux1, double uy1,
 134                                               double ux2, double uy2,
 135                                               double x, double y,
 136                                               double dx1, double dy1,
 137                                               double dx2, double dy2,
 138                                               double lw1, double lw2)
 139                 {
 140                     GraphicsPrimitive.tracePrimitive("OGLDrawAAParallelogram");
 141                     realpipe.drawParallelogram(sg2d,
 142                                                ux1, uy1, ux2, uy2,
 143                                                x, y, dx1, dy1, dx2, dy2,
 144                                                lw1, lw2);
 145                 }
 146             };
 147         }
 148         protected void validateContext(SunGraphics2D sg2d) {
 149             mtlr.validateContext(sg2d);
 150         }
 151         public void drawLine(SunGraphics2D sg2d,
 152                              int x1, int y1, int x2, int y2)
 153         {
 154             GraphicsPrimitive.tracePrimitive("OGLDrawLine");
 155             mtlr.drawLine(sg2d, x1, y1, x2, y2);
 156         }
 157         public void drawRect(SunGraphics2D sg2d, int x, int y, int w, int h) {
 158             GraphicsPrimitive.tracePrimitive("OGLDrawRect");
 159             mtlr.drawRect(sg2d, x, y, w, h);
 160         }
 161         protected void drawPoly(SunGraphics2D sg2d,
 162                                 int[] xPoints, int[] yPoints,
 163                                 int nPoints, boolean isClosed)
 164         {
 165             GraphicsPrimitive.tracePrimitive("OGLDrawPoly");
 166             mtlr.drawPoly(sg2d, xPoints, yPoints, nPoints, isClosed);
 167         }
 168         public void fillRect(SunGraphics2D sg2d, int x, int y, int w, int h) {
 169             GraphicsPrimitive.tracePrimitive("OGLFillRect");
 170             mtlr.fillRect(sg2d, x, y, w, h);
 171         }
 172         protected void drawPath(SunGraphics2D sg2d,
 173                                 Path2D.Float p2df, int transx, int transy)
 174         {
 175             GraphicsPrimitive.tracePrimitive("OGLDrawPath");
 176             mtlr.drawPath(sg2d, p2df, transx, transy);
 177         }
 178         protected void fillPath(SunGraphics2D sg2d,
 179                                 Path2D.Float p2df, int transx, int transy)
 180         {
 181             GraphicsPrimitive.tracePrimitive("OGLFillPath");
 182             mtlr.fillPath(sg2d, p2df, transx, transy);
 183         }
 184         protected void fillSpans(SunGraphics2D sg2d, SpanIterator si,
 185                                  int transx, int transy)
 186         {
 187             GraphicsPrimitive.tracePrimitive("OGLFillSpans");
 188             mtlr.fillSpans(sg2d, si, transx, transy);
 189         }
 190         public void fillParallelogram(SunGraphics2D sg2d,
 191                                       double ux1, double uy1,
 192                                       double ux2, double uy2,
 193                                       double x, double y,
 194                                       double dx1, double dy1,
 195                                       double dx2, double dy2)
 196         {
 197             GraphicsPrimitive.tracePrimitive("OGLFillParallelogram");
 198             mtlr.fillParallelogram(sg2d,
 199                                    ux1, uy1, ux2, uy2,
 200                                    x, y, dx1, dy1, dx2, dy2);
 201         }
 202         public void drawParallelogram(SunGraphics2D sg2d,
 203                                       double ux1, double uy1,
 204                                       double ux2, double uy2,
 205                                       double x, double y,
 206                                       double dx1, double dy1,
 207                                       double dx2, double dy2,
 208                                       double lw1, double lw2)
 209         {
 210             GraphicsPrimitive.tracePrimitive("OGLDrawParallelogram");
 211             mtlr.drawParallelogram(sg2d,
 212                                    ux1, uy1, ux2, uy2,
 213                                    x, y, dx1, dy1, dx2, dy2, lw1, lw2);
 214         }
 215         public void copyArea(SunGraphics2D sg2d,
 216                              int x, int y, int w, int h, int dx, int dy)
 217         {
 218             GraphicsPrimitive.tracePrimitive("OGLCopyArea");
 219             mtlr.copyArea(sg2d, x, y, w, h, dx, dy);
 220         }
 221     }
 222 }