Skip to content

Commit

Permalink
8235772: Remove use of deprecated finalize method from PiscesRenderer…
Browse files Browse the repository at this point in the history
… and AbstractSurface

Reviewed-by: jvos, kcr
  • Loading branch information
arapte committed Feb 24, 2020
1 parent 4eaff0d commit 3150562
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,6 +25,8 @@

package com.sun.pisces;

import com.sun.prism.impl.Disposer;

public abstract class AbstractSurface implements Surface {

private long nativePtr = 0L;
Expand All @@ -46,6 +48,10 @@ public abstract class AbstractSurface implements Surface {
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);
Expand Down Expand Up @@ -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() {
Expand All @@ -108,6 +128,4 @@ public final int getWidth() {
public final int getHeight() {
return height;
}

private native void nativeFinalize();
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,6 +25,8 @@

package com.sun.pisces;

import com.sun.prism.impl.Disposer;

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

private native void initialize();
Expand Down Expand Up @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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.");
}
}
}

32 changes: 5 additions & 27 deletions modules/javafx.graphics/src/main/native-prism-sw/JPiscesRenderer.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3150562

Please sign in to comment.