Skip to content

Commit

Permalink
Remove canvas save/translate/restore when it's not needed.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Rusino authored and Skia Commit-Bot committed May 20, 2020
1 parent 04283f3 commit 656ee7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
5 changes: 1 addition & 4 deletions modules/skparagraph/src/Decorations.cpp
Expand Up @@ -7,7 +7,7 @@ namespace skia {
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;
}
Expand Down Expand Up @@ -76,9 +76,6 @@ void Decorations::paint(SkCanvas* canvas, const TextStyle& textStyle, const Text
break;
default:break;
}

canvas->save();
canvas->restore();
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/skparagraph/src/Decorations.h
Expand Up @@ -12,7 +12,7 @@ namespace textlayout {

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:

Expand Down
44 changes: 22 additions & 22 deletions modules/skparagraph/src/TextLine.cpp
Expand Up @@ -189,8 +189,7 @@ SkRect TextLine::calculateBoundaries() {
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;
Expand All @@ -201,9 +200,6 @@ void TextLine::paint(SkCanvas* textCanvas) {
return;
}

textCanvas->save();
textCanvas->translate(this->offset().fX, this->offset().fY);

if (fHasBackground) {
this->iterateThroughVisualRuns(false,
[textCanvas, this]
Expand Down Expand Up @@ -257,8 +253,6 @@ void TextLine::paint(SkCanvas* textCanvas) {
return true;
});
}

textCanvas->restore();
}

void TextLine::format(TextAlign align, SkScalar maxWidth) {
Expand Down Expand Up @@ -348,19 +342,21 @@ void TextLine::paintText(SkCanvas* canvas, TextRange textRange, const TextStyle&
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());
}
}

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

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->drawTextBlob(builder.make(), this->offset().fX + context.fTextShift + shadow.fOffset.x(), this->offset().fY + shadow.fOffset.y(), paint);

if (context.clippingNeeded) {
canvas->restore();
}
canvas->translate(context.fTextShift, 0);
canvas->drawTextBlob(builder.make(), shadow.fOffset.x(), shadow.fOffset.y(), paint);
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) {
Expand Down

0 comments on commit 656ee7b

Please sign in to comment.