summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDeepanshu Gupta <deepanshu@google.com>2014-06-25 02:49:48 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2014-06-25 02:49:48 +0000
commitbaffd0f85b76382b346c18b0eedb0bc9d2766c49 (patch)
treec178da643631059644c6db917659bc1669c4d7a4 /tools
parentca87e9e11e1965a656c05a839bc53b0fa2e81acf (diff)
parent48d41ea0bee27147273fec6d753332c181861c77 (diff)
downloadframeworks_base-baffd0f85b76382b346c18b0eedb0bc9d2766c49.zip
frameworks_base-baffd0f85b76382b346c18b0eedb0bc9d2766c49.tar.gz
frameworks_base-baffd0f85b76382b346c18b0eedb0bc9d2766c49.tar.bz2
am ac626a67: Merge "Improve PorterDuff support." into lmp-preview-dev
* commit 'ac626a6724bc239fcf3d5b2e207de02c59595c5a': Improve PorterDuff support.
Diffstat (limited to 'tools')
-rw-r--r--tools/layoutlib/bridge/src/android/graphics/PorterDuffColorFilter_Delegate.java83
1 files changed, 50 insertions, 33 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/PorterDuffColorFilter_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/PorterDuffColorFilter_Delegate.java
index c44e03b..ee90595 100644
--- a/tools/layoutlib/bridge/src/android/graphics/PorterDuffColorFilter_Delegate.java
+++ b/tools/layoutlib/bridge/src/android/graphics/PorterDuffColorFilter_Delegate.java
@@ -51,25 +51,13 @@ public class PorterDuffColorFilter_Delegate extends ColorFilter_Delegate {
private final int mSrcColor;
private final Mode mMode;
- private int mWidth;
- private int mHeight;
- private BufferedImage mImage;
// ---- Public Helper methods ----
@Override
public boolean isSupported() {
- switch (mMode) {
- case CLEAR:
- case SRC:
- case SRC_IN:
- case DST_IN:
- case SRC_ATOP:
- return true;
- }
-
- return false;
+ return getAlphaCompositeRule(mMode) != -1;
}
@Override
@@ -79,9 +67,9 @@ public class PorterDuffColorFilter_Delegate extends ColorFilter_Delegate {
@Override
public void applyFilter(Graphics2D g, int width, int height) {
- createFilterImage(width, height);
+ BufferedImage image = createFilterImage(width, height);
g.setComposite(getComposite());
- g.drawImage(mImage, 0, 0, null);
+ g.drawImage(image, 0, 0, null);
}
// ---- native methods ----
@@ -98,36 +86,65 @@ public class PorterDuffColorFilter_Delegate extends ColorFilter_Delegate {
private PorterDuffColorFilter_Delegate(int srcColor, int mode) {
mSrcColor = srcColor;
- // Temporarily change multiply to SRC_IN to render menus.
- // TODO: support Mode.MULTIPLY
- if (mode == Mode.MULTIPLY.nativeInt) {
- mode = Mode.SRC_IN.nativeInt;
- }
- mMode = getPorterDuffMode(mode);
+ mMode = getCompatibleMode(getPorterDuffMode(mode));
}
- private void createFilterImage(int width, int height) {
- if (mWidth == width && mHeight == height && mImage != null) {
- return;
- }
- mWidth = width;
- mHeight = height;
- mImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- Graphics2D graphics = mImage.createGraphics();
+ private BufferedImage createFilterImage(int width, int height) {
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D graphics = image.createGraphics();
try {
graphics.setColor(new java.awt.Color(mSrcColor, true /* hasAlpha */));
graphics.fillRect(0, 0, width, height);
} finally {
graphics.dispose();
}
+ return image;
}
private AlphaComposite getComposite() {
- return AlphaComposite.getInstance(getAlphaCompositeRule(mMode),
- getAlpha() / 255f);
+ return AlphaComposite.getInstance(getAlphaCompositeRule(mMode));
}
- private int getAlpha() {
- return mSrcColor >>> 24;
+ // For filtering the colors, the src image should contain the "color" only for pixel values
+ // which are not transparent in the target image. But, we are using a simple rectangular image
+ // completely filled with color. Hence some AlphaComposite rules do not apply as intended.
+ // However, in such cases, they can usually be mapped to some other mode, which produces an
+ // equivalent result.
+ private Mode getCompatibleMode(Mode mode) {
+ Mode m = mode;
+ switch (mode) {
+ // Modes that are directly supported.
+ case CLEAR:
+ case DST:
+ case SRC_IN:
+ case DST_IN:
+ case DST_OUT:
+ case SRC_ATOP:
+ break;
+ // Modes that can be mapped to one of the supported modes.
+ case SRC:
+ m = Mode.SRC_IN;
+ break;
+ case SRC_OVER:
+ m = Mode.SRC_ATOP;
+ break;
+ case DST_OVER:
+ m = Mode.DST;
+ break;
+ case SRC_OUT:
+ m = Mode.CLEAR;
+ break;
+ case DST_ATOP:
+ m = Mode.DST_IN;
+ break;
+ case XOR:
+ m = Mode.DST_OUT;
+ break;
+ // This mode is not supported, but used by Action Bar Overflow Popup Menus. We map this
+ // to the closest supported mode, to prevent showing excessive warnings to the user.
+ case MULTIPLY:
+ m = Mode.SRC_IN;
+ }
+ return m;
}
}