Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 656ee7b

Browse files
RusinoSkia Commit-Bot
authored andcommitted
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 <[email protected]> Reviewed-by: Ben Wagner <[email protected]> Reviewed-by: Mike Reed <[email protected]>
1 parent 04283f3 commit 656ee7b

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

modules/skparagraph/src/Decorations.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace skia {
77
namespace textlayout {
88

99
static const float kDoubleDecorationSpacing = 3.0f;
10-
void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkScalar shift) {
10+
void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkPoint offset) {
1111
if (textStyle.getDecorationType() == TextDecoration::kNoDecoration) {
1212
return;
1313
}
@@ -76,9 +76,6 @@ void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const Text
7676
break;
7777
default:break;
7878
}
79-
80-
canvas->save();
81-
canvas->restore();
8279
}
8380
}
8481

modules/skparagraph/src/Decorations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace textlayout {
1212

1313
class Decorations {
1414
public:
15-
void paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkScalar shift);
15+
void paint(SkCanvas* canvas, const TextStyle& textStyle, const TextLine::ClipContext& context, SkScalar baseline, SkPoint offset);
1616

1717
private:
1818

modules/skparagraph/src/TextLine.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ SkRect TextLine::calculateBoundaries() {
189189
boundaries.fBottom += shadowRect.fBottom;
190190
}
191191

192-
boundaries.offset(this->fOffset); // Line offset from the beginning of the para
193-
boundaries.offset(this->fShift, 0); // Shift produced by formatting
192+
boundaries.offset(this->offset()); // Line offset from the beginning of the para
194193
boundaries.offset(0, this->baseline()); // Down by baseline
195194

196195
return boundaries;
@@ -201,9 +200,6 @@ void TextLine::paint(SkCanvas* textCanvas) {
201200
return;
202201
}
203202

204-
textCanvas->save();
205-
textCanvas->translate(this->offset().fX, this->offset().fY);
206-
207203
if (fHasBackground) {
208204
this->iterateThroughVisualRuns(false,
209205
[textCanvas, this]
@@ -257,8 +253,6 @@ void TextLine::paint(SkCanvas* textCanvas) {
257253
return true;
258254
});
259255
}
260-
261-
textCanvas->restore();
262256
}
263257

264258
void TextLine::format(TextAlign align, SkScalar maxWidth) {
@@ -348,19 +342,21 @@ void TextLine::paintText(SkCanvas* canvas, TextRange textRange, const TextStyle&
348342
SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + 0.5);
349343
SkTextBlobBuilder builder;
350344
context.run->copyTo(builder, SkToU32(context.pos), context.size, SkVector::Make(0, correctedBaseline));
351-
canvas->save();
352345
if (context.clippingNeeded) {
353-
canvas->clipRect(extendHeight(context));
346+
canvas->save();
347+
canvas->clipRect(extendHeight(context).makeOffset(this->offset()));
354348
}
355349

356-
canvas->translate(context.fTextShift, 0);
357-
canvas->drawTextBlob(builder.make(), 0, 0, paint);
358-
canvas->restore();
350+
canvas->drawTextBlob(builder.make(), this->offset().fX + context.fTextShift, this->offset().fY, paint);
351+
352+
if (context.clippingNeeded) {
353+
canvas->restore();
354+
}
359355
}
360356

361357
void TextLine::paintBackground(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const {
362358
if (style.hasBackground()) {
363-
canvas->drawRect(context.clip, style.getBackground());
359+
canvas->drawRect(context.clip.makeOffset(this->offset()), style.getBackground());
364360
}
365361
}
366362

@@ -379,24 +375,28 @@ void TextLine::paintShadow(SkCanvas* canvas, TextRange textRange, const TextStyl
379375

380376
SkTextBlobBuilder builder;
381377
context.run->copyTo(builder, context.pos, context.size, SkVector::Make(0, shiftDown));
382-
canvas->save();
383-
SkRect clip = context.clip;
384-
clip.offset(shadow.fOffset);
378+
385379
if (context.clippingNeeded) {
386-
canvas->clipRect(extendHeight(context));
380+
canvas->save();
381+
SkRect clip = extendHeight(context);
382+
clip.offset(this->offset());
383+
canvas->clipRect(clip);
384+
}
385+
canvas->drawTextBlob(builder.make(), this->offset().fX + context.fTextShift + shadow.fOffset.x(), this->offset().fY + shadow.fOffset.y(), paint);
386+
387+
if (context.clippingNeeded) {
388+
canvas->restore();
387389
}
388-
canvas->translate(context.fTextShift, 0);
389-
canvas->drawTextBlob(builder.make(), shadow.fOffset.x(), shadow.fOffset.y(), paint);
390-
canvas->restore();
391390
}
392391
}
393392

394393
void TextLine::paintDecorations(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const {
395394

395+
SkAutoCanvasRestore acr(canvas, true);
396+
canvas->translate(this->offset().fX, this->offset().fY);
396397
Decorations decorations;
397398
SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + 0.5);
398-
decorations.paint(canvas, style, context, correctedBaseline, this->fShift);
399-
399+
decorations.paint(canvas, style, context, correctedBaseline, this->offset());
400400
}
401401

402402
void TextLine::justify(SkScalar maxWidth) {

0 commit comments

Comments
 (0)