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 /**
     20  * Shader is the based class for objects that return horizontal spans of colors
     21  * during drawing. A subclass of Shader is installed in a Paint calling
     22  * paint.setShader(shader). After that any object (other than a bitmap) that is
     23  * drawn with that paint will get its color(s) from the shader.
     24  */
     25 public class Shader {
     26     /**
     27      * This is set by subclasses, but don't make it public.
     28      *
     29      * @hide
     30      */
     31     public int native_instance;
     32     /**
     33      * @hide
     34      */
     35     public int native_shader;
     36 
     37     private Matrix mLocalMatrix;
     38 
     39     public enum TileMode {
     40         /**
     41          * replicate the edge color if the shader draws outside of its
     42          * original bounds
     43          */
     44         CLAMP   (0),
     45         /**
     46          * repeat the shader's image horizontally and vertically
     47          */
     48         REPEAT  (1),
     49         /**
     50          * repeat the shader's image horizontally and vertically, alternating
     51          * mirror images so that adjacent images always seam
     52          */
     53         MIRROR  (2);
     54 
     55         TileMode(int nativeInt) {
     56             this.nativeInt = nativeInt;
     57         }
     58         final int nativeInt;
     59     }
     60 
     61     /**
     62      * Return true if the shader has a non-identity local matrix.
     63      * @param localM If not null, it is set to the shader's local matrix.
     64      * @return true if the shader has a non-identity local matrix
     65      */
     66     public boolean getLocalMatrix(Matrix localM) {
     67         if (mLocalMatrix != null) {
     68             localM.set(mLocalMatrix);
     69             return !mLocalMatrix.isIdentity();
     70         }
     71         return false;
     72     }
     73 
     74     /**
     75      * Set the shader's local matrix. Passing null will reset the shader's
     76      * matrix to identity
     77      * @param localM The shader's new local matrix, or null to specify identity
     78      */
     79     public void setLocalMatrix(Matrix localM) {
     80         mLocalMatrix = localM;
     81         nativeSetLocalMatrix(native_instance, native_shader,
     82                 localM == null ? 0 : localM.native_instance);
     83     }
     84 
     85     protected void finalize() throws Throwable {
     86         try {
     87             super.finalize();
     88         } finally {
     89             nativeDestructor(native_instance, native_shader);
     90         }
     91     }
     92 
     93     /**
     94      * @hide
     95      */
     96     protected Shader copy() {
     97         final Shader copy = new Shader();
     98         copyLocalMatrix(copy);
     99         return copy;
    100     }
    101 
    102     /**
    103      * @hide
    104      */
    105     protected void copyLocalMatrix(Shader dest) {
    106         if (mLocalMatrix != null) {
    107             final Matrix lm = new Matrix();
    108             getLocalMatrix(lm);
    109             dest.setLocalMatrix(lm);
    110         } else {
    111             dest.setLocalMatrix(null);
    112         }
    113     }
    114 
    115     private static native void nativeDestructor(int native_shader, int native_skiaShader);
    116     private static native void nativeSetLocalMatrix(int native_shader,
    117             int native_skiaShader, int matrix_instance);
    118 }
    119