Verify embedders can convert clip rects from mutation stack into surface space. (flutter/engine#14483)
Add a test to verify behavior discussed in b/143612326
This commit is contained in:
@@ -647,3 +647,15 @@ void arc_end_caps_correct() {
|
||||
};
|
||||
window.scheduleFrame();
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void scene_builder_with_clips() {
|
||||
window.onBeginFrame = (Duration duration) {
|
||||
SceneBuilder builder = SceneBuilder();
|
||||
builder.pushClipRect(Rect.fromLTRB(10.0, 10.0, 390.0, 290.0));
|
||||
builder.addPlatformView(42, width: 400.0, height: 300.0);
|
||||
builder.addPicture(Offset(0.0, 0.0), CreateGradientBox(Size(400.0, 300.0)));
|
||||
window.render(builder.build());
|
||||
};
|
||||
window.scheduleFrame();
|
||||
}
|
||||
|
||||
@@ -397,6 +397,19 @@ inline FlutterTransformation FlutterTransformationMake(const SkMatrix& matrix) {
|
||||
return transformation;
|
||||
}
|
||||
|
||||
inline SkMatrix SkMatrixMake(const FlutterTransformation& xformation) {
|
||||
return SkMatrix::MakeAll(xformation.scaleX, //
|
||||
xformation.skewX, //
|
||||
xformation.transX, //
|
||||
xformation.skewY, //
|
||||
xformation.scaleY, //
|
||||
xformation.transY, //
|
||||
xformation.pers0, //
|
||||
xformation.pers1, //
|
||||
xformation.pers2 //
|
||||
);
|
||||
}
|
||||
|
||||
inline flutter::EmbedderEngine* ToEmbedderEngine(const FlutterEngine& engine) {
|
||||
return reinterpret_cast<flutter::EmbedderEngine*>(engine);
|
||||
}
|
||||
@@ -410,6 +423,19 @@ inline FlutterRect FlutterRectMake(const SkRect& rect) {
|
||||
return r;
|
||||
}
|
||||
|
||||
inline FlutterRect FlutterRectMakeLTRB(double l, double t, double r, double b) {
|
||||
FlutterRect rect = {};
|
||||
rect.left = l;
|
||||
rect.top = t;
|
||||
rect.right = r;
|
||||
rect.bottom = b;
|
||||
return rect;
|
||||
}
|
||||
|
||||
inline SkRect SkRectMake(const FlutterRect& rect) {
|
||||
return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
|
||||
}
|
||||
|
||||
inline FlutterRoundedRect FlutterRoundedRectMake(const SkRRect& rect) {
|
||||
FlutterRoundedRect r = {};
|
||||
r.rect = FlutterRectMake(rect.rect());
|
||||
|
||||
@@ -3482,5 +3482,129 @@ TEST_F(EmbedderTest, ArcEndCapsAreDrawnCorrectly) {
|
||||
ASSERT_TRUE(ImageMatchesFixture("arc_end_caps.png", scene_image));
|
||||
}
|
||||
|
||||
static void FilterMutationsByType(
|
||||
const FlutterPlatformViewMutation** mutations,
|
||||
size_t count,
|
||||
FlutterPlatformViewMutationType type,
|
||||
std::function<void(const FlutterPlatformViewMutation& mutation)> handler) {
|
||||
if (mutations == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
const FlutterPlatformViewMutation* mutation = mutations[i];
|
||||
if (mutation->type != type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
handler(*mutation);
|
||||
}
|
||||
}
|
||||
|
||||
static void FilterMutationsByType(
|
||||
const FlutterPlatformView* view,
|
||||
FlutterPlatformViewMutationType type,
|
||||
std::function<void(const FlutterPlatformViewMutation& mutation)> handler) {
|
||||
return FilterMutationsByType(view->mutations, view->mutations_count, type,
|
||||
handler);
|
||||
}
|
||||
|
||||
static SkMatrix GetTotalMutationTransformationMatrix(
|
||||
const FlutterPlatformViewMutation** mutations,
|
||||
size_t count) {
|
||||
SkMatrix collected;
|
||||
|
||||
FilterMutationsByType(
|
||||
mutations, count, kFlutterPlatformViewMutationTypeTransformation,
|
||||
[&](const auto& mutation) {
|
||||
collected.preConcat(SkMatrixMake(mutation.transformation));
|
||||
});
|
||||
|
||||
return collected;
|
||||
}
|
||||
|
||||
static SkMatrix GetTotalMutationTransformationMatrix(
|
||||
const FlutterPlatformView* view) {
|
||||
return GetTotalMutationTransformationMatrix(view->mutations,
|
||||
view->mutations_count);
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest, ClipsAreCorrectlyCalculated) {
|
||||
auto& context = GetEmbedderContext();
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetOpenGLRendererConfig(SkISize::Make(400, 300));
|
||||
builder.SetCompositor();
|
||||
builder.SetDartEntrypoint("scene_builder_with_clips");
|
||||
|
||||
const auto root_surface_transformation =
|
||||
SkMatrix().preTranslate(0, 400).preRotate(-90, 0, 0);
|
||||
|
||||
context.SetRootSurfaceTransformation(root_surface_transformation);
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
context.GetCompositor().SetNextPresentCallback(
|
||||
[&](const FlutterLayer** layers, size_t layers_count) {
|
||||
ASSERT_EQ(layers_count, 3u);
|
||||
|
||||
{
|
||||
FlutterPlatformView platform_view = *layers[1]->platform_view;
|
||||
platform_view.struct_size = sizeof(platform_view);
|
||||
platform_view.identifier = 42;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypePlatformView;
|
||||
layer.platform_view = &platform_view;
|
||||
layer.size = FlutterSizeMake(300.0, 400.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
|
||||
ASSERT_EQ(*layers[1], layer);
|
||||
|
||||
bool clip_assertions_checked = false;
|
||||
|
||||
// The total transformation on the stack upto the platform view.
|
||||
const auto total_xformation =
|
||||
GetTotalMutationTransformationMatrix(layers[1]->platform_view);
|
||||
|
||||
FilterMutationsByType(
|
||||
layers[1]->platform_view,
|
||||
kFlutterPlatformViewMutationTypeClipRect,
|
||||
[&](const auto& mutation) {
|
||||
FlutterRect clip = mutation.clip_rect;
|
||||
|
||||
// The test is only setup to supply one clip. Make sure it is
|
||||
// the one we expect.
|
||||
const auto rect_to_compare =
|
||||
SkRect::MakeLTRB(10.0, 10.0, 390, 290);
|
||||
ASSERT_EQ(clip, FlutterRectMake(rect_to_compare));
|
||||
|
||||
// This maps the clip from device space into surface space.
|
||||
SkRect mapped;
|
||||
ASSERT_TRUE(total_xformation.mapRect(&mapped, rect_to_compare));
|
||||
ASSERT_EQ(mapped, SkRect::MakeLTRB(10, 10, 290, 390));
|
||||
clip_assertions_checked = true;
|
||||
});
|
||||
|
||||
ASSERT_TRUE(clip_assertions_checked);
|
||||
}
|
||||
|
||||
latch.Signal();
|
||||
});
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
FlutterWindowMetricsEvent event = {};
|
||||
event.struct_size = sizeof(event);
|
||||
event.width = 400;
|
||||
event.height = 300;
|
||||
event.pixel_ratio = 1.0;
|
||||
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event),
|
||||
kSuccess);
|
||||
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
Reference in New Issue
Block a user