0N/A/*
2362N/A * Copyright (c) 2002, 2007, 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/A#include <stdlib.h>
0N/A
0N/A#include "jni_util.h"
0N/A
0N/A#include "Region.h"
6295N/A#include "sizecalc.h"
0N/A
0N/Astatic jfieldID endIndexID;
0N/Astatic jfieldID bandsID;
0N/Astatic jfieldID loxID;
0N/Astatic jfieldID loyID;
0N/Astatic jfieldID hixID;
0N/Astatic jfieldID hiyID;
0N/A
0N/A#define InitField(var, env, jcl, name, type) \
0N/Ado { \
0N/A var = (*env)->GetFieldID(env, jcl, name, type); \
0N/A if (var == NULL) { \
0N/A return; \
0N/A } \
0N/A} while (0)
0N/A
0N/A/*
0N/A * Class: sun_java2d_pipe_Region
0N/A * Method: initIDs
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_java2d_pipe_Region_initIDs(JNIEnv *env, jclass reg)
0N/A{
0N/A InitField(endIndexID, env, reg, "endIndex", "I");
0N/A InitField(bandsID, env, reg, "bands", "[I");
0N/A
0N/A InitField(loxID, env, reg, "lox", "I");
0N/A InitField(loyID, env, reg, "loy", "I");
0N/A InitField(hixID, env, reg, "hix", "I");
0N/A InitField(hiyID, env, reg, "hiy", "I");
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/ARegion_GetInfo(JNIEnv *env, jobject region, RegionData *pRgnInfo)
0N/A{
0N/A if (JNU_IsNull(env, region)) {
0N/A pRgnInfo->bounds.x1 = pRgnInfo->bounds.y1 = 0x80000000;
0N/A pRgnInfo->bounds.x2 = pRgnInfo->bounds.y2 = 0x7fffffff;
0N/A pRgnInfo->endIndex = 0;
0N/A } else {
0N/A pRgnInfo->bounds.x1 = (*env)->GetIntField(env, region, loxID);
0N/A pRgnInfo->bounds.y1 = (*env)->GetIntField(env, region, loyID);
0N/A pRgnInfo->bounds.x2 = (*env)->GetIntField(env, region, hixID);
0N/A pRgnInfo->bounds.y2 = (*env)->GetIntField(env, region, hiyID);
0N/A pRgnInfo->endIndex = (*env)->GetIntField(env, region, endIndexID);
0N/A }
0N/A pRgnInfo->bands = (Region_IsRectangular(pRgnInfo)
0N/A ? NULL
0N/A : (*env)->GetObjectField(env, region, bandsID));
0N/A return 0;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/ARegion_GetBounds(JNIEnv *env, jobject region, SurfaceDataBounds *b)
0N/A{
0N/A if (JNU_IsNull(env, region)) {
0N/A b->x1 = b->y1 = 0x80000000;
0N/A b->x2 = b->y2 = 0x7fffffff;
0N/A } else {
0N/A b->x1 = (*env)->GetIntField(env, region, loxID);
0N/A b->y1 = (*env)->GetIntField(env, region, loyID);
0N/A b->x2 = (*env)->GetIntField(env, region, hixID);
0N/A b->y2 = (*env)->GetIntField(env, region, hiyID);
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/ARegion_StartIteration(JNIEnv *env, RegionData *pRgnInfo)
0N/A{
0N/A pRgnInfo->pBands =
0N/A (Region_IsRectangular(pRgnInfo)
0N/A ? NULL
0N/A : (*env)->GetPrimitiveArrayCritical(env, pRgnInfo->bands, 0));
0N/A pRgnInfo->index = 0;
0N/A pRgnInfo->numrects = 0;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/ARegion_CountIterationRects(RegionData *pRgnInfo)
0N/A{
0N/A jint totalrects;
0N/A if (Region_IsEmpty(pRgnInfo)) {
0N/A totalrects = 0;
0N/A } else if (Region_IsRectangular(pRgnInfo)) {
0N/A totalrects = 1;
0N/A } else {
0N/A jint *pBands = pRgnInfo->pBands;
0N/A int index = 0;
0N/A totalrects = 0;
0N/A while (index < pRgnInfo->endIndex) {
0N/A jint xy1 = pBands[index++];
0N/A jint xy2 = pBands[index++];
0N/A jint numrects = pBands[index++];
0N/A if (xy1 >= pRgnInfo->bounds.y2) {
0N/A break;
0N/A }
0N/A if (xy2 > pRgnInfo->bounds.y1) {
0N/A while (numrects > 0) {
0N/A xy1 = pBands[index++];
0N/A xy2 = pBands[index++];
0N/A numrects--;
0N/A if (xy1 >= pRgnInfo->bounds.x2) {
0N/A break;
0N/A }
0N/A if (xy2 > pRgnInfo->bounds.x1) {
0N/A totalrects++;
0N/A }
0N/A }
0N/A }
0N/A index += numrects * 2;
0N/A }
0N/A }
0N/A return totalrects;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/ARegion_NextIteration(RegionData *pRgnInfo, SurfaceDataBounds *pSpan)
0N/A{
0N/A jint index = pRgnInfo->index;
0N/A if (Region_IsRectangular(pRgnInfo)) {
0N/A if (index > 0 || Region_IsEmpty(pRgnInfo)) {
0N/A return 0;
0N/A }
0N/A pSpan->x1 = pRgnInfo->bounds.x1;
0N/A pSpan->x2 = pRgnInfo->bounds.x2;
0N/A pSpan->y1 = pRgnInfo->bounds.y1;
0N/A pSpan->y2 = pRgnInfo->bounds.y2;
0N/A index = 1;
0N/A } else {
0N/A jint *pBands = pRgnInfo->pBands;
0N/A jint xy1, xy2;
0N/A jint numrects = pRgnInfo->numrects;
0N/A while (JNI_TRUE) {
0N/A if (numrects <= 0) {
0N/A if (index >= pRgnInfo->endIndex) {
0N/A return 0;
0N/A }
0N/A xy1 = pBands[index++];
0N/A if (xy1 >= pRgnInfo->bounds.y2) {
0N/A return 0;
0N/A }
0N/A if (xy1 < pRgnInfo->bounds.y1) {
0N/A xy1 = pRgnInfo->bounds.y1;
0N/A }
0N/A xy2 = pBands[index++];
0N/A numrects = pBands[index++];
0N/A if (xy2 > pRgnInfo->bounds.y2) {
0N/A xy2 = pRgnInfo->bounds.y2;
0N/A }
0N/A if (xy2 <= xy1) {
0N/A index += numrects * 2;
0N/A numrects = 0;
0N/A continue;
0N/A }
0N/A pSpan->y1 = xy1;
0N/A pSpan->y2 = xy2;
0N/A }
0N/A xy1 = pBands[index++];
0N/A xy2 = pBands[index++];
0N/A numrects--;
0N/A if (xy1 >= pRgnInfo->bounds.x2) {
0N/A index += numrects * 2;
0N/A numrects = 0;
0N/A continue;
0N/A }
0N/A if (xy1 < pRgnInfo->bounds.x1) {
0N/A xy1 = pRgnInfo->bounds.x1;
0N/A }
0N/A if (xy2 > pRgnInfo->bounds.x2) {
0N/A xy2 = pRgnInfo->bounds.x2;
0N/A }
0N/A if (xy2 > xy1) {
0N/A pSpan->x1 = xy1;
0N/A pSpan->x2 = xy2;
0N/A break;
0N/A }
0N/A }
0N/A pRgnInfo->numrects = numrects;
0N/A }
0N/A pRgnInfo->index = index;
0N/A return 1;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/ARegion_EndIteration(JNIEnv *env, RegionData *pRgnInfo)
0N/A{
0N/A if (pRgnInfo->endIndex != 0) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, pRgnInfo->bands,
0N/A pRgnInfo->pBands, JNI_ABORT);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * The code was extracted from
0N/A * src/solaris/native/sun/java2d/x11/X11SurfaceData.c
0N/A * XSetClip() method.
0N/A *
0N/A * If the region is null, the shape is considered to be
0N/A * a rectangle (x1, y1, x2-x1, y2-y1).
0N/A *
0N/A * The *pRect must point to a buffer of initialBufferSize
0N/A * rectangles. If there're more than initialBufferSize
0N/A * rectangles in the region, the buffer is reallocated
0N/A * and its pointer is being stored at the *pRect. Using
0N/A * this practice we may use a small local (on the stack)
0N/A * buffer and avoid allocating/freeing a memory if we
0N/A * operate simple regions.
0N/A */
0N/AJNIEXPORT int JNICALL
0N/ARegionToYXBandedRectangles(JNIEnv *env,
0N/A jint x1, jint y1, jint x2, jint y2, jobject region,
0N/A RECT_T ** pRect, unsigned int initialBufferSize)
0N/A{
0N/A RegionData clipInfo;
0N/A SurfaceDataBounds span;
0N/A int i, numrects;
0N/A
0N/A if (region == NULL) {
0N/A if (x2 <= x1 || y2 <= y1) {
0N/A /* empty clip, disable rendering */
0N/A numrects = 0;
0N/A } else {
0N/A RECT_SET(**pRect, x1, y1, x2 - x1, y2 - y1);
0N/A numrects = 1;
0N/A }
0N/A } else {
0N/A if (Region_GetInfo(env, region, &clipInfo)) {
0N/A /* return; REMIND: What to do here? */
0N/A }
0N/A Region_StartIteration(env, &clipInfo);
0N/A numrects = Region_CountIterationRects(&clipInfo);
6295N/A if ((unsigned long)numrects > initialBufferSize) {
6295N/A *pRect = (RECT_T *) SAFE_SIZE_ARRAY_ALLOC(malloc, numrects, sizeof(RECT_T));
0N/A if (*pRect == NULL) {
0N/A Region_EndIteration(env, &clipInfo);
0N/A JNU_ThrowOutOfMemoryError(env,
0N/A "Can't allocate shape region memory");
0N/A return 0;
0N/A }
0N/A }
0N/A for (i = 0; Region_NextIteration(&clipInfo, &span); i++) {
0N/A RECT_SET((*pRect)[i], span.x1, span.y1, span.x2 - span.x1, span.y2 - span.y1);
0N/A }
0N/A Region_EndIteration(env, &clipInfo);
0N/A }
0N/A
0N/A return numrects;
0N/A}