Home | History | Annotate | Download | only in graphics
      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 = new NativeAllocationRegistry(
     30                 ColorFilter.class.getClassLoader(), nativeGetFinalizer(), 50);
     31     }
     32 
     33     /**
     34      * @deprecated Use subclass constructors directly instead.
     35      */
     36     @Deprecated
     37     public ColorFilter() {}
     38 
     39     /**
     40      * Current native SkColorFilter instance.
     41      */
     42     private long mNativeInstance;
     43     // Runnable to do immediate destruction
     44     private Runnable mCleaner;
     45 
     46     long createNativeInstance() {
     47         return 0;
     48     }
     49 
     50     void discardNativeInstance() {
     51         if (mNativeInstance != 0) {
     52             mCleaner.run();
     53             mCleaner = null;
     54             mNativeInstance = 0;
     55         }
     56     }
     57 
     58     /** @hide */
     59     public long getNativeInstance() {
     60         if (mNativeInstance == 0) {
     61             mNativeInstance = createNativeInstance();
     62 
     63             if (mNativeInstance != 0) {
     64                 // Note: we must check for null here, since it's possible for createNativeInstance()
     65                 // to return nullptr if the native SkColorFilter would be a no-op at draw time.
     66                 // See native implementations of subclass create methods for more info.
     67                 mCleaner = NoImagePreloadHolder.sRegistry.registerNativeAllocation(
     68                         this, mNativeInstance);
     69             }
     70         }
     71         return mNativeInstance;
     72 
     73     }
     74 
     75     private static native long nativeGetFinalizer();
     76 }
     77