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

Commit 1bc3bf2

Browse files
author
Jonah Williams
committed
adjust round out, use logical origin
1 parent ba5f49c commit 1bc3bf2

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

flow/raster_cache.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
3232
TRACE_EVENT0("flutter", "RasterCacheResult::draw");
3333
SkAutoCanvasRestore auto_restore(&canvas, true);
3434

35+
SkRect raw_bounds = RasterCacheUtil::GetRawDeviceBounds(
36+
logical_rect_, canvas.getTotalMatrix());
37+
#ifndef NDEBUG
3538
SkIRect bounds =
3639
RasterCacheUtil::GetDeviceBounds(logical_rect_, canvas.getTotalMatrix());
37-
#ifndef NDEBUG
3840
// The image dimensions should always be larger than the device bounds and
3941
// smaller than the device bounds plus one pixel, at the same time, we must
4042
// introduce epsilon to solve the round-off error. The value of epsilon is
@@ -47,8 +49,8 @@ void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
4749
#endif
4850
canvas.resetMatrix();
4951
flow_.Step();
50-
canvas.drawImage(image_, bounds.fLeft, bounds.fTop, SkSamplingOptions(),
51-
paint);
52+
canvas.drawImage(image_, raw_bounds.fLeft, raw_bounds.fRight,
53+
SkSamplingOptions(), paint);
5254
}
5355

5456
RasterCache::RasterCache(size_t access_threshold,
@@ -67,6 +69,8 @@ std::unique_ptr<RasterCacheResult> RasterCache::Rasterize(
6769

6870
SkIRect cache_rect =
6971
RasterCacheUtil::GetDeviceBounds(context.logical_rect, context.matrix);
72+
SkRect raw_cache_rect =
73+
RasterCacheUtil::GetRawDeviceBounds(context.logical_rect, context.matrix);
7074
const SkImageInfo image_info =
7175
SkImageInfo::MakeN32Premul(cache_rect.width(), cache_rect.height(),
7276
sk_ref_sp(context.dst_color_space));
@@ -82,7 +86,7 @@ std::unique_ptr<RasterCacheResult> RasterCache::Rasterize(
8286

8387
SkCanvas* canvas = surface->getCanvas();
8488
canvas->clear(SK_ColorTRANSPARENT);
85-
canvas->translate(-cache_rect.left(), -cache_rect.top());
89+
canvas->translate(-raw_cache_rect.left(), -raw_cache_rect.top());
8690
canvas->concat(context.matrix);
8791
draw_function(canvas);
8892

flow/raster_cache_util.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,25 @@ struct RasterCacheUtil {
5353
SkRect device_rect;
5454
ctm.mapRect(&device_rect, rect);
5555
SkIRect bounds;
56-
device_rect.roundOut(&bounds);
56+
// Rather than roundOut, we first subtract the fractional portion of fLeft
57+
// and fTop from fRight and fBottom. This ensures that we are more likely to
58+
// round up to the nearest physical pixel in each direction instead of potentially
59+
// two physical pixels in each direction.
60+
auto fractionalLeft = bounds.fLeft - SkScalarFloorToInt(bounds.fLeft);
61+
auto fractionalTop = bounds.fTop - SkScalarFloorToInt(bounds.fTop);
62+
auto adjustedBottom = SkScalarCeilToInt(bounds.fBottom - fractionalTop);
63+
auto adjustedRight = SkScalarCeilToInt(bounds.fRight - fractionalLeft);
64+
bounds.setLTRB(SkScalarFloorToInt(bounds.fLeft),
65+
SkScalarFloorToInt(bounds.fTop), adjustedRight,
66+
adjustedBottom);
5767
return bounds;
5868
}
69+
70+
static SkRect GetRawDeviceBounds(const SkRect& rect, const SkMatrix& ctm) {
71+
SkRect device_rect;
72+
ctm.mapRect(&device_rect, rect);
73+
return device_rect;
74+
}
5975
};
6076

6177
} // namespace flutter

0 commit comments

Comments
 (0)