BufImgSurfaceData.c revision 4632
0N/A/*
180N/A * Copyright (c) 1999, 2010, 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
180N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
180N/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 *
180N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
180N/A * or visit www.oracle.com if you need additional information or have any
180N/A * questions.
0N/A */
0N/A
0N/A#include "BufImgSurfaceData.h"
0N/A#include <stdlib.h>
399N/A
399N/A#include "sun_awt_image_BufImgSurfaceData.h"
206N/A
206N/A#include "img_util_md.h"
0N/A#include "jni_util.h"
0N/A/* Define uintptr_t */
223N/A#include "gdefs.h"
223N/A
223N/A/**
223N/A * This include file contains support code for loops using the
223N/A * SurfaceData interface to talk to an X11 drawable from native
223N/A * code.
223N/A */
223N/A
399N/Astatic LockFunc BufImg_Lock;
399N/Astatic GetRasInfoFunc BufImg_GetRasInfo;
223N/Astatic ReleaseFunc BufImg_Release;
223N/Astatic DisposeFunc BufImg_Dispose;
223N/A
223N/Astatic ColorData *BufImg_SetupICM(JNIEnv *env, BufImgSDOps *bisdo);
223N/A
399N/Astatic jfieldID rgbID;
399N/Astatic jfieldID mapSizeID;
399N/Astatic jfieldID colorDataID;
399N/Astatic jfieldID pDataID;
399N/Astatic jfieldID allGrayID;
399N/A
399N/Astatic jclass clsICMCD;
399N/Astatic jmethodID initICMCDmID;
399N/A/*
399N/A * Class: sun_awt_image_BufImgSurfaceData
399N/A * Method: initIDs
399N/A * Signature: ()V
399N/A */
223N/AJNIEXPORT void JNICALL
399N/AJava_sun_awt_image_BufImgSurfaceData_initIDs
399N/A(JNIEnv *env, jclass bisd, jclass icm, jclass cd)
0N/A{
399N/A if (sizeof(BufImgRIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
223N/A JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
399N/A return;
418N/A }
418N/A
418N/A clsICMCD = (*env)->NewWeakGlobalRef(env, cd);
418N/A initICMCDmID = (*env)->GetMethodID(env, cd, "<init>", "(J)V");
418N/A pDataID = (*env)->GetFieldID(env, cd, "pData", "J");
418N/A
418N/A rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I");
418N/A allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z");
418N/A mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I");
418N/A colorDataID = (*env)->GetFieldID(env, icm, "colorData",
418N/A "Lsun/awt/image/BufImgSurfaceData$ICMColorData;");
399N/A if (allGrayID == 0 || rgbID == 0 || mapSizeID == 0 || pDataID == 0|| colorDataID == 0 || initICMCDmID == 0) {
399N/A JNU_ThrowInternalError(env, "Could not get field IDs");
399N/A }
399N/A}
399N/A
399N/A/*
418N/A * Class: sun_java2d_SurfaceData
418N/A * Method: freeNativeICMData
418N/A * Signature: (Ljava/awt/image/IndexColorModel;)V
418N/A */
418N/AJNIEXPORT void JNICALL
399N/AJava_sun_awt_image_BufImgSurfaceData_freeNativeICMData
399N/A (JNIEnv *env, jclass sd, jlong pData)
418N/A{
418N/A ColorData *cdata = (ColorData*)jlong_to_ptr(pData);
157N/A freeICMColorData(cdata);
399N/A}
223N/A
399N/A/*
399N/A * Class: sun_awt_image_BufImgSurfaceData
399N/A * Method: initOps
223N/A * Signature: (Ljava/lang/Object;IIIII)V
399N/A */
223N/AJNIEXPORT void JNICALL
418N/AJava_sun_awt_image_BufImgSurfaceData_initRaster(JNIEnv *env, jobject bisd,
399N/A jobject array,
418N/A jint offset, jint bitoffset,
399N/A jint width, jint height,
399N/A jint pixStr, jint scanStr,
418N/A jobject icm)
223N/A{
399N/A BufImgSDOps *bisdo =
325N/A (BufImgSDOps*)SurfaceData_InitOps(env, bisd, sizeof(BufImgSDOps));
325N/A if (bisdo == NULL) {
325N/A JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
325N/A return;
325N/A }
325N/A bisdo->sdOps.Lock = BufImg_Lock;
325N/A bisdo->sdOps.GetRasInfo = BufImg_GetRasInfo;
325N/A bisdo->sdOps.Release = BufImg_Release;
325N/A bisdo->sdOps.Unlock = NULL;
325N/A bisdo->sdOps.Dispose = BufImg_Dispose;
325N/A bisdo->array = (*env)->NewWeakGlobalRef(env, array);
325N/A bisdo->offset = offset;
325N/A bisdo->bitoffset = bitoffset;
325N/A bisdo->scanStr = scanStr;
223N/A bisdo->pixStr = pixStr;
399N/A if (JNU_IsNull(env, icm)) {
399N/A bisdo->lutarray = NULL;
399N/A bisdo->lutsize = 0;
399N/A bisdo->icm = NULL;
157N/A } else {
206N/A jobject lutarray = (*env)->GetObjectField(env, icm, rgbID);
157N/A bisdo->lutarray = (*env)->NewWeakGlobalRef(env, lutarray);
157N/A bisdo->lutsize = (*env)->GetIntField(env, icm, mapSizeID);
bisdo->icm = (*env)->NewWeakGlobalRef(env, icm);
}
bisdo->rasbounds.x1 = 0;
bisdo->rasbounds.y1 = 0;
bisdo->rasbounds.x2 = width;
bisdo->rasbounds.y2 = height;
}
/*
* Method for disposing native BufImgSD
*/
static void BufImg_Dispose(JNIEnv *env, SurfaceDataOps *ops)
{
/* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
(*env)->DeleteWeakGlobalRef(env, bisdo->array);
if (bisdo->lutarray != NULL) {
(*env)->DeleteWeakGlobalRef(env, bisdo->lutarray);
}
if (bisdo->icm != NULL) {
(*env)->DeleteWeakGlobalRef(env, bisdo->icm);
}
}
static jint BufImg_Lock(JNIEnv *env,
SurfaceDataOps *ops,
SurfaceDataRasInfo *pRasInfo,
jint lockflags)
{
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
if ((lockflags & (SD_LOCK_LUT)) != 0 && JNU_IsNull(env, bisdo->lutarray)) {
/* REMIND: Should this be an InvalidPipe exception? */
JNU_ThrowNullPointerException(env, "Attempt to lock missing colormap");
return SD_FAILURE;
}
if ((lockflags & SD_LOCK_INVCOLOR) != 0 ||
(lockflags & SD_LOCK_INVGRAY) != 0)
{
bipriv->cData = BufImg_SetupICM(env, bisdo);
if (bipriv->cData == NULL) {
JNU_ThrowNullPointerException(env, "Could not initialize "
"inverse tables");
return SD_FAILURE;
}
} else {
bipriv->cData = NULL;
}
bipriv->lockFlags = lockflags;
bipriv->base = NULL;
bipriv->lutbase = NULL;
SurfaceData_IntersectBounds(&pRasInfo->bounds, &bisdo->rasbounds);
return SD_SUCCESS;
}
static void BufImg_GetRasInfo(JNIEnv *env,
SurfaceDataOps *ops,
SurfaceDataRasInfo *pRasInfo)
{
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
if ((bipriv->lockFlags & (SD_LOCK_RD_WR)) != 0) {
bipriv->base =
(*env)->GetPrimitiveArrayCritical(env, bisdo->array, NULL);
}
if ((bipriv->lockFlags & (SD_LOCK_LUT)) != 0) {
bipriv->lutbase =
(*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL);
}
if (bipriv->base == NULL) {
pRasInfo->rasBase = NULL;
pRasInfo->pixelStride = 0;
pRasInfo->pixelBitOffset = 0;
pRasInfo->scanStride = 0;
} else {
pRasInfo->rasBase = (void *)
(((uintptr_t) bipriv->base) + bisdo->offset);
pRasInfo->pixelStride = bisdo->pixStr;
pRasInfo->pixelBitOffset = bisdo->bitoffset;
pRasInfo->scanStride = bisdo->scanStr;
}
if (bipriv->lutbase == NULL) {
pRasInfo->lutBase = NULL;
pRasInfo->lutSize = 0;
} else {
pRasInfo->lutBase = bipriv->lutbase;
pRasInfo->lutSize = bisdo->lutsize;
}
if (bipriv->cData == NULL) {
pRasInfo->invColorTable = NULL;
pRasInfo->redErrTable = NULL;
pRasInfo->grnErrTable = NULL;
pRasInfo->bluErrTable = NULL;
} else {
pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;
pRasInfo->redErrTable = bipriv->cData->img_oda_red;
pRasInfo->grnErrTable = bipriv->cData->img_oda_green;
pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;
pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;
}
}
static void BufImg_Release(JNIEnv *env,
SurfaceDataOps *ops,
SurfaceDataRasInfo *pRasInfo)
{
BufImgSDOps *bisdo = (BufImgSDOps *)ops;
BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);
if (bipriv->base != NULL) {
jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0)
? 0 : JNI_ABORT);
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->array,
bipriv->base, mode);
}
if (bipriv->lutbase != NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,
bipriv->lutbase, JNI_ABORT);
}
}
static ColorData *BufImg_SetupICM(JNIEnv *env,
BufImgSDOps *bisdo)
{
ColorData *cData = NULL;
jobject colorData;
if (JNU_IsNull(env, bisdo->icm)) {
return (ColorData *) NULL;
}
colorData = (*env)->GetObjectField(env, bisdo->icm, colorDataID);
if (JNU_IsNull(env, colorData)) {
if (JNU_IsNull(env, clsICMCD)) {
// we are unable to create a wrapper object
return (ColorData*)NULL;
}
} else {
cData = (ColorData*)JNU_GetLongFieldAsPtr(env, colorData, pDataID);
}
if (cData != NULL) {
return cData;
}
cData = (ColorData*)calloc(1, sizeof(ColorData));
if (cData != NULL) {
jboolean allGray
= (*env)->GetBooleanField(env, bisdo->icm, allGrayID);
int *pRgb = (int *)
((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL));
cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);
if (allGray == JNI_TRUE) {
initInverseGrayLut(pRgb, bisdo->lutsize, cData);
}
(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,
JNI_ABORT);
initDitherTables(cData);
if (JNU_IsNull(env, colorData)) {
jlong pData = ptr_to_jlong(cData);
colorData = (*env)->NewObjectA(env, clsICMCD, initICMCDmID, (jvalue *)&pData);
(*env)->SetObjectField(env, bisdo->icm, colorDataID, colorData);
}
}
return cData;
}