Implement mutator stack on Android hybrid composition platform view (flutter/engine#19426)

This commit is contained in:
Chris Yang
2020-07-04 13:03:04 -07:00
committed by GitHub
parent 5419a5279c
commit cf4d2e25d9
18 changed files with 549 additions and 66 deletions

View File

@@ -712,6 +712,8 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/D
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/loader/ResourceExtractor.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorsStack.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/FlutterPlugin.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/PluginRegistry.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityAware.java

View File

@@ -50,4 +50,14 @@ MutatorsStack::Bottom() const {
return vector_.rbegin();
};
const std::vector<std::shared_ptr<Mutator>>::const_iterator
MutatorsStack::Begin() const {
return vector_.begin();
};
const std::vector<std::shared_ptr<Mutator>>::const_iterator MutatorsStack::End()
const {
return vector_.end();
};
} // namespace flutter

View File

@@ -138,12 +138,23 @@ class MutatorsStack {
// and destroys it.
void Pop();
// Returns an iterator pointing to the top of the stack.
// Returns a reverse iterator pointing to the top of the stack, which is the
// mutator that is furtherest from the leaf node.
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Top()
const;
// Returns an iterator pointing to the bottom of the stack.
// Returns a reverse iterator pointing to the bottom of the stack, which is
// the mutator that is closeset from the leaf node.
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Bottom()
const;
// Returns an iterator pointing to the begining of the mutator vector, which
// is the mutator that is furtherest from the leaf node.
const std::vector<std::shared_ptr<Mutator>>::const_iterator Begin() const;
// Returns an iterator pointing to the end of the mutator vector, which is the
// mutator that is closest from the leaf node.
const std::vector<std::shared_ptr<Mutator>>::const_iterator End() const;
bool is_empty() const { return vector_.empty(); }
bool operator==(const MutatorsStack& other) const {
@@ -196,7 +207,7 @@ class EmbeddedViewParams {
SkRect starting_rect = SkRect::MakeSize(size_points);
path.addRect(starting_rect);
path.transform(matrix);
final_bounding_rect_ = path.computeTightBounds();
final_bounding_rect_ = path.getBounds();
}
EmbeddedViewParams(const EmbeddedViewParams& other) {

View File

@@ -24,6 +24,26 @@ TEST(MutatorsStack, CopyConstructor) {
ASSERT_TRUE(copy == stack);
}
TEST(MutatorsStack, CopyAndUpdateTheCopy) {
MutatorsStack stack;
auto rrect = SkRRect::MakeEmpty();
auto rect = SkRect::MakeEmpty();
stack.PushClipRect(rect);
stack.PushClipRRect(rrect);
MutatorsStack copy = MutatorsStack(stack);
copy.Pop();
copy.Pop();
ASSERT_TRUE(copy != stack);
ASSERT_TRUE(copy.is_empty());
ASSERT_TRUE(!stack.is_empty());
auto iter = stack.Bottom();
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rrect);
ASSERT_TRUE(iter->get()->GetRRect() == rrect);
++iter;
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rect);
ASSERT_TRUE(iter->get()->GetRect() == rect);
}
TEST(MutatorsStack, PushClipRect) {
MutatorsStack stack;
auto rect = SkRect::MakeEmpty();

View File

@@ -149,6 +149,8 @@ android_java_sources = [
"io/flutter/embedding/engine/FlutterEnginePluginRegistry.java",
"io/flutter/embedding/engine/FlutterJNI.java",
"io/flutter/embedding/engine/FlutterOverlaySurface.java",
"io/flutter/embedding/engine/mutatorsstack/FlutterMutatorsStack.java",
"io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java",
"io/flutter/embedding/engine/FlutterShellArgs.java",
"io/flutter/embedding/engine/dart/DartExecutor.java",
"io/flutter/embedding/engine/dart/DartMessenger.java",

View File

@@ -62,11 +62,12 @@ std::vector<SkCanvas*> AndroidExternalViewEmbedder::GetCurrentCanvases() {
SkRect AndroidExternalViewEmbedder::GetViewRect(int view_id) const {
const EmbeddedViewParams& params = view_params_.at(view_id);
// TODO(egarciad): The rect should be computed from the mutator stack.
// (Clipping is missing)
// https://github.com/flutter/flutter/issues/59821
return SkRect::MakeXYWH(params.finalBoundingRect().x(), //
params.finalBoundingRect().y(), //
params.sizePoints().width() * device_pixel_ratio_, //
params.sizePoints().height() * device_pixel_ratio_ //
return SkRect::MakeXYWH(params.finalBoundingRect().x(), //
params.finalBoundingRect().y(), //
params.finalBoundingRect().width(), //
params.finalBoundingRect().height() //
);
}
@@ -146,13 +147,18 @@ bool AndroidExternalViewEmbedder::SubmitFrame(
for (int64_t view_id : composition_order_) {
SkRect view_rect = GetViewRect(view_id);
const EmbeddedViewParams& params = view_params_.at(view_id);
// Display the platform view. If it's already displayed, then it's
// just positioned and sized.
jni_facade_->FlutterViewOnDisplayPlatformView(view_id, //
view_rect.x(), //
view_rect.y(), //
view_rect.width(), //
view_rect.height() //
jni_facade_->FlutterViewOnDisplayPlatformView(
view_id, //
view_rect.x(), //
view_rect.y(), //
view_rect.width(), //
view_rect.height(), //
params.sizePoints().width() * device_pixel_ratio_,
params.sizePoints().height() * device_pixel_ratio_,
params.mutatorsStack() //
);
for (const SkRect& overlay_rect : overlay_layers.at(view_id)) {
CreateSurfaceIfNeeded(context, //

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/flow/embedded_views.h"
#include "flutter/flow/surface.h"
#include "flutter/fml/raster_thread_merger.h"
#include "flutter/fml/thread.h"
@@ -199,14 +200,17 @@ TEST(AndroidExternalViewEmbedder, PlatformViewRect) {
raster_thread_merger);
MutatorsStack stack;
SkMatrix matrix = SkMatrix::MakeTrans(10, 20);
stack.PushTransform(matrix);
SkMatrix matrix;
matrix.setIdentity();
// The framework always push a scale matrix based on the screen ratio.
matrix.setConcat(matrix, SkMatrix::MakeScale(1.5, 1.5));
matrix.setConcat(matrix, SkMatrix::MakeTrans(10, 20));
auto view_params =
std::make_unique<EmbeddedViewParams>(matrix, SkSize::Make(30, 40), stack);
auto view_id = 0;
embedder->PrerollCompositeEmbeddedView(view_id, std::move(view_params));
ASSERT_EQ(SkRect::MakeXYWH(10, 20, 45, 60), embedder->GetViewRect(view_id));
ASSERT_EQ(SkRect::MakeXYWH(15, 30, 45, 60), embedder->GetViewRect(view_id));
}
TEST(AndroidExternalViewEmbedder, PlatformViewRect__ChangedParams) {
@@ -223,22 +227,27 @@ TEST(AndroidExternalViewEmbedder, PlatformViewRect__ChangedParams) {
auto view_id = 0;
MutatorsStack stack1;
SkMatrix matrix1 = SkMatrix::MakeTrans(10, 20);
stack1.PushTransform(matrix1);
SkMatrix matrix1;
matrix1.setIdentity();
// The framework always push a scale matrix based on the screen ratio.
matrix1.setConcat(SkMatrix::MakeScale(1.5, 1.5), SkMatrix::MakeTrans(10, 20));
auto view_params_1 = std::make_unique<EmbeddedViewParams>(
matrix1, SkSize::Make(30, 40), stack1);
embedder->PrerollCompositeEmbeddedView(view_id, std::move(view_params_1));
MutatorsStack stack2;
SkMatrix matrix2 = SkMatrix::MakeTrans(50, 60);
stack2.PushTransform(matrix2);
SkMatrix matrix2;
matrix2.setIdentity();
// The framework always push a scale matrix based on the screen ratio.
matrix2.setConcat(matrix2, SkMatrix::MakeScale(1.5, 1.5));
matrix2.setConcat(matrix2, SkMatrix::MakeTrans(50, 60));
auto view_params_2 = std::make_unique<EmbeddedViewParams>(
matrix2, SkSize::Make(70, 80), stack2);
embedder->PrerollCompositeEmbeddedView(view_id, std::move(view_params_2));
ASSERT_EQ(SkRect::MakeXYWH(50, 60, 105, 120), embedder->GetViewRect(view_id));
ASSERT_EQ(SkRect::MakeXYWH(75, 90, 105, 120), embedder->GetViewRect(view_id));
}
TEST(AndroidExternalViewEmbedder, SubmitFrame__RecycleSurfaces) {
@@ -291,8 +300,13 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame__RecycleSurfaces) {
// Add an Android view.
MutatorsStack stack1;
SkMatrix matrix1 = SkMatrix::MakeTrans(100, 100);
stack1.PushTransform(matrix1);
SkMatrix matrix1;
matrix1.setIdentity();
SkMatrix scale = SkMatrix::MakeScale(1.5, 1.5);
SkMatrix trans = SkMatrix::MakeTrans(100, 100);
matrix1.setConcat(scale, trans);
stack1.PushTransform(scale);
stack1.PushTransform(trans);
// TODO(egarciad): Investigate why Flow applies the device pixel ratio to
// the offsetPixels, but not the sizePoints.
auto view_params_1 = std::make_unique<EmbeddedViewParams>(
@@ -318,8 +332,8 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame__RecycleSurfaces) {
ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
0, window))));
// The JNI call to display the Android view.
EXPECT_CALL(*jni_mock,
FlutterViewOnDisplayPlatformView(0, 100, 100, 300, 300));
EXPECT_CALL(*jni_mock, FlutterViewOnDisplayPlatformView(
0, 150, 150, 300, 300, 300, 300, stack1));
// The JNI call to display the overlay surface.
EXPECT_CALL(*jni_mock,
FlutterViewDisplayOverlaySurface(0, 50, 50, 200, 200));
@@ -342,8 +356,13 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame__RecycleSurfaces) {
// Add an Android view.
MutatorsStack stack1;
SkMatrix matrix1 = SkMatrix::MakeTrans(100, 100);
stack1.PushTransform(matrix1);
SkMatrix matrix1;
matrix1.setIdentity();
SkMatrix scale = SkMatrix::MakeScale(1.5, 1.5);
SkMatrix trans = SkMatrix::MakeTrans(100, 100);
matrix1.setConcat(scale, trans);
stack1.PushTransform(scale);
stack1.PushTransform(trans);
// TODO(egarciad): Investigate why Flow applies the device pixel ratio to
// the offsetPixels, but not the sizePoints.
auto view_params_1 = std::make_unique<EmbeddedViewParams>(
@@ -367,8 +386,8 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame__RecycleSurfaces) {
// frame.
EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface()).Times(0);
// The JNI call to display the Android view.
EXPECT_CALL(*jni_mock,
FlutterViewOnDisplayPlatformView(0, 100, 100, 300, 300));
EXPECT_CALL(*jni_mock, FlutterViewOnDisplayPlatformView(
0, 150, 150, 300, 300, 300, 300, stack1));
// The JNI call to display the overlay surface.
EXPECT_CALL(*jni_mock,
FlutterViewDisplayOverlaySurface(0, 50, 50, 200, 200));

View File

@@ -20,6 +20,7 @@ import androidx.annotation.VisibleForTesting;
import io.flutter.Log;
import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
import io.flutter.embedding.engine.dart.PlatformMessageHandler;
import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack;
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.embedding.engine.renderer.RenderSurface;
import io.flutter.plugin.common.StandardMessageCodec;
@@ -929,13 +930,22 @@ public class FlutterJNI {
// @SuppressWarnings("unused")
@UiThread
public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) {
public void onDisplayPlatformView(
int viewId,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
FlutterMutatorsStack mutatorsStack) {
ensureRunningOnMainThread();
if (platformViewsController == null) {
throw new RuntimeException(
"platformViewsController must be set before attempting to position a platform view");
}
platformViewsController.onDisplayPlatformView(viewId, x, y, width, height);
platformViewsController.onDisplayPlatformView(
viewId, x, y, width, height, viewWidth, viewHeight, mutatorsStack);
}
// TODO(mattcarroll): determine if this is nonull or nullable

View File

@@ -0,0 +1,95 @@
package io.flutter.embedding.engine.mutatorsstack;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
/**
* A view that applies the {@link io.flutter.embedding.engine.mutatorsstack.MutatorsStack} to its
* children.
*/
public class FlutterMutatorView extends FrameLayout {
private FlutterMutatorsStack mutatorsStack;
private float screenDensity;
private int left;
private int top;
/**
* Initialize the FlutterMutatorView. Use this to set the screenDensity, which will be used to
* correct the final transform matrix.
*/
public FlutterMutatorView(@NonNull Context context, float screenDensity) {
super(context, null);
this.screenDensity = screenDensity;
}
/** Initialize the FlutterMutatorView. */
public FlutterMutatorView(@NonNull Context context) {
super(context, null);
this.screenDensity = 1;
}
/**
* Pass the necessary parameters to the view so it can apply correct mutations to its children.
*/
public void readyToDisplay(
@NonNull FlutterMutatorsStack mutatorsStack, int left, int top, int width, int height) {
this.mutatorsStack = mutatorsStack;
this.left = left;
this.top = top;
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height);
layoutParams.leftMargin = left;
layoutParams.topMargin = top;
setLayoutParams(layoutParams);
setWillNotDraw(false);
}
@Override
public void draw(Canvas canvas) {
// Apply all clippings on the parent canvas.
canvas.save();
for (Path path : mutatorsStack.getFinalClippingPaths()) {
// Reverse the current offset.
//
// The frame of this view includes the final offset of the bounding rect.
// We need to apply all the mutators to the view, which includes the mutation that leads to
// the final offset. We should reverse this final offset, both as a translate mutation and to
// all the clipping paths
Path pathCopy = new Path(path);
pathCopy.offset(-left, -top);
canvas.clipPath(pathCopy);
}
super.draw(canvas);
canvas.restore();
}
@Override
public void dispatchDraw(Canvas canvas) {
// Apply all the transforms on the child canvas.
canvas.save();
Matrix finalMatrix = new Matrix(mutatorsStack.getFinalMatrix());
// Reverse scale based on screen scale.
//
// The Android frame is set based on the logical resolution instead of physical.
// (https://developer.android.com/training/multiscreen/screendensities).
// However, flow is based on the physical resolution. For example, 1000 pixels in flow equals
// 500 points in Android. And until this point, we did all the calculation based on the flow
// resolution. So we need to scale down to match Android's logical resolution.
finalMatrix.preScale(1 / screenDensity, 1 / screenDensity);
// Reverse the current offset.
//
// The frame of this view includes the final offset of the bounding rect.
// We need to apply all the mutators to the view, which includes the mutation that leads to
// the final offset. We should reverse this final offset, both as a translate mutation and to
// all the clipping paths
finalMatrix.postTranslate(-left, -top);
canvas.concat(finalMatrix);
super.dispatchDraw(canvas);
canvas.restore();
}
}

View File

@@ -0,0 +1,182 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.embedding.engine.mutatorsstack;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The mutator stack containing a list of mutators
*
* <p>The mutators can be applied to a {@link io.flutter.plugin.platform.PlatformView} to perform a
* series mutations. See {@link io.flutter.embedding.engine.mutatorsstack.Mutator} for informations
* on Mutators.
*/
public class FlutterMutatorsStack {
/**
* The type of a Mutator See {@link io.flutter.embedding.engine.mutatorsstack.Mutator} for
* informations on Mutators.
*/
public enum FlutterMutatorType {
CLIP_RECT,
CLIP_RRECT,
CLIP_PATH,
TRANSFORM,
OPACITY
}
/**
* A class represents a mutator
*
* <p>A mutator contains information of a single mutation operation that can be applied to a
* {@link io.flutter.plugin.platform.PlatformView}. See {@link
* io.flutter.embedding.engine.mutatorsstack.Mutator} for informations on Mutators.
*/
public class FlutterMutator {
@Nullable private Matrix matrix;
@Nullable private Rect rect;
@Nullable private Path path;
private FlutterMutatorType type;
/**
* Initialize a clip rect mutator.
*
* @param rect the rect to be clipped.
*/
public FlutterMutator(Rect rect) {
this.type = FlutterMutatorType.CLIP_RECT;
this.rect = rect;
}
/**
* Initialize a clip path mutator.
*
* @param rect the path to be clipped.
*/
public FlutterMutator(Path path) {
this.type = FlutterMutatorType.CLIP_PATH;
this.path = path;
}
/**
* Initialize a transform mutator.
*
* @param matrix the transform matrix to apply.
*/
public FlutterMutator(Matrix matrix) {
this.type = FlutterMutatorType.TRANSFORM;
this.matrix = matrix;
}
/**
* Get the mutator type.
*
* @return The type of the mutator.
*/
public FlutterMutatorType getType() {
return type;
}
/**
* Get the rect of the mutator if the {@link #getType()} returns FlutterMutatorType.CLIP_RECT.
*
* @return the clipping rect if the type is FlutterMutatorType.CLIP_RECT; otherwise null.
*/
public Rect getRect() {
return rect;
}
/**
* Get the path of the mutator if the {@link #getType()} returns FlutterMutatorType.CLIP_PATH.
*
* @return the clipping path if the type is FlutterMutatorType.CLIP_PATH; otherwise null.
*/
public Path getPath() {
return path;
}
/**
* Get the matrix of the mutator if the {@link #getType()} returns FlutterMutatorType.TRANSFORM.
*
* @return the matrix if the type is FlutterMutatorType.TRANSFORM; otherwise null.
*/
public Matrix getMatrix() {
return matrix;
}
}
private @NonNull List<FlutterMutator> mutators;
private List<Path> finalClippingPaths;
private Matrix finalMatrix;
/** Initialize the mutator stack. */
public FlutterMutatorsStack() {
this.mutators = new ArrayList<FlutterMutator>();
finalMatrix = new Matrix();
finalClippingPaths = new ArrayList<Path>();
}
/**
* Push a transform {@link io.flutter.embedding.engine.mutatorsstack.Mutator} to the stack.
*
* @param values the transform matrix to be pushed to the stack. The array matches how a {@link
* android.graphics.Matrix} is constructed.
*/
public void pushTransform(float[] values) {
Matrix matrix = new Matrix();
matrix.setValues(values);
FlutterMutator mutator = new FlutterMutator(matrix);
mutators.add(mutator);
finalMatrix.preConcat(mutator.getMatrix());
}
/** Push a clipRect {@link io.flutter.embedding.engine.mutatorsstack.Mutator} to the stack. */
public void pushClipRect(int left, int top, int right, int bottom) {
Rect rect = new Rect(left, top, right, bottom);
FlutterMutator mutator = new FlutterMutator(rect);
mutators.add(mutator);
Path path = new Path();
path.addRect(new RectF(rect), Path.Direction.CCW);
path.transform(finalMatrix);
finalClippingPaths.add(path);
}
/**
* Get a list of all the raw mutators. The 0 index of the returned list is the top of the stack.
*/
public List<FlutterMutator> getMutators() {
return mutators;
}
/**
* Get a list of all the clipping operations. All the clipping operations -- whether it is clip
* rect, clip rrect, or clip path -- are converted into Paths. The paths are also transformed with
* the matrix that up to their stack positions. For example: If the stack looks like (from top to
* bottom): TransA -> ClipA -> TransB -> ClipB, the final paths will look like [TransA*ClipA,
* TransA*TransB*ClipB].
*
* <p>Clipping this list to the parent canvas of a view results the final clipping path.
*/
public List<Path> getFinalClippingPaths() {
return finalClippingPaths;
}
/**
* Returns the final matrix. Apply this matrix to the canvas of a view results the final
* transformation of the view.
*/
public Matrix getFinalMatrix() {
return finalMatrix;
}
}

View File

@@ -23,6 +23,7 @@ import io.flutter.embedding.android.FlutterImageView;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterOverlaySurface;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.mutatorsstack.*;
import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel;
import io.flutter.plugin.editing.TextInputPlugin;
import io.flutter.view.AccessibilityBridge;
@@ -75,6 +76,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
private final SparseArray<PlatformViewsChannel.PlatformViewCreationRequest> platformViewRequests;
private final SparseArray<View> platformViews;
private final SparseArray<FlutterMutatorView> mutatorViews;
// Map of unique IDs to views that render overlay layers.
private final SparseArray<FlutterImageView> overlayLayerViews;
@@ -109,8 +111,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
platformViewRequests.remove(viewId);
}
if (platformViews.get(viewId) != null) {
((FlutterView) flutterView).removeView(platformViews.get(viewId));
((FlutterView) flutterView).removeView(mutatorViews.get(viewId));
platformViews.remove(viewId);
mutatorViews.remove(viewId);
}
}
@@ -335,6 +338,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
platformViewRequests = new SparseArray<>();
platformViews = new SparseArray<>();
mutatorViews = new SparseArray<>();
}
/**
@@ -630,29 +634,34 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
PlatformView platformView = factory.create(context, viewId, createParams);
View view = platformView.getView();
// Reverse scale the view based on the screen density.
//
// Flow pixels are based on the physical resolution. For example, 1000 pixels in flow equals
// 500 points in Android if the display density is 2.0.
float density = getDisplayDensity();
view.setPivotX(0);
view.setPivotY(0);
view.setScaleX(1 / density);
view.setScaleY(1 / density);
platformViews.put(viewId, view);
((FlutterView) flutterView).addView(view);
FlutterMutatorView mutatorView =
new FlutterMutatorView(context, context.getResources().getDisplayMetrics().density);
mutatorViews.put(viewId, mutatorView);
mutatorView.addView(platformView.getView());
((FlutterView) flutterView).addView(mutatorView);
}
public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) {
public void onDisplayPlatformView(
int viewId,
int x,
int y,
int width,
int height,
int viewWidth,
int ViewHeight,
FlutterMutatorsStack mutatorsStack) {
initializeRootImageViewIfNeeded();
initializePlatformViewIfNeeded(viewId);
FlutterMutatorView mutatorView = mutatorViews.get(viewId);
mutatorView.readyToDisplay(mutatorsStack, x, y, width, height);
mutatorView.setVisibility(View.VISIBLE);
mutatorView.bringToFront();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(viewWidth, ViewHeight);
View platformView = platformViews.get(viewId);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) width, (int) height);
layoutParams.leftMargin = (int) x;
layoutParams.topMargin = (int) y;
platformView.setLayoutParams(layoutParams);
platformView.setVisibility(View.VISIBLE);
platformView.bringToFront();
@@ -701,6 +710,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
int viewId = platformViews.keyAt(i);
if (!currentFrameUsedPlatformViewIds.contains(viewId)) {
platformViews.get(viewId).setVisibility(View.GONE);
mutatorViews.get(viewId).setVisibility(View.GONE);
}
}
// If the background surface is still an image, then acquire the latest image.

View File

@@ -14,6 +14,7 @@ source_set("jni") {
public_configs = [ "//flutter:config" ]
deps = [
"//flutter/flow",
"//flutter/fml",
"//flutter/lib/ui",
"//flutter/shell/platform/android/surface:native_window",

View File

@@ -63,7 +63,14 @@ class JNIMock final : public PlatformViewAndroidJNI {
MOCK_METHOD(void,
FlutterViewOnDisplayPlatformView,
(int view_id, int x, int y, int width, int height),
(int view_id,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
MutatorsStack mutators_stack),
(override));
MOCK_METHOD(void,

View File

@@ -12,6 +12,7 @@
#include "flutter/fml/platform/android/jni_weak_ref.h"
#endif
#include "flutter/flow/embedded_views.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "flutter/shell/platform/android/surface/android_native_window.h"
#include "third_party/skia/include/core/SkMatrix.h"
@@ -115,11 +116,15 @@ class PlatformViewAndroidJNI {
///
/// @note Must be called from the platform thread.
///
virtual void FlutterViewOnDisplayPlatformView(int view_id,
int x,
int y,
int width,
int height) = 0;
virtual void FlutterViewOnDisplayPlatformView(
int view_id,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
MutatorsStack mutators_stack) = 0;
//----------------------------------------------------------------------------
/// @brief Positions and sizes an overlay surface in hybrid composition.

View File

@@ -102,12 +102,20 @@ static jmethodID g_compute_platform_resolved_locale_method = nullptr;
// Called By Java
static jmethodID g_on_display_platform_view_method = nullptr;
// static jmethodID g_on_composite_platform_view_method = nullptr;
static jmethodID g_on_display_overlay_surface_method = nullptr;
static jmethodID g_overlay_surface_id_method = nullptr;
static jmethodID g_overlay_surface_surface_method = nullptr;
// Mutators
static fml::jni::ScopedJavaGlobalRef<jclass>* g_mutators_stack_class = nullptr;
static jmethodID g_mutators_stack_init_method = nullptr;
static jmethodID g_mutators_stack_push_transform_method = nullptr;
static jmethodID g_mutators_stack_push_cliprect_method = nullptr;
// Called By Java
static jlong AttachJNI(JNIEnv* env,
jclass clazz,
@@ -782,8 +790,42 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
return false;
}
g_on_display_platform_view_method = env->GetMethodID(
g_flutter_jni_class->obj(), "onDisplayPlatformView", "(IIIII)V");
g_mutators_stack_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
env,
env->FindClass(
"io/flutter/embedding/engine/mutatorsstack/FlutterMutatorsStack"));
if (g_mutators_stack_class == nullptr) {
FML_LOG(ERROR) << "Could not locate FlutterMutatorsStack";
return false;
}
g_mutators_stack_init_method =
env->GetMethodID(g_mutators_stack_class->obj(), "<init>", "()V");
if (g_mutators_stack_init_method == nullptr) {
FML_LOG(ERROR) << "Could not locate FlutterMutatorsStack.init method";
return false;
}
g_mutators_stack_push_transform_method =
env->GetMethodID(g_mutators_stack_class->obj(), "pushTransform", "([F)V");
if (g_mutators_stack_push_transform_method == nullptr) {
FML_LOG(ERROR)
<< "Could not locate FlutterMutatorsStack.pushTransform method";
return false;
}
g_mutators_stack_push_cliprect_method = env->GetMethodID(
g_mutators_stack_class->obj(), "pushClipRect", "(IIII)V");
if (g_mutators_stack_push_cliprect_method == nullptr) {
FML_LOG(ERROR)
<< "Could not locate FlutterMutatorsStack.pushCilpRect method";
return false;
}
g_on_display_platform_view_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onDisplayPlatformView",
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/"
"FlutterMutatorsStack;)V");
if (g_on_display_platform_view_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView method";
@@ -1094,20 +1136,62 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
FML_CHECK(CheckException(env));
}
void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(int view_id,
int x,
int y,
int width,
int height) {
void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(
int view_id,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
MutatorsStack mutators_stack) {
JNIEnv* env = fml::jni::AttachCurrentThread();
auto java_object = java_object_.get(env);
if (java_object.is_null()) {
return;
}
jobject mutatorsStack = env->NewObject(g_mutators_stack_class->obj(),
g_mutators_stack_init_method);
std::vector<std::shared_ptr<Mutator>>::const_iterator iter =
mutators_stack.Begin();
while (iter != mutators_stack.End()) {
switch ((*iter)->GetType()) {
case transform: {
const SkMatrix& matrix = (*iter)->GetMatrix();
SkScalar matrix_array[9];
matrix.get9(matrix_array);
fml::jni::ScopedJavaLocalRef<jfloatArray> transformMatrix(
env, env->NewFloatArray(9));
env->SetFloatArrayRegion(transformMatrix.obj(), 0, 9, matrix_array);
env->CallVoidMethod(mutatorsStack,
g_mutators_stack_push_transform_method,
transformMatrix.obj());
break;
}
case clip_rect: {
const SkRect& rect = (*iter)->GetRect();
env->CallVoidMethod(mutatorsStack,
g_mutators_stack_push_cliprect_method,
(int)rect.left(), (int)rect.top(),
(int)rect.right(), (int)rect.bottom());
break;
}
// TODO(cyanglaz): Implement other mutators.
// https://github.com/flutter/flutter/issues/58426
case clip_rrect:
case clip_path:
case opacity:
break;
}
++iter;
}
env->CallVoidMethod(java_object.obj(), g_on_display_platform_view_method,
view_id, x, y, width, height);
view_id, x, y, width, height, viewWidth, viewHeight,
mutatorsStack);
FML_CHECK(CheckException(env));
}

View File

@@ -54,7 +54,10 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
int x,
int y,
int width,
int height) override;
int height,
int viewWidth,
int viewHeight,
MutatorsStack mutators_stack) override;
void FlutterViewDisplayOverlaySurface(int surface_id,
int x,

View File

@@ -12,6 +12,7 @@ import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack;
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
import io.flutter.plugin.localization.LocalizationPlugin;
@@ -152,15 +153,29 @@ public class FlutterJNITest {
FlutterJNI flutterJNI = new FlutterJNI();
flutterJNI.setPlatformViewsController(platformViewsController);
FlutterMutatorsStack stack = new FlutterMutatorsStack();
// --- Execute Test ---
flutterJNI.onDisplayPlatformView(
/*viewId=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200);
/*viewId=*/ 1,
/*x=*/ 10,
/*y=*/ 20,
/*width=*/ 100,
/*height=*/ 200,
/*viewWidth=*/ 100,
/*viewHeight=*/ 200,
/*mutatorsStack=*/ stack);
// --- Verify Results ---
verify(platformViewsController, times(1))
.onDisplayPlatformView(
/*viewId=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200);
/*viewId=*/ 1,
/*x=*/ 10,
/*y=*/ 20,
/*width=*/ 100,
/*height=*/ 200,
/*viewWidth=*/ 100,
/*viewHeight=*/ 200,
/*mutatorsStack=*/ stack);
}
@Test

View File

@@ -40,6 +40,7 @@ def main():
'io.flutter.embedding.engine',
'io.flutter.embedding.engine.dart',
'io.flutter.embedding.engine.loader',
'io.flutter.embedding.engine.mutatorsstack',
'io.flutter.embedding.engine.plugins',
'io.flutter.embedding.engine.plugins.activity',
'io.flutter.embedding.engine.plugins.broadcastreceiver',