Skip to content

Commit 2bf51b3

Browse files
Implement PlatformViewsController.createOverlaySurface (flutter#19226)
1 parent 0f49789 commit 2bf51b3

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import android.annotation.TargetApi;
1111
import android.content.Context;
12+
import android.graphics.PixelFormat;
13+
import android.hardware.HardwareBuffer;
14+
import android.media.ImageReader;
1215
import android.os.Build;
1316
import android.util.DisplayMetrics;
1417
import android.util.Log;
@@ -17,6 +20,7 @@
1720
import androidx.annotation.NonNull;
1821
import androidx.annotation.UiThread;
1922
import androidx.annotation.VisibleForTesting;
23+
import io.flutter.embedding.android.FlutterImageView;
2024
import io.flutter.embedding.engine.FlutterOverlaySurface;
2125
import io.flutter.embedding.engine.dart.DartExecutor;
2226
import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel;
@@ -71,6 +75,12 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
7175
// it is associated with(e.g if a platform view creates other views in the same virtual display.
7276
private final HashMap<Context, View> contextToPlatformView;
7377

78+
// Map of unique IDs to views that render overlay layers.
79+
private HashMap<Long, FlutterImageView> overlayLayerViews;
80+
81+
// Next available unique ID for use in overlayLayerViews;
82+
private long nextOverlayLayerId = 0;
83+
7484
private final PlatformViewsChannel.PlatformViewsHandler channelHandler =
7585
new PlatformViewsChannel.PlatformViewsHandler() {
7686
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@@ -283,6 +293,7 @@ public PlatformViewsController() {
283293
vdControllers = new HashMap<>();
284294
accessibilityEventsDelegate = new AccessibilityEventsDelegate();
285295
contextToPlatformView = new HashMap<>();
296+
overlayLayerViews = new HashMap<>();
286297
}
287298

288299
/**
@@ -552,7 +563,25 @@ public void onEndFrame() {
552563
}
553564

554565
public FlutterOverlaySurface createOverlaySurface() {
555-
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
556-
return null;
566+
ImageReader imageReader;
567+
if (android.os.Build.VERSION.SDK_INT >= 29) {
568+
imageReader =
569+
ImageReader.newInstance(
570+
flutterView.getWidth(),
571+
flutterView.getHeight(),
572+
PixelFormat.RGBA_8888,
573+
2,
574+
HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE | HardwareBuffer.USAGE_GPU_COLOR_OUTPUT);
575+
} else {
576+
imageReader =
577+
ImageReader.newInstance(
578+
flutterView.getWidth(), flutterView.getHeight(), PixelFormat.RGBA_8888, 2);
579+
}
580+
581+
FlutterImageView imageView = new FlutterImageView(flutterView.getContext(), imageReader);
582+
long id = nextOverlayLayerId++;
583+
overlayLayerViews.put(id, imageView);
584+
585+
return new FlutterOverlaySurface(id, imageReader.getSurface());
557586
}
558587
}

0 commit comments

Comments
 (0)