From 579d57b445400a6c32d2db31f2b315c494f10769 Mon Sep 17 00:00:00 2001 From: tonihei Date: Thu, 13 Apr 2017 08:01:20 -0700 Subject: [PATCH] Less memory allocations for repeated calls to SubtitleView.draw() The draw method checks if all inputs are the same as in the previous call. The options to strip the subtitle of its styles were applied each time, in order to check if the final CharSequence is still the same. This additional computation (and memory allocations) can be prevented by checking if the original CharSequence (including all styles) and the flags to remove those styles are the same. The actual style removal is now part of setupTextlayout(). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=153061064 --- .../exoplayer2/ui/SubtitlePainter.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java index b75ac2b990..b6cfc9a6f3 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java @@ -153,8 +153,6 @@ import com.google.android.exoplayer2.util.Util; CaptionStyleCompat style, float textSizePx, float bottomPaddingFraction, Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) { boolean isTextCue = cue.bitmap == null; - CharSequence cueText = null; - Bitmap cueBitmap = null; int windowColor = Color.BLACK; if (isTextCue) { if (TextUtils.isEmpty(cue.text)) { @@ -163,30 +161,10 @@ import com.google.android.exoplayer2.util.Util; } windowColor = (cue.windowColorSet && applyEmbeddedStyles) ? cue.windowColor : style.windowColor; - // Remove embedded styling or font size if requested. - if (applyEmbeddedFontSizes && applyEmbeddedStyles) { - cueText = cue.text; - } else if (!applyEmbeddedStyles) { - cueText = cue.text.toString(); // Equivalent to erasing all spans. - } else { - SpannableStringBuilder newCueText = new SpannableStringBuilder(cue.text); - int cueLength = newCueText.length(); - AbsoluteSizeSpan[] absSpans = newCueText.getSpans(0, cueLength, AbsoluteSizeSpan.class); - RelativeSizeSpan[] relSpans = newCueText.getSpans(0, cueLength, RelativeSizeSpan.class); - for (AbsoluteSizeSpan absSpan : absSpans) { - newCueText.removeSpan(absSpan); - } - for (RelativeSizeSpan relSpan : relSpans) { - newCueText.removeSpan(relSpan); - } - cueText = newCueText; - } - } else { - cueBitmap = cue.bitmap; } - if (areCharSequencesEqual(this.cueText, cueText) + if (areCharSequencesEqual(this.cueText, cue.text) && Util.areEqual(this.cueTextAlignment, cue.textAlignment) - && this.cueBitmap == cueBitmap + && this.cueBitmap == cue.bitmap && this.cueLine == cue.line && this.cueLineType == cue.lineType && Util.areEqual(this.cueLineAnchor, cue.lineAnchor) @@ -213,9 +191,9 @@ import com.google.android.exoplayer2.util.Util; return; } - this.cueText = cueText; + this.cueText = cue.text; this.cueTextAlignment = cue.textAlignment; - this.cueBitmap = cueBitmap; + this.cueBitmap = cue.bitmap; this.cueLine = cue.line; this.cueLineType = cue.lineType; this.cueLineAnchor = cue.lineAnchor; @@ -262,6 +240,26 @@ import com.google.android.exoplayer2.util.Util; return; } + // Remove embedded styling or font size if requested. + CharSequence cueText; + if (applyEmbeddedFontSizes && applyEmbeddedStyles) { + cueText = this.cueText; + } else if (!applyEmbeddedStyles) { + cueText = this.cueText.toString(); // Equivalent to erasing all spans. + } else { + SpannableStringBuilder newCueText = new SpannableStringBuilder(this.cueText); + int cueLength = newCueText.length(); + AbsoluteSizeSpan[] absSpans = newCueText.getSpans(0, cueLength, AbsoluteSizeSpan.class); + RelativeSizeSpan[] relSpans = newCueText.getSpans(0, cueLength, RelativeSizeSpan.class); + for (AbsoluteSizeSpan absSpan : absSpans) { + newCueText.removeSpan(absSpan); + } + for (RelativeSizeSpan relSpan : relSpans) { + newCueText.removeSpan(relSpan); + } + cueText = newCueText; + } + Alignment textAlignment = cueTextAlignment == null ? Alignment.ALIGN_CENTER : cueTextAlignment; textLayout = new StaticLayout(cueText, textPaint, availableWidth, textAlignment, spacingMult, spacingAdd, true);