Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRDrawLine.java
41159 views
/*1* Copyright (c) 2013, 2014, 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*/2425/**26* Bresenham line-drawing implementation decomposing line segments27* into a series of rectangles.28* This is required, because xrender doesn't support line primitives directly.29* The code here is an almost 1:1 port of the existing C-source contained in30* sun/java2d/loop/DrawLine.c and sun/java2d/loop/LoopMacros.h31*/32package sun.java2d.xr;3334public class XRDrawLine {35static final int BIG_MAX = ((1 << 29) - 1);36static final int BIG_MIN = (-(1 << 29));3738static final int OUTCODE_TOP = 1;39static final int OUTCODE_BOTTOM = 2;40static final int OUTCODE_LEFT = 4;41static final int OUTCODE_RIGHT = 8;4243int x1, y1, x2, y2;44int ucX1, ucY1, ucX2, ucY2;4546DirtyRegion region = new DirtyRegion();4748protected void rasterizeLine(GrowableRectArray rectBuffer, int _x1,49int _y1, int _x2, int _y2, int cxmin, int cymin, int cxmax,50int cymax, boolean clip, boolean overflowCheck) {51float diagF;52int error;53int steps;54int errminor, errmajor;55boolean xmajor;56int dx, dy, ax, ay;5758initCoordinates(_x1, _y1, _x2, _y2, overflowCheck);5960dx = x2 - x1;61dy = y2 - y1;62ax = Math.abs(dx);63ay = Math.abs(dy);64xmajor = (ax >= ay);65diagF = ((float) ax) / ay;6667if (clip68&& !clipCoordinates(cxmin, cymin, cxmax, cymax, xmajor, dx, dy,69ax, ay)) {70// whole line was clipped away71return;72}7374region.setDirtyLineRegion(x1, y1, x2, y2);75int xDiff = region.x2 - region.x;76int yDiff = region.y2 - region.y;7778if (xDiff == 0 || yDiff == 0) {79// horizontal / diagonal lines can be represented by a single80// rectangle81rectBuffer.pushRectValues(region.x, region.y, region.x2 - region.x82+ 1, region.y2 - region.y + 1);83return;84}8586// Setup bresenham87if (xmajor) {88errmajor = ay * 2;89errminor = ax * 2;90ax = -ax; /* For clipping adjustment below */91steps = x2 - x1;92} else {93errmajor = ax * 2;94errminor = ay * 2;95ay = -ay; /* For clipping adjustment below */96steps = y2 - y1;97}9899if ((steps = (Math.abs(steps) + 1)) == 0) {100return;101}102103error = -(errminor / 2);104105if (y1 != ucY1) {106int ysteps = y1 - ucY1;107if (ysteps < 0) {108ysteps = -ysteps;109}110error += ysteps * ax * 2;111}112113if (x1 != ucX1) {114int xsteps = x1 - ucX1;115if (xsteps < 0) {116xsteps = -xsteps;117}118error += xsteps * ay * 2;119}120error += errmajor;121errminor -= errmajor;122123int xStep = (dx > 0 ? 1 : -1);124int yStep = (dy > 0 ? 1 : -1);125int orthogonalXStep = xmajor ? xStep : 0;126int orthogonalYStep = !xmajor ? yStep : 0;127128/*129* For lines which proceed in one direction faster, we try to generate130* rectangles instead of points. Otherwise we try to avoid the extra131* work...132*/133if (diagF <= 0.9 || diagF >= 1.1) {134lineToRects(rectBuffer, steps, error, errmajor, errminor, xStep,135yStep, orthogonalXStep, orthogonalYStep);136} else {137lineToPoints(rectBuffer, steps, error, errmajor, errminor, xStep,138yStep, orthogonalXStep, orthogonalYStep);139}140}141142private void lineToPoints(GrowableRectArray rectBuffer, int steps,143int error, int errmajor, int errminor, int xStep, int yStep,144int orthogonalXStep, int orthogonalYStep) {145int x = x1, y = y1;146147do {148rectBuffer.pushRectValues(x, y, 1, 1);149150// "Traditional" Bresenham line drawing151if (error < 0) {152error += errmajor;153x += orthogonalXStep;154y += orthogonalYStep;155} else {156error -= errminor;157x += xStep;158y += yStep;159}160} while (--steps > 0);161}162163private void lineToRects(GrowableRectArray rectBuffer, int steps,164int error, int errmajor, int errminor, int xStep, int yStep,165int orthogonalXStep, int orthogonalYStep) {166int x = x1, y = y1;167int rectX = Integer.MIN_VALUE, rectY = 0;168int rectW = 0, rectH = 0;169170do {171// Combine the resulting rectangles172// for steps performed in a single direction.173if (y == rectY) {174if (x == (rectX + rectW)) {175rectW++;176} else if (x == (rectX - 1)) {177rectX--;178rectW++;179}180} else if (x == rectX) {181if (y == (rectY + rectH)) {182rectH++;183} else if (y == (rectY - 1)) {184rectY--;185rectH++;186}187} else {188// Diagonal step: add the previous rectangle to the list,189// iff it was "real" (= not initialized before the first190// iteration)191if (rectX != Integer.MIN_VALUE) {192rectBuffer.pushRectValues(rectX, rectY, rectW, rectH);193}194rectX = x;195rectY = y;196rectW = rectH = 1;197}198199// "Traditional" Bresenham line drawing200if (error < 0) {201error += errmajor;202x += orthogonalXStep;203y += orthogonalYStep;204} else {205error -= errminor;206x += xStep;207y += yStep;208}209} while (--steps > 0);210211// Add last rectangle which isn't handled by the combination-code212// anymore213rectBuffer.pushRectValues(rectX, rectY, rectW, rectH);214}215216private boolean clipCoordinates(int cxmin, int cymin, int cxmax, int cymax,217boolean xmajor, int dx, int dy, int ax, int ay) {218int outcode1, outcode2;219220outcode1 = outcode(x1, y1, cxmin, cymin, cxmax, cymax);221outcode2 = outcode(x2, y2, cxmin, cymin, cxmax, cymax);222223while ((outcode1 | outcode2) != 0) {224long xsteps = 0, ysteps = 0;225226if ((outcode1 & outcode2) != 0) {227return false;228}229230if (outcode1 != 0) {231if ((outcode1 & (OUTCODE_TOP | OUTCODE_BOTTOM)) != 0) {232if ((outcode1 & OUTCODE_TOP) != 0) {233y1 = cymin;234} else {235y1 = cymax;236}237ysteps = y1 - ucY1;238if (ysteps < 0) {239ysteps = -ysteps;240}241xsteps = 2 * ysteps * ax + ay;242if (xmajor) {243xsteps += ay - ax - 1;244}245xsteps = xsteps / (2 * ay);246if (dx < 0) {247xsteps = -xsteps;248}249x1 = ucX1 + (int) xsteps;250} else if ((outcode1 & (OUTCODE_LEFT | OUTCODE_RIGHT)) != 0) {251if ((outcode1 & OUTCODE_LEFT) != 0) {252x1 = cxmin;253} else {254x1 = cxmax;255}256xsteps = x1 - ucX1;257if (xsteps < 0) {258xsteps = -xsteps;259}260ysteps = 2 * xsteps * ay + ax;261if (!xmajor) {262ysteps += ax - ay - 1;263}264ysteps = ysteps / (2 * ax);265if (dy < 0) {266ysteps = -ysteps;267}268y1 = ucY1 + (int) ysteps;269}270outcode1 = outcode(x1, y1, cxmin, cymin, cxmax, cymax);271} else {272if ((outcode2 & (OUTCODE_TOP | OUTCODE_BOTTOM)) != 0) {273if ((outcode2 & OUTCODE_TOP) != 0) {274y2 = cymin;275} else {276y2 = cymax;277}278ysteps = y2 - ucY2;279if (ysteps < 0) {280ysteps = -ysteps;281}282xsteps = 2 * ysteps * ax + ay;283if (xmajor) {284xsteps += ay - ax;285} else {286xsteps -= 1;287}288xsteps = xsteps / (2 * ay);289if (dx > 0) {290xsteps = -xsteps;291}292x2 = ucX2 + (int) xsteps;293} else if ((outcode2 & (OUTCODE_LEFT | OUTCODE_RIGHT)) != 0) {294if ((outcode2 & OUTCODE_LEFT) != 0) {295x2 = cxmin;296} else {297x2 = cxmax;298}299xsteps = x2 - ucX2;300if (xsteps < 0) {301xsteps = -xsteps;302}303ysteps = 2 * xsteps * ay + ax;304if (xmajor) {305ysteps -= 1;306} else {307ysteps += ax - ay;308}309ysteps = ysteps / (2 * ax);310if (dy > 0) {311ysteps = -ysteps;312}313y2 = ucY2 + (int) ysteps;314}315outcode2 = outcode(x2, y2, cxmin, cymin, cxmax, cymax);316}317}318319return true;320}321322private void initCoordinates(int x1, int y1, int x2, int y2,323boolean checkOverflow) {324/*325* Part of calculating the Bresenham parameters for line stepping326* involves being able to store numbers that are twice the magnitude of327* the biggest absolute difference in coordinates. Since we want the328* stepping parameters to be stored in jints, we then need to avoid any329* absolute differences more than 30 bits. Thus, we need to preprocess330* the coordinates to reduce their range to 30 bits regardless of331* clipping. We need to cut their range back before we do the clipping332* because the Bresenham stepping values need to be calculated based on333* the "unclipped" coordinates.334*335* Thus, first we perform a "pre-clipping" stage to bring the336* coordinates within the 30-bit range and then we proceed to the337* regular clipping procedure, pretending that these were the original338* coordinates all along. Since this operation occurs based on a339* constant "pre-clip" rectangle of +/- 30 bits without any340* consideration for the final clip, the rounding errors that occur here341* will depend only on the line coordinates and be invariant with342* respect to the particular device/user clip rectangles in effect at343* the time. Thus, rendering a given large-range line will be consistent344* under a variety of clipping conditions.345*/346if (checkOverflow347&& (OverflowsBig(x1) || OverflowsBig(y1) || OverflowsBig(x2) || OverflowsBig(y2))) {348/*349* Use doubles to get us into range for "Big" arithmetic.350*351* The math of adjusting an endpoint for clipping can involve an352* intermediate result with twice the number of bits as the original353* coordinate range. Since we want to maintain as much as 30 bits of354* precision in the resulting coordinates, we will get roundoff here355* even using IEEE double-precision arithmetic which cannot carry 60356* bits of mantissa. Since the rounding errors will be consistent357* for a given set of input coordinates the potential roundoff error358* should not affect the consistency of our rendering.359*/360double x1d = x1;361double y1d = y1;362double x2d = x2;363double y2d = y2;364double dxd = x2d - x1d;365double dyd = y2d - y1d;366367if (x1 < BIG_MIN) {368y1d = y1 + (BIG_MIN - x1) * dyd / dxd;369x1d = BIG_MIN;370} else if (x1 > BIG_MAX) {371y1d = y1 - (x1 - BIG_MAX) * dyd / dxd;372x1d = BIG_MAX;373}374/* Use Y1d instead of _y1 for testing now as we may have modified it */375if (y1d < BIG_MIN) {376x1d = x1 + (BIG_MIN - y1) * dxd / dyd;377y1d = BIG_MIN;378} else if (y1d > BIG_MAX) {379x1d = x1 - (y1 - BIG_MAX) * dxd / dyd;380y1d = BIG_MAX;381}382if (x2 < BIG_MIN) {383y2d = y2 + (BIG_MIN - x2) * dyd / dxd;384x2d = BIG_MIN;385} else if (x2 > BIG_MAX) {386y2d = y2 - (x2 - BIG_MAX) * dyd / dxd;387x2d = BIG_MAX;388}389/* Use Y2d instead of _y2 for testing now as we may have modified it */390if (y2d < BIG_MIN) {391x2d = x2 + (BIG_MIN - y2) * dxd / dyd;392y2d = BIG_MIN;393} else if (y2d > BIG_MAX) {394x2d = x2 - (y2 - BIG_MAX) * dxd / dyd;395y2d = BIG_MAX;396}397398x1 = (int) x1d;399y1 = (int) y1d;400x2 = (int) x2d;401y2 = (int) y2d;402}403404this.x1 = ucX1 = x1;405this.y1 = ucY1 = y1;406this.x2 = ucX2 = x2;407this.y2 = ucY2 = y2;408}409410private boolean OverflowsBig(int v) {411return ((v) != (((v) << 2) >> 2));412}413414private int out(int v, int vmin, int vmax, int cmin, int cmax) {415return ((v < vmin) ? cmin : ((v > vmax) ? cmax : 0));416}417418private int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax) {419return out(y, ymin, ymax, OUTCODE_TOP, OUTCODE_BOTTOM)420| out(x, xmin, xmax, OUTCODE_LEFT, OUTCODE_RIGHT);421}422}423424425