Skip to content

Commit 3150562

Browse files
committed
8235772: Remove use of deprecated finalize method from PiscesRenderer and AbstractSurface
Reviewed-by: jvos, kcr
1 parent 4eaff0d commit 3150562

File tree

5 files changed

+64
-72
lines changed

5 files changed

+64
-72
lines changed

modules/javafx.graphics/src/main/java/com/sun/pisces/AbstractSurface.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package com.sun.pisces;
2727

28+
import com.sun.prism.impl.Disposer;
29+
2830
public abstract class AbstractSurface implements Surface {
2931

3032
private long nativePtr = 0L;
@@ -46,6 +48,10 @@ public abstract class AbstractSurface implements Surface {
4648
this.height = height;
4749
}
4850

51+
protected void addDisposerRecord() {
52+
Disposer.addRecord(this, new AbstractSurfaceDisposerRecord(nativePtr));
53+
}
54+
4955
public final void getRGB(int[] argb, int offset, int scanLength, int x, int y, int width, int height) {
5056
this.rgbCheck(argb.length, offset, scanLength, x, y, width, height);
5157
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,
97103
}
98104
}
99105

100-
protected void finalize() {
101-
this.nativeFinalize();
106+
private static native void disposeNative(long nativeHandle);
107+
108+
private static class AbstractSurfaceDisposerRecord implements Disposer.Record {
109+
private long nativeHandle;
110+
111+
AbstractSurfaceDisposerRecord(long nh) {
112+
nativeHandle = nh;
113+
}
114+
115+
@Override
116+
public void dispose() {
117+
if (nativeHandle != 0L) {
118+
disposeNative(nativeHandle);
119+
nativeHandle = 0L;
120+
}
121+
}
102122
}
103123

104124
public final int getWidth() {
@@ -108,6 +128,4 @@ public final int getWidth() {
108128
public final int getHeight() {
109129
return height;
110130
}
111-
112-
private native void nativeFinalize();
113131
}

modules/javafx.graphics/src/main/java/com/sun/pisces/JavaSurface.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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) {
4343
this.dataBuffer = IntBuffer.wrap(this.dataInt);
4444

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

4854
public IntBuffer getDataIntBuffer() {

modules/javafx.graphics/src/main/java/com/sun/pisces/PiscesRenderer.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package com.sun.pisces;
2727

28+
import com.sun.prism.impl.Disposer;
29+
2830
/**
2931
* PiscesRenderer class is basic public API accessing Pisces library capabilities.
3032
*
@@ -86,6 +88,7 @@ public final class PiscesRenderer {
8688
public PiscesRenderer(AbstractSurface surface) {
8789
this.surface = surface;
8890
initialize();
91+
Disposer.addRecord(this, new PiscesRendererDisposerRecord(nativePtr));
8992
}
9093

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

439-
protected void finalize() {
440-
this.nativeFinalize();
441-
}
442+
private static native void disposeNative(long nativeHandle);
442443

443-
/**
444-
* Native finalizer. Releases native memory used by PiscesRenderer at lifetime.
445-
*/
446-
private native void nativeFinalize();
444+
private static class PiscesRendererDisposerRecord implements Disposer.Record {
445+
private long nativeHandle;
446+
447+
PiscesRendererDisposerRecord(long nh) {
448+
nativeHandle = nh;
449+
}
450+
451+
@Override
452+
public void dispose() {
453+
if (nativeHandle != 0L) {
454+
disposeNative(nativeHandle);
455+
nativeHandle = 0L;
456+
}
457+
}
458+
}
447459
}

modules/javafx.graphics/src/main/native-prism-sw/JAbstractSurface.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,7 +37,6 @@ static jfieldID fieldIds[SURFACE_LAST + 1];
3737
static jboolean fieldIdsInitialized = JNI_FALSE;
3838

3939
static jboolean initializeSurfaceFieldIds(JNIEnv* env, jobject objectHandle);
40-
static void disposeNativeImpl(JNIEnv* env, jobject objectHandle);
4140

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

5453
JNIEXPORT void JNICALL
55-
Java_com_sun_pisces_AbstractSurface_nativeFinalize(JNIEnv* env,
56-
jobject objectHandle) {
57-
disposeNativeImpl(env, objectHandle);
54+
Java_com_sun_pisces_AbstractSurface_disposeNative(JNIEnv *env, jclass cls, jlong nativePtr)
55+
{
56+
AbstractSurface* surface = (AbstractSurface*) JLongToPointer(nativePtr);
57+
58+
if (surface != NULL) {
59+
surface->cleanup(surface);
60+
surface_dispose(&surface->super);
61+
}
5862
}
5963

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

185189
return retVal;
186190
}
187-
188-
static void
189-
disposeNativeImpl(JNIEnv* env, jobject objectHandle) {
190-
AbstractSurface* surface;
191-
192-
if (!fieldIdsInitialized) {
193-
return;
194-
}
195-
196-
surface = (AbstractSurface*)JLongToPointer(
197-
(*env)->GetLongField(env, objectHandle,
198-
fieldIds[SURFACE_NATIVE_PTR]));
199-
200-
if (surface != NULL) {
201-
surface->cleanup(surface);
202-
surface_dispose(&surface->super);
203-
(*env)->SetLongField(env, objectHandle, fieldIds[SURFACE_NATIVE_PTR],
204-
(jlong)0);
205-
206-
if (JNI_TRUE == readAndClearMemErrorFlag()) {
207-
JNI_ThrowNew(env, "java/lang/OutOfMemoryError",
208-
"Allocation of internal renderer buffer failed.");
209-
}
210-
}
211-
}
212-

modules/javafx.graphics/src/main/native-prism-sw/JPiscesRenderer.c

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,6 @@ static jboolean fieldIdsInitialized = JNI_FALSE;
4949
static jboolean initializeRendererFieldIds(JNIEnv *env, jobject objectHandle);
5050

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

8483
JNIEXPORT void JNICALL
85-
Java_com_sun_pisces_PiscesRenderer_nativeFinalize(JNIEnv* env,
86-
jobject objectHandle)
84+
Java_com_sun_pisces_PiscesRenderer_disposeNative(JNIEnv *env, jclass cls, jlong nativePtr)
8785
{
88-
renderer_finalize(env, objectHandle);
89-
90-
if (JNI_TRUE == readAndClearMemErrorFlag()) {
91-
JNI_ThrowNew(env, "java/lang/OutOfMemoryError",
92-
"Allocation of internal renderer buffer failed.");
86+
Renderer* rdr = (Renderer*) JLongToPointer(nativePtr);
87+
if (rdr != NULL) {
88+
renderer_dispose(rdr);
9389
}
9490
}
9591

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

296-
static void
297-
renderer_finalize(JNIEnv *env, jobject objectHandle) {
298-
Renderer* rdr;
299-
300-
if (!fieldIdsInitialized) {
301-
return;
302-
}
303-
304-
rdr = (Renderer*)JLongToPointer((*env)->GetLongField(env, objectHandle,
305-
fieldIds[RENDERER_NATIVE_PTR]));
306-
307-
if (rdr != (Renderer*)0) {
308-
renderer_dispose(rdr);
309-
(*env)->SetLongField(env, objectHandle, fieldIds[RENDERER_NATIVE_PTR],
310-
(jlong)0);
311-
}
312-
}
313-
314292
static jboolean
315293
initializeObjectFieldIds(JNIEnv *env,
316294
jobject objectHandle,

0 commit comments

Comments
 (0)