diff options
| author | 2017-06-26 14:03:01 -0700 | |
|---|---|---|
| committer | 2017-06-26 21:33:47 +0000 | |
| commit | f65e92d9f96cabb23fd8afa43e0580ea616ea154 (patch) | |
| tree | 774771ddd3e219aba20c4190025425bbbcdf6555 | |
| parent | 5345fce58ff35b29e8e7264a2e36c9c990aedcb1 (diff) | |
Use NativeAllocationRegistry for ColorFilter
Bug: 62994689
Test: bit CtsGraphicsTestCases:*
Change-Id: Icea01fa7d4c6e78f3a93434de64bc8ddfe0c7a0e
| -rw-r--r-- | core/jni/android/graphics/ColorFilter.cpp | 11 | ||||
| -rw-r--r-- | graphics/java/android/graphics/ColorFilter.java | 46 |
2 files changed, 29 insertions, 28 deletions
diff --git a/core/jni/android/graphics/ColorFilter.cpp b/core/jni/android/graphics/ColorFilter.cpp index 5553a3ed22c5..4b6578bdff7f 100644 --- a/core/jni/android/graphics/ColorFilter.cpp +++ b/core/jni/android/graphics/ColorFilter.cpp @@ -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[] = { diff --git a/graphics/java/android/graphics/ColorFilter.java b/graphics/java/android/graphics/ColorFilter.java index 0ca3729dcc0e..b24b9885d1b0 100644 --- a/graphics/java/android/graphics/ColorFilter.java +++ b/graphics/java/android/graphics/ColorFilter.java @@ -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(); } |