[cc] Allow LCD text for non-uniform scales

In r872117 I added support for non-uniform scales, but I made a mistake
in PictureLayerImpl::CalculateRasterTranslation. The code would compare
each scale component of the transform matrix with each component of the
raster contents scale, returning false for non-uniform scales, thus
avoiding LCD text.

This patch compares X scales with X scales, and Y scales with Y scales.

Bug: 1119996

TEST=LegacySWPictureLayerImplTest.UpdateLCDTextPushToActiveTreeWith2dScale

Change-Id: Id9029be2a2c49232a8b1609d1f6e16b3142d89ba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2825899
Reviewed-by: Xianzhu Wang <wangxianzhu@chromium.org>
Commit-Queue: Oriol Brufau <obrufau@igalia.com>
Cr-Commit-Position: refs/heads/master@{#872974}
diff --git a/cc/layers/picture_layer_impl.cc b/cc/layers/picture_layer_impl.cc
index c03471b..b1399d6 100644
--- a/cc/layers/picture_layer_impl.cc
+++ b/cc/layers/picture_layer_impl.cc
@@ -1654,15 +1654,15 @@
   // a layer of size 10000px does not exceed 0.001px.
   static constexpr float kPixelErrorThreshold = 0.001f;
   static constexpr float kScaleErrorThreshold = kPixelErrorThreshold / 10000;
-  auto is_raster_scale = [this](float scale) -> bool {
-    return std::abs(scale - raster_contents_scale_.x()) <=
+  auto is_raster_scale = [this](const SkMatrix44& matrix) -> bool {
+    // The matrix has the X scale at (0,0), and the Y scale at (1,1).
+    return std::abs(matrix.getFloat(0, 0) - raster_contents_scale_.x()) <=
                kScaleErrorThreshold &&
-           std::abs(scale - raster_contents_scale_.y()) <= kScaleErrorThreshold;
+           std::abs(matrix.getFloat(1, 1) - raster_contents_scale_.y()) <=
+               kScaleErrorThreshold;
   };
-  if (!is_raster_scale(screen_transform.matrix().getFloat(0, 0)) ||
-      !is_raster_scale(screen_transform.matrix().getFloat(1, 1)) ||
-      !is_raster_scale(draw_transform.matrix().getFloat(0, 0)) ||
-      !is_raster_scale(draw_transform.matrix().getFloat(1, 1))) {
+  if (!is_raster_scale(screen_transform.matrix()) ||
+      !is_raster_scale(draw_transform.matrix())) {
     return false;
   }
 
diff --git a/cc/layers/picture_layer_impl_unittest.cc b/cc/layers/picture_layer_impl_unittest.cc
index c45146a..1e84ca0 100644
--- a/cc/layers/picture_layer_impl_unittest.cc
+++ b/cc/layers/picture_layer_impl_unittest.cc
@@ -149,27 +149,46 @@
   }
 
   void SetupDrawProperties(FakePictureLayerImpl* layer,
-                           float ideal_contents_scale,
+                           const gfx::Vector2dF& ideal_contents_scale,
                            float device_scale_factor,
                            float page_scale_factor) {
     layer->layer_tree_impl()->SetDeviceScaleFactor(device_scale_factor);
     host_impl()->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
 
     gfx::Transform scale_transform;
-    scale_transform.Scale(ideal_contents_scale, ideal_contents_scale);
+    scale_transform.Scale(ideal_contents_scale.x(), ideal_contents_scale.y());
     layer->draw_properties().screen_space_transform = scale_transform;
     layer->draw_properties().target_space_transform = scale_transform;
     layer->set_contributes_to_drawn_render_surface(true);
-    DCHECK_EQ(layer->GetIdealContentsScaleKey(), ideal_contents_scale);
+    DCHECK_EQ(layer->GetIdealContentsScale(), ideal_contents_scale);
+  }
+
+  void SetupDrawProperties(FakePictureLayerImpl* layer,
+                           float ideal_contents_scale,
+                           float device_scale_factor,
+                           float page_scale_factor) {
+    SetupDrawProperties(
+        layer, gfx::Vector2dF(ideal_contents_scale, ideal_contents_scale),
+        device_scale_factor, page_scale_factor);
+  }
+
+  void SetupDrawPropertiesAndUpdateTiles(
+      FakePictureLayerImpl* layer,
+      const gfx::Vector2dF& ideal_contents_scale,
+      float device_scale_factor,
+      float page_scale_factor) {
+    SetupDrawProperties(layer, ideal_contents_scale, device_scale_factor,
+                        page_scale_factor);
+    layer->UpdateTiles();
   }
 
   void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
                                          float ideal_contents_scale,
                                          float device_scale_factor,
                                          float page_scale_factor) {
-    SetupDrawProperties(layer, ideal_contents_scale, device_scale_factor,
-                        page_scale_factor);
-    layer->UpdateTiles();
+    SetupDrawPropertiesAndUpdateTiles(
+        layer, gfx::Vector2dF(ideal_contents_scale, ideal_contents_scale),
+        device_scale_factor, page_scale_factor);
   }
 
   static void VerifyAllPrioritizedTilesExistAndHaveRasterSource(
@@ -5250,6 +5269,44 @@
     EXPECT_FALSE(tile->can_use_lcd_text());
 }
 
+TEST_F(LegacySWPictureLayerImplTest, UpdateLCDTextPushToActiveTreeWith2dScale) {
+  SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
+  gfx::Vector2dF ideal_scale(4.f, 2.f);
+  float page_scale = 4.f;
+  SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_scale, 1.0f,
+                                    page_scale);
+  EXPECT_TRUE(pending_layer()->can_use_lcd_text());
+  EXPECT_TRUE(pending_layer()->HighResTiling()->can_use_lcd_text());
+  ActivateTree();
+
+  EXPECT_TRUE(active_layer()->can_use_lcd_text());
+  ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
+  ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
+  std::vector<Tile*> tiles =
+      active_layer()->HighResTiling()->AllTilesForTesting();
+  for (Tile* tile : tiles)
+    EXPECT_TRUE(tile->can_use_lcd_text());
+  for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
+    EXPECT_FALSE(tile->can_use_lcd_text());
+
+  SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
+  SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_scale, 1.0f,
+                                    page_scale);
+  pending_layer()->SetContentsOpaque(false);
+  pending_layer()->UpdateTiles();
+  EXPECT_FALSE(pending_layer()->can_use_lcd_text());
+  EXPECT_FALSE(pending_layer()->HighResTiling()->can_use_lcd_text());
+  ActivateTree();
+
+  EXPECT_FALSE(active_layer()->can_use_lcd_text());
+  ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
+  ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
+  for (Tile* tile : active_layer()->HighResTiling()->AllTilesForTesting())
+    EXPECT_FALSE(tile->can_use_lcd_text());
+  for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
+    EXPECT_FALSE(tile->can_use_lcd_text());
+}
+
 TEST_F(LegacySWPictureLayerImplTest, TilingAllTilesDone) {
   gfx::Size tile_size = host_impl()->settings().default_tile_size;
   size_t tile_mem = 4 * tile_size.width() * tile_size.height();