Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pipe/SpanShapeRenderer.java
38918 views
/*1* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pipe;2627import sun.java2d.SunGraphics2D;28import sun.java2d.SurfaceData;29import java.awt.Rectangle;30import java.awt.Shape;31import java.awt.BasicStroke;32import java.awt.geom.PathIterator;33import java.awt.geom.AffineTransform;34import java.awt.geom.Rectangle2D;35import sun.awt.SunHints;3637/**38* This class is used to convert raw geometry into a span iterator39* object using a simple flattening polygon scan converter.40* The iterator can be passed on to special SpanFiller loops to41* perform the actual rendering.42*/43public abstract class SpanShapeRenderer implements ShapeDrawPipe {44final static RenderingEngine RenderEngine = RenderingEngine.getInstance();4546public static class Composite extends SpanShapeRenderer {47CompositePipe comppipe;4849public Composite(CompositePipe pipe) {50comppipe = pipe;51}5253public Object startSequence(SunGraphics2D sg, Shape s,54Rectangle devR, int[] bbox) {55return comppipe.startSequence(sg, s, devR, bbox);56}5758public void renderBox(Object ctx, int x, int y, int w, int h) {59comppipe.renderPathTile(ctx, null, 0, w, x, y, w, h);60}6162public void endSequence(Object ctx) {63comppipe.endSequence(ctx);64}65}6667public static class Simple extends SpanShapeRenderer68implements LoopBasedPipe69{70public Object startSequence(SunGraphics2D sg, Shape s,71Rectangle devR, int[] bbox) {72return sg;73}7475public void renderBox(Object ctx, int x, int y, int w, int h) {76SunGraphics2D sg2d = (SunGraphics2D) ctx;77SurfaceData sd = sg2d.getSurfaceData();78sg2d.loops.fillRectLoop.FillRect(sg2d, sd, x, y, w, h);79}8081public void endSequence(Object ctx) {82}83}8485public void draw(SunGraphics2D sg, Shape s) {86if (sg.stroke instanceof BasicStroke) {87ShapeSpanIterator sr = LoopPipe.getStrokeSpans(sg, s);88try {89renderSpans(sg, sg.getCompClip(), s, sr);90} finally {91sr.dispose();92}93} else {94fill(sg, sg.stroke.createStrokedShape(s));95}96}9798public static final int NON_RECTILINEAR_TRANSFORM_MASK =99(AffineTransform.TYPE_GENERAL_TRANSFORM |100AffineTransform.TYPE_GENERAL_ROTATION);101102public void fill(SunGraphics2D sg, Shape s) {103if (s instanceof Rectangle2D &&104(sg.transform.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)105{106renderRect(sg, (Rectangle2D) s);107return;108}109110Region clipRegion = sg.getCompClip();111ShapeSpanIterator sr = LoopPipe.getFillSSI(sg);112try {113sr.setOutputArea(clipRegion);114sr.appendPath(s.getPathIterator(sg.transform));115renderSpans(sg, clipRegion, s, sr);116} finally {117sr.dispose();118}119}120121public abstract Object startSequence(SunGraphics2D sg, Shape s,122Rectangle devR, int[] bbox);123124public abstract void renderBox(Object ctx, int x, int y, int w, int h);125126public abstract void endSequence(Object ctx);127128public void renderRect(SunGraphics2D sg, Rectangle2D r) {129double corners[] = {130r.getX(), r.getY(), r.getWidth(), r.getHeight(),131};132corners[2] += corners[0];133corners[3] += corners[1];134if (corners[2] <= corners[0] || corners[3] <= corners[1]) {135return;136}137sg.transform.transform(corners, 0, corners, 0, 2);138if (corners[2] < corners[0]) {139double t = corners[2];140corners[2] = corners[0];141corners[0] = t;142}143if (corners[3] < corners[1]) {144double t = corners[3];145corners[3] = corners[1];146corners[1] = t;147}148int abox[] = {149(int) corners[0],150(int) corners[1],151(int) corners[2],152(int) corners[3],153};154Rectangle devR = new Rectangle(abox[0], abox[1],155abox[2] - abox[0],156abox[3] - abox[1]);157Region clipRegion = sg.getCompClip();158clipRegion.clipBoxToBounds(abox);159if (abox[0] >= abox[2] || abox[1] >= abox[3]) {160return;161}162Object context = startSequence(sg, r, devR, abox);163if (clipRegion.isRectangular()) {164renderBox(context, abox[0], abox[1],165abox[2] - abox[0],166abox[3] - abox[1]);167} else {168SpanIterator sr = clipRegion.getSpanIterator(abox);169while (sr.nextSpan(abox)) {170renderBox(context, abox[0], abox[1],171abox[2] - abox[0],172abox[3] - abox[1]);173}174}175endSequence(context);176}177178public void renderSpans(SunGraphics2D sg, Region clipRegion, Shape s,179ShapeSpanIterator sr)180{181Object context = null;182int abox[] = new int[4];183try {184sr.getPathBox(abox);185Rectangle devR = new Rectangle(abox[0], abox[1],186abox[2] - abox[0],187abox[3] - abox[1]);188clipRegion.clipBoxToBounds(abox);189if (abox[0] >= abox[2] || abox[1] >= abox[3]) {190return;191}192sr.intersectClipBox(abox[0], abox[1], abox[2], abox[3]);193context = startSequence(sg, s, devR, abox);194195spanClipLoop(context, sr, clipRegion, abox);196197} finally {198if (context != null) {199endSequence(context);200}201}202}203204public void spanClipLoop(Object ctx, SpanIterator sr,205Region r, int[] abox) {206if (!r.isRectangular()) {207sr = r.filter(sr);208}209while (sr.nextSpan(abox)) {210int x = abox[0];211int y = abox[1];212renderBox(ctx, x, y, abox[2] - x, abox[3] - y);213}214}215}216217218