annotate src/share/native/sun/awt/image/BufImgSurfaceData.c @ 2516:e03065fc64e7

6959998: Return of SurfaceData_InitOps point not checked in all cases (parfait found these) Reviewed-by: prr Contributed-by: ohair <kelly.ohair@oracle.com>
author igor
date Mon, 12 Jul 2010 13:16:28 -0700
parents 00cd9dc3c2b5
children 33cc629889bd
rev   line source
duke@0 1 /*
ohair@2362 2 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 #include "BufImgSurfaceData.h"
duke@0 27 #include "malloc.h"
duke@0 28
duke@0 29 #include "sun_awt_image_BufImgSurfaceData.h"
duke@0 30
duke@0 31 #include "img_util_md.h"
duke@0 32 #include "jni_util.h"
duke@0 33 /* Define uintptr_t */
duke@0 34 #include "gdefs.h"
duke@0 35
duke@0 36 /**
duke@0 37 * This include file contains support code for loops using the
duke@0 38 * SurfaceData interface to talk to an X11 drawable from native
duke@0 39 * code.
duke@0 40 */
duke@0 41
duke@0 42 static LockFunc BufImg_Lock;
duke@0 43 static GetRasInfoFunc BufImg_GetRasInfo;
duke@0 44 static ReleaseFunc BufImg_Release;
duke@0 45 static DisposeFunc BufImg_Dispose;
duke@0 46
duke@0 47 static ColorData *BufImg_SetupICM(JNIEnv *env, BufImgSDOps *bisdo);
duke@0 48
duke@0 49 static jfieldID rgbID;
duke@0 50 static jfieldID mapSizeID;
duke@0 51 static jfieldID CMpDataID;
duke@0 52 static jfieldID allGrayID;
duke@0 53
duke@0 54 /*
duke@0 55 * Class: sun_awt_image_BufImgSurfaceData
duke@0 56 * Method: initIDs
duke@0 57 * Signature: ()V
duke@0 58 */
duke@0 59 JNIEXPORT void JNICALL
duke@0 60 Java_sun_awt_image_BufImgSurfaceData_initIDs
duke@0 61 (JNIEnv *env, jclass bisd, jclass icm)
duke@0 62 {
duke@0 63 if (sizeof(BufImgRIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
duke@0 64 JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
duke@0 65 return;
duke@0 66 }
duke@0 67
duke@0 68 rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I");
duke@0 69 allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z");
duke@0 70 mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I");
duke@0 71 CMpDataID = (*env)->GetFieldID(env, icm, "pData", "J");
duke@0 72 if (allGrayID == 0 || rgbID == 0 || mapSizeID == 0 || CMpDataID == 0) {
duke@0 73 JNU_ThrowInternalError(env, "Could not get field IDs");
duke@0 74 }
duke@0 75 }
duke@0 76
duke@0 77 /*
duke@0 78 * Class: sun_java2d_SurfaceData
duke@0 79 * Method: freeNativeICMData
duke@0 80 * Signature: (Ljava/awt/image/IndexColorModel;)V
duke@0 81 */
duke@0 82 JNIEXPORT void JNICALL
duke@0 83 Java_sun_awt_image_BufImgSurfaceData_freeNativeICMData
duke@0 84 (JNIEnv *env, jclass sd, jobject icm)
duke@0 85 {
duke@0 86 jlong pData;
duke@0 87 ColorData *cdata;
duke@0 88
duke@0 89 if (JNU_IsNull(env, icm)) {
duke@0 90 JNU_ThrowNullPointerException(env, "IndexColorModel cannot be null");
duke@0 91 return;
duke@0 92 }
duke@0 93
duke@0 94 pData = (*env)->GetLongField (env, icm, CMpDataID);
duke@0 95 cdata = (ColorData *)pData;
duke@0 96 freeICMColorData(cdata);
duke@0 97 }
duke@0 98
duke@0 99 /*
duke@0 100 * Class: sun_awt_image_BufImgSurfaceData
duke@0 101 * Method: initOps
duke@0 102 * Signature: (Ljava/lang/Object;IIIII)V
duke@0 103 */
duke@0 104 JNIEXPORT void JNICALL
duke@0 105 Java_sun_awt_image_BufImgSurfaceData_initRaster(JNIEnv *env, jobject bisd,
duke@0 106 jobject array,
duke@0 107 jint offset, jint bitoffset,
duke@0 108 jint width, jint height,
duke@0 109 jint pixStr, jint scanStr,
duke@0 110 jobject icm)
duke@0 111 {
duke@0 112 BufImgSDOps *bisdo =
duke@0 113 (BufImgSDOps*)SurfaceData_InitOps(env, bisd, sizeof(BufImgSDOps));
igor@2516 114 if (bisdo == NULL) {
igor@2516 115 JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
igor@2516 116 return;
igor@2516 117 }
duke@0 118 bisdo->sdOps.Lock = BufImg_Lock;
duke@0 119 bisdo->sdOps.GetRasInfo = BufImg_GetRasInfo;
duke@0 120 bisdo->sdOps.Release = BufImg_Release;
duke@0 121 bisdo->sdOps.Unlock = NULL;
duke@0 122 bisdo->sdOps.Dispose = BufImg_Dispose;
duke@0 123 bisdo->array = (*env)->NewWeakGlobalRef(env, array);
duke@0 124 bisdo->offset = offset;
duke@0 125 bisdo->bitoffset = bitoffset;
duke@0 126 bisdo->scanStr = scanStr;
duke@0 127 bisdo->pixStr = pixStr;
duke@0 128 if (JNU_IsNull(env, icm)) {
duke@0 129 bisdo->lutarray = NULL;
duke@0 130 bisdo->lutsize = 0;
duke@0 131 bisdo->icm = NULL;
duke@0 132 } else {
duke@0 133 jobject lutarray = (*env)->GetObjectField(env, icm, rgbID);
duke@0 134 bisdo->lutarray = (*env)->NewWeakGlobalRef(env, lutarray);
duke@0 135 bisdo->lutsize = (*env)->GetIntField(env, icm, mapSizeID);
duke@0 136 bisdo->icm = (*env)->NewWeakGlobalRef(env, icm);
duke@0 137 }
duke@0 138 bisdo->rasbounds.x1 = 0;
duke@0 139 bisdo->rasbounds.y1 = 0;
duke@0 140 bisdo->rasbounds.x2 = width;
duke@0 141 bisdo->rasbounds.y2 = height;
duke@0 142 }
duke@0 143
duke@0 144 /*
duke@0 145 * Method for disposing native BufImgSD
duke@0 146 */
duke@0 147 static void BufImg_Dispose(JNIEnv *env, SurfaceDataOps *ops)
duke@0 148 {
duke@0 149 /* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
duke@0 150 BufImgSDOps *bisdo = (BufImgSDOps *)ops;
duke@0 151 (*env)->DeleteWeakGlobalRef(env, bisdo->array);
duke@0 152 if (bisdo->lutarray != NULL) {
duke@0 153 (*env)->DeleteWeakGlobalRef(env, bisdo->lutarray);
duke@0 154 }
duke@0 155 if (bisdo->icm != NULL) {
duke@0 156 (*env)->DeleteWeakGlobalRef(env, bisdo->icm);
duke@0 157 }
duke@0 158 }
duke@0 159
duke@0 160 static jint BufImg_Lock(JNIEnv *env,
duke@0 161 SurfaceDataOps *ops,
duke@0 162 SurfaceDataRasInfo *pRasInfo,
duke@0 163 jint lockflags)
duke@0 164 {
duke@0 165 BufImgSDOps *bisdo = (BufImgSDOps *)ops;
duke@0 166 BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
duke@0 167
duke@0 168 if ((lockflags & (SD_LOCK_LUT)) != 0 && JNU_IsNull(env, bisdo->lutarray)) {
duke@0 169 /* REMIND: Should this be an InvalidPipe exception? */
duke@0 170 JNU_ThrowNullPointerException(env, "Attempt to lock missing colormap");
duke@0 171 return SD_FAILURE;
duke@0 172 }
duke@0 173 if ((lockflags & SD_LOCK_INVCOLOR) != 0 ||
duke@0 174 (lockflags & SD_LOCK_INVGRAY) != 0)
duke@0 175 {
duke@0 176 bipriv->cData = BufImg_SetupICM(env, bisdo);
duke@0 177 if (bipriv->cData == NULL) {
duke@0 178 JNU_ThrowNullPointerException(env, "Could not initialize "
duke@0 179 "inverse tables");
duke@0 180 return SD_FAILURE;
duke@0 181 }
duke@0 182 } else {
duke@0 183 bipriv->cData = NULL;
duke@0 184 }
duke@0 185
duke@0 186 bipriv->lockFlags = lockflags;
duke@0 187 bipriv->base = NULL;
duke@0 188 bipriv->lutbase = NULL;
duke@0 189
duke@0 190 SurfaceData_IntersectBounds(&pRasInfo->bounds, &bisdo->rasbounds);
duke@0 191
duke@0 192 return SD_SUCCESS;
duke@0 193 }
duke@0 194
duke@0 195 static void BufImg_GetRasInfo(JNIEnv *env,
duke@0 196 SurfaceDataOps *ops,
duke@0 197 SurfaceDataRasInfo *pRasInfo)
duke@0 198 {
duke@0 199 BufImgSDOps *bisdo = (BufImgSDOps *)ops;
duke@0 200 BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
duke@0 201
duke@0 202 if ((bipriv->lockFlags & (SD_LOCK_RD_WR)) != 0) {
duke@0 203 bipriv->base =
duke@0 204 (*env)->GetPrimitiveArrayCritical(env, bisdo->array, NULL);
duke@0 205 }
duke@0 206 if ((bipriv->lockFlags & (SD_LOCK_LUT)) != 0) {
duke@0 207 bipriv->lutbase =
duke@0 208 (*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL);
duke@0 209 }
duke@0 210
duke@0 211 if (bipriv->base == NULL) {
duke@0 212 pRasInfo->rasBase = NULL;
duke@0 213 pRasInfo->pixelStride = 0;
duke@0 214 pRasInfo->pixelBitOffset = 0;
duke@0 215 pRasInfo->scanStride = 0;
duke@0 216 } else {
duke@0 217 pRasInfo->rasBase = (void *)
duke@0 218 (((uintptr_t) bipriv->base) + bisdo->offset);
duke@0 219 pRasInfo->pixelStride = bisdo->pixStr;
duke@0 220 pRasInfo->pixelBitOffset = bisdo->bitoffset;
duke@0 221 pRasInfo->scanStride = bisdo->scanStr;
duke@0 222 }
duke@0 223 if (bipriv->lutbase == NULL) {
duke@0 224 pRasInfo->lutBase = NULL;
duke@0 225 pRasInfo->lutSize = 0;
duke@0 226 } else {
duke@0 227 pRasInfo->lutBase = bipriv->lutbase;
duke@0 228 pRasInfo->lutSize = bisdo->lutsize;
duke@0 229 }
duke@0 230 if (bipriv->cData == NULL) {
duke@0 231 pRasInfo->invColorTable = NULL;
duke@0 232 pRasInfo->redErrTable = NULL;
duke@0 233 pRasInfo->grnErrTable = NULL;
duke@0 234 pRasInfo->bluErrTable = NULL;
duke@0 235 } else {
duke@0 236 pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;
duke@0 237 pRasInfo->redErrTable = bipriv->cData->img_oda_red;
duke@0 238 pRasInfo->grnErrTable = bipriv->cData->img_oda_green;
duke@0 239 pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;
duke@0 240 pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;
duke@0 241 }
duke@0 242 }
duke@0 243
duke@0 244 static void BufImg_Release(JNIEnv *env,
duke@0 245 SurfaceDataOps *ops,
duke@0 246 SurfaceDataRasInfo *pRasInfo)
duke@0 247 {
duke@0 248 BufImgSDOps *bisdo = (BufImgSDOps *)ops;
duke@0 249 BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
duke@0 250
duke@0 251 if (bipriv->base != NULL) {
duke@0 252 jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0)
duke@0 253 ? 0 : JNI_ABORT);
duke@0 254 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->array,
duke@0 255 bipriv->base, mode);
duke@0 256 }
duke@0 257 if (bipriv->lutbase != NULL) {
duke@0 258 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,
duke@0 259 bipriv->lutbase, JNI_ABORT);
duke@0 260 }
duke@0 261 }
duke@0 262
duke@0 263 static ColorData *BufImg_SetupICM(JNIEnv *env,
duke@0 264 BufImgSDOps *bisdo)
duke@0 265 {
duke@0 266 ColorData *cData;
duke@0 267
duke@0 268 if (JNU_IsNull(env, bisdo->icm)) {
duke@0 269 return (ColorData *) NULL;
duke@0 270 }
duke@0 271
duke@0 272 cData = (ColorData *) JNU_GetLongFieldAsPtr(env, bisdo->icm, CMpDataID);
duke@0 273
duke@0 274 if (cData == NULL) {
duke@0 275 cData = (ColorData*)calloc(1, sizeof(ColorData));
duke@0 276
duke@0 277 if (cData != NULL) {
duke@0 278 jboolean allGray
duke@0 279 = (*env)->GetBooleanField(env, bisdo->icm, allGrayID);
duke@0 280 int *pRgb = (int *)
duke@0 281 ((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL));
duke@0 282 cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);
duke@0 283 if (allGray == JNI_TRUE) {
duke@0 284 initInverseGrayLut(pRgb, bisdo->lutsize, cData);
duke@0 285 }
duke@0 286 (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,
duke@0 287 JNI_ABORT);
duke@0 288
duke@0 289 initDitherTables(cData);
duke@0 290
duke@0 291 JNU_SetLongFieldFromPtr(env, bisdo->icm, CMpDataID, cData);
duke@0 292 }
duke@0 293 }
duke@0 294
duke@0 295 return cData;
duke@0 296 }