Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/awt/image/BufImgSurfaceData.c
38918 views
/*1* Copyright (c) 1999, 2019, 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 "BufImgSurfaceData.h"26#include <stdlib.h>2728#include "sun_awt_image_BufImgSurfaceData.h"2930#include "img_util_md.h"31#include "jni_util.h"32/* Define uintptr_t */33#include "gdefs.h"34#include "Disposer.h"3536/**37* This include file contains support code for loops using the38* SurfaceData interface to talk to an X11 drawable from native39* code.40*/4142static LockFunc BufImg_Lock;43static GetRasInfoFunc BufImg_GetRasInfo;44static ReleaseFunc BufImg_Release;45static DisposeFunc BufImg_Dispose;4647static ColorData *BufImg_SetupICM(JNIEnv *env, BufImgSDOps *bisdo);4849static jfieldID rgbID;50static jfieldID mapSizeID;51static jfieldID colorDataID;52static jfieldID pDataID;53static jfieldID allGrayID;5455static jclass clsICMCD;56static jmethodID initICMCDmID;57/*58* Class: sun_awt_image_BufImgSurfaceData59* Method: initIDs60* Signature: ()V61*/62JNIEXPORT void JNICALL63Java_sun_awt_image_BufImgSurfaceData_initIDs64(JNIEnv *env, jclass bisd, jclass icm, jclass cd)65{66if (sizeof(BufImgRIPrivate) > SD_RASINFO_PRIVATE_SIZE) {67JNU_ThrowInternalError(env, "Private RasInfo structure too large!");68return;69}7071clsICMCD = (*env)->NewWeakGlobalRef(env, cd);72JNU_CHECK_EXCEPTION(env);73CHECK_NULL(initICMCDmID = (*env)->GetMethodID(env, cd, "<init>", "(J)V"));74CHECK_NULL(pDataID = (*env)->GetFieldID(env, cd, "pData", "J"));75CHECK_NULL(rgbID = (*env)->GetFieldID(env, icm, "rgb", "[I"));76CHECK_NULL(allGrayID = (*env)->GetFieldID(env, icm, "allgrayopaque", "Z"));77CHECK_NULL(mapSizeID = (*env)->GetFieldID(env, icm, "map_size", "I"));78CHECK_NULL(colorDataID = (*env)->GetFieldID(env, icm, "colorData",79"Lsun/awt/image/BufImgSurfaceData$ICMColorData;"));80}8182/*83* Class: sun_awt_image_BufImgSurfaceData84* Method: initOps85* Signature: (Ljava/lang/Object;IIIII)V86*/87JNIEXPORT void JNICALL88Java_sun_awt_image_BufImgSurfaceData_initRaster(JNIEnv *env, jobject bisd,89jobject array,90jint offset, jint bitoffset,91jint width, jint height,92jint pixStr, jint scanStr,93jobject icm)94{95BufImgSDOps *bisdo =96(BufImgSDOps*)SurfaceData_InitOps(env, bisd, sizeof(BufImgSDOps));97if (bisdo == NULL) {98JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");99return;100}101bisdo->sdOps.Lock = BufImg_Lock;102bisdo->sdOps.GetRasInfo = BufImg_GetRasInfo;103bisdo->sdOps.Release = BufImg_Release;104bisdo->sdOps.Unlock = NULL;105bisdo->sdOps.Dispose = BufImg_Dispose;106bisdo->array = (*env)->NewWeakGlobalRef(env, array);107JNU_CHECK_EXCEPTION(env);108bisdo->offset = offset;109bisdo->bitoffset = bitoffset;110bisdo->scanStr = scanStr;111bisdo->pixStr = pixStr;112if (JNU_IsNull(env, icm)) {113bisdo->lutarray = NULL;114bisdo->lutsize = 0;115bisdo->icm = NULL;116} else {117jobject lutarray = (*env)->GetObjectField(env, icm, rgbID);118bisdo->lutarray = (*env)->NewWeakGlobalRef(env, lutarray);119JNU_CHECK_EXCEPTION(env);120bisdo->lutsize = (*env)->GetIntField(env, icm, mapSizeID);121bisdo->icm = (*env)->NewWeakGlobalRef(env, icm);122}123bisdo->rasbounds.x1 = 0;124bisdo->rasbounds.y1 = 0;125bisdo->rasbounds.x2 = width;126bisdo->rasbounds.y2 = height;127}128129/*130* Releases native structures associated with BufImgSurfaceData.ICMColorData.131*/132static void BufImg_Dispose_ICMColorData(JNIEnv *env, jlong pData)133{134ColorData *cdata = (ColorData*)jlong_to_ptr(pData);135freeICMColorData(cdata);136}137138/*139* Method for disposing native BufImgSD140*/141static void BufImg_Dispose(JNIEnv *env, SurfaceDataOps *ops)142{143/* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */144BufImgSDOps *bisdo = (BufImgSDOps *)ops;145(*env)->DeleteWeakGlobalRef(env, bisdo->array);146if (bisdo->lutarray != NULL) {147(*env)->DeleteWeakGlobalRef(env, bisdo->lutarray);148}149if (bisdo->icm != NULL) {150(*env)->DeleteWeakGlobalRef(env, bisdo->icm);151}152}153154static jint BufImg_Lock(JNIEnv *env,155SurfaceDataOps *ops,156SurfaceDataRasInfo *pRasInfo,157jint lockflags)158{159BufImgSDOps *bisdo = (BufImgSDOps *)ops;160BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);161162if ((lockflags & (SD_LOCK_LUT)) != 0 && JNU_IsNull(env, bisdo->lutarray)) {163/* REMIND: Should this be an InvalidPipe exception? */164JNU_ThrowNullPointerException(env, "Attempt to lock missing colormap");165return SD_FAILURE;166}167if ((lockflags & SD_LOCK_INVCOLOR) != 0 ||168(lockflags & SD_LOCK_INVGRAY) != 0)169{170bipriv->cData = BufImg_SetupICM(env, bisdo);171if (bipriv->cData == NULL) {172(*env)->ExceptionClear(env);173JNU_ThrowNullPointerException(env, "Could not initialize inverse tables");174return SD_FAILURE;175}176} else {177bipriv->cData = NULL;178}179180bipriv->lockFlags = lockflags;181bipriv->base = NULL;182bipriv->lutbase = NULL;183184SurfaceData_IntersectBounds(&pRasInfo->bounds, &bisdo->rasbounds);185186return SD_SUCCESS;187}188189static void BufImg_GetRasInfo(JNIEnv *env,190SurfaceDataOps *ops,191SurfaceDataRasInfo *pRasInfo)192{193BufImgSDOps *bisdo = (BufImgSDOps *)ops;194BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);195196if ((bipriv->lockFlags & (SD_LOCK_RD_WR)) != 0) {197bipriv->base =198(*env)->GetPrimitiveArrayCritical(env, bisdo->array, NULL);199CHECK_NULL(bipriv->base);200}201if ((bipriv->lockFlags & (SD_LOCK_LUT)) != 0) {202bipriv->lutbase =203(*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL);204}205206if (bipriv->base == NULL) {207pRasInfo->rasBase = NULL;208pRasInfo->pixelStride = 0;209pRasInfo->pixelBitOffset = 0;210pRasInfo->scanStride = 0;211} else {212pRasInfo->rasBase = (void *)213(((uintptr_t) bipriv->base) + bisdo->offset);214pRasInfo->pixelStride = bisdo->pixStr;215pRasInfo->pixelBitOffset = bisdo->bitoffset;216pRasInfo->scanStride = bisdo->scanStr;217}218if (bipriv->lutbase == NULL) {219pRasInfo->lutBase = NULL;220pRasInfo->lutSize = 0;221} else {222pRasInfo->lutBase = bipriv->lutbase;223pRasInfo->lutSize = bisdo->lutsize;224}225if (bipriv->cData == NULL) {226pRasInfo->invColorTable = NULL;227pRasInfo->redErrTable = NULL;228pRasInfo->grnErrTable = NULL;229pRasInfo->bluErrTable = NULL;230} else {231pRasInfo->invColorTable = bipriv->cData->img_clr_tbl;232pRasInfo->redErrTable = bipriv->cData->img_oda_red;233pRasInfo->grnErrTable = bipriv->cData->img_oda_green;234pRasInfo->bluErrTable = bipriv->cData->img_oda_blue;235pRasInfo->invGrayTable = bipriv->cData->pGrayInverseLutData;236}237}238239static void BufImg_Release(JNIEnv *env,240SurfaceDataOps *ops,241SurfaceDataRasInfo *pRasInfo)242{243BufImgSDOps *bisdo = (BufImgSDOps *)ops;244BufImgRIPrivate *bipriv = (BufImgRIPrivate *) &(pRasInfo->priv);245246if (bipriv->base != NULL) {247jint mode = (((bipriv->lockFlags & (SD_LOCK_WRITE)) != 0)248? 0 : JNI_ABORT);249(*env)->ReleasePrimitiveArrayCritical(env, bisdo->array,250bipriv->base, mode);251}252if (bipriv->lutbase != NULL) {253(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray,254bipriv->lutbase, JNI_ABORT);255}256}257258static ColorData *BufImg_SetupICM(JNIEnv *env,259BufImgSDOps *bisdo)260{261ColorData *cData = NULL;262jobject colorData;263264if (JNU_IsNull(env, bisdo->icm)) {265return (ColorData *) NULL;266}267268colorData = (*env)->GetObjectField(env, bisdo->icm, colorDataID);269270if (JNU_IsNull(env, colorData)) {271if (JNU_IsNull(env, clsICMCD)) {272// we are unable to create a wrapper object273return (ColorData*)NULL;274}275} else {276cData = (ColorData*)JNU_GetLongFieldAsPtr(env, colorData, pDataID);277}278279if (cData != NULL) {280return cData;281}282283cData = (ColorData*)calloc(1, sizeof(ColorData));284285if (cData != NULL) {286jboolean allGray287= (*env)->GetBooleanField(env, bisdo->icm, allGrayID);288int *pRgb = (int *)289((*env)->GetPrimitiveArrayCritical(env, bisdo->lutarray, NULL));290291if (pRgb == NULL) {292free(cData);293return (ColorData*)NULL;294}295296cData->img_clr_tbl = initCubemap(pRgb, bisdo->lutsize, 32);297if (cData->img_clr_tbl == NULL) {298(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb, JNI_ABORT);299free(cData);300return (ColorData*)NULL;301}302if (allGray == JNI_TRUE) {303initInverseGrayLut(pRgb, bisdo->lutsize, cData);304}305(*env)->ReleasePrimitiveArrayCritical(env, bisdo->lutarray, pRgb,306JNI_ABORT);307308initDitherTables(cData);309310if (JNU_IsNull(env, colorData)) {311jlong pData = ptr_to_jlong(cData);312colorData = (*env)->NewObjectA(env, clsICMCD, initICMCDmID, (jvalue *)&pData);313314if ((*env)->ExceptionCheck(env))315{316free(cData);317return (ColorData*)NULL;318}319320(*env)->SetObjectField(env, bisdo->icm, colorDataID, colorData);321Disposer_AddRecord(env, colorData, BufImg_Dispose_ICMColorData, pData);322}323}324325return cData;326}327328329