1 /*
   2  * Copyright (c) 1999, 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 "BufImgSurfaceData.h"
  27 #include "malloc.h"
  28 
  29 #include "sun_awt_image_BufImgSurfaceData.h"
  30 
  31 #include "img_util_md.h"
  32 #include "jni_util.h"
  33 /* Define uintptr_t */
  34 #include "gdefs.h"
  35 
  36 /**
  37  * This include file contains support code for loops using the
  38  * SurfaceData interface to talk to an X11 drawable from native
  39  * code.
  40  */
  41 
  42 static LockFunc                 BufImg_Lock;
  43 static GetRasInfoFunc           BufImg_GetRasInfo;
  44 static ReleaseFunc              BufImg_Release;
  45 static DisposeFunc              BufImg_Dispose;
  46 
  47 static ColorData *BufImg_SetupICM(JNIEnv *env, BufImgSDOps *bisdo);
  48 
  49 static jfieldID         rgbID;
  50 static jfieldID         mapSizeID;
  51 static jfieldID         colorDataID;
  52 static jfieldID         pDataID;
  53 static jfieldID         allGrayID;
  54 
  55 static jclass           clsICMCD;
  56 static jmethodID        initICMCDmID;
  57 /*
  58  * Class:     sun_awt_image_BufImgSurfaceData
  59  * Method:    initIDs
  60  * Signature: ()V
  61  */
  62 JNIEXPORT void JNICALL
  63 Java_sun_awt_image_BufImgSurfaceData_initIDs
  64 (JNIEnv *env, jclass bisd, jclass icm, jclass cd)
  65 {
  66     if (sizeof(BufImgRIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
  67         JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
  68         return;
  69     }
  70 
  71     clsICMCD = (*env)->NewWeakGlobalRef(env, cd);
  72     initICMCDmID = (*env)->GetMethodID(env, cd, "<init>", "(J)V");
  73     pDataID = (*env)->GetFieldID(env, cd, "pData", "J");
  74 
  75     rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I");
  76     allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z");
  77     mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I");
  78     colorDataID = (*env)->GetFieldID(env, icm, "colorData",
  79         "Lsun/awt/image/BufImgSurfaceData$ICMColorData;");
  80     if (allGrayID == 0 || rgbID == 0 || mapSizeID == 0 || pDataID == 0|| colorDataID == 0 || initICMCDmID == 0) {
  81         JNU_ThrowInternalError(env, "Could not get field IDs");
  82     }
  83 }
  84 
  85 /*
  86  * Class:     sun_java2d_SurfaceData
  87  * Method:    freeNativeICMData
  88  * Signature: (Ljava/awt/image/IndexColorModel;)V
  89  */
  90 JNIEXPORT void JNICALL
  91 Java_sun_awt_image_BufImgSurfaceData_freeNativeICMData
  92     (JNIEnv *env, jclass sd, jlong pData)
  93 {
  94     ColorData *cdata = (ColorData*)jlong_to_ptr(pData);
  95     freeICMColorData(cdata);
  96 }
  97 
  98 /*
  99  * Class:     sun_awt_image_BufImgSurfaceData
 100  * Method:    initOps
 101  * Signature: (Ljava/lang/Object;IIIII)V
 102  */
 103 JNIEXPORT void JNICALL
 104 Java_sun_awt_image_BufImgSurfaceData_initRaster(JNIEnv *env, jobject bisd,
 105                                                 jobject array,
 106                                                 jint offset, jint bitoffset,
 107                                                 jint width, jint height,
 108                                                 jint pixStr, jint scanStr,
 109                                                 jobject icm)
 110 {
 111     BufImgSDOps *bisdo =
 112         (BufImgSDOps*)SurfaceData_InitOps(env, bisd, sizeof(BufImgSDOps));
 113     if (bisdo == NULL) {
 114         JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
 115         return;
 116     }
 117     bisdo->sdOps.Lock = BufImg_Lock;
 118     bisdo->sdOps.GetRasInfo = BufImg_GetRasInfo;
 119     bisdo->sdOps.Release = BufImg_Release;
 120     bisdo->sdOps.Unlock = NULL;
 121     bisdo->sdOps.Dispose = BufImg_Dispose;
 122     bisdo->array = (*env)->NewWeakGlobalRef(env, array);
 123     bisdo->offset = offset;
 124     bisdo->bitoffset = bitoffset;
 125     bisdo->scanStr = scanStr;
 126     bisdo->pixStr = pixStr;
 127     if (JNU_IsNull(env, icm)) {
 128         bisdo->lutarray = NULL;
 129         bisdo->lutsize = 0;
 130         bisdo->icm = NULL;
 131     } else {
 132         jobject lutarray = (*env)->GetObjectField(env, icm, rgbID);
 133         bisdo->lutarray = (*env)->NewWeakGlobalRef(env, lutarray);
 134         bisdo->lutsize = (*env)->GetIntField(env, icm, mapSizeID);
 135         bisdo->icm = (*env)->NewWeakGlobalRef(env, icm);
 136     }
 137     bisdo->rasbounds.x1 = 0;
 138     bisdo->rasbounds.y1 = 0;
 139     bisdo->rasbounds.x2 = width;
 140     bisdo->rasbounds.y2 = height;
 141 }
 142 
 143 /*
 144  * Method for disposing native BufImgSD
 145  */
 146 static void BufImg_Dispose(JNIEnv *env, SurfaceDataOps *ops)
 147 {
 148     /* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
 149     BufImgSDOps *bisdo = (BufImgSDOps *)ops;
 150     (*env)->DeleteWeakGlobalRef(env, bisdo->array);
 151     if (bisdo->lutarray != NULL) {
 152         (*env)->DeleteWeakGlobalRef(env, bisdo->lutarray);
 153     }
 154     if (bisdo->icm != NULL) {
 155         (*env)->DeleteWeakGlobalRef(env, bisdo->icm);
 156     }
 157 }
 158 
 159 static jint BufImg_Lock(JNIEnv *env,
 160                         SurfaceDataOps *ops,
 161                         SurfaceDataRasInfo *pRasInfo,
 162                         jint lockflags)
 163 {
 164     BufImgSDOps *bisdo = (BufImgSDOps *)ops;
 165     BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
 166 
 167     if ((lockflags & (SD_LOCK_LUT)) != 0 && JNU_IsNull(env, bisdo->lutarray)) {
 168         /* REMIND: Should this be an InvalidPipe exception? */
 169         JNU_ThrowNullPointerException(env, "Attempt to lock missing colormap");
 170         return SD_FAILURE;
 171     }
 172     if ((lockflags & SD_LOCK_INVCOLOR) != 0 ||
 173         (lockflags & SD_LOCK_INVGRAY) != 0)
 174     {
 175         bipriv->cData = BufImg_SetupICM(env, bisdo);
 176         if (bipriv->cData == NULL) {
 177             JNU_ThrowNullPointerException(env, "Could not initialize "
 178                                           "inverse tables");
 179             return SD_FAILURE;
 180         }
 181     } else {
 182         bipriv->cData = NULL;
 183     }
 184 
 185     bipriv->lockFlags = lockflags;
 186     bipriv->base = NULL;
 187     bipriv->lutbase = NULL;
 188 
 189     SurfaceData_IntersectBounds(&pRasInfo->bounds, &bisdo->rasbounds);
 190 
 191     return SD_SUCCESS;
 192 }
 193 
 194 static void BufImg_GetRasInfo(JNIEnv *env,
 195                               SurfaceDataOps *ops,
 196                               SurfaceDataRasInfo *pRasInfo)
 197 {
 198     BufImgSDOps *bisdo = (BufImgSDOps *)ops;
 199     BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
 200 
 201     if ((bipriv->lockFlags & (SD_LOCK_RD_WR)) != 0) {
 202         bipriv->base =
 203             (*env)->GetPrimitiveArrayCritical(env, bisdo->array, NULL);
 204     }
 205     if ((bipriv->lockFlags & (SD_LOCK_LUT)) != 0) {
 206         bipriv->lutbase =
 207             (*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL);
 208     }
 209 
 210     if (bipriv->base == NULL) {
 211         pRasInfo->rasBase = NULL;
 212         pRasInfo->pixelStride = 0;
 213         pRasInfo->pixelBitOffset = 0;
 214         pRasInfo->scanStride = 0;
 215     } else {
 216         pRasInfo->rasBase = (void *)
 217             (((uintptr_t) bipriv->base) + bisdo->offset);
 218         pRasInfo->pixelStride = bisdo->pixStr;
 219         pRasInfo->pixelBitOffset = bisdo->bitoffset;
 220         pRasInfo->scanStride = bisdo->scanStr;
 221     }
 222     if (bipriv->lutbase == NULL) {
 223         pRasInfo->lutBase = NULL;
 224         pRasInfo->lutSize = 0;
 225     } else {
 226         pRasInfo->lutBase = bipriv->lutbase;
 227         pRasInfo->lutSize = bisdo->lutsize;
 228     }
 229     if (bipriv->cData == NULL) {
 230         pRasInfo->invColorTable = NULL;
 231         pRasInfo->redErrTable = NULL;
 232         pRasInfo->grnErrTable = NULL;
 233         pRasInfo->bluErrTable = NULL;
 234     } else {
 235         pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;
 236         pRasInfo->redErrTable = bipriv->cData->img_oda_red;
 237         pRasInfo->grnErrTable = bipriv->cData->img_oda_green;
 238         pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;
 239         pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;
 240     }
 241 }
 242 
 243 static void BufImg_Release(JNIEnv *env,
 244                            SurfaceDataOps *ops,
 245                            SurfaceDataRasInfo *pRasInfo)
 246 {
 247     BufImgSDOps *bisdo = (BufImgSDOps *)ops;
 248     BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
 249 
 250     if (bipriv->base != NULL) {
 251         jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0)
 252                      ? 0 : JNI_ABORT);
 253         (*env)->ReleasePrimitiveArrayCritical(env, bisdo->array,
 254                                               bipriv->base, mode);
 255     }
 256     if (bipriv->lutbase != NULL) {
 257         (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,
 258                                               bipriv->lutbase, JNI_ABORT);
 259     }
 260 }
 261 
 262 static ColorData *BufImg_SetupICM(JNIEnv *env,
 263                                   BufImgSDOps *bisdo)
 264 {
 265     ColorData *cData = NULL;
 266     jobject colorData;
 267 
 268     if (JNU_IsNull(env, bisdo->icm)) {
 269         return (ColorData *) NULL;
 270     }
 271 
 272     colorData = (*env)->GetObjectField(env, bisdo->icm, colorDataID);
 273 
 274     if (JNU_IsNull(env, colorData)) {
 275         if (JNU_IsNull(env, clsICMCD)) {
 276             // we are unable to create a wrapper object
 277             return (ColorData*)NULL;
 278         }
 279     } else {
 280         cData = (ColorData*)JNU_GetLongFieldAsPtr(env, colorData, pDataID);
 281     }
 282 
 283     if (cData != NULL) {
 284         return cData;
 285     }
 286 
 287     cData = (ColorData*)calloc(1, sizeof(ColorData));
 288 
 289     if (cData != NULL) {
 290         jboolean allGray
 291             = (*env)->GetBooleanField(env, bisdo->icm, allGrayID);
 292         int *pRgb = (int *)
 293             ((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL));
 294         cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);
 295         if (allGray == JNI_TRUE) {
 296             initInverseGrayLut(pRgb, bisdo->lutsize, cData);
 297         }
 298         (*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,
 299                                               JNI_ABORT);
 300 
 301         initDitherTables(cData);
 302 
 303         if (JNU_IsNull(env, colorData)) {
 304             jlong pData = ptr_to_jlong(cData);
 305             colorData = (*env)->NewObjectA(env, clsICMCD, initICMCDmID, (jvalue *)&pData);
 306             (*env)->SetObjectField(env, bisdo->icm, colorDataID, colorData);
 307         }
 308     }
 309 
 310     return cData;
 311 }