Skip to content
Permalink
Browse files

8235772: Remove use of deprecated finalize method from PiscesRenderer…

… and AbstractSurface

Reviewed-by: jvos, kcr
  • Loading branch information
Ambarish Rapte
Ambarish Rapte committed Feb 24, 2020
1 parent 4eaff0d commit 31505620ad5fc49580ae7e7241a1edcd6d1f2e30
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@

package com.sun.pisces;

import com.sun.prism.impl.Disposer;

public abstract class AbstractSurface implements Surface {

private long nativePtr = 0L;
@@ -46,6 +48,10 @@
this.height = height;
}

protected void addDisposerRecord() {
Disposer.addRecord(this, new AbstractSurfaceDisposerRecord(nativePtr));
}

public final void getRGB(int[] argb, int offset, int scanLength, int x, int y, int width, int height) {
this.rgbCheck(argb.length, offset, scanLength, x, y, width, height);
this.getRGBImpl(argb, offset, scanLength, x, y, width, height);
@@ -97,8 +103,22 @@ private void rgbCheck(int arr_length, int offset, int scanLength, int x, int y,
}
}

protected void finalize() {
this.nativeFinalize();
private static native void disposeNative(long nativeHandle);

private static class AbstractSurfaceDisposerRecord implements Disposer.Record {
private long nativeHandle;

AbstractSurfaceDisposerRecord(long nh) {
nativeHandle = nh;
}

@Override
public void dispose() {
if (nativeHandle != 0L) {
disposeNative(nativeHandle);
nativeHandle = 0L;
}
}
}

public final int getWidth() {
@@ -108,6 +128,4 @@ public final int getWidth() {
public final int getHeight() {
return height;
}

private native void nativeFinalize();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,12 @@ public JavaSurface(int[] dataInt, int dataType, int width, int height) {
this.dataBuffer = IntBuffer.wrap(this.dataInt);

initialize(dataType, width, height);
// The native method initialize() creates the native object of
// struct JavaSurface and saves it's reference in the super class
// member AbstractSurface.nativePtr. This reference is needed for
// creating disposer record hence the below call to addDisposerRecord()
// is needed here and cannot be made in super class constructor.
addDisposerRecord();
}

public IntBuffer getDataIntBuffer() {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@

package com.sun.pisces;

import com.sun.prism.impl.Disposer;

/**
* PiscesRenderer class is basic public API accessing Pisces library capabilities.
*
@@ -86,6 +88,7 @@
public PiscesRenderer(AbstractSurface surface) {
this.surface = surface;
initialize();
Disposer.addRecord(this, new PiscesRendererDisposerRecord(nativePtr));
}

private native void initialize();
@@ -436,12 +439,21 @@ private void inputImageCheck(int width, int height, int offset, int stride, int
}
}

protected void finalize() {
this.nativeFinalize();
}
private static native void disposeNative(long nativeHandle);

/**
* Native finalizer. Releases native memory used by PiscesRenderer at lifetime.
*/
private native void nativeFinalize();
private static class PiscesRendererDisposerRecord implements Disposer.Record {
private long nativeHandle;

PiscesRendererDisposerRecord(long nh) {
nativeHandle = nh;
}

@Override
public void dispose() {
if (nativeHandle != 0L) {
disposeNative(nativeHandle);
nativeHandle = 0L;
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -37,7 +37,6 @@ static jfieldID fieldIds[SURFACE_LAST + 1];
static jboolean fieldIdsInitialized = JNI_FALSE;

static jboolean initializeSurfaceFieldIds(JNIEnv* env, jobject objectHandle);
static void disposeNativeImpl(JNIEnv* env, jobject objectHandle);

AbstractSurface*
surface_get(JNIEnv* env, jobject surfaceHandle) {
@@ -52,9 +51,14 @@ surface_initialize(JNIEnv* env, jobject surfaceHandle) {
}

JNIEXPORT void JNICALL
Java_com_sun_pisces_AbstractSurface_nativeFinalize(JNIEnv* env,
jobject objectHandle) {
disposeNativeImpl(env, objectHandle);
Java_com_sun_pisces_AbstractSurface_disposeNative(JNIEnv *env, jclass cls, jlong nativePtr)
{
AbstractSurface* surface = (AbstractSurface*) JLongToPointer(nativePtr);

if (surface != NULL) {
surface->cleanup(surface);
surface_dispose(&surface->super);
}
}

JNIEXPORT void JNICALL
@@ -184,29 +188,3 @@ initializeSurfaceFieldIds(JNIEnv* env, jobject objectHandle) {

return retVal;
}

static void
disposeNativeImpl(JNIEnv* env, jobject objectHandle) {
AbstractSurface* surface;

if (!fieldIdsInitialized) {
return;
}

surface = (AbstractSurface*)JLongToPointer(
(*env)->GetLongField(env, objectHandle,
fieldIds[SURFACE_NATIVE_PTR]));

if (surface != NULL) {
surface->cleanup(surface);
surface_dispose(&surface->super);
(*env)->SetLongField(env, objectHandle, fieldIds[SURFACE_NATIVE_PTR],
(jlong)0);

if (JNI_TRUE == readAndClearMemErrorFlag()) {
JNI_ThrowNew(env, "java/lang/OutOfMemoryError",
"Allocation of internal renderer buffer failed.");
}
}
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,6 @@ static jboolean fieldIdsInitialized = JNI_FALSE;
static jboolean initializeRendererFieldIds(JNIEnv *env, jobject objectHandle);

static int toPiscesCoords(unsigned int ff);
static void renderer_finalize(JNIEnv *env, jobject objectHandle);
static void fillAlphaMask(Renderer* rdr, jint minX, jint minY, jint maxX, jint maxY,
JNIEnv *env, jobject this, jint maskType, jbyteArray jmask, jint x, jint y,
jint maskWidth, jint maskHeight, jint offset, jint stride);
@@ -82,14 +81,11 @@ Java_com_sun_pisces_PiscesRenderer_initialize(JNIEnv* env, jobject objectHandle)
}

JNIEXPORT void JNICALL
Java_com_sun_pisces_PiscesRenderer_nativeFinalize(JNIEnv* env,
jobject objectHandle)
Java_com_sun_pisces_PiscesRenderer_disposeNative(JNIEnv *env, jclass cls, jlong nativePtr)
{
renderer_finalize(env, objectHandle);

if (JNI_TRUE == readAndClearMemErrorFlag()) {
JNI_ThrowNew(env, "java/lang/OutOfMemoryError",
"Allocation of internal renderer buffer failed.");
Renderer* rdr = (Renderer*) JLongToPointer(nativePtr);
if (rdr != NULL) {
renderer_dispose(rdr);
}
}

@@ -293,24 +289,6 @@ renderer_get(JNIEnv* env, jobject objectHandle) {
fieldIds[RENDERER_NATIVE_PTR]));
}

static void
renderer_finalize(JNIEnv *env, jobject objectHandle) {
Renderer* rdr;

if (!fieldIdsInitialized) {
return;
}

rdr = (Renderer*)JLongToPointer((*env)->GetLongField(env, objectHandle,
fieldIds[RENDERER_NATIVE_PTR]));

if (rdr != (Renderer*)0) {
renderer_dispose(rdr);
(*env)->SetLongField(env, objectHandle, fieldIds[RENDERER_NATIVE_PTR],
(jlong)0);
}
}

static jboolean
initializeObjectFieldIds(JNIEnv *env,
jobject objectHandle,

0 comments on commit 3150562

Please sign in to comment.
You can’t perform that action at this time.