Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Handle a case that View's size becomes empty unexpectedly
Browse files Browse the repository at this point in the history
BUG=458280

Review URL: https://codereview.chromium.org/926573002

Cr-Commit-Position: refs/heads/master@{#317889}

Review URL: https://codereview.chromium.org/989363002

Cr-Commit-Position: refs/branch-heads/2311@{#190}
Cr-Branched-From: 09b7de5-refs/heads/master@{#317474}
  • Loading branch information
Ted Choc committed Mar 10, 2015
1 parent 1bf239f commit 361f0f3
Showing 1 changed file with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
Expand Down Expand Up @@ -47,16 +48,19 @@ public ViewResourceAdapter(View view) {
public Bitmap getBitmap() {
if (!isDirty()) return mBitmap;
TraceEvent.begin("ViewResourceAdapter:getBitmap");
validateBitmap();
if (validateBitmap()) {
Canvas canvas = new Canvas(mBitmap);

Canvas canvas = new Canvas(mBitmap);
onCaptureStart(canvas, mDirtyRect.isEmpty() ? null : mDirtyRect);

onCaptureStart(canvas, mDirtyRect.isEmpty() ? null : mDirtyRect);
if (!mDirtyRect.isEmpty()) canvas.clipRect(mDirtyRect);
mView.draw(canvas);

if (!mDirtyRect.isEmpty()) canvas.clipRect(mDirtyRect);
mView.draw(canvas);

onCaptureEnd();
onCaptureEnd();
} else {
assert mBitmap.getWidth() == 1 && mBitmap.getHeight() == 1;
mBitmap.setPixel(0, 0, Color.TRANSPARENT);
}

mDirtyRect.setEmpty();
TraceEvent.end("ViewResourceAdapter:getBitmap");
Expand Down Expand Up @@ -143,22 +147,30 @@ protected void computeContentAperture(Rect outContentAperture) {
outContentAperture.set(0, 0, mView.getWidth(), mView.getHeight());
}

private void validateBitmap() {
/**
* @return Whether |mBitmap| is corresponding to |mView| or not.
*/
private boolean validateBitmap() {
int viewWidth = mView.getWidth();
int viewHeight = mView.getHeight();
boolean isEmpty = viewWidth == 0 || viewHeight == 0;
if (isEmpty) {
viewWidth = 1;
viewHeight = 1;
}
if (mBitmap != null
&& (mBitmap.getWidth() != mView.getWidth()
|| mBitmap.getHeight() != mView.getHeight())) {
&& (mBitmap.getWidth() != viewWidth || mBitmap.getHeight() != viewHeight)) {
mBitmap.recycle();
mBitmap = null;
}

if (mView.getWidth() == 0 || mView.getHeight() == 0) return;

if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(
mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
mBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
mBitmap.setHasAlpha(true);
mDirtyRect.set(0, 0, mView.getWidth(), mView.getHeight());
mDirtyRect.set(0, 0, viewWidth, viewHeight);
mBitmapSize.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
}

return !isEmpty;
}
}
}

0 comments on commit 361f0f3

Please sign in to comment.