Skip to content

Commit a997769

Browse files
arbrenggspencergoog
authored andcommitted
fuchsia: Don't send ViewportMetrics w/ 0 DPR (flutter#21392)
1 parent cc191bc commit a997769

File tree

5 files changed

+539
-368
lines changed

5 files changed

+539
-368
lines changed

lib/ui/window/viewport_metrics.cc

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ namespace flutter {
1010

1111
ViewportMetrics::ViewportMetrics() = default;
1212

13+
ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
14+
double p_physical_width,
15+
double p_physical_height)
16+
: device_pixel_ratio(p_device_pixel_ratio),
17+
physical_width(p_physical_width),
18+
physical_height(p_physical_height) {}
19+
1320
ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
1421
double p_physical_width,
1522
double p_physical_height,
@@ -44,11 +51,42 @@ ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
4451
physical_system_gesture_inset_left(p_physical_system_gesture_inset_left) {
4552
}
4653

47-
ViewportMetrics::ViewportMetrics(double p_device_pixel_ratio,
48-
double p_physical_width,
49-
double p_physical_height)
50-
: device_pixel_ratio(p_device_pixel_ratio),
51-
physical_width(p_physical_width),
52-
physical_height(p_physical_height) {}
54+
bool operator==(const ViewportMetrics& a, const ViewportMetrics& b) {
55+
return a.device_pixel_ratio == b.device_pixel_ratio &&
56+
a.physical_width == b.physical_width &&
57+
a.physical_height == b.physical_height &&
58+
a.physical_padding_top == b.physical_padding_top &&
59+
a.physical_padding_right == b.physical_padding_right &&
60+
a.physical_padding_bottom == b.physical_padding_bottom &&
61+
a.physical_padding_left == b.physical_padding_left &&
62+
a.physical_view_inset_top == b.physical_view_inset_top &&
63+
a.physical_view_inset_right == b.physical_view_inset_right &&
64+
a.physical_view_inset_bottom == b.physical_view_inset_bottom &&
65+
a.physical_view_inset_left == b.physical_view_inset_left &&
66+
a.physical_system_gesture_inset_top ==
67+
b.physical_system_gesture_inset_top &&
68+
a.physical_system_gesture_inset_right ==
69+
b.physical_system_gesture_inset_right &&
70+
a.physical_system_gesture_inset_bottom ==
71+
b.physical_system_gesture_inset_bottom &&
72+
a.physical_system_gesture_inset_left ==
73+
b.physical_system_gesture_inset_left;
74+
}
75+
76+
std::ostream& operator<<(std::ostream& os, const ViewportMetrics& a) {
77+
os << "DPR: " << a.device_pixel_ratio << " "
78+
<< "Size: [" << a.physical_width << "W " << a.physical_height << "H] "
79+
<< "Padding: [" << a.physical_padding_top << "T "
80+
<< a.physical_padding_right << "R " << a.physical_padding_bottom << "B "
81+
<< a.physical_padding_left << "L] "
82+
<< "Insets: [" << a.physical_view_inset_top << "T "
83+
<< a.physical_view_inset_right << "R " << a.physical_view_inset_bottom
84+
<< "B " << a.physical_view_inset_left << "L] "
85+
<< "Gesture Insets: [" << a.physical_system_gesture_inset_top << "T "
86+
<< a.physical_system_gesture_inset_right << "R "
87+
<< a.physical_system_gesture_inset_bottom << "B "
88+
<< a.physical_system_gesture_inset_left << "L]";
89+
return os;
90+
}
5391

5492
} // namespace flutter

lib/ui/window/viewport_metrics.h

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
#ifndef FLUTTER_LIB_UI_WINDOW_VIEWPORT_METRICS_H_
66
#define FLUTTER_LIB_UI_WINDOW_VIEWPORT_METRICS_H_
77

