PorterDuffXfermode_Delegate.java revision b1da1afa7418960b650780250bbd34c81af61aa3
1/*
2 * Copyright (C) 2010 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
19import com.android.layoutlib.bridge.Bridge;
20import com.android.layoutlib.bridge.impl.DelegateManager;
21
22import java.awt.AlphaComposite;
23import java.awt.Composite;
24
25/**
26 * Delegate implementing the native methods of android.graphics.PorterDuffXfermode
27 *
28 * Through the layoutlib_create tool, the original native methods of PorterDuffXfermode have been
29 * replaced by calls to methods of the same name in this delegate class.
30 *
31 * This class behaves like the original native implementation, but in Java, keeping previously
32 * native data into its own objects and mapping them to int that are sent back and forth between
33 * it and the original PorterDuffXfermode class.
34 *
35 * Because this extends {@link Xfermode_Delegate}, there's no need to use a
36 * {@link DelegateManager}, as all the PathEffect classes will be added to the manager owned by
37 * {@link Xfermode_Delegate}.
38 *
39 */
40public class PorterDuffXfermode_Delegate extends Xfermode_Delegate {
41
42    // ---- delegate data ----
43
44    private final int mMode;
45
46    // ---- Public Helper methods ----
47
48    public PorterDuff.Mode getMode() {
49        return getPorterDuffMode(mMode);
50    }
51
52    @Override
53    public Composite getComposite(int alpha) {
54        return getComposite(getPorterDuffMode(mMode), alpha);
55    }
56
57    @Override
58    public boolean isSupported() {
59        return true;
60    }
61
62    @Override
63    public String getSupportMessage() {
64        // no message since isSupported returns true;
65        return null;
66    }
67
68    public static PorterDuff.Mode getPorterDuffMode(int mode) {
69        for (PorterDuff.Mode m : PorterDuff.Mode.values()) {
70            if (m.nativeInt == mode) {
71                return m;
72            }
73        }
74
75        Bridge.getLog().error(null, String.format("Unknown PorterDuff.Mode: %d", mode));
76        assert false;
77        return PorterDuff.Mode.SRC_OVER;
78    }
79
80    public static Composite getComposite(PorterDuff.Mode mode, int alpha) {
81        float falpha = alpha != 0xFF ? (float)alpha / 255.f : 1.f;
82        switch (mode) {
83            case CLEAR:
84                return AlphaComposite.getInstance(AlphaComposite.CLEAR, falpha);
85            case DARKEN:
86                break;
87            case DST:
88                return AlphaComposite.getInstance(AlphaComposite.DST, falpha);
89            case DST_ATOP:
90                return AlphaComposite.getInstance(AlphaComposite.DST_ATOP, falpha);
91            case DST_IN:
92                return AlphaComposite.getInstance(AlphaComposite.DST_IN, falpha);
93            case DST_OUT:
94                return AlphaComposite.getInstance(AlphaComposite.DST_OUT, falpha);
95            case DST_OVER:
96                return AlphaComposite.getInstance(AlphaComposite.DST_OVER, falpha);
97            case LIGHTEN:
98                break;
99            case MULTIPLY:
100                break;
101            case SCREEN:
102                break;
103            case SRC:
104                return AlphaComposite.getInstance(AlphaComposite.SRC, falpha);
105            case SRC_ATOP:
106                return AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, falpha);
107            case SRC_IN:
108                return AlphaComposite.getInstance(AlphaComposite.SRC_IN, falpha);
109            case SRC_OUT:
110                return AlphaComposite.getInstance(AlphaComposite.SRC_OUT, falpha);
111            case SRC_OVER:
112                return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, falpha);
113            case XOR:
114                return AlphaComposite.getInstance(AlphaComposite.XOR, falpha);
115        }
116
117        Bridge.getLog().fidelityWarning(null,
118                String.format("Unsupported PorterDuff Mode: %s", mode.name()),
119                null);
120
121        return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, falpha);
122    }
123
124    // ---- native methods ----
125
126    /*package*/ static int nativeCreateXfermode(int mode) {
127        PorterDuffXfermode_Delegate newDelegate = new PorterDuffXfermode_Delegate(mode);
128        return sManager.addDelegate(newDelegate);
129    }
130
131    // ---- Private delegate/helper methods ----
132
133    private PorterDuffXfermode_Delegate(int mode) {
134        mMode = mode;
135    }
136}
137