Shader.java revision 06f96e2652e4855b6520ad9dd70583677605b79a
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
17package 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 */
25public 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    public enum TileMode {
38        /**
39         * replicate the edge color if the shader draws outside of its
40         * original bounds
41         */
42        CLAMP   (0),
43        /**
44         * repeat the shader's image horizontally and vertically
45         */
46        REPEAT  (1),
47        /**
48         * repeat the shader's image horizontally and vertically, alternating
49         * mirror images so that adjacent images always seam
50         */
51        MIRROR  (2);
52
53        TileMode(int nativeInt) {
54            this.nativeInt = nativeInt;
55        }
56        final int nativeInt;
57    }
58
59    /**
60     * Return true if the shader has a non-identity local matrix.
61     * @param localM If not null, it is set to the shader's local matrix.
62     * @return true if the shader has a non-identity local matrix
63     */
64    public boolean getLocalMatrix(Matrix localM) {
65        return nativeGetLocalMatrix(native_instance, localM.native_instance);
66    }
67
68    /**
69     * Set the shader's local matrix. Passing null will reset the shader's
70     * matrix to identity
71     * @param localM The shader's new local matrix, or null to specify identity
72     */
73    public void setLocalMatrix(Matrix localM) {
74        nativeSetLocalMatrix(native_instance, native_shader, localM.native_instance);
75    }
76
77    protected void finalize() throws Throwable {
78        try {
79            super.finalize();
80        } finally {
81            nativeDestructor(native_instance, native_shader);
82        }
83    }
84
85    private static native void nativeDestructor(int native_shader, int native_skiaShader);
86    private static native boolean nativeGetLocalMatrix(int native_shader,
87            int matrix_instance);
88    private static native void nativeSetLocalMatrix(int native_shader,
89            int native_skiaShader, int matrix_instance);
90}
91