Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f4901d3

Browse files
committed
Restore snapping on scale/translate only pictures
It turns out that our ios32 (iPhone42) performacne can regress a lot without snapping. My theory is that although the picture boudns has a fractional top left corner, many drawing operations inside the picture have integral coordinations. In older hardwares, keeping those coordinates integeral seems to be performance critical. To avoid flutter/flutter#41654, the snapping will still be disabled if the matrix has non-scale-translation transformations.
1 parent 358309f commit f4901d3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

flow/layers/picture_layer.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
2424
if (auto* cache = context->raster_cache) {
2525
TRACE_EVENT0("flutter", "PictureLayer::RasterCache (Preroll)");
2626

27-
cache->Prepare(context->gr_context, sk_picture, matrix,
27+
SkMatrix ctm = matrix;
28+
ctm.postTranslate(offset_.x(), offset_.y());
29+
#ifndef SUPPORT_FRACTIONAL_TRANSLATION
30+
ctm = RasterCache::GetIntegralTransCTM(ctm);
31+
#endif
32+
cache->Prepare(context->gr_context, sk_picture, ctm,
2833
context->dst_color_space, is_complex_, will_change_);
2934
}
3035

@@ -39,6 +44,10 @@ void PictureLayer::Paint(PaintContext& context) const {
3944

4045
SkAutoCanvasRestore save(context.leaf_nodes_canvas, true);
4146
context.leaf_nodes_canvas->translate(offset_.x(), offset_.y());
47+
#ifndef SUPPORT_FRACTIONAL_TRANSLATION
48+
context.leaf_nodes_canvas->setMatrix(RasterCache::GetIntegralTransCTM(
49+
context.leaf_nodes_canvas->getTotalMatrix()));
50+
#endif
4251

4352
if (context.raster_cache &&
4453
context.raster_cache->Draw(*picture(), *context.leaf_nodes_canvas)) {

flow/raster_cache.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ class RasterCache {
6262
return bounds;
6363
}
6464

65+
/**
66+
* @brief Snap the translation components of the matrix to integers.
67+
*
68+
* The snapping will only happen if the matrix only has scale and translation
69+
* transformations.
70+
*
71+
* @param ctm the current transformation matrix.
72+
* @return SkMatrix the snapped transformation matrix.
73+
*/
74+
static SkMatrix GetIntegralTransCTM(const SkMatrix& ctm) {
75+
// Avoid integral snapping if the matrix has complex transformation to avoid
76+
// the artifact observed in https://github.com/flutter/flutter/issues/41654.
77+
if (!ctm.isScaleTranslate()) {
78+
return ctm;
79+
}
80+
SkMatrix result = ctm;
81+
result[SkMatrix::kMTransX] = SkScalarRoundToScalar(ctm.getTranslateX());
82+
result[SkMatrix::kMTransY] = SkScalarRoundToScalar(ctm.getTranslateY());
83+
return result;
84+
}
85+
6586
// Return true if the cache is generated.
6687
//
6788
// We may return false and not generate the cache if

0 commit comments

Comments
 (0)