1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics;
18 
19 import libcore.util.NativeAllocationRegistry;
20 
21 /**
22  * A color filter can be used with a {@link Paint} to modify the color of
23  * each pixel drawn with that paint. This is an abstract class that should
24  * never be used directly.
25  */
26 public class ColorFilter {
27 
28     private static class NoImagePreloadHolder {
29         public static final NativeAllocationRegistry sRegistry =
30                 NativeAllocationRegistry.createMalloced(
31                 ColorFilter.class.getClassLoader(), nativeGetFinalizer());
32     }
33 
34     /**
35      * @deprecated Use subclass constructors directly instead.
36      */
37     @Deprecated
ColorFilter()38     public ColorFilter() {}
39 
40     /**
41      * Current native SkColorFilter instance.
42      */
43     private long mNativeInstance;
44     // Runnable to do immediate destruction
45     private Runnable mCleaner;
46 
createNativeInstance()47     long createNativeInstance() {
48         return 0;
49     }
50 
discardNativeInstance()51     synchronized final void discardNativeInstance() {
52         if (mNativeInstance != 0) {
53             mCleaner.run();
54             mCleaner = null;
55             mNativeInstance = 0;
56         }
57     }
58 
59     /** @hide */
getNativeInstance()60     public synchronized final long getNativeInstance() {
61         if (mNativeInstance == 0) {
62             mNativeInstance = createNativeInstance();
63 
64             if (mNativeInstance != 0) {
65                 // Note: we must check for null here, since it's possible for createNativeInstance()
66                 // to return nullptr if the native SkColorFilter would be a no-op at draw time.
67                 // See native implementations of subclass create methods for more info.
68                 mCleaner = NoImagePreloadHolder.sRegistry.registerNativeAllocation(
69                         this, mNativeInstance);
70             }
71         }
72         return mNativeInstance;
73 
74     }
75 
nativeGetFinalizer()76     private static native long nativeGetFinalizer();
77 }
78