Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect devicePixelRatio during simplification & avoid needless simplification/projection cache invalidation #1812

Merged
merged 6 commits into from
Feb 6, 2024
52 changes: 36 additions & 16 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,24 @@ class _PolygonLayerState extends State<PolygonLayer> {
List<_ProjectedPolygon>? _cachedProjectedPolygons;
final _cachedSimplifiedPolygons = <int, List<_ProjectedPolygon>>{};

double? _devicePixelRatio;

@override
void didUpdateWidget(PolygonLayer oldWidget) {
super.didUpdateWidget(oldWidget);

// Reuse cache
if (widget.simplificationTolerance != 0 &&
oldWidget.simplificationTolerance == widget.simplificationTolerance &&
listEquals(oldWidget.polygons, widget.polygons)) return;

_cachedSimplifiedPolygons.clear();
_cachedProjectedPolygons = null;
if (!listEquals(oldWidget.polygons, widget.polygons)) {
// If the polylines have changed, then both the projections and the
// projection-dependendent simplifications must be invalidated
_cachedProjectedPolygons = null;
_cachedSimplifiedPolygons.clear();
} else if (oldWidget.simplificationTolerance !=
widget.simplificationTolerance) {
// If only the simplification tolerance has changed, this does not affect
// the projections (as that is done before simplification), so only
// invalidate the simplifications
_cachedSimplifiedPolygons.clear();
}
}

@override
Expand All @@ -91,14 +98,25 @@ class _PolygonLayerState extends State<PolygonLayer> {
growable: false,
);

final simplified = widget.simplificationTolerance <= 0
? projected
: _cachedSimplifiedPolygons[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
polygons: projected,
pixelTolerance: widget.simplificationTolerance,
camera: camera,
);
late final List<_ProjectedPolygon> simplified;
if (widget.simplificationTolerance == 0) {
simplified = projected;
} else {
// If the DPR has changed, invalidate the simplification cache
final newDPR = MediaQuery.devicePixelRatioOf(context);
if (newDPR != _devicePixelRatio) {
_devicePixelRatio = newDPR;
_cachedSimplifiedPolygons.clear();
}

simplified = _cachedSimplifiedPolygons[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
camera: camera,
polygons: projected,
pixelTolerance: widget.simplificationTolerance,
devicePixelRatio: newDPR,
);
}

final culled = !widget.polygonCulling
? simplified
Expand All @@ -122,14 +140,16 @@ class _PolygonLayerState extends State<PolygonLayer> {
}

static List<_ProjectedPolygon> _computeZoomLevelSimplification({
required MapCamera camera,
required List<_ProjectedPolygon> polygons,
required double pixelTolerance,
required MapCamera camera,
required double devicePixelRatio,
}) {
final tolerance = getEffectiveSimplificationTolerance(
crs: camera.crs,
zoom: camera.zoom.floor(),
pixelTolerance: pixelTolerance,
devicePixelRatio: devicePixelRatio,
);

return List<_ProjectedPolygon>.generate(
Expand Down
84 changes: 52 additions & 32 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,27 @@ class PolylineLayer<R extends Object> extends StatefulWidget {
}

class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
List<_ProjectedPolyline>? _cachedProjectedPolylines;
final _cachedSimplifiedPolylines = <int, List<_ProjectedPolyline>>{};
List<_ProjectedPolyline<R>>? _cachedProjectedPolylines;
final _cachedSimplifiedPolylines = <int, List<_ProjectedPolyline<R>>>{};

final _culledPolylines =
<_ProjectedPolyline>[]; // Avoids repetitive memory reallocation
double? _devicePixelRatio;

@override
void didUpdateWidget(PolylineLayer<R> oldWidget) {
super.didUpdateWidget(oldWidget);

// Reuse cache
if (widget.simplificationTolerance != 0 &&
oldWidget.simplificationTolerance == widget.simplificationTolerance &&
listEquals(oldWidget.polylines, widget.polylines)) return;

_cachedSimplifiedPolylines.clear();
_cachedProjectedPolylines = null;
if (!listEquals(oldWidget.polylines, widget.polylines)) {
// If the polylines have changed, then both the projections and the
// projection-dependendent simplifications must be invalidated
_cachedProjectedPolylines = null;
_cachedSimplifiedPolylines.clear();
} else if (oldWidget.simplificationTolerance !=
widget.simplificationTolerance) {
// If only the simplification tolerance has changed, this does not affect
// the projections (as that is done before simplification), so only
// invalidate the simplifications
_cachedSimplifiedPolylines.clear();
}
}

@override
Expand All @@ -105,14 +109,25 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
growable: false,
);

final simplified = widget.simplificationTolerance == 0
? projected
: _cachedSimplifiedPolylines[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
polylines: projected,
pixelTolerance: widget.simplificationTolerance,
camera: camera,
);
late final List<_ProjectedPolyline<R>> simplified;
if (widget.simplificationTolerance == 0) {
simplified = projected;
} else {
// If the DPR has changed, invalidate the simplification cache
final newDPR = MediaQuery.devicePixelRatioOf(context);
if (newDPR != _devicePixelRatio) {
_devicePixelRatio = newDPR;
_cachedSimplifiedPolylines.clear();
}

simplified = _cachedSimplifiedPolylines[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
camera: camera,
polylines: projected,
pixelTolerance: widget.simplificationTolerance,
devicePixelRatio: newDPR,
);
}

final culled = widget.cullingMargin == null
? simplified
Expand All @@ -136,13 +151,13 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
);
}

