0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pipe;
0N/A
430N/Aimport java.awt.BasicStroke;
0N/Aimport java.awt.Polygon;
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.Arc2D;
0N/Aimport java.awt.geom.Ellipse2D;
0N/Aimport java.awt.geom.Path2D;
0N/Aimport java.awt.geom.IllegalPathStateException;
0N/Aimport java.awt.geom.PathIterator;
430N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.geom.RoundRectangle2D;
0N/Aimport sun.java2d.SunGraphics2D;
0N/Aimport sun.java2d.loops.ProcessPath;
0N/Aimport static sun.java2d.pipe.BufferedOpCodes.*;
0N/A
0N/A/**
0N/A * Base class for enqueuing rendering operations in a single-threaded
0N/A * rendering environment. Instead of each operation being rendered
0N/A * immediately by the underlying graphics library, the operation will be
0N/A * added to the provided RenderQueue, which will be processed at a later
0N/A * time by a single thread.
0N/A *
0N/A * This class provides implementations of drawLine(), drawRect(), drawPoly(),
0N/A * fillRect(), draw(Shape), and fill(Shape), which are useful for a
0N/A * hardware-accelerated renderer. The other draw*() and fill*() methods
0N/A * simply delegate to draw(Shape) and fill(Shape), respectively.
0N/A */
0N/Apublic abstract class BufferedRenderPipe
430N/A implements PixelDrawPipe, PixelFillPipe, ShapeDrawPipe, ParallelogramPipe
0N/A{
430N/A ParallelogramPipe aapgrampipe = new AAParallelogramPipe();
430N/A
0N/A static final int BYTES_PER_POLY_POINT = 8;
0N/A static final int BYTES_PER_SCANLINE = 12;
0N/A static final int BYTES_PER_SPAN = 16;
0N/A
0N/A protected RenderQueue rq;
0N/A protected RenderBuffer buf;
0N/A private BufferedDrawHandler drawHandler;
0N/A
0N/A public BufferedRenderPipe(RenderQueue rq) {
0N/A this.rq = rq;
0N/A this.buf = rq.getBuffer();
0N/A this.drawHandler = new BufferedDrawHandler();
0N/A }
0N/A
430N/A public ParallelogramPipe getAAParallelogramPipe() {
430N/A return aapgrampipe;
430N/A }
430N/A
0N/A /**
0N/A * Validates the state in the provided SunGraphics2D object and sets up
0N/A * any special resources for this operation (e.g. enabling gradient
0N/A * shading).
0N/A */
0N/A protected abstract void validateContext(SunGraphics2D sg2d);
430N/A protected abstract void validateContextAA(SunGraphics2D sg2d);
0N/A
0N/A public void drawLine(SunGraphics2D sg2d,
0N/A int x1, int y1, int x2, int y2)
0N/A {
0N/A int transx = sg2d.transX;
0N/A int transy = sg2d.transY;
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A rq.ensureCapacity(20);
0N/A buf.putInt(DRAW_LINE);
0N/A buf.putInt(x1 + transx);
0N/A buf.putInt(y1 + transy);
0N/A buf.putInt(x2 + transx);
0N/A buf.putInt(y2 + transy);
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A public void drawRect(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height)
0N/A {
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A rq.ensureCapacity(20);
0N/A buf.putInt(DRAW_RECT);
0N/A buf.putInt(x + sg2d.transX);
0N/A buf.putInt(y + sg2d.transY);
0N/A buf.putInt(width);
0N/A buf.putInt(height);
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A public void fillRect(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height)
0N/A {
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A rq.ensureCapacity(20);
0N/A buf.putInt(FILL_RECT);
0N/A buf.putInt(x + sg2d.transX);
0N/A buf.putInt(y + sg2d.transY);
0N/A buf.putInt(width);
0N/A buf.putInt(height);
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A public void drawRoundRect(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height,
0N/A int arcWidth, int arcHeight)
0N/A {
0N/A draw(sg2d, new RoundRectangle2D.Float(x, y, width, height,
0N/A arcWidth, arcHeight));
0N/A }
0N/A
0N/A public void fillRoundRect(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height,
0N/A int arcWidth, int arcHeight)
0N/A {
0N/A fill(sg2d, new RoundRectangle2D.Float(x, y, width, height,
0N/A arcWidth, arcHeight));
0N/A }
0N/A
0N/A public void drawOval(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height)
0N/A {
0N/A draw(sg2d, new Ellipse2D.Float(x, y, width, height));
0N/A }
0N/A
0N/A public void fillOval(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height)
0N/A {
0N/A fill(sg2d, new Ellipse2D.Float(x, y, width, height));
0N/A }
0N/A
0N/A public void drawArc(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height,
0N/A int startAngle, int arcAngle)
0N/A {
0N/A draw(sg2d, new Arc2D.Float(x, y, width, height,
0N/A startAngle, arcAngle,
0N/A Arc2D.OPEN));
0N/A }
0N/A
0N/A public void fillArc(SunGraphics2D sg2d,
0N/A int x, int y, int width, int height,
0N/A int startAngle, int arcAngle)
0N/A {
0N/A fill(sg2d, new Arc2D.Float(x, y, width, height,
0N/A startAngle, arcAngle,
0N/A Arc2D.PIE));
0N/A }
0N/A
0N/A protected void drawPoly(final SunGraphics2D sg2d,
0N/A final int[] xPoints, final int[] yPoints,
0N/A final int nPoints, final boolean isClosed)
0N/A {
0N/A if (xPoints == null || yPoints == null) {
0N/A throw new NullPointerException("coordinate array");
0N/A }
0N/A if (xPoints.length < nPoints || yPoints.length < nPoints) {
0N/A throw new ArrayIndexOutOfBoundsException("coordinate array");
0N/A }
0N/A
0N/A if (nPoints < 2) {
0N/A // render nothing
0N/A return;
0N/A } else if (nPoints == 2 && !isClosed) {
0N/A // render a simple line
0N/A drawLine(sg2d, xPoints[0], yPoints[0], xPoints[1], yPoints[1]);
0N/A return;
0N/A }
0N/A
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A
0N/A int pointBytesRequired = nPoints * BYTES_PER_POLY_POINT;
0N/A int totalBytesRequired = 20 + pointBytesRequired;
0N/A
0N/A if (totalBytesRequired <= buf.capacity()) {
0N/A if (totalBytesRequired > buf.remaining()) {
0N/A // process the queue first and then enqueue the points
0N/A rq.flushNow();
0N/A }
0N/A buf.putInt(DRAW_POLY);
0N/A // enqueue parameters
0N/A buf.putInt(nPoints);
0N/A buf.putInt(isClosed ? 1 : 0);
0N/A buf.putInt(sg2d.transX);
0N/A buf.putInt(sg2d.transY);
0N/A // enqueue the points
0N/A buf.put(xPoints, 0, nPoints);
0N/A buf.put(yPoints, 0, nPoints);
0N/A } else {
0N/A // queue is too small to accomodate all points; perform the
0N/A // operation directly on the queue flushing thread
0N/A rq.flushAndInvokeNow(new Runnable() {
0N/A public void run() {
0N/A drawPoly(xPoints, yPoints,
0N/A nPoints, isClosed,
0N/A sg2d.transX, sg2d.transY);
0N/A }
0N/A });
0N/A }
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A protected abstract void drawPoly(int[] xPoints, int[] yPoints,
0N/A int nPoints, boolean isClosed,
0N/A int transX, int transY);
0N/A
0N/A public void drawPolyline(SunGraphics2D sg2d,
0N/A int[] xPoints, int[] yPoints,
0N/A int nPoints)
0N/A {
0N/A drawPoly(sg2d, xPoints, yPoints, nPoints, false);
0N/A }
0N/A
0N/A public void drawPolygon(SunGraphics2D sg2d,
0N/A int[] xPoints, int[] yPoints,
0N/A int nPoints)
0N/A {
0N/A drawPoly(sg2d, xPoints, yPoints, nPoints, true);
0N/A }
0N/A
0N/A public void fillPolygon(SunGraphics2D sg2d,
0N/A int[] xPoints, int[] yPoints,
0N/A int nPoints)
0N/A {
0N/A fill(sg2d, new Polygon(xPoints, yPoints, nPoints));
0N/A }
0N/A
0N/A private class BufferedDrawHandler
0N/A extends ProcessPath.DrawHandler
0N/A {
0N/A BufferedDrawHandler() {
0N/A // these are bogus values; the caller will use validate()
0N/A // to ensure that they are set properly prior to each usage
0N/A super(0, 0, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * This method needs to be called prior to each draw/fillPath()
0N/A * operation to ensure the clip bounds are up to date.
0N/A */
0N/A void validate(SunGraphics2D sg2d) {
0N/A Region clip = sg2d.getCompClip();
0N/A setBounds(clip.getLoX(), clip.getLoY(),
0N/A clip.getHiX(), clip.getHiY(),
0N/A sg2d.strokeHint);
0N/A }
0N/A
0N/A /**
0N/A * drawPath() support...
0N/A */
0N/A
0N/A public void drawLine(int x1, int y1, int x2, int y2) {
0N/A // assert rq.lock.isHeldByCurrentThread();
0N/A rq.ensureCapacity(20);
0N/A buf.putInt(DRAW_LINE);
0N/A buf.putInt(x1);
0N/A buf.putInt(y1);
0N/A buf.putInt(x2);
0N/A buf.putInt(y2);
0N/A }
0N/A
0N/A public void drawPixel(int x, int y) {
0N/A // assert rq.lock.isHeldByCurrentThread();
0N/A rq.ensureCapacity(12);
0N/A buf.putInt(DRAW_PIXEL);
0N/A buf.putInt(x);
0N/A buf.putInt(y);
0N/A }
0N/A
0N/A /**
0N/A * fillPath() support...
0N/A */
0N/A
0N/A private int scanlineCount;
0N/A private int scanlineCountIndex;
0N/A private int remainingScanlines;
0N/A
0N/A private void resetFillPath() {
0N/A buf.putInt(DRAW_SCANLINES);
0N/A scanlineCountIndex = buf.position();
0N/A buf.putInt(0);
0N/A scanlineCount = 0;
0N/A remainingScanlines = buf.remaining() / BYTES_PER_SCANLINE;
0N/A }
0N/A
0N/A private void updateScanlineCount() {
0N/A buf.putInt(scanlineCountIndex, scanlineCount);
0N/A }
0N/A
0N/A /**
0N/A * Called from fillPath() to indicate that we are about to
0N/A * start issuing drawScanline() calls.
0N/A */
0N/A public void startFillPath() {
0N/A rq.ensureCapacity(20); // to ensure room for at least a scanline
0N/A resetFillPath();
0N/A }
0N/A
0N/A public void drawScanline(int x1, int x2, int y) {
0N/A if (remainingScanlines == 0) {
0N/A updateScanlineCount();
0N/A rq.flushNow();
0N/A resetFillPath();
0N/A }
0N/A buf.putInt(x1);
0N/A buf.putInt(x2);
0N/A buf.putInt(y);
0N/A scanlineCount++;
0N/A remainingScanlines--;
0N/A }
0N/A
0N/A /**
0N/A * Called from fillPath() to indicate that we are done
0N/A * issuing drawScanline() calls.
0N/A */
0N/A public void endFillPath() {
0N/A updateScanlineCount();
0N/A }
0N/A }
0N/A
0N/A protected void drawPath(SunGraphics2D sg2d,
0N/A Path2D.Float p2df, int transx, int transy)
0N/A {
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A drawHandler.validate(sg2d);
0N/A ProcessPath.drawPath(drawHandler, p2df, transx, transy);
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A protected void fillPath(SunGraphics2D sg2d,
0N/A Path2D.Float p2df, int transx, int transy)
0N/A {
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A drawHandler.validate(sg2d);
0N/A drawHandler.startFillPath();
0N/A ProcessPath.fillPath(drawHandler, p2df, transx, transy);
0N/A drawHandler.endFillPath();
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
0N/A private native int fillSpans(RenderQueue rq, long buf,
0N/A int pos, int limit,
0N/A SpanIterator si, long iterator,
0N/A int transx, int transy);
0N/A
0N/A protected void fillSpans(SunGraphics2D sg2d, SpanIterator si,
0N/A int transx, int transy)
0N/A {
0N/A rq.lock();
0N/A try {
0N/A validateContext(sg2d);
0N/A rq.ensureCapacity(24); // so that we have room for at least a span
0N/A int newpos = fillSpans(rq, buf.getAddress(),
0N/A buf.position(), buf.capacity(),
0N/A si, si.getNativeIterator(),
0N/A transx, transy);
0N/A buf.position(newpos);
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A }
0N/A
430N/A public void fillParallelogram(SunGraphics2D sg2d,
4190N/A double ux1, double uy1,
4190N/A double ux2, double uy2,
430N/A double x, double y,
430N/A double dx1, double dy1,
430N/A double dx2, double dy2)
430N/A {
430N/A rq.lock();
430N/A try {
430N/A validateContext(sg2d);
430N/A rq.ensureCapacity(28);
430N/A buf.putInt(FILL_PARALLELOGRAM);
430N/A buf.putFloat((float) x);
430N/A buf.putFloat((float) y);
430N/A buf.putFloat((float) dx1);
430N/A buf.putFloat((float) dy1);
430N/A buf.putFloat((float) dx2);
430N/A buf.putFloat((float) dy2);
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A public void drawParallelogram(SunGraphics2D sg2d,
4190N/A double ux1, double uy1,
4190N/A double ux2, double uy2,
430N/A double x, double y,
430N/A double dx1, double dy1,
430N/A double dx2, double dy2,
430N/A double lw1, double lw2)
430N/A {
430N/A rq.lock();
430N/A try {
430N/A validateContext(sg2d);
430N/A rq.ensureCapacity(36);
430N/A buf.putInt(DRAW_PARALLELOGRAM);
430N/A buf.putFloat((float) x);
430N/A buf.putFloat((float) y);
430N/A buf.putFloat((float) dx1);
430N/A buf.putFloat((float) dy1);
430N/A buf.putFloat((float) dx2);
430N/A buf.putFloat((float) dy2);
430N/A buf.putFloat((float) lw1);
430N/A buf.putFloat((float) lw2);
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A private class AAParallelogramPipe implements ParallelogramPipe {
430N/A public void fillParallelogram(SunGraphics2D sg2d,
4190N/A double ux1, double uy1,
4190N/A double ux2, double uy2,
430N/A double x, double y,
430N/A double dx1, double dy1,
430N/A double dx2, double dy2)
430N/A {
430N/A rq.lock();
430N/A try {
430N/A validateContextAA(sg2d);
430N/A rq.ensureCapacity(28);
430N/A buf.putInt(FILL_AAPARALLELOGRAM);
430N/A buf.putFloat((float) x);
430N/A buf.putFloat((float) y);
430N/A buf.putFloat((float) dx1);
430N/A buf.putFloat((float) dy1);
430N/A buf.putFloat((float) dx2);
430N/A buf.putFloat((float) dy2);
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A public void drawParallelogram(SunGraphics2D sg2d,
4190N/A double ux1, double uy1,
4190N/A double ux2, double uy2,
430N/A double x, double y,
430N/A double dx1, double dy1,
430N/A double dx2, double dy2,
430N/A double lw1, double lw2)
430N/A {
430N/A rq.lock();
430N/A try {
430N/A validateContextAA(sg2d);
430N/A rq.ensureCapacity(36);
430N/A buf.putInt(DRAW_AAPARALLELOGRAM);
430N/A buf.putFloat((float) x);
430N/A buf.putFloat((float) y);
430N/A buf.putFloat((float) dx1);
430N/A buf.putFloat((float) dy1);
430N/A buf.putFloat((float) dx2);
430N/A buf.putFloat((float) dy2);
430N/A buf.putFloat((float) lw1);
430N/A buf.putFloat((float) lw2);
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A }
430N/A
0N/A public void draw(SunGraphics2D sg2d, Shape s) {
0N/A if (sg2d.strokeState == sg2d.STROKE_THIN) {
0N/A if (s instanceof Polygon) {
0N/A if (sg2d.transformState < sg2d.TRANSFORM_TRANSLATESCALE) {
0N/A Polygon p = (Polygon)s;
0N/A drawPolygon(sg2d, p.xpoints, p.ypoints, p.npoints);
0N/A return;
0N/A }
0N/A }
0N/A Path2D.Float p2df;
0N/A int transx, transy;
0N/A if (sg2d.transformState <= sg2d.TRANSFORM_INT_TRANSLATE) {
0N/A if (s instanceof Path2D.Float) {
0N/A p2df = (Path2D.Float)s;
0N/A } else {
0N/A p2df = new Path2D.Float(s);
0N/A }
0N/A transx = sg2d.transX;
0N/A transy = sg2d.transY;
0N/A } else {
0N/A p2df = new Path2D.Float(s, sg2d.transform);
0N/A transx = 0;
0N/A transy = 0;
0N/A }
0N/A drawPath(sg2d, p2df, transx, transy);
0N/A } else if (sg2d.strokeState < sg2d.STROKE_CUSTOM) {
0N/A ShapeSpanIterator si = LoopPipe.getStrokeSpans(sg2d, s);
0N/A try {
0N/A fillSpans(sg2d, si, 0, 0);
0N/A } finally {
0N/A si.dispose();
0N/A }
0N/A } else {
0N/A fill(sg2d, sg2d.stroke.createStrokedShape(s));
0N/A }
0N/A }
0N/A
0N/A public void fill(SunGraphics2D sg2d, Shape s) {
0N/A int transx, transy;
0N/A
0N/A if (sg2d.strokeState == sg2d.STROKE_THIN) {
0N/A // Here we are able to use fillPath() for
0N/A // high-quality fills.
0N/A Path2D.Float p2df;
0N/A if (sg2d.transformState <= sg2d.TRANSFORM_INT_TRANSLATE) {
0N/A if (s instanceof Path2D.Float) {
0N/A p2df = (Path2D.Float)s;
0N/A } else {
0N/A p2df = new Path2D.Float(s);
0N/A }
0N/A transx = sg2d.transX;
0N/A transy = sg2d.transY;
0N/A } else {
0N/A p2df = new Path2D.Float(s, sg2d.transform);
0N/A transx = 0;
0N/A transy = 0;
0N/A }
0N/A fillPath(sg2d, p2df, transx, transy);
0N/A return;
0N/A }
0N/A
0N/A AffineTransform at;
0N/A if (sg2d.transformState <= sg2d.TRANSFORM_INT_TRANSLATE) {
0N/A // Transform (translation) will be done by FillSpans (we could
0N/A // delegate to fillPolygon() here, but most hardware accelerated
0N/A // libraries cannot handle non-convex polygons, so we will use
0N/A // the FillSpans approach by default)
0N/A at = null;
0N/A transx = sg2d.transX;
0N/A transy = sg2d.transY;
0N/A } else {
0N/A // Transform will be done by the PathIterator
0N/A at = sg2d.transform;
0N/A transx = transy = 0;
0N/A }
0N/A
0N/A ShapeSpanIterator ssi = LoopPipe.getFillSSI(sg2d);
0N/A try {
0N/A // Subtract transx/y from the SSI clip to match the
0N/A // (potentially untranslated) geometry fed to it
0N/A Region clip = sg2d.getCompClip();
0N/A ssi.setOutputAreaXYXY(clip.getLoX() - transx,
0N/A clip.getLoY() - transy,
0N/A clip.getHiX() - transx,
0N/A clip.getHiY() - transy);
0N/A ssi.appendPath(s.getPathIterator(at));
0N/A fillSpans(sg2d, ssi, transx, transy);
0N/A } finally {
0N/A ssi.dispose();
0N/A }
0N/A }
0N/A}