From 728640769bd45a36b712077a659f984b43dd708e Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 13 Jun 2022 16:43:05 -0700 Subject: [PATCH] dont pixel snap raster cache if SUPPORT_FRACTIONAL_TRANSLATION is enabled (flutter/engine#34002) --- .../src/flutter/flow/layer_snapshot_store.cc | 2 +- .../src/flutter/flow/layer_snapshot_store.h | 6 +++--- .../flutter/flow/layers/display_list_layer.cc | 2 +- engine/src/flutter/flow/raster_cache.cc | 20 +++++++++++-------- engine/src/flutter/flow/raster_cache.h | 9 +++++---- .../flutter/flow/raster_cache_unittests.cc | 13 ++++++++++++ .../flutter/flow/testing/mock_raster_cache.cc | 8 ++++---- .../flutter/flow/testing/mock_raster_cache.h | 8 +++++--- engine/src/flutter/shell/common/shell.cc | 2 +- 9 files changed, 45 insertions(+), 25 deletions(-) diff --git a/engine/src/flutter/flow/layer_snapshot_store.cc b/engine/src/flutter/flow/layer_snapshot_store.cc index 7e3cc8e6f8..00f0ed9463 100644 --- a/engine/src/flutter/flow/layer_snapshot_store.cc +++ b/engine/src/flutter/flow/layer_snapshot_store.cc @@ -12,7 +12,7 @@ namespace flutter { LayerSnapshotData::LayerSnapshotData(int64_t layer_unique_id, const fml::TimeDelta& duration, const sk_sp& snapshot, - const SkIRect& bounds) + const SkRect& bounds) : layer_unique_id_(layer_unique_id), duration_(duration), snapshot_(snapshot), diff --git a/engine/src/flutter/flow/layer_snapshot_store.h b/engine/src/flutter/flow/layer_snapshot_store.h index fd255b5186..eb444d2dcc 100644 --- a/engine/src/flutter/flow/layer_snapshot_store.h +++ b/engine/src/flutter/flow/layer_snapshot_store.h @@ -26,7 +26,7 @@ class LayerSnapshotData { LayerSnapshotData(int64_t layer_unique_id, const fml::TimeDelta& duration, const sk_sp& snapshot, - const SkIRect& bounds); + const SkRect& bounds); ~LayerSnapshotData() = default; @@ -36,13 +36,13 @@ class LayerSnapshotData { sk_sp GetSnapshot() const { return snapshot_; } - SkIRect GetBounds() const { return bounds_; } + SkRect GetBounds() const { return bounds_; } private: const int64_t layer_unique_id_; const fml::TimeDelta duration_; const sk_sp snapshot_; - const SkIRect bounds_; + const SkRect bounds_; }; /// Collects snapshots of layers during frame rasterization. diff --git a/engine/src/flutter/flow/layers/display_list_layer.cc b/engine/src/flutter/flow/layers/display_list_layer.cc index 5c02735da5..7ccea1e56c 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.cc +++ b/engine/src/flutter/flow/layers/display_list_layer.cc @@ -155,7 +155,7 @@ void DisplayListLayer::Paint(PaintContext& context) const { const fml::TimeDelta offscreen_render_time = fml::TimePoint::Now() - start_time; - const SkIRect device_bounds = + const SkRect device_bounds = RasterCache::GetDeviceBounds(paint_bounds(), ctm); sk_sp raster_data = offscreen_surface->GetRasterData(true); LayerSnapshotData snapshot_data(unique_id(), offscreen_render_time, diff --git a/engine/src/flutter/flow/raster_cache.cc b/engine/src/flutter/flow/raster_cache.cc index b5d16894db..e6dbf96870 100644 --- a/engine/src/flutter/flow/raster_cache.cc +++ b/engine/src/flutter/flow/raster_cache.cc @@ -29,11 +29,11 @@ RasterCacheResult::RasterCacheResult(sk_sp image, void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const { TRACE_EVENT0("flutter", "RasterCacheResult::draw"); SkAutoCanvasRestore auto_restore(&canvas, true); - SkIRect bounds = + + SkRect bounds = RasterCache::GetDeviceBounds(logical_rect_, canvas.getTotalMatrix()); - FML_DCHECK( - std::abs(bounds.size().width() - image_->dimensions().width()) <= 1 && - std::abs(bounds.size().height() - image_->dimensions().height()) <= 1); + FML_DCHECK(std::abs(bounds.width() - image_->dimensions().width()) <= 1 && + std::abs(bounds.height() - image_->dimensions().height()) <= 1); canvas.resetMatrix(); flow_.Step(); canvas.drawImage(image_, bounds.fLeft, bounds.fTop, SkSamplingOptions(), @@ -125,10 +125,14 @@ static std::unique_ptr Rasterize( const char* type, const std::function& draw_function) { TRACE_EVENT0("flutter", "RasterCachePopulate"); - SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); - const SkImageInfo image_info = SkImageInfo::MakeN32Premul( - cache_rect.width(), cache_rect.height(), sk_ref_sp(dst_color_space)); + SkRect dest_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); + // we always round out here so that the texture is integer sized. + int width = SkScalarCeilToInt(dest_rect.width()); + int height = SkScalarCeilToInt(dest_rect.height()); + + const SkImageInfo image_info = + SkImageInfo::MakeN32Premul(width, height, sk_ref_sp(dst_color_space)); sk_sp surface = context @@ -141,7 +145,7 @@ static std::unique_ptr Rasterize( SkCanvas* canvas = surface->getCanvas(); canvas->clear(SK_ColorTRANSPARENT); - canvas->translate(-cache_rect.left(), -cache_rect.top()); + canvas->translate(-dest_rect.left(), -dest_rect.top()); canvas->concat(ctm); draw_function(canvas); diff --git a/engine/src/flutter/flow/raster_cache.h b/engine/src/flutter/flow/raster_cache.h index 51d1d17e7f..f3dbac5b7e 100644 --- a/engine/src/flutter/flow/raster_cache.h +++ b/engine/src/flutter/flow/raster_cache.h @@ -149,12 +149,13 @@ class RasterCache { const SkMatrix& ctm, bool checkerboard) const; - static SkIRect GetDeviceBounds(const SkRect& rect, const SkMatrix& ctm) { + static SkRect GetDeviceBounds(const SkRect& rect, const SkMatrix& ctm) { SkRect device_rect; ctm.mapRect(&device_rect, rect); - SkIRect bounds; - device_rect.roundOut(&bounds); - return bounds; +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + device_rect.roundOut(&device_rect); +#endif + return device_rect; } /** diff --git a/engine/src/flutter/flow/raster_cache_unittests.cc b/engine/src/flutter/flow/raster_cache_unittests.cc index ff0234d41c..cfb37b87fd 100644 --- a/engine/src/flutter/flow/raster_cache_unittests.cc +++ b/engine/src/flutter/flow/raster_cache_unittests.cc @@ -330,6 +330,19 @@ TEST(RasterCache, DeviceRectRoundOutForSkPicture) { ASSERT_TRUE(cache.Draw(*picture, canvas)); } +// The device rect is pixel snapped if SUPPORT_FRACTIONAL_TRANSLATION +// is disabled. +TEST(RasterCache, ComputeDeviceRectBasedOnFractionalTranslation) { + SkRect logical_rect = SkRect::MakeLTRB(0, 0, 300.2, 300.3); + SkMatrix ctm = SkMatrix::MakeAll(2.0, 0, 0, 0, 2.0, 0, 0, 0, 1); + auto result = RasterCache::GetDeviceBounds(logical_rect, ctm); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + ASSERT_EQ(result, SkRect::MakeLTRB(0.0, 0.0, 601.0, 601.0)); +#else + ASSERT_EQ(result, SkRect::MakeLTRB(0.0, 0.0, 600.4, 600.6)); +#endif +} + // Construct a cache result whose device target rectangle rounds out to be one // pixel wider than the cached image. Verify that it can be drawn without // triggering any assertions. diff --git a/engine/src/flutter/flow/testing/mock_raster_cache.cc b/engine/src/flutter/flow/testing/mock_raster_cache.cc index dafa827461..4f1cf2c278 100644 --- a/engine/src/flutter/flow/testing/mock_raster_cache.cc +++ b/engine/src/flutter/flow/testing/mock_raster_cache.cc @@ -9,7 +9,7 @@ namespace flutter { namespace testing { -MockRasterCacheResult::MockRasterCacheResult(SkIRect device_rect) +MockRasterCacheResult::MockRasterCacheResult(SkRect device_rect) : RasterCacheResult(nullptr, SkRect::MakeEmpty(), "RasterCacheFlow::test"), device_rect_(device_rect) {} @@ -20,7 +20,7 @@ std::unique_ptr MockRasterCache::RasterizePicture( SkColorSpace* dst_color_space, bool checkerboard) const { SkRect logical_rect = picture->cullRect(); - SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); + SkRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); return std::make_unique(cache_rect); } @@ -32,7 +32,7 @@ std::unique_ptr MockRasterCache::RasterizeDisplayList( SkColorSpace* dst_color_space, bool checkerboard) const { SkRect logical_rect = display_list->bounds(); - SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); + SkRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); return std::make_unique(cache_rect); } @@ -44,7 +44,7 @@ std::unique_ptr MockRasterCache::RasterizeLayer( const SkMatrix& ctm, bool checkerboard) const { SkRect logical_rect = layer->paint_bounds(); - SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); + SkRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); return std::make_unique(cache_rect); } diff --git a/engine/src/flutter/flow/testing/mock_raster_cache.h b/engine/src/flutter/flow/testing/mock_raster_cache.h index 1bd54ab62c..d9921728d1 100644 --- a/engine/src/flutter/flow/testing/mock_raster_cache.h +++ b/engine/src/flutter/flow/testing/mock_raster_cache.h @@ -26,11 +26,13 @@ namespace testing { */ class MockRasterCacheResult : public RasterCacheResult { public: - explicit MockRasterCacheResult(SkIRect device_rect); + explicit MockRasterCacheResult(SkRect device_rect); void draw(SkCanvas& canvas, const SkPaint* paint = nullptr) const override{}; - SkISize image_dimensions() const override { return device_rect_.size(); }; + SkISize image_dimensions() const override { + return SkSize::Make(device_rect_.width(), device_rect_.height()).toCeil(); + }; int64_t image_bytes() const override { return image_dimensions().area() * @@ -38,7 +40,7 @@ class MockRasterCacheResult : public RasterCacheResult { } private: - SkIRect device_rect_; + SkRect device_rect_; }; /** diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index faa84613a7..eb95199756 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -1819,7 +1819,7 @@ static rapidjson::Value SerializeLayerSnapshot( result.AddMember("duration_micros", snapshot.GetDuration().ToMicroseconds(), allocator); - const SkIRect bounds = snapshot.GetBounds(); + const SkRect bounds = snapshot.GetBounds(); result.AddMember("top", bounds.top(), allocator); result.AddMember("left", bounds.left(), allocator); result.AddMember("width", bounds.width(), allocator);