ui: Fix NinePatchPainter to work in physical pixels.

This fixes NinePatchPainter to do layout in physical pixels after the
change to use slimming-paint in the ui compositor. Instead of using
the matrix on the canvas (which won't be meaningful for scale), it uses
the gfx::Canvas::SaveAndUnscale() method to move into a physical space
and do layout there.

R=oshima, sky
BUG=539496

Review URL: https://codereview.chromium.org/1424983005

Cr-Commit-Position: refs/heads/master@{#356983}
diff --git a/ui/gfx/canvas.cc b/ui/gfx/canvas.cc
index b228cd8..2465cfae 100644
--- a/ui/gfx/canvas.cc
+++ b/ui/gfx/canvas.cc
@@ -54,10 +54,9 @@
 }
 
 Canvas::Canvas()
-    : image_scale_(1.0),
+    : image_scale_(1.f),
       owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
-      canvas_(owned_canvas_.get()) {
-}
+      canvas_(owned_canvas_.get()) {}
 
 Canvas::Canvas(SkCanvas* canvas, float image_scale)
     : image_scale_(image_scale), owned_canvas_(), canvas_(canvas) {
@@ -385,63 +384,26 @@
                           int dest_h,
                           bool filter,
                           const SkPaint& paint) {
-  DrawImageIntHelper(image, src_x, src_y, src_w, src_h, dest_x, dest_y, dest_w,
-                     dest_h, filter, paint, image_scale_, false);
+  const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_);
+  if (image_rep.is_null())
+    return;
+  DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y,
+                     dest_w, dest_h, filter, paint, false);
 }
 
-void Canvas::DrawImageIntInPixel(const ImageSkia& image,
-                                 int src_x,
-                                 int src_y,
-                                 int src_w,
-                                 int src_h,
+void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep,
                                  int dest_x,
                                  int dest_y,
                                  int dest_w,
                                  int dest_h,
                                  bool filter,
                                  const SkPaint& paint) {
-  // All values passed into this function are in pixels, i.e. no scaling needs
-  // be done.
-  // Logic as below:-
-  // 1. Get the matrix transform from the canvas.
-  // 2. Set the scale in the matrix to 1.0 while honoring the direction of the
-  //    the scale (x/y). Example RTL layouts.
-  // 3. Round off the X and Y translation components in the matrix. This is to
-  //    reduce floating point errors during rect transformation. This is needed
-  //    for fractional scale factors like 1.25/1.5, etc.
-  // 4. Save the current state of the canvas and restore the state when going
-  //    out of scope with ScopedCanvas.
-  // 5. Set the modified matrix in the canvas. This ensures that no scaling
-  //    will be done for draw operations on the canvas.
-  // 6. Draw the image.
-  SkMatrix matrix = canvas_->getTotalMatrix();
-
-  // Ensure that the direction of the x and y scales is preserved. This is
-  // important for RTL layouts.
-  matrix.setScaleX(matrix.getScaleX() > 0 ? 1.0f : -1.0f);
-  matrix.setScaleY(matrix.getScaleY() > 0 ? 1.0f : -1.0f);
-
-  // Floor so that we get consistent rounding.
-  matrix.setTranslateX(SkScalarFloorToScalar(matrix.getTranslateX()));
-  matrix.setTranslateY(SkScalarFloorToScalar(matrix.getTranslateY()));
-
-  ScopedCanvas scoper(this);
-
-  canvas_->setMatrix(matrix);
-
-  DrawImageIntHelper(image,
-                     src_x,
-                     src_y,
-                     src_w,
-                     src_h,
-                     dest_x,
-                     dest_y,
-                     dest_w,
-                     dest_h,
-                     filter,
-                     paint,
-                     image_scale_,
-                     true);
+  int src_x = 0;
+  int src_y = 0;
+  int src_w = image_rep.pixel_width();
+  int src_h = image_rep.pixel_height();
+  DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y,
+                     dest_w, dest_h, filter, paint, true);
 }
 
 void Canvas::DrawImageInPath(const ImageSkia& image,
@@ -554,7 +516,7 @@
                                rect.width(), rect.height());
 }
 
-void Canvas::DrawImageIntHelper(const ImageSkia& image,
+void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep,
                                 int src_x,
                                 int src_y,
                                 int src_w,
@@ -565,7 +527,6 @@
                                 int dest_h,
                                 bool filter,
                                 const SkPaint& paint,
-                                float image_scale,
                                 bool pixel) {
   DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
               src_y + src_h < std::numeric_limits<int16_t>::max());
@@ -580,10 +541,6 @@
   float user_scale_x = static_cast<float>(dest_w) / src_w;
   float user_scale_y = static_cast<float>(dest_h) / src_h;
 
-  const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale);
-  if (image_rep.is_null())
-    return;
-
   SkRect dest_rect = { SkIntToScalar(dest_x),
                        SkIntToScalar(dest_y),
                        SkIntToScalar(dest_x + dest_w),