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