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