Path: blob/master/src/java.desktop/share/native/libawt/java2d/SurfaceData.c
41152 views
/*1* Copyright (c) 1999, 2018, 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#include "SurfaceData.h"2627#include "jni_util.h"28#include "Disposer.h"2930#include "stdlib.h"31#include "string.h"3233/**34* This include file contains information on how to use a SurfaceData35* object from native code.36*/3738static jclass pInvalidPipeClass;39static jclass pNullSurfaceDataClass;40static jfieldID pDataID;41static jfieldID allGrayID;4243jfieldID validID;44GeneralDisposeFunc SurfaceData_DisposeOps;4546#define InitClass(var, env, name) \47do { \48var = (*env)->FindClass(env, name); \49if (var == NULL) { \50return; \51} \52} while (0)5354#define InitField(var, env, jcl, name, type) \55do { \56var = (*env)->GetFieldID(env, jcl, name, type); \57if (var == NULL) { \58return; \59} \60} while (0)6162#define InitGlobalClassRef(var, env, name) \63do { \64jobject jtmp; \65InitClass(jtmp, env, name); \66var = (*env)->NewGlobalRef(env, jtmp); \67if (var == NULL) { \68return; \69} \70} while (0)7172/*73* Class: sun_java2d_SurfaceData74* Method: initIDs75* Signature: ()V76*/77JNIEXPORT void JNICALL78Java_sun_java2d_SurfaceData_initIDs(JNIEnv *env, jclass sd)79{80jclass pICMClass;8182InitGlobalClassRef(pInvalidPipeClass, env,83"sun/java2d/InvalidPipeException");8485InitGlobalClassRef(pNullSurfaceDataClass, env,86"sun/java2d/NullSurfaceData");8788InitField(pDataID, env, sd, "pData", "J");89InitField(validID, env, sd, "valid", "Z");9091InitClass(pICMClass, env, "java/awt/image/IndexColorModel");92InitField(allGrayID, env, pICMClass, "allgrayopaque", "Z");93}9495/*96* Class: sun_java2d_SurfaceData97* Method: isOpaqueGray98* Signature: ()Z99*/100JNIEXPORT jboolean JNICALL101Java_sun_java2d_SurfaceData_isOpaqueGray(JNIEnv *env, jclass sdClass,102jobject icm)103{104if (icm == NULL) {105return JNI_FALSE;106}107return (*env)->GetBooleanField(env, icm, allGrayID);108}109110static SurfaceDataOps *111GetSDOps(JNIEnv *env, jobject sData, jboolean callSetup)112{113SurfaceDataOps *ops;114if (JNU_IsNull(env, sData)) {115JNU_ThrowNullPointerException(env, "surfaceData");116return NULL;117}118ops = (SurfaceDataOps *)JNU_GetLongFieldAsPtr(env, sData, pDataID);119if (ops == NULL) {120if (!(*env)->ExceptionOccurred(env) &&121!(*env)->IsInstanceOf(env, sData, pNullSurfaceDataClass))122{123if (!(*env)->GetBooleanField(env, sData, validID)) {124SurfaceData_ThrowInvalidPipeException(env, "invalid data");125} else {126JNU_ThrowNullPointerException(env, "native ops missing");127}128}129} else if (callSetup) {130SurfaceData_InvokeSetup(env, ops);131}132return ops;133}134135JNIEXPORT SurfaceDataOps * JNICALL136SurfaceData_GetOps(JNIEnv *env, jobject sData)137{138return GetSDOps(env, sData, JNI_TRUE);139}140141JNIEXPORT SurfaceDataOps * JNICALL142SurfaceData_GetOpsNoSetup(JNIEnv *env, jobject sData)143{144return GetSDOps(env, sData, JNI_FALSE);145}146147JNIEXPORT void JNICALL148SurfaceData_SetOps(JNIEnv *env, jobject sData, SurfaceDataOps *ops)149{150if (JNU_GetLongFieldAsPtr(env, sData, pDataID) == NULL) {151JNU_SetLongFieldFromPtr(env, sData, pDataID, ops);152/* Register the data for disposal */153Disposer_AddRecord(env, sData,154SurfaceData_DisposeOps,155ptr_to_jlong(ops));156} else {157JNU_ThrowInternalError(env, "Attempting to set SurfaceData ops twice");158}159}160161JNIEXPORT void JNICALL162SurfaceData_ThrowInvalidPipeException(JNIEnv *env, const char *msg)163{164(*env)->ThrowNew(env, pInvalidPipeClass, msg);165}166167#define GETMIN(v1, v2) (((v1) > (t=(v2))) && ((v1) = t))168#define GETMAX(v1, v2) (((v1) < (t=(v2))) && ((v1) = t))169170JNIEXPORT void JNICALL171SurfaceData_IntersectBounds(SurfaceDataBounds *dst, SurfaceDataBounds *src)172{173int t;174GETMAX(dst->x1, src->x1);175GETMAX(dst->y1, src->y1);176GETMIN(dst->x2, src->x2);177GETMIN(dst->y2, src->y2);178}179180JNIEXPORT void JNICALL181SurfaceData_IntersectBoundsXYXY(SurfaceDataBounds *bounds,182jint x1, jint y1, jint x2, jint y2)183{184int t;185GETMAX(bounds->x1, x1);186GETMAX(bounds->y1, y1);187GETMIN(bounds->x2, x2);188GETMIN(bounds->y2, y2);189}190191JNIEXPORT void JNICALL192SurfaceData_IntersectBoundsXYWH(SurfaceDataBounds *bounds,193jint x, jint y, jint w, jint h)194{195w = (w <= 0) ? x : x+w;196if (w < x) {197w = 0x7fffffff;198}199if (bounds->x1 < x) {200bounds->x1 = x;201}202if (bounds->x2 > w) {203bounds->x2 = w;204}205h = (h <= 0) ? y : y+h;206if (h < y) {207h = 0x7fffffff;208}209if (bounds->y1 < y) {210bounds->y1 = y;211}212if (bounds->y2 > h) {213bounds->y2 = h;214}215}216217JNIEXPORT void JNICALL218SurfaceData_IntersectBlitBounds(SurfaceDataBounds *src,219SurfaceDataBounds *dst,220jint dx, jint dy)221{222int t;223GETMAX(dst->x1, src->x1 + dx);224GETMAX(dst->y1, src->y1 + dy);225GETMIN(dst->x2, src->x2 + dx);226GETMIN(dst->y2, src->y2 + dy);227GETMAX(src->x1, dst->x1 - dx);228GETMAX(src->y1, dst->y1 - dy);229GETMIN(src->x2, dst->x2 - dx);230GETMIN(src->y2, dst->y2 - dy);231}232233JNIEXPORT SurfaceDataOps * JNICALL234SurfaceData_InitOps(JNIEnv *env, jobject sData, int opsSize)235{236SurfaceDataOps *ops = malloc(opsSize);237SurfaceData_SetOps(env, sData, ops);238if (ops != NULL) {239memset(ops, 0, opsSize);240if (!(*env)->ExceptionCheck(env)) {241ops->sdObject = (*env)->NewWeakGlobalRef(env, sData);242}243}244return ops;245}246247void SurfaceData_DisposeOps(JNIEnv *env, jlong ops)248{249if (ops != 0) {250SurfaceDataOps *sdops = (SurfaceDataOps*)jlong_to_ptr(ops);251/* Invoke the ops-specific disposal function */252SurfaceData_InvokeDispose(env, sdops);253(*env)->DeleteWeakGlobalRef(env, sdops->sdObject);254free(sdops);255}256}257258259