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