Skip to content

Commit 59aad78

Browse files
author
Xavier Ducrohet
committed
LayoutLib: don't render when shader's local matrix is set to 0 scale.
Change-Id: I7726d87f3dd0475ac662f535a08c6435b8b9ed1f
1 parent 8a18dba commit 59aad78

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

tools/layoutlib/bridge/src/android/graphics/Shader_Delegate.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public static TileMode getTileMode(int tileMode) {
7373
public abstract boolean isSupported();
7474
public abstract String getSupportMessage();
7575

76+
public boolean isValid() {
77+
if (mLocalMatrix != null && mLocalMatrix.getAffineTransform().getDeterminant() == 0) {
78+
return false;
79+
}
80+
81+
return true;
82+
}
83+
7684
// ---- native methods ----
7785

7886
@LayoutlibDelegate
@@ -126,5 +134,4 @@ protected java.awt.geom.AffineTransform getLocalMatrix() {
126134

127135
return new java.awt.geom.AffineTransform();
128136
}
129-
130137
}

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/GcSnapshot.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,13 @@ private void drawInLayer(Layer layer, Drawable drawable, Paint_Delegate paint,
603603
createCustomGraphics(originalGraphics, paint, compositeOnly, forceSrcMode) :
604604
(Graphics2D) originalGraphics.create();
605605

606-
try {
607-
drawable.draw(configuredGraphics2D, paint);
608-
} finally {
609-
// dispose Graphics2D object
610-
configuredGraphics2D.dispose();
606+
if (configuredGraphics2D != null) {
607+
try {
608+
drawable.draw(configuredGraphics2D, paint);
609+
} finally {
610+
// dispose Graphics2D object
611+
configuredGraphics2D.dispose();
612+
}
611613
}
612614
}
613615

@@ -680,11 +682,13 @@ private void restoreLayer(Layer dstLayer) {
680682
Graphics2D g = createCustomGraphics(baseGfx, mLocalLayerPaint,
681683
true /*alphaOnly*/, false /*forceSrcMode*/);
682684

683-
g.drawImage(mLocalLayer.getImage(),
684-
mLayerBounds.left, mLayerBounds.top, mLayerBounds.right, mLayerBounds.bottom,
685-
mLayerBounds.left, mLayerBounds.top, mLayerBounds.right, mLayerBounds.bottom,
686-
null);
687-
g.dispose();
685+
if (g != null) {
686+
g.drawImage(mLocalLayer.getImage(),
687+
mLayerBounds.left, mLayerBounds.top, mLayerBounds.right, mLayerBounds.bottom,
688+
mLayerBounds.left, mLayerBounds.top, mLayerBounds.right, mLayerBounds.bottom,
689+
null);
690+
g.dispose();
691+
}
688692

689693
baseGfx.dispose();
690694
}
@@ -714,11 +718,17 @@ private Graphics2D createCustomGraphics(Graphics2D original, Paint_Delegate pain
714718
Shader_Delegate shaderDelegate = paint.getShader();
715719
if (shaderDelegate != null) {
716720
if (shaderDelegate.isSupported()) {
717-
java.awt.Paint shaderPaint = shaderDelegate.getJavaPaint();
718-
assert shaderPaint != null;
719-
if (shaderPaint != null) {
720-
g.setPaint(shaderPaint);
721-
customShader = true;
721+
// shader could have a local matrix that's not valid (for instance 0 scaling).
722+
if (shaderDelegate.isValid()) {
723+
java.awt.Paint shaderPaint = shaderDelegate.getJavaPaint();
724+
assert shaderPaint != null;
725+
if (shaderPaint != null) {
726+
g.setPaint(shaderPaint);
727+
customShader = true;
728+
}
729+
} else {
730+
g.dispose();
731+
return null;
722732
}
723733
} else {
724734
Bridge.getLog().fidelityWarning(LayoutLog.TAG_SHADER,
@@ -742,7 +752,7 @@ private Graphics2D createCustomGraphics(Graphics2D original, Paint_Delegate pain
742752

743753
if (forceSrcMode) {
744754
g.setComposite(AlphaComposite.getInstance(
745-
AlphaComposite.SRC, (float) alpha / 255.f));
755+
AlphaComposite.SRC, alpha / 255.f));
746756
} else {
747757
boolean customXfermode = false;
748758
Xfermode_Delegate xfermodeDelegate = paint.getXfermode();
@@ -766,7 +776,7 @@ private Graphics2D createCustomGraphics(Graphics2D original, Paint_Delegate pain
766776
// that will handle the alpha.
767777
if (customXfermode == false && alpha != 0xFF) {
768778
g.setComposite(AlphaComposite.getInstance(
769-
AlphaComposite.SRC_OVER, (float) alpha / 255.f));
779+
AlphaComposite.SRC_OVER, alpha / 255.f));
770780
}
771781
}
772782

0 commit comments

Comments
 (0)