Skip to content

Commit aa8d5d4

Browse files
authored
Avoid sending a 0 DPR to framework (#21389)
* Avoid sending a 0 DPR to framework * check width and height as well
1 parent 48ab5d1 commit aa8d5d4

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

lib/ui/window/viewport_metrics.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,13 @@ ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
4242
physical_system_gesture_inset_bottom(
4343
p_physical_system_gesture_inset_bottom),
4444
physical_system_gesture_inset_left(p_physical_system_gesture_inset_left) {
45-
// Ensure we don't have nonsensical dimensions.
46-
FML_DCHECK(physical_width >= 0);
47-
FML_DCHECK(physical_height >= 0);
48-
FML_DCHECK(device_pixel_ratio > 0);
4945
}
5046

5147
ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
5248
double p_physical_width,
5349
double p_physical_height)
5450
: device_pixel_ratio(p_device_pixel_ratio),
5551
physical_width(p_physical_width),
56-
physical_height(p_physical_height) {
57-
// Ensure we don't have nonsensical dimensions.
58-
FML_DCHECK(physical_width >= 0);
59-
FML_DCHECK(physical_height >= 0);
60-
FML_DCHECK(device_pixel_ratio > 0);
61-
}
52+
physical_height(p_physical_height) {}
6253

6354
} // namespace flutter

shell/common/fixtures/shell_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ void onPointerDataPacketMain() {
4747
@pragma('vm:entry-point')
4848
void emptyMain() {}
4949

50+
@pragma('vm:entry-point')
51+
void reportMetrics() {
52+
window.onMetricsChanged = () {
53+
_reportMetrics(
54+
window.devicePixelRatio,
55+
window.physicalSize.width,
56+
window.physicalSize.height,
57+
);
58+
};
59+
}
60+
61+
void _reportMetrics(double devicePixelRatio, double width, double height) native 'ReportMetrics';
62+
5063
@pragma('vm:entry-point')
5164
void dummyReportTimingsMain() {
5265
window.onReportTimings = (List<FrameTiming> timings) {};

shell/common/shell.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,16 @@ void Shell::OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) {
816816
FML_DCHECK(is_setup_);
817817
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
818818

819+
if (metrics.device_pixel_ratio <= 0 || metrics.physical_width <= 0 ||
820+
metrics.physical_height <= 0) {
821+
FML_DLOG(ERROR)
822+
<< "Embedding reported invalid ViewportMetrics, ignoring update."
823+
<< "\nphysical_width: " << metrics.physical_width
824+
<< "\nphysical_height: " << metrics.physical_height
825+
<< "\ndevice_pixel_ratio: " << metrics.device_pixel_ratio;
826+
return;
827+
}
828+
819829
// This is the formula Android uses.
820830
// https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/renderthread/CacheManager.cpp#41
821831
size_t max_bytes = metrics.physical_width * metrics.physical_height * 12 * 4;

shell/common/shell_unittests.cc

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,5 +2066,74 @@ TEST_F(ShellTest, DiscardLayerTreeOnResize) {
20662066
DestroyShell(std::move(shell));
20672067
}
20682068

2069+
TEST_F(ShellTest, IgnoresInvalidMetrics) {
2070+
fml::AutoResetWaitableEvent latch;
2071+
double last_device_pixel_ratio;
2072+
double last_width;
2073+
double last_height;
2074+
auto native_report_device_pixel_ratio = [&](Dart_NativeArguments args) {
2075+
auto dpr_handle = Dart_GetNativeArgument(args, 0);
2076+
ASSERT_TRUE(Dart_IsDouble(dpr_handle));
2077+
Dart_DoubleValue(dpr_handle, &last_device_pixel_ratio);
2078+
ASSERT_FALSE(last_device_pixel_ratio == 0.0);
2079+
2080+
auto width_handle = Dart_GetNativeArgument(args, 1);
2081+
ASSERT_TRUE(Dart_IsDouble(width_handle));
2082+
Dart_DoubleValue(width_handle, &last_width);
2083+
ASSERT_FALSE(last_width == 0.0);
2084+
2085+
auto height_handle = Dart_GetNativeArgument(args, 2);
2086+
ASSERT_TRUE(Dart_IsDouble(height_handle));
2087+
Dart_DoubleValue(height_handle, &last_height);
2088+
ASSERT_FALSE(last_height == 0.0);
2089+
2090+
latch.Signal();
2091+
};
2092+
2093+
Settings settings = CreateSettingsForFixture();
2094+
auto task_runner = CreateNewThread();
2095+
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
2096+
task_runner);
2097+
2098+
AddNativeCallback("ReportMetrics",
2099+
CREATE_NATIVE_ENTRY(native_report_device_pixel_ratio));
2100+
2101+
std::unique_ptr<Shell> shell =
2102+
CreateShell(std::move(settings), std::move(task_runners));
2103+
2104+
auto configuration = RunConfiguration::InferFromSettings(settings);
2105+
configuration.SetEntrypoint("reportMetrics");
2106+
2107+
RunEngine(shell.get(), std::move(configuration));
2108+
2109+
task_runner->PostTask([&]() {
2110+
shell->GetPlatformView()->SetViewportMetrics({0.0, 400, 200});
2111+
task_runner->PostTask([&]() {
2112+
shell->GetPlatformView()->SetViewportMetrics({0.8, 0.0, 200});
2113+
task_runner->PostTask([&]() {
2114+
shell->GetPlatformView()->SetViewportMetrics({0.8, 400, 0.0});
2115+
task_runner->PostTask([&]() {
2116+
shell->GetPlatformView()->SetViewportMetrics({0.8, 400, 200.0});
2117+
});
2118+
});
2119+
});
2120+
});
2121+
latch.Wait();
2122+
ASSERT_EQ(last_device_pixel_ratio, 0.8);
2123+
ASSERT_EQ(last_width, 400.0);
2124+
ASSERT_EQ(last_height, 200.0);
2125+
latch.Reset();
2126+
2127+
task_runner->PostTask([&]() {
2128+
shell->GetPlatformView()->SetViewportMetrics({1.2, 600, 300});
2129+
});
2130+
latch.Wait();
2131+
ASSERT_EQ(last_device_pixel_ratio, 1.2);
2132+
ASSERT_EQ(last_width, 600.0);
2133+
ASSERT_EQ(last_height, 300.0);
2134+
2135+
DestroyShell(std::move(shell), std::move(task_runners));
2136+
}
2137+
20692138
} // namespace testing
20702139
} // namespace flutter

0 commit comments

Comments
 (0)