Skip to content

Commit

Permalink
SubtitleGfxView: avoid recycled bitmaps
Browse files Browse the repository at this point in the history
avoid allocs in the onDraw
  • Loading branch information
courville committed Sep 13, 2024
1 parent 7aafa62 commit c6e8016
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class SubtitleGfxView extends View {
private Context mContext;
private int mScreenDpi;

private int[] location = new int[2]; // Preallocate the array
private Bitmap mScaledBitmap; // Preallocate the Bitmap

private Surface mExternalSurface = null;
// Subtitle size is multiplied with a ratio, Range to be set here
private static final double RATIO_MODIFIER_MIN = 0.5;
Expand Down Expand Up @@ -119,12 +122,12 @@ public void setSubtitle(Bitmap bitmap, int originalWidth, int originalHeight) {
double ratio;
if (mOriginalWidth > mOriginalHeight) {
// Original size = landscape => compare the longest sizes
int longestDisplaySize = (mDisplayWidth > mDisplayHeight) ? mDisplayWidth : mDisplayHeight;
int longestDisplaySize = Math.max(mDisplayWidth, mDisplayHeight);
ratio = longestDisplaySize / (float) mOriginalWidth;
}
else {
// Original size = portrait => compare the shortest sizes
int shortestDisplaySize = (mDisplayWidth > mDisplayHeight) ? mDisplayHeight : mDisplayWidth;
int shortestDisplaySize = Math.min(mDisplayWidth, mDisplayHeight);
ratio = shortestDisplaySize / (float) mOriginalWidth;
}

Expand All @@ -139,6 +142,9 @@ public void setSubtitle(Bitmap bitmap, int originalWidth, int originalHeight) {
mDrawHeight = (int) (mBitmap.getHeight() * ratio);
mDrawX = (mDisplayWidth - mDrawWidth) / 2;

// Initialize the preallocated Bitmap
mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, mDrawWidth, mDrawHeight, true);

setVisibility(View.VISIBLE);
requestLayout();
// need to Invalidate to force a refresh of this view
Expand Down Expand Up @@ -177,28 +183,25 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
if (mBitmap != null && !mBitmap.isRecycled()) {
Canvas c = canvas;
Rect r = new Rect();
if (mExternalSurface != null) {
try {
Rect r = new Rect();
r = canvas.getClipBounds();
int [] location = new int[2];
canvas.getClipBounds(r);
getLocationOnScreen(location);
r.offsetTo(location[0],location[1]);
r.offsetTo(location[0], location[1]);
c = mExternalSurface.lockCanvas(null);
c.save();
c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
c.clipRect(r);
c.translate(location[0],location[1]);
c.translate(location[0], location[1]);
} catch (Exception e) {
Log.d(TAG, "Can not lock canvas!!!!");
}
}

Bitmap draw = Bitmap.createScaledBitmap(mBitmap, mDrawWidth, mDrawHeight, true);
c.drawBitmap(draw, mDrawX, 0, mPaint);
draw.recycle();
if (c != canvas) {
c.restore();
mExternalSurface.unlockCanvasAndPost(c);
Expand Down

0 comments on commit c6e8016

Please sign in to comment.