SkCanvas::adjustToTopLayer()

Given a matrix and a clip bounds, offsets them to reflect the difference
between device coordinates and global coordinates. Useful when a client
wants an OS-specific backing for a canvas.

R=reed@google.com
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1986383002

Review-Url: https://codereview.chromium.org/1986383002
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index b6d0cc7..ec14829 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -1238,6 +1238,12 @@
                                const SkPaint* paint,
                                SrcRectConstraint constraint = kStrict_SrcRectConstraint);
 
+    // expose minimum amount of information necessary for transitional refactoring
+    /**
+     * Returns CTM and clip bounds, translated from canvas coordinates to top layer coordinates.
+     */
+    void temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds);
+
 protected:
     /** After calling saveLayer(), there can be any number of devices that make
         up the top-most drawing area. LayerIter can be used to iterate through
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 56a42c3..4150a9a 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -2036,6 +2036,18 @@
     }
 }
 
+void SkCanvas::temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds) {
+    SkIRect layer_bounds = this->getTopLayerBounds();
+    if (matrix) {
+        *matrix = this->getTotalMatrix();
+        matrix->preTranslate(-layer_bounds.left(), -layer_bounds.top());
+    }
+    if (clip_bounds) {
+        this->getClipDeviceBounds(clip_bounds);
+        clip_bounds->offset(-layer_bounds.left(), -layer_bounds.top());
+    }
+}
+
 //////////////////////////////////////////////////////////////////////////////
 //  These are the virtual drawing methods
 //////////////////////////////////////////////////////////////////////////////
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp
index 1f217e2..70c2c04 100644
--- a/tests/CanvasTest.cpp
+++ b/tests/CanvasTest.cpp
@@ -18,6 +18,7 @@
  *      function of the form:
  *
  *          static void MyTestStepFunction(SkCanvas* canvas,
+ *                                         const TestData& d,
  *                                         skiatest::Reporter* reporter,
  *                                         CanvasTestStep* testStep)
  *          {
@@ -498,6 +499,45 @@
 }
 TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
 
+static void DescribeTopLayerTestStep(SkCanvas* canvas,
+                                     const TestData& d,
+                                     skiatest::Reporter* reporter,
+                                     CanvasTestStep* testStep) {
+    SkMatrix m;
+    SkIRect r;
+    // NOTE: adjustToTopLayer() does *not* reduce the clip size, even if the canvas
+    // is smaller than 10x10!
+
+    canvas->temporary_internal_describeTopLayer(&m, &r);
+    REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
+    REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
+                            testStep->assertMessage());
+
+    // Putting a full-canvas layer on it should make no change to the results.
+    SkRect layerBounds = SkRect::MakeXYWH(0.f, 0.f, 10.f, 10.f);
+    canvas->saveLayer(layerBounds, nullptr);
+    canvas->temporary_internal_describeTopLayer(&m, &r);
+    REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
+    REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
+                            testStep->assertMessage());
+    canvas->restore();
+
+    // Adding a translated layer translates the results.
+    // Default canvas is only 2x2, so can't offset our layer by very much at all;
+    // saveLayer() aborts if the bounds don't intersect.
+    layerBounds = SkRect::MakeXYWH(1.f, 1.f, 6.f, 6.f);
+    canvas->saveLayer(layerBounds, nullptr);
+    canvas->temporary_internal_describeTopLayer(&m, &r);
+    REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f),
+                            testStep->assertMessage());
+    REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1),
+                            testStep->assertMessage());
+    canvas->restore();
+
+}
+TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep);
+
+
 class CanvasTestingAccess {
 public:
     static bool SameState(const SkCanvas* canvas1, const SkCanvas* canvas2) {