Position overlay layer views in PlatformViewsController.onDisplayOverlaySurface (flutter/engine#19295)
This commit is contained in:
@@ -954,18 +954,12 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC
|
||||
|
||||
removeAllViews();
|
||||
addView(flutterImageView);
|
||||
}
|
||||
|
||||
// TODO(jsimmons): this is a temporary hack that schedules a redraw of the FlutterImageView
|
||||
// at a time when the engine has presumably posted a frame. Remove this when
|
||||
// PlatformViewsController.onEndFrame callbacks have been implemented.
|
||||
postDelayed(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
flutterImageView.acquireLatestImage();
|
||||
flutterImageView.invalidate();
|
||||
}
|
||||
},
|
||||
1000);
|
||||
public void acquireLatestImageViewFrame() {
|
||||
if (flutterImageView != null) {
|
||||
flutterImageView.acquireLatestImage();
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if this {@code FlutterView} is currently attached to a {@link FlutterEngine}. */
|
||||
|
||||
@@ -12,14 +12,14 @@ import androidx.annotation.NonNull;
|
||||
public class FlutterOverlaySurface {
|
||||
@NonNull private final Surface surface;
|
||||
|
||||
private final long id;
|
||||
private final int id;
|
||||
|
||||
public FlutterOverlaySurface(long id, @NonNull Surface surface) {
|
||||
public FlutterOverlaySurface(int id, @NonNull Surface surface) {
|
||||
this.id = id;
|
||||
this.surface = surface;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,10 @@ import android.media.ImageReader;
|
||||
import android.os.Build;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.util.LongSparseArray;
|
||||
import android.util.SparseArray;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
@@ -31,6 +32,7 @@ import io.flutter.view.AccessibilityBridge;
|
||||
import io.flutter.view.TextureRegistry;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -78,14 +80,17 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
private final HashMap<Context, View> contextToPlatformView;
|
||||
|
||||
// Map of unique IDs to views that render overlay layers.
|
||||
private final LongSparseArray<FlutterImageView> overlayLayerViews;
|
||||
private final SparseArray<FlutterImageView> overlayLayerViews;
|
||||
|
||||
// Next available unique ID for use in overlayLayerViews.
|
||||
private long nextOverlayLayerId = 0;
|
||||
private int nextOverlayLayerId = 0;
|
||||
|
||||
// Tracks whether the flutterView has been converted to use a FlutterImageView.
|
||||
private boolean flutterViewConvertedToImageView = false;
|
||||
|
||||
// Overlay layer IDs that were displayed since the start of the current frame.
|
||||
private HashSet<Integer> currentFrameUsedOverlayLayerIds;
|
||||
|
||||
private final PlatformViewsChannel.PlatformViewsHandler channelHandler =
|
||||
new PlatformViewsChannel.PlatformViewsHandler() {
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
@@ -298,7 +303,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
vdControllers = new HashMap<>();
|
||||
accessibilityEventsDelegate = new AccessibilityEventsDelegate();
|
||||
contextToPlatformView = new HashMap<>();
|
||||
overlayLayerViews = new LongSparseArray<>();
|
||||
overlayLayerViews = new SparseArray<>();
|
||||
currentFrameUsedOverlayLayerIds = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -556,18 +562,46 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
}
|
||||
|
||||
public void onDisplayOverlaySurface(int id, int x, int y, int width, int height) {
|
||||
FlutterView flutterView = (FlutterView) this.flutterView;
|
||||
if (!flutterViewConvertedToImageView) {
|
||||
((FlutterView) flutterView).convertToImageView();
|
||||
flutterView.convertToImageView();
|
||||
flutterViewConvertedToImageView = true;
|
||||
}
|
||||
|
||||
FlutterImageView overlayView = overlayLayerViews.get(id);
|
||||
if (overlayView.getParent() == null) {
|
||||
flutterView.addView(overlayView);
|
||||
}
|
||||
|
||||
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) width, (int) height);
|
||||
layoutParams.leftMargin = (int) x;
|
||||
layoutParams.topMargin = (int) y;
|
||||
overlayView.setLayoutParams(layoutParams);
|
||||
overlayView.setVisibility(View.VISIBLE);
|
||||
overlayView.bringToFront();
|
||||
|
||||
currentFrameUsedOverlayLayerIds.add(id);
|
||||
}
|
||||
|
||||
public void onBeginFrame() {
|
||||
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
|
||||
currentFrameUsedOverlayLayerIds.clear();
|
||||
}
|
||||
|
||||
public void onEndFrame() {
|
||||
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
|
||||
for (int i = 0; i < overlayLayerViews.size(); i++) {
|
||||
int key = overlayLayerViews.keyAt(i);
|
||||
FlutterImageView overlayView = overlayLayerViews.valueAt(i);
|
||||
if (currentFrameUsedOverlayLayerIds.contains(key)) {
|
||||
overlayView.acquireLatestImage();
|
||||
} else {
|
||||
overlayView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
if (flutterViewConvertedToImageView) {
|
||||
FlutterView flutterView = (FlutterView) this.flutterView;
|
||||
flutterView.acquireLatestImageViewFrame();
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(19)
|
||||
@@ -588,7 +622,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
public FlutterOverlaySurface createOverlaySurface() {
|
||||
ImageReader imageReader = createImageReader(flutterView.getWidth(), flutterView.getHeight());
|
||||
FlutterImageView imageView = new FlutterImageView(flutterView.getContext(), imageReader);
|
||||
long id = nextOverlayLayerId++;
|
||||
int id = nextOverlayLayerId++;
|
||||
overlayLayerViews.put(id, imageView);
|
||||
|
||||
return new FlutterOverlaySurface(id, imageReader.getSurface());
|
||||
|
||||
@@ -709,7 +709,7 @@ bool RegisterApi(JNIEnv* env) {
|
||||
return false;
|
||||
}
|
||||
g_overlay_surface_id_method =
|
||||
env->GetMethodID(overlay_surface_class.obj(), "getId", "()J");
|
||||
env->GetMethodID(overlay_surface_class.obj(), "getId", "()I");
|
||||
if (g_overlay_surface_id_method == nullptr) {
|
||||
FML_LOG(ERROR) << "Could not locate FlutterOverlaySurface#getId() method";
|
||||
return false;
|
||||
@@ -1147,8 +1147,8 @@ PlatformViewAndroidJNIImpl::FlutterViewCreateOverlaySurface() {
|
||||
nullptr);
|
||||
}
|
||||
|
||||
jlong overlay_id =
|
||||
env->CallLongMethod(overlay.obj(), g_overlay_surface_id_method);
|
||||
jint overlay_id =
|
||||
env->CallIntMethod(overlay.obj(), g_overlay_surface_id_method);
|
||||
|
||||
jobject overlay_surface =
|
||||
env->CallObjectMethod(overlay.obj(), g_overlay_surface_surface_method);
|
||||
|
||||
Reference in New Issue
Block a user