Commit 8cef6682 authored by Wei-Yin Chen (陳威尹)'s avatar Wei-Yin Chen (陳威尹) Committed by Commit Bot

Reland "Support downsampling in ViewResourceAdapter"

This is a reland of 2bc512d7

Original change's description:
> Support downsampling in ViewResourceAdapter
>
> Bug: 965609
> Change-Id: I148d5fe8b7371a529b612ed2e9b407ced0062f82
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1623139
> Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
> Reviewed-by: David Trainor <dtrainor@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#662915}

Bug: 965609, 966859
TBR=dtrainor@chromium.org

Change-Id: I3da718de48413f22060f7504c78932d941929308
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1635431Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664615}
parent 2f3cd109
...@@ -138,8 +138,9 @@ public class ResourceManager implements ResourceLoaderCallback { ...@@ -138,8 +138,9 @@ public class ResourceManager implements ResourceLoaderCallback {
if (mNativeResourceManagerPtr == 0) return; if (mNativeResourceManagerPtr == 0) return;
nativeOnResourceReady( nativeOnResourceReady(mNativeResourceManagerPtr, resType, resId, bitmap,
mNativeResourceManagerPtr, resType, resId, bitmap, resource.createNativeResource()); resource.getBitmapSize().width(), resource.getBitmapSize().height(),
resource.createNativeResource());
} }
@Override @Override
...@@ -195,7 +196,7 @@ public class ResourceManager implements ResourceLoaderCallback { ...@@ -195,7 +196,7 @@ public class ResourceManager implements ResourceLoaderCallback {
} }
private native void nativeOnResourceReady(long nativeResourceManagerImpl, int resType, private native void nativeOnResourceReady(long nativeResourceManagerImpl, int resType,
int resId, Bitmap bitmap, long nativeResource); int resId, Bitmap bitmap, int width, int height, long nativeResource);
private native void nativeRemoveResource(long nativeResourceManagerImpl, int resType, private native void nativeRemoveResource(long nativeResourceManagerImpl, int resType,
int resId); int resId);
private native void nativeClearTintedResourceCache(long nativeResourceManagerImpl); private native void nativeClearTintedResourceCache(long nativeResourceManagerImpl);
......
...@@ -27,7 +27,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan ...@@ -27,7 +27,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan
private final Rect mDirtyRect = new Rect(); private final Rect mDirtyRect = new Rect();
private Bitmap mBitmap; private Bitmap mBitmap;
private Rect mBitmapSize = new Rect(); private Rect mViewSize = new Rect();
protected float mScale = 1;
/** /**
* Builds a {@link ViewResourceAdapter} instance around {@code view}. * Builds a {@link ViewResourceAdapter} instance around {@code view}.
...@@ -71,7 +72,19 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan ...@@ -71,7 +72,19 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan
@Override @Override
public Rect getBitmapSize() { public Rect getBitmapSize() {
return mBitmapSize; return mViewSize;
}
/**
* Set the downsampling scale. The rendered size is not affected.
* @param scale The scale to use. <1 means the Bitmap is smaller than the View.
*/
public void setDownsamplingScale(float scale) {
assert scale <= 1;
if (mScale != scale) {
invalidate(null);
}
mScale = scale;
} }
/** /**
...@@ -143,7 +156,10 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan ...@@ -143,7 +156,10 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan
* @param canvas The {@link Canvas} that will be drawn to. * @param canvas The {@link Canvas} that will be drawn to.
*/ */
protected void capture(Canvas canvas) { protected void capture(Canvas canvas) {
canvas.save();
canvas.scale(mScale, mScale);
mView.draw(canvas); mView.draw(canvas);
canvas.restore();
} }
/** /**
...@@ -156,8 +172,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan ...@@ -156,8 +172,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan
* @return Whether |mBitmap| is corresponding to |mView| or not. * @return Whether |mBitmap| is corresponding to |mView| or not.
*/ */
private boolean validateBitmap() { private boolean validateBitmap() {
int viewWidth = mView.getWidth(); int viewWidth = (int) (mView.getWidth() * mScale);
int viewHeight = mView.getHeight(); int viewHeight = (int) (mView.getHeight() * mScale);
boolean isEmpty = viewWidth == 0 || viewHeight == 0; boolean isEmpty = viewWidth == 0 || viewHeight == 0;
if (isEmpty) { if (isEmpty) {
viewWidth = 1; viewWidth = 1;
...@@ -172,8 +188,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan ...@@ -172,8 +188,8 @@ public class ViewResourceAdapter extends DynamicResource implements OnLayoutChan
if (mBitmap == null) { if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888); mBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
mBitmap.setHasAlpha(true); mBitmap.setHasAlpha(true);
mDirtyRect.set(0, 0, viewWidth, viewHeight); mViewSize.set(0, 0, mView.getWidth(), mView.getHeight());
mBitmapSize.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); mDirtyRect.set(mViewSize);
} }
return !isEmpty; return !isEmpty;
......
...@@ -78,6 +78,19 @@ public class ViewResourceAdapterTest { ...@@ -78,6 +78,19 @@ public class ViewResourceAdapterTest {
assertEquals(bitmap.getHeight(), rect.height()); assertEquals(bitmap.getHeight(), rect.height());
} }
@Test
public void testSetDownsamplingSize() {
float scale = 0.5f;
mAdapter.setDownsamplingScale(scale);
Bitmap bitmap = mAdapter.getBitmap();
assertEquals(mViewWidth * scale, bitmap.getWidth(), 1);
assertEquals(mViewHeight * scale, bitmap.getHeight(), 1);
Rect rect = mAdapter.getBitmapSize();
assertEquals(mViewWidth, rect.width());
assertEquals(mViewHeight, rect.height());
}
@Test @Test
public void testIsDirty() { public void testIsDirty() {
assertTrue(mAdapter.isDirty()); assertTrue(mAdapter.isDirty());
...@@ -99,6 +112,21 @@ public class ViewResourceAdapterTest { ...@@ -99,6 +112,21 @@ public class ViewResourceAdapterTest {
assertEquals(2, dirtyRect.height()); assertEquals(2, dirtyRect.height());
} }
@Test
public void testOnLayoutChangeDownsampled() {
mAdapter.setDownsamplingScale(0.5f);
mAdapter.getBitmap();
assertFalse(mAdapter.isDirty());
mAdapter.onLayoutChange(mView, 0, 0, 1, 2, 0, 0, mViewWidth, mViewHeight);
assertTrue(mAdapter.isDirty());
Rect dirtyRect = mAdapter.getDirtyRect();
assertEquals(1, dirtyRect.width());
assertEquals(2, dirtyRect.height());
}
@Test @Test
public void testInvalidate() { public void testInvalidate() {
mAdapter.getBitmap(); mAdapter.getBitmap();
...@@ -123,6 +151,19 @@ public class ViewResourceAdapterTest { ...@@ -123,6 +151,19 @@ public class ViewResourceAdapterTest {
assertEquals(dirtyRect.toString(), mAdapter.getDirtyRect().toString()); assertEquals(dirtyRect.toString(), mAdapter.getDirtyRect().toString());
} }
@Test
public void testInvalidateRectDownsampled() {
mAdapter.setDownsamplingScale(0.5f);
mAdapter.getBitmap();
assertFalse(mAdapter.isDirty());
Rect dirtyRect = new Rect(1, 2, 3, 4);
mAdapter.invalidate(dirtyRect);
assertTrue(mAdapter.isDirty());
assertEquals(dirtyRect.toString(), mAdapter.getDirtyRect().toString());
}
@Test @Test
public void testInvalidateRectUnion() { public void testInvalidateRectUnion() {
mAdapter.getBitmap(); mAdapter.getBitmap();
...@@ -217,4 +258,18 @@ public class ViewResourceAdapterTest { ...@@ -217,4 +258,18 @@ public class ViewResourceAdapterTest {
assertEquals(mViewWidth, rect.width()); assertEquals(mViewWidth, rect.width());
assertEquals(mViewHeight, rect.height()); assertEquals(mViewHeight, rect.height());
} }
@Test
public void testGetDirtyRectDownsampled() {
mAdapter.setDownsamplingScale(0.5f);
mAdapter.getBitmap();
Rect rect = mAdapter.getDirtyRect();
assertTrue(rect.isEmpty());
mAdapter.invalidate(null);
rect = mAdapter.getDirtyRect();
assertEquals(mViewWidth, rect.width());
assertEquals(mViewHeight, rect.height());
}
} }
...@@ -205,6 +205,8 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env, ...@@ -205,6 +205,8 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env,
jint res_type, jint res_type,
jint res_id, jint res_id,
const JavaRef<jobject>& bitmap, const JavaRef<jobject>& bitmap,
jint width,
jint height,
jlong native_resource) { jlong native_resource) {
DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST); DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST); DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
...@@ -222,7 +224,7 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env, ...@@ -222,7 +224,7 @@ void ResourceManagerImpl::OnResourceReady(JNIEnv* env,
resource->SetUIResource( resource->SetUIResource(
cc::ScopedUIResource::Create(ui_resource_manager_, cc::ScopedUIResource::Create(ui_resource_manager_,
cc::UIResourceBitmap(skbitmap)), cc::UIResourceBitmap(skbitmap)),
jbitmap.size()); gfx::Size(width, height));
} }
void ResourceManagerImpl::RemoveResource( void ResourceManagerImpl::RemoveResource(
......
...@@ -49,6 +49,8 @@ class UI_ANDROID_EXPORT ResourceManagerImpl ...@@ -49,6 +49,8 @@ class UI_ANDROID_EXPORT ResourceManagerImpl
jint res_type, jint res_type,
jint res_id, jint res_id,
const base::android::JavaRef<jobject>& bitmap, const base::android::JavaRef<jobject>& bitmap,
jint width,
jint height,
jlong native_resource); jlong native_resource);
void RemoveResource( void RemoveResource(
JNIEnv* env, JNIEnv* env,
......
...@@ -55,7 +55,7 @@ class TestResourceManagerImpl : public ResourceManagerImpl { ...@@ -55,7 +55,7 @@ class TestResourceManagerImpl : public ResourceManagerImpl {
small_bitmap.setImmutable(); small_bitmap.setImmutable();
OnResourceReady(nullptr, nullptr, res_type, res_id, OnResourceReady(nullptr, nullptr, res_type, res_id,
gfx::ConvertToJavaBitmap(&small_bitmap), gfx::ConvertToJavaBitmap(&small_bitmap), 1, 1,
reinterpret_cast<intptr_t>(new Resource())); reinterpret_cast<intptr_t>(new Resource()));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment