1/*
2 * Copyright (c) 2000, 2001, 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 "GraphicsPrimitiveMgr.h"
27#include "LineUtils.h"
28
29#include "sun_java2d_loops_DrawRect.h"
30
31/*
32 * Class:     sun_java2d_loops_DrawRect
33 * Method:    DrawRect
34 * Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;IIII)V
35 */
36JNIEXPORT void JNICALL
37Java_sun_java2d_loops_DrawRect_DrawRect
38    (JNIEnv *env, jobject self,
39     jobject sg2d, jobject sData,
40     jint x, jint y, jint w, jint h)
41{
42    SurfaceDataOps *sdOps;
43    SurfaceDataRasInfo rasInfo;
44    NativePrimitive *pPrim;
45    CompositeInfo compInfo;
46    jint lox, loy, hix, hiy;
47    jint pixel = GrPrim_Sg2dGetPixel(env, sg2d);
48
49    if (w < 0 || h < 0) {
50        return;
51    }
52
53    pPrim = GetNativePrim(env, self);
54    if (pPrim == NULL) {
55        return;
56    }
57    if (pPrim->pCompType->getCompInfo != NULL) {
58        GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);
59    }
60
61    sdOps = SurfaceData_GetOps(env, sData);
62    if (sdOps == 0) {
63        return;
64    }
65
66    lox = x;
67    loy = y;
68    hix = x + w + 1;
69    hiy = y + h + 1;
70    if (hix < lox) {
71        hix = 0x7fffffff;
72    }
73    if (hiy < loy) {
74        hiy = 0x7fffffff;
75    }
76
77    GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);
78    if (rasInfo.bounds.x1 < lox) rasInfo.bounds.x1 = lox;
79    if (rasInfo.bounds.y1 < loy) rasInfo.bounds.y1 = loy;
80    if (rasInfo.bounds.x2 > hix) rasInfo.bounds.x2 = hix;
81    if (rasInfo.bounds.y2 > hiy) rasInfo.bounds.y2 = hiy;
82    if (sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags) != SD_SUCCESS) {
83        return;
84    }
85
86    if (rasInfo.bounds.x2 > rasInfo.bounds.x1 &&
87        rasInfo.bounds.y2 > rasInfo.bounds.y1)
88    {
89        sdOps->GetRasInfo(env, sdOps, &rasInfo);
90        if (rasInfo.rasBase) {
91            DrawLineFunc *pLine = pPrim->funcs.drawline;
92            int loyin = (loy == rasInfo.bounds.y1);
93            int hiyin = (hiy == rasInfo.bounds.y2);
94            int xsize = (rasInfo.bounds.x2 - rasInfo.bounds.x1);
95            int ysize = (rasInfo.bounds.y2 - rasInfo.bounds.y1 - loyin - hiyin);
96            /*
97             * To avoid drawing the corners twice (both for performance
98             * and because XOR erases them otherwise) and to maximize the
99             * number of pixels we draw in the horizontal portions
100             * which are more cache-friendly, we include the corner
101             * pixels only in the top and bottom segments.
102             * We also protect against degenerate rectangles where we
103             * would draw the same line for top & bottom or left & right.
104             */
105            if (loyin) {
106                /* Line across the top */
107                (*pLine)(&rasInfo,
108                         rasInfo.bounds.x1, rasInfo.bounds.y1,
109                         pixel, xsize, 0,
110                         BUMP_POS_PIXEL, 0, BUMP_NOOP, 0, pPrim, &compInfo);
111            }
112            if (lox == rasInfo.bounds.x1 && ysize > 0) {
113                /* Line down the left side */
114                (*pLine)(&rasInfo,
115                         rasInfo.bounds.x1, rasInfo.bounds.y1 + loyin,
116                         pixel, ysize, 0,
117                         BUMP_POS_SCAN, 0, BUMP_NOOP, 0, pPrim, &compInfo);
118            }
119            if (hix == rasInfo.bounds.x2 && ysize > 0 && lox != hix - 1) {
120                /* Line down the right side */
121                (*pLine)(&rasInfo,
122                         rasInfo.bounds.x2 - 1, rasInfo.bounds.y1 + loyin,
123                         pixel, ysize, 0,
124                         BUMP_POS_SCAN, 0, BUMP_NOOP, 0, pPrim, &compInfo);
125            }
126            if (hiyin && loy != hiy - 1) {
127                /* Line across the bottom */
128                (*pLine)(&rasInfo,
129                         rasInfo.bounds.x1, rasInfo.bounds.y2 - 1,
130                         pixel, xsize, 0,
131                         BUMP_POS_PIXEL, 0, BUMP_NOOP, 0, pPrim, &compInfo);
132            }
133        }
134        SurfaceData_InvokeRelease(env, sdOps, &rasInfo);
135    }
136    SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
137}
138