Skip to content

Commit

Permalink
Add a check for the surface if it is valid (flutter#55277)
Browse files Browse the repository at this point in the history
Fixes an issue where the Surface is not valid and the `draw` method is crashing.

flutter/flutter#155018

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
  • Loading branch information
mahmuttaskiran authored Sep 24, 2024
1 parent fbf8c46 commit 57ae2d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ public void draw(Canvas canvas) {
Log.e(TAG, "Platform view cannot be composed without a RenderTarget.");
return;
}

final Surface targetSurface = renderTarget.getSurface();
if (!targetSurface.isValid()) {
Log.e(TAG, "Platform view cannot be composed without a valid RenderTarget surface.");
return;
}

final Canvas targetCanvas = targetSurface.lockHardwareCanvas();
if (targetCanvas == null) {
// Cannot render right now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.Surface;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
Expand All @@ -23,6 +24,7 @@
import android.widget.FrameLayout;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -63,6 +65,37 @@ public void onDraw(Canvas canvas) {
verify(canvas, times(1)).drawColor(Color.RED);
}

@Test
public void draw_withoutValidSurface() {
FlutterRenderer.debugDisableSurfaceClear = true;
final Surface surface = mock(Surface.class);
when(surface.isValid()).thenReturn(false);
final PlatformViewRenderTarget renderTarget = mock(PlatformViewRenderTarget.class);
when(renderTarget.getSurface()).thenReturn(surface);

final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget);
final Canvas canvas = mock(Canvas.class);
wrapper.draw(canvas);

verify(canvas, times(0)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
}

@Test
public void draw_withValidSurface() {
FlutterRenderer.debugDisableSurfaceClear = true;
final Canvas canvas = mock(Canvas.class);
final Surface surface = mock(Surface.class);
when(surface.isValid()).thenReturn(true);
final PlatformViewRenderTarget renderTarget = mock(PlatformViewRenderTarget.class);
when(renderTarget.getSurface()).thenReturn(surface);
when(surface.lockHardwareCanvas()).thenReturn(canvas);
final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget);

wrapper.draw(canvas);

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

@Test
public void focusChangeListener_hasFocus() {
final ViewTreeObserver viewTreeObserver = mock(ViewTreeObserver.class);
Expand Down

0 comments on commit 57ae2d2

Please sign in to comment.