Effect: Use Bitmap generation ID to detect changes.
This is much simpler than using protected methods that signal updates in bitmaps. PiperOrigin-RevId: 586295312
This commit is contained in:
parent
3d1d8f4439
commit
9add30e582
@ -44,7 +44,7 @@ public abstract class BitmapOverlay extends TextureOverlay {
|
||||
private final float[] flipVerticallyMatrix;
|
||||
|
||||
private int lastTextureId;
|
||||
private boolean hasUpdatedBitmapReference;
|
||||
private int lastBitmapGenerationId;
|
||||
private @Nullable Bitmap lastBitmap;
|
||||
|
||||
/* package */ BitmapOverlay() {
|
||||
@ -75,22 +75,13 @@ public abstract class BitmapOverlay extends TextureOverlay {
|
||||
return new Size(checkNotNull(lastBitmap).getWidth(), checkNotNull(lastBitmap).getHeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the cached bitmap overlay should be updated using the latest {@linkplain
|
||||
* #getBitmap bitmap}.
|
||||
*/
|
||||
protected boolean shouldInvalidateCache() {
|
||||
// Bitmap#sameAs() is documented as a slow method. Therefore, only use a reference comparison by
|
||||
// default, instead of the deeper comparison done in sameAs.
|
||||
return hasUpdatedBitmapReference;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTextureId(long presentationTimeUs) throws VideoFrameProcessingException {
|
||||
Bitmap bitmap = getBitmap(presentationTimeUs);
|
||||
hasUpdatedBitmapReference = bitmap != lastBitmap;
|
||||
if (shouldInvalidateCache()) {
|
||||
int generationId = bitmap.getGenerationId();
|
||||
if (bitmap != lastBitmap || generationId != lastBitmapGenerationId) {
|
||||
lastBitmap = bitmap;
|
||||
lastBitmapGenerationId = generationId;
|
||||
try {
|
||||
if (lastTextureId == C.INDEX_UNSET) {
|
||||
lastTextureId = GlUtil.generateTexture();
|
||||
|
@ -33,7 +33,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
*/
|
||||
@UnstableApi
|
||||
public abstract class DrawableOverlay extends BitmapOverlay {
|
||||
private boolean hasUpdatedDrawable;
|
||||
private @MonotonicNonNull Bitmap lastBitmap;
|
||||
private @MonotonicNonNull Drawable lastDrawable;
|
||||
|
||||
@ -47,22 +46,12 @@ public abstract class DrawableOverlay extends BitmapOverlay {
|
||||
*/
|
||||
public abstract Drawable getDrawable(long presentationTimeUs);
|
||||
|
||||
/**
|
||||
* Returns whether the cached drawable overlay should be updated using the latest {@linkplain
|
||||
* #getDrawable drawable}
|
||||
*/
|
||||
@Override
|
||||
protected final boolean shouldInvalidateCache() {
|
||||
return hasUpdatedDrawable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(long presentationTimeUs) {
|
||||
Drawable overlayDrawable = getDrawable(presentationTimeUs);
|
||||
// TODO(b/227625365): Drawable doesn't implement the equals method, so investigate other methods
|
||||
// of detecting the need to redraw the bitmap.
|
||||
hasUpdatedDrawable = !overlayDrawable.equals(lastDrawable);
|
||||
if (shouldInvalidateCache()) {
|
||||
if (!overlayDrawable.equals(lastDrawable)) {
|
||||
lastDrawable = overlayDrawable;
|
||||
if (lastBitmap == null
|
||||
|| lastBitmap.getWidth() != lastDrawable.getIntrinsicWidth()
|
||||
|
@ -80,8 +80,6 @@ public abstract class TextOverlay extends BitmapOverlay {
|
||||
|
||||
public static final int TEXT_SIZE_PIXELS = 100;
|
||||
|
||||
private boolean hasUpdatedText;
|
||||
|
||||
private @MonotonicNonNull Bitmap lastBitmap;
|
||||
private @MonotonicNonNull SpannableString lastText;
|
||||
|
||||
@ -92,20 +90,10 @@ public abstract class TextOverlay extends BitmapOverlay {
|
||||
*/
|
||||
public abstract SpannableString getText(long presentationTimeUs);
|
||||
|
||||
/**
|
||||
* Returns whether the cached text overlay should be updated using the latest {@linkplain #getText
|
||||
* text}
|
||||
*/
|
||||
@Override
|
||||
protected final boolean shouldInvalidateCache() {
|
||||
return hasUpdatedText;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(long presentationTimeUs) {
|
||||
SpannableString overlayText = getText(presentationTimeUs);
|
||||
hasUpdatedText = !overlayText.equals(lastText);
|
||||
if (shouldInvalidateCache()) {
|
||||
if (!overlayText.equals(lastText)) {
|
||||
lastText = overlayText;
|
||||
TextPaint textPaint = new TextPaint();
|
||||
textPaint.setTextSize(TEXT_SIZE_PIXELS);
|
||||
|
Loading…
x
Reference in New Issue
Block a user