Remove canvas save/translate/restore when it's not needed.

Except for decorations and ellipsis (for now).

Change-Id: I4079ff609e456fc2e3a15f0374b0bca18a318158
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291079
Commit-Queue: Julia Lavrova <jlavrova@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Mike Reed <reed@google.com>
diff --git a/modules/skparagraph/src/Decorations.cpp b/modules/skparagraph/src/Decorations.cpp
index 175fcce..512d466 100644
--- a/modules/skparagraph/src/Decorations.cpp
+++ b/modules/skparagraph/src/Decorations.cpp
@@ -7,7 +7,7 @@
 namespace textlayout {
 
 static const float kDoubleDecorationSpacing = 3.0f;
-void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkScalar shift) {
+void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkPoint offset) {
     if (textStyle.getDecorationType() == TextDecoration::kNoDecoration) {
         return;
     }
@@ -76,9 +76,6 @@
               break;
           default:break;
         }
-
-        canvas->save();
-        canvas->restore();
     }
 }
 
diff --git a/modules/skparagraph/src/Decorations.h b/modules/skparagraph/src/Decorations.h
index f2c61ab..c7cbc6f 100644
--- a/modules/skparagraph/src/Decorations.h
+++ b/modules/skparagraph/src/Decorations.h
@@ -12,7 +12,7 @@
 
 class Decorations {
     public:
-    void paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkScalar shift);
+    void paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkPoint offset);
 
     private:
 
diff --git a/modules/skparagraph/src/TextLine.cpp b/modules/skparagraph/src/TextLine.cpp
index a564cb7..cf6aacb 100644
--- a/modules/skparagraph/src/TextLine.cpp
+++ b/modules/skparagraph/src/TextLine.cpp
@@ -189,8 +189,7 @@
         boundaries.fBottom += shadowRect.fBottom;
     }
 
-    boundaries.offset(this->fOffset);         // Line offset from the beginning of the para
-    boundaries.offset(this->fShift, 0);     // Shift produced by formatting
+    boundaries.offset(this->offset());         // Line offset from the beginning of the para
     boundaries.offset(0, this->baseline()); // Down by baseline
 
     return boundaries;
@@ -201,9 +200,6 @@
         return;
     }
 
-    textCanvas->save();
-    textCanvas->translate(this->offset().fX, this->offset().fY);
-
     if (fHasBackground) {
         this->iterateThroughVisualRuns(false,
             [textCanvas, this]
@@ -257,8 +253,6 @@
                 return true;
         });
     }
-
-    textCanvas->restore();
 }
 
 void TextLine::format(TextAlign align, SkScalar maxWidth) {
@@ -348,19 +342,21 @@
     SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + 0.5);
     SkTextBlobBuilder builder;
     context.run->copyTo(builder, SkToU32(context.pos), context.size, SkVector::Make(0, correctedBaseline));
-    canvas->save();
     if (context.clippingNeeded) {
-        canvas->clipRect(extendHeight(context));
+        canvas->save();
+        canvas->clipRect(extendHeight(context).makeOffset(this->offset()));
     }
 
-    canvas->translate(context.fTextShift, 0);
-    canvas->drawTextBlob(builder.make(), 0, 0, paint);
-    canvas->restore();
+    canvas->drawTextBlob(builder.make(), this->offset().fX + context.fTextShift, this->offset().fY, paint);
+
+    if (context.clippingNeeded) {
+        canvas->restore();
+    }
 }
 
 void TextLine::paintBackground(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const {
     if (style.hasBackground()) {
-        canvas->drawRect(context.clip, style.getBackground());
+        canvas->drawRect(context.clip.makeOffset(this->offset()), style.getBackground());
     }
 }
 
@@ -379,24 +375,28 @@
 
         SkTextBlobBuilder builder;
         context.run->copyTo(builder, context.pos, context.size, SkVector::Make(0, shiftDown));
-        canvas->save();
-        SkRect clip = context.clip;
-        clip.offset(shadow.fOffset);
+
         if (context.clippingNeeded) {
-            canvas->clipRect(extendHeight(context));
+            canvas->save();
+            SkRect clip = extendHeight(context);
+            clip.offset(this->offset());
+            canvas->clipRect(clip);
         }
-        canvas->translate(context.fTextShift, 0);
-        canvas->drawTextBlob(builder.make(), shadow.fOffset.x(), shadow.fOffset.y(), paint);
-        canvas->restore();
+        canvas->drawTextBlob(builder.make(), this->offset().fX + context.fTextShift + shadow.fOffset.x(), this->offset().fY + shadow.fOffset.y(), paint);
+
+        if (context.clippingNeeded) {
+            canvas->restore();
+        }
     }
 }
 
 void TextLine::paintDecorations(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const {
 
+    SkAutoCanvasRestore acr(canvas, true);
+    canvas->translate(this->offset().fX, this->offset().fY);
     Decorations decorations;
     SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + 0.5);
-    decorations.paint(canvas, style, context, correctedBaseline, this->fShift);
-
+    decorations.paint(canvas, style, context, correctedBaseline, this->offset());
 }
 
 void TextLine::justify(SkScalar maxWidth) {