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:
huangdarwin 2023-11-29 04:27:22 -08:00 committed by Copybara-Service
parent 3d1d8f4439
commit 9add30e582
3 changed files with 6 additions and 38 deletions

View File

@ -44,7 +44,7 @@ public abstract class BitmapOverlay extends TextureOverlay {
private final float[] flipVerticallyMatrix; private final float[] flipVerticallyMatrix;
private int lastTextureId; private int lastTextureId;
private boolean hasUpdatedBitmapReference; private int lastBitmapGenerationId;
private @Nullable Bitmap lastBitmap; private @Nullable Bitmap lastBitmap;
/* package */ BitmapOverlay() { /* package */ BitmapOverlay() {
@ -75,22 +75,13 @@ public abstract class BitmapOverlay extends TextureOverlay {
return new Size(checkNotNull(lastBitmap).getWidth(), checkNotNull(lastBitmap).getHeight()); 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 @Override
public int getTextureId(long presentationTimeUs) throws VideoFrameProcessingException { public int getTextureId(long presentationTimeUs) throws VideoFrameProcessingException {
Bitmap bitmap = getBitmap(presentationTimeUs); Bitmap bitmap = getBitmap(presentationTimeUs);
hasUpdatedBitmapReference = bitmap != lastBitmap; int generationId = bitmap.getGenerationId();
if (shouldInvalidateCache()) { if (bitmap != lastBitmap || generationId != lastBitmapGenerationId) {
lastBitmap = bitmap; lastBitmap = bitmap;
lastBitmapGenerationId = generationId;
try { try {
if (lastTextureId == C.INDEX_UNSET) { if (lastTextureId == C.INDEX_UNSET) {
lastTextureId = GlUtil.generateTexture(); lastTextureId = GlUtil.generateTexture();

View File

@ -33,7 +33,6 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
*/ */
@UnstableApi @UnstableApi
public abstract class DrawableOverlay extends BitmapOverlay { public abstract class DrawableOverlay extends BitmapOverlay {
private boolean hasUpdatedDrawable;
private @MonotonicNonNull Bitmap lastBitmap; private @MonotonicNonNull Bitmap lastBitmap;
private @MonotonicNonNull Drawable lastDrawable; private @MonotonicNonNull Drawable lastDrawable;
@ -47,22 +46,12 @@ public abstract class DrawableOverlay extends BitmapOverlay {
*/ */
public abstract Drawable getDrawable(long presentationTimeUs); 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 @Override
public Bitmap getBitmap(long presentationTimeUs) { public Bitmap getBitmap(long presentationTimeUs) {
Drawable overlayDrawable = getDrawable(presentationTimeUs); Drawable overlayDrawable = getDrawable(presentationTimeUs);
// TODO(b/227625365): Drawable doesn't implement the equals method, so investigate other methods // TODO(b/227625365): Drawable doesn't implement the equals method, so investigate other methods
// of detecting the need to redraw the bitmap. // of detecting the need to redraw the bitmap.
hasUpdatedDrawable = !overlayDrawable.equals(lastDrawable); if (!overlayDrawable.equals(lastDrawable)) {
if (shouldInvalidateCache()) {
lastDrawable = overlayDrawable; lastDrawable = overlayDrawable;
if (lastBitmap == null if (lastBitmap == null
|| lastBitmap.getWidth() != lastDrawable.getIntrinsicWidth() || lastBitmap.getWidth() != lastDrawable.getIntrinsicWidth()

View File

@ -80,8 +80,6 @@ public abstract class TextOverlay extends BitmapOverlay {
public static final int TEXT_SIZE_PIXELS = 100; public static final int TEXT_SIZE_PIXELS = 100;
private boolean hasUpdatedText;
private @MonotonicNonNull Bitmap lastBitmap; private @MonotonicNonNull Bitmap lastBitmap;
private @MonotonicNonNull SpannableString lastText; private @MonotonicNonNull SpannableString lastText;
@ -92,20 +90,10 @@ public abstract class TextOverlay extends BitmapOverlay {
*/ */
public abstract SpannableString getText(long presentationTimeUs); 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 @Override
public Bitmap getBitmap(long presentationTimeUs) { public Bitmap getBitmap(long presentationTimeUs) {
SpannableString overlayText = getText(presentationTimeUs); SpannableString overlayText = getText(presentationTimeUs);
hasUpdatedText = !overlayText.equals(lastText); if (!overlayText.equals(lastText)) {
if (shouldInvalidateCache()) {
lastText = overlayText; lastText = overlayText;
TextPaint textPaint = new TextPaint(); TextPaint textPaint = new TextPaint();
textPaint.setTextSize(TEXT_SIZE_PIXELS); textPaint.setTextSize(TEXT_SIZE_PIXELS);