List<_ProjectedPolyline> _aggressivelyCullPolylines({
static List<_ProjectedPolyline> _aggressivelyCullPolylines({
required Projection projection,
required List<_ProjectedPolyline> polylines,
required MapCamera camera,
required double cullingMargin,
}) {
_culledPolylines.clear();
final culledPolylines = <_ProjectedPolyline>[];

final bounds = camera.visibleBounds;
final margin = cullingMargin / math.pow(2, camera.zoom);
Expand Down Expand Up @@ -171,7 +186,7 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {

// Gradient poylines cannot be easily segmented
if (polyline.gradientColors != null) {
_culledPolylines.add(projectedPolyline);
culledPolylines.add(projectedPolyline);
continue;
}

Expand All @@ -191,10 +206,12 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
} else {
// If we cannot see this segment but have seen previous ones, flush the last polyline fragment.
if (start != -1) {
_culledPolylines.add(_ProjectedPolyline._(
polyline: polyline,
points: projectedPolyline.points.sublist(start, i + 1),
));
culledPolylines.add(
_ProjectedPolyline._(
polyline: polyline,
points: projectedPolyline.points.sublist(start, i + 1),
),
);

// Reset start.
start = -1;
Expand All @@ -205,7 +222,7 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
// If the last segment was visible push that last visible polyline
// fragment, which may also be the entire polyline if `start == 0`.
if (containsSegment) {
_culledPolylines.add(
culledPolylines.add(
start == 0
? projectedPolyline
: _ProjectedPolyline._(
Expand All @@ -217,21 +234,24 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
}
}

return _culledPolylines;
return culledPolylines;
}

static List<_ProjectedPolyline> _computeZoomLevelSimplification({
required List<_ProjectedPolyline> polylines,
required double pixelTolerance,
static List<_ProjectedPolyline<R>>
_computeZoomLevelSimplification<R extends Object>({
required MapCamera camera,
required List<_ProjectedPolyline<R>> polylines,
required double pixelTolerance,
required double devicePixelRatio,
}) {
final tolerance = getEffectiveSimplificationTolerance(
crs: camera.crs,
zoom: camera.zoom.floor(),
pixelTolerance: pixelTolerance,
devicePixelRatio: devicePixelRatio,
);

return List<_ProjectedPolyline>.generate(
return List<_ProjectedPolyline<R>>.generate(
polylines.length,
(i) {
final polyline = polylines[i];
Expand Down
10 changes: 6 additions & 4 deletions lib/src/misc/simplify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ double getEffectiveSimplificationTolerance({
required Crs crs,
required int zoom,
required double pixelTolerance,
required double devicePixelRatio,
}) {
if (pixelTolerance <= 0) return 0;

final (x0, y0) = crs.untransform(0, 0, crs.scale(zoom.toDouble()));
final scale = crs.scale(zoom.toDouble());
final (x0, y0) = crs.untransform(0, 0, scale);
final (x1, y1) = crs.untransform(
pixelTolerance,
pixelTolerance,
crs.scale(zoom.toDouble()),
pixelTolerance * devicePixelRatio,
pixelTolerance * devicePixelRatio,
scale,
);

final dx = x1 - x0;
Expand Down
Loading