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 coposition of two other shaders, combined by
20    an {@link android.graphics.Xfermode} subclass.
21*/
22public class ComposeShader extends Shader {
23    /**
24     * Hold onto the shaders to avoid GC.
25     */
26    @SuppressWarnings({"UnusedDeclaration"})
27    private final Shader mShaderA;
28    @SuppressWarnings({"UnusedDeclaration"})
29    private final Shader mShaderB;
30
31    /** Create a new compose shader, given shaders A, B, and a combining mode.
32        When the mode is applied, it will be given the result from shader A as its
33        "dst", and the result from shader B as its "src".
34        @param shaderA  The colors from this shader are seen as the "dst" by the mode
35        @param shaderB  The colors from this shader are seen as the "src" by the mode
36        @param mode     The mode that combines the colors from the two shaders. If mode
37                        is null, then SRC_OVER is assumed.
38    */
39    public ComposeShader(Shader shaderA, Shader shaderB, Xfermode mode) {
40        mShaderA = shaderA;
41        mShaderB = shaderB;
42        native_instance = nativeCreate1(shaderA.native_instance, shaderB.native_instance,
43                (mode != null) ? mode.native_instance : 0);
44        if (mode instanceof PorterDuffXfermode) {
45            PorterDuff.Mode pdMode = ((PorterDuffXfermode) mode).mode;
46            native_shader = nativePostCreate2(native_instance, shaderA.native_shader,
47                    shaderB.native_shader, pdMode != null ? pdMode.nativeInt : 0);
48        } else {
49            native_shader = nativePostCreate1(native_instance, shaderA.native_shader,
50                    shaderB.native_shader, mode != null ? mode.native_instance : 0);
51        }
52    }
53
54    /** Create a new compose shader, given shaders A, B, and a combining PorterDuff mode.
55        When the mode is applied, it will be given the result from shader A as its
56        "dst", and the result from shader B as its "src".
57        @param shaderA  The colors from this shader are seen as the "dst" by the mode
58        @param shaderB  The colors from this shader are seen as the "src" by the mode
59        @param mode     The PorterDuff mode that combines the colors from the two shaders.
60    */
61    public ComposeShader(Shader shaderA, Shader shaderB, PorterDuff.Mode mode) {
62        mShaderA = shaderA;
63        mShaderB = shaderB;
64        native_instance = nativeCreate2(shaderA.native_instance, shaderB.native_instance,
65                mode.nativeInt);
66        native_shader = nativePostCreate2(native_instance, shaderA.native_shader,
67                shaderB.native_shader, mode.nativeInt);
68    }
69
70    private static native int nativeCreate1(int native_shaderA, int native_shaderB,
71            int native_mode);
72    private static native int nativeCreate2(int native_shaderA, int native_shaderB,
73            int porterDuffMode);
74    private static native int nativePostCreate1(int native_shader, int native_skiaShaderA,
75            int native_skiaShaderB, int native_mode);
76    private static native int nativePostCreate2(int native_shader, int native_skiaShaderA,
77            int native_skiaShaderB, int porterDuffMode);
78}
79