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     private long native_instance;
30 
31     /**
32      * Initialization step that should be called by subclasses in their
33      * constructors. Calling again may result in memory leaks.
34      * @hide
35      */
init(long ni)36     protected void init(long ni) {
37         native_instance = ni;
38     }
39 
40     private Matrix mLocalMatrix;
41 
42     public enum TileMode {
43         /**
44          * replicate the edge color if the shader draws outside of its
45          * original bounds
46          */
47         CLAMP   (0),
48         /**
49          * repeat the shader's image horizontally and vertically
50          */
51         REPEAT  (1),
52         /**
53          * repeat the shader's image horizontally and vertically, alternating
54          * mirror images so that adjacent images always seam
55          */
56         MIRROR  (2);
57 
TileMode(int nativeInt)58         TileMode(int nativeInt) {
59             this.nativeInt = nativeInt;
60         }
61         final int nativeInt;
62     }
63 
64     /**
65      * Return true if the shader has a non-identity local matrix.
66      * @param localM If not null, it is set to the shader's local matrix.
67      * @return true if the shader has a non-identity local matrix
68      */
getLocalMatrix(Matrix localM)69     public boolean getLocalMatrix(Matrix localM) {
70         if (mLocalMatrix != null) {
71             localM.set(mLocalMatrix);
72             return !mLocalMatrix.isIdentity();
73         }
74         return false;
75     }
76 
77     /**
78      * Set the shader's local matrix. Passing null will reset the shader's
79      * matrix to identity.
80      *
81      * @param localM The shader's new local matrix, or null to specify identity
82      */
setLocalMatrix(Matrix localM)83     public void setLocalMatrix(Matrix localM) {
84         mLocalMatrix = localM;
85         native_instance = nativeSetLocalMatrix(native_instance,
86                 localM == null ? 0 : localM.native_instance);
87     }
88 
finalize()89     protected void finalize() throws Throwable {
90         try {
91             super.finalize();
92         } finally {
93             nativeDestructor(native_instance);
94         }
95     }
96 
97     /**
98      * @hide
99      */
copy()100     protected Shader copy() {
101         final Shader copy = new Shader();
102         copyLocalMatrix(copy);
103         return copy;
104     }
105 
106     /**
107      * @hide
108      */
copyLocalMatrix(Shader dest)109     protected void copyLocalMatrix(Shader dest) {
110         if (mLocalMatrix != null) {
111             final Matrix lm = new Matrix();
112             getLocalMatrix(lm);
113             dest.setLocalMatrix(lm);
114         } else {
115             dest.setLocalMatrix(null);
116         }
117     }
118 
getNativeInstance()119     /* package */ long getNativeInstance() {
120         return native_instance;
121     }
122 
nativeDestructor(long native_shader)123     private static native void nativeDestructor(long native_shader);
nativeSetLocalMatrix(long native_shader, long matrix_instance)124     private static native long nativeSetLocalMatrix(long native_shader, long matrix_instance);
125 }
126