1 /*
   2  * Copyright (c) 2008, 2010, 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 #include "math.h"
  27 #include "GraphicsPrimitiveMgr.h"
  28 #include "ParallelogramUtils.h"
  29 
  30 #include "sun_java2d_loops_FillParallelogram.h"
  31 
  32 /*
  33  * Class:     sun_java2d_loops_FillParallelogram
  34  * Method:    FillParallelogram
  35  * Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;DDDDDD)V
  36  */
  37 JNIEXPORT void JNICALL
  38 Java_sun_java2d_loops_FillParallelogram_FillParallelogram
  39     (JNIEnv *env, jobject self,
  40      jobject sg2d, jobject sData,
  41      jdouble x0, jdouble y0,
  42      jdouble dx1, jdouble dy1,
  43      jdouble dx2, jdouble dy2)
  44 {
  45     SurfaceDataOps *sdOps;
  46     SurfaceDataRasInfo rasInfo;
  47     NativePrimitive *pPrim;
  48     CompositeInfo compInfo;
  49     jint pixel;
  50     jint ix1, iy1, ix2, iy2;
  51     if ((dy1 == 0 && dx1 == 0) || (dy2 == 0 && dx2 == 0)) {
  52         return;
  53     }
  54 
  55     /*
  56      * Sort parallelogram by y values, ensure that each delta vector
  57      * has a non-negative y delta.
  58      */
  59     SORT_PGRAM(x0, y0, dx1, dy1, dx2, dy2, );
  60 
  61     PGRAM_MIN_MAX(ix1, ix2, x0, dx1, dx2, JNI_FALSE);
  62     iy1 = (jint) floor(y0 + 0.5);
  63     iy2 = (jint) floor(y0 + dy1 + dy2 + 0.5);
  64 
  65     pPrim = GetNativePrim(env, self);
  66     if (pPrim == NULL) {
  67         return;
  68     }
  69     pixel = GrPrim_Sg2dGetPixel(env, sg2d);
  70     if (pPrim->pCompType->getCompInfo != NULL) {
  71         GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);
  72     }
  73 
  74     sdOps = SurfaceData_GetOps(env, sData);
  75     if (sdOps == NULL) {
  76         return;
  77     }
  78 
  79     GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);
  80     SurfaceData_IntersectBoundsXYXY(&rasInfo.bounds, ix1, iy1, ix2, iy2);
  81     if (rasInfo.bounds.y2 <= rasInfo.bounds.y1 ||
  82         rasInfo.bounds.x2 <= rasInfo.bounds.x1)
  83     {
  84         return;
  85     }
  86 
  87     if (sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags) != SD_SUCCESS) {
  88         return;
  89     }
  90 
  91     ix1 = rasInfo.bounds.x1;
  92     iy1 = rasInfo.bounds.y1;
  93     ix2 = rasInfo.bounds.x2;
  94     iy2 = rasInfo.bounds.y2;
  95     if (ix2 > ix1 && iy2 > iy1) {
  96         sdOps->GetRasInfo(env, sdOps, &rasInfo);
  97         if (rasInfo.rasBase) {
  98             jdouble lslope = (dy1 == 0) ? 0 : dx1 / dy1;
  99             jdouble rslope = (dy2 == 0) ? 0 : dx2 / dy2;
 100             jlong ldx = DblToLong(lslope);
 101             jlong rdx = DblToLong(rslope);
 102             jint cy1, cy2, loy, hiy;
 103             dx1 += x0;
 104             dy1 += y0;
 105             dx2 += x0;
 106             dy2 += y0;
 107             cy1 = (jint) floor(dy1 + 0.5);
 108             cy2 = (jint) floor(dy2 + 0.5);
 109 
 110             /* Top triangular portion. */
 111             loy = iy1;
 112             hiy = (cy1 < cy2) ? cy1 : cy2;
 113             if (hiy > iy2) hiy = iy2;
 114             if (loy < hiy) {
 115                 jlong lx = PGRAM_INIT_X(loy, x0, y0, lslope);
 116                 jlong rx = PGRAM_INIT_X(loy, x0, y0, rslope);
 117                 (*pPrim->funcs.fillparallelogram)(&rasInfo,
 118                                                   ix1, loy, ix2, hiy,
 119                                                   lx, ldx, rx, rdx,
 120                                                   pixel, pPrim, &compInfo);
 121             }
 122 
 123             /* Middle parallelogram portion, which way does it slant? */
 124             if (cy1 < cy2) {
 125                 /* Middle parallelogram portion, slanted to right. */
 126                 /* left leg turned a corner at y0+dy1 */
 127                 /* right leg continuing on its initial trajectory from y0 */
 128                 loy = cy1;
 129                 hiy = cy2;
 130                 if (loy < iy1) loy = iy1;
 131                 if (hiy > iy2) hiy = iy2;
 132                 if (loy < hiy) {
 133                     jlong lx = PGRAM_INIT_X(loy, dx1, dy1, rslope);
 134                     jlong rx = PGRAM_INIT_X(loy,  x0,  y0, rslope);
 135                     (*pPrim->funcs.fillparallelogram)(&rasInfo,
 136                                                       ix1, loy, ix2, hiy,
 137                                                       lx, rdx, rx, rdx,
 138                                                       pixel, pPrim, &compInfo);
 139                 }
 140             } else if (cy2 < cy1) {
 141                 /* Middle parallelogram portion, slanted to left. */
 142                 /* left leg continuing on its initial trajectory from y0 */
 143                 /* right leg turned a corner at y0+dy2 */
 144                 loy = cy2;
 145                 hiy = cy1;
 146                 if (loy < iy1) loy = iy1;
 147                 if (hiy > iy2) hiy = iy2;
 148                 if (loy < hiy) {
 149                     jlong lx = PGRAM_INIT_X(loy,  x0,  y0, lslope);
 150                     jlong rx = PGRAM_INIT_X(loy, dx2, dy2, lslope);
 151                     (*pPrim->funcs.fillparallelogram)(&rasInfo,
 152                                                       ix1, loy, ix2, hiy,
 153                                                       lx, ldx, rx, ldx,
 154                                                       pixel, pPrim, &compInfo);
 155                 }
 156             }
 157 
 158             /* Bottom triangular portion. */
 159             loy = (cy1 > cy2) ? cy1 : cy2;
 160             if (loy < iy1) loy = iy1;
 161             hiy = iy2;
 162             if (loy < hiy) {
 163                 /* left leg turned its corner at y0+dy1, now moving right */
 164                 /* right leg turned its corner at y0+dy2, now moving left */
 165                 jlong lx = PGRAM_INIT_X(loy, dx1, dy1, rslope);
 166                 jlong rx = PGRAM_INIT_X(loy, dx2, dy2, lslope);
 167                 (*pPrim->funcs.fillparallelogram)(&rasInfo,
 168                                                   ix1, loy, ix2, hiy,
 169                                                   lx, rdx, rx, ldx,
 170                                                   pixel, pPrim, &compInfo);
 171             }
 172         }
 173         SurfaceData_InvokeRelease(env, sdOps, &rasInfo);
 174     }
 175     SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
 176 }