8+
#include <ostream>
9+
810
namespace flutter {
911

1012
struct ViewportMetrics {
1113
ViewportMetrics();
14+
ViewportMetrics(double p_device_pixel_ratio,
15+
double p_physical_width,
16+
double p_physical_height);
1217
ViewportMetrics(double p_device_pixel_ratio,
1318
double p_physical_width,
1419
double p_physical_height,
@@ -25,12 +30,6 @@ struct ViewportMetrics {
2530
double p_physical_system_gesture_inset_bottom,
2631
double p_physical_system_gesture_inset_left);
2732

28-
// Create a ViewportMetrics instance that doesn't include depth, padding, or
29-
// insets.
30-
ViewportMetrics(double p_device_pixel_ratio,
31-
double p_physical_width,
32-
double p_physical_height);
33-
3433
double device_pixel_ratio = 1.0;
3534
double physical_width = 0;
3635
double physical_height = 0;
@@ -48,24 +47,8 @@ struct ViewportMetrics {
4847
double physical_system_gesture_inset_left = 0;
4948
};
5049

51-
struct LogicalSize {
52-
double width = 0.0;
53-
double height = 0.0;
54-
};
55-
56-
struct LogicalInset {
57-
double left = 0.0;
58-
double top = 0.0;
59-
double right = 0.0;
60-
double bottom = 0.0;
61-
};
62-
63-
struct LogicalMetrics {
64-
LogicalSize size;
65-
double scale = 1.0;
66-
LogicalInset padding;
67-
LogicalInset view_inset;
68-
};
50+
bool operator==(const ViewportMetrics& a, const ViewportMetrics& b);
51+
std::ostream& operator<<(std::ostream& os, const ViewportMetrics& a);
6952

7053
} // namespace flutter
7154

shell/platform/fuchsia/flutter/platform_view.cc

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
#include "flutter/fml/logging.h"
1515
#include "flutter/lib/ui/window/pointer_data.h"
1616
#include "flutter/lib/ui/window/window.h"
17+
#include "third_party/rapidjson/include/rapidjson/document.h"
18+
#include "third_party/rapidjson/include/rapidjson/stringbuffer.h"
19+
#include "third_party/rapidjson/include/rapidjson/writer.h"
20+
1721
#include "logging.h"
18-
#include "rapidjson/document.h"
19-
#include "rapidjson/stringbuffer.h"
20-
#include "rapidjson/writer.h"
2122
#include "runtime/dart/utils/inlines.h"
2223
#include "vsync_waiter.h"
2324

@@ -214,7 +215,8 @@ void PlatformView::OnScenicError(std::string error) {
214215
void PlatformView::OnScenicEvent(
215216
std::vector<fuchsia::ui::scenic::Event> events) {
216217
TRACE_EVENT0("flutter", "PlatformView::OnScenicEvent");
217-
bool should_update_metrics = false;
218+
219+
bool metrics_changed = false;
218220
for (const auto& event : events) {
219221
switch (event.Which()) {
220222
case fuchsia::ui::scenic::Event::Tag::kGfx:
@@ -223,31 +225,52 @@ void PlatformView::OnScenicEvent(
223225
const fuchsia::ui::gfx::Metrics& metrics =
224226
event.gfx().metrics().metrics;
225227
const float new_view_pixel_ratio = metrics.scale_x;
228+
if (new_view_pixel_ratio <= 0.f) {
229+
FML_DLOG(ERROR)
230+
<< "Got an invalid pixel ratio from Scenic; ignoring: "
231+
<< new_view_pixel_ratio;
232+
break;
233+
}
226234

227235
// Avoid metrics update when possible -- it is computationally
228236
// expensive.
229-
if (view_pixel_ratio_ != new_view_pixel_ratio) {
230-
view_pixel_ratio_ = new_view_pixel_ratio;
231-
should_update_metrics = true;
237+
if (view_pixel_ratio_.has_value() &&
238+
*view_pixel_ratio_ == new_view_pixel_ratio) {
239+
FML_DLOG(ERROR)
240+
<< "Got an identical pixel ratio from Scenic; ignoring: "
241+
<< new_view_pixel_ratio;
242+
break;
232243
}
244+
245+
view_pixel_ratio_ = new_view_pixel_ratio;
246+
metrics_changed = true;
233247
break;
234248
}
235249
case fuchsia::ui::gfx::Event::Tag::kViewPropertiesChanged: {
236250
const fuchsia::ui::gfx::BoundingBox& bounding_box =
237251
event.gfx().view_properties_changed().properties.bounding_box;
238-
const float new_view_width =
239-
std::max(bounding_box.max.x - bounding_box.min.x, 0.0f);
240-
const float new_view_height =
241-
std::max(bounding_box.max.y - bounding_box.min.y, 0.0f);
252+
const std::pair<float, float> new_view_size = {
253+
std::max(bounding_box.max.x - bounding_box.min.x, 0.0f),
254+
std::max(bounding_box.max.y - bounding_box.min.y, 0.0f)};
255+
if (new_view_size.first <= 0.f || new_view_size.second <= 0.f) {
256+
FML_DLOG(ERROR)
257+
<< "Got an invalid view size from Scenic; ignoring: "
258+
<< new_view_size.first << " " << new_view_size.second;
259+
break;
260+
}
242261

243262
// Avoid metrics update when possible -- it is computationally
244263
// expensive.
245-
if (view_width_ != new_view_width ||
246-
view_height_ != new_view_width) {
247-
view_width_ = new_view_width;
248-
view_height_ = new_view_height;
249-
should_update_metrics = true;
264+
if (view_logical_size_.has_value() &&
265+
*view_logical_size_ == new_view_size) {
266+
FML_DLOG(ERROR)
267+
<< "Got an identical view size from Scenic; ignoring: "
268+
<< new_view_size.first << " " << new_view_size.second;
269+
break;
250270
}
271+
272+
view_logical_size_ = new_view_size;
273+
metrics_changed = true;
251274
break;
252275
}
253276
case fuchsia::ui::gfx::Event::Tag::kViewConnected:
@@ -298,19 +321,22 @@ void PlatformView::OnScenicEvent(
298321
}
299322
}
300323

301-
if (should_update_metrics) {
324+
if (view_pixel_ratio_.has_value() && view_logical_size_.has_value() &&
325+
metrics_changed) {
326+
const float pixel_ratio = *view_pixel_ratio_;
327+
const std::pair<float, float> logical_size = *view_logical_size_;
302328
SetViewportMetrics({
303-
view_pixel_ratio_, // device_pixel_ratio
304-
view_width_ * view_pixel_ratio_, // physical_width
305-
view_height_ * view_pixel_ratio_, // physical_height
306-
0.0f, // physical_padding_top
307-
0.0f, // physical_padding_right
308-
0.0f, // physical_padding_bottom
309-
0.0f, // physical_padding_left
310-
0.0f, // physical_view_inset_top
311-
0.0f, // physical_view_inset_right
312-
0.0f, // physical_view_inset_bottom
313-
0.0f, // physical_view_inset_left
329+
pixel_ratio, // device_pixel_ratio
330+
logical_size.first * pixel_ratio, // physical_width
331+
logical_size.second * pixel_ratio, // physical_height
332+
0.0f, // physical_padding_top
333+
0.0f, // physical_padding_right
334+
0.0f, // physical_padding_bottom
335+
0.0f, // physical_padding_left
336+
0.0f, // physical_view_inset_top
337+
0.0f, // physical_view_inset_right
338+
0.0f, // physical_view_inset_bottom
339+
0.0f, // physical_view_inset_left
314340
0.0f, // p_physical_system_gesture_inset_top
315341
0.0f, // p_physical_system_gesture_inset_right
316342
0.0f, // p_physical_system_gesture_inset_bottom
@@ -421,15 +447,18 @@ bool PlatformView::OnHandlePointerEvent(
421447
PointerTraceHACK(pointer.radius_major, pointer.radius_minor);
422448
TRACE_FLOW_END("input", "dispatch_event_to_client", trace_id);
423449

450+
const float pixel_ratio =
451+
view_pixel_ratio_.has_value() ? *view_pixel_ratio_ : 0.f;
452+
424453
flutter::PointerData pointer_data;
425454
pointer_data.Clear();
426455
pointer_data.time_stamp = pointer.event_time / 1000;
427456
pointer_data.change = GetChangeFromPointerEventPhase(pointer.phase);
428457
pointer_data.kind = GetKindFromPointerType(pointer.type);
429458
pointer_data.device = pointer.pointer_id;
430459
// Pointer events are in logical pixels, so scale to physical.
431-
pointer_data.physical_x = pointer.x * view_pixel_ratio_;
432-
pointer_data.physical_y = pointer.y * view_pixel_ratio_;
460+
pointer_data.physical_x = pointer.x * pixel_ratio;
461+
pointer_data.physical_y = pointer.y * pixel_ratio;
433462
// Buttons are single bit values starting with kMousePrimaryButton = 1.
434463
pointer_data.buttons = static_cast<uint64_t>(pointer.buttons);
435464

@@ -601,7 +630,10 @@ void PlatformView::DispatchSemanticsAction(int32_t node_id,
601630
void PlatformView::UpdateSemantics(
602631
flutter::SemanticsNodeUpdates update,
603632
flutter::CustomAccessibilityActionUpdates actions) {
604-
accessibility_bridge_->AddSemanticsNodeUpdate(update, view_pixel_ratio_);
633+
const float pixel_ratio =
634+
view_pixel_ratio_.has_value() ? *view_pixel_ratio_ : 0.f;
635+
636+
accessibility_bridge_->AddSemanticsNodeUpdate(update, pixel_ratio);
605637
}
606638

607639
// Channel handler for kAccessibilityChannel

0 commit comments

Comments
 (0)