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
This commit is contained in:
tonihei 2017-04-13 08:01:20 -07:00 committed by Oliver Woodman
parent 12433ad146
commit 579d57b445

View file

@ -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);