1/*
2 * Copyright (C) 2007 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/** A subclass of shader that returns the composition of two other shaders, combined by
20    an {@link android.graphics.Xfermode} subclass.
21*/
22public class ComposeShader extends Shader {
23
24    private static final int TYPE_XFERMODE = 1;
25    private static final int TYPE_PORTERDUFFMODE = 2;
26
27    /**
28     * Type of the ComposeShader: can be either TYPE_XFERMODE or TYPE_PORTERDUFFMODE
29     */
30    private int mType;
31
32    private Xfermode mXferMode;
33    private PorterDuff.Mode mPorterDuffMode;
34
35    /**
36     * Hold onto the shaders to avoid GC.
37     */
38    @SuppressWarnings({"UnusedDeclaration"})
39    private final Shader mShaderA;
40    @SuppressWarnings({"UnusedDeclaration"})
41    private final Shader mShaderB;
42
43    /** Create a new compose shader, given shaders A, B, and a combining mode.
44        When the mode is applied, it will be given the result from shader A as its
45        "dst", and the result from shader B as its "src".
46        @param shaderA  The colors from this shader are seen as the "dst" by the mode
47        @param shaderB  The colors from this shader are seen as the "src" by the mode
48        @param mode     The mode that combines the colors from the two shaders. If mode
49                        is null, then SRC_OVER is assumed.
50    */
51    public ComposeShader(Shader shaderA, Shader shaderB, Xfermode mode) {
52        mType = TYPE_XFERMODE;
53        mShaderA = shaderA;
54        mShaderB = shaderB;
55        mXferMode = mode;
56        init(nativeCreate1(shaderA.getNativeInstance(), shaderB.getNativeInstance(),
57                (mode != null) ? mode.native_instance : 0));
58    }
59
60    /** Create a new compose shader, given shaders A, B, and a combining PorterDuff mode.
61        When the mode is applied, it will be given the result from shader A as its
62        "dst", and the result from shader B as its "src".
63        @param shaderA  The colors from this shader are seen as the "dst" by the mode
64        @param shaderB  The colors from this shader are seen as the "src" by the mode
65        @param mode     The PorterDuff mode that combines the colors from the two shaders.
66    */
67    public ComposeShader(Shader shaderA, Shader shaderB, PorterDuff.Mode mode) {
68        mType = TYPE_PORTERDUFFMODE;
69        mShaderA = shaderA;
70        mShaderB = shaderB;
71        mPorterDuffMode = mode;
72        init(nativeCreate2(shaderA.getNativeInstance(), shaderB.getNativeInstance(),
73                mode.nativeInt));
74    }
75
76    /**
77     * @hide
78     */
79    @Override
80    protected Shader copy() {
81        final ComposeShader copy;
82        switch (mType) {
83            case TYPE_XFERMODE:
84                copy = new ComposeShader(mShaderA.copy(), mShaderB.copy(), mXferMode);
85                break;
86            case TYPE_PORTERDUFFMODE:
87                copy = new ComposeShader(mShaderA.copy(), mShaderB.copy(), mPorterDuffMode);
88                break;
89            default:
90                throw new IllegalArgumentException(
91                        "ComposeShader should be created with either Xfermode or PorterDuffMode");
92        }
93        copyLocalMatrix(copy);
94        return copy;
95    }
96
97    private static native long nativeCreate1(long native_shaderA, long native_shaderB,
98            long native_mode);
99    private static native long nativeCreate2(long native_shaderA, long native_shaderB,
100            int porterDuffMode);
101}
102