Use NativeAllocationRegistry for ColorFilter

Bug: 62994689
Test: bit CtsGraphicsTestCases:*

Change-Id: Icea01fa7d4c6e78f3a93434de64bc8ddfe0c7a0e
This commit is contained in:
Chris Craik
2017-06-26 14:03:01 -07:00
parent 5345fce58f
commit f65e92d9f9
2 changed files with 29 additions and 28 deletions

View File

@@ -30,9 +30,12 @@ using namespace uirenderer;
class SkColorFilterGlue {
public:
static void SafeUnref(JNIEnv* env, jobject clazz, jlong skFilterHandle) {
SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(skFilterHandle);
SkSafeUnref(filter);
static void SafeUnref(SkShader* shader) {
SkSafeUnref(shader);
}
static jlong GetNativeFinalizer(JNIEnv*, jobject) {
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&SafeUnref));
}
static jlong CreatePorterDuffFilter(JNIEnv* env, jobject, jint srcColor, jint modeHandle) {
@@ -57,7 +60,7 @@ public:
};
static const JNINativeMethod colorfilter_methods[] = {
{"nSafeUnref", "(J)V", (void*) SkColorFilterGlue::SafeUnref}
{"nativeGetFinalizer", "()J", (void*) SkColorFilterGlue::GetNativeFinalizer }
};
static const JNINativeMethod porterduff_methods[] = {

View File

@@ -14,19 +14,22 @@
* limitations under the License.
*/
// This file was generated from the C++ include file: SkColorFilter.h
// Any changes made to this file will be discarded by the build.
// To change this file, either edit the include, or device/tools/gluemaker/main.cpp,
// or one of the auxilary file specifications in device/tools/gluemaker.
package android.graphics;
import libcore.util.NativeAllocationRegistry;
/**
* A color filter can be used with a {@link Paint} to modify the color of
* each pixel drawn with that paint. This is an abstract class that should
* never be used directly.
*/
public class ColorFilter {
private static class NoImagePreloadHolder {
public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
ColorFilter.class.getClassLoader(), nativeGetFinalizer(), 50);
}
/**
* @deprecated Use subclass constructors directly instead.
*/
@@ -34,9 +37,11 @@ public class ColorFilter {
public ColorFilter() {}
/**
* Holds the pointer to the native SkColorFilter instance.
* Current native SkColorFilter instance.
*/
private long mNativeInstance;
// Runnable to do immediate destruction
private Runnable mCleaner;
long createNativeInstance() {
return 0;
@@ -44,35 +49,28 @@ public class ColorFilter {
void discardNativeInstance() {
if (mNativeInstance != 0) {
nSafeUnref(mNativeInstance);
mCleaner.run();
mCleaner = null;
mNativeInstance = 0;
}
}
@Override
protected void finalize() throws Throwable {
try {
if (mNativeInstance != 0) {
nSafeUnref(mNativeInstance);
}
mNativeInstance = -1;
} finally {
super.finalize();
}
}
/** @hide */
public long getNativeInstance() {
if (mNativeInstance == -1) {
throw new IllegalStateException("attempting to use a finalized ColorFilter");
}
if (mNativeInstance == 0) {
mNativeInstance = createNativeInstance();
if (mNativeInstance != 0) {
// Note: we must check for null here, since it's possible for createNativeInstance()
// to return nullptr if the native SkColorFilter would be a no-op at draw time.
// See native implementations of subclass create methods for more info.
mCleaner = NoImagePreloadHolder.sRegistry.registerNativeAllocation(
this, mNativeInstance);
}
}
return mNativeInstance;
}
static native void nSafeUnref(long native_instance);
private static native long nativeGetFinalizer();
}