Skip to content
This repository has been archived by the owner on Feb 25, 2025. It is now read-only.

Use PorterDuff blend mode when below Android 29 #34800

Merged
merged 6 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Build;
Expand Down Expand Up @@ -308,7 +309,7 @@ public void draw(Canvas canvas) {
if (Build.VERSION.SDK_INT >= 29) {
surfaceCanvas.drawColor(Color.TRANSPARENT, BlendMode.CLEAR);
} else {
surfaceCanvas.drawColor(Color.TRANSPARENT);
surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These blocks can be simplified by using drawPaint and setting the color and blend mode on the paint. That's all the Android impl is doing here anyway

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You then won't have the branch here or need extra tests for different api levels

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to this method? https://developer.android.com/reference/android/graphics/Paint#setBlendMode(android.graphics.BlendMode).

Unfortunately, it also requires API 29.

Although, I agree that we don't need both methods. The PorterDuf blend mode isn't deprecated so I think we could just switch to that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ok, I didn't realize that. This looks good to me though.

}
super.draw(surfaceCanvas);
onFrameProduced();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.View;
Expand All @@ -26,7 +26,6 @@
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@TargetApi(31)
@RunWith(AndroidJUnit4.class)
public class PlatformViewWrapperTest {
private final Context ctx = ApplicationProvider.getApplicationContext();
Expand Down Expand Up @@ -120,6 +119,50 @@ public void draw(Canvas canvas) {
verifyNoMoreInteractions(canvas);
}

@Config(sdk = 28)
@Test
public void draw_clearsCanvasWithClearModeOnAndroidVersionsBelow29() {
final Surface surface = mock(Surface.class);
final PlatformViewWrapper wrapper =
new PlatformViewWrapper(ctx) {
@Override
protected Surface createSurface(@NonNull SurfaceTexture tx) {
return surface;
}
};

wrapper.addView(
new View(ctx) {
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.RED);
}
});

final int size = 100;
wrapper.measure(size, size);
wrapper.layout(0, 0, size, size);

final SurfaceTexture tx = mock(SurfaceTexture.class);
when(tx.isReleased()).thenReturn(false);

when(surface.lockHardwareCanvas()).thenReturn(mock(Canvas.class));

wrapper.setTexture(tx);

final Canvas canvas = mock(Canvas.class);
when(surface.lockHardwareCanvas()).thenReturn(canvas);
when(surface.isValid()).thenReturn(true);

// Test.
wrapper.invalidate();
wrapper.draw(new Canvas());

// Verify.
verify(canvas, times(1)).drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}

@Test
@Config(
shadows = {
Expand Down